if (params.containsKey(PARAM_KEY_PNG_TEXT_CHUNKS))
params.remove(PARAM_KEY_PNG_TEXT_CHUNKS);
if (params.size() > 0)
{
Object firstKey = params.keySet().iterator().next();
throw new ImageWriteException("Unknown parameter: " + firstKey);
}
params = rawParams;
int width = src.getWidth();
int height = src.getHeight();
boolean hasAlpha = new PaletteFactory().hasTransparency(src);
if (verbose)
Debug.debug("hasAlpha", hasAlpha);
// int transparency = new PaletteFactory().getTransparency(src);
boolean isGrayscale = new PaletteFactory().isGrayscale(src);
if (verbose)
Debug.debug("isGrayscale", isGrayscale);
byte colorType;
{
boolean forceIndexedColor = ParamMap.getParamBoolean(params,
PARAM_KEY_PNG_FORCE_INDEXED_COLOR, false);
boolean forceTrueColor = ParamMap.getParamBoolean(params,
PARAM_KEY_PNG_FORCE_TRUE_COLOR, false);
if (forceIndexedColor && forceTrueColor)
throw new ImageWriteException(
"Params: Cannot force both indexed and true color modes");
else if (forceIndexedColor)
{
colorType = COLOR_TYPE_INDEXED_COLOR;
} else if (forceTrueColor)
{
colorType = (byte) (hasAlpha ? COLOR_TYPE_TRUE_COLOR_WITH_ALPHA
: COLOR_TYPE_TRUE_COLOR);
isGrayscale = false;
} else
colorType = getColourType(hasAlpha, isGrayscale);
if (verbose)
Debug.debug("colorType", colorType);
}
byte bitDepth = getBitDepth(colorType, params);
if (verbose)
Debug.debug("bit_depth", bitDepth);
int sampleDepth;
if (colorType == COLOR_TYPE_INDEXED_COLOR)
sampleDepth = 8;
else
sampleDepth = bitDepth;
if (verbose)
Debug.debug("sample_depth", sampleDepth);
{
os.write(PNG_Signature);
}
{
// IHDR must be first
byte compressionMethod = COMPRESSION_TYPE_INFLATE_DEFLATE;
byte filterMethod = FILTER_METHOD_ADAPTIVE;
byte interlaceMethod = INTERLACE_METHOD_NONE;
ImageHeader imageHeader = new ImageHeader(width, height, bitDepth,
colorType, compressionMethod, filterMethod, interlaceMethod);
writeChunkIHDR(os, imageHeader);
}
{
// sRGB No Before PLTE and IDAT. If the sRGB chunk is present, the
// iCCP chunk should not be present.
// charles
}
Palette palette = null;
if (colorType == COLOR_TYPE_INDEXED_COLOR)
{
// PLTE No Before first IDAT
int max_colors = hasAlpha ? 255 : 256;
palette = new MedianCutQuantizer(true).process(src, max_colors,
verbose);
// Palette palette2 = new PaletteFactory().makePaletteSimple(src,
// max_colors);
// palette.dump();
writeChunkPLTE(os, palette);
}
if (params.containsKey(PARAM_KEY_XMP_XML))
{
String xmpXml = (String) params.get(PARAM_KEY_XMP_XML);
writeChunkXmpiTXt(os, xmpXml);
}
if (params.containsKey(PARAM_KEY_PNG_TEXT_CHUNKS))
{
List outputTexts = (List) params.get(PARAM_KEY_PNG_TEXT_CHUNKS);
for (int i = 0; i < outputTexts.size(); i++)
{
PngText text = (PngText) outputTexts.get(i);
if (text instanceof PngText.tEXt)
writeChunktEXt(os, (PngText.tEXt) text);
else if (text instanceof PngText.zTXt)
writeChunkzTXt(os, (PngText.zTXt) text);
else if (text instanceof PngText.iTXt)
writeChunkiTXt(os, (PngText.iTXt) text);
else
throw new ImageWriteException(
"Unknown text to embed in PNG: " + text);
}
}
{