From bd98b927b910d9421e63cf0dbc2b746eec019f80 Mon Sep 17 00:00:00 2001
From: Sven Gothel
+ * Implicitly converts the image to match the desired:
+ *
+ *
+ * destStrideInBytes
, see {@link #getStride()}destIsGLOriented
, see {@link #isGLOriented()}
null
to use source {@link PixelFormat}
+ * @param destDirectBuffer if true, using a direct NIO buffer, otherwise an array backed buffer
+ * @param destMinStrideInBytes used if greater than PNG's stride, otherwise using PNG's stride. Stride is width * bytes-per-pixel.
+ * @param destIsGLOriented
+ * @return the newly created PNGPixelRect instance
+ * @throws IOException
+ */
+ public static PNGPixelRect read(final InputStream in,
+ final PixelFormat ddestFmt, final boolean destDirectBuffer, final int destMinStrideInBytes,
+ final boolean destIsGLOriented) throws IOException {
+ final PngReader pngr = new PngReader(new BufferedInputStream(in), null);
+ final ImageInfo imgInfo = pngr.imgInfo;
+ final PngChunkPLTE plte = pngr.getMetadata().getPLTE();
+ final PngChunkTRNS trns = pngr.getMetadata().getTRNS();
+ final boolean indexed = imgInfo.indexed;
+ final boolean hasAlpha = indexed ? ( trns != null ) : imgInfo.alpha ;
+
+ if(DEBUG) {
+ System.err.println("PNGPixelRect: "+imgInfo);
+ }
+ final int channels = indexed ? ( hasAlpha ? 4 : 3 ) : imgInfo.channels ;
+ final boolean isGrayAlpha = 2 == channels && imgInfo.greyscale && imgInfo.alpha;
+ if ( ! ( 1 == channels || 3 == channels || 4 == channels || isGrayAlpha ) ) {
+ throw new RuntimeException("PNGPixelRect can only handle Lum/RGB/RGBA [1/3/4 channels] or Lum+A (GA) images for now. Channels "+channels + " Paletted: " + indexed);
+ }
+ final int bytesPerPixel = indexed ? channels : imgInfo.bytesPixel ;
+ if ( ! ( 1 == bytesPerPixel || 3 == bytesPerPixel || 4 == bytesPerPixel || isGrayAlpha ) ) {
+ throw new RuntimeException("PNGPixelRect can only handle Lum/RGB/RGBA [1/3/4 bpp] images for now. BytesPerPixel "+bytesPerPixel);
+ }
+ if( channels != bytesPerPixel ) {
+ throw new RuntimeException("PNGPixelRect currently only handles Channels [1/3/4] == BytePerPixel [1/3/4], channels: "+channels+", bytesPerPixel "+bytesPerPixel);
+ }
+ final int width = imgInfo.cols;
+ final int height = imgInfo.rows;
+ final double dpiX, dpiY;
+ {
+ final double[] dpi = pngr.getMetadata().getDpi();
+ dpiX = dpi[0];
+ dpiY = dpi[1];
+ }
+ final PixelFormat srcFmt;
+ if ( indexed ) {
+ if ( hasAlpha ) {
+ srcFmt = PixelFormat.RGBA8888;
+ } else {
+ srcFmt = PixelFormat.RGB888;
+ }
+ } else {
+ switch( channels ) {
+ case 1: srcFmt = PixelFormat.LUMINANCE; break;
+ case 2: srcFmt = isGrayAlpha ? PixelFormat.LUMINANCE : null; break;
+ case 3: srcFmt = PixelFormat.RGB888; break;
+ case 4: srcFmt = PixelFormat.RGBA8888; break;
+ default: srcFmt = null;
+ }
+ if( null == srcFmt ) {
+ throw new InternalError("XXX: channels: "+channels+", bytesPerPixel "+bytesPerPixel);
+ }
+ }
+ final PixelFormat destFmt;
+ if( null == ddestFmt ) {
+ if( isGrayAlpha ) {
+ destFmt = PixelFormat.BGRA8888; // save alpha value on gray-alpha
+ } else {
+ destFmt = srcFmt; // 1:1
+ }
+ } else {
+ destFmt = ddestFmt; // user choice
+ }
+ final int destStrideInBytes = Math.max(destMinStrideInBytes, destFmt.bytesPerPixel() * width);
+ final ByteBuffer destPixels = destDirectBuffer ? Buffers.newDirectByteBuffer(destStrideInBytes * height) :
+ ByteBuffer.allocate(destStrideInBytes * height);
+ {
+ final int reqBytes = destStrideInBytes * height;
+ if( destPixels.limit() < reqBytes ) {
+ throw new IndexOutOfBoundsException("Dest buffer has insufficient bytes left, needs "+reqBytes+": "+destPixels);
+ }
+ }
+ final boolean vert_flip = destIsGLOriented;
+
+ int[] rgbaScanline = indexed ? new int[width * channels] : null;
+ if(DEBUG) {
+ System.err.println("PNGPixelRect: indexed "+indexed+", alpha "+hasAlpha+", grayscale "+imgInfo.greyscale+", channels "+channels+"/"+imgInfo.channels+
+ ", bytesPerPixel "+bytesPerPixel+"/"+imgInfo.bytesPixel+
+ ", grayAlpha "+isGrayAlpha+", pixels "+width+"x"+height+", dpi "+dpiX+"x"+dpiY+", format "+srcFmt);
+ System.err.println("PNGPixelRect: destFormat "+destFmt+" ("+ddestFmt+", bytesPerPixel "+destFmt.bytesPerPixel()+", fast-path "+(destFmt==srcFmt)+"), destDirectBuffer "+destDirectBuffer+", destIsGLOriented (flip) "+destIsGLOriented);
+ System.err.println("PNGPixelRect: destStrideInBytes "+destStrideInBytes+" (destMinStrideInBytes "+destMinStrideInBytes+")");
+ }
+
+ for (int row = 0; row < height; row++) {
+ final ImageLine l1 = pngr.readRow(row);
+ int lineOff = 0;
+ int dataOff = vert_flip ? ( height - 1 - row ) * destStrideInBytes : row * destStrideInBytes;
+ if( indexed ) {
+ for (int j = width - 1; j >= 0; j--) {
+ rgbaScanline = ImageLineHelper.palette2rgb(l1, plte, trns, rgbaScanline); // reuse rgbaScanline and update if resized
+ dataOff = getPixelRGBA8ToAny(destFmt, destPixels, dataOff, rgbaScanline, lineOff, hasAlpha);
+ lineOff += bytesPerPixel;
+ }
+ } else if( 1 == channels ) {
+ for (int j = width - 1; j >= 0; j--) {
+ dataOff = getPixelLUMToAny(destFmt, destPixels, dataOff, (byte)l1.scanline[lineOff++], (byte)0xff); // Luminance, 1 bytesPerPixel
+ }
+ } else if( isGrayAlpha ) {
+ for (int j = width - 1; j >= 0; j--) {
+ dataOff = getPixelLUMToAny(destFmt, destPixels, dataOff, (byte)l1.scanline[lineOff++], (byte)l1.scanline[lineOff++]); // Luminance+Alpha, 2 bytesPerPixel
+ }
+ } else if( srcFmt == destFmt ) { // fast-path
+ for (int j = width - 1; j >= 0; j--) {
+ dataOff = getPixelRGBSame(destPixels, dataOff, l1.scanline, lineOff, bytesPerPixel);
+ lineOff += bytesPerPixel;
+ }
+ } else {
+ for (int j = width - 1; j >= 0; j--) {
+ dataOff = getPixelRGBA8ToAny(destFmt, destPixels, dataOff, l1.scanline, lineOff, hasAlpha);
+ lineOff += bytesPerPixel;
+ }
+ }
+ }
+ pngr.end();
+
+ return new PNGPixelRect(destFmt, new Dimension(width, height), destStrideInBytes, destIsGLOriented, destPixels, dpiX, dpiY);
+ }
+
+ private static final int getPixelLUMToAny(PixelFormat dest_fmt, ByteBuffer d, int dOff, byte lum, byte alpha) {
+ switch(dest_fmt) {
+ case LUMINANCE:
+ d.put(dOff++, lum);
+ break;
+ case BGR888:
+ case RGB888:
+ d.put(dOff++, lum);
+ d.put(dOff++, lum);
+ d.put(dOff++, lum);
+ break;
+ case ABGR8888:
+ case ARGB8888:
+ d.put(dOff++, alpha); // A
+ d.put(dOff++, lum);
+ d.put(dOff++, lum);
+ d.put(dOff++, lum);
+ break;
+ case BGRA8888:
+ case RGBA8888:
+ d.put(dOff++, lum);
+ d.put(dOff++, lum);
+ d.put(dOff++, lum);
+ d.put(dOff++, alpha); // A
+ break;
+ default:
+ throw new InternalError("Unhandled format "+dest_fmt);
+ }
+ return dOff;
+ }
+ private static final int getPixelRGBA8ToAny(final PixelFormat dest_fmt, final ByteBuffer d, int dOff, final int[] scanline, final int lineOff, final boolean srcHasAlpha) {
+ final int p = PixelFormatUtil.convertToInt32(dest_fmt, (byte)scanline[lineOff], // R
+ (byte)scanline[lineOff+1], // G
+ (byte)scanline[lineOff+2], // B
+ srcHasAlpha ? (byte)scanline[lineOff+3] : (byte)0xff); // A
+ final int dbpp = dest_fmt.bytesPerPixel();
+ d.put(dOff++, (byte) ( p )); // 1
+ if( 1 < dbpp ) {
+ d.put(dOff++, (byte) ( p >>> 8 )); // 2
+ d.put(dOff++, (byte) ( p >>> 16 )); // 3
+ if( 4 == dbpp ) {
+ d.put(dOff++, (byte) ( p >>> 24 )); // 4
+ }
+ }
+ return dOff;
+ }
+ private static final int getPixelRGBSame(final ByteBuffer d, int dOff, final int[] scanline, final int lineOff, final int bpp) {
+ d.put(dOff++, (byte)scanline[lineOff]); // R
+ if( 1 < bpp ) {
+ d.put(dOff++, (byte)scanline[lineOff + 1]); // G
+ d.put(dOff++, (byte)scanline[lineOff + 2]); // B
+ if( 4 == bpp ) {
+ d.put(dOff++, (byte)scanline[lineOff + 3]); // A
+ }
+ }
+ return dOff;
+ }
+ private int setPixelRGBA8(final ImageLine line, final int lineOff, final ByteBuffer d, final int dOff, final int bytesPerPixel, final boolean hasAlpha) {
+ final int b = hasAlpha ? 4-1 : 3-1;
+ if( d.limit() <= dOff + b ) {
+ throw new IndexOutOfBoundsException("Buffer has unsufficient bytes left, needs ["+dOff+".."+(dOff+b)+"]: "+d);
+ }
+ final int p = PixelFormatUtil.convertToInt32(hasAlpha ? PixelFormat.RGBA8888 : PixelFormat.RGB888, pixelformat, d, dOff);
+ line.scanline[lineOff ] = 0xff & p; // R
+ line.scanline[lineOff + 1] = 0xff & ( p >>> 8 ); // G
+ line.scanline[lineOff + 2] = 0xff & ( p >>> 16 ); // B
+ if(hasAlpha) {
+ line.scanline[lineOff + 3] = 0xff & ( p >>> 24 ); // A
+ }
+ return dOff + pixelformat.bytesPerPixel();
+ }
+
+ /**
+ * Creates a PNGPixelRect from data supplied by the end user. Shares
+ * data with the passed ByteBuffer.
+ *
+ * @param pixelformat
+ * @param size
+ * @param strideInBytes
+ * @param isGLOriented see {@link #isGLOriented()}.
+ * @param pixels
+ * @param dpiX
+ * @param dpiY
+ */
+ public PNGPixelRect(final PixelFormat pixelformat, final DimensionImmutable size,
+ final int strideInBytes, final boolean isGLOriented, final ByteBuffer pixels,
+ final double dpiX, final double dpiY) {
+ super(pixelformat, size, strideInBytes, isGLOriented, pixels);
+ this.dpi = new double[] { dpiX, dpiY };
+ }
+ public PNGPixelRect(final PixelRectangle src, final double dpiX, final double dpiY) {
+ super(src);
+ this.dpi = new double[] { dpiX, dpiY };
+ }
+ private final double[] dpi;
+
+ /** Returns the dpi of the image. */
+ public double[] getDpi() { return dpi; }
+
+ public void write(final OutputStream outstream, final boolean closeOutstream) throws IOException {
+ final int width = size.getWidth();
+ final int height = size.getHeight();
+ final int bytesPerPixel = pixelformat.bytesPerPixel();
+ final ImageInfo imi = new ImageInfo(width, height, 8 /* bitdepth */,
+ (4 == bytesPerPixel) ? true : false /* alpha */,
+ (1 == bytesPerPixel) ? true : false /* grayscale */,
+ false /* indexed */);
+
+ // open image for writing to a output stream
+ try {
+ final PngWriter png = new PngWriter(outstream, imi);
+ // add some optional metadata (chunks)
+ png.getMetadata().setDpi(dpi[0], dpi[1]);
+ png.getMetadata().setTimeNow(0); // 0 seconds fron now = now
+ png.getMetadata().setText(PngChunkTextVar.KEY_Title, "JogAmp PNGPixelRect");
+ // png.getMetadata().setText("my key", "my text");
+ final boolean hasAlpha = 4 == bytesPerPixel;
+
+ final ImageLine l1 = new ImageLine(imi);
+ for (int row = 0; row < height; row++) {
+ int dataOff = isGLOriented ? ( height - 1 - row ) * strideInBytes : row * strideInBytes;
+ int lineOff = 0;
+ if(1 == bytesPerPixel) {
+ for (int j = width - 1; j >= 0; j--) {
+ l1.scanline[lineOff++] = pixels.get(dataOff++); // // Luminance, 1 bytesPerPixel
+ }
+ } else {
+ for (int j = width - 1; j >= 0; j--) {
+ dataOff = setPixelRGBA8(l1, lineOff, pixels, dataOff, bytesPerPixel, hasAlpha);
+ lineOff += bytesPerPixel;
+ }
+ }
+ png.writeRow(l1, row);
+ }
+ png.end();
+ } finally {
+ if( closeOutstream ) {
+ IOUtil.close(outstream, false);
+ }
+ }
+ }
+}
--
cgit v1.2.3
From 21b68a5e73a672da34b37b5641b779aa1e3fa41d Mon Sep 17 00:00:00 2001
From: Sven Gothel