<< Chapter < Page | Chapter >> Page > |
If the image will fit in the display window, the code in Listing 6 is executed to begin the process of copying the pixel colors from the input image to the display image.
Although the syntax is unconventional, the call to the loadImage method in Listing 2 instantiates an object of the class named PImage and stores the object's reference in an instance variable named img . That object contains the pixel data extracted from the image file.
An object of the PImage class provides several methods that can be used to manipulate the image. In addition, an object of the PImage class contains fields for the width and height of the image, as well as an array named pixels[] that contains the values for every pixel in the image.
The array containing the pixel data for the input image in this sketch can be accessed as img.pixels[] .
There is a similar pixel array that contains the pixel data for the output display window. That array can be accessed simply as pixels[] .
The data stored in the elements of these arrays is of the Processing primitive type color .
Although the pixels in an image can be thought of as a residing on a two-dimensional grid, the pixels are stored in these one-dimensional arrays row-by-row. In otherwords, the array element that is accessed as pixels[0] contains color data for the upper leftmost pixel in the image. The last element in the array contains color data for the bottom rightmost pixel in the image.
The Pr0120a class inherits method named red , green , and blue .. These methods expect to receive a single parameter of type color , and return the value of the corresponding RGB component as type float . By default, the color component values range between 0.0 and 255.0.
Every array object in Java has a property named length whose value is equal to the number of elements in the array. This value is very usefulin the conditional clause of loops that are used to traverse an entire array.
Listing 6 shows the beginning of a for loop that
This process is complicated somewhat by the fact that the actual output image may be wider than the input image. This is indicated by the white space on the rightside of Image 1 . In other words, it is necessary to take this into account when computing the index of the output array element that isto receive the color from an input array element.
The code that accomplishes that is shown in Listing 7 .
Notification Switch
Would you like to follow the 'The processing programming environment' conversation and receive update notifications?