Download presentation
Presentation is loading. Please wait.
1
CSC1401 Viewing a picture as a 2D image - 2
2
Review from the last week
We treated a picture as a 2 dimensional array, and used the getPixel(x,y) method to access each of the pixels Since there were 2 dimensions (x and y), we needed a nested loop to access all of the pixels
3
Review A two-dimensional array has columns and rows
Use nested loops to work with 2-d arrays One loop for the x direction and one for the y for (int x = 0; x < this.getWidth(); x++) { // loop through the rows (y direction) for (int y = 0; y < this.getHeight(); y++)
4
Learning Goals Understand at a conceptual and practical level
How to copy pixels from one picture to another How to copy pixels from one picture to a specified location in another picture How to create larger or smaller pictures
5
Copying Pixels to a New Picture
Need to track the source picture x and y And the target picture x and y 1 2 3 4 1 2 3 4
6
Copying Pixels to a New Picture
1 What if we want to copy the target to a different location in the source Than 0,0 Say startX and startY What is an algorithm that will do this? 1 2 3 4 1 1 2 3 1 2 3 4 1 2 3
7
Copy Picture Algorithm
Copy a picture into another picture Create the picture objects Invoke the method on the target picture Loop through the source picture pixels Get the source and target pixels Set the color of the target pixel to the color of the source pixel
8
Copy Algorithm to Code Loop through the source pixels
// loop through the columns for (x = 0; x < insertedPicture.getWidth(); x++) { // loop through the rows for (y = 0; y < insertedPicture.getHeight(); y++)
9
Copy Algorithm to Code – Cont
Get the source and target pixels copyPixel = insertPicture.getPixel(x,y); targetX = ???; targetY = ???; targetPixel = this.getPixel(targetX,targetY); Set the color of the target pixel to the color of the source pixel targetPixel.setColor(copyPixel.getColor());
10
Creating a method What parameters are needed?
11
Copy Method
12
Trying out our method Copying Katie onto the beach
I found an x value of 247 and a y value of 125 was a good location to copy her onto the beach (using the explore)
13
Scaling You can make a picture smaller You can make a picture larger
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
14
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
15
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
16
Scaling Down Method
17
Invoking the method Picture smaller = stevepicture.decreasePicture (2);
18
Thinking Through Scaling Up
1 2 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 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
19
Scaling Up Algorithm Create the target picture
Invoke the method on the target picture Loop with source x starting at 0 as long as < source width Loop with source y starting at 0 as long as < source height Loop another x from 0 up to the factor of multiplication Loop another y from 0 up to the factor of multiplication 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.
21
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”);
22
Summary To copy pixels from one picture to another
Keep track of the sourceX, sourceY and targetX and targetY To copy one picture to a location in another Just keep track of the values for targetX and targetY
23
Assignment Review Media Computation Chapter 5, Section 2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.