File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Java Advanced Imaging (JAI) Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Advanced Imaging (JAI) Question" Watch "Java Advanced Imaging (JAI) Question" New topic
Author

Java Advanced Imaging (JAI) Question

Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1125

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: 2408
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1125

Tim,
Thank you for your extremely helpful reply. In it you wrote:
I advise to move away from JAI

What alternative do you recommend?
You also wrote:
You'll have to do some reading and learning about image processing before you're ready to tackle these

Please provide some recommended resources for learning about image processing.

Rob,
You wrote in your reply:
I think that instead of black-and-white Avi actually means grayscale.

No, I actually meant black-and-white. :-)

Cheers,
Avi.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Avi Abrami wrote:
I advise to move away from JAI

What alternative do you recommend?

I'm not up on Java image processing libraries; one that I have used to good success and which is still under active development is ImageJ.

Please provide some recommended resources for learning about image processing.

Not sure; maybe start at Image processing or Java image processing and take it from there.
 
Consider Paul's rocket mass heater.
 
subject: Java Advanced Imaging (JAI) Question