Raster Images

Raster Image Processing

Download the code below, and execute it. Examine the script, focusing on each of the component parts.

Click to Download: RasterStarter

Basically, it functions as follows.

For each pixel of the image:

  1. Each of the three color channels are extracted and stored in variables named r, g, and b.
  2. A new color value is created using these very same r, g, and b values.
  3. The old color value for the pixel is replaced by the newly created color.

The resulting image appears exactly the same as the original image. But what if the new color value created in Step 2 were to use altered values for r, g, and/or b? How might that affect the appearance of the resulting image?

Strategies for Manipulating Raster Images

To practice using Processing to manipulate pixels in an image—as well as RGB and arrays—work through the following tasks. By manipulating the program, you can manipulate the image representation programmatically. Try each of the following strategies. This list is by no means exhaustive, but it should give you a good foundation for exploration. It is important to spend time working through each of these tasks.

  1. Use one of the colors (r, g, or b) to define all three of the color channels in the new color.

    Example: color newColor = color(g, g, g);
    What is the result? Why?

  2. Keeping one of the values the same, set the other two to zero.

    Example: color newColor = color(0, g, 0);
  3. Swap the colors with one another. In other words, you might use r for the blue channel, b for the green channel, etc.

  4. Divide each of the color values by 2.

  5. Now, try multiplying one of the color values by 2.

  6. As mentioned previously, Processing uses 8 bits for each color channel, giving a usable range of 0–255 for the separate red, green, and blue channels. What do you think would happen if a value of 0 in the old image were mapped to a value of 255 in the new—and vice versa? Try it. Replace each of color channels r, g, and b with their complements, (i.e., (255 - r), (255 - g), and (255 - b)). What happens? Why?

Pixel Order

Manipulating each pixel achieves interesting effects, but we can also work with the entire array of pixels as well. Try these two simple examples:

  1. Instead of updating the color each time a new pixel is loaded, update the color only once every 20 times a pixel is loaded. HINT: Change the r, g, and b variables only when location is a multiple of 20.

    Example:
    if (location % 20 == 0) 
    { 
       update the variables 
    }
    
  2. The current loop begins at 0 and goes all the way through the array of pixels in order. The last pixel location is pixels.length - 1. Replace the line storing the pixel color at location with:

    pixels[pixels.length - 1 - location] = newColor;

Does the result surprise you? If it does, review the Swaps assignment from Unit 3: Data Representation.