Java Code Examples for java.awt.image.BufferedImage#getRaster()

The following examples show how to use java.awt.image.BufferedImage#getRaster() . 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: EffectUtils.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
 * a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
 * from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
 * image.</p>
 *
 * @param img    the source image
 * @param x      the x location at which to start grabbing pixels
 * @param y      the y location at which to start grabbing pixels
 * @param w      the width of the rectangle of pixels to grab
 * @param h      the height of the rectangle of pixels to grab
 * @param pixels a pre-allocated array of pixels of size w*h; can be null
 * @return <code>pixels</code> if non-null, a new array of integers otherwise
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
 */
static byte[] getPixels(BufferedImage img,
                               int x, int y, int w, int h, byte[] pixels) {
    if (w == 0 || h == 0) {
        return new byte[0];
    }

    if (pixels == null) {
        pixels = new byte[w * h];
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
        Raster raster = img.getRaster();
        return (byte[]) raster.getDataElements(x, y, w, h, pixels);
    } else {
        throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
    }
}
 
Example 2
Source File: GraphicsUtilities.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * <p>Writes a rectangular area of pixels in the destination
 * <code>BufferedImage</code>. Calling this method on
 * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
 * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
 *
 * @param img the destination image
 * @param x the x location at which to start storing pixels
 * @param y the y location at which to start storing pixels
 * @param w the width of the rectangle of pixels to store
 * @param h the height of the rectangle of pixels to store
 * @param pixels an array of pixels, stored as integers
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and
 *   of length &lt; w*h
 */
public static void setPixels(BufferedImage img,
                             int x, int y, int w, int h, int[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" +
                                           " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        // Unmanages the image
        img.setRGB(x, y, w, h, pixels, 0, w);
    }
}
 
Example 3
Source File: EffectUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Returns an array of pixels, stored as integers, from a
 * <code>BufferedImage</code>. The pixels are grabbed from a rectangular
 * area defined by a location and two dimensions. Calling this method on
 * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
 * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
 *
 * @param img the source image
 * @param x the x location at which to start grabbing pixels
 * @param y the y location at which to start grabbing pixels
 * @param w the width of the rectangle of pixels to grab
 * @param h the height of the rectangle of pixels to grab
 * @param pixels a pre-allocated array of pixels of size w*h; can be null
 * @return <code>pixels</code> if non-null, a new array of integers
 *   otherwise
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and
 *   of length &lt; w*h
 */
public static int[] getPixels(BufferedImage img,
                              int x, int y, int w, int h, int[] pixels) {
    if (w == 0 || h == 0) {
        return new int[0];
    }

    if (pixels == null) {
        pixels = new int[w * h];
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" +
                                           " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        Raster raster = img.getRaster();
        return (int[]) raster.getDataElements(x, y, w, h, pixels);
    }

    // Unmanages the image
    return img.getRGB(x, y, w, h, pixels, 0, w);
}
 
Example 4
Source File: GraphicsUtilities.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * <p>Writes a rectangular area of pixels in the destination
 * <code>BufferedImage</code>. Calling this method on
 * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
 * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
 *
 * @param img the destination image
 * @param x the x location at which to start storing pixels
 * @param y the y location at which to start storing pixels
 * @param w the width of the rectangle of pixels to store
 * @param h the height of the rectangle of pixels to store
 * @param pixels an array of pixels, stored as integers
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and
 *   of length &lt; w*h
 */
public static void setPixels(BufferedImage img,
                             int x, int y, int w, int h, int[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" +
                                           " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        // Unmanages the image
        img.setRGB(x, y, w, h, pixels, 0, w);
    }
}
 
Example 5
Source File: GraphicsUtilities.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * <p>Returns an array of pixels, stored as integers, from a
 * <code>BufferedImage</code>. The pixels are grabbed from a rectangular
 * area defined by a location and two dimensions. Calling this method on
 * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
 * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
 *
 * @param img the source image
 * @param x the x location at which to start grabbing pixels
 * @param y the y location at which to start grabbing pixels
 * @param w the width of the rectangle of pixels to grab
 * @param h the height of the rectangle of pixels to grab
 * @param pixels a pre-allocated array of pixels of size w*h; can be null
 * @return <code>pixels</code> if non-null, a new array of integers
 *   otherwise
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and
 *   of length &lt; w*h
 */
public static int[] getPixels(BufferedImage img,
                              int x, int y, int w, int h, int[] pixels) {
    if (w == 0 || h == 0) {
        return new int[0];
    }

    if (pixels == null) {
        pixels = new int[w * h];
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" +
                                           " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        Raster raster = img.getRaster();
        return (int[]) raster.getDataElements(x, y, w, h, pixels);
    }

    // Unmanages the image
    return img.getRGB(x, y, w, h, pixels, 0, w);
}
 
Example 6
Source File: GraphicsUtilities.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * <p>Writes a rectangular area of pixels in the destination
 * <code>BufferedImage</code>. Calling this method on
 * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
 * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
 *
 * @param img the destination image
 * @param x the x location at which to start storing pixels
 * @param y the y location at which to start storing pixels
 * @param w the width of the rectangle of pixels to store
 * @param h the height of the rectangle of pixels to store
 * @param pixels an array of pixels, stored as integers
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and
 *   of length &lt; w*h
 */
public static void setPixels(BufferedImage img,
                             int x, int y, int w, int h, int[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" +
                                           " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        // Unmanages the image
        img.setRGB(x, y, w, h, pixels, 0, w);
    }
}
 
Example 7
Source File: CoverageDataPng.java    From geopackage-java with MIT License 6 votes vote down vote up
/**
 * Draw a coverage data image tile from the flat array of coverage data
 * values of length tileWidth * tileHeight where each coverage data value is
 * at: (y * tileWidth) + x
 * 
 * @param griddedTile
 *            gridded tile
 * @param values
 *            coverage data values of length tileWidth * tileHeight
 * @param tileWidth
 *            tile width
 * @param tileHeight
 *            tile height
 * @return coverage data image tile
 */
public BufferedImage drawTile(GriddedTile griddedTile, Double[] values,
		int tileWidth, int tileHeight) {

	BufferedImage image = createImage(tileWidth, tileHeight);
	WritableRaster raster = image.getRaster();
	for (int x = 0; x < tileWidth; x++) {
		for (int y = 0; y < tileHeight; y++) {
			Double value = values[(y * tileWidth) + x];
			short pixelValue = getPixelValue(griddedTile, value);
			setPixelValue(raster, x, y, pixelValue);
		}
	}

	return image;
}
 
Example 8
Source File: GraphicsUtilities.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void setPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) {
    if ((pixels == null) || (w == 0) || (h == 0)) {
        return;
    }
    if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }

    int imageType = img.getType();
    if ((imageType == 2) || (imageType == 1)) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        img.setRGB(x, y, w, h, pixels, 0, w);
    }
}
 
Example 9
Source File: GraphicsUtilities.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * <p>Writes a rectangular area of pixels in the destination
 * <code>BufferedImage</code>. Calling this method on
 * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
 * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
 *
 * @param img the destination image
 * @param x the x location at which to start storing pixels
 * @param y the y location at which to start storing pixels
 * @param w the width of the rectangle of pixels to store
 * @param h the height of the rectangle of pixels to store
 * @param pixels an array of pixels, stored as integers
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and
 *   of length &lt; w*h
 */
public static void setPixels(BufferedImage img,
                             int x, int y, int w, int h, int[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" +
                                           " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        // Unmanages the image
        img.setRGB(x, y, w, h, pixels, 0, w);
    }
}
 
Example 10
Source File: EffectUtils.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
 * a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
 * from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
 * image.</p>
 *
 * @param img    the source image
 * @param x      the x location at which to start grabbing pixels
 * @param y      the y location at which to start grabbing pixels
 * @param w      the width of the rectangle of pixels to grab
 * @param h      the height of the rectangle of pixels to grab
 * @param pixels a pre-allocated array of pixels of size w*h; can be null
 * @return <code>pixels</code> if non-null, a new array of integers otherwise
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h
 */
static byte[] getPixels(BufferedImage img,
                               int x, int y, int w, int h, byte[] pixels) {
    if (w == 0 || h == 0) {
        return new byte[0];
    }

    if (pixels == null) {
        pixels = new byte[w * h];
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
        Raster raster = img.getRaster();
        return (byte[]) raster.getDataElements(x, y, w, h, pixels);
    } else {
        throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
    }
}
 
Example 11
Source File: EffectUtils.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Writes a rectangular area of pixels in the destination
 * <code>BufferedImage</code>. Calling this method on
 * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>
 * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>
 *
 * @param img the destination image
 * @param x the x location at which to start storing pixels
 * @param y the y location at which to start storing pixels
 * @param w the width of the rectangle of pixels to store
 * @param h the height of the rectangle of pixels to store
 * @param pixels an array of pixels, stored as integers
 * @throws IllegalArgumentException is <code>pixels</code> is non-null and
 *   of length &lt; w*h
 */
public static void setPixels(BufferedImage img,
                             int x, int y, int w, int h, int[] pixels) {
    if (pixels == null || w == 0 || h == 0) {
        return;
    } else if (pixels.length < w * h) {
        throw new IllegalArgumentException("pixels array must have a length" +
                                           " >= w*h");
    }

    int imageType = img.getType();
    if (imageType == BufferedImage.TYPE_INT_ARGB ||
        imageType == BufferedImage.TYPE_INT_RGB) {
        WritableRaster raster = img.getRaster();
        raster.setDataElements(x, y, w, h, pixels);
    } else {
        // Unmanages the image
        img.setRGB(x, y, w, h, pixels, 0, w);
    }
}
 
Example 12
Source File: BMPPluginTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean compare(BufferedImage in, BufferedImage out) {
    int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j);
            }
        }
    }
    return true;
}
 
Example 13
Source File: TexturePaintContext.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static PaintContext getContext(BufferedImage bufImg,
                                      AffineTransform xform,
                                      RenderingHints hints,
                                      Rectangle devBounds) {
    WritableRaster raster = bufImg.getRaster();
    ColorModel cm = bufImg.getColorModel();
    int maxw = devBounds.width;
    Object val = hints.get(RenderingHints.KEY_INTERPOLATION);
    boolean filter =
        (val == null
         ? (hints.get(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY)
         : (val != RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR));
    if (raster instanceof IntegerInterleavedRaster &&
        (!filter || isFilterableDCM(cm)))
    {
        IntegerInterleavedRaster iir = (IntegerInterleavedRaster) raster;
        if (iir.getNumDataElements() == 1 && iir.getPixelStride() == 1) {
            return new Int(iir, cm, xform, maxw, filter);
        }
    } else if (raster instanceof ByteInterleavedRaster) {
        ByteInterleavedRaster bir = (ByteInterleavedRaster) raster;
        if (bir.getNumDataElements() == 1 && bir.getPixelStride() == 1) {
            if (filter) {
                if (isFilterableICM(cm)) {
                    return new ByteFilter(bir, cm, xform, maxw);
                }
            } else {
                return new Byte(bir, cm, xform, maxw);
            }
        }
    }
    return new Any(raster, cm, xform, maxw, filter);
}
 
Example 14
Source File: SunToolkit.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static DataBufferInt getScaledIconData(java.util.List<Image> imageList, int width, int height) {
    BufferedImage bimage = getScaledIconImage(imageList, width, height);
    if (bimage == null) {
        return null;
    }
    Raster raster = bimage.getRaster();
    DataBuffer buffer = raster.getDataBuffer();
    return (DataBufferInt)buffer;
}
 
Example 15
Source File: TexturePaintContext.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static PaintContext getContext(BufferedImage bufImg,
                                      AffineTransform xform,
                                      RenderingHints hints,
                                      Rectangle devBounds) {
    WritableRaster raster = bufImg.getRaster();
    ColorModel cm = bufImg.getColorModel();
    int maxw = devBounds.width;
    Object val = hints.get(RenderingHints.KEY_INTERPOLATION);
    boolean filter =
        (val == null
         ? (hints.get(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY)
         : (val != RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR));
    if (raster instanceof IntegerInterleavedRaster &&
        (!filter || isFilterableDCM(cm)))
    {
        IntegerInterleavedRaster iir = (IntegerInterleavedRaster) raster;
        if (iir.getNumDataElements() == 1 && iir.getPixelStride() == 1) {
            return new Int(iir, cm, xform, maxw, filter);
        }
    } else if (raster instanceof ByteInterleavedRaster) {
        ByteInterleavedRaster bir = (ByteInterleavedRaster) raster;
        if (bir.getNumDataElements() == 1 && bir.getPixelStride() == 1) {
            if (filter) {
                if (isFilterableICM(cm)) {
                    return new ByteFilter(bir, cm, xform, maxw);
                }
            } else {
                return new Byte(bir, cm, xform, maxw);
            }
        }
    }
    return new Any(raster, cm, xform, maxw, filter);
}
 
Example 16
Source File: ImageUtils.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Can be used to verify impact of different encoding algorithms. Compares two BufferedImages and outputs differences between those.
 *
 * @param image
 * @param image2
 * @return
 */
public static int[] calculateBufferedImageDifferencesPxByPx(BufferedImage image, BufferedImage image2) {
    int width = image.getWidth();
    int height = image.getHeight();

    int width2 = image2.getWidth();
    int height2 = image2.getHeight();

    WritableRaster raster1 = image.getRaster();
    WritableRaster raster2 = image2.getRaster();

    if (width != width2 || height != height2)
        throw new IllegalArgumentException("Please insert two identical images that were treated with different image algorithms");

    int[] diff = new int[width * height];

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster1.getNumBands(); band++) {
                int p1 = raster1.getSample(col, row, band);
                int p2 = raster2.getSample(col, row, band);

                diff[((row * width) + col)] = p2 - p1;
            }
        }
    }

    return diff;
}
 
Example 17
Source File: BMPWriteParamTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean compare(final BufferedImage in,
                               final BufferedImage out)
{
    final int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                // showDiff(in, out);
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));
            }
        }
    }
    return true;
}
 
Example 18
Source File: JavaImgConverter.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 24位BGR转BufferedImage
 * @param src -源数据
 * @param width -宽度
 * @param height-高度
 * @return
 */
public static BufferedImage BGR2BufferedImage(ByteBuffer src,int width,int height) {
	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
	Raster ra = image.getRaster();
	DataBuffer out = ra.getDataBuffer();
	DataBufferByte db=(DataBufferByte)out;
	ByteBuffer.wrap(db.getData()).put(src);
	return image;
}
 
Example 19
Source File: BufImgSurfaceData.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static SurfaceData createDataIC(BufferedImage bImg,
                                       SurfaceType sType) {
    IntegerComponentRaster icRaster =
        (IntegerComponentRaster)bImg.getRaster();
    BufImgSurfaceData bisd =
        new BufImgSurfaceData(icRaster.getDataBuffer(), bImg, sType);
    bisd.initRaster(icRaster.getDataStorage(),
                    icRaster.getDataOffset(0) * 4, 0,
                    icRaster.getWidth(),
                    icRaster.getHeight(),
                    icRaster.getPixelStride() * 4,
                    icRaster.getScanlineStride() * 4,
                    null);
    return bisd;
}
 
Example 20
Source File: PSPathGraphics.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/** Redraw a rectanglular area using a proxy graphics
  * To do this we need to know the rectangular area to redraw and
  * the transform & clip in effect at the time of the original drawImage
  *
  */

public void redrawRegion(Rectangle2D region, double scaleX, double scaleY,
                         Shape savedClip, AffineTransform savedTransform)

        throws PrinterException {

    PSPrinterJob psPrinterJob = (PSPrinterJob)getPrinterJob();
    Printable painter = getPrintable();
    PageFormat pageFormat = getPageFormat();
    int pageIndex = getPageIndex();

    /* Create a buffered image big enough to hold the portion
     * of the source image being printed.
     */
    BufferedImage deepImage = new BufferedImage(
                                    (int) region.getWidth(),
                                    (int) region.getHeight(),
                                    BufferedImage.TYPE_3BYTE_BGR);

    /* Get a graphics for the application to render into.
     * We initialize the buffer to white in order to
     * match the paper and then we shift the BufferedImage
     * so that it covers the area on the page where the
     * caller's Image will be drawn.
     */
    Graphics2D g = deepImage.createGraphics();
    ProxyGraphics2D proxy = new ProxyGraphics2D(g, psPrinterJob);
    proxy.setColor(Color.white);
    proxy.fillRect(0, 0, deepImage.getWidth(), deepImage.getHeight());
    proxy.clipRect(0, 0, deepImage.getWidth(), deepImage.getHeight());

    proxy.translate(-region.getX(), -region.getY());

    /* Calculate the resolution of the source image.
     */
    float sourceResX = (float)(psPrinterJob.getXRes() / scaleX);
    float sourceResY = (float)(psPrinterJob.getYRes() / scaleY);

    /* The application expects to see user space at 72 dpi.
     * so change user space from image source resolution to
     *  72 dpi.
     */
    proxy.scale(sourceResX / DEFAULT_USER_RES,
                sourceResY / DEFAULT_USER_RES);
   proxy.translate(
        -psPrinterJob.getPhysicalPrintableX(pageFormat.getPaper())
           / psPrinterJob.getXRes() * DEFAULT_USER_RES,
        -psPrinterJob.getPhysicalPrintableY(pageFormat.getPaper())
           / psPrinterJob.getYRes() * DEFAULT_USER_RES);
   /* NB User space now has to be at 72 dpi for this calc to be correct */
    proxy.transform(new AffineTransform(getPageFormat().getMatrix()));

    proxy.setPaint(Color.black);

    painter.print(proxy, pageFormat, pageIndex);

    g.dispose();

    /* In PSPrinterJob images are printed in device space
     * and therefore we need to set a device space clip.
     */
    psPrinterJob.setClip(savedTransform.createTransformedShape(savedClip));


    /* Scale the bounding rectangle by the scale transform.
     * Because the scaling transform has only x and y
     * scaling components it is equivalent to multiply
     * the x components of the bounding rectangle by
     * the x scaling factor and to multiply the y components
     * by the y scaling factor.
     */
    Rectangle2D.Float scaledBounds
            = new Rectangle2D.Float(
                    (float) (region.getX() * scaleX),
                    (float) (region.getY() * scaleY),
                    (float) (region.getWidth() * scaleX),
                    (float) (region.getHeight() * scaleY));


    /* Pull the raster data from the buffered image
     * and pass it along to PS.
     */
    ByteComponentRaster tile = (ByteComponentRaster)deepImage.getRaster();

    psPrinterJob.drawImageBGR(tile.getDataStorage(),
                        scaledBounds.x, scaledBounds.y,
                        scaledBounds.width,
                        scaledBounds.height,
                        0f, 0f,
                        deepImage.getWidth(), deepImage.getHeight(),
                        deepImage.getWidth(), deepImage.getHeight());


}