"There is no dark side of the moon really,
As a matter of fact, its all dark."
// basic pseudo code
open file
read file into buffer (at least i do this...)
close file
check file signature
while( we havent reached end chunk "IEND" && we havent reached the end of our buffer )
{
get chunk length (read 32-bit int)
get chunk type (read 4 bytes)
switch (chunk type)
{
case "IHDR":
// handle IHDR, "Image header"
// load width, height, bit depth, color type,
// compression method, filter method,
// and interlace method.
break;
case "PLTE":
// load palette info
break;
case "IDAT":
// append this chunk's data onto end of
// some image buffer
break;
case "IEND":
// this is the "Image trailer" chunk, no data, just
// signals end of PNG file.
break;
default:
// dunno what this chunk is, so skip it
break;
}
get CRC of this chunk recorded in file (read 32 bit int)
calculate this chunk's CRC yourself (CRC is of chunk type and chunk data)
if they dont match then file is corrupt, bail!
}
if we didnt find an IHDR, (possibly a PLTE), at least one IDAT, and an IEND chunk then bail
if header told us image was compressed, then uncompress the image buffer
if header told us image was filtered, then unfilter the image buffer
// now you have everything you need (width, height, etc.; palette if applicable; and actual image pixel data)
// do with it as you please
free PNG file buffer and any other allocated memory
quote:
Original post by DarkHamster
I read the manual over a few times, and I was wondering, how would I go from png file to an OGL texture?
int textureObject = 0;
glGenTextures( 1, &textureObject);
glBindTextures (GL_TEXTURE_2D, textureObject);
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, imageWidth, imageHeight, GL_RGB, GL_UNSIGNED_BYTE, imageData);
quote:
I think I have most of it, but I''m entirely confused about the very last step (reading the image data from the png file).
// basic pseudo code
open file
read file into buffer (at least i do this...)
close file
check file signature
while( we havent reached end chunk "IEND" && we havent reached the end of our buffer )
{
get chunk length (read 32-bit int)
get chunk type (read 4 bytes)
switch (chunk type)
{
case "IHDR":
// handle IHDR, "Image header"
// load width, height, bit depth, color type,
// compression method, filter method,
// and interlace method.
break;
case "PLTE":
// load palette info
break;
case "IDAT":
// if compressed, uncompress
// append this chunk's data onto end of
// some image buffer
break;
case "IEND":
// this is the "Image trailer" chunk, no data, just
// signals end of PNG file.
break;
default:
// dunno what this chunk is, so skip it
break;
}
get CRC of this chunk recorded in file (read 32 bit int)
calculate this chunk's CRC yourself (CRC is of chunk type and chunk data)
if they dont match then file is corrupt, bail!
}
if we didnt find an IHDR, (possibly a PLTE), at least one IDAT, and an IEND chunk then bail
if header told us image was filtered, then unfilter the image buffer
// now you have everything you need (width, height, etc.; palette if applicable; and actual image pixel data)
// do with it as you please
free PNG file buffer and any other allocated memory
quote:
Original post by DarkHamster
maybe I''m just stupid but... I cant figure out how to use zlib now =( Ive got everything else I need to load pngs written (well I''m pretty sure...) Once again the manual was confusing, and it never actually says "you do x to inflate/deflate some data"...
quote:
Original post by DarkHamster
Im not using libPNG -- I''m writing my own implementation because I only need it for one case (I''m making all the data I need to load), and I already have just about everything written (aside from inflating the data).
1) allocate enough memory for the uncompressed image data.
this is (width + 1) * height * (bytes_per_pixel)
in my case, RGBA, it was "(width + 1) * height * 4" bytes.
why "(width + 1)" instead of just "width"? because at the
beginning of each scan line, there is one byte that tells
you which filter was used on that scan line. (that is, if
filters are used, and im guessing they generally always will
be used, since they help greatly). you could always check
if the header chunk said filters were used, and if not, just
leave off the "+ 1" part of "(width + 1)".
2) pass the complete IDAT chunk data to the zlib function
"uncompress". heres some modified code out of my program...
Uint32 err = 0;
Uint32 uncompressedlength = 0;
// output buffer is a pointer to memory where the uncompressed
// data will be put.
// myLoadedFilebuffer is the pointer to memory where the PNG
// file was loaded.
// indexOfIDATdata is the offset where this IDAT's data begins
// thisIDATschunklength is simply this chunk's length loaded at
// the beginning of the chunk.
err = uncompress(outputbuffer, &uncompressedlength, &myLoadedFileBuffer[ indexOfIDATdata ], thisIDATschunklength );
if (err != Z_OK)
{
// while not a complete list, here are some error meanings...
// if err == -3 then input data was corrupted
// if err == -4 then zlib didnt have enough memory
// if err == -5 then there wasnt enough memory in the output
// buffer
// free whatever memory needs freeing before we return
// "failed"
return -1;
}
// the variable "uncompressedlength" equals the length of the
// uncompressed image pixel data.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more