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.