Download presentation
Presentation is loading. Please wait.
Published byVajrahast Batra Modified over 5 years ago
1
Two-Dimensional Arrays and Nested Loops – part 6
Barb Ericson Georgia Institute of Technology August 2005 Georgia Institute of Technology
2
Georgia Institute of Technology
Learning Goals Understand at a conceptual and practical level How can you make a picture smaller Scale it down How can you make a picture bigger Scale it up How do you create a collage? Georgia Institute of Technology
3
Georgia Institute of Technology
Scaling You can make a picture smaller Faster to download on the web Increment the source x and y by a number larger than 1 Don’t use all the source pixels in target You can make a picture larger Show more detail Copy the same source x and y to more than one target x and y Use source pixels more than once in target Georgia Institute of Technology
4
Georgia Institute of Technology
Scaling Down a Picture passionFlower.jpg is 640pixels wide and 480 pixels high If we copy every other pixel we will have a new picture with width (640 / 2 = 320) and height (480 / 2 = 240) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 8 10 Georgia Institute of Technology
5
Scaling Down Algorithm
Create the target picture Invoke the method on the target picture Create the source picture Loop with source x starting at 0 and target x starting at 0 as long as < source width Increment the source x by 2 each time through the loop, increment the target x by 1 Loop with source y starting at 0 and target y starting at 0 as long as < source height Increment the source y by 2 each time through the loop, increment the target y by 1 Copy the color from the source to target pixel Georgia Institute of Technology
6
Georgia Institute of Technology
Scaling Down Method public void copyFlowerSmaller() { Picture flowerPicture = new Picture( FileChooser.getMediaPath(“passionFlower.jpg")); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX=0; sourceX < flowerPicture.getWidth(); sourceX+=2, targetX++) Georgia Institute of Technology
7
Scaling Down Method - Continued
// loop through the rows for (int sourceY=0, targetY=0; sourceY < flowerPicture.getHeight(); sourceY+=2, targetY++) { sourcePixel = flowerPicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } sourceY+= 2 means the same as sourceY = sourceY + 2; Georgia Institute of Technology
8
Trying Copy Flower Smaller
Create a new picture half the size of the original picture (+ 1 if odd size) Picture p1 = new Picture(320,240); Copy the flower to the new picture p1.copyFlowerSmaller(); Show the result p1.show(); Georgia Institute of Technology
9
Thinking Through Scaling Up
Copy each pixel in the source multiple times to the target Source (0,0) Target (0,0) Source (0,0) Target(1,0) Source (1,0) Target(2,0) Source (1,0) Target(3,0) Source (2,0) Target(4,0) Source (2,0) Target(5,0) Source (0,0) Target(0,1) Source (0,0) Target(1,1) 1 2 1 2 3 4 5 6 1 1 2 3 4 5 1 2 3 4 5 6 1 What could we add to source x and source y so that it is 0 twice, then 1 twice, then 2 twice, etc. Remember that 0.5 is 0 when you cast to integer. 2 3 Georgia Institute of Technology
10
Georgia Institute of Technology
Scaling Up Algorithm Create the target picture Invoke the method on the target picture Create the source picture Loop with source x starting at 0 and target x starting at 0 as long as < source width Increment the source x by 0.5 each time through the loop, increment the target x by 1 Loop with source y starting at 0 and target y starting at 0 as long as < source height Increment the source y by 0.5 each time through the loop, increment the target y by 1 Copy the color from the source to target pixel The source x and y will need to be double variables. Cast them to int to lose the fractional part to get the pixel x and y. Georgia Institute of Technology
11
Georgia Institute of Technology
Scaling Up Exercise Write a method copyFlowerBigger to scale up the picture flower1.jpg when you copy it to 640x480.jpg Save the result to a file using pictureObj.write(“file”); Georgia Institute of Technology
12
Georgia Institute of Technology
Create a Collage One of the things that you can do with pictures is create a collage There are two pictures of flowers flower1.jpg flower2.jpg Both pictures are 100 pixels wide The target picture is created from file 640x480.jpg The Picture’s bottom left is at x = 0 y = height - 5 Georgia Institute of Technology
13
Create Collage Algorithm
Create the target picture object Using the 640x480 file Invoke the method on the target picture Create the flower picture objects using flower1.jpg as source1Picture using flower2.jpg as source2Picture Set targetBottomY to the targetPicture height – 5 5 pixels from bottom of picture Georgia Institute of Technology
14
Create Collage Algorithm - Cont
Copy all of source1Picture to the current picture starting at x=0, y=targetBottomY – source1Picture’s height Copy all of source2Picture to the current picture starting at x=100, y = targetBottomY – source2Picture’s height Negate source1Picture Copy all of source1Picture to the current picture starting at x=200, y= targetBottomY – source1Picture’s height Clear the blue from source2Picture Copy all of source2Picture to the current picture starting at x=300, y= targetBottomY – source2Picture’s height Copy all of source1Picture to the current picture starting at x=400, y=targetBottomY – source1Picture’s height Georgia Institute of Technology
15
Create Flower Collage Method
public void createFlowerCollage() { Picture source1Picture = new Picture(FileChooser.getMediaPath("flower1.jpg")); Picture source2Picture = new Picture(FileChooser.getMediaPath("flower2.jpg")); int targetBottomY = this.getHeight() - 5; // copy source1Picture to 0, targetBottomY - height this.copy(source1Picture,0,0, source1Picture.getWidth(), source1Picture.getHeight(), 0,targetBottomY - source1Picture.getHeight()); // copy source2Picture to 100, targetBottomY - height this.copy(source2Picture,0,0, source2Picture.getWidth(), source2Picture.getHeight(), 100,targetBottomY - source2Picture.getHeight()); Georgia Institute of Technology
16
Create Flower Collage Method - Cont
// negate the source1Picture source1Picture.negate(); // copy negated source1Picture to 200 this.copy(source1Picture,0,0, source1Picture.getWidth(), source1Picture.getHeight(), 200,targetBottomY - source1Picture.getHeight()); // clear the blue from source 2 picture source2Picture.clearBlue(); // copy source2Picture to 300 this.copy(source2Picture,0,0, source2Picture.getWidth(), source2Picture.getHeight(), 300,targetBottomY - source2Picture.getHeight()); // copy negated source1Picture to 400 400,targetBottomY - } Georgia Institute of Technology
17
Testing createFlowerCollage
String file = FileChooser.getMediaPath(“640x480.jpg”); Picture p = new Picture(file); p.show(); p.createFlowerCollage(); p.repaint(); Georgia Institute of Technology
18
Create Collage Exercise
Try creating a collage At least 4 copies of an image in it The original image and 3 changes to the original image Scale, rotate, crop, change colors Then mirror the whole picture horizontally Save your collage using pictureObj.write(fileName); See for some examples from Georgia Tech students. Georgia Institute of Technology
19
Georgia Institute of Technology
Summary You can scale a picture down By not copying all the pixels Increment the source index by a number > 1 You can scale a picture up By copying the source pixels more than one time You can increment the source index by 0.5 and then cast it to integer You can create an image collage Using the methods we have written Georgia Institute of Technology
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.