Java Code Examples for java.awt.image.WritableRaster#getDataBuffer()

The following examples show how to use java.awt.image.WritableRaster#getDataBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Screenshots.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void convertScreenShot2(IntBuffer bgraBuf, BufferedImage out){
        WritableRaster wr = out.getRaster();
        DataBufferInt db = (DataBufferInt) wr.getDataBuffer();
        
        int[] cpuArray = db.getData();
        
        bgraBuf.clear();
        bgraBuf.get(cpuArray);
        
//        int width  = wr.getWidth();
//        int height = wr.getHeight();
//
//        // flip the components the way AWT likes them
//        for (int y = 0; y < height / 2; y++){
//            for (int x = 0; x < width; x++){
//                int inPtr  = (y * width + x);
//                int outPtr = ((height-y-1) * width + x);
//                int pixel = cpuArray[inPtr];
//                cpuArray[inPtr] = cpuArray[outPtr];
//                cpuArray[outPtr] = pixel;
//            }
//        }
    }
 
Example 2
Source File: Screenshots.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void convertScreenShot2(IntBuffer bgraBuf, BufferedImage out){
        WritableRaster wr = out.getRaster();
        DataBufferInt db = (DataBufferInt) wr.getDataBuffer();
        
        int[] cpuArray = db.getData();
        
        bgraBuf.clear();
        bgraBuf.get(cpuArray);
        
//        int width  = wr.getWidth();
//        int height = wr.getHeight();
//
//        // flip the components the way AWT likes them
//        for (int y = 0; y < height / 2; y++){
//            for (int x = 0; x < width; x++){
//                int inPtr  = (y * width + x);
//                int outPtr = ((height-y-1) * width + x);
//                int pixel = cpuArray[inPtr];
//                cpuArray[inPtr] = cpuArray[outPtr];
//                cpuArray[outPtr] = pixel;
//            }
//        }
    }
 
Example 3
Source File: ShaderUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static final ByteBuffer getImageDataFromImage(BufferedImage bufferedImage) {
	WritableRaster wr;
	DataBuffer db;

	BufferedImage bi = new BufferedImage(128, 64, BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = bi.createGraphics();
	g.drawImage(bufferedImage, null, null);
	bufferedImage = bi;
	wr = bi.getRaster();
	db = wr.getDataBuffer();

	DataBufferInt dbi = (DataBufferInt) db;
	int[] data = dbi.getData();

	ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * 4);
	byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	byteBuffer.asIntBuffer().put(data);
	byteBuffer.flip();

	return byteBuffer;
}
 
Example 4
Source File: CustomCompositeTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
    int w = src.getWidth();
    int h = src.getHeight();

    DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
    DataBufferInt dstOutDB = (DataBufferInt) dstOut.getDataBuffer();
    int srcRGB[] = srcDB.getBankData()[0];
    int dstOutRGB[] = dstOutDB.getBankData()[0];
    int srcOffset = srcDB.getOffset();
    int dstOutOffset = dstOutDB.getOffset();
    int srcScanStride = ((SinglePixelPackedSampleModel) src.getSampleModel()).getScanlineStride();
    int dstOutScanStride = ((SinglePixelPackedSampleModel) dstOut.getSampleModel()).getScanlineStride();
    int srcAdjust = srcScanStride - w;
    int dstOutAdjust = dstOutScanStride - w;

    int si = srcOffset;
    int doi = dstOutOffset;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            dstOutRGB[doi] = srcRGB[si] ^ 0x00ffffff;
            si++;
            doi++;
        }

        si += srcAdjust;
        doi += dstOutAdjust;
    }
}
 
Example 5
Source File: CustomCompositeTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
    int w = src.getWidth();
    int h = src.getHeight();

    DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
    DataBufferInt dstOutDB = (DataBufferInt) dstOut.getDataBuffer();
    int srcRGB[] = srcDB.getBankData()[0];
    int dstOutRGB[] = dstOutDB.getBankData()[0];
    int srcOffset = srcDB.getOffset();
    int dstOutOffset = dstOutDB.getOffset();
    int srcScanStride = ((SinglePixelPackedSampleModel) src.getSampleModel()).getScanlineStride();
    int dstOutScanStride = ((SinglePixelPackedSampleModel) dstOut.getSampleModel()).getScanlineStride();
    int srcAdjust = srcScanStride - w;
    int dstOutAdjust = dstOutScanStride - w;

    int si = srcOffset;
    int doi = dstOutOffset;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            dstOutRGB[doi] = srcRGB[si] ^ 0x00ffffff;
            si++;
            doi++;
        }

        si += srcAdjust;
        doi += dstOutAdjust;
    }
}
 
Example 6
Source File: CustomCompositeTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
    int w = src.getWidth();
    int h = src.getHeight();

    DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
    DataBufferInt dstOutDB = (DataBufferInt) dstOut.getDataBuffer();
    int srcRGB[] = srcDB.getBankData()[0];
    int dstOutRGB[] = dstOutDB.getBankData()[0];
    int srcOffset = srcDB.getOffset();
    int dstOutOffset = dstOutDB.getOffset();
    int srcScanStride = ((SinglePixelPackedSampleModel) src.getSampleModel()).getScanlineStride();
    int dstOutScanStride = ((SinglePixelPackedSampleModel) dstOut.getSampleModel()).getScanlineStride();
    int srcAdjust = srcScanStride - w;
    int dstOutAdjust = dstOutScanStride - w;

    int si = srcOffset;
    int doi = dstOutOffset;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            dstOutRGB[doi] = srcRGB[si] ^ 0x00ffffff;
            si++;
            doi++;
        }

        si += srcAdjust;
        doi += dstOutAdjust;
    }
}
 
Example 7
Source File: CustomCompositeTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
    int w = src.getWidth();
    int h = src.getHeight();

    DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
    DataBufferInt dstOutDB = (DataBufferInt) dstOut.getDataBuffer();
    int srcRGB[] = srcDB.getBankData()[0];
    int dstOutRGB[] = dstOutDB.getBankData()[0];
    int srcOffset = srcDB.getOffset();
    int dstOutOffset = dstOutDB.getOffset();
    int srcScanStride = ((SinglePixelPackedSampleModel) src.getSampleModel()).getScanlineStride();
    int dstOutScanStride = ((SinglePixelPackedSampleModel) dstOut.getSampleModel()).getScanlineStride();
    int srcAdjust = srcScanStride - w;
    int dstOutAdjust = dstOutScanStride - w;

    int si = srcOffset;
    int doi = dstOutOffset;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            dstOutRGB[doi] = srcRGB[si] ^ 0x00ffffff;
            si++;
            doi++;
        }

        si += srcAdjust;
        doi += dstOutAdjust;
    }
}
 
Example 8
Source File: ScenePreviewProcessor.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void postFrame(FrameBuffer fb) {
    if (currentPreviewRequest != null) {
        cpuBuf.clear();
        SceneApplication.getApplication().getRenderer().readFrameBuffer(offBuffer, cpuBuf);

        // copy native memory to java memory
        cpuBuf.clear();
        cpuBuf.get(cpuArray);
        cpuBuf.clear();

        // flip the components the way AWT likes them
        for (int i = 0; i < width * height * 4; i += 4) {
            byte b = cpuArray[i + 0];
            byte g = cpuArray[i + 1];
            byte r = cpuArray[i + 2];
            byte a = cpuArray[i + 3];

            cpuArray[i + 0] = a;
            cpuArray[i + 1] = b;
            cpuArray[i + 2] = g;
            cpuArray[i + 3] = r;
        }

        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_4BYTE_ABGR);
        WritableRaster wr = image.getRaster();
        DataBufferByte db = (DataBufferByte) wr.getDataBuffer();
        System.arraycopy(cpuArray, 0, db.getData(), 0, cpuArray.length);

        currentPreviewRequest.setImage(image);
        previewNode.detachAllChildren();
        SceneApplication.getApplication().notifySceneListeners(currentPreviewRequest);
        currentPreviewRequest = null;
    }
}
 
Example 9
Source File: CustomCompositeTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
    int w = src.getWidth();
    int h = src.getHeight();

    DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
    DataBufferInt dstOutDB = (DataBufferInt) dstOut.getDataBuffer();
    int srcRGB[] = srcDB.getBankData()[0];
    int dstOutRGB[] = dstOutDB.getBankData()[0];
    int srcOffset = srcDB.getOffset();
    int dstOutOffset = dstOutDB.getOffset();
    int srcScanStride = ((SinglePixelPackedSampleModel) src.getSampleModel()).getScanlineStride();
    int dstOutScanStride = ((SinglePixelPackedSampleModel) dstOut.getSampleModel()).getScanlineStride();
    int srcAdjust = srcScanStride - w;
    int dstOutAdjust = dstOutScanStride - w;

    int si = srcOffset;
    int doi = dstOutOffset;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            dstOutRGB[doi] = srcRGB[si] ^ 0x00ffffff;
            si++;
            doi++;
        }

        si += srcAdjust;
        doi += dstOutAdjust;
    }
}
 
Example 10
Source File: Screenshots.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void convertScreenShot(ByteBuffer bgraBuf, BufferedImage out){
    WritableRaster wr = out.getRaster();
    DataBufferByte db = (DataBufferByte) wr.getDataBuffer();

    byte[] cpuArray = db.getData();

    // copy native memory to java memory
    bgraBuf.clear();
    bgraBuf.get(cpuArray);
    bgraBuf.clear();

    int width  = wr.getWidth();
    int height = wr.getHeight();

    // flip the components the way AWT likes them
    for (int y = 0; y < height / 2; y++){
        for (int x = 0; x < width; x++){
            int inPtr  = (y * width + x) * 4;
            int outPtr = ((height-y-1) * width + x) * 4;

            byte b1 = cpuArray[inPtr+0];
            byte g1 = cpuArray[inPtr+1];
            byte r1 = cpuArray[inPtr+2];
            byte a1 = cpuArray[inPtr+3];

            byte b2 = cpuArray[outPtr+0];
            byte g2 = cpuArray[outPtr+1];
            byte r2 = cpuArray[outPtr+2];
            byte a2 = cpuArray[outPtr+3];

            cpuArray[outPtr+0] = a1;
            cpuArray[outPtr+1] = b1;
            cpuArray[outPtr+2] = g1;
            cpuArray[outPtr+3] = r1;

            cpuArray[inPtr+0] = a2;
            cpuArray[inPtr+1] = b2;
            cpuArray[inPtr+2] = g2;
            cpuArray[inPtr+3] = r2;
        }
    }
}
 
Example 11
Source File: WDataTransferer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected byte[] imageToPlatformBytes(Image image, long format)
        throws IOException {
    String mimeType = null;
    if (format == CF_PNG) {
        mimeType = "image/png";
    } else if (format == CF_JFIF) {
        mimeType = "image/jpeg";
    }
    if (mimeType != null) {
        return imageToStandardBytes(image, mimeType);
    }

    int width = 0;
    int height = 0;

    if (image instanceof ToolkitImage) {
        ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
        ir.reconstruct(ImageObserver.ALLBITS);
        width = ir.getWidth();
        height = ir.getHeight();
    } else {
        width = image.getWidth(null);
        height = image.getHeight(null);
    }

    // Fix for 4919639.
    // Some Windows native applications (e.g. clipbrd.exe) do not handle
    // 32-bpp DIBs correctly.
    // As a workaround we switched to 24-bpp DIBs.
    // MSDN prescribes that the bitmap array for a 24-bpp should consist of
    // 3-byte triplets representing blue, green and red components of a
    // pixel respectively. Additionally each scan line must be padded with
    // zeroes to end on a LONG data-type boundary. LONG is always 32-bit.
    // We render the given Image to a BufferedImage of type TYPE_3BYTE_BGR
    // with non-default scanline stride and pass the resulting data buffer
    // to the native code to fill the BITMAPINFO structure.
    int mod = (width * 3) % 4;
    int pad = mod > 0 ? 4 - mod : 0;

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = {8, 8, 8};
    int[] bOffs = {2, 1, 0};
    ColorModel colorModel =
            new ComponentColorModel(cs, nBits, false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    WritableRaster raster =
            Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
                    width * 3 + pad, 3, bOffs, null);

    BufferedImage bimage = new BufferedImage(colorModel, raster, false, null);

    // Some Windows native applications (e.g. clipbrd.exe) do not understand
    // top-down DIBs.
    // So we flip the image vertically and create a bottom-up DIB.
    AffineTransform imageFlipTransform =
            new AffineTransform(1, 0, 0, -1, 0, height);

    Graphics2D g2d = bimage.createGraphics();

    try {
        g2d.drawImage(image, imageFlipTransform, null);
    } finally {
        g2d.dispose();
    }

    DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();

    byte[] imageData = buffer.getData();
    return imageDataToPlatformImageBytes(imageData, width, height, format);
}
 
Example 12
Source File: WDataTransferer.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected byte[] imageToPlatformBytes(Image image, long format)
        throws IOException {
    String mimeType = null;
    if (format == CF_PNG) {
        mimeType = "image/png";
    } else if (format == CF_JFIF) {
        mimeType = "image/jpeg";
    }
    if (mimeType != null) {
        return imageToStandardBytes(image, mimeType);
    }

    int width = 0;
    int height = 0;

    if (image instanceof ToolkitImage) {
        ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
        ir.reconstruct(ImageObserver.ALLBITS);
        width = ir.getWidth();
        height = ir.getHeight();
    } else {
        width = image.getWidth(null);
        height = image.getHeight(null);
    }

    // Fix for 4919639.
    // Some Windows native applications (e.g. clipbrd.exe) do not handle
    // 32-bpp DIBs correctly.
    // As a workaround we switched to 24-bpp DIBs.
    // MSDN prescribes that the bitmap array for a 24-bpp should consist of
    // 3-byte triplets representing blue, green and red components of a
    // pixel respectively. Additionally each scan line must be padded with
    // zeroes to end on a LONG data-type boundary. LONG is always 32-bit.
    // We render the given Image to a BufferedImage of type TYPE_3BYTE_BGR
    // with non-default scanline stride and pass the resulting data buffer
    // to the native code to fill the BITMAPINFO structure.
    int mod = (width * 3) % 4;
    int pad = mod > 0 ? 4 - mod : 0;

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = {8, 8, 8};
    int[] bOffs = {2, 1, 0};
    ColorModel colorModel =
            new ComponentColorModel(cs, nBits, false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    WritableRaster raster =
            Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
                    width * 3 + pad, 3, bOffs, null);

    BufferedImage bimage = new BufferedImage(colorModel, raster, false, null);

    // Some Windows native applications (e.g. clipbrd.exe) do not understand
    // top-down DIBs.
    // So we flip the image vertically and create a bottom-up DIB.
    AffineTransform imageFlipTransform =
            new AffineTransform(1, 0, 0, -1, 0, height);

    Graphics2D g2d = bimage.createGraphics();

    try {
        g2d.drawImage(image, imageFlipTransform, null);
    } finally {
        g2d.dispose();
    }

    DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();

    byte[] imageData = buffer.getData();
    return imageDataToPlatformImageBytes(imageData, width, height, format);
}
 
Example 13
Source File: WDataTransferer.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected byte[] imageToPlatformBytes(Image image, long format)
        throws IOException {
    String mimeType = null;
    if (format == CF_PNG) {
        mimeType = "image/png";
    } else if (format == CF_JFIF) {
        mimeType = "image/jpeg";
    }
    if (mimeType != null) {
        return imageToStandardBytes(image, mimeType);
    }

    int width = 0;
    int height = 0;

    if (image instanceof ToolkitImage) {
        ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
        ir.reconstruct(ImageObserver.ALLBITS);
        width = ir.getWidth();
        height = ir.getHeight();
    } else {
        width = image.getWidth(null);
        height = image.getHeight(null);
    }

    // Fix for 4919639.
    // Some Windows native applications (e.g. clipbrd.exe) do not handle
    // 32-bpp DIBs correctly.
    // As a workaround we switched to 24-bpp DIBs.
    // MSDN prescribes that the bitmap array for a 24-bpp should consist of
    // 3-byte triplets representing blue, green and red components of a
    // pixel respectively. Additionally each scan line must be padded with
    // zeroes to end on a LONG data-type boundary. LONG is always 32-bit.
    // We render the given Image to a BufferedImage of type TYPE_3BYTE_BGR
    // with non-default scanline stride and pass the resulting data buffer
    // to the native code to fill the BITMAPINFO structure.
    int mod = (width * 3) % 4;
    int pad = mod > 0 ? 4 - mod : 0;

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = {8, 8, 8};
    int[] bOffs = {2, 1, 0};
    ColorModel colorModel =
            new ComponentColorModel(cs, nBits, false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    WritableRaster raster =
            Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
                    width * 3 + pad, 3, bOffs, null);

    BufferedImage bimage = new BufferedImage(colorModel, raster, false, null);

    // Some Windows native applications (e.g. clipbrd.exe) do not understand
    // top-down DIBs.
    // So we flip the image vertically and create a bottom-up DIB.
    AffineTransform imageFlipTransform =
            new AffineTransform(1, 0, 0, -1, 0, height);

    Graphics2D g2d = bimage.createGraphics();

    try {
        g2d.drawImage(image, imageFlipTransform, null);
    } finally {
        g2d.dispose();
    }

    DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();

    byte[] imageData = buffer.getData();
    return imageDataToPlatformImageBytes(imageData, width, height, format);
}
 
Example 14
Source File: RLSAOpImage.java    From pdfxtk with Apache License 2.0 4 votes vote down vote up
/** Does vertical RLSA */

  private void byteLoop_v(Raster src, WritableRaster dst) {
    int minX = getMinX();
    int maxX = getMaxX();
    int minY = getMinY();
    int maxY = getMaxY();

    DataBufferByte srcdb = (DataBufferByte) src.getDataBuffer();
    DataBufferByte dstdb = (DataBufferByte) dst.getDataBuffer();

    byte srcData[] = srcdb.getData();
    byte dstData[] = dstdb.getData();

    MultiPixelPackedSampleModel srcsm = (MultiPixelPackedSampleModel) src.getSampleModel();
    MultiPixelPackedSampleModel dstsm = (MultiPixelPackedSampleModel) dst.getSampleModel();

    int srcScanlineStride = srcsm.getScanlineStride();
    int dstScanlineStride = dstsm.getScanlineStride();

    int srcScanlineOffset = minY*srcScanlineStride;
    int dstScanlineOffset = 0;

    int[] column = new int[maxY];

    int white = getWhite();

    for (int x = minX, dstx = 0; x < maxX; x++, dstx++) {
      int srcxOffset = x / 8;
      int srcxBit = x % 8;
      int dstxOffset = dstx / 8;
      int dstxBit = dstx % 8;

      int srcLineOffset = srcScanlineOffset;
      int dstLineOffset = dstScanlineOffset;

      int lasty = minY-(threshold+2);

      if (white == 0) {
	for (int y = minY; y < maxY; y++) {
	  if ((srcData[srcLineOffset+srcxOffset] & bitAccess[srcxBit]) != 0) {
	    column[y] = 1;
	    if (y < lasty+threshold+2) {
	      for (int i = lasty; i<y; i++) {
		column[i] = 1;
	      }
	    }
	    lasty = y;
	  } else {
	    column[y] = 0;
	  }
	  srcLineOffset += srcScanlineStride;
	}
      } else {
	for (int y = minY; y < maxY; y++) {
	  if ((srcData[srcLineOffset+srcxOffset] & bitAccess[srcxBit]) == 0) {
	    column[y] = 0;
	    if (y < lasty+threshold+2) {
	      for (int i = lasty; i<y; i++) {
		column[i] = 0;
	      }
	    }
	    lasty = y;
	  } else {
	    column[y] = 1;
	  }
	  srcLineOffset += srcScanlineStride;
	}
      }

      // Copy resulting column onto image

      for (int srcy = minY, lineOffset = 0; srcy < maxY; srcy++, lineOffset += dstScanlineStride) {
	if (column[srcy] == 1) {
	  dstData[lineOffset+dstxOffset] |= bitAccess[dstxBit];
	} else {
	  dstData[lineOffset+dstxOffset] &= ~bitAccess[dstxBit];
	}
      }
    }
  }
 
Example 15
Source File: ImageIOHelper.java    From JewelCrawler with GNU General Public License v3.0 4 votes vote down vote up
public static byte[] image_byte_data(BufferedImage image) {
	WritableRaster raster = image.getRaster();
	DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
	return buffer.getData();
}
 
Example 16
Source File: WDataTransferer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected byte[] imageToPlatformBytes(Image image, long format)
        throws IOException {
    String mimeType = null;
    if (format == CF_PNG) {
        mimeType = "image/png";
    } else if (format == CF_JFIF) {
        mimeType = "image/jpeg";
    }
    if (mimeType != null) {
        return imageToStandardBytes(image, mimeType);
    }

    int width = 0;
    int height = 0;

    if (image instanceof ToolkitImage) {
        ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
        ir.reconstruct(ImageObserver.ALLBITS);
        width = ir.getWidth();
        height = ir.getHeight();
    } else {
        width = image.getWidth(null);
        height = image.getHeight(null);
    }

    // Fix for 4919639.
    // Some Windows native applications (e.g. clipbrd.exe) do not handle
    // 32-bpp DIBs correctly.
    // As a workaround we switched to 24-bpp DIBs.
    // MSDN prescribes that the bitmap array for a 24-bpp should consist of
    // 3-byte triplets representing blue, green and red components of a
    // pixel respectively. Additionally each scan line must be padded with
    // zeroes to end on a LONG data-type boundary. LONG is always 32-bit.
    // We render the given Image to a BufferedImage of type TYPE_3BYTE_BGR
    // with non-default scanline stride and pass the resulting data buffer
    // to the native code to fill the BITMAPINFO structure.
    int mod = (width * 3) % 4;
    int pad = mod > 0 ? 4 - mod : 0;

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = {8, 8, 8};
    int[] bOffs = {2, 1, 0};
    ColorModel colorModel =
            new ComponentColorModel(cs, nBits, false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    WritableRaster raster =
            Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
                    width * 3 + pad, 3, bOffs, null);

    BufferedImage bimage = new BufferedImage(colorModel, raster, false, null);

    // Some Windows native applications (e.g. clipbrd.exe) do not understand
    // top-down DIBs.
    // So we flip the image vertically and create a bottom-up DIB.
    AffineTransform imageFlipTransform =
            new AffineTransform(1, 0, 0, -1, 0, height);

    Graphics2D g2d = bimage.createGraphics();

    try {
        g2d.drawImage(image, imageFlipTransform, null);
    } finally {
        g2d.dispose();
    }

    DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();

    byte[] imageData = buffer.getData();
    return imageDataToPlatformImageBytes(imageData, width, height, format);
}
 
Example 17
Source File: WDataTransferer.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected byte[] imageToPlatformBytes(Image image, long format)
        throws IOException {
    String mimeType = null;
    if (format == CF_PNG) {
        mimeType = "image/png";
    } else if (format == CF_JFIF) {
        mimeType = "image/jpeg";
    }
    if (mimeType != null) {
        return imageToStandardBytes(image, mimeType);
    }

    int width = 0;
    int height = 0;

    if (image instanceof ToolkitImage) {
        ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
        ir.reconstruct(ImageObserver.ALLBITS);
        width = ir.getWidth();
        height = ir.getHeight();
    } else {
        width = image.getWidth(null);
        height = image.getHeight(null);
    }

    // Fix for 4919639.
    // Some Windows native applications (e.g. clipbrd.exe) do not handle
    // 32-bpp DIBs correctly.
    // As a workaround we switched to 24-bpp DIBs.
    // MSDN prescribes that the bitmap array for a 24-bpp should consist of
    // 3-byte triplets representing blue, green and red components of a
    // pixel respectively. Additionally each scan line must be padded with
    // zeroes to end on a LONG data-type boundary. LONG is always 32-bit.
    // We render the given Image to a BufferedImage of type TYPE_3BYTE_BGR
    // with non-default scanline stride and pass the resulting data buffer
    // to the native code to fill the BITMAPINFO structure.
    int mod = (width * 3) % 4;
    int pad = mod > 0 ? 4 - mod : 0;

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = {8, 8, 8};
    int[] bOffs = {2, 1, 0};
    ColorModel colorModel =
            new ComponentColorModel(cs, nBits, false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    WritableRaster raster =
            Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
                    width * 3 + pad, 3, bOffs, null);

    BufferedImage bimage = new BufferedImage(colorModel, raster, false, null);

    // Some Windows native applications (e.g. clipbrd.exe) do not understand
    // top-down DIBs.
    // So we flip the image vertically and create a bottom-up DIB.
    AffineTransform imageFlipTransform =
            new AffineTransform(1, 0, 0, -1, 0, height);

    Graphics2D g2d = bimage.createGraphics();

    try {
        g2d.drawImage(image, imageFlipTransform, null);
    } finally {
        g2d.dispose();
    }

    DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();

    byte[] imageData = buffer.getData();
    return imageDataToPlatformImageBytes(imageData, width, height, format);
}
 
Example 18
Source File: WDataTransferer.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
protected byte[] imageToPlatformBytes(Image image, long format)
        throws IOException {
    String mimeType = null;
    if (format == CF_PNG) {
        mimeType = "image/png";
    } else if (format == CF_JFIF) {
        mimeType = "image/jpeg";
    }
    if (mimeType != null) {
        return imageToStandardBytes(image, mimeType);
    }

    int width = 0;
    int height = 0;

    if (image instanceof ToolkitImage) {
        ImageRepresentation ir = ((ToolkitImage)image).getImageRep();
        ir.reconstruct(ImageObserver.ALLBITS);
        width = ir.getWidth();
        height = ir.getHeight();
    } else {
        width = image.getWidth(null);
        height = image.getHeight(null);
    }

    // Fix for 4919639.
    // Some Windows native applications (e.g. clipbrd.exe) do not handle
    // 32-bpp DIBs correctly.
    // As a workaround we switched to 24-bpp DIBs.
    // MSDN prescribes that the bitmap array for a 24-bpp should consist of
    // 3-byte triplets representing blue, green and red components of a
    // pixel respectively. Additionally each scan line must be padded with
    // zeroes to end on a LONG data-type boundary. LONG is always 32-bit.
    // We render the given Image to a BufferedImage of type TYPE_3BYTE_BGR
    // with non-default scanline stride and pass the resulting data buffer
    // to the native code to fill the BITMAPINFO structure.
    int mod = (width * 3) % 4;
    int pad = mod > 0 ? 4 - mod : 0;

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = {8, 8, 8};
    int[] bOffs = {2, 1, 0};
    ColorModel colorModel =
            new ComponentColorModel(cs, nBits, false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    WritableRaster raster =
            Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
                    width * 3 + pad, 3, bOffs, null);

    BufferedImage bimage = new BufferedImage(colorModel, raster, false, null);

    // Some Windows native applications (e.g. clipbrd.exe) do not understand
    // top-down DIBs.
    // So we flip the image vertically and create a bottom-up DIB.
    AffineTransform imageFlipTransform =
            new AffineTransform(1, 0, 0, -1, 0, height);

    Graphics2D g2d = bimage.createGraphics();

    try {
        g2d.drawImage(image, imageFlipTransform, null);
    } finally {
        g2d.dispose();
    }

    DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();

    byte[] imageData = buffer.getData();
    return imageDataToPlatformImageBytes(imageData, width, height, format);
}
 
Example 19
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private static int[] getData(BufferedImage image) {
  WritableRaster wr = image.getRaster();
  DataBufferInt dbi = (DataBufferInt) wr.getDataBuffer();
  return dbi.getData();
  // return ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
}
 
Example 20
Source File: CoverageDataPng.java    From geopackage-java with MIT License 2 votes vote down vote up
/**
 * Get the pixel values of the raster as "unsigned shorts"
 * 
 * @param raster
 *            image raster
 * @return "unsigned short" pixel values
 */
public short[] getPixelValues(WritableRaster raster) {
	DataBufferUShort buffer = (DataBufferUShort) raster.getDataBuffer();
	short[] pixelValues = buffer.getData();
	return pixelValues;
}