33-Two-Dimensional-Arrays - CS106A Handout 33 May 16th,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106A Handout 33 Spring 2011 May 16 th , 2011 Two-Dimensional Arrays, Images There are two large examples I’ll use to illustrate how two-dimensional arrays and images (which are quite clearly two-dimensional arrays of pixels) can be defined and programmatically manipulated. Image Processing Here’s a program that takes an image of Marilyn Monroe and, Andy Warhol style, displays the same images in three different ways—once with just blue turned on and red and green turned off, once with just red but no green or blue, and once with green and nothing else. The program is fairly short and helps us understand how information about RGB content can be stuffed into a single int ’s worth of memory. public class WarholImages extends GraphicsProgram { public void run() { setTitle("Andy Warhol Homage"); GImage primaryImage = new GImage("marilyn.jpg"); GImage blueImage = createPrimaryColorImage(primaryImage, BLUE_MASK ); GImage redImage = createPrimaryColorImage(primaryImage, RED_MASK ); GImage greenImage = createPrimaryColorImage(primaryImage, GREEN_MASK ); addImage(primaryImage, getWidth()/4, getHeight()/4); addImage(blueImage, 3 * getWidth()/4, getHeight()/4); addImage(redImage, getWidth()/4, 3 * getHeight()/4); addImage(greenImage, 3 * getWidth()/4, 3 * getHeight()/4); } private void addImage(GImage image, int cx, int cy) { double ulx = cx - image.getWidth()/2; double uly = cy - image.getHeight()/2; add(image, ulx, uly); } private GImage createPrimaryColorImage(GImage image, int mask) { int [][] pixels = image.getPixelArray(); for ( int row = 0; row < pixels.length; row++) { for ( int col = 0; col < pixels[0].length; col++) { pixels[row][col] = transformPixel(pixels[row][col], mask); } } return new GImage(pixels); } private int transformPixel( int pixel, int mask) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/28/2011 for the course CS 106A taught by Professor Sahami,m during the Spring '08 term at Stanford.

Page1 / 6

33-Two-Dimensional-Arrays - CS106A Handout 33 May 16th,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online