My knowledge of computer graphics, especially image formats and the like, is extremely limited so I may be using terms in an incorrect context.
Please ask for clarifications if you require them.
My java application uses a third-party DLL to print images on a printer device. The image to be printed must be a black-and-white TIFF image.
My java application obtains images via different methods, including retrieving an image stored as a BLOB in an Oracle database. The image may be a color image or a grayscale image or a black-and-white (i.e. binary) image and may not be in TIFF format, for example it may be a JPEG image.
How can I determine whether the image is black-and-white ?
How can I determine if the image is in TIFF format ?
How can I convert an image to TIFF format if it is not ?
How can I convert an image to black-and-white if it is not ?
Note that the current code -- which is legacy code that was not originally written by me -- uses JAI and the image is an instance of class
Thanks (in advance),
Avi.
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2329
posted
0
javax.jai.media.PlanarImage is a java.awt.image.RenderedImage - so you can get its Raster object, and from that you have access to its pixel data. So:
How can I determine whether the image is black-and-white ?
An image is B&W if all its pixels are either black or white, which means their individual R, G and B components all have values of 0, or all have values of 255.
How can I determine if the image is in TIFF format ?
TIFF is a file format, but a PlanarImage is an in-memory representation, so this question does not apply.
How can I convert an image to TIFF format if it is not ?
I generally use the javax.imageio.ImageIO class to load and store images. It doesn't support TIFF out of the box, but can be made to do so easily. Search for the JAI-ImageIO package. I'm not sure where binary builds can be found, since the screwed-up move from java.net to the Kenai infrastructure lots of projects there are broken.
How can I convert an image to black-and-white if it is not ?
Devise an algorithm that reads all pixel values and -based on their R, G and B components- decides whether each pixel should be black or white.
You'll have to do some reading and learning about image processing before you're ready to tackle these, if you have never done any of this.
Also, in the long term, I advise to move away from JAI; it's been dead for years now.
I think that instead of black-and-white Avi actually means grayscale. If that is the case Wikipedia suggests the following calculation:
This calculation of gray is also what javax.swing.GrayFilter uses, although it increases or decreases the gray factor a bit afterwards.