summaryrefslogtreecommitdiffstats
path: root/ue2/imgsynth2/cbitmap.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ue2/imgsynth2/cbitmap.cpp')
-rw-r--r--ue2/imgsynth2/cbitmap.cpp172
1 files changed, 126 insertions, 46 deletions
diff --git a/ue2/imgsynth2/cbitmap.cpp b/ue2/imgsynth2/cbitmap.cpp
index acee870..ed26600 100644
--- a/ue2/imgsynth2/cbitmap.cpp
+++ b/ue2/imgsynth2/cbitmap.cpp
@@ -5,6 +5,7 @@
5 * @date 17.04.2009 5 * @date 17.04.2009
6 */ 6 */
7 7
8#include <algorithm>
8#include <boost/lexical_cast.hpp> 9#include <boost/lexical_cast.hpp>
9#include <boost/numeric/conversion/cast.hpp> 10#include <boost/numeric/conversion/cast.hpp>
10#include "cbitmap.h" 11#include "cbitmap.h"
@@ -25,14 +26,20 @@ CBitmap::~CBitmap()
25 for (it = m_handlers.begin(); it != m_handlers.end(); it++) 26 for (it = m_handlers.begin(); it != m_handlers.end(); it++)
26 delete *it; 27 delete *it;
27 m_pixelformat = NULL; 28 m_pixelformat = NULL;
28 29
29 /* delete color table */ 30 /* delete colortable content */
30 map<string, map<string, uint32_t* > >::iterator it1; 31 //TODO
31 map<string, uint32_t* >::iterator it2; 32 /*map<string, CPixelFormat::RGBPIXEL *>::iterator it2;
32 for ( it1= xpmColors.begin() ; it1 != xpmColors.end(); it1++ ) 33 for (it2 = m_colortable.begin(); it2 != m_colortable.end(); it2++)
33 for ( it2=(*it1).second.begin() ; it2 != (*it1).second.end(); it2++ ) 34 delete (*it2).second;*/
34 delete [] (*it2).second; 35
35 36 map<string, map<string, uint32_t *> >::iterator it1;
37 map<string, uint32_t *>::iterator it2;
38 for(it1 = xpmColors.begin(); it1 != xpmColors.end(); it1++)
39 {
40 for(it2 = (*it1).second.begin(); it2 != (*it1).second.end(); it2++)
41 delete[] (*it2).second;
42 }
36} 43}
37 44
38/*----------------------------------------------------------------------------*/ 45/*----------------------------------------------------------------------------*/
@@ -44,6 +51,8 @@ void CBitmap::callFunc(const std::string& func, const std::list<std::string>& pa
44 51
45 if (func == "fillrect") 52 if (func == "fillrect")
46 fillrect(params); 53 fillrect(params);
54 else if (func == "brightness")
55 brightness(params);
47 else if (func == "mirror_x") 56 else if (func == "mirror_x")
48 mirror_x(params); 57 mirror_x(params);
49 else if (func == "mirror_y") 58 else if (func == "mirror_y")
@@ -88,17 +97,22 @@ void CBitmap::fillrect(std::list<std::string> params)
88 throw FileError("At least one x/y-parameter is out of range."); 97 throw FileError("At least one x/y-parameter is out of range.");
89 98
90 /* check parameter values are in range */ 99 /* check parameter values are in range */
91 unsigned int max[3]; 100 CPixelFormat::RGBPIXEL pixel;
92 m_pixelformat->getMaxColor(&max[0], &max[1], &max[2]); 101 m_pixelformat->getMaxColor(pixel);
93 if (pparams[4] < 0 || pparams[4] > max[0] 102 if (pparams[4] < 0 || pparams[4] > pixel.red
94 || pparams[5] < 0 || pparams[5] > max[1] 103 || pparams[5] < 0 || pparams[5] > pixel.green
95 || pparams[6] < 0 || pparams[6] > max[2]) 104 || pparams[6] < 0 || pparams[6] > pixel.blue)
96 throw FileError("At least one pixel color parameter is out of range."); 105 throw FileError("At least one pixel color parameter is out of range.");
97 106
98 if (pparams[2] < 0 || pparams[2] + pparams[0] > getWidth() 107 if (pparams[2] < 0 || pparams[2] + pparams[0] > getWidth()
99 || pparams[3] < 0 || pparams[3] + pparams[1] > getHeight()) 108 || pparams[3] < 0 || pparams[3] + pparams[1] > getHeight())
100 throw FileError("At least one w/h-parameter is out of range."); 109 throw FileError("At least one w/h-parameter is out of range.");
101 110
111 /* new pixel data */
112 pixel.red = pparams[4];
113 pixel.green = pparams[5];
114 pixel.blue = pparams[6];
115
102 /* call setPixel for every pixel in the rectangel */ 116 /* call setPixel for every pixel in the rectangel */
103 for(uint32_t i = pparams[0]; i < pparams[2] + pparams[0]; i++) 117 for(uint32_t i = pparams[0]; i < pparams[2] + pparams[0]; i++)
104 { 118 {
@@ -106,7 +120,7 @@ void CBitmap::fillrect(std::list<std::string> params)
106 { 120 {
107 try 121 try
108 { 122 {
109 m_pixelformat->setPixel(&pparams[4], i, j); 123 m_pixelformat->setPixel(pixel, i, j);
110 } 124 }
111 catch(CPixelFormat::PixelFormatError& ex) 125 catch(CPixelFormat::PixelFormatError& ex)
112 { 126 {
@@ -122,7 +136,6 @@ void CBitmap::fillrect(std::list<std::string> params)
122 136
123/*----------------------------------------------------------------------------*/ 137/*----------------------------------------------------------------------------*/
124 138
125/* TODO */
126void CBitmap::invert(std::list<std::string> params) 139void CBitmap::invert(std::list<std::string> params)
127{ 140{
128 /* check prerequirements */ 141 /* check prerequirements */
@@ -133,49 +146,116 @@ void CBitmap::invert(std::list<std::string> params)
133 if (m_pixeldata == NULL || m_pixelformat == NULL) 146 if (m_pixeldata == NULL || m_pixelformat == NULL)
134 return; 147 return;
135 148
136 /* TODO pixelwidth */ 149 CPixelFormat::RGBPIXEL pixel;
137 unsigned int pixelwidth = m_pixelformat->getBitCount()/8; 150 CPixelFormat::RGBPIXEL max;
138 151 m_pixelformat->getMaxColor(max);
139 /* calc rowsize - boundary is 32 */ 152 if (hasColorTable())
140 uint32_t rowsize = 4 * static_cast<uint32_t>(
141 ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32
142 );
143
144 for(uint32_t i = 0; i < getHeight(); i++)
145 { 153 {
146 for(uint32_t j = 0; j <= getWidth(); j++) 154 /* invert every entry in the colortable */
155 map<string, CPixelFormat::RGBPIXEL *>::iterator it;
156 //TODO
157 /*for (it = m_colortable.begin(); it != m_colortable.end(); it++)
147 { 158 {
148 /*TODO cout << j << endl;*/ 159 (*it).second->red = max.red - (*it).second->red;
149 break; 160 (*it).second->green = max.green - (*it).second->green;
150 } 161 (*it).second->blue = max.blue - (*it).second->blue;
162 }*/
151 } 163 }
152 164 else
153#if 0 165 {
154 uint32_t offset = i * rowsize; 166 /* invert per pixel */
155 167 for(uint32_t y = 0; y < getHeight(); y++)
156 for(uint32_t j = 0; j <= getWidth()/2; j++)
157 { 168 {
158 uint32_t poffset = offset + j * pixelwidth; 169 for(uint32_t x = 0; x < getWidth(); x++)
159 uint32_t pbackset = offset + getWidth() * pixelwidth - j * pixelwidth; 170 {
160 171 try
161 /* boundary check */ 172 {
162 if (pbackset > m_infoheader.biSizeImage) 173 m_pixelformat->getPixel(pixel, x, y);
163 throw FileError("Mirrored pixel position is out of range."); 174 pixel.red = max.red - pixel.red;
164 175 pixel.green = max.green - pixel.green;
165 /* mirroring, backup right data first */ 176 pixel.blue = max.blue - pixel.blue;
166 copy(m_pixeldata + pbackset - pixelwidth, m_pixeldata + pbackset, buf); 177 m_pixelformat->setPixel(pixel, x, y);
167 copy(m_pixeldata + poffset, m_pixeldata + poffset + pixelwidth, m_pixeldata + pbackset - pixelwidth); 178 }
168 copy(buf, buf + pixelwidth, m_pixeldata + poffset); 179 catch(CPixelFormat::PixelFormatError& ex)
180 {
181 stringstream errstr;
182 errstr << "Can't invert pixel (pos=[" << x << "," << y << "]): "
183 << ex.what();
184 throw FileError(errstr.str());
185 }
186 }
169 } 187 }
170 } 188 }
171#endif
172} 189}
173 190
174/*----------------------------------------------------------------------------*/ 191/*----------------------------------------------------------------------------*/
175 192
176/* TODO */
177void CBitmap::brightness(std::list<std::string> params) 193void CBitmap::brightness(std::list<std::string> params)
178{ 194{
195 /* check prerequirements */
196 if (params.size() != 1)
197 throw FileError("Invalid number of function parameters (must be 1).");
198
199 /* do nothing if no pixel exists */
200 if (m_pixeldata == NULL || m_pixelformat == NULL)
201 return;
202
203 /* convert parameters */
204 float factor;
205 try
206 {
207 factor = boost::lexical_cast<float>(params.front());
208 params.pop_front();
209 }
210 catch(boost::bad_lexical_cast& ex)
211 {
212 throw FileError("Invalid parameter (" + params.front() + ").");
213 }
214
215 /* negative factor doesn't make sense */
216 if (factor < 0)
217 throw FileError("Brightness parameter must be positive.");
218
219 CPixelFormat::RGBPIXEL pixel;
220 CPixelFormat::RGBPIXEL max;
221 m_pixelformat->getMaxColor(max);
222 if (hasColorTable())
223 {
224 /* invert every entry in the colortable */
225 map<string, CPixelFormat::RGBPIXEL *>::iterator it;
226 //TODO
227 /*for (it = m_colortable.begin(); it != m_colortable.end(); it++)
228 {
229 (*it).second->red = min(max.red, static_cast<uint32_t>((*it).second->red * factor));
230 (*it).second->green = min(max.green, static_cast<uint32_t>((*it).second->green * factor));
231 (*it).second->blue = min(max.blue, static_cast<uint32_t>((*it).second->blue * factor));
232 }*/
233 }
234 else
235 {
236 /* invert per pixel */
237 for(uint32_t y = 0; y < getHeight(); y++)
238 {
239 for(uint32_t x = 0; x < getWidth(); x++)
240 {
241 try
242 {
243 m_pixelformat->getPixel(pixel, x, y);
244 pixel.red = min(max.red, static_cast<uint32_t>(pixel.red * factor));
245 pixel.green = min(max.green, static_cast<uint32_t>(pixel.green * factor));
246 pixel.blue = min(max.blue, static_cast<uint32_t>(pixel.blue * factor));
247 m_pixelformat->setPixel(pixel, x, y);
248 }
249 catch(CPixelFormat::PixelFormatError& ex)
250 {
251 stringstream errstr;
252 errstr << "Can't invert pixel (pos=[" << x << "," << y << "]): "
253 << ex.what();
254 throw FileError(errstr.str());
255 }
256 }
257 }
258 }
179} 259}
180 260
181/*----------------------------------------------------------------------------*/ 261/*----------------------------------------------------------------------------*/