PDA

View Full Version : PPM file: I have no color and a skewed image



Aeneas117
01-03-2010, 08:55 AM
Hello everyone,

I've got a problem. I'm trying to create a simple sliding puzzle. For the images I'm loading a ppm file. I'm using Visual C++ with OpenGL and GLUT. My idea was to store every pixel in a 3D array in the memory. I think I'm doing everything right, but when I try to redraw the image, it looks like it is grey-scale and some part has shifted.
The image I used is: http://www.area7.nl/OpenGL/spongebob_squarepants_3dc.jpg
but what I get is this: http://www.area7.nl/OpenGL/spongebob.jpg

the main code I use is:



void display(void)
{
GLfloat p[2];

glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);

int i = 0;
int j = 0;
int k = 0;
int x = 0;
int y = 0;
int o = 0;
int r = 0;
int MaxHeight;
int MaxWidth;
MaxHeight = 425;
MaxWidth = 640;

float ***pixels;
char word[64];
float number = 0;
float Width;
float Height;
float MaxVal;
float ColRed;
float ColGreen;
float ColBlue;

//images of ppm files will be placed in the map images so the program can take a random image for the puzzle
FILE * fp = fopen("images/spongebob.ppm", "r");

if(fscanf(fp, "%s", word) != EOF)
{
if(strcmp(word, "P3") == 0)
{
while(!isdigit(word[0])) //go on while there are no numbers we are interested in
{
fscanf(fp, "%s", word);
}
Width = atof(word);
fscanf(fp, "%f", &number);
Height = number;
fscanf(fp, "%f", &number);
MaxVal = number; //number where all the rgb-values are going to be devided with, so we get a value between 0 and 1

//Width = Width + 4; //if I actually do this, the image is shifted even more

//allocate a 3D Array for storing pixels
pixels = (float ***)malloc(sizeof(float **) * Width + 1);
for(i=0;i<=Width;i++)
{
pixels[i] = (float **)malloc(sizeof(float *) * Height + 1);
for(j=0;j<=Height;j++)
{
pixels[i][j] = (float *)malloc(sizeof(float) * 3); // allocate memory for every y of the x pixel
}
}

while (fscanf(fp, "%f", &amp;number) != EOF)
{
x = 0;
//put all the next numbers in a 3d array
while (x < Width)
{
//put current pixel in the array

pixels[x][y][0] = number;
fscanf(fp, "%f", &amp;number);
pixels[x][y][1] = number;
fscanf(fp, "%f", &amp;number);
pixels[x][y][2] = number;
fscanf(fp, "%f", &amp;number);

x++;
}
//done with this line, next
y++;
}

}
else
{
printf("This is not a ppm file, don't try to fool me");
exit(1);
}
}
else
{
printf("The file is empty, I can't make an image of that");
exit(1);
}

fclose(fp);
x = 0;
y = 0;

for(r = Height; r>0;r--)
{

x = 0;
for(o = 0; o<Width;o++)
{

ColRed = (pixels[o][r][0]) / 255.0;
ColGreen = (pixels[o][r][1]) / 255.0;
ColBlue = (pixels[o][r][2]) / 255.0;

glColor3f(ColRed, ColGreen, ColBlue);
p[0] = x; // x coord
p[1] = y; // y coord
glVertex2fv(p); // draw it

x++;
}
y++;
}

glEnd();


glFlush(); glutSwapBuffers();
}
As you can see, It is more C code than OpenGL commands. But I just can't figure out why the pixels dont't have the right color. If anyone could tell me where I can find my problem I would be very thankful

Thanks Aeneas

matchStickMan
01-03-2010, 11:52 PM
You've got a funny way of drawing ppm files. :P

Anyways if you want to fix your problem, just replace:

while (fscanf(fp, "%f", &amp;number) != EOF)

with

while( y < Height )


That should fix your problem.

Aeneas117
01-04-2010, 01:38 AM
Wow, Thank you so much matchStickMan, I did as you said and I changed:
pixels[x][y][0] = number;
fscanf(fp, "%f", &amp;number);
pixels[x][y][1] = number;
fscanf(fp, "%f", &amp;number);
pixels[x][y][2] = number;
fscanf(fp, "%f", &amp;number);

to this:

fscanf(fp, "%f", &amp;number);
pixels[x][y][0] = number;
fscanf(fp, "%f", &amp;number);
pixels[x][y][1] = number;
fscanf(fp, "%f", &amp;number);
pixels[x][y][2] = number;

and now it works perfectly.

Thank you so much