Search code examples
javajpegtiffjai

JPEG in TIFF encoding


I do this steps:

TIFFEncodeParam tep = new TIFFEncodeParam();

tep.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);

BufferedImage buff = new BufferedImage(newimage.getWidth(null),
                                                   newimage.getHeight(null),
                                                   BufferedImage.TYPE_BYTE_BINARY);
//newimage is an awt image

buff.createGraphics().drawImage(newimage, 0,0,null);

ParameterBlock outPB = new ParameterBlock();

outPB.addSource(buff);
outPB.add("myjpegfile.jpg");
outPB.add("tiff");
outPB.add(tep);

PlanarImage outPI = JAI.create("filestore",outPB);

Here I get:java.lang.Error: JPEG-in-TIFF encoding supported only for 8-bit sampl es and either 1 (grayscale) or 3 (RGB or YCbCr) samples per pixel.

This because I need to have maximum compression in jpeg file for monochromatic image. I'm able to write tiff (24Kb), and jpeg (212Kb) (A4 page size 200dpi BW) but jpeg is too huge.

What does it mean this error? What is 8-bit samples?

Thx.


Solution

  • This means that JPEG-in-TIFF only supports 8-bit greyscale (= 1 sample with 8 bits per pixel) or 24-bit color images (= 3 samples with 8 bits per pixels).

    TYPE_BYTE_BINARY represents a black/white picture (= 1 sample with 1 bit per pixel). You'd want to use TYPE_BYTE_GRAY instead.