Other displays Saving Arrays Using fors to process Picture Processing Other displays Saving Arrays Using fors to process Just using counting for Copyright © 2009 – 2016 Curt Hill
Up to now we can: Use FileChooser to find a picture Load a picture into memory Display it with the show method Drop it into a turtle world Have the turtle draw over it Consider the next screen for a sample program that does the first three of these Copyright © 2009 – 2016 Curt Hill
Load and Display public class PictureDemo2{ public static void main(String [] a){ String fileName; FileChooser.setMediaPath( “.../mediasources"); fileName = FileChooser.pickAFile(); Picture p = new Picture(fileName); p.show(); } Copyright © 2009 – 2016 Curt Hill
Picture Explorer One of the things that we want to do is look at or change individual pixels The picture show method does not help us with this There is a Picture explorer which does Instead of: p.show(); use: p.explore(); Copyright © 2009 – 2016 Curt Hill
What it does Picture explorer (or just explorer) displays the picture It also allows you to click any point on the picture When you do it shows: The x and y locations The red, green and blue values at that location Consider the beach picture next Look for the cross hairs Copyright © 2009 – 2016 Curt Hill
Picture Explorer Copyright © 2009 – 2016 Curt Hill
Commentary The cross hair was at location x = 230, y = 289 The color was: red = 145 green = 160 blue = 165 This is a handy tool for determining what kind of colors exist in an area or in a picture Copyright © 2009 – 2016 Curt Hill
Saving a picture So far we have not changed anything on a picture so there is nothing to save However, we are going to look at the saving next and then modification The method needed is write It requires a filename as a parameter If you use the old filename you will replace the file Generally, not a good idea Copyright © 2009 – 2016 Curt Hill
Saving You may use FileChooser to select the new filename You may select an existing file or type in a new one Consider the next two screens The new program The execution Copyright © 2009 – 2016 Curt Hill
Load, Display, Save public class PictureDemo3{ public static void main(String [] a){ String fileName; FileChooser.setMediaPath(“.../mediasources"); fileName = FileChooser.pickAFile(); Picture p = new Picture(fileName); p.explore(); process(p); p.write(fileName); } Copyright © 2009 – 2016 Curt Hill
Choosing Save File Name Copyright © 2009 – 2016 Curt Hill
Commentary Did you notice the process method? This is where we want to do something to the picture Inside this method we will use a for to do something to each pixel Most of our picture processing programs will have this form Main method reads/displays picture Process method modifies it Before we do that we need to consider a few things Copyright © 2009 – 2016 Curt Hill
What We need to be able to access a one dimensional array of pixels Later we will consider arrays in more depth We need to know how many pixels in the above array We need to know what can be done to each pixel First a digression through arrays Copyright © 2009 – 2016 Curt Hill
What is an array? Multiple occurrences of values of one type One name many values Item is selected at run time by an integer index A true data type in Java, unlike C++ Always an object Always dynamic The mark of arrays is a pair of square brackets: [ ] Copyright © 2009 – 2016 Curt Hill
Why do we need arrays? We often need to select a value by an index rather than just name A picture will typically have thousands or millions of pixels Awkward to think of having thousands or millions of names, one for each pixel Instead we find one name and use an integer subscript to choose which of the pixels we want Copyright © 2009 – 2016 Curt Hill
Obtaining the pixel array Pictures have a method called getPixels It returns an array of pixels We will process that array with a counting for today and a for all later Suppose that p is our picture, then use: Pixel [] pixels= p.getPixels(); Let us consider this further Copyright © 2009 – 2016 Curt Hill
Obtaining Pixels The type that is returned: Pixel [] is actually an array of pixels The brackets [ ] is the sure sign of an array in Java The size will be determined by what it receives We can obtain either a one dimensional or two dimensional access to the pixels For now we prefer the simpler one dimensional Copyright © 2009 – 2016 Curt Hill
Size How many pixels are there? We do not care before hand, but must know at run-time in order to process each one This is done using the length property Any array may be asked its size int i = pixels.length; Since it is a property it needs no parentheses Copyright © 2009 – 2016 Curt Hill
Basic Process Form static void process(Picture p){ Pixel [] px = p.getPixels(); int len = px.length; for(int i = 0;i<len;i++){ Pixel item = px[i]; //... } // end of for } // end of process Copyright © 2009 – 2016 Curt Hill
Commentary The getPixels obtains a handle to the array Inside the for we obtain one element of that array and make it a Pixel What can we do to a pixel? Copyright © 2009 – 2016 Curt Hill
A Pixel object The type is Pixel A pixel contains information about its location in the picture as well as the color Recall that the three colors in a picture are red, green and blue This leads to six methods: getRed and setRed getGreen and setGreen getBlue and setBlue Copyright © 2009 – 2016 Curt Hill
Getters and Setters Typical in Java for an object to have a get and set method The get usually returns a value as its result: int r = px.getRed(); The set takes a parameter which is the new value: px.setRed(r); Copyright © 2009 – 2016 Curt Hill
Example: Remove Red If we take the process method and have it remove all the red, what will happen? How will we do this? In the for setRed to zero The next two screens shows the code and results Copyright © 2009 – 2016 Curt Hill
The main program public static void main(String [] a){ String fileName; FileChooser.setMediaPath(“…/mediasources"); fileName = FileChooser.pickAFile(); Picture p = new Picture(fileName); p.explore(); process(p); //fileName = FileChooser.pickAFile(); //p.write(fileName); } Copyright © 2009 – 2016 Curt Hill
The process method static void process(Picture p){ Pixel [] px = p.getPixels(); int len = px.length; for(int i = 0;i<len;i++){ Pixel item = px[i]; item.setRed(0); } Copyright © 2009 – 2016 Curt Hill
Red Removed Copyright © 2009 – 2016 Curt Hill
Filters A photographic filter removes some of the light from a the camera The removal of red was like that We can also make the image a single color, by setting two of the colors to zero Copyright © 2009 – 2016 Curt Hill
Red Only Copyright © 2009 – 2016 Curt Hill
Darkening So far we have just set the colors to predefined values such as zero Most of the time we want to modify the colors Based upon existing values In this next one we make each pixel three quarters as bright Copyright © 2009 – 2016 Curt Hill
The process method static void process(Picture p){ // Process every pixel Pixel [] pixels = p.getPixels(); int len = pixels.length; for(int i = 0;i<len;i++){ Pixel px = pixels[i]; // Do something with px int j = px.getRed()*3/4; px.setRed(j); j = px.getGreen()*3/4; px.setGreen(j); j = px.getBlue()*3/4; px.setBlue(j); } Copyright © 2009 – 2016 Curt Hill
Commentary In the dimming process we obtain each pixel’s red value and reduce it by ¾ Set it back into the red Repeat for green and blue Copyright © 2009 – 2016 Curt Hill
Three Quarters Bright Copyright © 2009 – 2016 Curt Hill
Commentary In the dimming process we obtain each pixel’s red value and reduce it by ¾ Set it back into the red Repeat for green and blue Copyright © 2009 – 2016 Curt Hill
Pixels again Recall that the values for each color are strictly in the range 0 – 255 Thus we should make sure that whatever modifications we do leaves them in this range Thus brightening should not increase a color past 255 Copyright © 2009 – 2016 Curt Hill
Finally We are now able to read in a picture, modify it and display the result This allows us to do most any kind of processing that does not care where the pixel came from We will later do processing that is positionally sensitive Copyright © 2009 – 2016 Curt Hill