topical media & game development

talk show tell print

graphic-processing-algorithm-Ch05-p117-p117.pde / pde



  PImage myImage; //define an image object
  myImage = loadImage("memorial.jpg"); //load it
  size(myImage.width,myImage.height); //size it to fit the window
  image(myImage, 0,0); //display the image
  loadPixels(); //get access to the array of pixels[]
  for(int index=0;index<width*height;index++)
  {
    int myPixel = pixels[index]; //get a pixel value
    int r = myPixel >> 16 & 0xFF; // get red(myPixel)
    int g = myPixel >> 8 & 0xFF; // get green(myPixel)
    int b = myPixel & 0xFF; // get blue(myPixel)
    int av= (g + b) /2; // average only green and blue
    if(av>128){      //e.g. if av is 00000000 00000000 00000000 11111111
      r = av << 16;       // Binary: 00000000 11111111 00000000 00000000
      g = av << 8;        // Binary: 00000000 00000000 11111111 00000000
      b = av;             // Binary: 00000000 00000000 00000000 11111111
      pixels[index] = r | g | b; //compose a color using bitwise OR
    }
  }
  updatePixels(); // see the result
  save("memorial_altered.jpg"); // save the image as a file


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.