From 5f499a8233c7bb68b52b8fdeddac9a06061ea4d7 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 28 Apr 2009 18:32:15 +0200 Subject: Moved a lot of stuff around to get abstract cbitmap working --- ue2/imgsynth2/cbitmap.cpp | 253 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 ue2/imgsynth2/cbitmap.cpp (limited to 'ue2/imgsynth2/cbitmap.cpp') 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 @@ +/** + * @module cbitmap + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Abstract implementation of CFile handling Bitmaps. + * @date 17.04.2009 + */ + +#include +#include +#include "cbitmap.h" + +using namespace std; + +/*----------------------------------------------------------------------------*/ + +CBitmap::~CBitmap() +{ + /* delete pixeldata */ + if (m_pixeldata != NULL) + delete[] m_pixeldata; + m_pixeldata = NULL; + + /* delete pixelformat handlers */ + set::iterator it; + for (it = m_handlers.begin(); it != m_handlers.end(); it++) + delete *it; + m_pixelformat = NULL; +} + +/*----------------------------------------------------------------------------*/ + +void CBitmap::callFunc(const std::string& func, const std::list& params) +{ + if (func.empty()) + throw FileError("Function name is empty."); + + if (func == "fillrect") + fillrect(params); + else if (func == "mirror_x") + mirror_x(params); + else if (func == "mirror_y") + mirror_y(params); + else if (func == "invert") + invert(params); + else + throw FileError("Unknown function '" + func + "'."); +} + +/*----------------------------------------------------------------------------*/ + +void CBitmap::fillrect(std::list params) +{ + /* check prerequirements */ + if (params.size() != 7) + throw FileError("Invalid number of function parameters (must be 7)."); + + /* do nothing if no pixel exists */ + if (m_pixeldata == NULL || m_pixelformat == NULL) + return; + + /* convert parameters */ + uint32_t pparams[7]; + int i = 0; + try + { + for(i = 0; i < 7; i++) + { + pparams[i] = boost::lexical_cast(params.front()); + params.pop_front(); + } + } + catch(boost::bad_lexical_cast& ex) + { + throw FileError("Invalid parameter (" + params.front() + ")."); + } + + /* check parameter values are in range */ + if (pparams[0] < 0 || pparams[0] > getWidth() + || pparams[1] < 0 || pparams[1] > getHeight()) + 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]) + 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."); + + /* call setPixel for every pixel in the rectangel */ + for(uint32_t i = pparams[0]; i < pparams[2] + pparams[0]; i++) + { + for(uint32_t j = pparams[1]; j < pparams[3] + pparams[1]; j++) + { + try + { + m_pixelformat->setPixel(&pparams[4], i, j); + } + catch(CPixelFormat::PixelFormatError& ex) + { + stringstream errstr; + errstr << "Can't set pixel (pos=[" << i << "," << j << "] col=[" + << pparams[4] << "," << pparams[5] << "," << pparams[6] << "]): " + << ex.what(); + throw FileError(errstr.str()); + } + } + } +} + +/*----------------------------------------------------------------------------*/ + +/* TODO */ +void CBitmap::invert(std::list params) +{ + /* check prerequirements */ + if (params.size() != 0) + throw FileError("Invalid number of function parameters (must be 0)."); + + /* do nothing if no pixel exists */ + 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++) + { + for(uint32_t j = 0; j <= getWidth(); j++) + { + /*TODO cout << j << endl;*/ + break; + } + } + +#if 0 + uint32_t offset = i * rowsize; + + for(uint32_t j = 0; j <= getWidth()/2; j++) + { + 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); + } + } +#endif +} + +/*----------------------------------------------------------------------------*/ + +/* TODO */ +void CBitmap::brightness(std::list params) +{ +} + +/*----------------------------------------------------------------------------*/ + +void CBitmap::mirror_y(std::list params) +{ + /* check prerequirements */ + if (params.size() != 0) + throw FileError("Invalid number of function parameters (must be 0)."); + + /* do nothing if no pixel exists */ + if (m_pixeldata == NULL || m_pixelformat == NULL) + return; + + /* calc rowsize - boundary is 32 */ + uint32_t rowsize = 4 * static_cast( + ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32 + ); + + uint8_t *buf = new uint8_t[rowsize]; + for(uint32_t i = 0; i < getHeight()/2; i++) + { + uint32_t j = getHeight() - i - 1; + uint32_t offset = i * rowsize; + uint32_t backset = j * rowsize; + + /* boundary check */ + if (offset + rowsize > getPixelDataSize() + || backset + rowsize > getPixelDataSize()) + throw FileError("Mirrored pixel position is out of range."); + + /* mirroring, backup lower data first */ + copy(m_pixeldata + backset, m_pixeldata + backset + rowsize, buf); + copy(m_pixeldata + offset, m_pixeldata + offset + rowsize, m_pixeldata + backset); + copy(buf, buf + rowsize, m_pixeldata + offset); + } + delete[] buf; +} + +/*----------------------------------------------------------------------------*/ + +void CBitmap::mirror_x(std::list params) +{ + /* check prerequirements */ + if (params.size() != 0) + throw FileError("Invalid number of function parameters (must be 0)."); + + /* do nothing if no pixel exists */ + if (m_pixeldata == NULL || m_pixelformat == NULL) + return; + + /* calc rowsize - boundary is 32 */ + uint32_t rowsize = 4 * static_cast( + ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32 + ); + + /* calc pixelwidth */ + unsigned int pixelwidth = m_pixelformat->getBitCount()/8; + + uint8_t *buf = new uint8_t[pixelwidth]; + for(uint32_t i = 0; i < getHeight(); i++) + { + uint32_t offset = i * rowsize; + + for(uint32_t j = 0; j <= getWidth()/2; j++) + { + uint32_t poffset = offset + j * pixelwidth; + uint32_t pbackset = offset + getWidth() * pixelwidth - j * pixelwidth; + + /* boundary check */ + if (pbackset > getPixelDataSize()) + 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); + } + } + delete[] buf; +} + +/* vim: set et sw=2 ts=2: */ -- cgit v1.2.3