Search in sources :

Example 46 with PixelGrabber

use of java.awt.image.PixelGrabber in project LoboEvolution by LoboEvolution.

the class InputImage method hasAlpha.

private static boolean hasAlpha(Image image) {
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }
    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 47 with PixelGrabber

use of java.awt.image.PixelGrabber in project vcell by virtualcell.

the class NativeImage method createPixels.

/**
 * This method was created in VisualAge.
 * @return byte[]
 */
private void createPixels() throws Exception {
    if (bPixelsExist()) {
        return;
    }
    PixelGrabber pg = new PixelGrabber(getJavaImage(), 0, 0, getSize().getX(), getSize().getX(), false);
    if (!pg.grabPixels()) {
        throw new Exception("Error getting pixels, status = " + pg.getStatus());
    }
    Object pixels = pg.getPixels();
    ColorModel colorModel = null;
    while (colorModel == null) {
        colorModel = pg.getColorModel();
    }
    if ((pixels instanceof int[]) && (colorModel instanceof DirectColorModel)) {
        newRGBAPixels = (int[]) pixels;
    } else if ((pixels instanceof byte[]) && (colorModel instanceof IndexColorModel)) {
        newIndexPixels = (byte[]) pixels;
        IndexColorModel indexColorModel = (IndexColorModel) colorModel;
        int colorMapSize = indexColorModel.getMapSize();
        byte[] temp_colorMap = new byte[colorMapSize * IndexColorMap.RGB_PACK_SIZE];
        byte[] reds = new byte[colorMapSize];
        indexColorModel.getReds(reds);
        byte[] greens = new byte[colorMapSize];
        indexColorModel.getGreens(greens);
        byte[] blues = new byte[colorMapSize];
        indexColorModel.getBlues(blues);
        for (int c = 0; c < colorMapSize; c += 1) {
            // make packed R,G,B array for ColorIndexModel
            int packCount = c * IndexColorMap.RGB_PACK_SIZE;
            temp_colorMap[packCount] = reds[c];
            temp_colorMap[packCount + 1] = greens[c];
            temp_colorMap[packCount + 2] = blues[c];
        }
        colorMap = new IndexColorMap(temp_colorMap);
    } else {
        throw new Exception("Unknown combination of data type=" + pixels.getClass().toString() + " and ColorModel=" + colorModel.getClass().toString());
    }
}
Also used : ColorModel(java.awt.image.ColorModel) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) PixelGrabber(java.awt.image.PixelGrabber) IOException(java.io.IOException) IndexColorModel(java.awt.image.IndexColorModel)

Example 48 with PixelGrabber

use of java.awt.image.PixelGrabber in project hale by halestudio.

the class ImageMarker method hasAlpha.

/**
 * Determines if an image has transparent pixels
 *
 * @param image the {@link Image}
 * @return if the given 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) {
    // ignore
    }
    // 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 49 with PixelGrabber

use of java.awt.image.PixelGrabber in project intellij-plugins by JetBrains.

the class ImageUtil method write.

public static void write(VirtualFile file, String mimeType, FileOutputStream out) throws IOException {
    Image image = Toolkit.getDefaultToolkit().createImage(file.contentsToByteArray());
    PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, -1, -1, true);
    try {
        pixelGrabber.grabPixels();
    } catch (InterruptedException ignored) {
        throw new IOException("Failed to grab pixels for image " + file.getPresentableUrl());
    }
    if (((pixelGrabber.getStatus() & ImageObserver.WIDTH) == 0) || ((pixelGrabber.getStatus() & ImageObserver.HEIGHT) == 0)) {
        throw new IOException("Failed to grab pixels for image " + file.getPresentableUrl());
    }
    final byte[] byteBuffer = BUFFER.get();
    IOUtil.writeShort(pixelGrabber.getWidth(), byteBuffer, 0);
    IOUtil.writeShort(pixelGrabber.getHeight(), byteBuffer, 2);
    byteBuffer[4] = (byte) ((mimeType == null ? file.getName().endsWith(".jpg") : mimeType.equals("image/jpeg")) ? 0 : 1);
    out.write(byteBuffer, 0, 5);
    int bufferLength = 0;
    for (int pixel : (int[]) pixelGrabber.getPixels()) {
        byteBuffer[bufferLength++] = (byte) ((pixel >> 24) & 0xff);
        byteBuffer[bufferLength++] = (byte) ((pixel >> 16) & 0xff);
        byteBuffer[bufferLength++] = (byte) ((pixel >> 8) & 0xff);
        byteBuffer[bufferLength++] = (byte) (pixel & 0xff);
        if (bufferLength == MAX_BUFFER_LENGTH) {
            //noinspection ConstantConditions
            out.write(byteBuffer, 0, bufferLength);
            bufferLength = 0;
        }
    }
    if (bufferLength > 0) {
        out.write(byteBuffer, 0, bufferLength);
    }
}
Also used : PixelGrabber(java.awt.image.PixelGrabber) IOException(java.io.IOException)

Example 50 with PixelGrabber

use of java.awt.image.PixelGrabber in project intellij-community by JetBrains.

the class UIUtil method getColorAt.

@Nullable
public static Color getColorAt(final Icon icon, final int x, final int y) {
    if (0 <= x && x < icon.getIconWidth() && 0 <= y && y < icon.getIconHeight()) {
        final BufferedImage image = createImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        icon.paintIcon(null, image.getGraphics(), 0, 0);
        final int[] pixels = new int[1];
        final PixelGrabber pixelGrabber = new PixelGrabber(image, x, y, 1, 1, pixels, 0, 1);
        try {
            pixelGrabber.grabPixels();
            return new Color(pixels[0]);
        } catch (InterruptedException ignored) {
        }
    }
    return null;
}
Also used : PixelGrabber(java.awt.image.PixelGrabber) BufferedImage(java.awt.image.BufferedImage) Nullable(org.jetbrains.annotations.Nullable)

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