Advertisement

LibPNG problem with OpenGL

Started by April 23, 2003 02:24 PM
4 comments, last by ProfEich 21 years, 10 months ago
I created 3 programs without OpenGL and used the same function to read a PNG file. (using LibPNG) But if I copy this function into my OpenGL project it breaks down at this line: png_read_info(png_ptr, info_ptr); Is there maybe an example how to use/read PNG files for OpenGL somewhere?
What is it doing as far as "breaking down?" Is it a compile error, or a run-time error? Some other surrounding code would be nice, as well as a description of what you''re hoping to do "in the end."


- sighuh?
- sighuh?
Advertisement
It's a free code as well - so here it is:

int x, y;

int width, height;
png_byte color_type;
png_byte bit_depth;

png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;

bool read_png_file(char* file_name)
{
char header[8]; // 8 is the maximum size that can be checked

/* open file and test for it being a png */
FILE *fp = fopen(file_name, "rb");
if (!fp) return false;

fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8)) return false;

/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

if (!png_ptr) return false;

info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) return false;

if (setjmp(png_jmpbuf(png_ptr))) return false;

png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);

png_read_info(png_ptr, info_ptr);

width = info_ptr->width;
height = info_ptr->height;
color_type = info_ptr->color_type;
bit_depth = info_ptr->bit_depth;

number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);

/* read file */
if (setjmp(png_jmpbuf(png_ptr))) return false;

row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);

png_read_image(png_ptr, row_pointers);

return true;
}



---------------
Break down means there comes an error message - "Zugriffsverletzung" in german ~ Google translation says "access injury". <.<

[edited by - ProfEich on April 23, 2003 5:28:41 PM]
I never got the "easy" png_read_image() function to work, it always segfaults/access violates or however you might call it. You can add some lines and do it "the hard way" as the documentation says, it's not really that hard and gives you some additional flexibility if you ever need it.

Here's my PNG loader code (without error handling):


      png_byte header[8];  int flags;  png_bytep* row_pointers;  FILE *fp = fopen( fn, "rb");  // check if this is a PNG file  fread( (char*)header, 1, 8, fp );  if ( png_sig_cmp(header, 0, 8) ) {    throw "Not a PNG file";  }    png_structp pngfile = png_create_read_struct(    PNG_LIBPNG_VER_STRING,    NULL, NULL, NULL );  png_infop pnginfo = png_create_info_struct( pngfile );  png_infop pngendinfo = png_create_info_struct( pngfile );  // In case of an error, we want to die here!  if( setjmp( png_jmpbuf(pngfile) ) ) {    png_destroy_read_struct(&pngfile, &pnginfo, &pngendinfo);    fclose(fp);    throw "Error! longjmp() called!";  }  png_init_io( pngfile, fp );  png_set_sig_bytes( pngfile, 8 ); // we read 8 bytes to verify    flags = PNG_TRANSFORM_STRIP_16|PNG_TRANSFORM_PACKING;  // you can set additional transforms here to convert unuseable formats as you need it    // "the hard way"  png_read_png( pngfile, pnginfo, flags, NULL );    // get pointers to each row of the image  row_pointers = png_get_rows( pngfile, pnginfo );  // read the image into the target buffer. sucks but works, could stand a little reworking  if( PNGNeedFree ) free( Textures[l] );  Textures[l] = (GLubyte*)malloc( pnginfo->rowbytes * pnginfo->height * sizeof(GLubyte) );  PNGNeedFree[l] = true;  for( int i=0; i<pnginfo->height; i++ ) {    memcpy( &(Textures[l][i*pnginfo->rowbytes]), row_pointers[i], pnginfo->rowbytes );  }  // this will clear all memory used other than the stored texture  png_destroy_read_struct( &pngfile, &pnginfo, &pngendinfo );  fclose( fp );  


(Edit: Some outdated comment slipped in)

[edited by - Shadowdancer on April 23, 2003 5:49:13 PM]
Thx - but it doesn''t work at all =(

Now I''ve the same problem in this line:
png_read_png( pngfile, pnginfo, flags, NULL );

If I copy it in other projects it works...

Maybe there''s a problem with the FMOD.DLL or something like that =(
FMOD? If libPNG is not working, why are you searching for bugs in FMOD?

I had a similar problem with ogg vorbis codec some time ago. Maybe you are not linking with the correct library. (debug/release/single/multithreaded).

If I can remember well, my program was crashing on ov_test_open or another open-like call. After trying another lib/dll, I was able to run correctly.
Maybe a similar problem applies here.

Previously "Krohm"

This topic is closed to new replies.

Advertisement