RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lab14b
2-D arrays
processing pictures II

We will continue working on lab14a.

Review

Review, on processing a 2-D array: a helper method, vs. a nested loop:


representing Colors

A java.awt.Color has three components: red (from 0 to 255), green, and blue (customarily in that order). So new Color(0, 255, 0) represents bright green: it has the maximum possible green-ness, but zero red-ness and zero blue-ness. White light is the mixture of all of these three primary colors: new Color(255, 255, 255). What represents black? What colors can we mix, to get purple? Gray?

We can use Pict.getPixelsColor to take in a filename or URL, and be given a 2-D array of java.awt.Colors, to work with. Likewise, Pict.displayPixels will take such a 2-D array and display it on the screen as an image. And as before, we might modify that 2-D array ourselves.

  /** Change one pixel of an image, keeping all the red 
   *  (but throwing away the blue and green components).
   * @param img The image (2-D array of Colors) to modify.
   * @param r the row-number of the pixel to modify.
   *   pre-condition: 0 <= r < img.length
   * @param c the row-number of the pixel to modify.
   *   pre-condition: 0 <= c < img[r].length
   */ 
  void filterRed( Color[][] img, int r, int c ) {
    Color origPxl = img[r][c];
    int origRed   = origPxl.getRed();

    Color udpatedPxl = new Color( origRed, 0, 0 );
    img[r][c] = updatedPxl;
  }
(Of course, the body of this method could easily be combined into one line: img[r][c] = new Color( img[r][c].getRed(), 0, 0 );; it is broken into multiple lines just to the individual steps easier to see. Your task: write a method that filters the purple of an entire image, not just one pixel. (You can process the entire array in the same way we initialized a 2-D array in class: We already have a method to modify an designated pixel; make a method which modifies one designated row (calling the modify-one-pixel method as a helper) then make a method which modifies every row (calling the modify-one-row method as a helper).


Color effects

  1. Make method filterPurple which keeps the red and blue components of an entire image, but sets the green component of each pixel to zero.
  2. Task: Make method invertColor, similar to invert, which complements each component: a color of red=0,green=10,blue=254 would be replaced with a color of red=255,green=245,blue=1.
    Submit this on WebCT (by Friday 17:00). It will count as 5pts of homework extra-credit. (Your code can also include other functions, of course.) You need only submit your code that uses Pict; you don't need to submit Pict.java itself since I already have that file (and you didn't change it!).
  3. Make method toBlackAndWhite, which replaces each pixel with the average of the three components. That is, a color of red=0,green=10,blue=80 would be replaced with a color of red=30,green=30,blue=30.
  4. We can also combine two pictures in various ways:
    Superimpose one small picture on top of another.
    Or, take the average of two pictures, to create a third.
    Best: for any double p in 0..1, blend two pictures by adding p times each pixel from the first picture, plus (1-p) times each pixel for the second picture. (If you were make a movie where p varied from 0 to 1, you'd have a continuous fade from one picture to another.)
  5. Greenscreen -- Take a picture of yoruself against a solid green background1 Now, copy all non-green pixels in that picture onto a picture of (say) the moon. Voila, we now have a pciture of you on the moon!
  6. Take any two other effects you've written, and make a method which applies both of them. (Does the order matter? That is, if you filterRed and then convert to black and white, is that the same as converting to black and white and then filtering out the red?)
  7. Write a method filterRight which is like FilterRed except that it only filters the right half (or, the bottom half) of the image.

More Ideas

If you want more ideas of photoshop effects, there are plenty more ideas. Try any of these, if they grab your fancy:

Further notions

For each of the above effects, it's interesting to wonder:

The answers of course vary for the different choices of effects.


1 The only reason green screens are used is that bright greens rarely occur otherwise (say in your face or clothing), so it makes it easy to tell the subject from the background.      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Dec.09 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme