/** * Gets the transparency of an image. * * @param image the image * @return OPAQUE, BITMASK or TRANSLUCENT (see java.awt.Transparency) */ public static int getTransparency( Image image ) { // If buffered image, the color model is readily available if ( image instanceof BufferedImage ) { BufferedImage bimage = (BufferedImage) image; return bimage.getColorModel().getTransparency(); } // 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(); int transparency = Transparency.OPAQUE; if ( cm != null ) { transparency = cm.getTransparency(); } return transparency; }
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(); if (pixels[0] == -1) return Color.WHITE; // System background not customized } catch (InterruptedException e) { return getNonGTKProfilerResultsBackground(); } return pixels[0] != -1 ? new Color(pixels[0]) : getNonGTKProfilerResultsBackground(); }
/** * For the given input color, return the color that this color * will map to in an offscreen image created by the given Component */ public static int getOffscreenEquivalent(int color, Component obs) { Image im = obs.createImage(1, 1); Graphics2D g = (Graphics2D) im.getGraphics(); g.setColor(new java.awt.Color(color)); g.fillRect(0, 0, 1, 1); g.dispose(); int[] bg = new int[1]; PixelGrabber pg = new PixelGrabber(im, 0, 0, 1, 1, bg, 0, 1); try { pg.grabPixels(); } catch (InterruptedException ex) { logger.error("", ex); } return bg[0]; }
public static int[] getPixels(Image img, int x, int y, int w, int h) { int[] pixels = new int[w * h]; // PixelGrabber does the work of getting // actual RGB pixel values for // us from // the image. PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); } return pixels; }
public static boolean hasAlpha(Image image) { if (image instanceof BufferedImage) return ((BufferedImage)image).getColorModel().hasAlpha(); PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); try { pg.grabPixels(); } catch (InterruptedException e) { } return pg.getColorModel().hasAlpha(); }
static private Image bressed(final Image image) { final int i = image.getHeight(null); final int i340 = image.getWidth(null); final int[] is = new int[i340 * i]; final PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, i340, i, is, 0, i340); try { pixelgrabber.grabPixels(); } catch (final InterruptedException ignored) { } final Color color = new Color(247, 255, 165); for (int i341 = 0; i341 < i340 * i; i341++) if (is[i341] != is[i340 * i - 1]) { is[i341] = color.getRGB(); } return xt.createImage(new MemoryImageSource(i340, i, is, 0, i340)); }
static private Image pressed(final Image image) { final int i = image.getHeight(null); final int i337 = image.getWidth(null); final int[] is = new int[i337 * i]; final PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, i337, i, is, 0, i337); try { pixelgrabber.grabPixels(); } catch (final InterruptedException ignored) { } for (int i338 = 0; i338 < i337 * i; i338++) if (is[i338] != is[i337 * i - 1]) { is[i338] = -16777216; } return xt.createImage(new MemoryImageSource(i337, i, is, 0, i337)); }
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(); }
/** * Get the image to write. This is the mosaic image with alpha channel stripped, as this * doesn't work with the JPEG export. */ private BufferedImage getImageToWrite() throws InterruptedException { BufferedImage image = SwingFXUtils.fromFXImage(mainController.getMosaicImage(), null); final int[] RGB_MASKS = {0xFF0000, 0xFF00, 0xFF}; final ColorModel rgbOpaque = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]); PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, true); pg.grabPixels(); int width = pg.getWidth(), height = pg.getHeight(); DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight()); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null); BufferedImage bi = new BufferedImage(rgbOpaque, raster, false, null); return bi; }
private boolean convertImage(Image parImage, int parWidth, int parHeight) { int pad; bitmap = new int[parWidth * parHeight]; final PixelGrabber pg = new PixelGrabber(parImage, 0, 0, parWidth, parHeight, bitmap, 0, parWidth); try { pg.grabPixels(); } catch (final InterruptedException e) { e.printStackTrace(); return (false); } pad = (4 - ((parWidth * 3) % 4)) * parHeight; biSizeImage = ((parWidth * parHeight) * 3) + pad; bfSize = biSizeImage + BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE; biWidth = parWidth; biHeight = parHeight; return (true); }
/** * Returns the transparency. Returns either OPAQUE, BITMASK, or TRANSLUCENT. * * @param image * The image. * @return the transparency of this ColorModel. */ private int getTransparency(Image image) { if (image instanceof BufferedImage) { BufferedImage bimage = (BufferedImage) image; return bimage.getColorModel().getTransparency(); } PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); try { if (pg.grabPixels() && pg.getColorModel() != null) { return pg.getColorModel().getTransparency(); } } catch (InterruptedException e) { LOGGER.warn(e.getMessage()); } // fallback to generic type return Transparency.TRANSLUCENT; }
/** * Use a PixelGrabber to grab the pixel data from the camera stream. * */ public void grabPixels ( ) { // Get the width and height of the camera image. int width = this.cameraStream.getWidth( this ); int height = this.cameraStream.getHeight( this ); // Create a new pixels array with a given width and height. this.pixels = new int[ width * height ]; // Create a new PixelGrabber using the camera stream and place the grabbed // pixel data into this.pixels. this.grabber = new PixelGrabber( this.cameraStream, 0, 0, width, height, this.pixels, 0, width ); try { // Grab the pixels. this.grabber.grabPixels( ); } catch ( Exception e ) { log.error("Failed to grab pixels.", e); } // end catch }
public Sprite(byte abyte0[], Component component) { try { Image image = Toolkit.getDefaultToolkit().createImage(abyte0); MediaTracker mediatracker = new MediaTracker(component); mediatracker.addImage(image, 0); mediatracker.waitForAll(); myWidth = image.getWidth(component); myHeight = image.getHeight(component); anInt1444 = myWidth; anInt1445 = myHeight; drawOffsetX = 0; drawOffsetY = 0; myPixels = new int[myWidth * myHeight]; PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, myWidth, myHeight, myPixels, 0, myWidth); pixelgrabber.grabPixels(); } catch (Exception _ex) { System.out.println("Error converting jpg"); } }
public Sprite(String img, int width, int height) { try { Image image = Toolkit.getDefaultToolkit().createImage(FileOperations.ReadFile(img)); myWidth = width; myHeight = height; anInt1444 = myWidth; anInt1445 = myHeight; drawOffsetX = 0; drawOffsetY = 0; myPixels = new int[myWidth * myHeight]; PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, myWidth, myHeight, myPixels, 0, myWidth); pixelgrabber.grabPixels(); image = null; } catch (Exception _ex) { System.out.println(_ex); } }
public Sprite(String img) { try { Image image = Toolkit.getDefaultToolkit().getImage(location + img + ".png"); ImageIcon sprite = new ImageIcon(image); myWidth = sprite.getIconWidth(); myHeight = sprite.getIconHeight(); anInt1444 = myWidth; anInt1445 = myHeight; drawOffsetX = 0; drawOffsetY = 0; myPixels = new int[myWidth * myHeight]; PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, myWidth, myHeight, myPixels, 0, myWidth); pixelgrabber.grabPixels(); image = null; setTransparency(255, 0, 255); } catch (Exception _ex) { System.out.println(_ex); } }
public Sprite(byte spriteData[]) { try { Image image = Toolkit.getDefaultToolkit().createImage(spriteData); ImageIcon sprite = new ImageIcon(image); myWidth = sprite.getIconWidth(); myHeight = sprite.getIconHeight(); anInt1444 = myWidth; anInt1445 = myHeight; drawOffsetX = 0; drawOffsetY = 0; myPixels = new int[myWidth * myHeight]; PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, myWidth, myHeight, myPixels, 0, myWidth); pixelgrabber.grabPixels(); image = null; setTransparency(255, 0, 255); } catch (Exception _ex) { System.out.println(_ex); } }
@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; }
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(); }
/** * The next two mehtds where got from http://javaalmanac * .com/egs/java.awt.image/HasAlpha.html T * * @param image * @return */ // 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(); }
/** * Given an image, this method extracts a color that is calculated as the * average of the colors of the image * * @param image the given image * @return the "average" color */ public static Color getAverageColor(BufferedImage image) { if (image == null) { return null; } Image i = image.getScaledInstance(1, 1, BufferedImage.SCALE_SMOOTH); int[] pixels = new int[1]; PixelGrabber pg = new PixelGrabber(i, 0, 0, 1, 1, pixels, 0, 1); try { pg.grabPixels(); } catch (InterruptedException e) { } int c = pixels[0]; int red = (c & 0x00ff0000) >> 16; int green = (c & 0x0000ff00) >> 8; int blue = c & 0x000000ff; return new Color(red, green, blue); }
static private void readImage( final String urlString, final int w, final int h, final int[] data ) throws IOException, InterruptedException { final URL url = new URL( urlString ); final BufferedImage jpg = ImageIO.read( url ); /* This gymnastic is necessary to get reproducible gray * values, just opening a JPG or PNG, even when saved by * ImageIO, and grabbing its pixels results in gray values * with a non-matching gamma transfer function, I cannot tell * why... */ final BufferedImage image = new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB ); image.createGraphics().drawImage( jpg, 0, 0, null ); final PixelGrabber pg = new PixelGrabber( image, 0, 0, w, h, data, 0, w ); pg.grabPixels(); }
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(); }
/** * Returns the Color of point x, y on the image. Uses * <code>PixelGrabber</code> to get the exact color. * * @param x Coordinate on the image. * @param y Coordinate on the image. * @return The Color of the specified point. */ public Color getPointColor(int x, int y) { // return new Color(((BufferedImage) img).getRGB(x, y)); int w = 1, h = 1; int[] pixels = new int[w * h]; PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return null; } int alpha, blue, red, green; alpha = (pixels[0] >> 24) & 0xff; red = (pixels[0] >> 16) & 0xff; green = (pixels[0] >> 8) & 0xff; blue = (pixels[0]) & 0xff; return new Color(red, green, blue, alpha); }
/** * This method returns true if the specified image has transparent pixels * @param image * @return */ public static boolean hasAlpha(java.awt.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(); }
/** * Cretae a BufferedImage from an ImageProducer. * @param producer the ImageProducer * @return a new TYPE_INT_ARGB BufferedImage */ public static BufferedImage createImage(ImageProducer producer) { PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("Image fetch interrupted"); } if ((pg.status() & ImageObserver.ABORT) != 0) throw new RuntimeException("Image fetch aborted"); if ((pg.status() & ImageObserver.ERROR) != 0) throw new RuntimeException("Image fetch error"); BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB); p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth()); return p; }
/** * This method returns true if the specified image has transparent pixels * * @param image an image. * * @return {@code true} if the image has transparent pixels, {@code false} * otherwise. */ private 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(); }
/** * Cretae a BufferedImage from an ImageProducer. * @param producer the ImageProducer * @return a new TYPE_INT_ARGB BufferedImage */ public static BufferedImage createImage(ImageProducer producer) { PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("Image fetch interrupted"); } if ((pg.status() & ImageObserver.ABORT) != 0) { throw new RuntimeException("Image fetch aborted"); } if ((pg.status() & ImageObserver.ERROR) != 0) { throw new RuntimeException("Image fetch error"); } BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB); p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth()); return p; }
private static int[][] getPixels(Image image) throws IOException { int w = image.getWidth(null); int h = image.getHeight(null); int pix[] = new int[w * h]; PixelGrabber grabber = new PixelGrabber(image, 0, 0, w, h, pix, 0, w); try { if (!grabber.grabPixels()) { throw new IOException("Grabber returned false: " + grabber.status()); } } catch (InterruptedException e) { e.printStackTrace(); } int pixels[][] = new int[w][h]; for (int x = w; x-- > 0; ) { for (int y = h; y-- > 0; ) { pixels[x][y] = pix[y * w + x]; } } return pixels; }
/** * Snag the pixels from an image. */ public static int[][] getPixels(Image image) throws IOException { int w = image.getWidth(null); int h = image.getHeight(null); int pix[] = new int[w * h]; PixelGrabber grabber = new PixelGrabber(image, 0, 0, w, h, pix, 0, w); try { if (!grabber.grabPixels()) { throw new IOException("Grabber returned false: " + grabber.status()); } } catch (InterruptedException e) { e.printStackTrace(); } int pixels[][] = new int[w][h]; for (int x = w; x-- > 0; ) { for (int y = h; y-- > 0; ) { pixels[x][y] = pix[y * w + x]; } } return pixels; }
/** * This method returns {@code true} if the specified image has the * possibility to store transparent pixels. * Inspired by http://www.exampledepot.com/egs/java.awt.image/HasAlpha.html * @param image Image that should be checked for alpha channel. * @return {@code true} if the specified image can have transparent pixels, * {@code false} otherwise */ public static boolean hasAlpha(Image image) { ColorModel cm; // If buffered image, the color model is readily available if (image instanceof BufferedImage) { BufferedImage bimage = (BufferedImage) image; cm = bimage.getColorModel(); } else { // 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) { return false; } // Get the image's color model cm = pg.getColorModel(); } return cm.hasAlpha(); }
private 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(); } // 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(); }
public static PixelGrabber getPixelGrabber(Image image, String location) { PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, -1, -1, true); try { pixelGrabber.grabPixels(); } catch (InterruptedException interruptedException) { if (Trace.error) { interruptedException.printStackTrace(); } throw new RuntimeException("Failed to grab pixels for image " + location); } if (((pixelGrabber.getStatus() & ImageObserver.WIDTH) == 0) || ((pixelGrabber.getStatus() & ImageObserver.HEIGHT) == 0)) { throw new RuntimeException("Failed to grab pixels for image " + location); } return pixelGrabber; }
private void init(Image image) { PixelGrabber pixelGrabber = ImageUtil.getPixelGrabber(image, location); width = pixelGrabber.getWidth(); height = pixelGrabber.getHeight(); Object p = pixelGrabber.getPixels(); if (p != null) { Class ct = p.getClass().getComponentType(); if (ct != null) { if (ct.equals(Integer.TYPE)) pixels = (int[])p; else if (ct.equals(Byte.TYPE)) throw new IllegalStateException("int[] of pixels expected, received byte[] instead."); } } }