if (n==1||n==2||n==4||n==8)
//PNGEncodeParam.Palette can handle these numbers only.
img = IndexImage.getIndexedImage(img,1<<n);
}
PNGEncodeParam params = PNGEncodeParam.getDefaultEncodeParam(img);
if (params instanceof PNGEncodeParam.RGB) {
((PNGEncodeParam.RGB)params).setBackgroundRGB
(new int [] { 255, 255, 255 });
}
// If they specify GAMMA key then use it otherwise don't
// write a gAMA chunk, (default Gamma=2.2).
if (hints.containsKey(KEY_GAMMA)) {
params.setGamma(((Float)hints.get(KEY_GAMMA)).floatValue());
}
// We always want an sRGB chunk and Our encoding intent is
// perceptual
params.setSRGBIntent(PNGEncodeParam.INTENT_PERCEPTUAL);
float PixSzMM = userAgent.getPixelUnitToMillimeter();
// num Pixs in 1 Meter
int numPix = (int)((1000/PixSzMM)+0.5);
params.setPhysicalDimension(numPix, numPix, 1); // 1 means 'pix/meter'
try {
PNGImageEncoder pngEncoder = new PNGImageEncoder(ostream, params);
pngEncoder.encode(img);
ostream.close();