#include <Image.h>
Public Types | |
enum | ImageType { RGB, RGBA, Unknown } |
Public Member Functions | |
Image (const std::string &fileName, bool alpha=false, int ar=ALPHA_COLOR_R, int ag=ALPHA_COLOR_G, int ab=ALPHA_COLOR_B) | |
Constructor given image filename and alpha values. | |
Image (unsigned char *image, int width, int height, int bits, ImageType type, bool alpha=false, int ar=ALPHA_COLOR_R, int ag=ALPHA_COLOR_G, int ab=ALPHA_COLOR_B) | |
Constructor given the raw image data. | |
~Image (void) | |
Destructor. | |
bool | isLoaded (void) |
Returns true if the image was loaded correctly, else false. | |
const std::string & | getFileName (void) const |
Returns the filename of the image. | |
ImageType | getType (void) |
Returns the type (RGB, RGBA, ...). | |
unsigned char * | getImage (void) |
Returns the raw image data. | |
int | getWidth (void) |
Returns the width. | |
int | getHeight (void) |
Returns the height. | |
int | getBitsPerPixel (void) |
Returns the bits per pixel (mostly 24, 32). | |
void | swapBGRToRGB (void) |
This swaps the color components blue and red. | |
void | addAlphaChannel (int ar, int ag, int ab) |
Add alpha channel to this image. | |
Private Member Functions | |
bool | loadFile (int alpha, bool swapBGR, int ar, int ag, int ab) |
Load a BMP file. | |
bool | loadTargaFile (int alpha, int ar, int ag, int ab) |
Load a targa file. | |
Private Attributes | |
std::string | m_fileName |
Filename of the image. | |
ImageType | m_type |
Type of the image. | |
unsigned char * | m_image |
The pixeldata of the image. | |
int | m_width |
Width of the image. | |
int | m_height |
Height of the image. | |
int | m_bitsPerPixel |
Bits per pixel. | |
bool | m_loadOk |
This is true if the image was loaded correctly. |
|
Definition at line 23 of file Image.h. Referenced by getType().
|
|
Constructor given image filename and alpha values.
Definition at line 20 of file Image.cpp. References loadFile(), loadTargaFile(), m_bitsPerPixel, m_fileName, m_height, m_image, m_loadOk, m_type, m_width, and Unknown.
00020 { 00021 bool result; 00022 bool swapBGR; 00023 00024 00025 // Initialize class variables. 00026 m_type = Unknown; 00027 m_image = NULL; 00028 m_width = 0; 00029 m_height = 0; 00030 m_bitsPerPixel = 0; 00031 m_loadOk = false; 00032 00033 result = false; 00034 swapBGR = false; 00035 00036 // Get the extension of the image filename 00037 std::string ext = fileutils::getExtension(fileName); 00038 00039 if(ext == "not_found") { 00040 // TODO: log 00041 printf("[%s]: Error: Can not load file without extension. What file type? <%s>\n", __FILE__, fileName.c_str()); 00042 return; 00043 } 00044 00045 // Copy filename 00046 m_fileName = fileName; 00047 00048 // If extensions equals "tga" or "TGA", load tga files 00049 if(ext == "tga" || ext == "TGA") { 00050 result = loadTargaFile(alpha, ar, ag, ab); 00051 00052 } else { 00053 // BMP 00054 if(ext == "bmp" || ext == "BMP") { 00055 swapBGR = true; 00056 // JPEG 00057 } else if(ext == "jpg" || ext == "JPG" || ext == "jpeg" || ext == "JPEG") { 00058 swapBGR = false; 00059 } else { 00060 // TODO: log message and add new filetypes 00061 return; 00062 } 00063 // Load image 00064 result = loadFile(alpha, swapBGR, ar, ag, ab); 00065 } 00066 00067 // Check result 00068 if(!result) { 00069 // TODO: log 00070 printf("[%s]: Error: Could not load image <%s>.\n", __FILE__, fileName.c_str()); 00071 m_loadOk = false; 00072 } else { 00073 // Loading was successfully done 00074 m_loadOk = true; 00075 } 00076 } |
Here is the call graph for this function:
|
Constructor given the raw image data.
Definition at line 84 of file Image.cpp. References addAlphaChannel(), m_bitsPerPixel, m_height, m_image, m_loadOk, m_type, m_width, RGB, RGBA, and Unknown.
00085 { 00086 00087 if(image != NULL) { 00088 m_image = image; 00089 image = NULL; 00090 } else { 00091 // TODO: log 00092 printf("[%s]: Error: You must give a valid image to the constructor!\n", __FILE__); 00093 m_loadOk = false; 00094 return; 00095 } 00096 00097 m_width = width; 00098 m_height = height; 00099 m_bitsPerPixel = bits; 00100 00101 // set pixel type 00102 if(m_bitsPerPixel == 24) { 00103 m_type = RGB; 00104 } else if(m_bitsPerPixel == 32) { 00105 m_type = RGBA; 00106 } else { 00107 // TODO: log 00108 printf("[%s]: Warning: Unknown pixeltype!\n", __FILE__); 00109 m_type = Unknown; 00110 } 00111 00112 if(alpha) { 00113 addAlphaChannel(ar, ag, ab); 00114 } 00115 00116 m_loadOk = true; 00117 } |
Here is the call graph for this function:
|
Destructor.
Definition at line 125 of file Image.cpp. References m_image.
|
|
Add alpha channel to this image.
Definition at line 311 of file Image.cpp. References m_bitsPerPixel, m_height, m_image, m_type, m_width, RGB, RGBA, and uint8. Referenced by Image(), loadFile(), and loadTargaFile().
00311 { 00312 // 00313 // Variables 00314 // 00315 uint8 *alphaImage; 00316 int i; 00317 int j; 00318 int size; 00319 00320 00321 // If we already have alpha, then don't do anything 00322 // but if the type is RGB, then add alpha 00323 if(this->m_type == RGB) { 00324 00325 // The size of the original image 00326 size = this->m_width * this->m_height * (this->m_bitsPerPixel / 8); 00327 00328 // The new image with alpha 00329 alphaImage = new uint8[this->m_width * this->m_height * 4]; 00330 if(alphaImage == NULL) { 00331 // TODO: log 00332 printf("[%s]: Error: Could not allocate space for image.\n", __FILE__); 00333 return; 00334 } 00335 00336 // Add alpha 00337 for(i = 0, j = 0; i < size; i += 3, j += 4) { 00338 alphaImage[j] = this->m_image[i]; 00339 alphaImage[j + 1] = this->m_image[i + 1]; 00340 alphaImage[j + 2] = this->m_image[i + 2]; 00341 if(this->m_image[i] == ar && this->m_image[i + 1] == ag && this->m_image[i + 2] == ab) { 00342 alphaImage[j + 3] = 0; 00343 } else { 00344 alphaImage[j + 3] = 255; 00345 } 00346 } 00347 // Delete old image 00348 delete [] this->m_image; 00349 this->m_image = NULL; 00350 // Copy over and set new infos 00351 this->m_image = alphaImage; 00352 this->m_type = RGBA; 00353 this->m_bitsPerPixel = 32; 00354 } 00355 } |
|
Returns the bits per pixel (mostly 24, 32).
Definition at line 90 of file Image.h. References m_bitsPerPixel.
00090 { 00091 return m_bitsPerPixel; 00092 } |
|
Returns the filename of the image.
Definition at line 65 of file Image.h. References m_fileName.
00065 { 00066 return m_fileName; 00067 } |
|
Returns the height.
Definition at line 85 of file Image.h. References m_height. Referenced by pge::HeightmapLoader::generateTerrain(), and pge::Texture::Texture().
00085 { 00086 return m_height; 00087 } |
|
Returns the raw image data.
Definition at line 75 of file Image.h. References m_image. Referenced by pge::Texture::createTexture2D(), and pge::HeightmapLoader::generateTerrain().
00075 { 00076 return m_image; 00077 } |
|
Returns the type (RGB, RGBA, ...).
Definition at line 70 of file Image.h. References ImageType, and m_type. Referenced by pge::Texture::createTexture2D(), and pge::HeightmapLoader::loadHeightmap().
00070 { 00071 return m_type; 00072 } |
|
Returns the width.
Definition at line 80 of file Image.h. References m_width. Referenced by pge::TextureFactory::createCubemapTexture(), pge::HeightmapLoader::generateTerrain(), and pge::Texture::Texture().
00080 { 00081 return m_width; 00082 } |
|
Returns true if the image was loaded correctly, else false.
Definition at line 60 of file Image.h. References m_loadOk. Referenced by pge::TextureFactory::createCubemapTexture(), pge::TextureFactory::createTexture2D(), and pge::HeightmapLoader::loadHeightmap().
00060 { 00061 return m_loadOk; 00062 } |
|
Load a BMP file.
Definition at line 138 of file Image.cpp. References addAlphaChannel(), m_bitsPerPixel, m_fileName, m_height, m_image, m_type, m_width, RGB, RGBA, swapBGRToRGB(), and Unknown. Referenced by Image().
00138 { 00139 // 00140 // Variables 00141 // 00142 SDL_RWops *rwops; 00143 SDL_Surface *sur; 00144 SDL_PixelFormat *pix; 00145 00146 00147 // Load file 00148 rwops = SDL_RWFromFile(m_fileName.c_str(), "rb"); 00149 if(rwops == NULL) { 00150 // TODO: log 00151 printf("[%s]: Error: Could not open file.\n", __FILE__); 00152 return false; 00153 } 00154 sur = IMG_Load_RW(rwops, 0); 00155 if(sur == NULL) { 00156 // TODO: log 00157 printf("[%s]: Error: Could not load image.\n", __FILE__); 00158 return false; 00159 } 00160 00161 pix = sur->format; 00162 if(pix == NULL) { 00163 // TODO: log 00164 printf("[%s]: Error: Could not get pixel format.\n", __FILE__); 00165 return false; 00166 } 00167 00168 // Set pixel type 00169 if(pix->BitsPerPixel == 24) { 00170 m_type = RGB; 00171 } else if(pix->BitsPerPixel == 32) { 00172 m_type = RGBA; 00173 } else { 00174 // TODO: log 00175 printf("[%s]: Warning: Unknown pixeltype!\n", __FILE__); 00176 m_type = Unknown; 00177 } 00178 00179 // Copy data over 00180 m_width = sur->w; 00181 m_height = sur->h; 00182 m_bitsPerPixel = pix->BitsPerPixel; 00183 00184 m_image = new unsigned char[m_width * m_height * pix->BytesPerPixel]; 00185 memcpy(m_image, sur->pixels, m_width * m_height * pix->BytesPerPixel); 00186 00187 // Swap blue and red 00188 if(swapBGR) { 00189 swapBGRToRGB(); 00190 } 00191 00192 if(alpha) { 00193 addAlphaChannel(ar, ag, ab); 00194 } 00195 00196 // Clean up 00197 SDL_FreeSurface(sur); 00198 SDL_FreeRW(rwops); 00199 00200 return true; 00201 } |
Here is the call graph for this function:
|
Load a targa file.
Definition at line 209 of file Image.cpp. References addAlphaChannel(), m_bitsPerPixel, m_fileName, m_height, m_image, m_type, m_width, RGB, RGBA, swapBGRToRGB(), and Unknown. Referenced by Image().
00209 { 00210 // 00211 // Variables 00212 // 00213 SDL_RWops *rwops; 00214 SDL_Surface *sur; 00215 SDL_PixelFormat *pix; 00216 00217 00218 // Load file 00219 rwops = SDL_RWFromFile(m_fileName.c_str(), "rb"); 00220 if(rwops == NULL) { 00221 // TODO: log 00222 printf("[%s]: Error: Could not open file.\n", __FILE__); 00223 return false; 00224 } 00225 sur = IMG_LoadTGA_RW(rwops); 00226 if(sur == NULL) { 00227 // TODO: log 00228 printf("[%s]: Error: Could not load image.\n", __FILE__); 00229 return false; 00230 } 00231 00232 pix = sur->format; 00233 if(pix == NULL) { 00234 // TODO: log 00235 printf("[%s]: Error: Could not get pixel format.\n", __FILE__); 00236 return false; 00237 } 00238 00239 // Set pixel type 00240 if(pix->BitsPerPixel == 24) { 00241 m_type = RGB; 00242 } else if(pix->BitsPerPixel == 32) { 00243 m_type = RGBA; 00244 } else { 00245 // TODO: log 00246 printf("[%s]: Warning: Unknown pixeltype!\n", __FILE__); 00247 m_type = Unknown; 00248 } 00249 00250 // Copy data over 00251 m_width = sur->w; 00252 m_height = sur->h; 00253 m_bitsPerPixel = pix->BitsPerPixel; 00254 00255 m_image = new unsigned char[m_width * m_height * pix->BytesPerPixel]; 00256 memcpy(m_image, sur->pixels, m_width * m_height * pix->BytesPerPixel); 00257 00258 // Swap blue and red 00259 swapBGRToRGB(); 00260 00261 if(alpha) { 00262 this->addAlphaChannel(ar, ag, ab); 00263 } 00264 00265 // Clean up 00266 SDL_FreeSurface(sur); 00267 SDL_FreeRW(rwops); 00268 00269 return true; 00270 } |
Here is the call graph for this function:
|
This swaps the color components blue and red.
Definition at line 278 of file Image.cpp. References m_bitsPerPixel, m_height, m_image, and m_width. Referenced by loadFile(), and loadTargaFile().
00278 { 00279 // 00280 // Variables 00281 // 00282 unsigned char swap; 00283 int i; 00284 int size; 00285 00286 00287 size = m_width * m_height * (m_bitsPerPixel / 8); 00288 00289 // Swap BGR to RGB 00290 if(m_bitsPerPixel == 24) { 00291 for(i = 0; i < size; i += 3) { 00292 swap = m_image[i]; 00293 m_image[i] = m_image[i + 2]; 00294 m_image[i + 2] = swap; 00295 } 00296 } else if(m_bitsPerPixel == 32) { 00297 for(i = 0; i < size; i += 4) { 00298 swap = m_image[i]; 00299 m_image[i] = m_image[i + 2]; 00300 m_image[i + 2] = swap; 00301 } 00302 } 00303 } |
|
Bits per pixel.
Definition at line 135 of file Image.h. Referenced by addAlphaChannel(), getBitsPerPixel(), Image(), loadFile(), loadTargaFile(), and swapBGRToRGB(). |
|
Filename of the image.
Definition at line 120 of file Image.h. Referenced by getFileName(), Image(), loadFile(), and loadTargaFile(). |
|
Height of the image.
Definition at line 132 of file Image.h. Referenced by addAlphaChannel(), getHeight(), Image(), loadFile(), loadTargaFile(), and swapBGRToRGB(). |
|
The pixeldata of the image.
Definition at line 126 of file Image.h. Referenced by addAlphaChannel(), getImage(), Image(), loadFile(), loadTargaFile(), swapBGRToRGB(), and ~Image(). |
|
This is true if the image was loaded correctly.
Definition at line 138 of file Image.h. Referenced by Image(), and isLoaded(). |
|
Type of the image.
Definition at line 123 of file Image.h. Referenced by addAlphaChannel(), getType(), Image(), loadFile(), and loadTargaFile(). |
|
Width of the image.
Definition at line 129 of file Image.h. Referenced by addAlphaChannel(), getWidth(), Image(), loadFile(), loadTargaFile(), and swapBGRToRGB(). |