Skip to main content
GameDev.net gamedev.net
🔒 Locked

Loading PNGs (solved)

Started by Habba Apr 15, 2005 at 5:39 AM 3 replies 1.3k views
Original Post
Habba
Habba
I'm looking for a good way to load PNG images into DIB or into DirectDraw(7) Surface. I've tried FreeImage, devIL and PNGlib, but I'm not that good with using libraries and such, so I coulnd't get them working. So I'm looking for a good tutorial or helpful samples of how to load PNGs. Or if you know any other lossless & compressed image format that supports Alpha-Channel and is easier to use, I'm all ears. Edit: And I forgot to mention... I'm using Visual C++ 6.0 and I'm doing window application... [Edited by - Habba on April 15, 2005 12:02:34 PM]
Ilici
Ilici
I can't think of any easier way that to use DevIL.

Just from the back of my head the steps you need to do:

ilGenImages(1, &img)
ilBindImage(img);
ilLoadImage("img.png");
DDrawSurface->Lock(...);
//use DDrawSurface->GetLockedArray() to get pointer to surface buffer
// copy data using memcpy from ilGetData() which returns the memory buffer of the image loaded with devil to the pointer of the ddraw surface. the size of the buffer is width*height*numcolors (4 for alpha)
DDrawSurface->UnLock();
ilDeleteImages(1, &img);
Cygon
Cygon
I think I can, Corona :P

#include "Corona.h"int main() {  corona::Image *pImage = corona::OpenImage("my.png", PF_R8G8B8A8);  // pImage->getPixels() returns a pointer to the image pixels  // pImage->getWidth() returns the image's width  // pImage->getHeight() returns the image's height  delete pImage;}


-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
darookie
darookie
Try looking at NexgenIPL.
It's an easy to use, yet very powerful library.
Example:
// sample function that loads an image to a 32 bit pixel array// // Params:////   fileName [in]  - Image file to be loaded//   data     [out] - Reference to pointer that will contain 32 bit data//   width    [out] - Reference to integer that will contain image width//   height   [out] - Reference to integer that will contain image height//// Notes://    "data" must not be allocated. Just provide a null pointer, it will//    be allocated by the function and must be deleted manually once you're//    done with it (use "delete[] data").bool LoadImg( const char * fileName, unsigned char*& data, int& width, int& height) {    // allow for safe "delete [] data" call even after failure    data = 0;    // set image size to zero    width = height = 0;    // must be valid file name    if ( !fileName || !*fileName)        return false;    BTCImageObject img;    // Load() returns a codec Id or -1 if failed.    long result = img.Load( fileName );    if (result == -1) {       return false;    }    BTCImageData* imgData = img.GetObjectDataPtr();    // always return 32 bit image (use fully opaque alpha by default)    imgData->ConvertTo32BPP( 255 );    // assign outputs    data = new unsigned char[imgData->GetImageSize()];    width = imgData->GetWidth();    height= imgData->GetHeight();    // copy the data    std::copy(imgData->GetBits(), imgData->GetBits() + imgData->GetImageSize(), data);    return true;}


HTH,
Pat
Habba
Habba
Quote:
Original post by Cygon
I think I can, Corona :P


Thank you all! Now I got working PNG loader! I made it with Corona. It was the easiest one I found... although it wasn't easy for me at all. Maybe it tells more about me than Corona... :p

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.