278 case IDAT_TYPE:
279 return chunk[0];
280 case IEND_TYPE:
281 return 0;
282 case PLTE_TYPE:
283 parsePaletteChunk(chunk[0]);
284 break;
285 case tRNS_TYPE:
286 parseTransparencyChunk(chunk[0]);
287 break;
288 default:
289 skip(chunk[0]);
290 }
291 int crc = stream.readInt();
292 }
293 }
294
295 public void dispose() {
296 }
297
298 private ImageMetadata updateMetadata() {
299 ImageMetadata metaData = new ImageMetadata(null, true,
300 null, null, null, null, null, width, height, null, null, null);
301 updateImageMetadata(metaData);
302 return metaData;
303 }
304
305 private ImageStorage.ImageType getType() {
306 switch (colorType) {
307 case PNG_COLOR_GRAY:
308 return tRNS_present
309 ? ImageStorage.ImageType.GRAY_ALPHA
310 : ImageStorage.ImageType.GRAY;
311 case PNG_COLOR_RGB:
312 return tRNS_present
313 ? ImageStorage.ImageType.RGBA
314 : ImageStorage.ImageType.RGB;
315 case PNG_COLOR_PALETTE:
316 return ImageStorage.ImageType.PALETTE;
317 case PNG_COLOR_GRAY_ALPHA:
318 return ImageStorage.ImageType.GRAY_ALPHA;
319 case PNG_COLOR_RGB_ALPHA:
320 return ImageStorage.ImageType.RGBA;
321 default: // unreacheble
322 throw new RuntimeException();
323 }
324 }
630 }
631
632 private int bytesPerColor() {
633 return bitDepth == 16 ? 2 : 1;
634 }
635
636 public ImageFrame load(int imageIndex, int rWidth, int rHeight,
637 boolean preserveAspectRatio, boolean smooth) throws IOException {
638
639 if (imageIndex != 0) {
640 return null;
641 }
642
643 int dataSize = parsePngMeta();
644
645 if (dataSize == 0) {
646 emitWarning("No image data in PNG");
647 return null;
648 }
649
650 int bpp = bpp();
651 ByteBuffer bb = ByteBuffer.allocate(bpp * width * height);
652 ImageMetadata metadata = updateMetadata();
653
654 PNGIDATChunkInputStream iDat = new PNGIDATChunkInputStream(stream, dataSize);
655 Inflater inf = new Inflater();
656 InputStream data = new BufferedInputStream(new InflaterInputStream(iDat, inf));
657
658 try {
659 load(bb.array(), data);
660 } catch (IOException e) {
661 throw e;
662 } finally {
663 if (inf != null) {
664 inf.end();
665 }
666 }
667
668 ImageFrame imgPNG = colorType == PNG_COLOR_PALETTE
669 ? decodePalette(bb.array(), metadata)
670 : new ImageFrame(getType(), bb, width, height, bpp * width, palette, metadata);
671
672 // need remove scaler form loader
673 int[] outWH = ImageTools.computeDimensions(width, height, rWidth, rHeight, preserveAspectRatio);
674
675 if (width != outWH[0] || height != outWH[1]) {
676 imgPNG = scaleImage(imgPNG, outWH[0], outWH[1], smooth);
677 }
678
679 return imgPNG;
680 }
681
682 private ImageFrame scaleImage(ImageFrame imgPNG, int rWidth, int rHeight, boolean smooth) {
683 byte image[] = ((ByteBuffer) imgPNG.getImageData()).array();
684 int bpp = ImageStorage.getNumBands(imgPNG.getImageType());
685
686 PushbroomScaler scaler = ScalerFactory.createScaler(width, height, bpp,
687 rWidth, rHeight, smooth);
688
689 for (int y = 0; y != height; ++y) {
690 scaler.putSourceScanline(image, y * width * bpp);
691 }
692
693 return new ImageFrame(imgPNG.getImageType(), scaler.getDestination(),
694 rWidth, rHeight, rWidth * bpp, null, imgPNG.getMetadata());
695 }
696 }
|
278 case IDAT_TYPE:
279 return chunk[0];
280 case IEND_TYPE:
281 return 0;
282 case PLTE_TYPE:
283 parsePaletteChunk(chunk[0]);
284 break;
285 case tRNS_TYPE:
286 parseTransparencyChunk(chunk[0]);
287 break;
288 default:
289 skip(chunk[0]);
290 }
291 int crc = stream.readInt();
292 }
293 }
294
295 public void dispose() {
296 }
297
298 private ImageStorage.ImageType getType() {
299 switch (colorType) {
300 case PNG_COLOR_GRAY:
301 return tRNS_present
302 ? ImageStorage.ImageType.GRAY_ALPHA
303 : ImageStorage.ImageType.GRAY;
304 case PNG_COLOR_RGB:
305 return tRNS_present
306 ? ImageStorage.ImageType.RGBA
307 : ImageStorage.ImageType.RGB;
308 case PNG_COLOR_PALETTE:
309 return ImageStorage.ImageType.PALETTE;
310 case PNG_COLOR_GRAY_ALPHA:
311 return ImageStorage.ImageType.GRAY_ALPHA;
312 case PNG_COLOR_RGB_ALPHA:
313 return ImageStorage.ImageType.RGBA;
314 default: // unreacheble
315 throw new RuntimeException();
316 }
317 }
623 }
624
625 private int bytesPerColor() {
626 return bitDepth == 16 ? 2 : 1;
627 }
628
629 public ImageFrame load(int imageIndex, int rWidth, int rHeight,
630 boolean preserveAspectRatio, boolean smooth) throws IOException {
631
632 if (imageIndex != 0) {
633 return null;
634 }
635
636 int dataSize = parsePngMeta();
637
638 if (dataSize == 0) {
639 emitWarning("No image data in PNG");
640 return null;
641 }
642
643 int[] outWH = ImageTools.computeDimensions(width, height, rWidth, rHeight, preserveAspectRatio);
644 rWidth = outWH[0];
645 rHeight = outWH[1];
646
647 ImageMetadata metaData = new ImageMetadata(null, true,
648 null, null, null, null, null, rWidth, rHeight, null, null, null);
649 updateImageMetadata(metaData);
650
651 int bpp = bpp();
652 ByteBuffer bb = ByteBuffer.allocate(bpp * width * height);
653
654 PNGIDATChunkInputStream iDat = new PNGIDATChunkInputStream(stream, dataSize);
655 Inflater inf = new Inflater();
656 InputStream data = new BufferedInputStream(new InflaterInputStream(iDat, inf));
657
658 try {
659 load(bb.array(), data);
660 } catch (IOException e) {
661 throw e;
662 } finally {
663 if (inf != null) {
664 inf.end();
665 }
666 }
667
668 ImageFrame imgPNG = colorType == PNG_COLOR_PALETTE
669 ? decodePalette(bb.array(), metaData)
670 : new ImageFrame(getType(), bb, width, height, bpp * width, palette, metaData);
671
672 if (width != rWidth || height != rHeight) {
673 imgPNG = ImageTools.scaleImageFrame(imgPNG, rWidth, rHeight, smooth);
674 }
675
676 return imgPNG;
677 }
678 }
|