Skip to content

Commit

Permalink
codec: introduce experimental methods PNGImage.toBufferedImage() an…
Browse files Browse the repository at this point in the history
…d `PNGImageDecoder.decodeAsBufferedImage(int)`

The produced `BufferedImage` contains the properties that were obtained during decoding.

Package visibility for now.
  • Loading branch information
carlosame committed Aug 7, 2024
1 parent ea2b833 commit e8afce3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1929,4 +1929,30 @@ private void decodeImage(boolean useInterlacing) {
}
}

/**
* Convert to a {@code BufferedImage}.
* <p>
* This method is experimental and could be removed without warning.
* </p>
*
* @return the {@code BufferedImage}.
*/
BufferedImage toBufferedImage() {
ColorModel cm = getColorModel();
SampleModel sm = getSampleModel();

Point loc = new Point(getMinX(), getMinY());

WritableRaster raster = Raster.createWritableRaster(sm, loc);
Hashtable<String, Object> props = new Hashtable<>(properties);

BufferedImage img = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), props);

Graphics2D ig = img.createGraphics();
ig.drawRenderedImage(this, new AffineTransform());
ig.dispose();

return img;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,27 @@ public RenderedImage decodeAsRenderedImage(int page) throws IOException {
return new PNGImage(input, (PNGDecodeParam) param);
}

/**
* Returns a <code>BufferedImage</code> that contains the decoded contents of
* the <code>SeekableStream</code> associated with this
* <code>ImageDecoder</code>. The given page of a multi-page image is decoded.
* Page numbering begins at zero.
* <p>
* This method is experimental and could be removed in the future.
* </p>
*
* @param page The page to be decoded.
* @throws IOException if the page does not exist.
*/
BufferedImage decodeAsBufferedImage(int page) throws IOException {
if (page != 0) {
throw new IOException(PropertyUtil.formatMessage("PNGImageDecoder.unknown.page",
new Object[] { page }));
}

PNGImage png = new PNGImage(input, (PNGDecodeParam) param);

return png.toBufferedImage();
}

}

0 comments on commit e8afce3

Please sign in to comment.