DOC PREVIEW
Stanford CS 106A - Lecture Notes

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Multidimensional Arrays and Image ManipulationGraphics Contest ResultsSlide 3Class StandingsSlide 5In Our Last Episode . . .Multidimensional ArraysMultidimensional Arrays and ImagesPixel ArraysPixel ValuesTransparencyImage ManipulationThe FlipVertical ProgramSelecting Color ComponentsStatic Methods in GImageCreating a Grayscale ImageThe createGrayscaleImage MethodSteganographyThe EndMultidimensional Arraysand Image ManipulationEric RobertsCS 106AFebruary 17, 2010Graphics Contest ResultsThe CS106AGraphics ContestFebruary 2010Graphics Contest ResultsFirst place (algorithmic): Dustin Janatpour, Ball PitRunner-up (algorithmic): Thomas Davids, BaseballRunner-up (algorithmic): Austin Hulse, Ocean CliffFirst place (aesthetic): Caroline Shen, Egg Adoption CenterRunner-up (aesthetic): Michael Greer, Tricky FixieRunner-up (aesthetic): Austin Hulse, Ocean CliffHonorable mention: Angela Hayes, Fountain HoppingHonorable mention: Hong Jang, PongHonorable mention: William Monroe IV, Egyptian Rat ScrewBest in show: Joseph Victor, Fractal BopRunner-up (aesthetic): Dustin Janatpour, Ball PitHonorable mention: Jon Rodriguez, BreakInClass Standings20/580.3419/730.2619/390.496/480.13Freshman Sophomore Junior Senior28/730.3822/390.5635/580.609/480.19Multidimensional Arraysand Image ManipulationIn Our Last Episode . . .•In last week’s section, you were asked to assume the existence of a methoddouble[] readScoresArray(String filename)that reads score data from the specified file, one value per line, and returns an array of doubles containing those values.•Before we start on today’s topic, I want to finish this exercise so that you have another code example for working with files.Multidimensional Arrays•Because the elements of an array can be of any Java type, those elements can themselves be arrays. Arrays of arrays are called multidimensional arrays.•In Java, you can create a multidimensional array by using multiple brackets in both the type and the initialization parts of the declaration. For example, you can create array space for a 3 x 3 tic-tac-toe board using the following declaration:char[][] board = new char[3][3];•This declaration creates a two-dimensional array of characters that is organized like this:board[0][0] board[0][1] board[0][2]board[1][0] board[1][1] board[1][2]board[2][0] board[2][1] board[2][2]Multidimensional Arrays and Images•One of the best examples of multidimensional arrays is a Java image, which is logically a two-dimensional array of pixels.•Consider, for example, the logo for the Java Task Force at the top right. That logo is actually an array of pixels as shown in the expanded diagram at the bottom.•The GImage class allows you to convert the data for the image into a two-dimensional array of pixel values. Once you have this array, you can work with the data to change the image.Pixel Arrays•If you have a GImage object, you can obtain the underlying pixel array by calling the getPixelArray, which returns a two-dimensional array of type int.•For example, if you wanted to get the pixels from the image file JTFLogo.gif, you could do so with the following code:GImage logo = new GImage("JTFLogo.gif");int[][] pixels = logo.getPixelArray();•The first subscript in a pixel array selects a row in the image, beginning at the top. The height of the image is therefore given by the expression pixels.length.•The second subscript in a pixel array selects an individual pixel within a row, beginning at the left. You can use the expression pixels[0].length to determine the width of the image.Pixel Values•Each individual element in a pixel array is an int in which the 32 bits are interpreted as follows:•The first byte of the pixel value specifies the transparency of the color, which is described in more detail on the next slide.1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 0 0 1 1 0 0 1 1•The next three bytes indicate the amount of red, green, and blue in the pixel, in which each value varies from 0 to 255. Together, these three bytes form the RGB value of the color, which is typically expressed using six hexadecimal digits. The color in the example has the RGB value 0x996633, which is a light brown:transparency () red green blueTransparency•The first byte of the pixel value specifies the transparency of the color, which indicates how much of the background shows through. This value is often denoted using the Greek letter alpha ().•Transparency values vary from 0 to 255. The value 0 is used to indicate a completely transparent color in which only the background appears. The value 255 indicates an opaque color that completely obscures the background. The standard color constants all have alpha values of 255.•Fully transparent colors are particularly useful in images, because they make it possible to display images that do not have rectangular outlines. For example, if the gray pixels in the corners of the JTFLogo.gif image have an alpha value of 0, the background will show through those parts of the logo.Image Manipulation•You can use the facilities of the GImage class to manipulate images by executing the following steps: •The program on the next slide shows how you can apply this technique to flip an image vertically. The general strategy for inverting the image is simply to reverse the elements of the pixel array, using the same technique as the reverseArray method on an earlier slide. Read an existing image from a file into a GImage object. 1.Call getPixelArray to get the pixels.2.Write the code to manipulate the pixel values in the array.3.Call the GImage constructor to create a new image.4.The FlipVertical Programskip simulationFlipVerticalpublic void run() { GImage original = new GImage("Candle.gif"); GImage flipped = flipVertical(original); double y = (getHeight() - original.getHeight()) / 2; double x1 = (getWidth() - IMAGE_SEP) / 2; double x0 = x1 - original.getWidth() - IMAGE_SEP; add(original, x0, y); add(flipped, x1, y);}originalflippedyx1x024305164private GImage flipVertical(GImage image) { int[][] array = image.getPixelArray(); int height = array.length; for (int p1 = 0; p1 < height / 2; p1++) { int p2 = height - p1 - 1; int[] temp = array[p1];


View Full Document

Stanford CS 106A - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?