Search in sources :

Example 66 with PixelGrabber

use of java.awt.image.PixelGrabber in project servoy-client by Servoy.

the class JpegInfo method getYCCArray.

/*
	 * This method creates and fills three arrays, Y, Cb, and Cr using the input image.
	 */
private void getYCCArray() {
    int[] values = new int[imageWidth * imageHeight];
    int r, g, b, y, x;
    // In order to minimize the chance that grabPixels will throw an exception
    // it may be necessary to grab some pixels every few scanlines and process
    // those before going for more.  The time expense may be prohibitive.
    // However, for a situation where memory overhead is a concern, this may be
    // the only choice.
    PixelGrabber grabber = new PixelGrabber(imageobj.getSource(), 0, 0, imageWidth, imageHeight, values, 0, imageWidth);
    MaxHsampFactor = 1;
    MaxVsampFactor = 1;
    for (y = 0; y < NumberOfComponents; y++) {
        MaxHsampFactor = Math.max(MaxHsampFactor, HsampFactor[y]);
        MaxVsampFactor = Math.max(MaxVsampFactor, VsampFactor[y]);
    }
    for (y = 0; y < NumberOfComponents; y++) {
        compWidth[y] = (((imageWidth % 8 != 0) ? ((int) Math.ceil(imageWidth / 8.0)) * 8 : imageWidth) / MaxHsampFactor) * HsampFactor[y];
        if (compWidth[y] != ((imageWidth / MaxHsampFactor) * HsampFactor[y])) {
            lastColumnIsDummy[y] = true;
        }
        // results in a multiple of 8 for compWidth
        // this will make the rest of the program fail for the unlikely
        // event that someone tries to compress an 16 x 16 pixel image
        // which would of course be worse than pointless
        BlockWidth[y] = (int) Math.ceil(compWidth[y] / 8.0);
        compHeight[y] = (((imageHeight % 8 != 0) ? ((int) Math.ceil(imageHeight / 8.0)) * 8 : imageHeight) / MaxVsampFactor) * VsampFactor[y];
        if (compHeight[y] != ((imageHeight / MaxVsampFactor) * VsampFactor[y])) {
            lastRowIsDummy[y] = true;
        }
        BlockHeight[y] = (int) Math.ceil(compHeight[y] / 8.0);
    }
    try {
        if (grabber.grabPixels() != true) {
            try {
                // $NON-NLS-1$
                throw new AWTException("Grabber returned false: " + grabber.status());
            } catch (Exception e) {
            }
            ;
        }
    } catch (InterruptedException e) {
    }
    ;
    float[][] Y = new float[compHeight[0]][compWidth[0]];
    float[][] Cr1 = new float[compHeight[0]][compWidth[0]];
    float[][] Cb1 = new float[compHeight[0]][compWidth[0]];
    float[][] Cb2 = new float[compHeight[1]][compWidth[1]];
    float[][] Cr2 = new float[compHeight[2]][compWidth[2]];
    int index = 0;
    for (y = 0; y < imageHeight; ++y) {
        for (x = 0; x < imageWidth; ++x) {
            r = ((values[index] >> 16) & 0xff);
            g = ((values[index] >> 8) & 0xff);
            b = (values[index] & 0xff);
            // The following three lines are a more correct color conversion but
            // the current conversion technique is sufficient and results in a higher
            // compression rate.
            // Y[y][x] = 16 + (float)(0.8588*(0.299 * (float)r + 0.587 * (float)g + 0.114 * (float)b ));
            // Cb1[y][x] = 128 + (float)(0.8784*(-0.16874 * (float)r - 0.33126 * (float)g + 0.5 * (float)b));
            // Cr1[y][x] = 128 + (float)(0.8784*(0.5 * (float)r - 0.41869 * (float)g - 0.08131 * (float)b));
            Y[y][x] = (float) ((0.299 * r + 0.587 * g + 0.114 * b));
            Cb1[y][x] = 128 + (float) ((-0.16874 * r - 0.33126 * g + 0.5 * b));
            Cr1[y][x] = 128 + (float) ((0.5 * r - 0.41869 * g - 0.08131 * b));
            index++;
        }
    }
    // Need a way to set the H and V sample factors before allowing downsampling.
    // For now (04/04/98) downsampling must be hard coded.
    // Until a better downsampler is implemented, this will not be done.
    // Downsampling is currently supported.  The downsampling method here
    // is a simple box filter.
    Components[0] = Y;
    // Cb2 = DownSample(Cb1, 1);
    Components[1] = Cb1;
    // Cr2 = DownSample(Cr1, 2);
    Components[2] = Cr1;
}
Also used : PixelGrabber(java.awt.image.PixelGrabber) AWTException(java.awt.AWTException) IOException(java.io.IOException) AWTException(java.awt.AWTException)

Example 67 with PixelGrabber

use of java.awt.image.PixelGrabber in project jzy3d-api by jzy3d.

the class AWTImageConvert method getImagePixels.

/**
 * Return the image pixels in default Java int ARGB format.
 *
 * @param image
 * @param width
 * @param height
 * @return
 */
public static int[] getImagePixels(Image image, int width, int height) {
    int[] pixels = null;
    if (image != null) {
        pixels = new int[width * height];
        PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
        try {
            pg.grabPixels();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return pixels;
}
Also used : PixelGrabber(java.awt.image.PixelGrabber)

Example 68 with PixelGrabber

use of java.awt.image.PixelGrabber in project visualgc_java8 by beansoft.

the class UIUtils method getGTKProfilerResultsBackground.

private static Color getGTKProfilerResultsBackground() {
    int[] pixels = new int[1];
    pixels[0] = -1;
    // Prepare textarea to grab the color from
    JTextArea textArea = new JTextArea();
    textArea.setSize(new Dimension(10, 10));
    textArea.doLayout();
    // Print the textarea to an image
    Image image = new BufferedImage(textArea.getSize().width, textArea.getSize().height, BufferedImage.TYPE_INT_RGB);
    textArea.printAll(image.getGraphics());
    // Grab appropriate pixels to get the color
    PixelGrabber pixelGrabber = new PixelGrabber(image, 5, 5, 1, 1, pixels, 0, 1);
    try {
        pixelGrabber.grabPixels();
        // System background not customized
        if (pixels[0] == -1)
            return Color.WHITE;
    } catch (InterruptedException e) {
        return getNonGTKProfilerResultsBackground();
    }
    return pixels[0] != -1 ? new Color(pixels[0]) : getNonGTKProfilerResultsBackground();
}
Also used : PixelGrabber(java.awt.image.PixelGrabber) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage)

Example 69 with PixelGrabber

use of java.awt.image.PixelGrabber in project myrobotlab by MyRobotLab.

the class Util method hasAlpha.

// This method returns true if the specified image has transparent pixels
public static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }
    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }
    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}
Also used : ColorModel(java.awt.image.ColorModel) PixelGrabber(java.awt.image.PixelGrabber) BufferedImage(java.awt.image.BufferedImage)

Example 70 with PixelGrabber

use of java.awt.image.PixelGrabber in project mdsplus by MDSplus.

the class Frames method getPixelArray.

protected int[] getPixelArray(int idx, int x, int y, int img_w, int img_h) {
    error = "";
    Image img;
    try {
        img = cache.getImageAt(idx);
    } catch (final Exception exc) {
        error = "INTERNAL ERROR in Frame.getPixelArrat: " + exc;
        System.out.println("INTERNAL ERROR in Frame.getPixelArrat: " + exc);
        return null;
    }
    if (img_w == -1 && img_h == -1) {
        img_width = img_w = img.getWidth(this);
        img_height = img_h = img.getHeight(this);
    }
    final int[] pixel_array = new int[img_w * img_h];
    final PixelGrabber grabber = new PixelGrabber(img, x, y, img_w, img_h, pixel_array, 0, img_w);
    try {
        grabber.grabPixels();
    } catch (final InterruptedException ie) {
        error = "Pixel array not completed";
        System.err.println("Pixel array not completed");
        return null;
    }
    return pixel_array;
}
Also used : PixelGrabber(java.awt.image.PixelGrabber) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException)

Aggregations

PixelGrabber (java.awt.image.PixelGrabber)88 BufferedImage (java.awt.image.BufferedImage)37 ColorModel (java.awt.image.ColorModel)20 IOException (java.io.IOException)17 Image (java.awt.Image)13 Color (java.awt.Color)9 DirectColorModel (java.awt.image.DirectColorModel)7 MemoryImageSource (java.awt.image.MemoryImageSource)6 Point2D (java.awt.geom.Point2D)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ObfuscatedName (net.runelite.mapping.ObfuscatedName)5 ObfuscatedSignature (net.runelite.mapping.ObfuscatedSignature)5 Graphics2D (java.awt.Graphics2D)3 MediaTracker (java.awt.MediaTracker)3 ByteProcessor (ij.process.ByteProcessor)2 AWTException (java.awt.AWTException)2 Graphics (java.awt.Graphics)2 IndexColorModel (java.awt.image.IndexColorModel)2 WritableRaster (java.awt.image.WritableRaster)2