You can use libpng+zlib, but that can be difficult. Another possibility is to check out D3DX image loading.
Yet another possibility is I think Windows OleLoadPicture() can do it, here's a snippet out of some code I wrote
class Sprite
{
public:
~Sprite();
Sprite(const char *);
private:
void *read_bmp(const char *filename, long *lenp=NULL);
unsigned int *pix;
unsigned int width, height;
};
void *Sprite::read_bmp(const char *filename, long *lenp)
{
FILE *f;
long len;
void *file=NULL;
fopen_s(&f, filename, "rb");
if (f)
{
fseek(f, 0, SEEK_END);
len = ftell(f);
if (lenp) *lenp = len;
fseek(f, 0, SEEK_SET);
file = new char[len];
if (file)
fread(file, 1, len, f);
fclose(f);
}
return file;
}
Sprite::Sprite(const char *fname)
{
HDC hdc;
static struct
{
BITMAPINFO bmpi;
RGBQUAD masks[3];
} header;
IPicture *picture=NULL;
HBITMAP bmp;
ULONG cnt;
memset(&header.bmpi, 0, sizeof header.bmpi);
header.bmpi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmp = (HBITMAP)LoadImage(NULL, fname, IMAGE_BITMAP, 0,0, LR_LOADFROMFILE);
if (!bmp)
{
IStream *istm;
HRESULT err;
HGLOBAL hMem;
void *lpMem;
long size;
void *img;
img = read_bmp(fname, &size);
assert(img);
hMem = GlobalAlloc(GMEM_MOVEABLE, size);
assert(hMem);
lpMem = GlobalLock(hMem);
assert(lpMem);
memcpy(lpMem, img, size);
GlobalUnlock(hMem);
delete [] img;
err = CreateStreamOnHGlobal(hMem, FALSE, &istm);
assert(err == S_OK);
err = OleLoadPicture(istm, size, TRUE, IID_IPicture, (void **)&picture);
GlobalFree(hMem);
assert(err == S_OK);
cnt = istm->Release();
picture->get_Handle((OLE_HANDLE*)&bmp);
}
hdc = GetDC(NULL);
GetDIBits(hdc, bmp, 0,0, NULL, &header.bmpi, 0);
width = header.bmpi.bmiHeader.biWidth;
height = header.bmpi.bmiHeader.biHeight;
pix = new unsigned int[width * height];
assert(pix != NULL);
header.bmpi.bmiHeader.biBitCount = 32;
GetDIBits(hdc, bmp, 0, height, pix, &header.bmpi, DIB_RGB_COLORS);
ReleaseDC(NULL, hdc);
if (picture)
cnt = picture->Release();
else
DeleteObject(bmp);
//bitmaps are stored upside down (doh!)
unsigned int *line0 = pix;
unsigned int *lineN = pix+(height-1)*width;
unsigned int *tmpln = new unsigned int[width];
unsigned int y;
for (y = 0; y < height/2; y++)
{
memcpy(tmpln, line0, width * sizeof *pix);
memcpy(line0, lineN, width * sizeof *pix);
memcpy(lineN, tmpln, width * sizeof *pix);
line0 += width;
lineN -= width;
}
delete [] tmpln;
}
That probably won't compile right out of the box, but it shows the technique. It should load nearly all Windows format images too.
Welcome back to the forum!
Jim