Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

PixelTool Class Reference


Detailed Description

Pixel rutines.

Definition at line 9 of file PixelTool.h.

Static Public Member Functions

static bool colorEquals (const SDL_Color &color1, const SDL_Color &color2)
 Compare colors.
static Uint32 convertColor (SDL_PixelFormat *format, const SDL_Color &color)
static SDL_Color getColor (SDL_Surface *surface, int x, int y)
 Get color at x, y.
static void putColor (SDL_Surface *surface, int x, int y, const SDL_Color &color)
 Put color at x, y.
static Uint32 getPixel (SDL_Surface *surface, int x, int y)
 Get pixel at x, y.
static void putPixel (SDL_Surface *surface, int x, int y, Uint32 pixel)
 Put pixel at x, y.
static Uint32 unpackPixel (Uint8 bpp, Uint8 *p)
 Decodes pixel from memory.
static void packPixel (Uint8 bpp, Uint8 *p, Uint32 pixel)
 Encodes pixel to memory.


Member Function Documentation

bool PixelTool::colorEquals const SDL_Color &  color1,
const SDL_Color &  color2
[static]
 

Compare colors.

NOTE: aplha values are ignored

Definition at line 21 of file PixelTool.cpp.

00022 {
00023     return color1.r == color2.r
00024         && color1.g == color2.g
00025         && color1.b == color2.b;
00026 }

Uint32 PixelTool::convertColor SDL_PixelFormat *  format,
const SDL_Color &  color
[static]
 

Definition at line 29 of file PixelTool.cpp.

00030 {
00031     return SDL_MapRGB(format, color.r, color.g, color.b);
00032 }

SDL_Color PixelTool::getColor SDL_Surface *  surface,
int  x,
int  y
[static]
 

Get color at x, y.

Surface must be locked.

Returns:
color

Definition at line 40 of file PixelTool.cpp.

00041 {
00042     SDL_Color color;
00043     SDL_GetRGBA(getPixel(surface, x, y), surface->format,
00044             &color.r, &color.g, &color.b, &color.unused);
00045     return color;
00046 }

Uint32 PixelTool::getPixel SDL_Surface *  surface,
int  x,
int  y
[static]
 

Get pixel at x, y.

Surface must be locked.

Returns:
pixel

Definition at line 68 of file PixelTool.cpp.

00069 {
00070     assert((0 <= x && x < surface->w) && (0 <= y && y < surface->h));
00071 
00072     int bpp = surface->format->BytesPerPixel;
00073     Uint8 *p = static_cast<Uint8*>(surface->pixels) + y * surface->pitch
00074         + x * bpp;
00075 
00076     return unpackPixel(bpp, p);
00077 }

void PixelTool::packPixel Uint8  bpp,
Uint8 *  p,
Uint32  pixel
[static]
 

Encodes pixel to memory.

Parameters:
bpp color depth (8, 16, 24, 32)
p pointer to the memory
pixel prepared pixel in bpp color depth
Exceptions:
LogicException for unknown color depth

Definition at line 133 of file PixelTool.cpp.

00134 {
00135     assert(p != NULL);
00136 
00137     switch(bpp) {
00138         case 1:
00139             *p = pixel;
00140             break;
00141         case 2:
00142             *reinterpret_cast<Uint16*>(p) = pixel;
00143             break;
00144         case 3:
00145             if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
00146                 p[0] = (pixel >> 16) & 0xff;
00147                 p[1] = (pixel >> 8) & 0xff;
00148                 p[2] = pixel & 0xff;
00149             } else {
00150                 p[0] = pixel & 0xff;
00151                 p[1] = (pixel >> 8) & 0xff;
00152                 p[2] = (pixel >> 16) & 0xff;
00153             }
00154             break;
00155         case 4:
00156             *reinterpret_cast<Uint32*>(p) = pixel;
00157             break;
00158         default:
00159             throw LogicException(ExInfo("unknown color depth")
00160                     .addInfo("bpp", bpp));
00161     }
00162 }

void PixelTool::putColor SDL_Surface *  surface,
int  x,
int  y,
const SDL_Color &  color
[static]
 

Put color at x, y.

Surface must be locked. TODO: support alpha values

Definition at line 54 of file PixelTool.cpp.

00056 {
00057     Uint32 pixel = SDL_MapRGBA(surface->format,
00058             color.r, color.g, color.b, color.unused);
00059     putPixel(surface, x, y, pixel);
00060 }

void PixelTool::putPixel SDL_Surface *  surface,
int  x,
int  y,
Uint32  pixel
[static]
 

Put pixel at x, y.

Surface must be locked.

Definition at line 84 of file PixelTool.cpp.

00085 {
00086     if ((0 <= x && x < surface->w) && (0 <= y && y < surface->h)) {
00087         int bpp = surface->format->BytesPerPixel;
00088         Uint8 *p = static_cast<Uint8*>(surface->pixels) + y * surface->pitch
00089             + x * bpp;
00090 
00091         packPixel(bpp, p, pixel);
00092     }
00093 }

Uint32 PixelTool::unpackPixel Uint8  bpp,
Uint8 *  p
[static]
 

Decodes pixel from memory.

Parameters:
bpp color depth (8, 16, 24, 32)
p pointer to the memory
Returns:
pixel in bpp color depth
Exceptions:
LogicException for unknown color depth

Definition at line 103 of file PixelTool.cpp.

00104 {
00105     switch(bpp) {
00106         case 1: // 8bit
00107             return *p;
00108         case 2: // 16bit 
00109             return *reinterpret_cast<Uint16*>(p);
00110         case 3: // 24bit 
00111             if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
00112                 return p[0] << 16 | p[1] << 8 | p[2];
00113             }
00114             else {
00115                 return p[0] | p[1] << 8 | p[2] << 16;
00116             }
00117         case 4: // 32 bit
00118             return *reinterpret_cast<Uint32*>(p);
00119         default:
00120             throw LogicException(ExInfo("unknown color depth")
00121                     .addInfo("bpp", bpp));
00122     }
00123 }


The documentation for this class was generated from the following files:
Generated on Wed Jun 1 09:56:15 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2