private static PNGChunk readChunk(DataInputStream distream) { try { int length = distream.readInt(); int type = distream.readInt(); byte[] data = new byte[length]; distream.readFully(data); int crc = distream.readInt(); return new PNGChunk(length, type, data, crc); } catch (Exception e) { e.printStackTrace(); return null; } }
private void parse_zTXt_chunk(PNGChunk chunk) { int textIndex = 0; StringBuffer key = new StringBuffer(); byte b; while ((b = chunk.getByte(textIndex++)) != 0) { key.append( (char)b ); } /* int method = */ chunk.getByte(textIndex++); StringBuffer value = new StringBuffer(); try { int length = chunk.getLength() - textIndex; byte[] data = chunk.getData(); InputStream cis = new ByteArrayInputStream(data, textIndex, length); InputStream iis = new InflaterInputStream(cis); int c; while ((c = iis.read()) != -1) { value.append( (char)c ); } ztextKeys.add(key.toString() ); ztextStrings.add(value.toString() ); } catch (Exception e) { e.printStackTrace(); } }
private void parse_tIME_chunk(PNGChunk chunk) { int year = chunk.getInt2(0); int month = chunk.getInt1(2) - 1; int day = chunk.getInt1(3); int hour = chunk.getInt1(4); int minute = chunk.getInt1(5); int second = chunk.getInt1(6); TimeZone gmt = TimeZone.getTimeZone("GMT"); GregorianCalendar cal = new GregorianCalendar(gmt); cal.set(year, month, day, hour, minute, second); Date date = cal.getTime(); if (encodeParam != null) { encodeParam.setModificationTime(date); } if (emitProperties) { properties.put("timestamp", date); } }
private void parse_PLTE_chunk(PNGChunk chunk) { paletteEntries = chunk.getLength()/3; redPalette = new byte[paletteEntries]; greenPalette = new byte[paletteEntries]; bluePalette = new byte[paletteEntries]; int pltIndex = 0; // gAMA chunk must precede PLTE chunk if (performGammaCorrection) { if (gammaLut == null) { initGammaLut(bitDepth == 16 ? 16 : 8); } for (int i = 0; i < paletteEntries; i++) { byte r = chunk.getByte(pltIndex++); byte g = chunk.getByte(pltIndex++); byte b = chunk.getByte(pltIndex++); redPalette[i] = (byte)gammaLut[r & 0xff]; greenPalette[i] = (byte)gammaLut[g & 0xff]; bluePalette[i] = (byte)gammaLut[b & 0xff]; } } else { for (int i = 0; i < paletteEntries; i++) { redPalette[i] = chunk.getByte(pltIndex++); greenPalette[i] = chunk.getByte(pltIndex++); bluePalette[i] = chunk.getByte(pltIndex++); } } }
private void parse_tRNS_chunk(PNGChunk chunk) { if (colorType == PNG_COLOR_PALETTE) { int entries = chunk.getLength(); if (entries > paletteEntries) { alphaPalette[i] = chunk.getByte(i); grayTransparentAlpha = chunk.getInt2(0); redTransparentAlpha = chunk.getInt2(0); greenTransparentAlpha = chunk.getInt2(2); blueTransparentAlpha = chunk.getInt2(4);
private void parse_pHYs_chunk(PNGChunk chunk) { int xPixelsPerUnit = chunk.getInt4(0); int yPixelsPerUnit = chunk.getInt4(4); int unitSpecifier = chunk.getInt1(8); if (encodeParam != null) { encodeParam.setPhysicalDimension(xPixelsPerUnit, yPixelsPerUnit, unitSpecifier); } if (emitProperties) { properties.put("x_pixels_per_unit", new Integer(xPixelsPerUnit)); properties.put("y_pixels_per_unit", new Integer(yPixelsPerUnit)); properties.put("pixel_aspect_ratio", new Float((float)xPixelsPerUnit/yPixelsPerUnit)); if (unitSpecifier == 1) { properties.put("pixel_units", "Meters"); } else if (unitSpecifier != 0) { // Error -- unit specifier must be 0 or 1 String msg = PropertyUtil.getString("PNGImageDecoder12"); throw new RuntimeException(msg); } } }
} else if (chunkType.equals("IDAT")) { chunk = readChunk(distream); streamVec.add(new ByteArrayInputStream(chunk.getData())); } else if (chunkType.equals("IEND")) { chunk = readChunk(distream); String type = chunk.getTypeString(); byte[] data = chunk.getData(); if (encodeParam != null) { encodeParam.addPrivateChunk(type, data);
private void parse_iCCP_chunk(PNGChunk chunk) { String name = ""; // todo simplify this byte b; int textIndex = 0; while ((b = chunk.getByte(textIndex++)) != 0) { name += (char)b; } }
private void parse_cHRM_chunk(PNGChunk chunk) { // If an sRGB chunk exists, ignore cHRM chunks if (sRGBRenderingIntent != -1) { return; } chromaticity = new float[8]; chromaticity[0] = chunk.getInt4(0)/100000.0F; chromaticity[1] = chunk.getInt4(4)/100000.0F; chromaticity[2] = chunk.getInt4(8)/100000.0F; chromaticity[3] = chunk.getInt4(12)/100000.0F; chromaticity[4] = chunk.getInt4(16)/100000.0F; chromaticity[5] = chunk.getInt4(20)/100000.0F; chromaticity[6] = chunk.getInt4(24)/100000.0F; chromaticity[7] = chunk.getInt4(28)/100000.0F; if (encodeParam != null) { encodeParam.setChromaticity(chromaticity); } if (emitProperties) { properties.put("white_point_x", chromaticity[0]); properties.put("white_point_y", chromaticity[1]); properties.put("red_x", chromaticity[2]); properties.put("red_y", chromaticity[3]); properties.put("green_x", chromaticity[4]); properties.put("green_y", chromaticity[5]); properties.put("blue_x", chromaticity[6]); properties.put("blue_y", chromaticity[7]); } }
private void parse_hIST_chunk(PNGChunk chunk) { if (redPalette == null) { String msg = PropertyUtil.getString("PNGImageDecoder18"); throw new RuntimeException(msg); } int length = redPalette.length; int[] hist = new int[length]; for (int i = 0; i < length; i++) { hist[i] = chunk.getInt2(2*i); } if (encodeParam != null) { encodeParam.setPaletteHistogram(hist); } }
private void parse_tRNS_chunk(PNGChunk chunk) { if (colorType == PNG_COLOR_PALETTE) { int entries = chunk.getLength(); if (entries > paletteEntries) { alphaPalette[i] = chunk.getByte(i); grayTransparentAlpha = chunk.getInt2(0); redTransparentAlpha = chunk.getInt2(0); greenTransparentAlpha = chunk.getInt2(2); blueTransparentAlpha = chunk.getInt2(4);
private void parse_PLTE_chunk(PNGChunk chunk) { paletteEntries = chunk.getLength()/3; redPalette = new byte[paletteEntries]; greenPalette = new byte[paletteEntries]; bluePalette = new byte[paletteEntries]; int pltIndex = 0; // gAMA chunk must precede PLTE chunk if (performGammaCorrection) { if (gammaLut == null) { initGammaLut(bitDepth == 16 ? 16 : 8); } for (int i = 0; i < paletteEntries; i++) { byte r = chunk.getByte(pltIndex++); byte g = chunk.getByte(pltIndex++); byte b = chunk.getByte(pltIndex++); redPalette[i] = (byte)gammaLut[r & 0xff]; greenPalette[i] = (byte)gammaLut[g & 0xff]; bluePalette[i] = (byte)gammaLut[b & 0xff]; } } else { for (int i = 0; i < paletteEntries; i++) { redPalette[i] = chunk.getByte(pltIndex++); greenPalette[i] = chunk.getByte(pltIndex++); bluePalette[i] = chunk.getByte(pltIndex++); } } }
private void parse_pHYs_chunk(PNGChunk chunk) { int xPixelsPerUnit = chunk.getInt4(0); int yPixelsPerUnit = chunk.getInt4(4); int unitSpecifier = chunk.getInt1(8); if (encodeParam != null) { encodeParam.setPhysicalDimension(xPixelsPerUnit, yPixelsPerUnit, unitSpecifier); } if (emitProperties) { properties.put("x_pixels_per_unit", xPixelsPerUnit); properties.put("y_pixels_per_unit", yPixelsPerUnit); properties.put("pixel_aspect_ratio", (float) xPixelsPerUnit / yPixelsPerUnit); if (unitSpecifier == 1) { properties.put("pixel_units", "Meters"); } else if (unitSpecifier != 0) { // Error -- unit specifier must be 0 or 1 String msg = PropertyUtil.getString("PNGImageDecoder12"); throw new RuntimeException(msg); } } }
} else if (chunkType.equals("IDAT")) { chunk = readChunk(distream); streamVec.add(new ByteArrayInputStream(chunk.getData())); } else if (chunkType.equals("IEND")) { chunk = readChunk(distream); String type = chunk.getTypeString(); byte[] data = chunk.getData(); if (encodeParam != null) { encodeParam.addPrivateChunk(type, data);
private void parse_iCCP_chunk(PNGChunk chunk) { String name = ""; // todo simplify this byte b; int textIndex = 0; while ((b = chunk.getByte(textIndex++)) != 0) { name += (char)b; } }
private void parse_cHRM_chunk(PNGChunk chunk) { // If an sRGB chunk exists, ignore cHRM chunks if (sRGBRenderingIntent != -1) { return; } chromaticity = new float[8]; chromaticity[0] = chunk.getInt4(0)/100000.0F; chromaticity[1] = chunk.getInt4(4)/100000.0F; chromaticity[2] = chunk.getInt4(8)/100000.0F; chromaticity[3] = chunk.getInt4(12)/100000.0F; chromaticity[4] = chunk.getInt4(16)/100000.0F; chromaticity[5] = chunk.getInt4(20)/100000.0F; chromaticity[6] = chunk.getInt4(24)/100000.0F; chromaticity[7] = chunk.getInt4(28)/100000.0F; if (encodeParam != null) { encodeParam.setChromaticity(chromaticity); } if (emitProperties) { properties.put("white_point_x", chromaticity[0]); properties.put("white_point_y", chromaticity[1]); properties.put("red_x", chromaticity[2]); properties.put("red_y", chromaticity[3]); properties.put("green_x", chromaticity[4]); properties.put("green_y", chromaticity[5]); properties.put("blue_x", chromaticity[6]); properties.put("blue_y", chromaticity[7]); } }
private void parse_hIST_chunk(PNGChunk chunk) { if (redPalette == null) { String msg = PropertyUtil.getString("PNGImageDecoder18"); throw new RuntimeException(msg); } int length = redPalette.length; int[] hist = new int[length]; for (int i = 0; i < length; i++) { hist[i] = chunk.getInt2(2*i); } if (encodeParam != null) { encodeParam.setPaletteHistogram(hist); } }
private void parse_zTXt_chunk(PNGChunk chunk) { int textIndex = 0; StringBuffer key = new StringBuffer(); byte b; while ((b = chunk.getByte(textIndex++)) != 0) { key.append( (char)b ); } /* int method = */ chunk.getByte(textIndex++); StringBuffer value = new StringBuffer(); try { int length = chunk.getLength() - textIndex; byte[] data = chunk.getData(); InputStream cis = new ByteArrayInputStream(data, textIndex, length); InputStream iis = new InflaterInputStream(cis); int c; while ((c = iis.read()) != -1) { value.append( (char)c ); } ztextKeys.add(key.toString() ); ztextStrings.add(value.toString() ); } catch (Exception e) { e.printStackTrace(); } }