PDA

View Full Version : png Programme



vivek vyas
07-06-2009, 03:44 PM
i am working on OpenGL programme
to load a PNG image on square plate

1st:i worked on console programme to test a png image of validity by signature test be png file which passed

2nd:this will more good then 1st it not only test but also give information about image file like

Dimension:
bit-depth:
compression value:
color type RGB/RGBA:

3rd:it is final one but here i got message

./pngtest xxxx segmentation fault

and i give code and part which pressence affect programme

The code to load png:

str = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL, NULL,NULL);
if(!pstr)
{printf("can't create read struct");
exit(0);
}
pinfo = png_create_info_struct(pstr);
if(!pinfo)
{png_destroy_read_struct(&pstr,&pinfo,NULL);
printf("info struct creation failed");
exit(0);
}
if(setjmp(pstr->jmpbuf))
{printf("Something goes wrong\n");
png_destroy_read_struct(&pstr,&pinfo,NULL);
exit(0);
}
png_init_io(pstr,fp);
png_set_sig_bytes(pstr, 8);
png_read_info(pstr, pinfo);
printf("image dimension %i x %i\n",pinfo->width,pinfo->height);
if(pinfo->color_type = 2){flag = GL_RGB;
}
else if(pinfo->color_type = 4){flag = GL_RGBA;
}
else {
exit(0);
}
data = (unsigned char *)malloc(pinfo->rowbytes*pinfo->height);
png_destroy_read_struct(&pstr,&pinfo,NULL);
exit(0);
}

for(int i = 0;i < pstr->height;++i)
pinfo->row_pointers[i] = data + i*pinfo->rowbytes;
png_read_image(pstr,pinfo->row_pointers);
png_read_end(pstr,NULL);

and code fraction presence which affect:

for(int i = 0;i < pstr->height;++i)
pinfo->row_pointers[i] = data + i*pinfo->rowbytes;
png_read_image(pstr,pinfo->row_pointers);
png_read_end(pstr,NULL);

if code fraction note there then whole programme work but object not wrapped up

if it present then that of sagmentation fault
comes on output window

lil pocced of pro:
open file
signature check
read struct job
info struct job
png file io job
signature setting job
initialising unsigned char * data via file's
component
and placing data at end corner
of
gluBuild2DMipmaps()

bmateusz
07-06-2009, 05:07 PM
It's easier to use a library for texture loading.
e.g.
SOIL: http://www.lonesock.net/soil.html
DevIL: http://openil.sourceforge.net/
etc..