int pixel = image.getRGB(j % pixelWidth, j / pixelHeight);
int pixel = image.getRGB(j % pixelWidth, j / pixelWidth);
	private void writePostScriptImage(BufferedImage image, double x, double y,		double width, double height, double rotation)		throws IOException, MapyrusException	{		int j;		int pixelWidth, pixelHeight;		pixelWidth = image.getWidth(null);		pixelHeight = image.getHeight(null);		/*		 * Check if image is a single color.		 * Draw single color images with transparent background		 * using PostScript 'imagemask' operator.		 * Draw other images as RGB images using 'image' operator.		 */		Color singleColor = getSingleImageColor(image);		/*		 * Write PostScript image directionary entry to draw image.		 * Taken from Adobe PostScript Language Reference Manual		 * (2nd Edition), p. 234.		 */		writePostScriptLine("gs");		writePostScriptLine("/DeviceRGB setcolorspace");		writePostScriptLine(x + " " + y + " translate");		writePostScriptLine(rotation + " radtodeg rotate");		writePostScriptLine(width + " " + height + " scale");		/*		 * Image is centred at each point.		 * Shift image left and down half it's size so that it is displayed centred.		 */		writePostScriptLine("-0.5 -0.5 translate");		/*		 * Set color for drawing single color images.		 */		if (singleColor != null)		{			float []c = singleColor.getColorComponents(null);			writePostScriptLine(c[0] + " " + c[1] + " " + c[2] + " rgb");		}		writePostScriptLine("<<");		writePostScriptLine("/ImageType 1");		writePostScriptLine("/Width " + pixelWidth);		writePostScriptLine("/Height " + pixelHeight);		if (singleColor != null)		{			writePostScriptLine("/BitsPerComponent 1");			writePostScriptLine("/Decode [0 1]");		}		else		{			writePostScriptLine("/BitsPerComponent 8");			writePostScriptLine("/Decode [0 1 0 1 0 1]");		}		writePostScriptLine("/ImageMatrix [" + pixelWidth + " 0 0 " +			-pixelHeight + " 0 " + pixelHeight + "]");		writePostScriptLine("/DataSource currentfile /ASCII85Decode filter");		writePostScriptLine(">>");		if (singleColor != null)			writePostScriptLine("imagemask");		else			writePostScriptLine("image");		/*		 * Write ASCII85 encoded string containing all pixel values.		 */		ASCII85Writer ascii85 = new ASCII85Writer(mWriter);		int byteValue = 0;		int bitCounter = 0;		for (j = 0; j < pixelWidth * pixelHeight; j++)		{			int pixel = image.getRGB(j % pixelWidth, j / pixelHeight);			if (singleColor != null)			{				/*				 * Pixel is set in PostScript image if it is transparent.				 */				int nextBit = ((pixel >> 24) == 0) ? 1 : 0;				/*				 * Store next pixel value as a single bit in a byte.				 * If we've completed a byte or reached the end of a row				 * then write byte out and begin next byte.				 */				nextBit <<= (7 - bitCounter);				byteValue |= nextBit;				bitCounter++;				if (bitCounter == 8 || (j + 1) % pixelWidth == 0)				{					ascii85.write(byteValue);					byteValue = bitCounter = 0;				}			}			else			{				/*				 * Ignore transparency, we want only red, green, blue components				 * of pixel.				 */				int blue = (pixel & 0xff);				int green = ((pixel >> 8) & 0xff);				int red = ((pixel >> 16) & 0xff);				ascii85.write(red);				ascii85.write(green);				ascii85.write(blue);			}		}		ascii85.close();		/*		 * Write ASCII85 end-of-data marker.		 */		writePostScriptLine("~>");		writePostScriptLine("gr");	}