Session 16 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Session 16

Description:

Scaling Down My camera takes photos at 2304 x 1708 pixels. ... Yesterday, Ashley had a photo of Paris Hilton she wanted to make bigger so she ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 17
Provided by: BenSc
Category:
Tags: hilton | paris | session

less

Transcript and Presenter's Notes

Title: Session 16


1
Session 16
  • Scaling

2
What can I do when things arent the size I want?
  • Scaling Down My camera takes photos at 2304 x
    1708 pixels. That is WAY too big for posting to
    my website (where 300 x 200 would still be a good
    sized picture).

3
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)

4
Scaling Down Algorithm (different from that in
book)
  • Create an instance of the source picture
  • Invoke the method on the source picture
  • Create the new target picture at half size
  • Loop with x starting at 0 and as long as
    x
    through the loop.
  • Loop with y starting at 0 and as long as y target height, increment y by 1 each time through
    the loop
  • For each x,y value in the target picture, copy
    the pixel from x2,y2 from the source picture

5
  • public Picture scaleDown()
  • Picture other new Picture(this.getWidth()/2
    , this.getHeight()/2)
  • Pixel source null
  • Pixel target null
  • for (int x0 x
  • for (int y0 y
  • source getPixel(x2,y2)
  • target other.getPixel(x,y)
  • target.setColor(source.getColor())
  • return other

6
Trying the scaleDown() method
  • Create an instance of the original photo
  • Picture p new Picture(FileChooser.getMediaPath(
    minol.jpg))
  • Invoke the scaleDown() method, capturing the
    result
  • Picture p1 p.scaleDown()
  • Show the result
  • p1.show()

7
Do you see any potential problems with this
algorithm?
  • public Picture scaleDown()
  • Picture other new Picture(this.getWidth()/2
    , this.getHeight()/2)
  • Pixel source null
  • Pixel target null
  • for (int x0 x
  • for (int y0 y
  • source getPixel(x2,y2)
  • target other.getPixel(x,y)
  • target.setColor(source.getColor())
  • return other

What happens if width or height is an odd number?
8
A simple fix
  • public Picture scaleDown()
  • Picture other new
  • Picture((this.getWid
    th()1)/2,(this.getHeight()1)/2)
  • Pixel source null
  • Pixel target null
  • for (int x0 x
  • for (int y0 y
  • source getPixel(x2,y2)
  • target other.getPixel(x,y)
  • target.setColor(source.getColor())
  • return other

9
What can I do when things arent the size I want?
  • Scaling Up Yesterday, Ashley had a photo of
    Paris Hilton she wanted to make bigger so she
    could put a pigs head on her body.

10
Thinking Through Scaling Up
0
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)

0
1
0
1
2
3
4
5
0
1
2
3
11
Scaling Up Algorithm (again, different)
  • Create the source picture
  • Invoke the method on the source picture
  • Create the target picture at twice the size
  • Loop with source x starting at 0 and target x
    starting at 0 as long as
  • 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
  • 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

12
  • public Picture scaleUpBlocks()
  • Picture other new Picture(this.getWidth()2,
    this.getHeight()2)
  • Pixel source null, target null
  • for (int x0 x
  • for (int y0 y
  • source getPixel(x,y)
  • target other.getPixel(x2,y2)
  • target.setColor(source.getColor())
  • target other.getPixel(x21,y2) //to
    the right
  • target.setColor(source.getColor())
  • target other.getPixel(x2,y21)
    //below
  • target.setColor(source.getColor())
  • target other.getPixel(x21,y21)
    //diagonal right and below
  • target.setColor(source.getColor())

13
Trying the scaleUpBlocks() method
  • Create an instance of the original photo
  • Picture p new Picture(FileChooser.getMediaPath(
    butterfly.jpg))
  • Invoke the scaleDown() method, capturing the
    result
  • Picture p1 p.scaleUpBlocks()
  • Show the result
  • p1.show()

14
Can you think of a less blocky way to do this?
  • Average two adjoining Pixels to come up with a
    brand new Pixel between them
  • Has strengths and weaknesses as an overall
    solution

15
  • public Picture scaleUpAverage()
  • Picture other new Picture(this.getWidth()2-
    1,this.getHeight()2-1)
  • Pixel one null
  • Pixel two null
  • Pixel target null
  • for (int x0 x
  • for (int y0 y
  • one getPixel(x,y)
  • target other.getPixel(x2,y2)
  • target.setColor(one.getColor())
  • two getPixel(x1,y)
  • target other.getPixel(x21,y2)
  • target.setColor(new Color (
    (one.getRed()two.getRed())/2,
  • (one.getGreen()two.
    getGreen())/2,
  • (one.getBlue()two.g
    etBlue())/2 ))

16
  • two getPixel(x1,y1)
  • target other.getPixel(x21,y21)
  • c.setColor(new Color (
    (one.getRed()two.getRed())/2,
  • (one.getGreen()two.
    getGreen())/2,
  • (one.getBlue()two.g
    etBlue())/2 ))
  • two getPixel(x,y1)
  • target other.getPixel(x2,y21)
  • c.setColor(new Color (
    (one.getRed()two.getRed())/2,
  • (one.getGreen()two.
    getGreen())/2,
  • (one.getBlue()two.g
    etBlue())/2 ))
  • return other
Write a Comment
User Comments (0)
About PowerShow.com