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

The following examples show how to use java.awt.image.PixelGrabber#status() . 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: ImageUtils.java    From openbd-core with 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 2
Source File: PerformanceTest.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
public static int[] pixelGrabber(Image image) {
		int W = 300;//image.getWidth();
		int H = 150;//image.getHeight();
		
		int[] mapPixels = new int[W * H];
		PixelGrabber grabber = new PixelGrabber(image, 0, 0, W, H, mapPixels, 0, W);
		try {
			grabber.grabPixels();
		} catch (InterruptedException e) {
			System.out.println("grabber error");
		}
		if ((grabber.status() & ImageObserver.ABORT) != 0)
			System.out.println("grabber error");

//		for (int x = 0; x < H; x++) {
//			for (int y = 0; y < W; y++) {
//				int pixel = mapPixels[(x * W) + y];
//				Color color = new Color(pixel);
//				if (Color.white.equals(color)) {
//					double pixel_offset = (Math.PI / 150D) / 2D;
//					double phi = (((double) x / 150D) * Math.PI) + pixel_offset;
//					double theta = (((double) y / 150D) * Math.PI) + Math.PI + pixel_offset;
//					if (theta > (2D * Math.PI))
//						theta -= (2D * Math.PI);
//				}
//			}
//		}
		
		return mapPixels;
	}
 
Example 3
Source File: RandomMineralMap.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
	 * Gets a set of location coordinates representing a topographical region.
	 * 
	 * @param imageMapName the topographical region map image.
	 * @return set of location coordinates.
	 */
	private Set<Coordinates> getTopoRegionSet(String imageMapName) {
		Set<Coordinates> result = new HashSet<Coordinates>(3000);
//		[landrus, 26.11.09]: don't use the system classloader in a webstart env.
		URL imageMapURL = getClass().getResource("/images/" + imageMapName);
		ImageIcon mapIcon = new ImageIcon(imageMapURL);
		Image mapImage = mapIcon.getImage();

		int[] mapPixels = new int[W * H];
		PixelGrabber topoGrabber = new PixelGrabber(mapImage, 0, 0, W, H, mapPixels, 0, W);
		try {
			topoGrabber.grabPixels();
		} catch (InterruptedException e) {
			logger.log(Level.SEVERE, "grabber error" + e);
		}
		if ((topoGrabber.status() & ImageObserver.ABORT) != 0)
			logger.info("grabber error");

		for (int x = 0; x < H; x++) {
			for (int y = 0; y < W; y++) {
				int pixel = mapPixels[(x * W) + y];
				Color color = new Color(pixel);
				if (Color.white.equals(color)) {
					double pixel_offset = (Math.PI / 150D) / 2D;
					double phi = (((double) x / 150D) * Math.PI) + pixel_offset;
					double theta = (((double) y / 150D) * Math.PI) + Math.PI + pixel_offset;
					if (theta > (2D * Math.PI))
						theta -= (2D * Math.PI);
					result.add(new Coordinates(phi, theta));
				}
			}
		}

		return result;
	}
 
Example 4
Source File: AreothermalMap.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Load areothermal hot spots from volcanic map image.
 */
private void loadHotspots() {
	hotspots = new HashSet<Coordinates>(700);
	URL imageMapURL = getClass().getResource("/images/" + VOLCANIC_IMG);
	ImageIcon mapIcon = new ImageIcon(imageMapURL);
	Image mapImage = mapIcon.getImage();

	int[] mapPixels = new int[W * H];
	PixelGrabber grabber = new PixelGrabber(mapImage, 0, 0, W, H, mapPixels, 0, W);
	try {
		grabber.grabPixels();
	} catch (InterruptedException e) {
		logger.log(Level.SEVERE, "grabber error" + e);
	}
	if ((grabber.status() & ImageObserver.ABORT) != 0)
		logger.severe("grabber error");

	for (int x = 0; x < H; x++) {
		for (int y = 0; y < W; y++) {
			int pixel = mapPixels[(x * W) + y];
			Color color = new Color(pixel);
			if (Color.white.equals(color)) {
				double pixel_offset = (Math.PI / 150D) / 2D;
				double phi = (((double) x / 150D) * Math.PI) + pixel_offset;
				double theta = (((double) y / 150D) * Math.PI) + Math.PI + pixel_offset;
				if (theta > (2D * Math.PI))
					theta -= (2D * Math.PI);
				hotspots.add(new Coordinates(phi, theta));
			}
		}
	}
}
 
Example 5
Source File: GIFEncoder.java    From jplag with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Convenience constructor for class <CODE>GIFEncoder</CODE>. The argument
 * will be converted to an indexed color array. <B>This may take some
 * time.</B>
 * 
 * @param image
 *            The image to encode. The image <B>must</B> be completely
 *            loaded.
 * @exception AWTException
 *                Will be thrown if the pixel grab fails. This can happen if
 *                Java runs out of memory. It may also indicate that the
 *                image contains more than 256 colors.
 */
public GIFEncoder(Image image) throws AWTException {
	this.imageWidth = (short) image.getWidth(null);
	this.imageHeight = (short) image.getHeight(null);

	int values[] = new int[this.imageWidth * this.imageHeight];
	PixelGrabber grabber = new PixelGrabber(image, 0, 0, this.imageWidth, this.imageHeight, values, 0, this.imageWidth);

	try {
		if (grabber.grabPixels() != true)
			throw new AWTException("Grabber returned false: " + grabber.status());
	} // ends try

	catch (InterruptedException ie) {
	}

	byte[][] r = new byte[this.imageWidth][this.imageHeight];
	byte[][] g = new byte[this.imageWidth][this.imageHeight];
	byte[][] b = new byte[this.imageWidth][this.imageHeight];
	int index = 0;

	for (int y = 0; y < this.imageHeight; y++) {
		for (int x = 0; x < this.imageWidth; x++, index++) {
			r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
			g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
			b[x][y] = (byte) ((values[index]) & 0xFF);
		} // ends for

	} // ends for

	this.toIndexColor(r, g, b);
}
 
Example 6
Source File: ImageFunction2D.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public ImageFunction2D(Image image, int edgeAction, boolean alpha) {
PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, null, 0, -1);
try {
	pg.grabPixels();
} catch (InterruptedException e) {
	throw new RuntimeException("interrupted waiting for pixels!");
}
if ((pg.status() & ImageObserver.ABORT) != 0) {
	throw new RuntimeException("image fetch aborted");
}
init((int[])pg.getPixels(), pg.getWidth(), pg.getHeight(), edgeAction, alpha);
}
 
Example 7
Source File: BmpWriter.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * The constructor.
 * 
 * @param img
 */
public BmpWriter( Image img )
{
	if ( img == null )
	{
		return;
	}

	PixelGrabber pg = new PixelGrabber( img, 0, 0, -1, -1, true );

	try
	{
		pg.grabPixels( );
	}
	catch ( InterruptedException e )
	{
		return;
	}

	if ( ( pg.status( ) & ImageObserver.ABORT ) != 0 )
	{
		return;
	}

	this.pix = (int[]) pg.getPixels( );
	this.width = pg.getWidth( );
	this.height = pg.getHeight( );
}
 
Example 8
Source File: WMFGraphics.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int setGDIFillBrush()
{
    int i = brushhandle;

    if (brushfillstyle == 3) {
        if (brushpattern != null) {
            int j = brushpattern.getWidth(null);
            int k = brushpattern.getHeight(null);
            int[] ai = new int[j * k];
            PixelGrabber pixelgrabber = new PixelGrabber(brushpattern, 0, 0, j, k, ai, 0, j);

            try {
                pixelgrabber.grabPixels();

                if ((pixelgrabber.status() & 0x80) != 0) {
                    brushhandle = wmf.createBrushIndirect(0, foreground, brushhatch);
                } else {
                    brushhandle = wmf.createPatternBrush(ai, j, k);
                }
            } catch (InterruptedException _ex) {
                brushhandle = wmf.createBrushIndirect(0, foreground, brushhatch);
            }
        } else {
            brushhandle = wmf.createBrushIndirect(0, foreground, brushhatch);
        }
    } else {
        brushhandle = wmf.createBrushIndirect(brushfillstyle, foreground, brushhatch);
    }

    wmf.selectObject(brushhandle);
    wmf.deleteObject(i);
    state.setBrush(brushhandle);

    return brushhandle;
}
 
Example 9
Source File: GIFEncoder.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Construct a GIFEncoder. The constructor will convert the image to
 * an indexed color array. <B>This may take some time.</B><P>
 *
 * @param image The image to encode. The image <B>must</B> be
 * completely loaded.
 * @exception AWTException Will be thrown if the pixel grab fails. This
 * can happen if Java runs out of memory. It may also indicate that the image
 * contains more than 256 colors.
 * */
public GIFEncoder(Image image) throws AWTException {
  width_ = (short) image.getWidth(null);
  height_ = (short) image.getHeight(null);
  int values[] = new int[width_*height_];
  PixelGrabber grabber = new PixelGrabber(image, 0, 0, width_, height_, values, 0, width_);
  try {
    if(grabber.grabPixels()!=true) {
      throw new AWTException("Grabber returned false: "+grabber.status()); //$NON-NLS-1$
    }
  } catch(InterruptedException ex) {
    ex.printStackTrace();
  }
  byte r[][] = new byte[width_][height_];
  byte g[][] = new byte[width_][height_];
  byte b[][] = new byte[width_][height_];
  int index = 0;
  for(int y = 0; y<height_; ++y) {
    for(int x = 0; x<width_; ++x) {
      r[x][y] = (byte) ((values[index]>>16)&0xFF);
      g[x][y] = (byte) ((values[index]>>8)&0xFF);
      b[x][y] = (byte) ((values[index])&0xFF);
      ++index;
    }
  }
  ToIndexedColor(r, g, b);
}
 
Example 10
Source File: MarsMap.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
/** Sets up Points and Colors for Sphere */
private void setupSphere() {

	// Initialize variables
	int row, col_num, map_col;
	double phi, theta;
	double circum, offset;
	double ih_d = (double) MAP_H;

	// Initialize color arrays
	int[] pixels_color = new int[MAP_H * MAP_W];
	int[][] map_pixels = new int[MAP_W][MAP_H];

	// Grab mars_surface image into pixels_color array using PixelGrabber
	PixelGrabber pg_color = new PixelGrabber(marsMap, 0, 0, MAP_W, MAP_H, pixels_color, 0, MAP_W);
	try {
		pg_color.grabPixels();
	} catch (InterruptedException e) {
		logger.log(Level.SEVERE, Msg.getString("MarsMap.log.grabberError") + e); //$NON-NLS-1$
	}
	if ((pg_color.status() & ImageObserver.ABORT) != 0)
		logger.info(Msg.getString("MarsMap.log.grabberError")); //$NON-NLS-1$

	// Transfer contents of 1-dimensional pixels_color into 2-dimensional map_pixels
	for (int x = 0; x < MAP_W; x++)
		for (int y = 0; y < MAP_H; y++)
			map_pixels[x][y] = pixels_color[x + (y * MAP_W)];

	// Initialize variables
	// rho = map_height / Math.PI;
	offset = Math.PI / (2 * ih_d);

	// Go through each row and create Sphere_Color vector with it
	for (phi = offset; phi < Math.PI; phi += (Math.PI / ih_d)) {
		row = MoreMath.floor((float) ((phi / Math.PI) * ih_d));//(int) Math.floor((phi / Math.PI) * ih_d);
		circum = 2 * Math.PI * (rho * MoreMath.sin(phi));
		col_num = (int) Math.round(circum);
		sphereColor[row] = new Vector<Integer>(col_num);

		// Fill vector with colors
		for (theta = 0; theta < (2 * Math.PI); theta += ((Math.PI * 2) / circum)) {
			if (theta == 0) {
				map_col = 0;
			} else {
				map_col = MoreMath.floor((float)((theta / Math.PI) * ih_d));
			}

			sphereColor[row].addElement(map_pixels[map_col][row]);
		}
	}
}
 
Example 11
Source File: WMFGraphics.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean drawImage(
    Image image, int leftD, int topD, int rightD, int bottomD, int leftS, int topS, int rightS, int bottomS, Color color,
    ImageObserver imageobserver)
{
    restore();

    int imagewidth = image.getWidth(imageobserver);
    int imageheight = image.getHeight(imageobserver);
    int[] pixbuffer = new int[imagewidth * imageheight];
    PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, imagewidth, imageheight, pixbuffer, 0, imagewidth);

    try {
        pixelgrabber.grabPixels();
    } catch (InterruptedException _ex) {
        return false;
    }

    if ((pixelgrabber.status() & 0x80) != 0) {
        return false;
    }

    int diffx = rightD - leftD;
    int diffy = bottomD - topD;
    int sourcediffx = rightS - leftS;
    int sourcediffy = bottomS - topS;
    int k3 = bottomS;
    bottomS = imageheight - topS;
    topS = imageheight - k3;

    if ((diffx < 0) != (sourcediffx < 0)) {
        flipHorizontal(pixbuffer, imagewidth, imageheight);

        if (sourcediffx < 0) {
            leftS = imagewidth - leftS;
        } else {
            leftS = imagewidth - rightS;
        }
    }

    if (diffx < 0) {
        leftD = rightD;
        if (sourcediffx < 0) {
            leftS = rightS;
        }
        diffx = -diffx;
    }

    if (sourcediffx < 0) {
        sourcediffx = -sourcediffx;
    }

    if ((diffy < 0) != (sourcediffy < 0)) {
        flipVertical(pixbuffer, imagewidth, imageheight);

        if (sourcediffy < 0) {
            topS = imageheight - topS;
        } else {
            topS = imageheight - bottomS;
        }
    }

    if (diffy < 0) {
        topD = bottomD;

        if (sourcediffy < 0) {
            topS = bottomS;
        }

        diffy = -diffy;
    }

    if (sourcediffy < 0) {
        sourcediffy = -sourcediffy;
    }

    int l3 = color.getRGB();

    for (int i4 = 0; i4 < pixbuffer.length; i4++)
        if ((pixbuffer[i4] & 0xff000000) == 0) {
            pixbuffer[i4] = l3;
        }

    wmf.stretchBlt(leftD, topD, diffx, diffy, leftS, topS,
        sourcediffx, sourcediffy, SRC_COPY, pixbuffer, imagewidth, imageheight);

    return true;
}
 
Example 12
Source File: WMFGraphics.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean drawImage(Image image,
                         int leftD, int topD, int rightD, int bottomD, int leftS, int topS, int rightS, int bottomS,
                         ImageObserver imageobserver)
{
    restore();

    int imagewidth = image.getWidth(imageobserver);
    int imageheight = image.getHeight(imageobserver);
    int[] pixarray = new int[imagewidth * imageheight];
    PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, imagewidth, imageheight, pixarray, 0, imagewidth);

    try {
        pixelgrabber.grabPixels();
    } catch (InterruptedException _ex) {
        return false;
    }

    if ((pixelgrabber.status() & 0x80) != 0) {
        return false;
    }

    int ddiffx = rightD - leftD;
    int ddiffy = bottomD - topD;
    int sdiffx = rightS - leftS;
    int sdiffy = bottomS - topS;
    bottomS = imageheight - topS;
    topS = imageheight - bottomS;

    if ((ddiffx < 0) != (sdiffx < 0)) {
        flipHorizontal(pixarray, imagewidth, imageheight);

        if (sdiffx < 0) {
            leftS = imagewidth - leftS;
        } else {
            leftS = imagewidth - rightS;
        }
    }

    if (ddiffx < 0) {
        leftD = rightD;

        if (sdiffx < 0) {
            leftS = rightS;
        }

        ddiffx = -ddiffx;
    }

    if (sdiffx < 0) {
        sdiffx = -sdiffx;
    }

    if ((ddiffy < 0) != (sdiffy < 0)) {
        flipVertical(pixarray, imagewidth, imageheight);

        if (sdiffy < 0) {
            topS = imageheight - topS;
        } else {
            topS = imageheight - bottomS;
        }
    }

    if (ddiffy < 0) {
        topD = bottomD;

        if (sdiffy < 0) {
            topS = bottomS;
        }

        ddiffy = -ddiffy;
    }

    if (sdiffy < 0) {
        sdiffy = -sdiffy;
    }

    int[] ai1 = new int[pixarray.length];
    boolean flag = false;

    for (int l3 = 0; l3 < pixarray.length; l3++)
        if ((pixarray[l3] & 0xff000000) == 0) {
            ai1[l3] = -1;
            pixarray[l3] = 0;
            flag = true;
        } else {
            ai1[l3] = 0;
        }

    if (flag) {
        wmf.stretchBlt(leftD, topD, ddiffx, ddiffy, leftS, topS, sdiffx, sdiffy, SRC_AND, ai1, imagewidth, imageheight);
        wmf.stretchBlt(leftD, topD, ddiffx, ddiffy, leftS, topS, sdiffx, sdiffy, SRC_PAINT, pixarray, imagewidth, imageheight);
    } else {
        wmf.stretchBlt(leftD, topD, ddiffx, ddiffy, leftS, topS, sdiffx, sdiffy, SRC_COPY, pixarray, imagewidth, imageheight);
    }

    return true;
}