| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "PixelTool.h" |
| 10 |
|
| 11 |
#include "LogicException.h" |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
|
| 15 |
//----------------------------------------------------------------- |
| 16 |
/** |
| 17 |
* Compare colors. |
| 18 |
* NOTE: aplha values are ignored |
| 19 |
*/ |
| 20 |
bool |
| 21 |
PixelTool::colorEquals(const SDL_Color &color1, const SDL_Color &color2) |
| 22 |
{ |
| 23 |
return color1.r == color2.r |
| 24 |
&& color1.g == color2.g |
| 25 |
&& color1.b == color2.b; |
| 26 |
} |
| 27 |
//----------------------------------------------------------------- |
| 28 |
Uint32 |
| 29 |
PixelTool::convertColor(SDL_PixelFormat *format, const SDL_Color &color) |
| 30 |
{ |
| 31 |
return SDL_MapRGB(format, color.r, color.g, color.b); |
| 32 |
} |
| 33 |
//----------------------------------------------------------------- |
| 34 |
/** |
| 35 |
* Get color at x, y. |
| 36 |
* Surface must be locked. |
| 37 |
* @return color |
| 38 |
*/ |
| 39 |
SDL_Color |
| 40 |
PixelTool::getColor(SDL_Surface *surface, int x, int y) |
| 41 |
{ |
| 42 |
SDL_Color color; |
| 43 |
SDL_GetRGBA(getPixel(surface, x, y), surface->format, |
| 44 |
&color.r, &color.g, &color.b, &color.unused); |
| 45 |
return color; |
| 46 |
} |
| 47 |
//----------------------------------------------------------------- |
| 48 |
/** |
| 49 |
* Put color at x, y. |
| 50 |
* Surface must be locked. |
| 51 |
* TODO: support alpha values |
| 52 |
*/ |
| 53 |
void |
| 54 |
PixelTool::putColor(SDL_Surface *surface, int x, int y, |
| 55 |
const SDL_Color &color) |
| 56 |
{ |
| 57 |
Uint32 pixel = SDL_MapRGBA(surface->format, |
| 58 |
color.r, color.g, color.b, color.unused); |
| 59 |
putPixel(surface, x, y, pixel); |
| 60 |
} |
| 61 |
//----------------------------------------------------------------- |
| 62 |
/** |
| 63 |
* Get pixel at x, y. |
| 64 |
* Surface must be locked. |
| 65 |
* @return pixel |
| 66 |
*/ |
| 67 |
Uint32 |
| 68 |
PixelTool::getPixel(SDL_Surface *surface, int x, int y) |
| 69 |
{ |
| 70 |
assert((0 <= x && x < surface->w) && (0 <= y && y < surface->h)); |
| 71 |
|
| 72 |
int bpp = surface->format->BytesPerPixel; |
| 73 |
Uint8 *p = static_cast<Uint8*>(surface->pixels) + y * surface->pitch |
| 74 |
+ x * bpp; |
| 75 |
|
| 76 |
return unpackPixel(bpp, p); |
| 77 |
} |
| 78 |
//----------------------------------------------------------------- |
| 79 |
/** |
| 80 |
* Put pixel at x, y. |
| 81 |
* Surface must be locked. |
| 82 |
*/ |
| 83 |
void |
| 84 |
PixelTool::putPixel(SDL_Surface *surface, int x, int y, Uint32 pixel) |
| 85 |
{ |
| 86 |
if ((0 <= x && x < surface->w) && (0 <= y && y < surface->h)) { |
| 87 |
int bpp = surface->format->BytesPerPixel; |
| 88 |
Uint8 *p = static_cast<Uint8*>(surface->pixels) + y * surface->pitch |
| 89 |
+ x * bpp; |
| 90 |
|
| 91 |
packPixel(bpp, p, pixel); |
| 92 |
} |
| 93 |
} |
| 94 |
//----------------------------------------------------------------- |
| 95 |
/** |
| 96 |
* Decodes pixel from memory. |
| 97 |
* @param bpp color depth (8, 16, 24, 32) |
| 98 |
* @param p pointer to the memory |
| 99 |
* @return pixel in bpp color depth |
| 100 |
* @throws LogicException for unknown color depth |
| 101 |
*/ |
| 102 |
Uint32 |
| 103 |
PixelTool::unpackPixel(Uint8 bpp, Uint8 *p) |
| 104 |
{ |
| 105 |
switch(bpp) { |
| 106 |
case 1: // 8bit |
| 107 |
return *p; |
| 108 |
case 2: // 16bit |
| 109 |
return *reinterpret_cast<Uint16*>(p); |
| 110 |
case 3: // 24bit |
| 111 |
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { |
| 112 |
return p[0] << 16 | p[1] << 8 | p[2]; |
| 113 |
} |
| 114 |
else { |
| 115 |
return p[0] | p[1] << 8 | p[2] << 16; |
| 116 |
} |
| 117 |
case 4: // 32 bit |
| 118 |
return *reinterpret_cast<Uint32*>(p); |
| 119 |
default: |
| 120 |
throw LogicException(ExInfo("unknown color depth") |
| 121 |
.addInfo("bpp", bpp)); |
| 122 |
} |
| 123 |
} |
| 124 |
//----------------------------------------------------------------- |
| 125 |
/** |
| 126 |
* Encodes pixel to memory. |
| 127 |
* @param bpp color depth (8, 16, 24, 32) |
| 128 |
* @param p pointer to the memory |
| 129 |
* @param pixel prepared pixel in bpp color depth |
| 130 |
* @throws LogicException for unknown color depth |
| 131 |
*/ |
| 132 |
void |
| 133 |
PixelTool::packPixel(Uint8 bpp, Uint8 *p, Uint32 pixel) |
| 134 |
{ |
| 135 |
assert(p != NULL); |
| 136 |
|
| 137 |
switch(bpp) { |
| 138 |
case 1: |
| 139 |
*p = pixel; |
| 140 |
break; |
| 141 |
case 2: |
| 142 |
*reinterpret_cast<Uint16*>(p) = pixel; |
| 143 |
break; |
| 144 |
case 3: |
| 145 |
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) { |
| 146 |
p[0] = (pixel >> 16) & 0xff; |
| 147 |
p[1] = (pixel >> 8) & 0xff; |
| 148 |
p[2] = pixel & 0xff; |
| 149 |
} else { |
| 150 |
p[0] = pixel & 0xff; |
| 151 |
p[1] = (pixel >> 8) & 0xff; |
| 152 |
p[2] = (pixel >> 16) & 0xff; |
| 153 |
} |
| 154 |
break; |
| 155 |
case 4: |
| 156 |
*reinterpret_cast<Uint32*>(p) = pixel; |
| 157 |
break; |
| 158 |
default: |
| 159 |
throw LogicException(ExInfo("unknown color depth") |
| 160 |
.addInfo("bpp", bpp)); |
| 161 |
} |
| 162 |
} |
| 163 |
|