Java Files for processing images (bmp, jpeg, gif)

Written January 2001, updated 2003 by Dorothea Blostein
February 2006, new version of utils.java and BMPFile.java by Aubry Yves

This sample image-manipulation program consists of five .java files:

imageMain.java
imageIO.java
iObserver.java
utils.java
BMPFile.java

This sample program reads in an image (gif, jpeg or bmp format; the user supplies the file name). Then it modifies the image, according to selections made by the user. (The user can apply a green filter to the top half, apply a fading green filter across the whole image, add vertical black bars to the image, convert to a grayscale image, or save as a bmp file.) Look at file imageMain.java to see the code that implements each of these operations.

This sample program was originally used for CISC124 (fall 2000), but can be used by anyone wanting to write image manipulation in Java.

To change the image manipulations that are performed, only one file needs to be modified: mageMain.java. The other .java files provide the image I/O facilities used by imageMain.java. Many thanks to Jeb Thorley for providing the image I/O code.

In order to keep imageMain simple, it does not display images. When you run imageProgram, you need to use a separate image-display program, so that you can see the effect of the image modifications made by imageMain. If you are running Windows, use paint. Select start | program | acessories | paint and then use file open. If you are running under another platform, Java version 1.2 or later, you can use the Grave image viewer (written by Jeb Thorley): Grave.java FileMenuControl.java utils.java. Run Grave in the background, before starting imageMain. That way you can view images as soon as you save them under imageMain. In unix, the command to start Grave in the background is "java Grave &".

Another version of the code, using an array of abstract classes

Here is another version of the image-operation program. In this version, the image operations are stored in an array. The image program consists of five .java files:

Image Rotation code

Here is sample code for rotating an image.

The image program (with rotation operation) consists of five .java files:

If you want to experiment with this code, here are suggestions for extensions and improvements that you could make.

(1) Change the center of rotation to be in the middle of the image. In the current code, the center of rotation is the bottom left of the image. In the current code, the (0,0) point in the (x, y) coordinate system is located at the bottom left of the iamge. Here's all you have to do in order to move the center of rotation to the middle of the image.

    - After a (row, col) coordinate has been changed to (x, y):
      subtract MAXROWS/2 from x and subtract MAXCOLS/2 from y.
      This moves the (0,0) point of the coordinate system to the center of the image.
    - When you have computed newx, newy and before you convert back to
      row, col coordinates, you need to add MAXROWS/2 to x and add MAXCOLS/2 to y.
(2) Don't just copy pixels, but compute a weighted average of pixels. This will yield a result image with better image quality(less choppy looking). This applies to the Version 2 code. This code starts with a newrow, newcol location in the new image. It computes an oldx, oldy location in the old image where the data should come from. Both oldx and oldy are floating point numbers. If they land about halfway between two pixels in the old image, you should compute the average value of those two pixels. In general, you could compute the weighted average of 4 pixels. For example, if oldx and oldy are (14.8, 33.4) then you compute a weighted average of pixels [14][33] and [14][34] and [15][33] and [15][34]. Compute the distance from (14.8, 33.4) to each of these four pixels to compute the weights.