Workshop for Programming And Systems Management Teachers Chapter 5 Manipulating Pictures, Arrays, and Loops Georgia Institute of Technology Learning Goals • Understand at a conceptual and practical level – How to manipulate digital images using methods? – What is an array? – What is a loop? • While and for loops – What is a package? – How do you import classes? – What is a comment? Georgia Institute of Technology Digital Pictures • Represented as an array of pixels – With a red, green, and blue value stored for each pixel • Stored in .jpg (JPEG) files – International standard – With lossy compression • Lossy means not all data is stored – But what is lost isn’t that important • Compression means made smaller • Other formats for storing digital pictures are GIFF and BMP Georgia Institute of Technology What is an Array? • Storage for a sequence of items – Of the same type • You can access items by using an index • The index starts at 0 0 1 2 3 4 5 3 7 9 2 1 5 0 1 2 3 8 3 2 6 – The first item is at index 0 – The second item is at index 1 – The last item is at index (length – 1) Georgia Institute of Technology How do you Declare an Array? • There are two ways – Type[] name; – Type name[]; • You can give values for the array when you declare it int[] grades = {88, 90, 75, 93, 94}; • You can also create the array when you declare it int[] grades = new int[5]; Georgia Institute of Technology How do you Access Array Items? • In Java to access an array item use – arrayName[index] • To set a value in an array grades[0] = 88; grades[1] = 90; • To get a value from an array int total = grades[0] + grades[1] + grades[2] + grades[3] + grades[4]; • Arrays know their lengths arrayName.length; Georgia Institute of Technology Array Exercise • In DrJava declare and create these two arrays • Check that you created them correctly by getting the value at each index • Check the length of the array with 3 7 9 2 8 3 2 6 arrayName.length Georgia Institute of Technology 1 5 Manipulating a Picture • To manipulate a picture we need to manipulate the pixels that make up the picture – Change the red, green, or blue values at the pixel • Pixel is also a class – Each pixel object has a red, green, and blue value Georgia Institute of Technology What Data does a Picture Object Have? • A picture object has an array of pixel objects – That it read from the JPEG file • It knows it’s width pictureObj.getWidth() • It knows it’s height pictureObj.getHeight() • It knows how to return an array of pixels Pixel[] pixels = pictureObject.getPixels() Georgia Institute of Technology Picture Exercise • Create a picture in DrJava – get the pictures width, height, and pixels String fileName = FileChooser.pickAFile(); Picture picture = new Picture(fileName); int width = picture.getWidth(); System.out.println(“The picture width is “ + width); int height = picture.getHeight(); System.out.println(“The picture height is “ + height); Pixel[] pixels = picture.getPixels(); System.out.println(pixels.length + “ pixels”); Georgia Institute of Technology Pixel Objects • Each pixel has a red, green, and blue value – getRed(), getGreen(), getBlue() – setRed(v), setGreen(v), setBlue(v) • Each pixel knows the location it was in the picture object – getX(), get(Y) • You can also get and set the color at the pixel – getColor(), setColor(color) Georgia Institute of Technology Pixel X and Y Values • A picture has pixels arranged in a grid – The location of the pixel along the horizontal axis is the x value • x starts at 0 to width - 1 – The location of the pixel along the vertical axis is the y value • y starts at 0 to height - 1 Georgia Institute of Technology Color Objects • There is a class defined in Java that represents color – The Color class in the package java.awt – To use the class you must either • import java.awt.Color; • Use the full name java.awt.Color • You can create a color object by giving the red, green, and blue values – Color color = new Color(255,10,125); Georgia Institute of Technology Predefined Colors • The Color class has defined class constants for many colors – Color.red, Color.green, Color.blue, Color.black, Color.white, Color.yellow, Color.gray, Color.orange, Color.pink, Color.cyan, Color.magenta – Or you can use all uppercase names • Color.RED, Color.BLUE, … Georgia Institute of Technology Getting and Setting Pixel Colors • To get a pixel’s color as a color object – Color color = pixel.getColor(); – int red = color.getRed(); – int green = color.getGreen(); • To set a pixel’s color using a new color object – Color newColor = new Color(red,green,blue); – Pixel.setColor(newColor); Georgia Institute of Technology Using Classes in Packages • All classes in the Java language are in a package – You can use any class in java.lang • System, Math, Object • For classes in other packages you need to import them – import java.awt.Color; – Import java.awt.*; //import all classes in this package • To use the short name: Color • Or use the fully qualified name – packageName.ClassName Georgia Institute of Technology Undefined Class Error • If you forget to import a class – Yet, you use the short name for the class – It won’t compile • Undefined class error • Undefined class errors mean – You need to import the class – You misspelled the class Georgia Institute of Technology Pixel Exercise • In DrJava – Pick a file and create a picture object – Get the array of pixels from the picture object – Get the 1st pixel from the array of pixels – Get the red, green, and blue value for this pixel – Get the x and y location of this pixel – Get the color of this pixel • Get the red, green, and blue values of the color Georgia Institute of Technology Changing Pixel Colors • There are two ways to change the color of a pixel in a picture – pixel.setRed(value), pixel.setGreen(value), pixel.setBlue(value) – pixel.setColor(color) • But, you won’t see any change in the picture – Until you ask it to repaint • picture.repaint(); Georgia Institute of Technology Changing a Color • The Color class has methods for making a color object – Lighter • color.brighter(); – Darker • color.darker(); • Example > Color testColor = new Color(168,131,105); > System.out.println(testColor); java.awt.Color[r=168,g=131,b=105] > testColor = testColor.darker(); > System.out.println(testColor); java.awt.Color[r=117,g=91,b=73] > testColor = testColor.brighter(); > System.out.println(testColor); java.awt.Color[r=167,g=130,b=104] Georgia Institute of Technology Rounding Errors • Notice that when you made the color darker and then lighter the resulting color was slightly off of the original – The change is calculated in floating point – The result is stored in integer form – The decimal part is lost • Rounding errors also occur because of the limited storage for floating point numbers – We can’t store all the digits in some numbers Georgia Institute of Technology Changing a Picture Exercise > String fileName = "C:/intro-progjava/mediasources/catapillarClipart.jpg"; > Picture picture = new Picture(fileName); > picture.show(); > picture.getPixel(10,100).setColor(Color.black); > picture.getPixel(11,100).setColor(Color.black); > picture.getPixel(12,100).setColor(Color.black); > picture.getPixel(13,100).setColor(Color.black); > picture.getPixel(14,100).setColor(Color.black); > picture.getPixel(15,100).setColor(Color.black); > picture.getPixel(16,100).setColor(Color.black); > picture.getPixel(17,100).setColor(Color.black); > picture.getPixel(18,100).setColor(Color.black); > picture.getPixel(19,100).setColor(Color.black); > picture.repaint(); Georgia Institute of Technology How do we Know if it Worked? • A very important part of programming is testing the result – Just because code compiles and runs without error doesn’t mean it is correct – There could be an error in the logic – It could fail under certain conditions – It could even return the correct answer but for the wrong reason Georgia Institute of Technology The Picture Explorer • Tool that creates a copy of the current picture and lets you explore it – See the color, x, and y values at the cursor • To use the tool on a picture object – picture.explore(); • Use it to see if the colors have changed Georgia Institute of Technology Changing the Red in a Picture • One way to change a picture is to reduce the amount of red in it – What if we want to decrease it by half? • If we have a value of 255 what should the new value be? • How do we reduce any value by half? – What if we want to increase it by 25%? • If we have a value of 125 what should the new value be? • How do we increase any value by 25%? Georgia Institute of Technology Changing all the Pixels in a Picture • There are 360 x 181 = 65,160 pixels in the caterpillar picture • Do we really want to write the code to change each one of these – Get the current pixel – Get the red value of the current pixel – Change the red value of the current pixel to 0.5 the original value – Put the new red value in the current pixel Georgia Institute of Technology We Need a Loop (Iteration) • A way to execute a series of statements – With something changing each time the statements are executed • Different pixel to change – And some way to tell when we are done with the repetition • Some test to see if the loop should stop Georgia Institute of Technology Loop Exercise • Ask a person to clap 12 times – How does s/he know when to stop? – What changes each time s/he claps? • If you are following a recipe that asks you to stir the ingredients 50 times how would you do this? • What if you were trying to break a sit-up record – How would you know if you did break it? Georgia Institute of Technology Loops often need Counters • If you want to do something x times you often need a counter – That starts at 0 – And you add 1 to it each time you finish doing the thing you are repeating – When the counter reaches the number you are trying to do you stop the loop • What is the value of the counter the last time the statements of the loop are executed? Georgia Institute of Technology The While Loop • In Java one way to repeat a block of statements while an expression is true is to use a while loop • Create a counter and set it to the start value • Check that the counter is less then the stop value • If it is less than execute the statements in the loop • Add one to the counter and go back to check that the counter is less than the stop value Georgia Institute of Technology Total the Numbers from 1 to 100 • What if you want to add all the numbers from 1 to 100? – You will need something to hold the total • What type should it be? • What value should it start out with? – You will need something that counts from 1 to 100 • And add that value to the total • Stop when you get to 100 • What type should it be? What value should it start with? Georgia Institute of Technology While Loop Syntax • Adding up the numbers from 1 to 100 int counter = 1; int total = 0; while (counter <= 100) { total = total + counter; counter = counter + 1; } System.out.println(total); Georgia Institute of Technology Decrease Red Algorithm • To decrease the red value in a picture by 50% 1. Get the array of pixels from the picture 2. Set up an index to start at 0 3. Check if the index is less than the length of the array 1. If it is go on to step 4 of the loop 2. If it isn’t jump to the first instruction after the loop 4. Get the pixel at the current index from the array of pixels 5. Get the red value at the pixel 6. Multiply the red value by 0.5 7. Set the red value at the pixel to the reduced red value 8. Increment the index and go back to step 3 Georgia Institute of Technology What is an Algorithm? • An algorithm is a description of the steps needed to do a task – Can be written in English – A recipe is an algorithm • A program is an implementation of an algorithm – in a particular computer language Georgia Institute of Technology From Algorithm to Program (code) • How do we get the array of pixels from the current picture object? – We have used Pixel[] pixels = picture.getPixels(); – But we want to get the array of pixels from the current object Pixel[] pixels = this.getPixels(); – Or we can leave off the this Pixel[] pixels = getPixels(); Georgia Institute of Technology Loop Algorithm to Code • How to write (code) the loop? – Use a while loop with a counter for the index starting at 0 int index = 0; – Loop while the index is less than the length of the array while (index < pixels.length) – Get the current pixel from the array of pixels for the current index Pixel pixel = pixels[index]; Georgia Institute of Technology Loop Algorithm to Code - Continued – Get the red value at the pixel int redValue = pixel.getRed(); – Decrease the red value by 0.5 redValue = redValue * 0.5; – Set the pixel red value pixel.setRed(redValue); – Add one to (increment) the index • index = index + 1; Georgia Institute of Technology Decrease Red Method public void decreaseRed() { // get the array of pixels for this picture object Pixel[] pixels = this.getPixels(); // start the index at 0 int index = 0; // loop while the index is less than the length of the pixels array while (index < pixels.length) { // get the current pixel at this index Pixel pixel = pixels[index]; // get the red value at the pixel int redValue = pixel.getRed(); // set the red value to half what it was redValue = redValue * 0.5; // set the red for this pixel to the new value pixel.setRed(redValue); // increment the index index = index + 1; } } Georgia Institute of Technology Comments • Comments are explanations of your code to help people understand your code – They are ignored by the compiler • There are several kinds in Java // a comment that lasts to the end of the line /* a comment that can take up several lines until a */ /** a Javadoc comment that is used to create html documentation of your code */ Georgia Institute of Technology Loss of Precision • If you try the code on the previous slide you will get a compiler error – Possible loss of precision • It is complaining about putting a double value into a int variable – Loss of fractional part Georgia Institute of Technology Casting to Solve Loss of Precision Error • It will compile if we tell the compiler we know about the possible loss of precision – And that it is intended • By using a cast to int redValue = (int) (redValue * 0.5); • Notice that we cast the result of the multiplication back to an integer • Casting is forcing a value into a type (type) expression Georgia Institute of Technology Move Declarations Outside Loops • When you need a variable in a loop it is best to declare it before the loop – Otherwise you are declaring a new variable each time through the loop • Which is slower than just changing the value associated with the variable • In some languages you must declare all variables at the beginning of a method (function) • In Java you can declare variables anywhere in a method – As long as you declare them before you use them Georgia Institute of Technology Shortcuts for Common Operations • In programming you often need to add one to a value index = index + 1; • You may use the shortcut index++; or ++index; • If you wanted to subtract 1 instead index = index – 1; index--; or -- index; Georgia Institute of Technology Decrease Red Method Version 2 public void decreaseRed() { Pixel pixel = null; // the current pixel int redValue; // the amount of red // get the array of pixels for this picture object Pixel[] pixels = this.getPixels(); // start the index at 0 int index = 0; // loop while the index is less than the length of the pixels array while (index < pixels.length) { // get the current pixel at this index pixel = pixels[index]; // get the red value at the pixel redValue = pixel.getRed(); // set the red value to half what it was redValue = (int) (redValue * 0.5); // set the red for this pixel to the new value pixel.setRed(redValue); // increment the index index++; } } Georgia Institute of Technology Decrease Red Exercise • In DrJava – Add the method decreaseRed() to Picture.java – Compile the method – Test it by doing the following in the interactions pane > String fileName = "C:/intro-prog-java/mediasources/catapillarClipart.jpg"; > Picture picture = new Picture(fileName); > picture.show(); > picture.explore(); > picture.decreaseRed(); > picture.repaint(); > picture.explore(); Georgia Institute of Technology Tracing Code • An important skill to develop is the ability to trace code – Also called walking through or stepping through your code – Look at each line and predict what will happen – Show the variables and their values Georgia Institute of Technology Step Through decreaseRed() • A picture object was created from the file “catapillarClipart.jpg” and then was sent the message decreaseRed() • The picture object was implicitly passed to the method decreaseRed() • Some variables were declared for later use in the loop Pixel pixel = null; int redValue; • The array of pixel objects was returned from sending getPixels() to the picture object Pixel[] pixels = this.getPixels(); Georgia Institute of Technology Step Through decreaseRed() - cont • An index variable was declared and set to 0 int index = 0; • The while loop tests if the index is less than the length of the array while (index < pixels.length) { – And if so it executes the statements in the body of the loop {} • It sets the variable pixel to the pixel at the index in the array of pixels pixel = pixels[index]; • It gets the red value of that pixel redValue = pixel.getRed(); • it sets the red value to 0.5 * the original value redValue = (int) (redValue * 0.5); • It sets the pixel’s red to the new value pixel.setRed(redValue); • It increments the index value index++; Georgia Institute of Technology Memory Map of decreaseRed() width=360 height=181 • What does memory this look like the first time through the loop? • How about the 2nd pixels R=255, G=255, time through? B=255, X=0, pixel rd • How about the 3 Y=0 time through? • How about the last redValue = 255 time through? index = 0 Picture: Class getPixels() … R=255, R=255, G=255, G=255, B=255, B=255, X=1, X=2, Y=0 Y=0 Pixel: Class getRed() setRed()… Georgia Institute of Technology … Increase Red • What if you want to increase red by 30% – How would you do that? • Multiplying by 0.5 reduces the red by 50% – Multiplying by 1.0 would keep the same red value – Multiplying by 1.3 would increase the red by 30% – Multiplying by 1.7 would increase the red by 70% Georgia Institute of Technology Increase Red Algorithm • To increase the red value in a picture by 30% 1. Get the array of pixels from the picture 2. Set up an index to start at 0 3. Check if the index is less than the length of the array 1.If it is go on to step 4 of the loop 2.If it isn’t jump to the first instruction after the loop 4. Get the pixel at the current index from the array of pixels 5. Get the red value at the pixel 6. Multiply the red value by 1.3 7. Set the red value at the pixel to the reduced red value 8. Increment the index and go back to step 3 Georgia Institute of Technology Increase Red: Algorithm to Code • The algorithm for this method is very similar to the algorithm for decrease red – Start with the code for decreaseRed – Change the line of code that multiplied the current red value by 0.5 to 1.3 – Change the name of the method – Change any comments that need changing Georgia Institute of Technology increaseRed Method public void increaseRed() { Pixel pixel = null; // the current pixel int redValue; // the amount of red // get the array of pixels for this picture object Pixel[] pixels = this.getPixels(); // start the index at 0 int index = 0; // loop while the index is less than the length of the pixels array while (index < pixels.length) { // get the current pixel at this index pixel = pixels[index]; // get the red value at the pixel redValue = pixel.getRed(); // increase the red value to 30% more than the original redValue = (int) (redValue * 1.3); // set the red for this pixel to the new value pixel.setRed(redValue); // increment the index index++; } } Georgia Institute of Technology Clear the Blue Values • What if you want to clear the blue from a picture – Set all the blue values to 0 for all the pixels • The algorithm is similar to decreaseRed() and increaseRed() except – You don’t need to get the value out for blue and multiply it by some value and then set the blue value – Just set the blue value to 0 Georgia Institute of Technology Clear Blue Exercise • In Picture.java • Write the object method – public void clearBlue() • That sets the blue value to 0 for all pixels in a picture Georgia Institute of Technology Faking a Sunset • If you want to make an outdoor scene look like it happened during sunset – You might want to increase the red • But you can’t increase past 255 – Another idea is to reduce the blue and green • To emphasize the red • Try to reduce the blue and green by 30% Georgia Institute of Technology Faking a Sunset Algorithm • Reduce the blue and green by 30% 1. Get the array of pixels from the picture 2. Set up an index to start at 0 3. Check if the index is less than the length of the array 1.If it is go on to step 4 of the loop 2.If it isn’t jump to the first instruction after the loop 4. Get the pixel at the current index from the array of pixels 5. Set the blue value at the pixel to 0.7 times the original value 6. Set the green value at the pixel to 0.7 times the original value 7. Increment the index and go back to step 3 Georgia Institute of Technology Faking a Sunset Method /** * Method to fake a sunset by reducing the blue and green in a picture * by 30% */ public void fakeSunset() { Pixel pixel = null; // the current pixel // get the array of pixels for this picture object Pixel[] pixels = this.getPixels(); // start the index at 0 int index = 0; // loop while the index is less than the length of the pixels array while (index < pixels.length) { // get the current pixel at this index pixel = pixels[index]; // set the blue value to 0.7 times the original blue value pixel.setBlue((int) 0.7 * pixel.getBlue()); // set the green value to 0.7 times the original green value pixel.setGreen((int) 0.7 * pixel.getGreen()); // increment the index index++; } } Georgia Institute of Technology A For Loop • Programmers like shortcuts – Especially those that reduce errors – And mean less typing • We have been using a while loop with an index – We had to declare the index variable and initialize it before the loop • If you forget this there will be a compiler error – We had to increment the index in the loop • If you forget this it will be an infinite loop • The shortcut for this is a For Loop Georgia Institute of Technology For Loop Syntax • for (initialization area; continuation test; change area) – Initialization area • Declare variables and initialize them – Continuation test • If true do body of loop • If false jump to next statement after the loop – Change area • Change the loop variables – Increment or decrement them Georgia Institute of Technology Comparison of While and For Loops int index = 0; while (index < pixels.length) { statements . . . index++; } for (int i=0; i < pixels.length; i++) { statements . . . } Georgia Institute of Technology Change clearBlue() to use a For Loop /** * Method to clear the blue from the picture (set * the blue to 0 for all pixels) */ public void clearBlue() { Pixel pixel = null; // current pixel in loop /** * Method to clear the blue from the picture (set * the blue to 0 for all pixels) */ public void clearBlue() { // get the array of pixels from the current picture Pixel[] pixels = this.getPixels(); // declare the variable that will refer to the current pixel Pixel pixel = null; // get the array of pixels from the current picture Pixel[] pixels = this.getPixels(); // declare the index and initialize it to 0 int index = 0; // loop through all the pixels for (int i = 0; i < pixels.length; i++) { // get the current pixel pixel = pixels[i]; // loop through all the pixels while (index < pixels.length) { // get the current pixel pixel = pixels[index]; // set the blue on the pixel to 0 pixel.setBlue(0); // set the blue on the pixel to 0 pixel.setBlue(0); index++; } } } } Georgia Institute of Technology Using System.out.println() in a Loop • One way to check what is happening in your program is to add – System.out.println(expression); • You might add this to the loop to check the value of ‘i’ during it. – And to verify that the increment happens after the last statement in the loop Georgia Institute of Technology Change to For Loop Exercise • Edit fakeSunset() and change it from using a while loop to using a for loop – Move the declaration of the index to the for loop initialization area – Move the index increment to the for loop change area – Execute the code to make sure it still works Georgia Institute of Technology Negating an Image • How would you turn a picture into a negative? – White should become black • 255,255,255 becomes 0,0,0 – Black should become white • 0,0,0 becomes 255,255,255 Georgia Institute of Technology Negate Algorithm • Subtract current value from 255 for red, green, and blue 1. Get the array of pixels from the picture 2. Loop starting an index at 0 and incrementing by 1 3. Check if the index is less than the length of the array 1.If it is go on to step 4 of the loop 2.If it isn’t jump to the first instruction after the loop 4. Get the pixel at the current index from the array of pixels 5. Set the red value to 255 – current red value 6. Set the blue value to 255 – current blue value 7. Set the green value to 255 – current green value 8. Increment the index and go back to step 3 Georgia Institute of Technology Negate Method /** * Method to negate the picture */ public void negate() { Pixel pixel = null; // get the array of pixels Pixel[] pixels = this.getPixels(); // loop through all the pixels for (int i = 0; i < pixels.length; i++) { // get the current pixel pixel = pixels[i]; // set the pixel color values to the new values pixel.setRed(255 - pixel.getRed()); pixel.setGreen(255 - pixel.getGreen()); pixel.setBlue(255 - pixel.getBlue()); } } Georgia Institute of Technology Changing to Grayscale • Grayscale ranges from black to white – The red, green, and blue values are the same • How can we change any color to gray? – What number can we use for all three values? • The intensity of the color – We can average the colors • (red + green + blue) / 3 – Example • (15 + 25 + 230) / 3 = 90 Georgia Institute of Technology Grayscale Algorithm • Set color values to the average of the original values 1. Get the array of pixels from the picture 2. Loop starting an index at 0 3. Check if the index is less than the length of the array 1.If it is go on to step 4 of the loop 2.If it isn’t jump to the first instruction after the loop 4. Get the pixel at the current index from the array of pixels 5. Calculate the average of the current values 6. Set the red value to the average 7. Set the blue value to the average 8. Set the green value to the average 9. Increment the index and go to step 3 Georgia Institute of Technology Grayscale Method /** * Method to change the picture to gray scale */ public void grayscale() { Pixel pixel = null; int intensity = 0; // get the array of pixels Pixel[] pixels = this.getPixels(); // loop through all the pixels for (int i = 0; i < pixels.length; i++) { // get the current pixel pixel = pixels[i]; // compute the intensity of the pixel (average value) intensity = (int) ((pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3); // set the pixel color to the new color pixel.setColor(new Color(intensity,intensity,intensity)); } } Georgia Institute of Technology Grayscale Result Georgia Institute of Technology Luminance • We perceive blue to be darker than red, and green – Even when the same amount of light is reflected • A better grayscale model should take this into account – Weight green the highest (* 0.587) – red less (* 0.299) and – blue the very least (* 0.114) Georgia Institute of Technology Grayscale with Luminance Exercise • Create a new method grayscaleWithLumina nce • Using the new algorithm for calculating intensity Georgia Institute of Technology Summary • Pictures have a grid of pixels – You can change the picture by changing the color of the pixel – You will need to repaint the picture • Arrays let you store and retrieve values of the same type using an index • You will need to import classes that you wish to use that aren’t in java.lang • Loops allow you to execute a block of statements zero to many times Georgia Institute of Technology