RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

processing bitmaps (color)
2-D arrays of objects

We will continue processing images as 2-d arrays of pixels, as in the previous lab (pixel as int — B&W), and the preceding lecture (pixel as a Color object).

Review

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


Pict.fileToPixelsBW takes the name of a jpeg or gif file (either a filename, or a URL), and returns a 2-D array of ints in [0,256).
Here are some pictures you might want to convert to an array-of-ints, and then modify:

See further documentation on the provided class Pict. You can also look at and run the main method of that class. You can put your methods in a separate class; just be sure to call the provided (static?!) methods by using their class-name: Pict.methodName.


representing Colors

Review: These are the issues we talked about this morning in lecture

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.fileToPixelsColor 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. Ignore: We did this code in lecture
    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.
  2. Make method filterPurple which keeps the red and blue components of an entire image, but sets the green component of each pixel to zero.
    You should have a helper which just filters the purple in one single row, and another which just filters an individual pixel.

    Optional: The resulting image is less bright than the original (perhaps by 1/3 on average?). If you want, try to compensate by scaling up the other two components (but making sure not to exceed 255 in any one component).
  3. Make method filterPurpleEveryOther which only filters every other row of the input image/array.
    This should call the helper from the last problem.

    Be careful that you correctly handle images of odd width — don't try to tint a column that doesn't exist!
    hint: One possible approach: Make a variable i that goes from zero to half the width (quotient); then color column 2*i.
  4. Make method filterPurpleBands which only filters every other group of k rows of the input image/array.
    I recommend a helper function that just filters one band of k rows. What piece(s) of info do you need pass this function?
    hint: One possible approach: Make a variable i that goes from zero to 1/2k of the width (quotient); then start a new band at column i*2*k, coloring the following k rows.
  5. Go back and rewrite filterPurpleEveryOther so that it is just a one line call to filterPurpleBands.
  6. Go back and rewrite filterPurple so that it is just a one line call to filterPurpleBands.
    hint: Remember how we compute the height and/or width of a 2-d array.

Submit what you are able to complete on D2L; I hope that you complete at least #2 and #3.

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.      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.