Original Post
what library/classes do you recommend for 16-24bit .png and .tga files?... it has to be quite simple to use...
class CImage{public: CImage() { m_Data = 0; } ~CImage() {}; void Release(); bool Load(char *Filename); bool Load(CImage *Img); bool Load(unsigned char *Img, int Width, int Height, int Bpp); bool Load(int Width, int Height, int Bpp); bool LoadRaw(char *Filename, int Bpp); bool SaveToFile(char *Filename); void Resize(int Width, int Height); void *Lock(); int Width(); int Height(); int Bpp(); char *Filename();private: void FlipVertically(); void FlipHorizontally(); void SwapRB(); unsigned char *m_Data; char m_Filename[255]; int m_Width; int m_Height; int m_Bpp;};#include "CImage.h"#include "il/il.h"#include "il/il_wrap.h"void CImage::Release(){ // free memory if(m_Data) { delete [] m_Data; m_Data = 0; }}bool CImage::Load(char *Filename){ // free memory Release(); // load iamge if(!ilLoadImage(Filename)) return false; // save filename strcpy(m_Filename, Filename); // save image settings m_Width = ilGetInteger(IL_IMAGE_WIDTH); m_Height = ilGetInteger(IL_IMAGE_HEIGHT); m_Bpp = ilGetInteger(IL_IMAGE_BPP); //get a pointer to image data unsigned char *Data = ilGetData(); if(!Data) return false; // allocate memory for image m_Data = new unsigned char[m_Width*m_Height*m_Bpp]; if(!m_Data) return false; // copy image data for(int i=0; i < m_Width * m_Height * m_Bpp; i++) m_Data = Data; // rotate and swap channels if needed string temp = Filename; transform(temp.begin(), temp.end(), temp.begin(), tolower); // if .bmp file, convert from BGR to RGB format if(strstr(temp.c_str(), ".bmp") || strstr(temp.c_str(), ".tga")) SwapRB(); // if .tga file, flip vertically if(strstr(temp.c_str(), ".tga") || strstr(temp.c_str(), ".bmp") /*|| strstr(temp.c_str(), ".jpg") ||*/ /*strstr(temp.c_str(), ".dds") */) FlipVertically(); if(strstr(temp.c_str(), ".jpg")) FlipHorizontally(); // success return true;}bool CImage::Load(CImage *Img){ // check pointer if(!Img) return false; // free memory Release(); // save filename strcpy(m_Filename, Img->Filename()); // save image settings m_Width = Img->Width(); m_Height = Img->Height(); m_Bpp = Img->Bpp(); //get a pointer to image data unsigned char *Data = (unsigned char *)Img->Lock(); if(!Data) return false; // allocate memory for image m_Data = new unsigned char[m_Width*m_Height*m_Bpp]; if(!m_Data) return false; // copy image data for(int i=0; i < m_Width * m_Height * m_Bpp; i++) m_Data = Data; // rotate and swap channels if needed string temp = Img->Filename(); transform(temp.begin(), temp.end(), temp.begin(), tolower); // if .bmp file, convert from BGR to RGB format if(strstr(temp.c_str(), ".bmp") || strstr(temp.c_str(), ".tga")) SwapRB(); // if .tga file, flip vertically if(strstr(temp.c_str(), ".tga") || strstr(temp.c_str(), ".bmp") /*|| strstr(temp.c_str(), ".jpg") ||*/ /*strstr(temp.c_str(), ".dds") */) FlipVertically(); if(strstr(temp.c_str(), ".jpg")) FlipHorizontally(); // success return true;}bool CImage::Load(unsigned char *Img, int Width, int Height, int Bpp){ // check pointer if(!Img) return false; // free memory Release(); // save filename strcpy(m_Filename, ""); // save image settings m_Width = Width; m_Height = Height; m_Bpp = Bpp; // allocate memory for image m_Data = new unsigned char[m_Width*m_Height*m_Bpp]; if(!m_Data) return false; // copy image data for(int i=0; i < m_Width * m_Height * m_Bpp; i++) m_Data = Img; // success return true;}bool CImage::Load(int Width, int Height, int Bpp){ // free memory Release(); // save filename strcpy(m_Filename, ""); // save image settings m_Width = Width; m_Height = Height; m_Bpp = Bpp; // allocate memory for image m_Data = new unsigned char[m_Width*m_Height*m_Bpp]; if(!m_Data) return false; // set image data to 0 for(int i=0; i < m_Width * m_Height * m_Bpp; i++) m_Data = 0; // success return true;}bool CImage::LoadRaw(char *Filename, int Bpp){ // free memory Release(); // check file FILE *pFile; pFile = fopen(Filename, "rbD"); if(!pFile) return false; // calculate dimensions of heightmap fseek (pFile, 0, SEEK_END); m_Width = sqrt(ftell(pFile)/(double)Bpp); m_Height = m_Width; rewind(pFile); // save format m_Bpp = Bpp; // allocate memory m_Data = new unsigned char[m_Width*m_Height*m_Bpp]; if(!m_Data) return false; // read image data fread(m_Data, 1, m_Width*m_Height*m_Bpp, pFile); // close file fclose(pFile); // success return true;}bool CImage::SaveToFile(char *Filename){ // see if we have a .RAW file if(strstr(Filename, ".raw")) { FILE *fp = fopen(Filename, "wbD"); if(!fp) return false; fwrite(m_Data, 1, m_Width*m_Height*m_Bpp, fp); fclose(fp); return true; } // flip image if needed if(strstr(Filename, ".tga") || strstr(Filename, ".bmp") || strstr(Filename, ".jpg")) FlipVertically(); // set DevIL parameters and save to file ILuint image; ilGenImages(1, ℑ); ilBindImage(image); ilEnable(IL_FILE_OVERWRITE); ilSetInteger(IL_NO_COMPRESSION, 1); int Format; if(m_Bpp == 3) Format = IL_RGB; if(m_Bpp == 4) Format = IL_RGBA; ilTexImage(m_Width, m_Height, 1, m_Bpp, Format, IL_UNSIGNED_BYTE, m_Data); if(!ilSaveImage(Filename)) return false;}void CImage::Resize(int Width, int Height){ // check image allocation if(!m_Data) return; ILuint Image; ilGenImages(1, &Image); ilBindImage(Image); if(m_Bpp == 1) ilTexImage(m_Width, m_Height, 1, m_Bpp, IL_LUMINANCE, IL_UNSIGNED_BYTE, m_Data); else if(m_Bpp == 3) ilTexImage(m_Width, m_Height, 1, m_Bpp, IL_RGB, IL_UNSIGNED_BYTE, m_Data); else if(m_Bpp == 4) ilTexImage(m_Width, m_Height, 1, m_Bpp, IL_RGBA, IL_UNSIGNED_BYTE, m_Data); iluImageParameter(ILU_FILTER, ILU_SCALE_BSPLINE); iluScale(Width, Height, 1); unsigned char* Data = (unsigned char*)ilGetData(); if(!Data) return; // free old image data Release(); // save new image properties m_Width = ilGetInteger(IL_IMAGE_WIDTH); m_Height = ilGetInteger(IL_IMAGE_HEIGHT); m_Bpp = ilGetInteger(IL_IMAGE_BPP); // create new image data m_Data = new unsigned char[m_Width * m_Height * m_Bpp]; // copy resized data for(int i = 0; i < m_Width * m_Height * m_Bpp; i++) m_Data = Data; // delete memory ilDeleteImages(1, &Image);}void *CImage::Lock(){ // return pointer to image data return (void *)m_Data;}int CImage::Width(){ return m_Width;}int CImage::Height(){ return m_Height;}int CImage::Bpp(){ return m_Bpp;}char *CImage::Filename(){ return m_Filename;}void CImage::FlipVertically(){ // check for allocation if(!m_Data) return; // flip image data vertically for(int i = 0; i < m_Width; i++) { for(int j = 0; j < m_Height * 0.5f; j++) { for(int b = 0; b < m_Bpp; b++) { char T; T = m_Data[(j * m_Width + i) * m_Bpp + b]; m_Data[(j * m_Width + i) * m_Bpp + b] = m_Data[((m_Height - j - 1) * m_Width+i) * m_Bpp + b]; m_Data[((m_Height - j - 1) * m_Width + i) * m_Bpp + b] = T; } } }}void CImage::FlipHorizontally(){ // check for allocation if(!m_Data) return; // flip image data vertically for(int i = 0; i < m_Height; i++) { for(int j = 0; j < m_Width * 0.5f; j++) { for(int b = 0; b < m_Bpp; b++) { char T; T = m_Data[(i * m_Width + j) * m_Bpp + b]; m_Data[(i * m_Width + j) * m_Bpp + b] = m_Data[((m_Height - i - 1) * m_Width + j) * m_Bpp + b]; m_Data[((m_Height - i - 1) * m_Width + j) * m_Bpp + b] = T; } } }}void CImage::SwapRB(){ // check for allocation if(!m_Data) return; // swap R and B components for(int i =0 ; i < m_Width * m_Height; i++) { char T; T = m_Data[i * m_Bpp]; m_Data[i * m_Bpp + 0] = m_Data[i* m_Bpp + 2]; m_Data[i * m_Bpp + 2] = T; }}inline int GetRed(int x, int z){ int BPP = m_Bpp/8; return m_Data[x+z*m_Width*BPP*2]*pow(2,16)+m_Data[x+z*m_Width*BPP];}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