From f3c5bc280737573cf8597f18c011a1a1092e32d3 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 28 Apr 2009 16:29:24 +0200 Subject: making cbitmap abstract --- ue2/imgsynth2/cbitmap.cpp | 382 --------------------------------------- ue2/imgsynth2/cbitmap.h | 236 ------------------------ ue2/imgsynth2/cpixelformat.h | 4 +- ue2/imgsynth2/cwindowsbitmap.cpp | 382 +++++++++++++++++++++++++++++++++++++++ ue2/imgsynth2/cwindowsbitmap.h | 236 ++++++++++++++++++++++++ ue2/imgsynth2/x1 | 26 --- ue2/imgsynth2/x2 | 26 --- 7 files changed, 620 insertions(+), 672 deletions(-) delete mode 100644 ue2/imgsynth2/cbitmap.cpp delete mode 100644 ue2/imgsynth2/cbitmap.h create mode 100644 ue2/imgsynth2/cwindowsbitmap.cpp create mode 100644 ue2/imgsynth2/cwindowsbitmap.h delete mode 100644 ue2/imgsynth2/x1 delete mode 100644 ue2/imgsynth2/x2 (limited to 'ue2') diff --git a/ue2/imgsynth2/cbitmap.cpp b/ue2/imgsynth2/cbitmap.cpp deleted file mode 100644 index fc1a7c0..0000000 --- a/ue2/imgsynth2/cbitmap.cpp +++ /dev/null @@ -1,382 +0,0 @@ -/** - * @module cbitmap - * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief Implementation of CFile handling Windows Bitmaps. - * @date 17.04.2009 - */ - -#include -#include -#ifdef DEBUG -# include -#endif -#include "cbitmap.h" -#include "cpixelformat_bgr24.h" -#include "cpixelformat_bgr555.h" - -using namespace std; - -CBitmap::CBitmap() - : m_pixeldata(NULL), m_pixelformat(NULL) -{ - m_types.insert("BMP"); - - /* add our handlers */ - m_handlers.insert(new CPixelFormat_BGR24(this)); - m_handlers.insert(new CPixelFormat_BGR555(this)); -} - -/*----------------------------------------------------------------------------*/ - -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::read(std::ifstream& in) -{ - /* read and check file header */ - in.read(reinterpret_cast(&m_fileheader), sizeof(m_fileheader)); - - if (m_fileheader.bfType[0] != 'B' || m_fileheader.bfType[1] != 'M') - throw FileError("Imagefile has invalid Bitmap header."); - /* bfSize is unreliable (http://de.wikipedia.org/wiki/Windows_Bitmap) */ - if (m_fileheader.bfSize < 0) - throw FileError("Bitmap filesize is less than zero?"); - - /* read and check info header */ - in.read(reinterpret_cast(&m_infoheader), sizeof(m_infoheader)); - - if (m_infoheader.biSize != 40) - throw FileError("Bitmap info header size is invalid."); - if (m_infoheader.biPlanes != 1) - throw FileError("Bitmap color planes is not set to 1."); - if (m_infoheader.biCompression != 0) - throw FileError("Bitmap compression is set but not supported."); - if (m_infoheader.biSizeImage < 0) - throw FileError("Bitmap image size is less than zero?"); - if (m_infoheader.biClrUsed != 0 || m_infoheader.biClrImportant != 0) - throw FileError("Bitmap colortable is used but not supported."); - - /* read pixel data using separate class */ - if (m_infoheader.biSizeImage > 0) - { - if (m_pixeldata != NULL) - delete[] m_pixeldata; - m_pixeldata = new uint8_t[m_infoheader.biSizeImage]; - in.read(reinterpret_cast(m_pixeldata), m_infoheader.biSizeImage); - } - - /* get pixelformat instance */ - m_pixelformat = NULL; - set::iterator it; - for (it = m_handlers.begin(); it != m_handlers.end(); it++) - { - if (m_infoheader.biBitCount == (*it)->getBitCount()) - { - m_pixelformat = *it; - break; - } - } - if (m_pixelformat == NULL) - throw FileError("Bitmap bitcount is not supported."); -} - -/*----------------------------------------------------------------------------*/ - -void CBitmap::write(std::ofstream& out) -{ - /* set header values */ - m_fileheader.bfSize = m_infoheader.biSizeImage + sizeof(m_infoheader) + sizeof(m_fileheader); - - /* write file header */ - out.write(reinterpret_cast(&m_fileheader), sizeof(m_fileheader)); - - /* write info header */ - out.write(reinterpret_cast(&m_infoheader), sizeof(m_infoheader)); - - /* write pixel data */ - if (m_pixeldata != NULL) - out.write(reinterpret_cast(m_pixeldata), m_infoheader.biSizeImage); -} - -/*----------------------------------------------------------------------------*/ - -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() + ")."); - } - - /* width and height can be negativ */ - uint32_t width = static_cast(abs(m_infoheader.biWidth)); - uint32_t height = static_cast(abs(m_infoheader.biHeight)); - - /* check parameter values are in range */ - if (pparams[0] < 0 || pparams[0] > width - || pparams[1] < 0 || pparams[1] > height) - 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] > width - || pparams[3] < 0 || pparams[3] + pparams[1] > height) - 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()); - } - } - } -} - -/*----------------------------------------------------------------------------*/ - -#include -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; - - /* width and height can be negativ */ - uint32_t width = static_cast(abs(m_infoheader.biWidth)); - uint32_t height = static_cast(abs(m_infoheader.biHeight)); - unsigned int pixelwidth = m_pixelformat->getBitCount()/8; - - /* calc rowsize - boundary is 32 */ - uint32_t rowsize = 4 * static_cast( - ((m_pixelformat->getBitCount() * abs(m_infoheader.biWidth)) + 31) / 32 - ); - - for(uint32_t i = 0; i < height; i++) - { - for(uint32_t j = 0; j <= width; j++) - { - cout << j << endl; - } - } - -#if 0 -/* uint32_t offset = i * rowsize; - - for(uint32_t j = 0; j <= width/2; j++) - { - uint32_t poffset = offset + j * pixelwidth; - uint32_t pbackset = offset + width * 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 -} - -/*----------------------------------------------------------------------------*/ - -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; - - /* height can be negativ */ - uint32_t height = static_cast(abs(m_infoheader.biHeight)); - - /* calc rowsize - boundary is 32 */ - uint32_t rowsize = 4 * static_cast( - ((m_pixelformat->getBitCount() * abs(m_infoheader.biWidth)) + 31) / 32 - ); - - uint8_t *buf = new uint8_t[rowsize]; - for(uint32_t i = 0; i < height/2; i++) - { - uint32_t j = height - i - 1; - uint32_t offset = i * rowsize; - uint32_t backset = j * rowsize; - - /* boundary check */ - if (offset + rowsize > m_infoheader.biSizeImage - || backset + rowsize > m_infoheader.biSizeImage) - 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; - - /* width and height can be negativ */ - uint32_t width = static_cast(abs(m_infoheader.biWidth)); - uint32_t height = static_cast(abs(m_infoheader.biHeight)); - - /* calc rowsize - boundary is 32 */ - uint32_t rowsize = 4 * static_cast( - ((m_pixelformat->getBitCount() * abs(m_infoheader.biWidth)) + 31) / 32 - ); - - /* calc pixelwidth */ - unsigned int pixelwidth = m_pixelformat->getBitCount()/8; - - uint8_t *buf = new uint8_t[pixelwidth]; - for(uint32_t i = 0; i < height; i++) - { - uint32_t offset = i * rowsize; - - for(uint32_t j = 0; j <= width/2; j++) - { - uint32_t poffset = offset + j * pixelwidth; - uint32_t pbackset = offset + width * 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); - } - } - delete[] buf; -} - -/*----------------------------------------------------------------------------*/ - -#ifdef DEBUG -void CBitmap::dump(std::ostream& out) -{ - out - << "Bitmap File Header:" << endl - << " bfType=" << m_fileheader.bfType[0] << m_fileheader.bfType[1] - << ", bfSize=" << m_fileheader.bfSize - << ", bfReserved=" << m_fileheader.bfReserved - << ", bfOffBits=" << m_fileheader.bfOffBits - << endl; - - out - << "Bitmap Info Header:" << endl - << " biSize=" << m_infoheader.biSize - << ", biWidth=" << m_infoheader.biWidth - << ", biHeight=" << m_infoheader.biHeight - << ", biPlanes=" << m_infoheader.biPlanes - << endl - - << " biBitCount=" << m_infoheader.biBitCount - << ", biCompression=" << m_infoheader.biCompression - << ", biSizeImage=" << m_infoheader.biSizeImage - << endl - - << " biXPelsPerMeter=" << m_infoheader.biXPelsPerMeter - << ", biYPelsPerMeter=" << m_infoheader.biYPelsPerMeter - << ", biClrUsed=" << m_infoheader.biClrUsed - << ", biClrImportant=" << m_infoheader.biClrImportant - << endl; -} -#endif - -/* vim: set et sw=2 ts=2: */ diff --git a/ue2/imgsynth2/cbitmap.h b/ue2/imgsynth2/cbitmap.h deleted file mode 100644 index ca3d6ef..0000000 --- a/ue2/imgsynth2/cbitmap.h +++ /dev/null @@ -1,236 +0,0 @@ -/** - * @module cbitmap - * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) - * @brief Implementation of CFile handling Windows Bitmaps. - * @date 17.04.2009 - */ - -#ifndef CBITMAP_H -#define CBITMAP_H - -#include -#include "cfile.h" - -class CPixelFormat; -#include "cpixelformat.h" - -/** - * @class CBitmap - * @brief Implementation of CFile handling Windows Bitmaps. - * - * In order to support operations on bitmaps with different color bitcounts - * different implementations of CPixelFormat are used. These classes are - * allowed to modify the bitmap headers and pixelbuffer directly. - * - * On error CFile::FileError is thrown. - */ -class CBitmap : public CFile -{ - public: - /** - * @method CBitmap - * @brief Default ctor - * @param - - * @return - - * @globalvars none - * @exception none - * @conditions none - */ - CBitmap(); - - /** - * @method ~CBitmap - * @brief Default dtor - * @param - - * @return - - * @globalvars none - * @exception none - * @conditions none - */ - ~CBitmap(); - - /** - * @method read - * @brief Reads Windows Bitmap from filestream. - * On error an exception is thrown. - * @param in filestream to read data from - * @return - - * @globalvars none - * @exception CFile::FileError - * @exception bad_alloc - * @conditions none - */ - void read(std::ifstream& in); - - /** - * @method write - * @brief Writes Windows Bitmap to filestream. - * @param out filestream to read data from - * @return - - * @globalvars none - * @exception FileError - * @exception bad_alloc - * @conditions none - */ - void write(std::ofstream& out); - - /** - * @method callFunc - * @brief Delegates the function and its parameters to the correct - * internal method - * @param func function name - * @param params function parameters as list - * @return - - * @globalvars none - * @exception ParserError - * @conditions none - */ - void callFunc(const std::string& func, const std::list& params); - -#ifdef DEBUG - /** - * @method dump - * @brief Dumps the Windows Bitmap file headers to ostream - * @param out output stream - * @return - - * @globalvars - * @exception - * @conditions - */ - void dump(std::ostream& out); -#endif - - /** - * @brief Windows Bitmap File Header structure - */ -#pragma pack(push,1) - typedef struct - { - /** the magic number used to identify the BMP file */ - uint8_t bfType[2]; - /** the size of the BMP file in bytes */ - uint32_t bfSize; - /** reserved */ - uint32_t bfReserved; - /** the offset of the byte where the bitmap data can be found */ - uint32_t bfOffBits; - } BITMAP_FILEHEADER; -#pragma pack(pop) - - /** - * @brief Windows Bitmap Info Header structure - */ -#pragma pack(push,1) - typedef struct - { - /** the size of this header (40 bytes) */ - uint32_t biSize; - /** the bitmap width in pixels (signed integer) */ - int32_t biWidth; - /** the bitmap height in pixels (signed integer) */ - int32_t biHeight; - /** the number of color planes being used. Must be set to 1 */ - uint16_t biPlanes; - /** the number of bits per pixel, which is the color depth of the image */ - uint16_t biBitCount; - /** the compression method being used */ - uint32_t biCompression; - /** the image size */ - uint32_t biSizeImage; - /** the horizontal resolution of the image (pixel per meter) */ - int32_t biXPelsPerMeter; - /** the vertical resolution of the image (pixel per meter) */ - int32_t biYPelsPerMeter; - /** the number of colors in the color palette, or 0 to default to 2^n */ - uint32_t biClrUsed; - /** the number of important colors used, or 0 when every color is - * important; generally ignored. */ - uint32_t biClrImportant; - } BITMAP_INFOHEADER; -#pragma pack(pop) - - /** - * @method getFileHeader - * @brief Returns reference to fileheader structure of bitmap - * @param - - * @return reference to fileheader structure - * @globalvars none - * @exception none - * @conditions none - */ - BITMAP_FILEHEADER &getFileHeader() - { - return m_fileheader; - } - - /** - * @method getInfoHeader - * @brief Returns reference to infoheader structure of bitmap - * @param - - * @return reference to infoheader structure - * @globalvars none - * @exception none - * @conditions none - */ - BITMAP_INFOHEADER &getInfoHeader() - { - return m_infoheader; - } - - /** - * @method getPixelData - * @brief Returns pointer to pixelbuffer - * @param - - * @return pointer to pixelbuffer - * @globalvars none - * @exception none - * @conditions none - */ - uint8_t *getPixelData() - { - return m_pixeldata; - } - - protected: - /** - * @method fillrect - * @brief Fills rectangle in image starting on position x, y - * width size width, height and color red, green, blue. - * @param params function parameters as list - * @return - - * @globalvars none - * @exception FileError - * @conditions none - * - * Scriptfile syntax: fillrect(x, y, width, height, red, green, blue) - */ - void fillrect(std::list params); - - /* TODO */ - void invert(std::list params); - - /* TODO */ - void brightness(std::list params); - - /* TODO */ - void mirror_y(std::list params); - - /* TODO */ - void mirror_x(std::list params); - - /* members */ - /** fileheader */ - BITMAP_FILEHEADER m_fileheader; - /** infoheader */ - BITMAP_INFOHEADER m_infoheader; - /** pointer to pixelbuffer */ - uint8_t *m_pixeldata; - /** set of supported PixelFormat handlers */ - std::set m_handlers; - /** pointer to CPixelFormat implementation */ - CPixelFormat *m_pixelformat; -}; - -#endif - -/* vim: set et sw=2 ts=2: */ diff --git a/ue2/imgsynth2/cpixelformat.h b/ue2/imgsynth2/cpixelformat.h index 49145df..0568dd4 100644 --- a/ue2/imgsynth2/cpixelformat.h +++ b/ue2/imgsynth2/cpixelformat.h @@ -47,7 +47,7 @@ class CPixelFormat }; /** - * @method CBitmap + * @method CPixelFormat * @brief Default ctor * @param bitmap pointer to CBitmap instance * @return - @@ -95,7 +95,7 @@ class CPixelFormat * @exception PixelFormatError * @conditions none */ - //TODO virtual void getPixel(const uint32_t *pixel, const uint32_t x, const uint32_t y) = 0; + virtual void getPixel(const uint32_t *pixel, const uint32_t x, const uint32_t y) = 0; /** * @method getBitCount diff --git a/ue2/imgsynth2/cwindowsbitmap.cpp b/ue2/imgsynth2/cwindowsbitmap.cpp new file mode 100644 index 0000000..5466dc2 --- /dev/null +++ b/ue2/imgsynth2/cwindowsbitmap.cpp @@ -0,0 +1,382 @@ +/** + * @module cwindowsbitmap + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Implementation of CBitmap handling Windows Bitmaps. + * @date 17.04.2009 + */ + +#include +#include +#ifdef DEBUG +# include +#endif +#include "cwindowsbitmap.h" +#include "cpixelformat_bgr24.h" +#include "cpixelformat_bgr555.h" + +using namespace std; + +CWindowsBitmap::CWindowsBitmap() + : m_pixeldata(NULL), m_pixelformat(NULL) +{ + m_types.insert("BMP"); + + /* add our handlers */ + m_handlers.insert(new CPixelFormat_BGR24(this)); + m_handlers.insert(new CPixelFormat_BGR555(this)); +} + +/*----------------------------------------------------------------------------*/ + +CWindowsBitmap::~CWindowsBitmap() +{ + /* 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 CWindowsBitmap::read(std::ifstream& in) +{ + /* read and check file header */ + in.read(reinterpret_cast(&m_fileheader), sizeof(m_fileheader)); + + if (m_fileheader.bfType[0] != 'B' || m_fileheader.bfType[1] != 'M') + throw FileError("Imagefile has invalid Bitmap header."); + /* bfSize is unreliable (http://de.wikipedia.org/wiki/Windows_Bitmap) */ + if (m_fileheader.bfSize < 0) + throw FileError("Bitmap filesize is less than zero?"); + + /* read and check info header */ + in.read(reinterpret_cast(&m_infoheader), sizeof(m_infoheader)); + + if (m_infoheader.biSize != 40) + throw FileError("Bitmap info header size is invalid."); + if (m_infoheader.biPlanes != 1) + throw FileError("Bitmap color planes is not set to 1."); + if (m_infoheader.biCompression != 0) + throw FileError("Bitmap compression is set but not supported."); + if (m_infoheader.biSizeImage < 0) + throw FileError("Bitmap image size is less than zero?"); + if (m_infoheader.biClrUsed != 0 || m_infoheader.biClrImportant != 0) + throw FileError("Bitmap colortable is used but not supported."); + + /* read pixel data using separate class */ + if (m_infoheader.biSizeImage > 0) + { + if (m_pixeldata != NULL) + delete[] m_pixeldata; + m_pixeldata = new uint8_t[m_infoheader.biSizeImage]; + in.read(reinterpret_cast(m_pixeldata), m_infoheader.biSizeImage); + } + + /* get pixelformat instance */ + m_pixelformat = NULL; + set::iterator it; + for (it = m_handlers.begin(); it != m_handlers.end(); it++) + { + if (m_infoheader.biBitCount == (*it)->getBitCount()) + { + m_pixelformat = *it; + break; + } + } + if (m_pixelformat == NULL) + throw FileError("Bitmap bitcount is not supported."); +} + +/*----------------------------------------------------------------------------*/ + +void CWindowsBitmap::write(std::ofstream& out) +{ + /* set header values */ + m_fileheader.bfSize = m_infoheader.biSizeImage + sizeof(m_infoheader) + sizeof(m_fileheader); + + /* write file header */ + out.write(reinterpret_cast(&m_fileheader), sizeof(m_fileheader)); + + /* write info header */ + out.write(reinterpret_cast(&m_infoheader), sizeof(m_infoheader)); + + /* write pixel data */ + if (m_pixeldata != NULL) + out.write(reinterpret_cast(m_pixeldata), m_infoheader.biSizeImage); +} + +/*----------------------------------------------------------------------------*/ + +void CWindowsBitmap::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 CWindowsBitmap::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() + ")."); + } + + /* width and height can be negativ */ + uint32_t width = static_cast(abs(m_infoheader.biWidth)); + uint32_t height = static_cast(abs(m_infoheader.biHeight)); + + /* check parameter values are in range */ + if (pparams[0] < 0 || pparams[0] > width + || pparams[1] < 0 || pparams[1] > height) + 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] > width + || pparams[3] < 0 || pparams[3] + pparams[1] > height) + 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()); + } + } + } +} + +/*----------------------------------------------------------------------------*/ + +#include +void CWindowsBitmap::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; + + /* width and height can be negativ */ + uint32_t width = static_cast(abs(m_infoheader.biWidth)); + uint32_t height = static_cast(abs(m_infoheader.biHeight)); + unsigned int pixelwidth = m_pixelformat->getBitCount()/8; + + /* calc rowsize - boundary is 32 */ + uint32_t rowsize = 4 * static_cast( + ((m_pixelformat->getBitCount() * abs(m_infoheader.biWidth)) + 31) / 32 + ); + + for(uint32_t i = 0; i < height; i++) + { + for(uint32_t j = 0; j <= width; j++) + { + cout << j << endl; + } + } + +#if 0 +/* uint32_t offset = i * rowsize; + + for(uint32_t j = 0; j <= width/2; j++) + { + uint32_t poffset = offset + j * pixelwidth; + uint32_t pbackset = offset + width * 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 +} + +/*----------------------------------------------------------------------------*/ + +void CWindowsBitmap::brightness(std::list params) +{ +} + +/*----------------------------------------------------------------------------*/ + +void CWindowsBitmap::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; +W + /* height can be negativ */ + uint32_t height = static_cast(abs(m_infoheader.biHeight)); + + /* calc rowsize - boundary is 32 */ + uint32_t rowsize = 4 * static_cast( + ((m_pixelformat->getBitCount() * abs(m_infoheader.biWidth)) + 31) / 32 + ); + + uint8_t *buf = new uint8_t[rowsize]; + for(uint32_t i = 0; i < height/2; i++) + { + uint32_t j = height - i - 1; + uint32_t offset = i * rowsize; + uint32_t backset = j * rowsize; + + /* boundary check */ + if (offset + rowsize > m_infoheader.biSizeImage + || backset + rowsize > m_infoheader.biSizeImage) + 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 CWindowsBitmap::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; + + /* width and height can be negativ */ + uint32_t width = static_cast(abs(m_infoheader.biWidth)); + uint32_t height = static_cast(abs(m_infoheader.biHeight)); + + /* calc rowsize - boundary is 32 */ + uint32_t rowsize = 4 * static_cast( + ((m_pixelformat->getBitCount() * abs(m_infoheader.biWidth)) + 31) / 32 + ); + + /* calc pixelwidth */ + unsigned int pixelwidth = m_pixelformat->getBitCount()/8; + + uint8_t *buf = new uint8_t[pixelwidth]; + for(uint32_t i = 0; i < height; i++) + { + uint32_t offset = i * rowsize; + + for(uint32_t j = 0; j <= width/2; j++) + { + uint32_t poffset = offset + j * pixelwidth; + uint32_t pbackset = offset + width * 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); + } + } + delete[] buf; +} + +/*----------------------------------------------------------------------------*/ + +#ifdef DEBUG +void CWindowsBitmap::dump(std::ostream& out) +{ + out + << "Bitmap File Header:" << endl + << " bfType=" << m_fileheader.bfType[0] << m_fileheader.bfType[1] + << ", bfSize=" << m_fileheader.bfSize + << ", bfReserved=" << m_fileheader.bfReserved + << ", bfOffBits=" << m_fileheader.bfOffBits + << endl; + + out + << "Bitmap Info Header:" << endl + << " biSize=" << m_infoheader.biSize + << ", biWidth=" << m_infoheader.biWidth + << ", biHeight=" << m_infoheader.biHeight + << ", biPlanes=" << m_infoheader.biPlanes + << endl + + << " biBitCount=" << m_infoheader.biBitCount + << ", biCompression=" << m_infoheader.biCompression + << ", biSizeImage=" << m_infoheader.biSizeImage + << endl + + << " biXPelsPerMeter=" << m_infoheader.biXPelsPerMeter + << ", biYPelsPerMeter=" << m_infoheader.biYPelsPerMeter + << ", biClrUsed=" << m_infoheader.biClrUsed + << ", biClrImportant=" << m_infoheader.biClrImportant + << endl; +} +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue2/imgsynth2/cwindowsbitmap.h b/ue2/imgsynth2/cwindowsbitmap.h new file mode 100644 index 0000000..420ad1e --- /dev/null +++ b/ue2/imgsynth2/cwindowsbitmap.h @@ -0,0 +1,236 @@ +/** + * @module cwindowsbitmap + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Implementation of CBitmap handling Windows Bitmaps. + * @date 17.04.2009 + */ + +#ifndef CWINDOWSBITMAP_H +#define CWINDOWSBITMAP_H + +#include +#include "cbitmap.h" + +class CPixelFormat; +#include "cpixelformat.h" + +/** + * @class CWindowsBitmap + * @brief Implementation of CBitmap handling Windows Bitmaps. + * + * In order to support operations on bitmaps with different color bitcounts + * different implementations of CPixelFormat are used. These classes are + * allowed to modify the bitmap headers and pixelbuffer directly. + * + * On error CFile::FileError is thrown. + */ +class CWindowsBitmap : public CBitmap +{ + public: + /** + * @method CWindowsBitmap + * @brief Default ctor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CWindowsBitmap(); + + /** + * @method ~CWindowsBitmap + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ~CWindowsBitmap(); + + /** + * @method read + * @brief Reads Windows Bitmap from filestream. + * On error an exception is thrown. + * @param in filestream to read data from + * @return - + * @globalvars none + * @exception CFile::FileError + * @exception bad_alloc + * @conditions none + */ + void read(std::ifstream& in); + + /** + * @method write + * @brief Writes Windows Bitmap to filestream. + * @param out filestream to read data from + * @return - + * @globalvars none + * @exception FileError + * @exception bad_alloc + * @conditions none + */ + void write(std::ofstream& out); + + /** + * @method callFunc + * @brief Delegates the function and its parameters to the correct + * internal method + * @param func function name + * @param params function parameters as list + * @return - + * @globalvars none + * @exception ParserError + * @conditions none + */ + void callFunc(const std::string& func, const std::list& params); + +#ifdef DEBUG + /** + * @method dump + * @brief Dumps the Windows Bitmap file headers to ostream + * @param out output stream + * @return - + * @globalvars + * @exception + * @conditions + */ + void dump(std::ostream& out); +#endif + + /** + * @brief Windows Bitmap File Header structure + */ +#pragma pack(push,1) + typedef struct + { + /** the magic number used to identify the BMP file */ + uint8_t bfType[2]; + /** the size of the BMP file in bytes */ + uint32_t bfSize; + /** reserved */ + uint32_t bfReserved; + /** the offset of the byte where the bitmap data can be found */ + uint32_t bfOffBits; + } BITMAP_FILEHEADER; +#pragma pack(pop) + + /** + * @brief Windows Bitmap Info Header structure + */ +#pragma pack(push,1) + typedef struct + { + /** the size of this header (40 bytes) */ + uint32_t biSize; + /** the bitmap width in pixels (signed integer) */ + int32_t biWidth; + /** the bitmap height in pixels (signed integer) */ + int32_t biHeight; + /** the number of color planes being used. Must be set to 1 */ + uint16_t biPlanes; + /** the number of bits per pixel, which is the color depth of the image */ + uint16_t biBitCount; + /** the compression method being used */ + uint32_t biCompression; + /** the image size */ + uint32_t biSizeImage; + /** the horizontal resolution of the image (pixel per meter) */ + int32_t biXPelsPerMeter; + /** the vertical resolution of the image (pixel per meter) */ + int32_t biYPelsPerMeter; + /** the number of colors in the color palette, or 0 to default to 2^n */ + uint32_t biClrUsed; + /** the number of important colors used, or 0 when every color is + * important; generally ignored. */ + uint32_t biClrImportant; + } BITMAP_INFOHEADER; +#pragma pack(pop) + + /** + * @method getFileHeader + * @brief Returns reference to fileheader structure of bitmap + * @param - + * @return reference to fileheader structure + * @globalvars none + * @exception none + * @conditions none + */ + BITMAP_FILEHEADER &getFileHeader() + { + return m_fileheader; + } + + /** + * @method getInfoHeader + * @brief Returns reference to infoheader structure of bitmap + * @param - + * @return reference to infoheader structure + * @globalvars none + * @exception none + * @conditions none + */ + BITMAP_INFOHEADER &getInfoHeader() + { + return m_infoheader; + } + + /** + * @method getPixelData + * @brief Returns pointer to pixelbuffer + * @param - + * @return pointer to pixelbuffer + * @globalvars none + * @exception none + * @conditions none + */ + uint8_t *getPixelData() + { + return m_pixeldata; + } + + protected: + /** + * @method fillrect + * @brief Fills rectangle in image starting on position x, y + * width size width, height and color red, green, blue. + * @param params function parameters as list + * @return - + * @globalvars none + * @exception FileError + * @conditions none + * + * Scriptfile syntax: fillrect(x, y, width, height, red, green, blue) + */ + void fillrect(std::list params); + + /* TODO */ + void invert(std::list params); + + /* TODO */ + void brightness(std::list params); + + /* TODO */ + void mirror_y(std::list params); + + /* TODO */ + void mirror_x(std::list params); + + /* members */ + /** fileheader */ + BITMAP_FILEHEADER m_fileheader; + /** infoheader */ + BITMAP_INFOHEADER m_infoheader; + /** pointer to pixelbuffer */ + uint8_t *m_pixeldata; + /** set of supported PixelFormat handlers */ + std::set m_handlers; + /** pointer to CPixelFormat implementation */ + CPixelFormat *m_pixelformat; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue2/imgsynth2/x1 b/ue2/imgsynth2/x1 deleted file mode 100644 index 5593645..0000000 --- a/ue2/imgsynth2/x1 +++ /dev/null @@ -1,26 +0,0 @@ -00000000 42 4d 8a 01 00 00 00 00 00 00 36 00 00 00 28 00 |BM........6...(.| -00000010 00 00 09 00 00 00 11 00 00 00 01 00 10 00 00 00 |................| -00000020 00 00 54 01 00 00 13 0b 00 00 13 0b 00 00 00 00 |..T.............| -00000030 00 00 00 00 00 00 00 00 00 00 1f 00 1f 00 1f 00 |................| -00000040 1f 00 1f 00 1f 00 1f 00 00 00 00 00 00 00 1f 00 |................| -00000050 1f 00 1f 00 1f 00 1f 00 1f 00 1f 00 00 00 00 00 |................| -00000060 00 00 1f 00 1f 00 1f 00 1f 00 1f 00 1f 00 1f 00 |................| -00000070 00 00 00 00 00 00 1f 00 1f 00 1f 00 1f 00 1f 00 |................| -00000080 1f 00 1f 00 00 00 00 00 00 00 00 00 a0 7f 00 00 |................| -00000090 a0 7f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| -000000a0 a0 7f 00 00 a0 7f 00 00 00 00 00 00 00 00 00 00 |................| -000000b0 a0 7f 00 00 a0 7f a0 7f a0 7f 00 00 a0 7f 00 00 |................| -000000c0 00 00 00 00 a0 7f 00 00 a0 7f a0 7f a0 7f 00 00 |................| -000000d0 a0 7f 00 00 00 00 00 00 a0 7f 00 00 a0 7f a0 7f |................| -000000e0 a0 7f 00 00 a0 7f 00 00 00 00 e0 03 e0 03 e0 03 |................| -000000f0 e0 03 e0 03 e0 03 00 00 a0 7f 00 00 00 00 e0 03 |................| -00000100 e0 03 e0 03 e0 03 e0 03 e0 03 a0 7f 00 00 00 00 |................| -00000110 00 00 e0 03 e0 03 e0 03 e0 03 e0 03 e0 03 00 00 |................| -00000120 00 00 00 00 00 00 e0 03 e0 03 e0 03 e0 03 e0 03 |................| -00000130 e0 03 00 00 00 00 00 00 00 00 e0 03 e0 03 e0 03 |................| -00000140 e0 03 e0 03 e0 03 00 00 00 00 00 00 00 00 00 00 |................| -00000150 00 00 00 00 a0 7f a0 7f a0 7f 00 00 00 00 00 00 |................| -00000160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| -* -00000180 00 00 00 00 00 00 00 00 00 00 |..........| -0000018a diff --git a/ue2/imgsynth2/x2 b/ue2/imgsynth2/x2 deleted file mode 100644 index 26cbf82..0000000 --- a/ue2/imgsynth2/x2 +++ /dev/null @@ -1,26 +0,0 @@ -00000000 42 4d 8a 01 00 00 00 00 00 00 36 00 00 00 28 00 |BM........6...(.| -00000010 00 00 09 00 00 00 11 00 00 00 01 00 10 00 00 00 |................| -00000020 00 00 54 01 00 00 13 0b 00 00 13 0b 00 00 00 00 |..T.............| -00000030 00 00 00 00 00 00 00 00 00 00 1f 00 1f 00 1f 00 |................| -00000040 1f 00 1f 00 1f 00 1f 00 00 00 00 00 00 00 1f 00 |................| -00000050 1f 00 1f 00 1f 00 1f 00 1f 00 1f 00 00 00 00 00 |................| -00000060 00 00 1f 00 1f 7f 1f 00 1f 7f 1f 00 1f 00 1f 00 |................| -00000070 00 00 00 00 00 00 1f 00 1f 7f 1f 00 1f 7f 1f 00 |................| -00000080 1f 00 1f 00 00 00 00 00 00 00 00 00 a0 7f 00 00 |................| -00000090 a0 7f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| -000000a0 a0 7f 00 00 a0 7f 00 00 00 00 00 00 00 00 00 00 |................| -000000b0 a0 7f 00 00 a0 7f a0 7f a0 7f 00 00 a0 7f 00 00 |................| -000000c0 00 00 00 00 a0 7f 00 00 a0 7f a0 7f a0 7f 00 00 |................| -000000d0 a0 7f 00 00 00 00 00 00 a0 7f 00 00 a0 7f a0 7f |................| -000000e0 a0 7f 00 00 a0 7f 00 00 00 00 00 00 00 7f 00 00 |................| -000000f0 00 7f 00 7f 00 7f 00 00 a0 7f 00 00 00 00 00 00 |................| -00000100 00 00 00 7f 00 7f 00 7f 00 7f a0 7f 00 00 00 00 |................| -00000110 00 00 00 00 00 00 00 00 00 00 00 7f 00 00 00 00 |................| -00000120 00 00 00 00 00 00 00 00 00 00 00 00 00 7f 00 7f |................| -00000130 00 7f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| -00000140 00 7f 00 7f 00 7f 00 00 00 00 00 00 00 00 00 00 |................| -00000150 00 00 00 00 a0 7f a0 7f a0 7f 00 00 00 00 00 00 |................| -00000160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| -* -00000180 00 00 00 00 00 00 00 00 00 00 |..........| -0000018a -- cgit v1.2.3