![]() |
![]() |
|
This is a two-session lab; We will continue working on picture-processing methods next lab as well.
Download the provided Pict.java, and add it to your project. It has a helpful static method:
Task allocate-and-initialize the array below, and then call displayPixels on it).
// int pixels as a literal array, then displayed: int[][] data1; data1 = { {30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, {30, 30, 90, 200, 200, 200, 200, 200, 90, 30}, {30, 30, 200, 0, 0, 0, 0, 0, 200, 30}, {30, 30, 200, 0, 0, 0, 0, 0, 200, 30}, {30, 30, 200, 0, 255, 0, 255, 0, 200, 30}, {30, 30, 200, 0, 255, 0, 255, 0, 200, 30}, {30, 30, 200, 0, 0, 0, 0, 0, 200, 30}, {30, 30, 200, 0, 0, 0, 0, 0, 200, 30}, {30, 30, 200, 90, 0, 0, 0, 90, 200, 30}, {30, 30, 200, 90, 90, 0, 90, 90, 200, 30}, {30, 30, 200, 0, 90, 90, 90, 0, 200, 30}, {30, 30, 200, 0, 0, 0, 0, 0, 200, 30}, {30, 30, 90, 200, 200, 200, 200, 200, 90, 30}, {30, 30, 30, 30, 30, 30, 30, 30, 30, 30} }; Pict.displayPixels( data1 ); |
Task, together:
Task: Now, tweak the above method to try different formulas. (For each one, you're encouraged to predict the result first.)
(i*j)*256/(width*height) |
if ((i*j)%59 == 0) set location i,j to 0 else set location i,j to 255. |
if ((i*j)%60 == 0) set location i,j to 0 else set location i,j to 255. |
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
You can either use a helper method
(
Your Task
Write a method twice as close
to 256.
You can either use a helper method
(something like
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 } } |
Flip the picture left-to-right.
To do this, you need to think:
where should pixel 0,0 end up?
What about pixel 2,0? or 2,3?
See if you can figure out the general formula for pixel r,c.
(Your formula should involve the array's width and height, in general.)
When you've finished one of the functions above,
you can use
There are also plenty of other photo effects you could do, instead of the above.
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |