diff options
Diffstat (limited to 'ue2/imgsynth2/cbitmap.cpp')
| -rw-r--r-- | ue2/imgsynth2/cbitmap.cpp | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/ue2/imgsynth2/cbitmap.cpp b/ue2/imgsynth2/cbitmap.cpp new file mode 100644 index 0000000..82ba4ae --- /dev/null +++ b/ue2/imgsynth2/cbitmap.cpp | |||
| @@ -0,0 +1,253 @@ | |||
| 1 | /** | ||
| 2 | * @module cbitmap | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief Abstract implementation of CFile handling Bitmaps. | ||
| 5 | * @date 17.04.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <boost/lexical_cast.hpp> | ||
| 9 | #include <boost/numeric/conversion/cast.hpp> | ||
| 10 | #include "cbitmap.h" | ||
| 11 | |||
| 12 | using namespace std; | ||
| 13 | |||
| 14 | /*----------------------------------------------------------------------------*/ | ||
| 15 | |||
| 16 | CBitmap::~CBitmap() | ||
| 17 | { | ||
| 18 | /* delete pixeldata */ | ||
| 19 | if (m_pixeldata != NULL) | ||
| 20 | delete[] m_pixeldata; | ||
| 21 | m_pixeldata = NULL; | ||
| 22 | |||
| 23 | /* delete pixelformat handlers */ | ||
| 24 | set<CPixelFormat *>::iterator it; | ||
| 25 | for (it = m_handlers.begin(); it != m_handlers.end(); it++) | ||
| 26 | delete *it; | ||
| 27 | m_pixelformat = NULL; | ||
| 28 | } | ||
| 29 | |||
| 30 | /*----------------------------------------------------------------------------*/ | ||
| 31 | |||
| 32 | void CBitmap::callFunc(const std::string& func, const std::list<std::string>& params) | ||
| 33 | { | ||
| 34 | if (func.empty()) | ||
| 35 | throw FileError("Function name is empty."); | ||
| 36 | |||
| 37 | if (func == "fillrect") | ||
| 38 | fillrect(params); | ||
| 39 | else if (func == "mirror_x") | ||
| 40 | mirror_x(params); | ||
| 41 | else if (func == "mirror_y") | ||
| 42 | mirror_y(params); | ||
| 43 | else if (func == "invert") | ||
| 44 | invert(params); | ||
| 45 | else | ||
| 46 | throw FileError("Unknown function '" + func + "'."); | ||
| 47 | } | ||
| 48 | |||
| 49 | /*----------------------------------------------------------------------------*/ | ||
| 50 | |||
| 51 | void CBitmap::fillrect(std::list<std::string> params) | ||
| 52 | { | ||
| 53 | /* check prerequirements */ | ||
| 54 | if (params.size() != 7) | ||
| 55 | throw FileError("Invalid number of function parameters (must be 7)."); | ||
| 56 | |||
| 57 | /* do nothing if no pixel exists */ | ||
| 58 | if (m_pixeldata == NULL || m_pixelformat == NULL) | ||
| 59 | return; | ||
| 60 | |||
| 61 | /* convert parameters */ | ||
| 62 | uint32_t pparams[7]; | ||
| 63 | int i = 0; | ||
| 64 | try | ||
| 65 | { | ||
| 66 | for(i = 0; i < 7; i++) | ||
| 67 | { | ||
| 68 | pparams[i] = boost::lexical_cast<uint32_t>(params.front()); | ||
| 69 | params.pop_front(); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | catch(boost::bad_lexical_cast& ex) | ||
| 73 | { | ||
| 74 | throw FileError("Invalid parameter (" + params.front() + ")."); | ||
| 75 | } | ||
| 76 | |||
| 77 | /* check parameter values are in range */ | ||
| 78 | if (pparams[0] < 0 || pparams[0] > getWidth() | ||
| 79 | || pparams[1] < 0 || pparams[1] > getHeight()) | ||
| 80 | throw FileError("At least one x/y-parameter is out of range."); | ||
| 81 | |||
| 82 | /* check parameter values are in range */ | ||
| 83 | unsigned int max[3]; | ||
| 84 | m_pixelformat->getMaxColor(&max[0], &max[1], &max[2]); | ||
| 85 | if (pparams[4] < 0 || pparams[4] > max[0] | ||
| 86 | || pparams[5] < 0 || pparams[5] > max[1] | ||
| 87 | || pparams[6] < 0 || pparams[6] > max[2]) | ||
| 88 | throw FileError("At least one pixel color parameter is out of range."); | ||
| 89 | |||
| 90 | if (pparams[2] < 0 || pparams[2] + pparams[0] > getWidth() | ||
| 91 | || pparams[3] < 0 || pparams[3] + pparams[1] > getHeight()) | ||
| 92 | throw FileError("At least one w/h-parameter is out of range."); | ||
| 93 | |||
| 94 | /* call setPixel for every pixel in the rectangel */ | ||
| 95 | for(uint32_t i = pparams[0]; i < pparams[2] + pparams[0]; i++) | ||
| 96 | { | ||
| 97 | for(uint32_t j = pparams[1]; j < pparams[3] + pparams[1]; j++) | ||
| 98 | { | ||
| 99 | try | ||
| 100 | { | ||
| 101 | m_pixelformat->setPixel(&pparams[4], i, j); | ||
| 102 | } | ||
| 103 | catch(CPixelFormat::PixelFormatError& ex) | ||
| 104 | { | ||
| 105 | stringstream errstr; | ||
| 106 | errstr << "Can't set pixel (pos=[" << i << "," << j << "] col=[" | ||
| 107 | << pparams[4] << "," << pparams[5] << "," << pparams[6] << "]): " | ||
| 108 | << ex.what(); | ||
| 109 | throw FileError(errstr.str()); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | /*----------------------------------------------------------------------------*/ | ||
| 116 | |||
| 117 | /* TODO */ | ||
| 118 | void CBitmap::invert(std::list<std::string> params) | ||
| 119 | { | ||
| 120 | /* check prerequirements */ | ||
| 121 | if (params.size() != 0) | ||
| 122 | throw FileError("Invalid number of function parameters (must be 0)."); | ||
| 123 | |||
| 124 | /* do nothing if no pixel exists */ | ||
| 125 | if (m_pixeldata == NULL || m_pixelformat == NULL) | ||
| 126 | return; | ||
| 127 | |||
| 128 | /* TODO pixelwidth */ | ||
| 129 | unsigned int pixelwidth = m_pixelformat->getBitCount()/8; | ||
| 130 | |||
| 131 | /* calc rowsize - boundary is 32 */ | ||
| 132 | uint32_t rowsize = 4 * static_cast<uint32_t>( | ||
| 133 | ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32 | ||
| 134 | ); | ||
| 135 | |||
| 136 | for(uint32_t i = 0; i < getHeight(); i++) | ||
| 137 | { | ||
| 138 | for(uint32_t j = 0; j <= getWidth(); j++) | ||
| 139 | { | ||
| 140 | /*TODO cout << j << endl;*/ | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | #if 0 | ||
| 146 | uint32_t offset = i * rowsize; | ||
| 147 | |||
| 148 | for(uint32_t j = 0; j <= getWidth()/2; j++) | ||
| 149 | { | ||
| 150 | uint32_t poffset = offset + j * pixelwidth; | ||
| 151 | uint32_t pbackset = offset + getWidth() * pixelwidth - j * pixelwidth; | ||
| 152 | |||
| 153 | /* boundary check */ | ||
| 154 | if (pbackset > m_infoheader.biSizeImage) | ||
| 155 | throw FileError("Mirrored pixel position is out of range."); | ||
| 156 | |||
| 157 | /* mirroring, backup right data first */ | ||
| 158 | copy(m_pixeldata + pbackset - pixelwidth, m_pixeldata + pbackset, buf); | ||
| 159 | copy(m_pixeldata + poffset, m_pixeldata + poffset + pixelwidth, m_pixeldata + pbackset - pixelwidth); | ||
| 160 | copy(buf, buf + pixelwidth, m_pixeldata + poffset); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | #endif | ||
| 164 | } | ||
| 165 | |||
| 166 | /*----------------------------------------------------------------------------*/ | ||
| 167 | |||
| 168 | /* TODO */ | ||
| 169 | void CBitmap::brightness(std::list<std::string> params) | ||
| 170 | { | ||
| 171 | } | ||
| 172 | |||
| 173 | /*----------------------------------------------------------------------------*/ | ||
| 174 | |||
| 175 | void CBitmap::mirror_y(std::list<std::string> params) | ||
| 176 | { | ||
| 177 | /* check prerequirements */ | ||
| 178 | if (params.size() != 0) | ||
| 179 | throw FileError("Invalid number of function parameters (must be 0)."); | ||
| 180 | |||
| 181 | /* do nothing if no pixel exists */ | ||
| 182 | if (m_pixeldata == NULL || m_pixelformat == NULL) | ||
| 183 | return; | ||
| 184 | |||
| 185 | /* calc rowsize - boundary is 32 */ | ||
| 186 | uint32_t rowsize = 4 * static_cast<uint32_t>( | ||
| 187 | ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32 | ||
| 188 | ); | ||
| 189 | |||
| 190 | uint8_t *buf = new uint8_t[rowsize]; | ||
| 191 | for(uint32_t i = 0; i < getHeight()/2; i++) | ||
| 192 | { | ||
| 193 | uint32_t j = getHeight() - i - 1; | ||
| 194 | uint32_t offset = i * rowsize; | ||
| 195 | uint32_t backset = j * rowsize; | ||
| 196 | |||
| 197 | /* boundary check */ | ||
| 198 | if (offset + rowsize > getPixelDataSize() | ||
| 199 | || backset + rowsize > getPixelDataSize()) | ||
| 200 | throw FileError("Mirrored pixel position is out of range."); | ||
| 201 | |||
| 202 | /* mirroring, backup lower data first */ | ||
| 203 | copy(m_pixeldata + backset, m_pixeldata + backset + rowsize, buf); | ||
| 204 | copy(m_pixeldata + offset, m_pixeldata + offset + rowsize, m_pixeldata + backset); | ||
| 205 | copy(buf, buf + rowsize, m_pixeldata + offset); | ||
| 206 | } | ||
| 207 | delete[] buf; | ||
| 208 | } | ||
| 209 | |||
| 210 | /*----------------------------------------------------------------------------*/ | ||
| 211 | |||
| 212 | void CBitmap::mirror_x(std::list<std::string> params) | ||
| 213 | { | ||
| 214 | /* check prerequirements */ | ||
| 215 | if (params.size() != 0) | ||
| 216 | throw FileError("Invalid number of function parameters (must be 0)."); | ||
| 217 | |||
| 218 | /* do nothing if no pixel exists */ | ||
| 219 | if (m_pixeldata == NULL || m_pixelformat == NULL) | ||
| 220 | return; | ||
| 221 | |||
| 222 | /* calc rowsize - boundary is 32 */ | ||
| 223 | uint32_t rowsize = 4 * static_cast<uint32_t>( | ||
| 224 | ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32 | ||
| 225 | ); | ||
| 226 | |||
| 227 | /* calc pixelwidth */ | ||
| 228 | unsigned int pixelwidth = m_pixelformat->getBitCount()/8; | ||
| 229 | |||
| 230 | uint8_t *buf = new uint8_t[pixelwidth]; | ||
| 231 | for(uint32_t i = 0; i < getHeight(); i++) | ||
| 232 | { | ||
| 233 | uint32_t offset = i * rowsize; | ||
| 234 | |||
| 235 | for(uint32_t j = 0; j <= getWidth()/2; j++) | ||
| 236 | { | ||
| 237 | uint32_t poffset = offset + j * pixelwidth; | ||
| 238 | uint32_t pbackset = offset + getWidth() * pixelwidth - j * pixelwidth; | ||
| 239 | |||
| 240 | /* boundary check */ | ||
| 241 | if (pbackset > getPixelDataSize()) | ||
| 242 | throw FileError("Mirrored pixel position is out of range."); | ||
| 243 | |||
| 244 | /* mirroring, backup right data first */ | ||
| 245 | copy(m_pixeldata + pbackset - pixelwidth, m_pixeldata + pbackset, buf); | ||
| 246 | copy(m_pixeldata + poffset, m_pixeldata + poffset + pixelwidth, m_pixeldata + pbackset - pixelwidth); | ||
| 247 | copy(buf, buf + pixelwidth, m_pixeldata + poffset); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | delete[] buf; | ||
| 251 | } | ||
| 252 | |||
| 253 | /* vim: set et sw=2 ts=2: */ | ||
