Java Code Examples for java.awt.image.PixelGrabber

The following examples show how to use java.awt.image.PixelGrabber. These examples are extracted from open source projects. 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 Project: netbeans   Source File: UIUtils.java    License: Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 2
Source Project: visualvm   Source File: UIUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 3
Source Project: selenium-shutterbug   Source File: ImageProcessor.java    License: MIT License 6 votes vote down vote up
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 ignored) {
    }
    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}
 
Example 4
Source Project: seaglass   Source File: Effect.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 5
Source Project: openbd-core   Source File: ImageUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
    * 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;
}
 
Example 6
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
private static int[] makeGradientPallet(int range) {
  BufferedImage image = new BufferedImage(range, 1, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = image.createGraphics();
  Point2D start = new Point2D.Float();
  Point2D end = new Point2D.Float(range - 1f, 0f);
  float[] dist = {0f, .8f, .9f, 1f};
  Color[] colors = {Color.GREEN, Color.YELLOW, Color.ORANGE, Color.RED};
  g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
  g2.fillRect(0, 0, range, 1);
  g2.dispose();

  int width = image.getWidth(null);
  int[] pallet = new int[width];
  PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
  try {
    pg.grabPixels();
  } catch (InterruptedException ex) {
    ex.printStackTrace();
    Toolkit.getDefaultToolkit().beep();
    Thread.currentThread().interrupt();
  }
  return pallet;
}
 
Example 7
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
private static int[] makeGradientPallet() {
  BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = image.createGraphics();
  Point2D start = new Point2D.Float();
  Point2D end = new Point2D.Float(99f, 0f);
  float[] dist = {0f, .5f, 1f};
  Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
  g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
  g2.fillRect(0, 0, 100, 1);
  g2.dispose();

  int width = image.getWidth(null);
  int[] pallet = new int[width];
  PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
  try {
    pg.grabPixels();
  } catch (InterruptedException ex) {
    ex.printStackTrace();
    Toolkit.getDefaultToolkit().beep();
    Thread.currentThread().interrupt();
  }
  return pallet;
}
 
Example 8
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
public static int[] makeGradientPallet() {
  BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = image.createGraphics();
  Point2D start = new Point2D.Float();
  Point2D end = new Point2D.Float(99f, 0f);
  float[] dist = {.0f, .5f, 1f};
  Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
  g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
  g2.fillRect(0, 0, 100, 1);
  g2.dispose();

  int width = image.getWidth(null);
  int[] pallet = new int[width];
  PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
  try {
    pg.grabPixels();
  } catch (InterruptedException ex) {
    ex.printStackTrace();
    Toolkit.getDefaultToolkit().beep();
    Thread.currentThread().interrupt();
  }
  return pallet;
}
 
Example 9
Source Project: RipplePower   Source File: GraphicsUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 将黑色颜色部分透明化
 * 
 * @param img
 * @return
 */
final static public Image transparencyBlackColor(final Image img) {
	int width = img.getWidth(null);
	int height = img.getHeight(null);
	PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, true);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	int pixels[] = (int[]) pg.getPixels();
	int length = pixels.length;
	for (int i = 0; i < length; i++) {
		if (pixels[i] == 0) {
			pixels[i] = 0xffffff;
		}
	}
	return toolKit.createImage(new MemoryImageSource(width, height, pixels, 0, width));
}
 
Example 10
Source Project: RipplePower   Source File: GraphicsUtils.java    License: Apache License 2.0 6 votes vote down vote up
final static public Image transparencyBlackColor(final Image img, final Color c) {
	int width = img.getWidth(null);
	int height = img.getHeight(null);
	PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, true);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	int pixels[] = (int[]) pg.getPixels();
	int length = pixels.length;
	for (int i = 0; i < length; i++) {
		int pixel = pixels[i];
		int[] rgbs = LColor.getRGBs(pixel);
		if (rgbs[0] >= 252 && rgbs[1] >= 252 && rgbs[1] >= 252) {
			pixels[i] = 0xffffff;
		}
	}
	return toolKit.createImage(new MemoryImageSource(width, height, pixels, 0, width));
}
 
Example 11
Source Project: RipplePower   Source File: GraphicsUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 生成Image的hash序列
 * 
 * @param img
 * @return
 */
public static int hashImage(Image img) {
	int hash_result = 0;
	int width = img.getWidth(null);
	int height = img.getHeight(null);
	hash_result = (hash_result << 7) ^ height;
	hash_result = (hash_result << 7) ^ width;
	PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, true);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	int pixels[] = (int[]) pg.getPixels();
	for (int pixel = 0; pixel < 20; ++pixel) {
		int x = (pixel * 50) % width;
		int y = (pixel * 100) % height;
		hash_result = (hash_result << 7) ^ pixels[x + width * y];
	}
	return hash_result;
}
 
Example 12
Source Project: jpexs-decompiler   Source File: BMPFile.java    License: GNU General Public License v3.0 6 votes vote down vote up
private boolean convertImage(Image parImage, int parWidth, int parHeight) {
    int pad;
    bitmap = new int[parWidth * parHeight];
    PixelGrabber pg = new PixelGrabber(parImage, 0, 0, parWidth, parHeight,
            bitmap, 0, parWidth);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        return (false);
    }

    pad = ((4 - ((parWidth * 3) % 4)) % 4) * parHeight;
    biSizeImage = ((parWidth * parHeight) * 3) + pad;
    bfSize = biSizeImage + BITMAPFILEHEADER_SIZE
            + BITMAPINFOHEADER_SIZE;
    biWidth = parWidth;
    biHeight = parHeight;
    return (true);
}
 
Example 13
Source Project: PolyGlot   Source File: LogoNode.java    License: MIT License 6 votes vote down vote up
/**
 * Tests pixel for pixel equality of images
 * @param image1
 * @param image2
 * @return 
 */
private boolean imagesEqual(Image image1, Image image2) {
    boolean ret;
    try {
        PixelGrabber grabImage1Pixels = new PixelGrabber(image1, 0, 0, -1, -1, false);
        PixelGrabber grabImage2Pixels = new PixelGrabber(image2, 0, 0, -1, -1, false);

        int[] image1Data = null;

        if (grabImage1Pixels.grabPixels()) {
            image1Data = (int[]) grabImage1Pixels.getPixels();
        }

        int[] image2Data = null;

        if (grabImage2Pixels.grabPixels()) {
            image2Data = (int[]) grabImage2Pixels.getPixels();
        }

        ret = Arrays.equals(image1Data, image2Data);
    } catch (InterruptedException e) {
        ret = false;
    }
    
    return ret;
}
 
Example 14
Source Project: Face-Recognition   Source File: Picture.java    License: GNU General Public License v3.0 6 votes vote down vote up
public double[] getImagePixels() {
	
	int w = img.getWidth(this);
	int h = img.getHeight(this);
	int[] pixels = new int[w * h];
	PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		System.err.println("interrupted waiting for pixels!");
		return new double[0];
	}
	if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
		System.err.println("image fetch aborted or errored");
		return new double[0];
	}
	double[] ret =new double[w*h];
	ColorModel cm = pg.getColorModel();
	for (int i=0; i<ret.length; i++)
	{
		ret[i] = cm.getBlue(pixels[i]) + cm.getGreen(pixels[i]) + cm.getRed(pixels[i]);
		ret[i] /= 3.0;
	}
	return ret;
}
 
Example 15
Source Project: Face-Recognition   Source File: Picture.java    License: GNU General Public License v3.0 6 votes vote down vote up
public double[] getImageColourPixels() {
    
    int w = img.getWidth(this);
    int h = img.getHeight(this);
    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println("interrupted waiting for pixels!");
        return new double[0];
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        System.err.println("image fetch aborted or errored");
        return new double[0];
    }
    double[] ret =new double[w*h];
    ColorModel cm = pg.getColorModel();
    for (int i=0; i<ret.length; i++)
    {
        Color c=new Color(cm.getRed(pixels[i]),cm.getGreen(pixels[i]),cm.getBlue(pixels[i]));
        ret[i]=c.getRGB();
     
    }
    return ret;
}
 
Example 16
protected boolean hasAlpha(Image image) throws ApsSystemException {
       // 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) {
       	throw new ApsSystemException("Error grabbing a single pixel", e);
       }
       // Get the image's color model
       ColorModel cm = pg.getColorModel();
       return cm.hasAlpha();
}
 
Example 17
Source Project: birt   Source File: PostscriptWriter.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private ArrayImageSource getImageSource( Image image ) throws IOException
{
	ImageIcon imageIcon = new ImageIcon( image );
	int w = imageIcon.getIconWidth( );
	int h = imageIcon.getIconHeight( );
	int[] pixels = new int[w * h];

	try
	{
		PixelGrabber pg = new PixelGrabber( image, 0, 0, w, h, pixels, 0, w );
		pg.grabPixels( );
		if ( ( pg.getStatus( ) & ImageObserver.ABORT ) != 0 )
		{
			throw new IOException( "failed to load image contents" );
		}
	}
	catch ( InterruptedException e )
	{
		throw new IOException( "image load interrupted" );
	}

	ArrayImageSource imageSource = new ArrayImageSource( w, h, pixels );
	return imageSource;
}
 
Example 18
Source Project: 317refactor   Source File: Sprite.java    License: MIT License 6 votes vote down vote up
public Sprite(final byte[] abyte0, final Component component) {
	try {
		// Image image =
		// Toolkit.getDefaultToolkit().getImage(signlink.findcachedir()+"mopar.jpg");
		final Image image = Toolkit.getDefaultToolkit().createImage(abyte0);
		final MediaTracker mediatracker = new MediaTracker(component);
		mediatracker.addImage(image, 0);
		mediatracker.waitForAll();
           this.width = image.getWidth(component);
           this.height = image.getHeight(component);
           this.maxWidth = this.width;
           this.maxHeight = this.height;
           this.offsetX = 0;
           this.offsetY = 0;
           this.pixels = new int[this.width * this.height];
		final PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, this.width, this.height, this.pixels, 0, this.width);
		pixelgrabber.grabPixels();
	} catch (final Exception _ex) {
		System.out.println("Error converting jpg");
	}
}
 
Example 19
Source Project: jrobin   Source File: GifEncoder.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
DirectGif89Frame(Image img) throws IOException {
	PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true);
	String errmsg = null;
	try {
		if (!pg.grabPixels()) {
			errmsg = "can't grab pixels from image";
		}
	}
	catch (InterruptedException e) {
		errmsg = "interrupted grabbing pixels from image";
	}
	if (errmsg != null) {
		throw new IOException(errmsg + " (" + getClass().getName() + ")");
	}
	theWidth = pg.getWidth();
	theHeight = pg.getHeight();
	argbPixels = (int[]) pg.getPixels();
	ciPixels = new byte[argbPixels.length];
}
 
Example 20
Source Project: nextreports-designer   Source File: ImageUtil.java    License: Apache License 2.0 6 votes vote down vote up
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) {
        e.printStackTrace();
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}
 
Example 21
Source Project: consulo   Source File: UIUtil.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 22
Source Project: TrakEM2   Source File: Patch.java    License: GNU General Public License v3.0 6 votes vote down vote up
/** Expects x,y in world coordinates.  This method is intended for grabing an occasional pixel; to grab all pixels, see @getImageProcessor method. */
public int[] getPixel(final int x, final int y, final double mag) {
	if (project.getLoader().isUnloadable(this)) return new int[4];
	final MipMapImage mipMap = project.getLoader().fetchImage(this, mag);
	if (Loader.isSignalImage(mipMap.image)) return new int[4];
	final int w = mipMap.image.getWidth(null);
	final Point2D.Double pd = inverseTransformPoint(x, y);
	final int x2 = (int)(pd.x / mipMap.scaleX);
	final int y2 = (int)(pd.y / mipMap.scaleY);
	final int[] pvalue = new int[4];
	final PixelGrabber pg = new PixelGrabber( mipMap.image, x2, y2, 1, 1, pvalue, 0, w);
	try {
		pg.grabPixels();
	} catch (final InterruptedException ie) {
		return pvalue;
	}

	approximateTransferPixel(pvalue);

	return pvalue;
}
 
Example 23
Source Project: TrakEM2   Source File: Patch.java    License: GNU General Public License v3.0 6 votes vote down vote up
/** Considers the alpha mask. */
@Override
public boolean contains(final double x_p, final double y_p) {
	if (!hasAlphaChannel()) return super.contains(x_p, y_p);
	// else, get pixel from image
	if (project.getLoader().isUnloadable(this)) return super.contains(x_p, y_p);
	final MipMapImage mipMap = project.getLoader().fetchImage(this, 0.12499); // TODO ideally, would ask for image within 256x256 dimensions, but that would need knowing the screen image dimensions beforehand, or computing it from the CoordinateTransform, which may be very costly.
	if (Loader.isSignalImage(mipMap.image)) return super.contains(x_p, y_p);
	final int w = mipMap.image.getWidth(null);
	final Point2D.Double pd = inverseTransformPoint(x_p, y_p);
	final int x2 = (int)(pd.x / mipMap.scaleX);
	final int y2 = (int)(pd.y / mipMap.scaleY);
	final int[] pvalue = new int[1];
	final PixelGrabber pg = new PixelGrabber(mipMap.image, x2, y2, 1, 1, pvalue, 0, w);
	try {
		pg.grabPixels();
	} catch (final InterruptedException ie) {
		return super.contains(x_p, y_p);
	}
	// Not true if alpha value is zero
	return 0 != (pvalue[0] & 0xff000000);
}
 
Example 24
Source Project: davmail   Source File: ExeCompiler.java    License: GNU General Public License v2.0 5 votes vote down vote up
private Hashtable calculateColorCount(Image img) {
    int width = img.getWidth(null);
    int height = img.getHeight(null);
    int[] pixels = new int[width * height];
    PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
    try {
        grabber.grabPixels();
    } catch (InterruptedException e) {
        System.err.println("interrupted waiting for pixels!");
        //		    throw new Exception("Can't load the image provided",e);
    }


    Hashtable result = new Hashtable();
    int colorindex = 0;
    for (int i = 0; i < pixels.length; i++) {
        int pix = pixels[i];
        if (((pix >> 24) & 0xFF) > 0) {
            pix &= 0x00FFFFFF;
            Integer pixi = new Integer(pix);
            Object o = result.get(pixi);
            if (o == null) {
                result.put(pixi, new Integer(colorindex++));
            }
            //			if (colorindex > 256)
            //			    return result;
        }
    }
    return result;
}
 
Example 25
Source Project: jivejdon   Source File: ImageUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the image has transparent pixels.
 * 
 * @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;
	}
}
 
Example 26
Source Project: JewelCrawler   Source File: ImageFilter.java    License: GNU General Public License v3.0 5 votes vote down vote up
/** 图像二值化 */
public BufferedImage changeGrey() {
	PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih, pixels, 0, iw);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	// 设定二值化的域值,默认值为100
	int grey = 100;
	// 对图像进行二值化处理,Alpha值保持不变
	ColorModel cm = ColorModel.getRGBdefault();
	for (int i = 0; i < iw * ih; i++) {
		int red, green, blue;
		int alpha = cm.getAlpha(pixels[i]);
		if (cm.getRed(pixels[i]) > grey) {
			red = 255;
		} else {
			red = 0;
		}

		if (cm.getGreen(pixels[i]) > grey) {
			green = 255;
		} else {
			green = 0;
		}

		if (cm.getBlue(pixels[i]) > grey) {
			blue = 255;
		} else {
			blue = 0;
		}

		pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
	}
	// 将数组中的象素产生一个图像
	return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih, pixels, 0, iw));
}
 
Example 27
Source Project: JewelCrawler   Source File: ImageFilter.java    License: GNU General Public License v3.0 5 votes vote down vote up
/** 线性灰度变换 */
public BufferedImage lineGrey() {
	PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih, pixels, 0, iw);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	// 对图像进行进行线性拉伸,Alpha值保持不变
	ColorModel cm = ColorModel.getRGBdefault();
	for (int i = 0; i < iw * ih; i++) {
		int alpha = cm.getAlpha(pixels[i]);
		int red = cm.getRed(pixels[i]);
		int green = cm.getGreen(pixels[i]);
		int blue = cm.getBlue(pixels[i]);

		// 增加了图像的亮度
		red = (int) (1.1 * red + 30);
		green = (int) (1.1 * green + 30);
		blue = (int) (1.1 * blue + 30);
		if (red >= 255) {
			red = 255;
		}
		if (green >= 255) {
			green = 255;
		}
		if (blue >= 255) {
			blue = 255;
		}
		pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
	}

	// 将数组中的象素产生一个图像

	return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih, pixels, 0, iw));
}
 
Example 28
Source Project: scipio-erp   Source File: ImageTransform.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * SCIPIO: Simple copy of a source image to a destination buffered image using a slow but surefire
 * transfer loop. WARN: slow and very slow.
 * Added 2017-07-14.
 * <p>
 * @return the destImage
 */
public static BufferedImage copyToBufferedImageManual(Image srcImage, BufferedImage destImage, RenderingHints renderingHints) {
    Graphics2D g = destImage.createGraphics();
    try {
        if (renderingHints != null) g.setRenderingHints(renderingHints);
        if (srcImage instanceof BufferedImage) {
            // FIXME: very slow
            if (ImageUtil.verboseOn()) Debug.logInfo("Executing manual BufferedImage pixel copy (very slow, but can avoid dithering)", module);
            BufferedImage srcBufImage = ((BufferedImage) srcImage);
            for (int x = 0; x < srcImage.getWidth(null); x++) {
                for (int y = 0; y < srcImage.getHeight(null); y++) {
                    destImage.setRGB(x, y, srcBufImage.getRGB(x, y));
                }
            }
        } else {
            // FIXME: even worse than above! creates a whole copy for nothing.
            if (ImageUtil.verboseOn()) Debug.logInfo("Executing manual Image double pixel copy (extremely slow, but can avoid dithering)", module);
            int[] pixels = new int[srcImage.getWidth(null)*srcImage.getHeight(null)];
            PixelGrabber pg = new PixelGrabber(srcImage, 0, 0, srcImage.getWidth(null),
                    srcImage.getHeight(null), pixels, 0, srcImage.getWidth(null));
            try {
                pg.grabPixels();
            } catch (InterruptedException e) {
                throw new IllegalStateException("Couldn't get image pixels: " + e.getMessage(), e);
            }
            if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
                throw new IllegalStateException("Couldn't get image pixels: aborted");
            }
            for (int x = 0; x < srcImage.getWidth(null); x++) {
                for (int y = 0; y < srcImage.getHeight(null); y++) {
                    destImage.setRGB(x, y, pixels[y*srcImage.getWidth(null)+x]);
                }
            }
        }
    } finally { // SCIPIO: added finally
        g.dispose();
    }
    return destImage;
}
 
Example 29
Source Project: runelite   Source File: ImageUtil.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Converts the buffered image into a sprite image and returns it
 * @param image  The image to be converted
 * @param client Current client instance
 * @return       The buffered image as a sprite image
 */
public static SpritePixels getImageSpritePixels(BufferedImage image, Client client)
{
	int[] pixels = new int[image.getWidth() * image.getHeight()];

	try
	{
		PixelGrabber g = new PixelGrabber(image, 0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
		g.setColorModel(new DirectColorModel(32, 0xff0000, 0xff00, 0xff, 0xff000000));
		g.grabPixels();

		// Make any fully transparent pixels fully black, because the sprite draw routines
		// check for == 0, not actual transparency
		for (int i = 0; i < pixels.length; i++)
		{
			if ((pixels[i] & 0xFF000000) == 0)
			{
				pixels[i] = 0;
			}
		}
	}
	catch (InterruptedException ex)
	{
		log.debug("PixelGrabber was interrupted: ", ex);
	}

	return client.createSpritePixels(pixels, image.getWidth(), image.getHeight());
}
 
Example 30
Source Project: mars-sim   Source File: StarRater.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts an image to a compressed byte array. GZIPs the image to reduce the size. Use compressedByteArrayToImage(byte[] data) to
 * retrieve the original image. The image is not recognizable as image from standard tools.
 *
 * @param image  The image to convert.
 * @return  The byte array.
 * @throws IOException if something goes wrong.
 */
public byte[] imageToCompressedByteArray(Image image) throws IOException {
  // get image size
  int width = image.getWidth(null);
  int height = image.getHeight(null);

  // store image data as raw int values
  try {
    int[] imageSource = new int[width * height];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, imageSource, 0, width);
    pg.grabPixels();

    // zip data
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    GZIPOutputStream zippedStream = new GZIPOutputStream(byteStream);
    ObjectOutputStream objectStream = new ObjectOutputStream(zippedStream);
    objectStream.writeShort(width);
    objectStream.writeShort(height);
    objectStream.writeObject(imageSource);
    objectStream.flush();
    objectStream.close();
    return byteStream.toByteArray();
  }
  catch (Exception e) {
    throw new IOException("Error storing image in object: " + e);
  }
}