List of usage examples for java.awt.image PixelGrabber grabPixels
public boolean grabPixels() throws InterruptedException
From source file:ImageUtils.java
/** * This method returns true if the specified image has transparent pixels * This snippet was taken from: http://www.exampledepot.com/egs/java.awt.image/HasAlpha.html * @param image//w ww . jav a 2 s. c o m * @return True if the image has any transparency */ 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(); }
From source file:org.wings.SImageIcon.java
protected static String determineMimeType(ImageIcon imageIcon) { String mimeType = extractMimeTypeFromPath(imageIcon.getDescription()); if (mimeType != null) return mimeType; PixelGrabber pg = new PixelGrabber(imageIcon.getImage(), 0, 0, 1, 1, false); try {//w w w.j ava 2s . c o m pg.grabPixels(); } catch (InterruptedException e) { log.warn("interrupted waiting for pixels!"); } mimeType = "image/"; if (!(pg.getColorModel() instanceof IndexColorModel)) mimeType += ImageExternalizer.FORMAT_PNG; else mimeType += ImageExternalizer.FORMAT_GIF; return mimeType; }
From source file:script.imglib.analysis.ChartUtils.java
public static final Image<RGBALegacyType> asImage(final JFreeChart chart, int width, int height) { ChartPanel panel = new ChartPanel(chart); Dimension d = panel.getPreferredSize(); if (-1 == width && -1 == height) { width = d.width;// w w w.ja va 2 s . c om height = d.height; panel.setSize(d); } else { panel.setSize(width, height); } layoutComponent(panel); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = bi.createGraphics(); if (!panel.isOpaque()) { g.setColor(panel.getBackground()); g.fillRect(0, 0, width, height); } panel.paint(g); int[] pixels = new int[width * height]; PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { } g.dispose(); Array<RGBALegacyType, IntAccess> a = new Array<RGBALegacyType, IntAccess>(new ArrayContainerFactory(), new IntArray(pixels), new int[] { width, height }, 1); // create a Type that is linked to the container final RGBALegacyType linkedType = new RGBALegacyType(a); // pass it to the DirectAccessContainer a.setLinkedType(linkedType); return new Image<RGBALegacyType>(a, new RGBALegacyType()); }
From source file:net.imglib2.script.analysis.ChartUtils.java
public static final Img<ARGBType> asImage(final JFreeChart chart, int width, int height) { final ChartPanel panel = new ChartPanel(chart); final Dimension d = panel.getPreferredSize(); if (-1 == width && -1 == height) { width = d.width;/*from w w w.j a v a2s .com*/ height = d.height; panel.setSize(d); } else { panel.setSize(width, height); } layoutComponent(panel); final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); final Graphics2D g = bi.createGraphics(); if (!panel.isOpaque()) { g.setColor(panel.getBackground()); g.fillRect(0, 0, width, height); } panel.paint(g); final int[] pixels = new int[width * height]; final PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width); try { pg.grabPixels(); } catch (final InterruptedException e) { } g.dispose(); final ArrayImg<ARGBType, IntArray> a = new ArrayImg<ARGBType, IntArray>(new IntArray(pixels), new long[] { width, height }, 1); // create a Type that is linked to the container final ARGBType linkedType = new ARGBType(a); // pass it to the DirectAccessContainer a.setLinkedType(linkedType); return a; }
From source file:FontAlgo.java
private static TextualChar getTextualChar(char a_char) throws Throwable { BufferedImage bImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = bImg.getGraphics(); g.setColor(Color.green);/*from w w w .j a v a2 s .co m*/ g.fillRect(0, 0, WIDTH, HEIGHT); g.setFont(appliedFont); g.setColor(Color.black); g.drawString(new String(new char[] { a_char }), 10, g.getFontMetrics().getHeight()); PixelGrabber p = new PixelGrabber(bImg, 0, 0, WIDTH, HEIGHT, true); if (p.grabPixels()) { char[][] pattern = new char[WIDTH][HEIGHT]; int baseColourPixel = 0, contrastColourPixel = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0; int[] pixels = (int[]) p.getPixels(); baseColourPixel = pixels[0]; // System.out.println("base: " + base); int xCounter = 0, yCounter = 0; for (int iPixel : pixels) { // System.out.println(iX + " - " + iY); if (isReverse) { pattern[xCounter][yCounter] = iPixel == baseColourPixel ? CHAR_TO_PATTERN : ' '; } else { pattern[xCounter][yCounter] = iPixel != baseColourPixel ? CHAR_TO_PATTERN : ' '; } yCounter++; if (yCounter > 49) { xCounter++; yCounter = 0; } if (contrastColourPixel == 0 && iPixel != baseColourPixel) { contrastColourPixel = iPixel; x1 = xCounter - 2; y1 = yCounter - 3; y2 = yCounter + 3; } if (contrastColourPixel == iPixel) { x2 = xCounter + 3; if (y1 > (yCounter - 3)) { y1 = yCounter - 3; } if (y2 < (yCounter + 3)) { y2 = yCounter + 3; } } } return new TextualChar(x1, x2, y1, y2, pattern); } return null; }
From source file:com.jaeksoft.searchlib.util.ImageUtils.java
public static boolean hasAlpha(Image image) throws InterruptedException { if (image instanceof BufferedImage) return ((BufferedImage) image).getColorModel().hasAlpha(); PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); pg.grabPixels(); return pg.getColorModel().hasAlpha(); }
From source file:ImageUtils.java
/** * Determines if the image has transparent pixels. * //w w w . ja v a 2 s . c o m * @param image The image to check for transparent pixel.s * @return <code>true</code> of <code>false</code>, according to the result */ public static boolean hasAlpha(Image image) { try { PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); pg.grabPixels(); return pg.getColorModel().hasAlpha(); } catch (InterruptedException e) { return false; } }
From source file:ro.nextreports.designer.util.ImageUtil.java
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(); }/*from w ww. j a va2s.co m*/ // 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) { e.printStackTrace(); } // Get the image's color model ColorModel cm = pg.getColorModel(); return cm.hasAlpha(); }
From source file:net.rptools.lib.image.ImageUtil.java
/** * Look at the image and determine which Transparency is most appropriate. If it finds any translucent pixels it * returns Transparency.TRANSLUCENT, if it finds at least one purely transparent pixel and no translucent pixels it * will return Transparency.BITMASK, in all other cases it returns Transparency.OPAQUE, including errors * /* www .j av a 2 s . c om*/ * @param image * @return one of Transparency constants */ public static int pickBestTransparency(Image image) { // Take a shortcut if possible if (image instanceof BufferedImage) { return pickBestTransparency((BufferedImage) image); } // Legacy method // NOTE: This is a horrible memory hog int width = image.getWidth(null); int height = image.getHeight(null); int[] pixelArray = new int[width * height]; PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixelArray, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return Transparency.OPAQUE; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return Transparency.OPAQUE; } // Look for specific pixels boolean foundTransparent = false; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get the next pixel int pixel = pixelArray[y * width + x]; int alpha = (pixel >> 24) & 0xff; // Is there translucency or just pure transparency ? if (alpha > 0 && alpha < 255) { return Transparency.TRANSLUCENT; } if (alpha == 0 && !foundTransparent) { foundTransparent = true; } } } return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE; }
From source file:com.t3.image.ImageUtil.java
/** * Look at the image and determine which Transparency is most appropriate. * If it finds any translucent pixels it returns Transparency.TRANSLUCENT, if * it finds at least one purely transparent pixel and no translucent pixels * it will return Transparency.BITMASK, in all other cases it returns * Transparency.OPAQUE, including errors * /* www . j a v a2 s. c o m*/ * @param image * @return one of Transparency constants */ public static int pickBestTransparency(Image image) { // Take a shortcut if possible if (image instanceof BufferedImage) { return pickBestTransparency((BufferedImage) image); } // Legacy method // NOTE: This is a horrible memory hog int width = image.getWidth(null); int height = image.getHeight(null); int[] pixelArray = new int[width * height]; PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixelArray, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return Transparency.OPAQUE; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return Transparency.OPAQUE; } // Look for specific pixels boolean foundTransparent = false; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get the next pixel int pixel = pixelArray[y * width + x]; int alpha = (pixel >> 24) & 0xff; // Is there translucency or just pure transparency ? if (alpha > 0 && alpha < 255) { return Transparency.TRANSLUCENT; } if (alpha == 0 && !foundTransparent) { foundTransparent = true; } } } return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE; }