CSC 1401 Viewing a picture as a 2

  • Slides: 23
Download presentation
CSC 1401 Viewing a picture as a 2 D image 2

CSC 1401 Viewing a picture as a 2 D image 2

Review from the last week We treated a picture as a 2 dimensional array,

Review from the last week We treated a picture as a 2 dimensional array, and used the get. Pixel(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

Review A two-dimensional array has columns and rows Use nested loops to work with

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. get. Width(); x++) { // loop through the rows (y direction) for (int y = 0; y < this. get. Height(); y++) {

Learning Goals Understand at a conceptual and practical level How to copy pixels from

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

Copying Pixels to a New Picture Need to track the source picture x and

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 3 2 4

Copying Pixels to a New Picture What if we want to copy the target

Copying Pixels to a New Picture What if we want to copy the target to a different location in the source Than 0, 0 Say start. X and start. Y What is an algorithm that will do this? 0 1 1 2 3 4 0 1 2 3 1 3 2 4

Copy Picture Algorithm Copy a picture into another picture Create the picture objects Invoke

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

Copy Algorithm to Code Loop through the source pixels // loop through the columns

Copy Algorithm to Code Loop through the source pixels // loop through the columns for (x = 0; x < inserted. Picture. get. Width(); x++) { // loop through the rows for (y = 0; y < inserted. Picture. get. Height(); y++) {

Copy Algorithm to Code – Cont Get the source and target pixels copy. Pixel

Copy Algorithm to Code – Cont Get the source and target pixels copy. Pixel = insert. Picture. get. Pixel(x, y); target. X = ? ? ? ; target. Y = ? ? ? ; target. Pixel = this. get. Pixel(target. X, target. Y); Set the color of the target pixel to the color of the source pixel target. Pixel. set. Color(copy. Pixel. get. Color());

Creating a method What parameters are needed?

Creating a method What parameters are needed?

Copy Method

Copy Method

Trying out our method Copying Katie onto the beach I found an x value

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)

Scaling You can make a picture smaller Faster to download on the web Increment

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

Scaling Down a Picture passion. Flower. jpg is 640 pixels wide and 480 pixels

Scaling Down a Picture passion. Flower. jpg is 640 pixels wide and 480 pixels high If we copy every other pixel we will have a 0 new picture with width 4 (640 / 2 = 320) and height (480 / 2 = 240) 8 12 1 5 9 13 2 6 10 14 3 0 7 8 11 15 2 10

Scaling Down Algorithm Create the target picture Invoke the method on the target picture

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

Scaling Down Method

Scaling Down Method

Invoking the method Picture smaller = stevepicture. decrease. Picture (2);

Invoking the method Picture smaller = stevepicture. decrease. Picture (2);

Thinking Through Scaling Up Copy each pixel in the source multiple times to the

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) 0 1 2 1 4 2 5 3 6 0 1 0 0 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5 6 6

Scaling Up Algorithm Create the target picture Invoke the method on the target picture

Scaling Up Algorithm Create the target picture Invoke the method on the target picture Create 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

Scaling Up Exercise Write a method copy. Flower. Bigger to scale up the picture

Scaling Up Exercise Write a method copy. Flower. Bigger to scale up the picture flower 1. jpg when you copy it to 640 x 480. jpg Save the result to a file using picture. Obj. write(“file”);

Summary To copy pixels from one picture to another Keep track of the source.

Summary To copy pixels from one picture to another Keep track of the source. X, source. Y and target. X and target. Y To copy one picture to a location in another Just keep track of the values for target. X and target. Y

Assignment Review Media Computation Chapter 5, Section 2

Assignment Review Media Computation Chapter 5, Section 2