bufferedImage.setRGB(x,y,rgba) ?

Hi,

I have a instance of BufferedImage. What I’m trying to do with this instance is to modify some pixels in it, by doing

bufferedImage.setRGB(x,y,rgba)

This works, that is, I get some results. But I do not get the right color!

It only seems to “allow” me to set a rgba value that exists in the “colorspace” of that bufferedImage instance. So, if I loaded up a image that is 4 colors, red, green, blue, black, then I cannot change any pixels to anything but red, green, blue, black. If I were to do change some pixels to, let’s say, cyan, then I would get weird results (no error/exception though).

I can’t seem to find a easy way to add a new color to the colorspace. Any ideas?

What is the exact purpose? Do you want to colorize the image or is it intended to modify one particular pixel? There are easier ways to colorize the image, e.g. troughout Paint or Composite. ???

I have a world map. Each country is colored in a unique color (solid). When I click on this world map with the mouse I can look up on which pixel was clicked on by using mouseX,mouseY, and then I can see what the color is for that pixel. When I know the color that was clicked on, I know what country was also clicked on.
Also, I wish to replace all those pixels, that are in that countrys color, with another color…a highlight color. However, that highlight color does not exist in the colorspace, and that’s where the problem is.

If I recreate the worldmap image, that is create a new BufferedImage with TYPE_INT_ARGB and then draw the worldmap image onto it, the speed of Java2D will downgrade to 30 something fps instead of 65ish.

Another question. Instead of recoloring each individual pixel, isn’t it possible to simply replace the color in the colorspace with another color? (gonna experiment with it now)

Ok, I’m using TYPE_INT_RGB instead of TYPE_INT_ARGB, increasing the fps dramatically. Problem solved I believe.

You can also increase the FPS dramatically with the following code (bypassing setRGB)


		ColorModel colorModel = new DirectColorModel(24, 0xff0000,
				 0x00ff00, 0x0000ff);
		
		SampleModel sampleModel = new SinglePixelPackedSampleModel(
				DataBuffer.TYPE_INT, width, height, new int[] { 0xff0000, 0x00ff00,
						0x0000ff });

		DataBuffer dataBuffer = new DataBufferInt(buffer, width * height);

		WritableRaster raster = Raster.createWritableRaster(sampleModel,
				dataBuffer, new Point(0, 0));
		
		image = new BufferedImage (colorModel, raster,true, new Hashtable());

[quote=“appel,post:3,topic:30373”]
This is as simple as to add a alpha-Composite-layer on the pictures in Photoshop. That looks as follows :


public class HighlightByMouse extends MouseAdapter {
      /** this would be the surface area to highlight */
     public Dimension surfaceHighlight = new Dimension(20, 20);
     /***/
     public Color colorHighlight = Color.RED;
     /***/
     public Image theWorldMapBufferedImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);

     public void mouseReleased(MouseEvent e) {
          theWorldMapBufferedImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
     }

     public void mousePressed(MouseEvent e) {
      Graphics2D g2 = (Graphics2D)theWorldMapBufferedImage.getGraphics();
      Composite cps = g2.getComposite();
      g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
      Color c = g2.getColor();
      g2.setColor(colorHighlight);
      Rectangle highlight = new Rectangle(
              (int)((float)e.getX() - (float)surfaceHighlight.width / 2.0f), 
              (int)((float)e.gety() - (float)surfaceHighlight.height / 2.0f),
              surfaceHighlight.width, 
              surfaceHighlight.height
       );
      g2.fillRect(highlight.x, highlight.y, highlight.width, highlight.height );
      g2.setComposite(cps);
     g2.setColor(c);
    }
}

:smiley: of course the WorldMap image should have contents to load. that means you may replace the blank image with one read from your favorite picture.