header

Homework 11

In this assignment you will enable your image viewer application to load and display an image. Modify your program from Lab 11 as follows:

  1. Uncomment the directive to include the graphics.h header file.
  2. Implement the loadIndexFile function.
  3. Implement the loadImageFile function.
  4. Implement the read function in the image class.
  5. Implement the displayImage function in the image class.
  6. Implement the destructor in the image class.

File Submission

Because you will be using the graphics package in your program, you'll need to create a project for this assignment. Name the project "Image". Create a copy of your Lab11.cpp file named Hmwk11.cpp and add it to your Image project. Submit your completed assignment by sending me your project file and your Hmwk11.cpp program file as email attachments.

Project Data Files

Log on to the Asbury University network and navigate to the public folder (Q:\SEARLS). Copy all of the files in the "ImageData" folder to the folder that contains your image viewer application. The index files (*.IDX) are ordinary text files and can be opened using Quincy, Notepad, Wordpad, Word, or any other text editing application. The image files (*.BN#) are binary image files. If you try to open these in a text-editing application all you will see is gibberish. When you have successfully completed this assignment, your application will be able to open any of the seven index files and view the corresponding images.

The loadIndexFile Function

An index file contains information about a collection of related images. Index files were used with LandSat satellite images in the 1980's when multiple images of the same terrain were captured using different colored light filters (typically blue, green, red, and infrared). An index file is an ordinary text file.

The first line of the file contains the location of the terrain. (In some of our index files, this will serve as a description of the collection rather than a location.)

The second line contains four integer values that describe the position of the image within a broader context. The first two integers represent the range of x-coordinates for the pixels in the image (minimum x followed by maximum x). We will use these values to determine the width of the image (maximum x - minimum x + 1). The last two integers represent the range of y-coordinates for the pixels in the image (minimum y followed by maximum y). These values will be used to determine the height of the image.

The third line contains a single integer value that represents the number of images in the collection.

The remaining lines contain information about the images. There are two additional lines for each image in the collection; a description of the image and the name of the image file.

Here is an example of an index file:

Washington D.C. T.M. Scene - Nov 1982
2600 2919 3050 3324
4
Band 1: 0.45-0.52 microns (blue)
DC.BN1
Band 2: 0.52-0.60 microns (green)
DC.BN2
Band 3: 0.63-0.69 microns (red)
DC.BN3
Band 4: 0.76-0.90 microns (infrared)
DC.BN4

The images in this collection are of Washington D.C. and were taken in November of 1982. The images are all 320 (2919 - 2600 + 1) pixels wide and 275  (3324 - 3050 + 1) pixels high. There are four images in this collection. The description of the first image is "Band 1: 0.45 - 0.52 microns (blue)". A micron is a micrometer (a millionth of a meter) and, in this context, is used to measure the wavelength of light. The first image was taken through a blue filter that allowed light with wavelengths of 0.45 to 0.52 microns through. Today, the wavelength of light is more commonly expressed in nanometers (billionths of a meter). The first image was created using light with wavelengths of 450 to 520 nanometers. Finally, the name of the binary data file for the first image is "DC.BN1".

The information in an index file is used to fill the global variable "myIndex" which is declared as an indexType structure. Notice that the image file data (the descriptions and the file names) are stored as an array of indexEntry structures.

The constant, IDX_FILE, is an array of strings in which each string is the name of an index file. The loadIndexFile function presents this list of file names to the user as a menu, asks the user to make a selection, and then opens and reads the selected index file. If the user makes an invalid selection, an appropriate error message is displayed and the function terminates. The sample run that follows shows what happens when the user enters the command to load an index file:

Image Viewer

This is a command line interface. To see a list of the available
commands enter the "help" command.

> li

Available Index Files:

    1) BEARLAKE.IDX
    2) DC.IDX
    3) DULLES.IDX
    4) GLACIERPARK.IDX
    5) OHARE_76.IDX
    6) WESTPT.IDX
    7) MISC.IDX

Your choice: 8

You made an invalid choice.

> li

Available Index Files:

    1) BEARLAKE.IDX
    2) DC.IDX
    3) DULLES.IDX
    4) GLACIERPARK.IDX
    5) OHARE_76.IDX
    6) WESTPT.IDX
    7) MISC.IDX

Your choice: 2

The DC.IDX file is now in memory.

>

The loadImageFile Function

The purpose of the loadImageFile function is to load one of the images listed in the current index into memory. If no index file has been loaded into memory, this function should generate an appropriate error message and terminate. (If the location field of "myIndex" is the empty string, then no index file has been loaded into memory.)

This function displays a list of the available files (as listed in the current index) as a menu, asks the user to make a selection, and invokes the read function of myImage (an imageType object) to actually read the file data. Here is a sample run:

Image Viewer

This is a command line interface. To see a list of the available
commands enter the "help" command.

> lf

You must first load an index file.

> li

Available Index Files:

    1) BEARLAKE.IDX
    2) DC.IDX
    3) DULLES.IDX
    4) GLACIERPARK.IDX
    5) OHARE_76.IDX
    6) WESTPT.IDX
    7) MISC.IDX

Your choice: 2

The DC.IDX file is now in memory.

> lf

Washington D.C. T.M. Scene - Nov 1982:

    1) Band 1: 0.45-0.52 microns (blue)
    2) Band 2: 0.52-0.60 microns (green)
    3) Band 3: 0.63-0.69 microns (red)
    4) Band 4: 0.76-0.90 microns (infrared)

Your choice: 5

You made an invalid choice.

> lf

Washington D.C. T.M. Scene - Nov 1982:

    1) Band 1: 0.45-0.52 microns (blue)
    2) Band 2: 0.52-0.60 microns (green)
    3) Band 3: 0.63-0.69 microns (red)
    4) Band 4: 0.76-0.90 microns (infrared)

Your choice: 1

>

No message is needed to tell the user that the selected file has been loaded into memory because it will be displayed in a graphics window.

The read function returns a boolean value indicating whether the read operation was successful or not. If not, the loadImageFile function should display a message indicating that no image was loaded into memory.

The imageType read Function

The read function of the imageType class performs these basic tasks:

  1. Allocate memory for the image data.
  2. Read the image data into the allocated memory.
  3. Create a new graphics window.
  4. Display the image in the graphics window (by invoking the private displayImage function).

If there is a file failure in opening the file or in reading the file, an appropriate error message should be displayed and the function should terminate (and the memory that was allocated for the image returned to the operating system).

If the application has already read an image file, don't forget to return the memory allocated for that image back to the operating system before processing the current request.

The imageType displayImage Function

This private function displays the current image in the current graphics window. Conceptually, the following logic is applied to each pixel in the image (where intensity is an integer type variable):

intensity = int(image[y*width + x]);
putpixel(x, y, COLOR(intensity, intensity, intensity));

The putpixel function in the graphics package sets the pixel at the specified location to the specified color. The COLOR function requires three integer parameters: red intensity, green intensity, and blue intensity. Setting all three to the same value results in the appropriate shade of gray.

The imageType Destructor

The destructor for the imageType class returns the memory allocated for the image back to the operating system. Keep in mind that if the read function of the imageType class has never been invoked or if the most recent read operation failed, there will be no memory allocated for the image and no memory to return to the operating system. When an imageType object is destroyed, any graphic windows associated with the object should be closed.