CECS 301 Digital Image Compression

MCVL Homepage Department of Computer Engineering and Computer Science 

Instructor:

Xinhua Zhuang
313 EBW
Department of Computer Engineering and Computer Science
University of Missouri-Columbia
Columbia, MO 65211
Phone: 882-2382
E-mail: zhuang@cecs.missouri.edu
Office Hours: TBA


Project One

PGM Format  

PGM (Portable GreyMap) formats is a convenient (simple) method of saving image data, it is equally easy to read in ones own applications. This format was popularized by the pbmplus image toolkit otherwise known as the "enhanced portable bitmap toolkit". 

A PGM file consists of two parts, a header and the image data. The header consists of at least three parts normally delinineated by carriage returns and/or linefeeds. The first "line" is a magic PGM identifier, it can be "P2" or "P5" (not including the double quotes!) these correspond to the ASCII and binary form of the data respectively. The next line consists of the width and height of the image as ASCII numbers. The last part of the header gives the maximum value of the color components for the pixels, this allows the format to describe more than single byte (0..255) color values. In addition to the above required lines, a comment can be placed anywhere with a "#" character, the comment extends to the end of the line. 

An example of a PGM file of type "P2" is given below 

P2

24 7

15

0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0

0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0

0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0

0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0

0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0

0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

 

It represents a grayscale image 24x7 and 4 bits quantization (16 grayscale level, usually 16-1 will appear on the third line)

 


Test Images

  In this project, the test image is PGM format, lenna.pgm (256x256x4bits). As you can imagine, we can have a PGM file like below:

P2

256 256

15

0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 .....................

0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0......................

......................................................................................................

......................................................................................................

  Although now each pixel is quantized by four pixels, each pixel still occupies 1 byte in PGM files (I do not know why, all I can say is that it is a fact). So if you want to read an image into memory to do further processing, no matter how many bits are used to do quantization, read one byte for one pixel.


Image I/O Functions

  In order to do image processing, you need read images from an image file into memory, write the results from memory to an image file on the hard disk. Four image I/O functions, ReadPGMImage(), WritePGMImage(), NewImage(), and FreeImage(), are provided. A predefined structure Image is involved in all of these functions. 

typedef struct

{

  unsigned int cols;           // col # of image

  unsigned int rows;           // rows # of image

  unsigned char **ptr;         // 2-D array for image pixels

} Bit8MonchromeImage;

 

typedef Bit8MonchromeImage  Image;

 

**********************************************************************

NAME

         ReadPGMImage() – read a pgm image into memory 

**********************************************************************

  SYNOPSIS

        ReadPGMImage(char *fp)

 DESCRIPTION

    ReadPGMImage() opens a pgm file name fp, reads the content into a Image structure. 

RETURN VALUE

The call returns an Image structure point on success, or NULL if an error occurred. 

 

**********************************************************************

   NAME

         WritePGMImage() – write a pgm image in memory to a file 

**********************************************************************

SYNOPSIS

        WritePGMImage(Image *Img, char *pf, unsigned int bit_num)

 

DESCRIPTION

    WritePGMImage() writes an image stored in an Image structure to a file named fp, bit_num is the quantized bit number. 

RETURN VALUE

        N/A

 

*********************************************************************

   NAME

         NewImage() – generate an Image strucutre in memory 

**********************************************************************

SYNOPSIS

        NewImage(unsigned int rows, unsigned int cols)

 DESCRIPTION

    NewImage() generate an Image structure in memory. 

RETURN VALUE

The call returns an Image structure point on success, or NULL if an error occurred. 

 

**********************************************************************

   NAME

         FreeImage() – free Image structure after processing 

**********************************************************************

SYNOPSIS

        FreeImage(Image *Img)

DESCRIPTION

    FreeImage() free Image structure from memory after processing. 

RETURN VALUE

N/A

 

 

Below is an example showing how to use these four functions to implement 8-bit-to-4-bit conversion:

#include "Image.h" //head file define functions and data structures

void main()

{

Image *SourceImg;

unsigned int i,j;

unsigned int cols,rows;

Item **ptr;

    

SourceImg=ReadPGMImage("lenna.pgm");

    

rows=SourceImg->rows;

cols=SourceImg->cols;

    

ptr=SourceImg->ptr;

     

for (i=0; i<rows; i++)

for (j=0; j<cols; j++)

    ptr[i][j]=(ptr[i][j]>>4) & 0x0f; 

    

        WritePGMImage(SourceImg,"lenna4.pgm",4);

    

FreeImage(SourceImg); 

 

system("PAUSE"); 

  It is easy, isn’t it?


Download File 

Lenna4.pgm (256x256 grayscale image using 4 bit for quantization)

Image.h: Head file for basic image I/O function
ReadPGMImage.cpp: contains ReadPGMImage() function
WritePGMImage.cpp: contians WritePGMImage() function
NewImage.cpp: contains NewImage() function
FreeImage.cpp: contains FreeImage() function

Quantization.cpp: Implement the conversion from a 8-bit grayscale image to 4-bit grayscale image.

 

 

        

 

 

If you find any error in these new lecture notes, please contact Jianhong Wang.

 

[Home]CECS Multimedia Communications and Visualization Laboratory