Skip to main content

Writing PBM files with J2SE 5.0

2 replies [Last post]
keithbannister
Offline
Joined: 2006-08-31

Hello,

I'm new to all this stuff & I'm having trouble with the difference between JAI & Image I/O.

I'm trying to write a PBM file (a black and white version of a PNM file) using J2SE 1.5.

I've not been able to install the image I/O tools because when I run the installer it says "Needs JDK1.4 or higher".

I've got JAI installed.

I thought the following would work, but the encoder returned is null:

OutputStream out = new FileOutputStream(new File(fullFileName));

// Create the ParameterBlock.
PNMEncodeParam param = new PNMEncodeParam();
param.setRaw(true);
// Create the PNM image encoder.
ImageEncoder encoder = ImageCodec.createImageEncoder("PBM", out,
param);

// encoder == null!

if I do ImageCodec.createImageEncoder("PNM"...) it works OK, but I get a full colour file, not a B/W.

Any ideas?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
robinhout
Offline
Joined: 2006-01-04

The class PNMImageEncoder in JAI automatically chooses the appropriate file variant based on the actual SampleModel of the source image: PBM for monochrome images, PGM for grey scale images, and PPM for color images (from javadoc of PNMImageEncoder).

E.g. PBM will be chosen when the SampleModel of the image contains a single band of samples, with 1 bit per sample (e.g. a BufferedImage of type TYPE_BYTE_BINARY with an IndexColorModel containing 2 colors, white and black).

This is also the case for Image I/O.

keithbannister
Offline
Joined: 2006-08-31

Hey wow! That's nifty. Thanks for that. I'll try it out.