🔒 Locked
Image Loading
Advertisement
Original Post
Hey, Can anyone point to or show me how to load in jpeg, png, gif(doesnt have to be animated), tiff, pcx and dib image files in c++ (DLL,LIB,or code) please. If you want to know why I want to load these files, I just want to. Thanks
you could use SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0){ cerr << "error L init SDL [" << SDL_GetError() << "]\n"; exit(-1);}SDL_Surface *screen = SDL_SetVideoMode(800,600,16,SDL_HWSURFACE);if(!screen){ cerr << "error : SDL_SetVideoMode(" << w << "," << h << "," << bpp << ",SDL_SWSURFACE)\t" << SDL_GetError() << "\n"; exit(-1);}/*here we create a tmp surface, convertit, and free the tmp*/SDL_Surface *tmp = IMG_Load("myimage.jpg");if(!tmp) // error checking{ cerr << "error : LoadImage(" << src << ")\t" << SDL_GetError() << "\n";}SDL_Surface *img = SDL_DisplayFormat(tmp);SDL_FreeSurface(tmp);SDL_Rect dest; // create destination rectangledest.x = 10;dest.y = 100;SDL_BlitSurface(img , NULL , screen , &dest);
oh yer
no SDL, lol
no SDL, lol
I can recommend GDI+. It took me about 15 mins to get my image loading code working. Here's my function for loading any image file into an array of BYTEs (representing ARGB data):
I don't know if my CStream implementation is nessecary, there's probably some COM type I could have used. But it was just as fast for me to derive my own type as it would be for me to hunt through the docs to find the COM type and then work out how to create it...
#include <gdiplus.h>using namespace Gdiplus;#pragma comment (lib,"gdiplus.lib")//==========================================================================//// Stream implementation ////==========================================================================//class CStream : public IStream{public: CStream(const BYTE* pBuffer, size_t nLen) : m_pBuffer(pBuffer), m_nLen(nLen), m_nPos(0) {} // IUnknown // virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) {return E_FAIL;} virtual ULONG STDMETHODCALLTYPE AddRef() {return 1;} virtual ULONG STDMETHODCALLTYPE Release() {return 1;} // ISequentialStream // virtual HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead) { if(cb > m_nLen-m_nPos) { memcpy(pv,m_pBuffer+m_nPos,m_nLen-m_nPos); if(pcbRead) *pcbRead = m_nLen-m_nPos; m_nPos = m_nLen; return S_OK; } else { memcpy(pv,m_pBuffer+m_nPos,cb); if(pcbRead) *pcbRead = cb; m_nPos += cb; return S_OK; } } virtual HRESULT STDMETHODCALLTYPE Write(const void *pv, ULONG cb, ULONG *pcbWritten) {return E_FAIL;} // IStream // virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize) {return E_FAIL;} virtual HRESULT STDMETHODCALLTYPE CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten) {return E_FAIL;} virtual HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags) {return E_FAIL;} virtual HRESULT STDMETHODCALLTYPE Revert() {return E_FAIL;} virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) {return E_FAIL;} virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) {return E_FAIL;} virtual HRESULT STDMETHODCALLTYPE Clone(IStream **ppstm) {return E_FAIL;} virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG *pInfo, DWORD grfStatFlag) { HRESULT hResult; memset(pInfo,0,sizeof(STATSTG)); if((grfStatFlag & STATFLAG_NONAME) == 0) { IMalloc* pMalloc; hResult = CoGetMalloc(1,&pMalloc); if(FAILED(hResult)) return hResult; pInfo->pwcsName = (LPOLESTR)pMalloc->Alloc(4); pMalloc->Release(); if(!pInfo->pwcsName) return E_OUTOFMEMORY; memcpy(pInfo->pwcsName,L"?",4); } pInfo->type = STGTY_STREAM; pInfo->cbSize.QuadPart = m_nLen; return S_OK; } virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition) { switch(dwOrigin) { case STREAM_SEEK_SET: m_nPos = (size_t)dlibMove.QuadPart; break; case STREAM_SEEK_CUR: m_nPos = (size_t)(((int)m_nPos)+dlibMove.QuadPart); break; case STREAM_SEEK_END: m_nPos = (size_t)(((int)m_nLen)+dlibMove.QuadPart); break; } if(plibNewPosition) plibNewPosition->QuadPart = m_nPos; return S_OK; }private: const BYTE* m_pBuffer; size_t m_nLen; size_t m_nPos;};BYTE* LoadImageInMemory(const BYTE* pData, DWORD dwLen, DWORD& rWidth, DWORD& rHeight, DWORD dwMaxX, DWORD dwMaxY){BYTE* pRet; // Init GDI+ GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; if(GdiplusStartup(&gdiplusToken,&gdiplusStartupInput,NULL) != Ok) return NULL; // Load from our stream // { CStream theStream(pData,dwLen); Bitmap theImage(&theStream); if(theImage.GetLastStatus() != Ok) { GdiplusShutdown(gdiplusToken); return NULL; } // Allocate memory for return // rWidth = min(theImage.GetWidth(),dwMaxX); rHeight = min(theImage.GetHeight(),dwMaxY); pRet = new BYTE[rWidth*rHeight*4]; if(!pRet) { GdiplusShutdown(gdiplusToken); return NULL; } Rect rc(0,0,rWidth,rHeight); BitmapData theLock; theLock.Width = rWidth; theLock.Height = rHeight; theLock.Stride = theLock.Width*4; theLock.Scan0 = pRet; if(theImage.LockBits(&rc,ImageLockModeRead|ImageLockModeUserInputBuf,PixelFormat32bppRGB,&theLock) != Ok) { delete[] pRet; GdiplusShutdown(gdiplusToken); return NULL; } theImage.UnlockBits(&theLock); } // Cleanup and return // GdiplusShutdown(gdiplusToken); return pRet;}I don't know if my CStream implementation is nessecary, there's probably some COM type I could have used. But it was just as fast for me to derive my own type as it would be for me to hunt through the docs to find the COM type and then work out how to create it...
Corona.
It's a standalone library that loads/saves all those formats and performs pixel format conversion if you want to. Usage is very simple as well:
-Markus-
It's a standalone library that loads/saves all those formats and performs pixel format conversion if you want to. Usage is very simple as well:
#include "Corona.h"int main() { corona::Image *pImage = corona::OpenImage("my.jpg", corona::PF_R8G8B8A8); // pImage->getWidth() gives you the image's width // pImage->getHeight() gives you the image's height // pImage->getPixels() returns a pointer to the image's pixels delete pImage;}-Markus-
You could also just visit Wotsit.org to get the formats specs, and then write your own image loaders...
It requires more time and effort, but you'll be learning a lot from doing it yourself - although the time you spend on it could also be used to learn something else of course.
It requires more time and effort, but you'll be learning a lot from doing it yourself - although the time you spend on it could also be used to learn something else of course.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement