In-Class Exercise: Color to Greyscale Program
Conversion
You are to write a program that reads in a TIFF color image, displays it
and then converts it to a greyscale image and display the results. You
are to use the MIL library.
Solution
Program Steps:
1. Ask user for the name of the input TIFF file
2. Ask user for height and width of image and
store in variables: Image_Width,
Image_Height
3. Set up the MIL system so that you can use
the MIL display windows and other MIL routines as needed. READ the
following and also simply use the code provided.....you do not have to write
this portion yourself....but, you should given the description below and the
comments in the code and the MIL manual be able to understand each step.
See blue3.c prototype code or the following link. Involves a
number of MIL calls in the following order:
a) Initialize ...telling MIL you are going to start using
its routines and display information using MIL calls.
b) Next determine the size of the display
that will be needed to show the image.
c) Allocate a MIL buffer for each image
that you will be reading in or creating (say call this
MIL_buffer_I and MIL_buffer_I_out for our problem of
two images). This buffer is what will actually be displayed in a
MIL program window. Hence, for our case, we will need to do this
twice. Use the following MIL call:
d) Clear each MIL buffer you created in c) so
that it contains nothing at first.
e) OPTIONAL: You can now, initially tie the
MIL display to one of the MIL buffers...you will only see a black image
as we cleared the buffer in step d). We can make this ties
using:
f) Finally, in case hardward speedup via
OverlayKeying exists you can turn it on to make things run
faster. This is done with the following MIL call:
CODE_FOR_ALL_STEP3
4. Declare two unsigned char pointer, *I and *Iout,
where you will store the input image and the output image.
5. Allocate memory to the pointers using
malloc().
6. Open up file and read data into I using
MIL calls.
Hint: Use the two MIL calls: MbufImport() and
MbufGetColor(). See manual or prototype code blue3.c given
above.
7. Set up the MIL system to display this
image. This has been done already if you implemented Optional step 3
e).
8. Visit each pixel in the image and convert
the R,G,B values stored in I to a single greyscale value and store it
in Iout.
9. Dump the data in the array Iout into its
MIL buffer MIL_buffer_Iout created in step 3c). This
is because MIL has its own datastructures for images for the purpose of
displaying them and they are called MIL_buffers. You can make
this dump using the following MIL call:
10. Display the contents of Iout.
First you must take the display and deselect any previous MIL buffer tied to
it (either in step 3e or 7) using MdispDeselect() and then use
MdispSelect() to select the buffer MIL_buffer_Iout to be tied to
it...so its values get displayed.
11. Ask the user when wants to terminate
program.
12. Close down MIL system and deallocate any
MIL buffers created in 3c.