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

processing bitmaps (B&W)
2-D arrays of ints

This is a two-session lab; We will continue working on picture-processing methods next lab as well.

Pictures!

Download the provided Pict.java, and add it to your project. It has a helpful static method:

Initializing 2-D arrays

Task, together:

  1. create a variable to hold a two-dimentional array of ints.
  2. Allocate space for a 400x500 array, and assign it to that variable.
  3. Write a method fill_pattern then initializes location i,j is initialized to i % 256. Call it on your array above.
  4. What do you think this will look like, as a picture? Display it with Pict.displayPixels, and find out.

Task: Now, tweak the above method to try different formulas. (For each one, you're encouraged to predict the result first.)

Methods on 2-D arrays

methods that return a value

In lecture, we already wrote a method to find the average value of a 2-D array:

    /** @return the sum of the numbers in row `r` of `nums`.
    static int addOneRow( int[][] nums, int r ) {
        int sumSoFar = 0;
        for (int c=0;  c < (nums[r]).length;  ++c) {
            sumSoFar += (nums[r])[c];
        }
        return sumSoFar;
    }
    
    
    /** @return the avg, cast to an int, of all the numbers in `nums`.
     *  @pre nums.length != 0 && ∃ i: nums[i].length != 0
     */
    static int avg( int[][] nums ) {
        int sumSoFar = 0;
        for (int r=0;  r < nums.length;  ++r) {
            sumSoFar += addOneRow(nums,r);
        }
        return sumSoFar / (nums.length * nums[0].length);
    }

Your Task Write a method which takes any 2-D array of ints, and finds the maximum value.
You can either use a helper method (findMaxInRow( int[][] nums, int rowNum)), or you can use a nested loop.

methods that modify array-contents

Your Task Write a method moveEachToward(int[][] nums, int target) which takes any 2-D array of ints, and replaces each number n with1 (n+target)/2. That is, moveEachToward(0) ends up halving each item in the array, and moveEachToward(256) makes each item twice as close to 256.
You can either use a helper method (something like moveOneRowToward( int[][] nums, int target, int rowNum)), or you can use a nested loop.

Effects

Choose one effect from the following list, and implement it! To figure out what you should do, work a small test case by hand (say, a 3x4 array like

  int[][] data2 = { {  90,  90,  90,  90 }
                  , {  20,  90,  20,  90 }
                  , { 200, 200,  99, 200 }
                  }

When you've finished one of the functions above, you can use Pict.fileToPixelsBW to convert an image to an array-of-pixels, display that image, then display the image after applying your effect!

There are also plenty of other photo effects you could do, instead of the above.


1 Hey, for once integer division is what we want, since we're storing the result back into an int. Note that any number in [0,256), when average with 256, is still in [0,256). That's because (255+256)/2 is 255, by integer division. This will be important below.      

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.