An Image Editing Program: Source code

In this project we have implement an image editing program that allows you to load in one or more images and perform various operations on them. Consider it to be a miniature Photoshop or a reasonably functional copy of XV.

Things on this page may be modified as circumstances warrant it, so check back frequently for changes.

The operations are summarized here, with details on implementing them below.

Details on Things to Implement

Things in bold are category headings. The comments associated with each category apply to all the sub-operations. For instance, the comments associated with Filtering apply to all of the filtering operations.
Operation Keyword Arguments Details
Load load filename Load the specified image file and make it the current image.
Save save filename Save the current image to the specified file.
Difference diff filename Subtract the given image file from the current image and put the result in the current image.
Run run filename Executes the script named filename. The script should contain a sequence of other commands for the program, one per line. The script must end with a newline.
Color to Grayscale gray Use the formula I = 0.299r + 0.587g + 0.114b to convert color images to grayscale. This will be a key pre-requisite for many other operations. This operation should not affect alpha in any way.
24 to 8 bit Color All of these operations assume that the current image has 24 bits of color information. They should still produce 24 bit images, but there should only be 256 different colors in the resulting image (so the image could be stored as an 8 bit indexed color image). Don't be concerned with what happens if you run these operations on something that is already quantized. These operations should not affect alpha.
Uniform Quantization quant-unif Use the uniform quantization algorithm to convert the current image from a 24 bit color image to an 8 bit color image. Use 4 shades of blue, 8 shades of red, and 8 shades of green in the quantized image.
Populosity quant-pop Use the populosity algorithm to convert the current 24 bit color image to an 8 bit color image. Before building the color usage histogram, do a uniform quantization step down to 32 shades of each color. Then find the 256 most popular colors, then map the original colors onto their closest chosen color. To find the closest color, use the euclidean (L2) distance in RGB space. If (r1,g1,b1) and (r2,g2,b2) are the colors, use sqrt((r1-r2)^2 + (g1-g2)^2 + (b1-b2)^2) suitably converted into C++ code.
Dithering All of these operations should convert the current image into an image that only contains black and white pixels. If the current image is color, you should automatically convert it to grayscale first (in fact, you could convert all images to grayscale - it won't hurt already gray images). These operations should all threshold dither the alpha channel, regardless of what they do to the gray channel. That is, if the alpha channel is less than 0.5, it goes to 0, otherwise it goes to 1.0.
Naive Threshold Dithering dither-thresh Dither an image to black and white using threshold dithering with a threshold of 0.5.
Brightness Preserving Threshold Dithering dither-bright Dither an image to black and white using threshold dithering with a threshold chosen to keep the average brightness constant.
Random Dithering dither-rand Dither an image to black and white using random dithering. Add random values chosen uniformly from the range [-0.2,0.2], assuming that the input image intensity runs from 0 to 1 (scale appropriately). There is no easy way to match the reference program with this method, so do not try. Use either a threshold of 0.5 or the brightness preserving threshold.
Ordered Dithering dither-order Dither an image to black and white using ordered dithering with the matrix shown below. The image pixels should be compared to a threshold that depends on the dither matrix below. The pixel should be drawn white if: I[x][y] >= mask[x%4][y%4]. The matrix is:
    0.1250 1.0000 0.1875 0.8125
    0.6250 0.3750 0.6875 0.4375
    0.2500 0.8750 0.0625 0.9375
    0.7500 0.5000 0.5625 0.3125
    
Clustered Dithering dither-cluster Dither an image to black and white using cluster dithering with the matrix shown below. The image pixels should be compared to a threshold that depends on the dither matrix below. The pixel should be drawn white if: I[x][y] >= mask[x%4][y%4]. The matrix is:
    0.7500 0.3750 0.6250 0.2500
    0.0625 1.0000 0.8750 0.4375
    0.5000 0.8125 0.9375 0.1250
    0.1875 0.5625 0.3125 0.6875
    
If you do this one, but not ordered dithering, then you will get 10 points for it.
Floyd-Steinberg Dithering dither-fs Dither an image to black and white using Floyd-Steinberg dithering as described in class. (Distribution of error to four neighbors and zig-zag ordering).
Filtering All of these operations should modify the current image, and assume color images. The alpha channel should NOT be filtered. The alpha channel for all the test images will be 1 for all pixels, so you do not need to worry about the differences between filtering regular pixels or pre-multiplied pixels. Implement whichever approach you prefer.
Box Filter filter-box Apply a 5x5 box filter.
Bartlett Filter filter-bartlett Apply a 5x5 Bartlett filter.
Gaussian Filter filter-gauss Apply a 5x5 Gaussian filter.
Edge Detect (High-Pass) filter-edge Apply a 5x5 edge detect filter derived from a Gaussian as indicated in the lectures. (Note that the lecture notes derive the edge detect filter from a Bartlett, so the matrix used in this operation should not be identical). Clamp pixel values that fall outside the range 0-255.
Edge Enhance filter-enhance Apply a 5x5 edge enhancement operator, using a Gaussian filter as the underlying smoothing filter. Use the method shown in class to come up with a single filter that does the enhancement in one pass, or use image subtraction operations if you prefer. You should clamp pixel values that fall outside the range 0-255.
Arbitrary-Size Gaussian Filter filter-gauss-n N (size) Apply an NxN Gaussian filter (necessary to implement NPR-Paint).
Image Resizing All of these functions should change the size of the current image by the appropriate amount. They should also operate on the alpha channel.
Half Size half Halve the image size, using a 5x5 Bartlett filter to perform the smoothing.
Double Size double Double the image size, using a 5x5 Bartlett filter to compute the intermediate pixel values.
Arbitrary Uniform Scale scale amount Scale the image up or down by the given multiplicative factor. By uniform scaling I mean scale the x and y axes by the same amount, so the aspect ratio does not change. Use Bartlett filters for the reconstruction. The filter size should vary so that you always pick up at least nine (three by three) values from the input image in constructing each pixel of the output image.
Compositing image All of these operations should composite the current image, A, with the specified image, B, using A op B, where op is one of the operations below. The result should replace the current image.
Over comp-over image See class notes from Feb 14
Inside comp-in image
Outside comp-out image
Atop comp-atop image
Xor comp-xor image
Misc
Arbitrary Rotation rotate amount Rotate the image clockwise by the given amount, specified in degrees. The output image should be the same size as the imput image, with black pixels where there is no input image data. Use Bartlett filters for the reconstruction. The filter size should vary so that you always pick up at least nine (three by three) values from the input image in constructing each pixel of the output image.
Blue Screen blue-screen Extract an alpha image from the current image using the blue-screen method. The current image should be replaced by one that has zero alpha where the original image is close to blue.
NPR Paint npr-paint

Apply a simplified version of Aaron Hertzmann's painterly rendering algorithm from the 1998 SIGGRAPH Paper Painterly Rendering with Curved Brush Strokes of Multiple Sizes. You need only implement the multiple (circular) brush size version from section 2.1 of this paper. A function to do the actual drawing of the circular strokes (TargaImage::Paint_Stroke) has been provided for you.

To match the reference solution (which is what you're graded on), your implementation should use the brush size radii of 15, 7 and 2. Remember that when calling the Gaussian-blur function, the filter size is

2 × radius + 1

The fg parameter should be set to 0.25, and the threshold parameter T should be set to 50.

The difference function in Hertzmann's pseudo-code is really Euclidean distance (as specified in the text below the paintLayer figure), so you'll need to compute and store these distances on a per-pixel basis.

horizontal rule