CSC1401 Manipulating Pictures 3 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CSC1401 Manipulating Pictures 3

Description:

Reduce the blue value at each pixel by 50% Get the array of pixels from the picture ... We parameterize how much to decrease the blue by ... – PowerPoint PPT presentation

Number of Views:570
Avg rating:3.0/5.0
Slides: 17
Provided by: Wanda6
Category:

less

Transcript and Presenter's Notes

Title: CSC1401 Manipulating Pictures 3


1
CSC1401Manipulating Pictures 3
2
Recall last class
  • We used a while loop to change each pixels color

3
removeBlue() using while
4
Another Picture Manipulation
  • Lets write a method to modify all the pixels in
    a picture
  • Reduce the blue value at each pixel by 50
  • Get the array of pixels from the picture
  • Loop through all the pixels in the picture
  • Get the blue value at the current pixel
  • Calculate the new blue value
  • Set the blue value of the current pixel to the
    new value

5
Decrease Blue Algorithm
  • To decrease the blue value in a picture by 50
  • Get the array of pixels from the picture
  • We want to loop through all the pixels in a
    picture so we need to declare and initialize some
    variables
  • Set up an index to start at 0
  • Check if the index is less than the length of the
    array
  • Get the pixel at the current index from the array
    of pixels
  • Get the blue value at the pixel
  • Divide the blue value by 2
  • Set the blue value at the pixel to the reduced
    blue value
  • Increment the index and go back to step 3

6
Loop Algorithm to Code
  • How to write (code) the loop?
  • Use a while loop with a counter for the index
    starting at 0
  • int index 0
  • Loop while the index is less than the length of
    the array
  • while (index
  • Get the current pixel from the array of pixels
    for the current index
  • Pixel pixelObj pixelArrayindex

7
Loop Algorithm to Code - Continued
  • Get the blue value at the pixel
  • int valueBlue pixelObj.getBlue()
  • Divide the blue value by 2
  • valueBlue valueBlue / 2
  • Set the pixel blue value
  • pixel.setBlue(valueBlue)
  • Add one to (increment) the index
  • index index 1

8
Decrease Blue Method
  • public void decreaseBlue()
  • Pixel pixelArray this.getPixels()
  • Pixel pixelObj null
  • int index 0
  • // loop through all the pixels
  • while(index
  • // get the current pixel
  • pixelObj pixelArrayindex
  • // get the red value
  • int value pixelObj.getBlue()
  • // decrease the red value
  • value value / 2
  • // set the pixel red
  • pixelObj.setBlue(value)
  • // increment the index
  • index index 1

9
Using Multiplication by 0.5
  • You could have also multiplied the blue value by
    0.5
  • value value 0.5
  • Change the line that divided by 2 and try to
    compile this
  • In the decreaseBlue() method

10
Casting to Solve Loss of Precision Error
  • It will compile if we tell the compiler we know
    about the possible loss of precision
  • And that it is intended
  • By using a cast to int
  • value (int) (value 0.5)
  • Notice that we cast the result of the
    multiplication back to an integer
  • Casting is forcing a value into a type
  • (type) expression

11
Move Declarations Outside Loops
  • When you need a variable in a loop it is best to
    declare it before the loop
  • Otherwise you are declaring a new variable each
    time through the loop
  • Which is slower than just changing the value
    associated with the variable
  • In Java you can declare variables anywhere in a
    method
  • As long as you declare them before you use them

12
A new problem
  • Create a new method
  • public void decreaseBlue (int percentage)
  • We parameterize how much to decrease the blue by
  • Note Java allows us to have two methods of the
    same name, as long as they have different numbers
    of parameters, or different parameter types
  • If the user wishes to decrease the blue by 20,
    they can specify
  • mypicture.decreaseBlue(20)

13
What should the formula look like?
  • What is the matter with
  • blueValue pixelcount.getBlue()
  • blueValue blueValue ((100-percentage)/100)
  • Pixelcount.setBlue(blueValue)

14
Exercise
  • Create a similar method called
  • public void decreaseBlue (int percentage)
  • Now create a method called sunset()
  • This method should decrease the blue and the
    green by 30 percent, and should call the
    decreaseBlue and decreaseGreen methods

15
Another problem
  • Lets convert a picture to Grayscale
  • The book has a complex formula for converting to
    grayscale
  • A simple formula is to set the amount of red,
    green and blue to each be
  • (red green blue) / 3

16
Assignment
  • Review Media Computation Chapter 4
Write a Comment
User Comments (0)
About PowerShow.com