Image Processing Solution



Today’s digital images are composed of individual picture elements, call pixels. With a large number of pixels, the image can be clear. But, with a large number of pixels the image file would need a larger disk space for storage. Thus, most of the popular image file standards involve some compression scheme to reduce the storage overhead. In this lab, we’ll concentrate on one of the simplest color image format, portable pixmap format (PPM).

An image can be represented by a two-dimension array of pixels, the x-direction is the width and y-direction is the height of the image. In PPM, each pixel is represented by 3 bytes for red, green and blue colors. Thus, the total number of color levels is 224, 16M. The format of a PPM file is


  • H 255


Where W and H are two integers and 255 is the intensity level for each color components (R, G, and B). Since each color component R, G, B takes one byte, the intensity level is 255. After the header, the remaining file consists of 3N, N=W H, bytes, each byte representing the intensity of one of the color components. This file can be read as following: the first line read using a string of two characters, the second line read using “%d %d” for two integers, the third line read using a string of 3 characters, then the remaining file read using 3N characters. Note that the new-line character after the 3rd line should be handled carefully to ensure the image are read in correctly. Also, the pixels are arranged in column-major fashion, instead of row-major matrix storage use by C compiler.

In this lab, you will need to read in an image file, an EE Department logo and the NTHU logo. You will need to covert the original color image to a black-and-white image, add the EE Department logo to the lower right corner of the image and the NTHU logo to the center of the image. The EE department logo retains its colors, but the NTHU logo modifies the image to a purple tone.

To convert a color image to a black-and-white image, the following method has been popular. Let Rc, Gc and Bc be the intensities of a color pixel and Rg, Gg, Bg be the intensities of the gray-scale image, then

Rg = Gg = Bg = Rc 0:2126 + Gc 0:7152 + Bc 0:0722:

Note that when R = G = B at a pixel, this pixel has a gray color. And the levels of gray scale is 256. When R = G = B =255, the color is white and when R = G = B = 0, the pixel has the color black.

When placing EE Department logo, the pixels of the original image are simple replaced by the logo pixels when the logo pixel is not white. But in placing the NTHU logo, we simply set the red and blue components of the pixel to be 255 when the NTHU logo pixel is not white. This will make the logo purple since both blue and red color components are always on.

The data structure for the images should be as following:


struct sPIXEL {

// a single pixel

unsigned char r,g,b;

// three color components



struct sIMG {

// an image of PPM style

char header[3];

// header, either P3 or P6



// width and height of the image



// intensity level of each color component


// two-dimensional array for all the pixels

} IMG;

Your program should have the following functions:

  1. IMG *PPMin(char *inFile);

This function opens the inFile, reads the image data and returns a pointer pointing to the newly created image data structure.

  1. void *PPMout(IMG *p1,char *outFile);

This function writes the image pointed by p1 to the output file outFile.

  1. IMG *PPMcvt(IMG *p1,IMG *ee,IMG *nthu);

This function processes the image pointed by p1 adds the logos and returns the new image as a result.

One image file is available for you to test your program. It is pic1.ppm. The EE Department and the NTHU logos are also given: EE.ppm and NTHU.ppm. Since these ppm files take a large disk space, do not copy them to your own directory.

Your program needs to read an image file, EE Department log and NTHU logo files and produce an output file. Thus, the execution of your program should be invoked by the following command line

  • ./a.out ee231002/lab14/pic1.ppm ee231002/lab14/EE.ppm \ ee231002/lab14/NTHU.ppm pic1_out1.ppm

In order to read from a file, a FILE variable needs to be declared, the file opened and assigned to the file variable, then fscanf can be used to read and fprintf for write. After all data have been read and written, those file variables should be closed. The following example shows reading a integer from a file and write to data.out.

FILE *fin,*fout;

int k;







Of course, your can use fopen(argv[1],”r”); to open a file specified by the command line argument.


  1. Create a directory lab14 and use it as the working directory.

  1. Name your program source file as lab14.c.

  1. The first few lines of your program should be comments as the following.

/* EE231002 Lab16. Image Processing ID, Name



  1. After you finish verifying your program, you can submit your source code by

$ ee231002/bin/submit lab14 lab14.c

If you see a ”submitted successfully” message, then you are done. In case you want to check which file and at what time you submitted your labs, you can type in the following command:

$ ee231002/bin/subrec lab14

It will show submission records for lab14.

  1. You should try to write the program as efficient as possible. The format of your program should be compact and easy to understand. These are part of the grading criteria.