Java Code Examples for java.awt.image.Raster#createInterleavedRaster()

The following examples show how to use java.awt.image.Raster#createInterleavedRaster() . 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: IncorrectManagedImageSourceOffset.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the custom buffered image, which mostly identical to
 * BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
 * This means that the raster will have gaps, between the rows.
 */
private static BufferedImage makeCustomManagedBI() {
    int w = 511, h = 255;
    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, w, h,
                                           w * 3 + 2, 3, bOffs, null);
    BufferedImage bi = new BufferedImage(colorModel, raster, true, null);
    SunWritableRaster.makeTrackable(raster.getDataBuffer());
    SunWritableRaster.markDirty(bi);
    return bi;
}
 
Example 2
Source File: BMPSubsamplingTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel colorModel =
        new ComponentColorModel(cs, nBits,
                                false, false,
                                Transparency.OPAQUE,
                                DataBuffer.TYPE_BYTE);
    WritableRaster raster =
        Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                       w, h,
                                       w*3, 3,
                                       bOffs, null);
    return new BufferedImage(colorModel, raster, false, null);
}
 
Example 3
Source File: Win32ColorModel24.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a WritableRaster with the specified width and height, that
 * has a data layout (SampleModel) compatible with this ColorModel.
 * @see WritableRaster
 * @see SampleModel
 */
public WritableRaster createCompatibleWritableRaster (int w, int h) {
    int[] bOffs = {2, 1, 0};
    return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                          w, h, w*3, 3,
                                          bOffs, null);
}
 
Example 4
Source File: IncorrectUnmanagedImageSourceOffset.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the custom buffered image, which mostly identical to
 * BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
 * This means that the raster will have gaps, between the rows.
 */
private static BufferedImage makeCustomUnmanagedBI() {
    int w = 511, h = 255;
    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, w, h,
                                           w * 3 + 2, 3, bOffs, null);
    return new BufferedImage(colorModel, raster, true, null);
}
 
Example 5
Source File: SimpleUnmanagedImage.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the custom buffered image, which mostly identical to
 * BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
 * This means that the raster will have gaps, between the rows.
 */
private static BufferedImage makeCustomUnmanagedBI() {
    int w = 511, h = 255;
    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, w, h,
                                           w * 3 + 2, 3, bOffs, null);
    return new BufferedImage(colorModel, raster, true, null);
}
 
Example 6
Source File: JFIFMarkerSegment.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
BufferedImage getThumbnail(ImageInputStream iis,
                           JPEGImageReader reader)
    throws IOException {
    iis.mark();
    iis.seek(streamPos);
    DataBufferByte buffer = new DataBufferByte(getLength());
    readByteBuffer(iis,
                   buffer.getData(),
                   reader,
                   1.0F,
                   0.0F);
    iis.reset();

    WritableRaster raster =
        Raster.createInterleavedRaster(buffer,
                                       thumbWidth,
                                       thumbHeight,
                                       thumbWidth*3,
                                       3,
                                       new int [] {0, 1, 2},
                                       null);
    ColorModel cm = new ComponentColorModel(JPEG.JCS.sRGB,
                                            false,
                                            false,
                                            ColorModel.OPAQUE,
                                            DataBuffer.TYPE_BYTE);
    return new BufferedImage(cm,
                             raster,
                             false,
                             null);
}
 
Example 7
Source File: Win32ColorModel24.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a WritableRaster with the specified width and height, that
 * has a data layout (SampleModel) compatible with this ColorModel.
 * @see WritableRaster
 * @see SampleModel
 */
public WritableRaster createCompatibleWritableRaster (int w, int h) {
    int[] bOffs = {2, 1, 0};
    return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                          w, h, w*3, 3,
                                          bOffs, null);
}
 
Example 8
Source File: BMPSubsamplingTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel colorModel =
        new ComponentColorModel(cs, nBits,
                                false, false,
                                Transparency.OPAQUE,
                                DataBuffer.TYPE_BYTE);
    WritableRaster raster =
        Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                       w, h,
                                       w*3, 3,
                                       bOffs, null);
    return new BufferedImage(colorModel, raster, false, null);
}
 
Example 9
Source File: Win32ColorModel24.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a WritableRaster with the specified width and height, that
 * has a data layout (SampleModel) compatible with this ColorModel.
 * @see WritableRaster
 * @see SampleModel
 */
public WritableRaster createCompatibleWritableRaster (int w, int h) {
    int[] bOffs = {2, 1, 0};
    return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                          w, h, w*3, 3,
                                          bOffs, null);
}
 
Example 10
Source File: Index16ColorModel.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public WritableRaster createCompatibleWritableRaster(final int w,
	final int h)
{
	return Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT, w, h, 1,
		null);
}
 
Example 11
Source File: BMPSubsamplingTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel colorModel =
        new ComponentColorModel(cs, nBits,
                                false, false,
                                Transparency.OPAQUE,
                                DataBuffer.TYPE_BYTE);
    WritableRaster raster =
        Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                       w, h,
                                       w*3, 3,
                                       bOffs, null);
    return new BufferedImage(colorModel, raster, false, null);
}
 
Example 12
Source File: Win32ColorModel24.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a WritableRaster with the specified width and height, that
 * has a data layout (SampleModel) compatible with this ColorModel.
 * @see WritableRaster
 * @see SampleModel
 */
public WritableRaster createCompatibleWritableRaster (int w, int h) {
    int[] bOffs = {2, 1, 0};
    return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                          w, h, w*3, 3,
                                          bOffs, null);
}
 
Example 13
Source File: PCXReader.java    From icafe with Eclipse Public License 1.0 4 votes vote down vote up
private BufferedImage read256ColorPcx(InputStream is) throws Exception {
	int totalBytes = bytesPerLine*NPlanes;
       byte pixels[] = new byte[totalBytes*height];		

       byte[] data = IOUtils.readFully(is, 4096);

	int colorsUsed = (1<<NPlanes*pcxHeader.bits_per_pixel);
	int color_tb_bytes = 3*colorsUsed;
	
	rgbColorPalette = new int[colorsUsed];

	int buf_len = data.length - color_tb_bytes;
	byte brgb[] = ArrayUtils.subArray(data, 0, buf_len);
     
       readPalette(ArrayUtils.subArray(data, buf_len, color_tb_bytes));

    /** 
     * If a BufferedInputStream is used for a 256-color image, we have to use 
	 * mark(int), skip(long) and reset() methods to put the stream pointer to
	 * the beginning of the color palette, fill the color palette and reset the pointer to
	 * the beginning of the image data. This is awkward and equally memory consuming since
	 * the system must maintain a buffer for the reset() method and we have to keep track
	 * of the skip(long) method to ensure reading of the required bytes as shown below:
	 * 	
	 * <p> 
	 * is.mark(available);
        * long i = 0, count = 0;
	 *
        * while(count < buf_len)
	 *  {
	 *     i = is.skip(buf_len-count);
        *     count += i;
	 *  }
	 *
	 * readPalette(is, color_tb_bytes);
	 * is.reset();
        *
 	 * @see java.io.BufferedInputStream
	 */

	LOGGER.info("256 color pcx image!");

	readScanLines(brgb, buf_len, pixels);   	
   	is.close();    	
	//Create a BufferedImage
	int[] off = {0};//band offset, we have only one band start at 0
	DataBuffer db = new DataBufferByte(pixels, pixels.length);
	WritableRaster raster = Raster.createInterleavedRaster(db, width, height, bytesPerLine, 1, off, null);
	ColorModel cm = new IndexColorModel(8, rgbColorPalette.length, rgbColorPalette, 0, false, -1, DataBuffer.TYPE_BYTE);
	
	return new BufferedImage(cm, raster, false, null);
}
 
Example 14
Source File: EdgeNoOpCrash.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static WritableRaster createDstRaster() {
    WritableRaster r = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
            w, h, 4, new Point(0, 0));

    return r;
}
 
Example 15
Source File: SampledImageReader.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Returns the content of the given image as an AWT buffered image with an RGB color space.
 * If a color key mask is provided then an ARGB image is returned instead.
 * This method never returns null.
 * @param pdImage the image to read
 * @param region The region of the source image to get, or null if the entire image is needed.
 *               The actual region will be clipped to the dimensions of the source image.
 * @param subsampling The amount of rows and columns to advance for every output pixel, a value
 * of 1 meaning every pixel will be read. It must not be larger than the image width or height.
 * @param colorKey an optional color key mask
 * @return content of this image as an (A)RGB buffered image
 * @throws IOException if the image cannot be read
 */
public static BufferedImage getRGBImage(PDImage pdImage, Rectangle region, int subsampling,
                                        COSArray colorKey) throws IOException
{
    if (pdImage.isEmpty())
    {
        throw new IOException("Image stream is empty");
    }
    Rectangle clipped = clipRegion(pdImage, region);

    // get parameters, they must be valid or have been repaired
    final PDColorSpace colorSpace = pdImage.getColorSpace();
    final int numComponents = colorSpace.getNumberOfComponents();
    final int width = (int) Math.ceil(clipped.getWidth() / subsampling);
    final int height = (int) Math.ceil(clipped.getHeight() / subsampling);
    final int bitsPerComponent = pdImage.getBitsPerComponent();
    final float[] decode = getDecodeArray(pdImage);

    if (width <= 0 || height <= 0 || pdImage.getWidth() <= 0 || pdImage.getHeight() <= 0)
    {
        throw new IOException("image width and height must be positive");
    }

    try
    {
        if (bitsPerComponent == 1 && colorKey == null && numComponents == 1)
        {
            return from1Bit(pdImage, clipped, subsampling, width, height);
        }

        // An AWT raster must use 8/16/32 bits per component. Images with < 8bpc
        // will be unpacked into a byte-backed raster. Images with 16bpc will be reduced
        // in depth to 8bpc as they will be drawn to TYPE_INT_RGB images anyway. All code
        // in PDColorSpace#toRGBImage expects an 8-bit range, i.e. 0-255.
        // Interleaved raster allows chunk-copying for 8-bit images.
        WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
                numComponents, new Point(0, 0));
        final float[] defaultDecode = pdImage.getColorSpace().getDefaultDecode(8);
        if (bitsPerComponent == 8 && Arrays.equals(decode, defaultDecode) && colorKey == null)
        {
            // convert image, faster path for non-decoded, non-colormasked 8-bit images
            return from8bit(pdImage, raster, clipped, subsampling, width, height);
        }
        return fromAny(pdImage, raster, colorKey, clipped, subsampling, width, height);
    }
    catch (NegativeArraySizeException ex)
    {
        throw new IOException(ex);
    }
}
 
Example 16
Source File: EdgeNoOpCrash.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static WritableRaster createDstRaster() {
    WritableRaster r = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
            w, h, 4, new Point(0, 0));

    return r;
}
 
Example 17
Source File: EdgeNoOpCrash.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static WritableRaster createDstRaster() {
    WritableRaster r = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
            w, h, 4, new Point(0, 0));

    return r;
}
 
Example 18
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 19
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 20
Source File: EdgeNoOpCrash.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static Raster createSrcRaster() {
    WritableRaster r = Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT,
            w, h, 4, new Point(0, 0));

    return r;
}