10.5.1.1. Exercises: QUndoCommand and Image Manipulation

Add some other undoable image manipulation operations to Example 10.11. Here are some ideas to try.

  1. Monochrome – Convert a three-color image to a monochrome image with a grayscale. Gray is produced by setting the all three color components to the same value. Unfortunately, if you simply replace each color value by the average of the three for all pixels in the image, the overall effect is to make the image seem too dark. The standard approach to producing an acceptable grayscale image is to correct for the fact that blue is considered to be a "darker" color than red. You can adjust each pixel by applying commonly used weight factors to each of the three colors[80]

    redVal *= 0.30; greenVal *= 0.59; blueVal *=0.11;

    Then you can compute the luminance of that pixel. Luminance (or intensity) is an int equal to the weighted average of the three color values. In this case, because you have already weighted them,

    luminance = redVal + greenVal + blueVal

    Finally, replace each of the three color values of the pixel with the luminance.

  2. Negative – Convert a three-color image to its negative. To do this, simply replace each color value v by 255 – v.

  3. Scramble colors – For each pixel, permute the color values so that the red value gets the original blue value, the green value gets the original red value, and the blue value gets the original green value.

  4. TriColor – For each pixel, compute its color intensity (average value of its three colors) ci. If ci is below 85, reduce its red and blue values to zero. If ci is 85 or higher, but below 170, reduce its blue and green values to zero. If ci is 170 or higher, reduce its red and green values to zero.

  5. Expose edges – For each pixel, compare its color insensity with that of the pixel below it. If the absolute value of the difference exceeds the threshold value (supplied as an argument), set its color to black (all three color values zero); otherwise set its color to white (all three color values 255).

[ fromfile: commandpattern.xml id: None ]



[80] For example, http://tinyurl.com/ydpjvgk discusses luminance.