Using BufferedImage in Java

Objects of the BufferedImage class represent images that we can draw to.


Putting it all together

Here is a code fragment that takes an existing image myImage, and creates a new image redderImage containing the same picture but with maximal red and maximal opaqueness.

  BufferedImage redderImage = new BufferedImage(myImage.getWidth(),
                                                myImage.getHeight(),
                                                BufferedImage.TYPE_INT_ARGB);
              
  for (int ix = 0; ix < myImage.getWidth(); ++ix) {
    for (int iy = 0; iy < myImage.getHeight(); ++iy) {
 
      int rgb = myImage.getRGB(ix,iy);
    
      int new_alpha = 255;                        // Maximum!
      int new_red = 255;                          // Maximum!
      int new_green = (rgb & 0x0000FF00) >>> 8;   // Copy original greenness
      int new_blue  = (rgb & 0x000000FF) >>> 0;   // Copy original blueness

      int new_rgb = (new_alpha << 24) | (new_red << 16) | (new_green << 8) | new_blue;
      redderImage.setRGB(ix, iy, new_rgb);
    }
  }