Java Code Examples for java.awt.image.DataBufferByte#getData()

The following examples show how to use java.awt.image.DataBufferByte#getData() . 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: DIBCodec.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
public void readKey8(byte[] in, int offset, int length, BufferedImage img) {
    DataBufferByte buf = (DataBufferByte) img.getRaster().getDataBuffer();
    WritableRaster raster = img.getRaster();
    int scanlineStride = raster.getSampleModel().getWidth();
    Rectangle r = raster.getBounds();
    r.x -= raster.getSampleModelTranslateX();
    r.y -= raster.getSampleModelTranslateY();
    
    int h=img.getHeight();
    int w=img.getWidth();
    int i=offset;
    int j=r.x+r.y*scanlineStride+(h-1)*scanlineStride;
    byte[] out=buf.getData();
    for (int y=0;y<h;y++) {
        System.arraycopy(in,i,out,j,w);
        i+=w;
        j-=scanlineStride;
    }
}
 
Example 2
Source File: MenuMisc.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
public static void WritePNGfile(String imagename, byte[] linear, int width, int height, IndexColorModel icm) {
    BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, icm);
    DataBufferByte sh = (DataBufferByte) buf.getRaster().getDataBuffer();
    byte[] shd = sh.getData();
    System.arraycopy(linear, 0, shd, 0, Math.min(linear.length, shd.length));
    try {
        ImageIO.write(buf, "PNG", new File(imagename));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: DirectRasterAccessor.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
/** Constructs a DirectRasterAccessor object */

  public DirectRasterAccessor(Raster raster, ColorModel cm) {
    DataBuffer db = raster.getDataBuffer();

    offsetX = raster.getMinX()-raster.getSampleModelTranslateX();
    offsetY = raster.getMinY()-raster.getSampleModelTranslateY();

    if (!(db instanceof DataBufferByte)) {
      throw new RuntimeException("DataBuffer of Raster not of correct type " +
				 "(expected DataBufferByte, got " +
				 db.getClass().getName() + ")");
    }

    DataBufferByte dbb = (DataBufferByte) db;

    SampleModel sm = raster.getSampleModel();

    if (!(sm instanceof MultiPixelPackedSampleModel)) {
      throw new RuntimeException("SampleModel of Raster not of correct type " +
				 "(expected MultiPixelPackedSampleModel, got " +
				 sm.getClass().getName() + ")");
    }

    MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sm;
    
    data = dbb.getData();
    scanlineStride = mppsm.getScanlineStride();

    if (cm.getRGB(0) == Color.white.getRGB()) {
      white = 0;
      black = 1;
    } else {
      white = 1;
      black = 0;
    }
  }
 
Example 4
Source File: BlackOrOpImage.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
/** Bytewise OR of the black pixels */

  private void byteLoop(Raster src0, Raster src1, 
			WritableRaster dst, Rectangle dstRect) {
    int w = dst.getWidth();
    int h = dst.getHeight();

    DataBufferByte src0db = (DataBufferByte) src0.getDataBuffer();
    DataBufferByte src1db = (DataBufferByte) src1.getDataBuffer();
    DataBufferByte dstdb = (DataBufferByte) dst.getDataBuffer();

    byte src0Data[] = src0db.getData();
    byte src1Data[] = src1db.getData();
    byte dstData[] = dstdb.getData();

    MultiPixelPackedSampleModel src0sm = 
      (MultiPixelPackedSampleModel) src0.getSampleModel();
    MultiPixelPackedSampleModel src1sm = 
      (MultiPixelPackedSampleModel) src1.getSampleModel();
    MultiPixelPackedSampleModel dstsm = 
      (MultiPixelPackedSampleModel) dst.getSampleModel();

    int src0ScanlineStride = src0sm.getScanlineStride();
    int src1ScanlineStride = src1sm.getScanlineStride();
    int dstScanlineStride = dstsm.getScanlineStride();

    int white = getWhite();

    if (white == 0) {
      for (int offset = 0; offset < h*dstScanlineStride; offset++) {
	dstData[offset] = (byte) (src0Data[offset] | src1Data[offset]);
      }
    } else {
      for (int offset = 0; offset < h*dstScanlineStride; offset++) {
	dstData[offset] = (byte) (src0Data[offset] & src1Data[offset]);
      }
    }
  }
 
Example 5
Source File: ImageService.java    From jlineup with Apache License 2.0 5 votes vote down vote up
public static boolean bufferedImagesEqualQuick(BufferedImage image1, BufferedImage image2) {
    DataBuffer dataBuffer1 = image1.getRaster().getDataBuffer();
    DataBuffer dataBuffer2 = image2.getRaster().getDataBuffer();
    if (dataBuffer1 instanceof DataBufferByte && dataBuffer2 instanceof DataBufferByte) {
        DataBufferByte dataBufferBytes1 = (DataBufferByte) dataBuffer1;
        DataBufferByte dataBufferBytes2 = (DataBufferByte) dataBuffer2;
        for (int bank = 0; bank < dataBufferBytes1.getNumBanks(); bank++) {
            byte[] bytes1 = dataBufferBytes1.getData(bank);
            byte[] bytes2 = dataBufferBytes2.getData(bank);
            if (!Arrays.equals(bytes1, bytes2)) {
                return false;
            }
        }
    } else if (dataBuffer1 instanceof DataBufferInt && dataBuffer2 instanceof DataBufferInt) {
        DataBufferInt dataBufferInt1 = (DataBufferInt) dataBuffer1;
        DataBufferInt dataBufferInt2 = (DataBufferInt) dataBuffer2;
        for (int bank = 0; bank < dataBufferInt1.getNumBanks(); bank++) {
            int[] ints1 = dataBufferInt1.getData(bank);
            int[] ints2 = dataBufferInt2.getData(bank);
            if (!Arrays.equals(ints1, ints2)) {
                return false;
            }
        }
    } else {
        return false;
    }
    return true;
}
 
Example 6
Source File: AWTLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Object extractImageData(BufferedImage img){
    DataBuffer buf = img.getRaster().getDataBuffer();
    switch (buf.getDataType()){
        case DataBuffer.TYPE_BYTE:
            DataBufferByte byteBuf = (DataBufferByte) buf;
            return byteBuf.getData();
        case DataBuffer.TYPE_USHORT:
            DataBufferUShort shortBuf = (DataBufferUShort) buf;
            return shortBuf.getData();
    }
    return null;
}
 
Example 7
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 8
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 9
Source File: Screenshots.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Flips the image along the Y axis and converts BGRA to ABGR
 * 
 * @param bgraBuf
 * @param out 
 */
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
    
    // calcuate half of height such that all rows of the array are written to
    // e.g. for odd heights, write 1 more scanline
    int heightdiv2ceil = height % 2 == 1 ? (height / 2) + 1 : height / 2;
    for (int y = 0; y < heightdiv2ceil; 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 10
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 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 openjdk-8 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 13
Source File: WDataTransferer.java    From hottub 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: 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 15
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 16
Source File: WDataTransferer.java    From openjdk-jdk9 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 openjdk-jdk8u-backup 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-jdk8u 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 19
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 20
Source File: WDataTransferer.java    From TencentKona-8 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);
}