Archived

This topic is now archived and is closed to further replies.

LibPng problems

This topic is 5148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

A few weeks ago, as I was having trouble creating a PNG decoder, someone suggested that I should use LibPng, so I decided to give it a try this morning. I''m following the procedure mention in their document for the high level interface but everytime I call png_read_png I get an Unhandle Exception code. The code looks like this: ========================================= BOOL bResult; FILE* pFile = NULL; pFile = fopen(lpFileName,"rb"); BYTE* pBuffer = NULL; // Buffer use to load the data // Check Header pBuffer = new BYTE[8]; fread (pBuffer,1,8,pFile); bResult = png_sig_cmp (pBuffer,0,8); delete []pBuffer; png_structp pPngStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,NULL,NULL); png_infop pInfo_ptr = png_create_info_struct(pPngStruct); png_infop pEnd_info = png_create_info_struct(pPngStruct); png_init_io(pPngStruct, pFile); png_set_sig_bytes(pPngStruct, 8); png_read_png(pPngStruct, pInfo_ptr, PNG_TRANSFORM_IDENTITY, NULL); fclose (pFile); ========================================= (NOTE: I''ve remove the error checking codes but I check if all my pointers are NULL before calling png_read_png) I''ve tried a lot of stuff but I can''t seem to figure out why it''s not working, can anyone tell me what''s wrong with the code ? Do I absolutely need to add Row and Unknown Chunks callback functions ? Thanks Nick

Share this post


Link to post
Share on other sites
Try this, it is an excerpt from my own code, and it does work.


FILE* fp = fopen(filename.c_str(), "rb");

if(fp == NULL)
{
return; //error

}


png_structp png_ptr;
png_infop info_ptr;

png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);


info_ptr = png_create_info_struct(png_ptr);
if (setjmp(png_jmpbuf(png_ptr)))
{
/* Free all of the memory associated with the png_ptr and info_ptr */
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
/* If we get here, we had a problem reading the file */

fclose(fp);
return; //error

}
else
{
png_init_io(png_ptr, fp);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING, png_voidp_NULL);

// Now we need to extract the data from that was read


png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);

typedef unsigned char BYTE;
BYTE* pData = new BYTE[png_ptr->height * png_ptr->rowbytes];

for(int i = 0; i < png_ptr->height; ++i)
{
memcpy((pData + i * png_ptr->rowbytes), row_pointers[i], png_ptr->rowbytes);
}

/* clean up after the read, and free any memory allocated - REQUIRED */
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
}


Oh and after the code is done, pData contains the raw pixel data.

[edited by - duke on November 7, 2003 2:57:24 PM]

Share this post


Link to post
Share on other sites
It is always possible you are not using the right version of the lib

Are you using libpng as a DLL? I statically link it, not sure if it would make a difference, it probably shouldn''t.

What exactly doesn''t work? Does the code crash? or what? Does it even compile? Have you tried writing the pData buffer straight to disk and loading into something like photoshop as a raw file?

Either way, give a few more specifics about how it is working and I might can help.

Jeff

Share this post


Link to post
Share on other sites