Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Image Class Reference

#include <image.h>

List of all members.

Public Types

enum  { ColorChannel = 1, AlphaChannel = 2 }

Public Member Functions

ImagecomputeNormalMap (float scale, bool wrap) const
int getComponents () const
int getFormat () const
int getHeight () const
unsigned char * getMipLevel (int mip)
int getMipLevelCount () const
int getMipLevelSize (int mip) const
unsigned char * getPixelRow (int mip, int row)
unsigned char * getPixelRow (int row)
unsigned char * getPixels ()
int getSize () const
int getWidth () const
bool hasAlpha () const
 Image (int fmt, int w, int h, int mips=1)
bool isCompressed () const
 ~Image ()

Private Attributes

int components
int format
int height
int mipLevels
unsigned char * pixels
int size
int width


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
ColorChannel 
AlphaChannel 

Definition at line 45 of file image.h.

00045          {
00046         ColorChannel = 1,
00047         AlphaChannel = 2
00048     };


Constructor & Destructor Documentation

Image::Image int  fmt,
int  w,
int  h,
int  mips = 1
 

Definition at line 139 of file image.cpp.

References calcMipLevelSize(), components, formatComponents(), mipLevels, pixels, and size.

Referenced by computeNormalMap().

00139                                             :
00140     width(w),
00141     height(h),
00142     mipLevels(mips), 
00143     format(fmt),
00144     pixels(NULL)
00145 {
00146     components = formatComponents(fmt);
00147     assert(components != 0);
00148 
00149     size = 0;
00150     for (int i = 0; i < mipLevels; i++)
00151         size += calcMipLevelSize(fmt, w, h, i);
00152     pixels = new unsigned char[size];
00153 }

Image::~Image  ) 
 

Definition at line 156 of file image.cpp.

References pixels.

00157 {
00158     if (pixels != NULL)
00159         delete[] pixels;
00160 }


Member Function Documentation

Image * Image::computeNormalMap float  scale,
bool  wrap
const
 

Definition at line 283 of file image.cpp.

References components, getPixels(), height, Image(), isCompressed(), pixels, sqrt(), and width.

Referenced by LoadHeightMapFromFile().

00284 {
00285     // Can't do anything with compressed input; there are probably some other
00286     // formats that should be rejected as well . . .
00287     if (isCompressed())
00288         return NULL;
00289 
00290     Image* normalMap = new Image(GL_RGBA, width, height);
00291     if (normalMap == NULL)
00292         return NULL;
00293 
00294     unsigned char* nmPixels = normalMap->getPixels();
00295 
00296     // Compute normals using differences between adjacent texels.
00297     for (int i = 0; i < height; i++)
00298     {
00299         for (int j = 0; j < width; j++)
00300         {
00301             int i0 = i;
00302             int j0 = j;
00303             int i1 = i - 1;
00304             int j1 = j - 1;
00305             if (i1 < 0)
00306             {
00307                 if (wrap)
00308                 {
00309                     i1 = height - 1;
00310                 }
00311                 else
00312                 {
00313                     i0++;
00314                     i1++;
00315                 }   
00316             }
00317             if (j1 < 0)
00318             {
00319                 if (wrap)
00320                 {
00321                     j1 = width - 1;
00322                 }
00323                 else
00324                 {
00325                     j0++;
00326                     j1++;
00327                 }
00328             }
00329 
00330             int h00 = (int) pixels[(i0 * width + j0) * components];
00331             int h10 = (int) pixels[(i0 * width + j1) * components];
00332             int h01 = (int) pixels[(i1 * width + j0) * components];
00333             
00334             float dx = (float) (h10 - h00) * (1.0f / 255.0f) * scale;
00335             float dy = (float) (h01 - h00) * (1.0f / 255.0f) * scale;
00336 
00337             float mag = (float) sqrt(dx * dx + dy * dy + 1.0f);
00338             float rmag = 1.0f / mag;
00339 
00340             int n = (i * width + j) * 4;
00341             nmPixels[n]     = (unsigned char) (128 + 127 * dx * rmag);
00342             nmPixels[n + 1] = (unsigned char) (128 + 127 * dy * rmag);
00343             nmPixels[n + 2] = (unsigned char) (128 + 127 * rmag);
00344             nmPixels[n + 3] = 255;
00345         }
00346     }
00347 
00348     return normalMap;
00349 }

int Image::getComponents  )  const
 

Definition at line 193 of file image.cpp.

References components.

Referenced by CreateProceduralCubeMap(), and CreateProceduralTexture().

00194 {
00195     return components;
00196 }

int Image::getFormat  )  const
 

Definition at line 187 of file image.cpp.

References format.

Referenced by CubeMap::CubeMap(), and TiledTexture::TiledTexture().

00188 {
00189     return format;
00190 }

int Image::getHeight  )  const
 

Definition at line 169 of file image.cpp.

References height.

Referenced by SplashWindow::createBitmap(), SplashWindow::createWindow(), VirtualTexture::loadTileTexture(), and TiledTexture::TiledTexture().

00170 {
00171     return height;
00172 }

unsigned char * Image::getMipLevel int  mip  ) 
 

Definition at line 226 of file image.cpp.

References calcMipLevelSize(), format, height, mipLevels, pixels, and width.

Referenced by getPixelRow(), and TiledTexture::TiledTexture().

00227 {
00228     if (mip >= mipLevels)
00229         return NULL;
00230 
00231     int offset = 0;
00232     for (int i = 0; i < mip; i++)
00233         offset += calcMipLevelSize(format, width, height, i);
00234 
00235     return pixels + offset;
00236 }

int Image::getMipLevelCount  )  const
 

Definition at line 175 of file image.cpp.

References mipLevels.

00176 {
00177     return mipLevels;
00178 }

int Image::getMipLevelSize int  mip  )  const
 

Definition at line 239 of file image.cpp.

References calcMipLevelSize(), format, height, mipLevels, and width.

00240 {
00241     if (mip >= mipLevels)
00242         return 0;
00243     else
00244         return calcMipLevelSize(format, width, height, mip);
00245 }

unsigned char * Image::getPixelRow int  mip,
int  row
 

Definition at line 205 of file image.cpp.

References components, getMipLevel(), height, isCompressed(), max, mipLevels, and width.

00206 {
00207     int w = max(width >> mip, 1);
00208     int h = max(height >> mip, 1);
00209     if (mip >= mipLevels || row >= h)
00210         return NULL;
00211 
00212     // Row addressing of compressed textures is not allowed
00213     if (isCompressed())
00214         return NULL;
00215 
00216     return getMipLevel(mip) + row * w * components;
00217 }

unsigned char * Image::getPixelRow int  row  ) 
 

Definition at line 220 of file image.cpp.

Referenced by CreateProceduralCubeMap(), CreateProceduralTexture(), LoadBMPImage(), LoadJPEGImage(), and LoadPNGImage().

00221 {
00222     return getPixelRow(0, row);
00223 }

unsigned char * Image::getPixels  ) 
 

Definition at line 199 of file image.cpp.

References pixels.

Referenced by computeNormalMap(), SplashWindow::createBitmap(), CubeMap::CubeMap(), LoadDDSImage(), LoadJPEGImage(), and TiledTexture::TiledTexture().

00200 {
00201     return pixels;
00202 }

int Image::getSize  )  const
 

Definition at line 181 of file image.cpp.

References size.

Referenced by LoadDDSImage().

00182 {
00183     return size;
00184 }

int Image::getWidth  )  const
 

Definition at line 163 of file image.cpp.

References width.

Referenced by SplashWindow::createBitmap(), SplashWindow::createWindow(), VirtualTexture::loadTileTexture(), and TiledTexture::TiledTexture().

00164 {
00165     return width;
00166 }

bool Image::hasAlpha  )  const
 

Definition at line 262 of file image.cpp.

References format, GL_BGRA_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, and GL_COMPRESSED_RGBA_S3TC_DXT5_EXT.

00263 {
00264     switch (format)
00265     {
00266     case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
00267     case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
00268     case GL_RGBA:
00269     case GL_BGRA_EXT:
00270     case GL_LUMINANCE_ALPHA:
00271     case GL_ALPHA:
00272         return true;
00273     default:
00274         return false;
00275     }
00276 }

bool Image::isCompressed  )  const
 

Definition at line 248 of file image.cpp.

References format, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, and GL_COMPRESSED_RGBA_S3TC_DXT5_EXT.

Referenced by computeNormalMap(), and getPixelRow().

00249 {
00250     switch (format)
00251     {
00252     case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
00253     case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
00254     case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
00255         return true;
00256     default:
00257         return false;
00258     }
00259 }


Member Data Documentation

int Image::components [private]
 

Definition at line 54 of file image.h.

Referenced by computeNormalMap(), getComponents(), getPixelRow(), and Image().

int Image::format [private]
 

Definition at line 55 of file image.h.

Referenced by getFormat(), getMipLevel(), getMipLevelSize(), hasAlpha(), isCompressed(), and LoadDDSImage().

int Image::height [private]
 

Definition at line 52 of file image.h.

Referenced by computeNormalMap(), getHeight(), getMipLevel(), getMipLevelSize(), getPixelRow(), and LoadDDSImage().

int Image::mipLevels [private]
 

Definition at line 53 of file image.h.

Referenced by getMipLevel(), getMipLevelCount(), getMipLevelSize(), getPixelRow(), and Image().

unsigned char* Image::pixels [private]
 

Definition at line 57 of file image.h.

Referenced by computeNormalMap(), getMipLevel(), getPixels(), Image(), and ~Image().

int Image::size [private]
 

Definition at line 56 of file image.h.

Referenced by getSize(), and Image().

int Image::width [private]
 

Definition at line 51 of file image.h.

Referenced by computeNormalMap(), getMipLevel(), getMipLevelSize(), getPixelRow(), getWidth(), and LoadDDSImage().


The documentation for this class was generated from the following files:
Generated on Sat Jan 14 22:33:22 2006 for Celestia by  doxygen 1.4.1