From b22395ec4de60326eb629df882b4b8781dbca15c Mon Sep 17 00:00:00 2001 From: manuel Date: Fri, 1 May 2009 14:54:45 +0200 Subject: - removed runtime generated output files - implemented brightness() - implemented invert() - implemented getPixel in CPixelFormat_BGR24 and CPixelFormat_BGR555 --- ue2/imgsynth2/cbitmap.cpp | 151 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 114 insertions(+), 37 deletions(-) (limited to 'ue2/imgsynth2/cbitmap.cpp') diff --git a/ue2/imgsynth2/cbitmap.cpp b/ue2/imgsynth2/cbitmap.cpp index b9cfc62..a064130 100644 --- a/ue2/imgsynth2/cbitmap.cpp +++ b/ue2/imgsynth2/cbitmap.cpp @@ -5,6 +5,7 @@ * @date 17.04.2009 */ +#include #include #include #include "cbitmap.h" @@ -25,6 +26,11 @@ CBitmap::~CBitmap() for (it = m_handlers.begin(); it != m_handlers.end(); it++) delete *it; m_pixelformat = NULL; + + /* delete colortable content */ + map::iterator it2; + for (it2 = m_colortable.begin(); it2 != m_colortable.end(); it2++) + delete (*it2).second; } /*----------------------------------------------------------------------------*/ @@ -36,6 +42,8 @@ void CBitmap::callFunc(const std::string& func, const std::list& pa if (func == "fillrect") fillrect(params); + else if (func == "brightness") + brightness(params); else if (func == "mirror_x") mirror_x(params); else if (func == "mirror_y") @@ -80,17 +88,22 @@ void CBitmap::fillrect(std::list params) throw FileError("At least one x/y-parameter is out of range."); /* check parameter values are in range */ - unsigned int max[3]; - m_pixelformat->getMaxColor(&max[0], &max[1], &max[2]); - if (pparams[4] < 0 || pparams[4] > max[0] - || pparams[5] < 0 || pparams[5] > max[1] - || pparams[6] < 0 || pparams[6] > max[2]) + CPixelFormat::RGBPIXEL pixel; + m_pixelformat->getMaxColor(pixel); + if (pparams[4] < 0 || pparams[4] > pixel.red + || pparams[5] < 0 || pparams[5] > pixel.green + || pparams[6] < 0 || pparams[6] > pixel.blue) throw FileError("At least one pixel color parameter is out of range."); if (pparams[2] < 0 || pparams[2] + pparams[0] > getWidth() || pparams[3] < 0 || pparams[3] + pparams[1] > getHeight()) throw FileError("At least one w/h-parameter is out of range."); + /* new pixel data */ + pixel.red = pparams[4]; + pixel.green = pparams[5]; + pixel.blue = pparams[6]; + /* call setPixel for every pixel in the rectangel */ for(uint32_t i = pparams[0]; i < pparams[2] + pparams[0]; i++) { @@ -98,7 +111,7 @@ void CBitmap::fillrect(std::list params) { try { - m_pixelformat->setPixel(&pparams[4], i, j); + m_pixelformat->setPixel(pixel, i, j); } catch(CPixelFormat::PixelFormatError& ex) { @@ -114,7 +127,6 @@ void CBitmap::fillrect(std::list params) /*----------------------------------------------------------------------------*/ -/* TODO */ void CBitmap::invert(std::list params) { /* check prerequirements */ @@ -125,49 +137,114 @@ void CBitmap::invert(std::list params) if (m_pixeldata == NULL || m_pixelformat == NULL) return; - /* TODO pixelwidth */ - unsigned int pixelwidth = m_pixelformat->getBitCount()/8; - - /* calc rowsize - boundary is 32 */ - uint32_t rowsize = 4 * static_cast( - ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32 - ); - - for(uint32_t i = 0; i < getHeight(); i++) + CPixelFormat::RGBPIXEL pixel; + CPixelFormat::RGBPIXEL max; + m_pixelformat->getMaxColor(max); + if (hasColorTable()) { - for(uint32_t j = 0; j <= getWidth(); j++) + /* invert every entry in the colortable */ + map::iterator it; + for (it = m_colortable.begin(); it != m_colortable.end(); it++) { - /*TODO cout << j << endl;*/ - break; + (*it).second->red = max.red - (*it).second->red; + (*it).second->green = max.green - (*it).second->green; + (*it).second->blue = max.blue - (*it).second->blue; } } - -#if 0 - uint32_t offset = i * rowsize; - - for(uint32_t j = 0; j <= getWidth()/2; j++) + else + { + /* invert per pixel */ + for(uint32_t y = 0; y < getHeight(); y++) { - uint32_t poffset = offset + j * pixelwidth; - uint32_t pbackset = offset + getWidth() * pixelwidth - j * pixelwidth; - - /* boundary check */ - if (pbackset > m_infoheader.biSizeImage) - throw FileError("Mirrored pixel position is out of range."); - - /* mirroring, backup right data first */ - copy(m_pixeldata + pbackset - pixelwidth, m_pixeldata + pbackset, buf); - copy(m_pixeldata + poffset, m_pixeldata + poffset + pixelwidth, m_pixeldata + pbackset - pixelwidth); - copy(buf, buf + pixelwidth, m_pixeldata + poffset); + for(uint32_t x = 0; x < getWidth(); x++) + { + try + { + m_pixelformat->getPixel(pixel, x, y); + pixel.red = max.red - pixel.red; + pixel.green = max.green - pixel.green; + pixel.blue = max.blue - pixel.blue; + m_pixelformat->setPixel(pixel, x, y); + } + catch(CPixelFormat::PixelFormatError& ex) + { + stringstream errstr; + errstr << "Can't invert pixel (pos=[" << x << "," << y << "]): " + << ex.what(); + throw FileError(errstr.str()); + } + } } } -#endif } /*----------------------------------------------------------------------------*/ -/* TODO */ void CBitmap::brightness(std::list params) { + /* check prerequirements */ + if (params.size() != 1) + throw FileError("Invalid number of function parameters (must be 1)."); + + /* do nothing if no pixel exists */ + if (m_pixeldata == NULL || m_pixelformat == NULL) + return; + + /* convert parameters */ + float factor; + try + { + factor = boost::lexical_cast(params.front()); + params.pop_front(); + } + catch(boost::bad_lexical_cast& ex) + { + throw FileError("Invalid parameter (" + params.front() + ")."); + } + + /* negative factor doesn't make sense */ + if (factor < 0) + throw FileError("Brightness parameter must be positive."); + + CPixelFormat::RGBPIXEL pixel; + CPixelFormat::RGBPIXEL max; + m_pixelformat->getMaxColor(max); + if (hasColorTable()) + { + /* invert every entry in the colortable */ + map::iterator it; + for (it = m_colortable.begin(); it != m_colortable.end(); it++) + { + (*it).second->red = min(max.red, static_cast((*it).second->red * factor)); + (*it).second->green = min(max.green, static_cast((*it).second->green * factor)); + (*it).second->blue = min(max.blue, static_cast((*it).second->blue * factor)); + } + } + else + { + /* invert per pixel */ + for(uint32_t y = 0; y < getHeight(); y++) + { + for(uint32_t x = 0; x < getWidth(); x++) + { + try + { + m_pixelformat->getPixel(pixel, x, y); + pixel.red = min(max.red, static_cast(pixel.red * factor)); + pixel.green = min(max.green, static_cast(pixel.green * factor)); + pixel.blue = min(max.blue, static_cast(pixel.blue * factor)); + m_pixelformat->setPixel(pixel, x, y); + } + catch(CPixelFormat::PixelFormatError& ex) + { + stringstream errstr; + errstr << "Can't invert pixel (pos=[" << x << "," << y << "]): " + << ex.what(); + throw FileError(errstr.str()); + } + } + } + } } /*----------------------------------------------------------------------------*/ -- cgit v1.2.3