Text

 


                           Convert to ppm (pnm) image format:

Since the program 'xv' is not available in our unix enivonment anymore, you will
have to use other programs to convert an image to ppm/pnm format. In our cis
unix environment, you can use the program 'gimp' to do the job.

How to convert to ppm/pnm using gimp
 

Find a machine in our lab, type 'gimp'.  If you never use gimp before, you will need to
answer a few questions to install the program.

Go to the main menu, select File -> Open to open your imag.

Now move the mouse to the image window, click the right mouse button, select 'File' -> 'Save as',
and then choose 'PNM' in the 'Save Option' panel. This will allow you to convert and
save the input image to a PNM (ppm) file.

Resize your image

To resize your image (so that you will satisfy the power of 2 requirement in OpenGL),  move
your mouse cursor to the image window, click the right mouse button, choose 'Image' ->
'Scale Image', and then input the desired pixel width and height to resize the image.
 
                                             Read ppm (pnm) images:

If you have no idea how to read image files, here are some information for

reading ppm files. The ppm file

format is one of the simplest image format because it has no compression and

it is defined in plain ascii. A ppm file will look like:

p3
256 128
255
100 100 255 200 100 164 ....

here p3 is a header of ppm file, don't worry about it. Then the following two
integers are the width and height of the image, in this example, width = 256 and
height = 128. The next integer is the maximum value of the color component,
255 in this example. This  means the range of value for each component is
from 0 to 255 (one byte per component). Next , you will see width*height*3
unsigned char. The first three numbers define R, G, and B for the first
pixel at the upper left corner of your image, and then the next three numbers
define the color of the next pixel to the right in the same scan line ... and so on.
After width* 3 of numbers, pixels of the next scanline (toward to
the bottom of the image) are defined... and so on.

Note: ppm file may have comments. A comment line starts with a '#' sign. For instance,
you can have a file like:

p3
256 128
# I have no comments
255
100 100 255 200 100 164 ....

Following is a simple code for reading a ppm file into an array, assuming no comment lines
exist. For your lab4, you need to remove the comment lines (start with #) of the ppm file if you
want to can use the following code.

/////////////////////////////////////////////////////////////////////////////////////////////

unsigned char readImage[256][256][3];  // just a hack. A better way is to
                                                             // define a dynamic array

void read_Image()
{
  FILE* in = fopen("sample.ppm", "r");

  int height, width, ccv;
  char header[100];
  fscanf(in, "%s %d %d %d", header, &width, &height, &ccv);  // assuming the width and height are 256 and 256 here ...

  int r, g, b;

  for (int i=height-1; i>=0; i--)
     for (int j=0; j<width; j++)
{
      fscanf(in, "%d %d %d", &r, &g, &b);
      readImage[i][j][0] = (GLubyte)r;
      readImage[i][j][1] = (GLubyte)g;
      readImage[i][j][2] = (GLubyte )b;
}
}


After you read the above texture file, you will then use glTexImage2D() to load the texture such as


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, readImage);


////////////////////////////////////////////////////////////////////////////////////////////
 

Two sample ppm images can be found in the cis unix server:

/usr/class/graphics/694g/buck.ppm and qb.ppm
 

One last thing, if you read the red book you will know that OpenGL requies each
dimension of the input texture to be power of 2. I will tell you in class how to
deal with this.