Java Code Examples for java.awt.image.PixelGrabber#getStatus()

The following examples show how to use java.awt.image.PixelGrabber#getStatus() . 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 File: Picture.java    From Face-Recognition with 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 2
Source File: Picture.java    From Face-Recognition with 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 3
Source File: PostscriptWriter.java    From birt with 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 4
Source File: ImageTransform.java    From scipio-erp with 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 5
Source File: BarCode.java    From util with Apache License 2.0 4 votes vote down vote up
protected Image zlkj(Image image, int i, int j, int k)
{
    int l = image.getWidth(null);
    int i1 = image.getHeight(null);
    if(j > l)
        j = l;
    if(k > i1)
        k = i1;
    int ai[] = new int[l * i1];
    int ai1[] = new int[j * k];
    PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, l, i1, ai, 0, l);
    try
    {
        pixelgrabber.grabPixels();
    }
    catch(InterruptedException interruptedexception)
    {
        System.err.println("interrupted waiting for pixels!");
        return null;
    }
    if((pixelgrabber.getStatus() & 0x80) != 0)
    {
        System.err.println("image fetch aborted or errored");
        return null;
    }
    if(i == 90)
    {
        for(int j1 = 0; j1 < j; j1++)
        {
            for(int i2 = 0; i2 < k; i2++)
                ai1[k * (j - (j1 + 1)) + i2] = ai[i2 * l + j1];

        }

        return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(k, j, ai1, 0, k));
    }
    if(i == 180)
    {
        for(int k1 = 0; k1 < j; k1++)
        {
            for(int j2 = 0; j2 < k; j2++)
                ai1[(k - (j2 + 1)) * j + (j - (k1 + 1))] = ai[j2 * l + k1];

        }

        return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(j, k, ai1, 0, j));
    }
    if(i == 270)
    {
        for(int l1 = 0; l1 < j; l1++)
        {
            for(int k2 = 0; k2 < k; k2++)
                ai1[k * l1 + (k - (k2 + 1))] = ai[k2 * l + l1];

        }

        return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(k, j, ai1, 0, k));
    } else
    {
        return null;
    }
}
 
Example 6
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private static Optional<MemoryImageSource> makeRoundedMemoryImageSource(Image image, int width, int height) {
  int[] pix = new int[height * width];
  PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pix, 0, width);
  try {
    pg.grabPixels();
  } catch (InterruptedException ex) {
    System.err.println("interrupted waiting for pixels!");
    ex.printStackTrace();
    Thread.currentThread().interrupt();
    return Optional.empty();
  }
  if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
    System.err.println("image fetch aborted or errored");
    return Optional.empty();
  }

  Area area = makeNorthWestConer();
  Rectangle r = area.getBounds();

  Shape s = area; // NW
  for (int y = 0; y < r.height; y++) {
    for (int x = 0; x < r.width; x++) {
      if (s.contains(x, y)) {
        pix[x + y * width] = 0x0;
      }
    }
  }
  AffineTransform at = AffineTransform.getScaleInstance(-1d, 1d);
  at.translate(-width, 0);
  s = at.createTransformedShape(area); // NE
  for (int y = 0; y < r.height; y++) {
    for (int x = width - r.width; x < width; x++) {
      if (s.contains(x, y)) {
        pix[x + y * width] = 0x0;
      }
    }
  }

  // at = AffineTransform.getScaleInstance(1.0, -1.0);
  // at.translate(0, -height);
  // s = at.createTransformedShape(area); // SE
  // for (int x = 0; x < r.width; x++) {
  //   for (int y = height - r.height; y < height; y++) {
  //     if (s.contains(x, y)) {
  //       pix[y * width + x] = 0x0;
  //     }
  //   }
  // }

  // // NW
  // for (int y = 0; y < 5; y++) {
  //   for (int x = 0; x < 5; x++) {
  //     if (y == 0 && x < 5 || y == 1 && x < 3 ||
  //       y == 2 && x < 2 || y == 3 && x < 1 ||
  //       y == 4 && x < 1) {
  //       pix[y * width + x] = 0x0;
  //     }
  //   }
  // }
  // // NE
  // for (int y = 0; y < 5; y++) {
  //   for (int x = width - 5; x < width; x++) {
  //     if (y == 0 && x >= width - 5 || y == 1 && x >= width - 3 ||
  //       y == 2 && x >= width - 2 || y == 3 && x >= width - 1 ||
  //       y == 4 && x >= width - 1) {
  //       pix[y * width + x] = 0x0;
  //     }
  //   }
  // }
  // int n=0;
  // for (int y = 0; y < 5; y++) {
  //   for (int x = 0; x < width; x++) {
  //     n = y * width + x;
  //     if (x >= 5 && x < width - 5) { continue; }
  //     else if (y == 0 && (x < 5 || x >= width - 5)) { pix[n] = 0x0; }
  //     else if (y == 1 && (x < 3 || x >= width - 3)) { pix[n] = 0x0; }
  //     else if (y == 2 && (x < 2 || x >= width - 2)) { pix[n] = 0x0; }
  //     else if (y == 3 && (x < 1 || x >= width - 1)) { pix[n] = 0x0; }
  //     else if (y == 4 && (x < 1 || x >= width - 1)) { pix[n] = 0x0; }
  //   }
  // }

  return Optional.of(new MemoryImageSource(width, height, pix, 0, width));
}