From 384539f7cc9feaa7ef7cee385cce472c6966c843 Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 27 Apr 2009 00:24:16 +0200 Subject: Adding ue1 --- ue1/imgsynth/Makefile | 39 ++++++ ue1/imgsynth/cbitmap.cpp | 208 ++++++++++++++++++++++++++++++++ ue1/imgsynth/cbitmap.h | 225 +++++++++++++++++++++++++++++++++++ ue1/imgsynth/cfile.h | 121 +++++++++++++++++++ ue1/imgsynth/cpixelformat.h | 106 +++++++++++++++++ ue1/imgsynth/cpixelformat_24.cpp | 47 ++++++++ ue1/imgsynth/cpixelformat_24.h | 78 ++++++++++++ ue1/imgsynth/cscriptparser.cpp | 208 ++++++++++++++++++++++++++++++++ ue1/imgsynth/cscriptparser.h | 181 ++++++++++++++++++++++++++++ ue1/imgsynth/imgsynth.cbp | 60 ++++++++++ ue1/imgsynth/imgsynth.cpp | 84 +++++++++++++ ue1/imgsynth/imgsynth.layout | 7 ++ ue1/imgsynth/test/input | 6 + ue1/imgsynth/test/test.sh | 12 ++ ue1/imgsynth/test/yellow_man_in.bmp | Bin 0 -> 530 bytes ue1/imgsynth/test/yellow_man_out.bmp | Bin 0 -> 530 bytes ue1/imgsynth/test/yellow_man_ref.bmp | Bin 0 -> 530 bytes 17 files changed, 1382 insertions(+) create mode 100644 ue1/imgsynth/Makefile create mode 100644 ue1/imgsynth/cbitmap.cpp create mode 100644 ue1/imgsynth/cbitmap.h create mode 100644 ue1/imgsynth/cfile.h create mode 100644 ue1/imgsynth/cpixelformat.h create mode 100644 ue1/imgsynth/cpixelformat_24.cpp create mode 100644 ue1/imgsynth/cpixelformat_24.h create mode 100644 ue1/imgsynth/cscriptparser.cpp create mode 100644 ue1/imgsynth/cscriptparser.h create mode 100644 ue1/imgsynth/imgsynth.cbp create mode 100644 ue1/imgsynth/imgsynth.cpp create mode 100644 ue1/imgsynth/imgsynth.layout create mode 100644 ue1/imgsynth/test/input create mode 100755 ue1/imgsynth/test/test.sh create mode 100644 ue1/imgsynth/test/yellow_man_in.bmp create mode 100644 ue1/imgsynth/test/yellow_man_out.bmp create mode 100644 ue1/imgsynth/test/yellow_man_ref.bmp (limited to 'ue1/imgsynth') diff --git a/ue1/imgsynth/Makefile b/ue1/imgsynth/Makefile new file mode 100644 index 0000000..6b52d60 --- /dev/null +++ b/ue1/imgsynth/Makefile @@ -0,0 +1,39 @@ +# Makefile for imgsynth +# Author: Manuel Mausz (0728348) +# Created: 14.04.2009 + +CC= g++ +LD= $(CC) +DEBUGFLAGS= +CFLAGS= -O -ansi -pedantic-errors -Wall $(DEBUGFLAGS) +LDFLAGS= +LIBS= -lboost_program_options + +BIN= imgsynth +OBJS= cpixelformat_24.o cbitmap.o cscriptparser.o imgsynth.o +HEADERS= cpixelformat.h cpixelformat_24.h cfile.h cbitmap.h cscriptparser.h + +.SUFFIXES: .cpp .o + +all: $(BIN) + +.cpp.o: + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJS): $(HEADERS) + +$(BIN): $(OBJS) + $(LD) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) + +debug: + @$(MAKE) all "DEBUGFLAGS=-DDEBUG -g" + +clean: + rm -f $(OBJS) $(BIN) + +run test: all + @./test/test.sh + +.PHONY: clean + +# vim600: noet sw=8 ts=8 diff --git a/ue1/imgsynth/cbitmap.cpp b/ue1/imgsynth/cbitmap.cpp new file mode 100644 index 0000000..0205f0b --- /dev/null +++ b/ue1/imgsynth/cbitmap.cpp @@ -0,0 +1,208 @@ +/** + * @module cbitmap + * @author 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_24.h" + +using namespace std; + +CBitmap::~CBitmap() +{ + if (m_pixeldata != NULL) + delete[] m_pixeldata; + m_pixeldata = NULL; + + if (m_pixelformat != NULL) + delete m_pixelformat; + 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."); + + /* currently only 24bit */ + if (m_infoheader.biBitCount != 24) + throw FileError("Bitmap bitcount is 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); + } + + /* create pixelformat instance */ + if (m_pixelformat != NULL) + delete m_pixelformat; + m_pixelformat = NULL; + if (m_infoheader.biBitCount == 24) + m_pixelformat = new CPixelFormat_24(this); +} + +/*----------------------------------------------------------------------------*/ + +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 + 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)."); + + /* 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."); + + 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."); + + if (pparams[4] < 0 || pparams[4] > 255 + || pparams[5] < 0 || pparams[5] > 255 + || pparams[6] < 0 || pparams[6] > 255) + throw FileError("At least one pixel color parameter is out of range."); + + /* call setPixel for every pixel in the rectangel */ + if (m_pixeldata != NULL && m_pixelformat != NULL) + { + 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()); + } + } + } + } +} + +/*----------------------------------------------------------------------------*/ + +#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/ue1/imgsynth/cbitmap.h b/ue1/imgsynth/cbitmap.h new file mode 100644 index 0000000..300e836 --- /dev/null +++ b/ue1/imgsynth/cbitmap.h @@ -0,0 +1,225 @@ +/** + * @module cbitmap + * @author Manuel Mausz, 0728348 + * @brief Implementation of CFile handling Windows Bitmaps. + * @date 17.04.2009 + */ + +#ifndef CBITMAP_H +#define CBITMAP_H + +#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() + : m_pixeldata(NULL), m_pixelformat(NULL) + { + m_types.insert("BMP"); + } + + /** + * @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); + + /* members */ + /** fileheader */ + BITMAP_FILEHEADER m_fileheader; + /** infoheader */ + BITMAP_INFOHEADER m_infoheader; + /** pointer to pixelbuffer */ + uint8_t *m_pixeldata; + /** pointer to CPixelFormat implementation */ + CPixelFormat *m_pixelformat; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue1/imgsynth/cfile.h b/ue1/imgsynth/cfile.h new file mode 100644 index 0000000..d190d14 --- /dev/null +++ b/ue1/imgsynth/cfile.h @@ -0,0 +1,121 @@ +/** + * @module cfile + * @author Manuel Mausz, 0728348 + * @brief Abstract class for handling files. + * Needed for generic use in CScriptparser. + * @date 17.04.2009 + */ + +#ifndef CFILE_H +#define CFILE_H + +#include +#include +#include +#include +#include + +/** + * @class CFile + * @brief Abstract class for handling files. Needed for generic use in + * CScriptparser. + * + * In order for CScriptparser to determine which instance of CFile supports + * which filetype, every implemententation need to insert their filetypes to + * the member m_types in their constructor. + * + * On error throw FileError. + */ +class CFile +{ + public: + /** + * @class FileError + * @brief Exception thrown by implemententations of CFile + */ + class FileError : public std::invalid_argument { + public: + /** + * @method FileError + * @brief Default exception ctor + * @param what message to pass along + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + FileError(const std::string& what) + : std::invalid_argument(what) + {} + }; + + /** + * @method ~CFile + * @brief Default dtor (virtual) + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + virtual ~CFile() + {}; + + /** + * @method read + * @brief Pure virtual method (interface). Should read data from filestream. + * @param in filestream to read data from + * @return - + * @globalvars none + * @exception FileError + * @conditions none + */ + virtual void read(std::ifstream& in) = 0; + + /** + * @method write + * @brief Pure virtual method (interface). Should write data to filestream. + * @param out filestream to write data to + * @return - + * @globalvars none + * @exception FileError + * @conditions none + */ + virtual void write(std::ofstream& out) = 0; + + /** + * @method callFunc + * @brief Pure virtual method (interface). Should delegate the function + * and its parameters to the correct internal method. + * @param func function name + * @param params function parameters as list + * @return - + * @globalvars none + * @exception FileError + * @conditions none + */ + virtual void callFunc(const std::string& func, const std::list& params) = 0; + + /** + * @method supportsType + * @brief Check if filetype is supported by this implementation. + * @param type filetype + * @return true if filetype is supported. false otherwise + * @globalvars none + * @exception none + * @conditions none + */ + bool supportsType(const std::string& type) + { + return (m_types.find(type) == m_types.end()) ? false : true; + } + + protected: + /* members */ + /** set of filetypes suppported by this implementation */ + std::set m_types; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue1/imgsynth/cpixelformat.h b/ue1/imgsynth/cpixelformat.h new file mode 100644 index 0000000..0e20cf8 --- /dev/null +++ b/ue1/imgsynth/cpixelformat.h @@ -0,0 +1,106 @@ +/** + * @module cpixelformat + * @author Manuel Mausz, 0728348 + * @brief Abstract class for handling different color bitcount of Bitmaps. + * Needed for generic use in CBitmap. + * @date 18.04.2009 + */ + +#ifndef CPIXELFORMAT_H +#define CPIXELFORMAT_H + +#include +#include + +class CBitmap; +#include "cbitmap.h" + +/** + * @class CPixelFormat + * @brief Abstract class for handling different color bitcount of Bitmaps. + * + * Needed for generic use in CBitmap. + * + * On error throw PixelFormatError. + */ +class CPixelFormat +{ + public: + /** + * @class PixelFormatError + * @brief Exception thrown by implemententations of CPixelFormat + */ + class PixelFormatError : public std::invalid_argument { + public: + /** + * @method PixelFormatError + * @brief Default exception ctor + * @param what message to pass along + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + PixelFormatError(const std::string& what) + : std::invalid_argument(what) + {} + }; + + /** + * @method CBitmap + * @brief Default ctor + * @param bitmap pointer to CBitmap instance + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CPixelFormat(CBitmap *bitmap) + : m_bitmap(bitmap) + {} + + /** + * @method ~CPixelFormat + * @brief Default dtor (virtual) + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + virtual ~CPixelFormat() + {}; + + /** + * @method setPixel + * @brief Modifies pixel at coordinates x, y + * @param pixel pointer to new pixel data + * @param x x-coordinate + * @param y y-coordinate + * @return - + * @globalvars none + * @exception PixelFormatError + * @conditions none + */ + virtual void setPixel(const uint32_t *pixel, const uint32_t x, const uint32_t y) = 0; + + /** + * @method getBitCount + * @brief returns color bitcount supported by this class + * @param - + * @return color bitcount supported by this class + * @globalvars none + * @exception none + * @conditions none + */ + virtual uint32_t getBitCount() = 0; + + protected: + /* members */ + /** pointer to CBitmap instance */ + CBitmap *m_bitmap; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue1/imgsynth/cpixelformat_24.cpp b/ue1/imgsynth/cpixelformat_24.cpp new file mode 100644 index 0000000..022b592 --- /dev/null +++ b/ue1/imgsynth/cpixelformat_24.cpp @@ -0,0 +1,47 @@ +/** + * @module cpixelformat_24 + * @author Manuel Mausz, 0728348 + * @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps. + * @date 18.04.2009 + */ + +#include +#include "cpixelformat_24.h" + +using namespace std; + +void CPixelFormat_24::setPixel(const uint32_t *pixel, uint32_t x, uint32_t y) +{ + if (m_bitmap->getPixelData() == NULL) + throw PixelFormatError("No pixelbuffer allocated."); + + uint32_t rowsize = 4 * static_cast( + ((CPixelFormat_24::getBitCount() * abs(m_bitmap->getInfoHeader().biWidth)) + 31) / 32 + ); + + /* if height is positive the y-coordinates are mirrored */ + if (m_bitmap->getInfoHeader().biHeight > 0) + y = m_bitmap->getInfoHeader().biHeight - y - 1; + uint32_t offset = y * rowsize + x * (4 * getBitCount() / 32); + + /* boundary check */ + if (offset + 3 * sizeof(uint8_t) > m_bitmap->getInfoHeader().biSizeImage) + throw PixelFormatError("Pixel position is out of range."); + + /* convert color values to correct types */ + uint8_t data[3]; + try + { + data[0] = boost::numeric_cast(pixel[2]); + data[1] = boost::numeric_cast(pixel[1]); + data[2] = boost::numeric_cast(pixel[0]); + } + catch(boost::numeric::bad_numeric_cast& ex) + { + throw PixelFormatError("Unable to convert pixelcolor to correct size: " + string(ex.what())); + } + + copy(data, data + 3, m_bitmap->getPixelData() + offset); +} + +/* vim: set et sw=2 ts=2: */ diff --git a/ue1/imgsynth/cpixelformat_24.h b/ue1/imgsynth/cpixelformat_24.h new file mode 100644 index 0000000..e4fcc41 --- /dev/null +++ b/ue1/imgsynth/cpixelformat_24.h @@ -0,0 +1,78 @@ +/** + * @module cpixelformat_24 + * @author Manuel Mausz, 0728348 + * @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps. + * @date 18.04.2009 + */ + +#ifndef CPIXELFORMAT_24_H +#define CPIXELFORMAT_24_H + +#include +#include "cpixelformat.h" + +/** + * @class CPixelFormat_24 + * @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps. + * + * On error CPixelFormat::PixelFormatError is thrown. + */ +class CPixelFormat_24 : public CPixelFormat +{ + public: + /** + * @method CPixelFormat_24 + * @brief Default ctor + * @param bitmap pointer to CBitmap instance + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CPixelFormat_24(CBitmap *bitmap) + : CPixelFormat(bitmap) + {} + + /** + * @method ~CPixelFormat_24 + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ~CPixelFormat_24() + {} + + /** + * @method setPixel + * @brief Modifies pixel at coordinates x, y + * @param pixel pointer to new pixel data + * @param x x-coordinate + * @param y y-coordinate + * @return - + * @globalvars none + * @exception PixelFormatError + * @conditions none + */ + void setPixel(const uint32_t *pixel, uint32_t x, uint32_t y); + + /** + * @method getBitCount + * @brief returns color bitcount supported by this class + * @param - + * @return color bitcount supported by this class + * @globalvars none + * @exception none + * @conditions none + */ + uint32_t getBitCount() + { + return 24; + } +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue1/imgsynth/cscriptparser.cpp b/ue1/imgsynth/cscriptparser.cpp new file mode 100644 index 0000000..4854064 --- /dev/null +++ b/ue1/imgsynth/cscriptparser.cpp @@ -0,0 +1,208 @@ +/** + * @module cscriptparser + * @author Manuel Mausz, 0728348 + * @brief class for parsing simple scriptfiles + * @date 17.04.2009 + */ + +#include +#include +#include +#include "cscriptparser.h" +#include "cbitmap.h" + +using namespace std; +using namespace boost; + +CScriptparser::CScriptparser(const std::string& scriptfile) + : m_scriptfile(scriptfile), m_handler(NULL) +{ + /* add our handlers */ + m_handlers.insert(new CBitmap); +} + +/*----------------------------------------------------------------------------*/ + +CScriptparser::~CScriptparser() +{ + /* delete image handlers */ + set::iterator it; + for (it = m_handlers.begin(); it != m_handlers.end(); it++) + delete *it; +} + +/*----------------------------------------------------------------------------*/ + +void CScriptparser::parse() +{ + /* open and read file */ + ifstream file(m_scriptfile.c_str(), ios::in); + if (!file) + throw ParserError("Unable to open scriptfile '" + m_scriptfile + "'."); + + while (!file.eof() && file.good()) + { + /* read file pre line */ + getline(file, m_curline); + if (m_curline.empty()) + continue; + + trim(m_curline); + + /* ignore comments */ + if (m_curline.find_first_of('#') == 0) + continue; + + /* line has no function call */ + size_t pos1 = m_curline.find_first_of('('); + size_t pos2 = m_curline.find_last_of(')'); + if (pos1 == string::npos || pos2 == string::npos) + continue; + + /* first parse function name and tokenize all parameters */ + string func = m_curline.substr(0, pos1); + string params = m_curline.substr(pos1 + 1, pos2 - pos1 - 1); + list funcparams; + tokenizer< char_separator > tokens(params, char_separator(",")); + /* BOOST_FOREACH isn't available on OOP-servers... */ + for (tokenizer< char_separator >::iterator it = tokens.begin(); + it != tokens.end(); + it++) + { + string tok(*it); + trim(tok); + if (tok.find_first_of(' ') != string::npos) + { + if (tok.find_first_of('"') == string::npos) + throw ParserError("Invalid syntax", m_curline); + } + trim_if(tok, is_any_of("\"")); + funcparams.push_back(tok); + } + + /* then call the corresponding function */ + callFunc(func, funcparams); + } + + file.close(); +} + +/*----------------------------------------------------------------------------*/ + +void CScriptparser::callFunc(const std::string& func, const std::list& funcparams) +{ + if (func.empty()) + throw ParserError("Function name is empty.", m_curline); + + if (func == "read") + read(funcparams); + else if (func == "write") + write(funcparams); + else + { + if (m_handler == NULL) + throw ParserError("No image is being processed.", m_curline); + + /* call function from handler */ + try + { + m_handler->callFunc(func, funcparams); + } + catch(CFile::FileError& ex) + { + throw ParserError(ex.what(), m_curline); + } + } +} + +/*----------------------------------------------------------------------------*/ + +void CScriptparser::read(std::list funcparams) +{ + /* check prerequirements */ + if (funcparams.size() != 2) + throw ParserError("Invalid number of function parameters (must be 2).", m_curline); + if (m_handler != NULL) + throw ParserError("An image is already being processed. Unable to open another.", m_curline); + + string type = funcparams.front(); + to_upper(type); + funcparams.pop_front(); + string filename = funcparams.front(); + + /* fetch image handler supporting requested filetype */ + m_handler = NULL; + set::iterator it; + for (it = m_handlers.begin(); it != m_handlers.end(); it++) + { + if ((*it)->supportsType(type)) + { + m_handler = *it; + break; + } + } + if (m_handler == NULL) + throw ParserError("Unknown filetype.", m_curline); + + /* open file in binary mode */ + ifstream file(filename.c_str(), ios::in | ios::binary); + if (!file) + throw ParserError("Unable to read file.", m_curline); + + /* let handlers read() parse the file */ + try + { + m_handler->read(file); + if (!file.good()) + throw ParserError("Error while reading image file.", m_curline); + file.close(); + } + catch(CFile::FileError& ex) + { + file.close(); + throw ParserError(ex.what(), m_curline); + } +} + +/*----------------------------------------------------------------------------*/ + +void CScriptparser::write(std::list funcparams) +{ + /* check prerequirements */ + if (funcparams.size() != 2) + throw ParserError("Invalid number of function parameters (must be 2).", m_curline); + if (m_handler == NULL) + throw ParserError("No image is being processed.", m_curline); + + string type = funcparams.front(); + to_upper(type); + funcparams.pop_front(); + string filename = funcparams.front(); + + /* do we have an image handler supporting the filetype? */ + if (!m_handler->supportsType(type)) + throw ParserError("Unknown filetype.", m_curline); + + /* open file in binary mode */ + ofstream file(filename.c_str(), ios::out | ios::binary); + if (!file) + throw ParserError("Unable to open file.", m_curline); + + /* let handlers write() parse the file */ + try + { + m_handler->write(file); + if (!file.good()) + throw ParserError("Error while writing image file.", m_curline); + file.close(); + m_handler = NULL; + } + catch(CFile::FileError& ex) + { + file.close(); + m_handler = NULL; + throw ParserError(ex.what(), m_curline); + } +} + +/* vim: set et sw=2 ts=2: */ diff --git a/ue1/imgsynth/cscriptparser.h b/ue1/imgsynth/cscriptparser.h new file mode 100644 index 0000000..01bb953 --- /dev/null +++ b/ue1/imgsynth/cscriptparser.h @@ -0,0 +1,181 @@ +/** + * @module cscriptparser + * @author Manuel Mausz, 0728348 + * @brief class for parsing simple scriptfiles + * @date 17.04.2009 + */ + +#ifndef CSCRIPTPARSER_H +#define CSCRIPTPARSER_H + +#include +#include +#include +#include +#include "cfile.h" + +/** + * @class CScriptparser + * + * Parses a simple line based scriptfile with some limitations: + * first function (starting a block) must be a read-command, + * last must be a write-command (ending this block). + * + * read- and write-commands have hard coded parameters, number#1 being a filetype. + * Classes handling certain filetypes must be of type CFile. + * Custom functions will be passed to CFile::callFunc(). + * + * On error ParserError will be thrown. + */ +class CScriptparser +{ + public: + /** + * @class ParserError + * @brief Exception thrown by CScriptparser + */ + class ParserError : public std::invalid_argument { + public: + /** + * @method ParserError + * @brief Default exception ctor + * @param what message to pass along + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ParserError(const std::string& what) + : std::invalid_argument(what), m_line("") + {} + + /** + * @method ParserError + * @brief Custom exception ctor + * @param what message to pass along + * @param line scriptline which is currently being parsed + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ParserError(const std::string& what, const std::string& line) + : std::invalid_argument(what), m_line(line) + {} + + /** + * @method ~ParserError + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception not allowed + * @conditions none + */ + ~ParserError() throw() + {} + + /** + * @method getLine + * @brief returns reference to currently parsed scriptline (if set) + * @return reference to currently parsed scriptline (maybe empty string) + * @globalvars none + * @exception none + * @conditions none + */ + const std::string &getLine() + { + return m_line; + } + + private: + /* members*/ + std::string m_line; + }; + + /** + * @method CScriptparser + * @brief Default ctor + * @param scriptfile filename of script to parse + * @return - + * @globalvars none + * @exception bad_alloc + * @conditions none + */ + CScriptparser(const std::string& scriptfile); + + /** + * @method ~CScriptparser + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ~CScriptparser(); + + /** + * @method parse + * @brief Start parsing the scriptfile + * @param - + * @return - + * @globalvars none + * @exception ParserError + * @conditions none + */ + void parse(); + + protected: + /** + * @method callFunc + * @brief Delegates the function and its parameters to the correct + * method (internal or handler) + * @param func function name + * @param funcparams function parameters as list + * @return - + * @globalvars none + * @exception ParserError + * @conditions none + */ + void callFunc(const std::string& func, const std::list& funcparams); + + /** + * @method read + * @brief Handles/wrappes read-command. according to the filetype the + * read-method of the corresponding handler will be called inside. + * @param funcparams function parameters as list + * @return - + * @globalvars none + * @exception ParserError + * @conditions none + * + * Scriptfile syntax: read(, ) + */ + void read(std::list funcparams); + + /** + * @method write + * @brief Handles/wrappes write-command. according to the filetype the + * write-method of the corresponding handler will be called inside. + * @param funcparams function parameters as list + * @return - + * @globalvars none + * @exception ParserError + * @conditions none + * + * Scriptfile syntax: write(, ) + */ + void write(std::list funcparams); + + private: + /* members */ + std::set m_handlers; + std::string m_scriptfile; + std::string m_curline; + CFile *m_handler; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue1/imgsynth/imgsynth.cbp b/ue1/imgsynth/imgsynth.cbp new file mode 100644 index 0000000..7badc7c --- /dev/null +++ b/ue1/imgsynth/imgsynth.cbp @@ -0,0 +1,60 @@ + + + + + + diff --git a/ue1/imgsynth/imgsynth.cpp b/ue1/imgsynth/imgsynth.cpp new file mode 100644 index 0000000..a172c76 --- /dev/null +++ b/ue1/imgsynth/imgsynth.cpp @@ -0,0 +1,84 @@ +/** + * @module imgsynth + * @author Manuel Mausz, 0728348 + * @brief imgsynth reads a scriptfile given as commandline option + * and executes all known function inside. + * On error (e.g. unknown function) the program will terminate + * @date 17.04.2009 + * @par Exercise + * 1 + */ + +#include +#include +#include "cscriptparser.h" + +using namespace std; +namespace po = boost::program_options; + +/** + * @func main + * @brief program entry point + * @param argc standard parameter of main + * @param argv standard parameter of main + * @return 0 on success, not 0 otherwise + * @globalvars none + * @exception none + * @conditions none + * + * setup commandline options, parse them and pass scriptfile to scriptparser + * instance. On error print error message to stderr. + * Unknown commandline options will print a usage message. + */ +int main(int argc, char* argv[]) +{ + string me(argv[0]); + + /* define commandline options */ + po::options_description desc("Allowed options"); + desc.add_options() + ("help,h", "this help message") + ("input,i", po::value(), "input scriptfile"); + + /* parse commandline options */ + po::variables_map vm; + try + { + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + } + catch(po::error& ex) + { + cerr << "Error: " << ex.what() << endl; + } + + /* print usage upon request or missing params */ + if (vm.count("help") || !vm.count("input")) + { + cout << "Usage: " << me << " -i " << endl; + cout << desc << endl; + return 0; + } + + CScriptparser parser(vm["input"].as()); + try + { + parser.parse(); + } + catch(CScriptparser::ParserError& ex) + { + cerr << me << ": Error while processing scriptfile: " << ex.what() << endl; + if (!ex.getLine().empty()) + cerr << "Scriptline: '" << ex.getLine() << "'" << endl; + return 1; + } + catch(exception& ex) + { + cerr << me << ": Unexpected exception: " << ex.what() << endl; + return 1; + } + + return 0; +} + +/* vim: set et sw=2 ts=2: */ diff --git a/ue1/imgsynth/imgsynth.layout b/ue1/imgsynth/imgsynth.layout new file mode 100644 index 0000000..9e25fa1 --- /dev/null +++ b/ue1/imgsynth/imgsynth.layout @@ -0,0 +1,7 @@ + + + + + + + diff --git a/ue1/imgsynth/test/input b/ue1/imgsynth/test/input new file mode 100644 index 0000000..8036ae3 --- /dev/null +++ b/ue1/imgsynth/test/input @@ -0,0 +1,6 @@ +read(BMP, test/yellow_man_in.bmp) +fillrect(0,3,6,5,0,255,0) +fillrect(2,13,7,4,0,0,255) +write(BMP, "test/yellow_man_out.bmp") + + diff --git a/ue1/imgsynth/test/test.sh b/ue1/imgsynth/test/test.sh new file mode 100755 index 0000000..45ea077 --- /dev/null +++ b/ue1/imgsynth/test/test.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +rm -f test/yellow_man_out.bmp +./imgsynth -i test/input +if [ "$(md5sum < test/yellow_man_out.bmp)" != "$(md5sum < test/yellow_man_ref.bmp)" ] +then + echo "ERROR: test/yellow_man_ref.bmp and test/yellow_man_ref.bmp differ" + exit 1 +else + echo "Test successful" +fi + diff --git a/ue1/imgsynth/test/yellow_man_in.bmp b/ue1/imgsynth/test/yellow_man_in.bmp new file mode 100644 index 0000000..49372b2 Binary files /dev/null and b/ue1/imgsynth/test/yellow_man_in.bmp differ diff --git a/ue1/imgsynth/test/yellow_man_out.bmp b/ue1/imgsynth/test/yellow_man_out.bmp new file mode 100644 index 0000000..340eab9 Binary files /dev/null and b/ue1/imgsynth/test/yellow_man_out.bmp differ diff --git a/ue1/imgsynth/test/yellow_man_ref.bmp b/ue1/imgsynth/test/yellow_man_ref.bmp new file mode 100644 index 0000000..340eab9 Binary files /dev/null and b/ue1/imgsynth/test/yellow_man_ref.bmp differ -- cgit v1.2.3