From aa139a7d2b3f26af7590edbf413df67195c5d900 Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 27 Apr 2009 00:25:16 +0200 Subject: Adding ue2 --- ue2/imgsynth2/Makefile | 41 +++ ue2/imgsynth2/cbitmap.cpp | 382 ++++++++++++++++++++++++++ ue2/imgsynth2/cbitmap.h | 236 ++++++++++++++++ ue2/imgsynth2/cfile.h | 121 ++++++++ ue2/imgsynth2/cpixelformat.h | 124 +++++++++ ue2/imgsynth2/cpixelformat_bgr24.cpp | 90 ++++++ ue2/imgsynth2/cpixelformat_bgr24.h | 89 ++++++ ue2/imgsynth2/cpixelformat_bgr555.cpp | 46 ++++ ue2/imgsynth2/cpixelformat_bgr555.h | 96 +++++++ ue2/imgsynth2/cscriptparser.cpp | 209 ++++++++++++++ ue2/imgsynth2/cscriptparser.h | 181 ++++++++++++ ue2/imgsynth2/imgsynth2.cpp | 84 ++++++ ue2/imgsynth2/test/input_yellow_man1_rgb24 | 10 + ue2/imgsynth2/test/input_yellow_man1_rgb555 | 10 + ue2/imgsynth2/test/input_yellow_man2_rgb24 | 13 + ue2/imgsynth2/test/input_yellow_man2_rgb555 | 13 + ue2/imgsynth2/test/test.sh | 48 ++++ ue2/imgsynth2/test/yellow_man1_rgb24_in.bmp | Bin 0 -> 530 bytes ue2/imgsynth2/test/yellow_man1_rgb24_out.bmp | Bin 0 -> 530 bytes ue2/imgsynth2/test/yellow_man1_rgb24_ref.bmp | Bin 0 -> 530 bytes ue2/imgsynth2/test/yellow_man1_rgb555_in.bmp | Bin 0 -> 394 bytes ue2/imgsynth2/test/yellow_man1_rgb555_out.bmp | Bin 0 -> 394 bytes ue2/imgsynth2/test/yellow_man1_rgb555_ref.bmp | Bin 0 -> 394 bytes ue2/imgsynth2/test/yellow_man2_rgb24_in.bmp | Bin 0 -> 530 bytes ue2/imgsynth2/test/yellow_man2_rgb24_out.bmp | Bin 0 -> 530 bytes ue2/imgsynth2/test/yellow_man2_rgb24_ref.bmp | Bin 0 -> 530 bytes ue2/imgsynth2/test/yellow_man2_rgb555_in.bmp | Bin 0 -> 394 bytes ue2/imgsynth2/test/yellow_man2_rgb555_out.bmp | Bin 0 -> 394 bytes ue2/imgsynth2/test/yellow_man2_rgb555_ref.bmp | Bin 0 -> 394 bytes ue2/imgsynth2/x1 | 26 ++ ue2/imgsynth2/x2 | 26 ++ 31 files changed, 1845 insertions(+) create mode 100644 ue2/imgsynth2/Makefile create mode 100644 ue2/imgsynth2/cbitmap.cpp create mode 100644 ue2/imgsynth2/cbitmap.h create mode 100644 ue2/imgsynth2/cfile.h create mode 100644 ue2/imgsynth2/cpixelformat.h create mode 100644 ue2/imgsynth2/cpixelformat_bgr24.cpp create mode 100644 ue2/imgsynth2/cpixelformat_bgr24.h create mode 100644 ue2/imgsynth2/cpixelformat_bgr555.cpp create mode 100644 ue2/imgsynth2/cpixelformat_bgr555.h create mode 100644 ue2/imgsynth2/cscriptparser.cpp create mode 100644 ue2/imgsynth2/cscriptparser.h create mode 100644 ue2/imgsynth2/imgsynth2.cpp create mode 100644 ue2/imgsynth2/test/input_yellow_man1_rgb24 create mode 100644 ue2/imgsynth2/test/input_yellow_man1_rgb555 create mode 100644 ue2/imgsynth2/test/input_yellow_man2_rgb24 create mode 100644 ue2/imgsynth2/test/input_yellow_man2_rgb555 create mode 100755 ue2/imgsynth2/test/test.sh create mode 100644 ue2/imgsynth2/test/yellow_man1_rgb24_in.bmp create mode 100644 ue2/imgsynth2/test/yellow_man1_rgb24_out.bmp create mode 100644 ue2/imgsynth2/test/yellow_man1_rgb24_ref.bmp create mode 100644 ue2/imgsynth2/test/yellow_man1_rgb555_in.bmp create mode 100644 ue2/imgsynth2/test/yellow_man1_rgb555_out.bmp create mode 100644 ue2/imgsynth2/test/yellow_man1_rgb555_ref.bmp create mode 100644 ue2/imgsynth2/test/yellow_man2_rgb24_in.bmp create mode 100644 ue2/imgsynth2/test/yellow_man2_rgb24_out.bmp create mode 100644 ue2/imgsynth2/test/yellow_man2_rgb24_ref.bmp create mode 100644 ue2/imgsynth2/test/yellow_man2_rgb555_in.bmp create mode 100644 ue2/imgsynth2/test/yellow_man2_rgb555_out.bmp create mode 100644 ue2/imgsynth2/test/yellow_man2_rgb555_ref.bmp create mode 100644 ue2/imgsynth2/x1 create mode 100644 ue2/imgsynth2/x2 (limited to 'ue2/imgsynth2') diff --git a/ue2/imgsynth2/Makefile b/ue2/imgsynth2/Makefile new file mode 100644 index 0000000..cbf3d33 --- /dev/null +++ b/ue2/imgsynth2/Makefile @@ -0,0 +1,41 @@ +# Makefile for imgsynth2 +# Author: Guenther Neuwirth (0626638), Manuel Mausz (0728348) +# Created: 26.04.2009 + +CC= g++ +LD= $(CC) +DEBUGFLAGS= -DNDEBUG +CFLAGS= -O -ansi -pedantic-errors -Wall $(DEBUGFLAGS) +LDFLAGS= +LIBS= -lboost_program_options + +BIN= imgsynth2 +OBJS= cpixelformat_bgr24.o cpixelformat_bgr555.o \ + cbitmap.o cscriptparser.o imgsynth2.o +HEADERS= cpixelformat.h cpixelformat_bgr24.h cpixelformat_bgr555.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/ue2/imgsynth2/cbitmap.cpp b/ue2/imgsynth2/cbitmap.cpp new file mode 100644 index 0000000..fc1a7c0 --- /dev/null +++ b/ue2/imgsynth2/cbitmap.cpp @@ -0,0 +1,382 @@ +/** + * @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 new file mode 100644 index 0000000..ca3d6ef --- /dev/null +++ b/ue2/imgsynth2/cbitmap.h @@ -0,0 +1,236 @@ +/** + * @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/cfile.h b/ue2/imgsynth2/cfile.h new file mode 100644 index 0000000..a5c4d2a --- /dev/null +++ b/ue2/imgsynth2/cfile.h @@ -0,0 +1,121 @@ +/** + * @module cfile + * @author Guenther Neuwirth (0626638), 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/ue2/imgsynth2/cpixelformat.h b/ue2/imgsynth2/cpixelformat.h new file mode 100644 index 0000000..49145df --- /dev/null +++ b/ue2/imgsynth2/cpixelformat.h @@ -0,0 +1,124 @@ +/** + * @module cpixelformat + * @author Guenther Neuwirth (0626638), 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 getPixel + * @brief Get pixel at coordinates x, y + * @param pixel pointer to pixel data + * @param x x-coordinate + * @param y y-coordinate + * @return - + * @globalvars none + * @exception PixelFormatError + * @conditions none + */ + //TODO virtual void getPixel(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; + + /* + * TODO + */ + virtual void getMaxColor(unsigned int *red, unsigned int *green, unsigned int *blue) = 0; + + protected: + /* members */ + /** pointer to CBitmap instance */ + CBitmap *m_bitmap; +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue2/imgsynth2/cpixelformat_bgr24.cpp b/ue2/imgsynth2/cpixelformat_bgr24.cpp new file mode 100644 index 0000000..cc02dcc --- /dev/null +++ b/ue2/imgsynth2/cpixelformat_bgr24.cpp @@ -0,0 +1,90 @@ +/** + * @module cpixelformat_BGR24 + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps. + * @date 18.04.2009 + */ + +#include +#include "cpixelformat_bgr24.h" + +using namespace std; + +void CPixelFormat_BGR24::getPixel(uint32_t *pixel, uint32_t x, uint32_t y) +{ + /* + * pixel[0] ... red + * pixel[1] ... green + * pixel[2] ... blue + */ + if (m_bitmap->getPixelData() == NULL) + throw PixelFormatError("No pixelbuffer allocated."); + + /* calc rowsize - boundary is 32 */ + uint32_t rowsize = 4 * static_cast( + ((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 + getBitCount()/8 > m_bitmap->getInfoHeader().biSizeImage) + throw PixelFormatError("Pixel position is out of range."); + + /* get pixel */ + try + { + pixel[0] = boost::numeric_cast(*(m_bitmap->getPixelData() + offset + 2)); + pixel[1] = boost::numeric_cast(*(m_bitmap->getPixelData() + offset + 1)); + pixel[2] = boost::numeric_cast(*(m_bitmap->getPixelData() + offset)); + } + catch(boost::numeric::bad_numeric_cast& ex) + { + throw PixelFormatError("Unable to convert pixelcolor to correct size: " + string(ex.what())); + } +} + +void CPixelFormat_BGR24::setPixel(const uint32_t *pixel, uint32_t x, uint32_t y) +{ + /* + * pixel[0] ... red + * pixel[1] ... green + * pixel[2] ... blue + */ + if (m_bitmap->getPixelData() == NULL) + throw PixelFormatError("No pixelbuffer allocated."); + + /* calc rowsize - boundary is 32 */ + uint32_t rowsize = 4 * static_cast( + ((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 + getBitCount()/8 > 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/ue2/imgsynth2/cpixelformat_bgr24.h b/ue2/imgsynth2/cpixelformat_bgr24.h new file mode 100644 index 0000000..c3e1b72 --- /dev/null +++ b/ue2/imgsynth2/cpixelformat_bgr24.h @@ -0,0 +1,89 @@ +/** + * @module cpixelformat_bgr24 + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps. + * @date 18.04.2009 + */ + +#ifndef CPIXELFORMAT_BGR24_H +#define CPIXELFORMAT_BGR24_H + +#include +#include "cpixelformat.h" + +/** + * @class CPixelFormat_BGR24 + * @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps. + * + * On error CPixelFormat::PixelFormatError is thrown. + */ +class CPixelFormat_BGR24 : public CPixelFormat +{ + public: + /** + * @method CPixelFormat_BGR24 + * @brief Default ctor + * @param bitmap pointer to CBitmap instance + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CPixelFormat_BGR24(CBitmap *bitmap) + : CPixelFormat(bitmap) + {} + + /** + * @method ~CPixelFormat_BGR24 + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ~CPixelFormat_BGR24() + {} + + /** + * @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); + + /* TODO */ + void getPixel(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; + } + + /* + * TODO + */ + void getMaxColor(unsigned int *red, unsigned int *green, unsigned int *blue) + { + *red = *green = *blue = 255; /* 2^8 - 1 */ + } +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue2/imgsynth2/cpixelformat_bgr555.cpp b/ue2/imgsynth2/cpixelformat_bgr555.cpp new file mode 100644 index 0000000..4a3c833 --- /dev/null +++ b/ue2/imgsynth2/cpixelformat_bgr555.cpp @@ -0,0 +1,46 @@ +/** + * @module cpixelformat_BGR555 + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Implementation of CPixelFormat handling BGR555 color (real color) + * Windows Bitmaps. + * @date 18.04.2009 + */ + +#include +#include "cpixelformat_bgr555.h" + +using namespace std; + +void CPixelFormat_BGR555::setPixel(const uint32_t *pixel, uint32_t x, uint32_t y) +{ + /* + * pixel[0] ... red + * pixel[1] ... green + * pixel[2] ... blue + */ + + if (m_bitmap->getPixelData() == NULL) + throw PixelFormatError("No pixelbuffer allocated."); + + /* calc rowsize - boundary is 32 */ + uint32_t rowsize = 4 * static_cast( + ((CPixelFormat_BGR555::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 + getBitCount()/8 > m_bitmap->getInfoHeader().biSizeImage) + throw PixelFormatError("Pixel position is out of range."); + + /* convert color values to correct types */ + uint8_t *o = m_bitmap->getPixelData() + offset; + *(uint16_t *)o = (uint16_t)(((pixel[2] << BGR555_BLUE_SHIFT) & BGR555_BLUE_MASK) | + ((pixel[1] << BGR555_GREEN_SHIFT) & BGR555_GREEN_MASK) | + ((pixel[0] << BGR555_RED_SHIFT) & BGR555_RED_MASK)); +} + +/* vim: set et sw=2 ts=2: */ diff --git a/ue2/imgsynth2/cpixelformat_bgr555.h b/ue2/imgsynth2/cpixelformat_bgr555.h new file mode 100644 index 0000000..7a49c7c --- /dev/null +++ b/ue2/imgsynth2/cpixelformat_bgr555.h @@ -0,0 +1,96 @@ +/** + * @module cpixelformat_bgr555 + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief Implementation of CPixelFormat handling BGR555 color (real color) + * Windows Bitmaps. + * @date 26.04.2009 + */ + +#ifndef CPIXELFORMAT_BGR555_H +#define CPIXELFORMAT_BGR555_H + +#include +#include "cpixelformat.h" + +/* TODO */ +#define BGR555_RED_SHIFT 10 +#define BGR555_GREEN_SHIFT 5 +#define BGR555_BLUE_SHIFT 0 +#define BGR555_RED_MASK 0x7C00 +#define BGR555_GREEN_MASK 0x03E0 +#define BGR555_BLUE_MASK 0x001F + +/** + * @class CPixelFormat_BGR555 + * @brief Implementation of CPixelFormat handling BGR555 color (real color) + * Windows Bitmaps. + * + * On error CPixelFormat::PixelFormatError is thrown. + */ +class CPixelFormat_BGR555 : public CPixelFormat +{ + public: + /** + * @method CPixelFormat_BGR55 + * @brief Default ctor + * @param bitmap pointer to CBitmap instance + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + CPixelFormat_BGR555(CBitmap *bitmap) + : CPixelFormat(bitmap) + {} + + /** + * @method ~CPixelFormat_BGR555 + * @brief Default dtor + * @param - + * @return - + * @globalvars none + * @exception none + * @conditions none + */ + ~CPixelFormat_BGR555() + {} + + /** + * @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 16; + } + + /* + * TODO + */ + void getMaxColor(unsigned int *red, unsigned int *green, unsigned int *blue) + { + *red = *green = *blue = 31; /* 2^5 -1 */ + } +}; + +#endif + +/* vim: set et sw=2 ts=2: */ diff --git a/ue2/imgsynth2/cscriptparser.cpp b/ue2/imgsynth2/cscriptparser.cpp new file mode 100644 index 0000000..df3df1e --- /dev/null +++ b/ue2/imgsynth2/cscriptparser.cpp @@ -0,0 +1,209 @@ +/** + * @module cscriptparser + * @author Guenther Neuwirth (0626638), 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; + m_handler = NULL; +} + +/*----------------------------------------------------------------------------*/ + +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) + throw ParserError("Invalid syntax. Not a function", m_curline); + + /* 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/ue2/imgsynth2/cscriptparser.h b/ue2/imgsynth2/cscriptparser.h new file mode 100644 index 0000000..d4ebd0e --- /dev/null +++ b/ue2/imgsynth2/cscriptparser.h @@ -0,0 +1,181 @@ +/** + * @module cscriptparser + * @author Guenther Neuwirth (0626638), 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/ue2/imgsynth2/imgsynth2.cpp b/ue2/imgsynth2/imgsynth2.cpp new file mode 100644 index 0000000..135d31f --- /dev/null +++ b/ue2/imgsynth2/imgsynth2.cpp @@ -0,0 +1,84 @@ +/** + * @module imgsynth2 + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief imgsynth2 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 + * 2 + */ + +#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/ue2/imgsynth2/test/input_yellow_man1_rgb24 b/ue2/imgsynth2/test/input_yellow_man1_rgb24 new file mode 100644 index 0000000..ce2743b --- /dev/null +++ b/ue2/imgsynth2/test/input_yellow_man1_rgb24 @@ -0,0 +1,10 @@ +#in: test/yellow_man1_rgb24_in.bmp +#out: test/yellow_man1_rgb24_out.bmp +#ref: test/yellow_man1_rgb24_ref.bmp + +read(BMP, test/yellow_man1_rgb24_in.bmp) + +fillrect(0,3,6,5,0,255,0) +fillrect(2,13,7,4,0,0,255) + +write(BMP, "test/yellow_man1_rgb24_out.bmp") diff --git a/ue2/imgsynth2/test/input_yellow_man1_rgb555 b/ue2/imgsynth2/test/input_yellow_man1_rgb555 new file mode 100644 index 0000000..1723489 --- /dev/null +++ b/ue2/imgsynth2/test/input_yellow_man1_rgb555 @@ -0,0 +1,10 @@ +#in: test/yellow_man1_rgb555_in.bmp +#out: test/yellow_man1_rgb555_out.bmp +#ref: test/yellow_man1_rgb555_ref.bmp + +read(BMP, test/yellow_man1_rgb555_in.bmp) + +fillrect(0,3,6,5,0,31,0) +fillrect(2,13,7,4,0,0,31) + +write(BMP, "test/yellow_man1_rgb555_out.bmp") diff --git a/ue2/imgsynth2/test/input_yellow_man2_rgb24 b/ue2/imgsynth2/test/input_yellow_man2_rgb24 new file mode 100644 index 0000000..37108b8 --- /dev/null +++ b/ue2/imgsynth2/test/input_yellow_man2_rgb24 @@ -0,0 +1,13 @@ +#in: test/yellow_man2_rgb24_in.bmp +#out: test/yellow_man2_rgb24_out.bmp +#ref: test/yellow_man2_rgb24_ref.bmp + +read(BMP, test/yellow_man2_rgb24_in.bmp) + +fillrect(0,3,6,5,0,255,0) +fillrect(2,13,6,4,0,0,255) +mirror_x() +mirror_y() +#invert() + +write(BMP, "test/yellow_man2_rgb24_out.bmp") diff --git a/ue2/imgsynth2/test/input_yellow_man2_rgb555 b/ue2/imgsynth2/test/input_yellow_man2_rgb555 new file mode 100644 index 0000000..3c00301 --- /dev/null +++ b/ue2/imgsynth2/test/input_yellow_man2_rgb555 @@ -0,0 +1,13 @@ +#in: test/yellow_man2_rgb555_in.bmp +#out: test/yellow_man2_rgb555_out.bmp +#ref: test/yellow_man2_rgb555_ref.bmp + +read(BMP, test/yellow_man2_rgb555_in.bmp) + +fillrect(0,3,6,5,0,24,0) +fillrect(2,13,7,4,0,0,24) +mirror_x() +mirror_y() +#invert() + +write(BMP, "test/yellow_man2_rgb555_out.bmp") diff --git a/ue2/imgsynth2/test/test.sh b/ue2/imgsynth2/test/test.sh new file mode 100755 index 0000000..5d760b9 --- /dev/null +++ b/ue2/imgsynth2/test/test.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +inputs=( $(ls test/input_* | sort -n) ) + +for input in ${inputs[@]} +do + echo "Testing $input ..." + + files=() + for tag in "#in: " "#out: " "#ref: " + do + tmp=$(grep "$tag" "$input") + if [ -z "$tmp" ] + then + echo " ERROR: Tag '$tag' missing" + exit 1 + fi + + tmp=${tmp:${#tag}} + #if [ ! -e "$tmp" ] + #then + # echo " ERROR: File '$tmp' doesn't exist" + # exit 1 + #fi + + files+=("$tmp") + done + + rm -f "${files[1]}" + ./imgsynth2 -i "$input" + ret=$? + if [ $ret -ne 0 ] + then + echo " ERROR: Script didn't exit properly" + exit 1 + fi + + md5_1=$(md5sum < "${files[1]}") + md5_2=$(md5sum < "${files[2]}") + if [ "$md5_1" != "$md5_2" ] + then + echo " ERROR: ${files[1]} and ${files[2]} differ" + exit 1 + else + echo " Test successful" + fi +done + diff --git a/ue2/imgsynth2/test/yellow_man1_rgb24_in.bmp b/ue2/imgsynth2/test/yellow_man1_rgb24_in.bmp new file mode 100644 index 0000000..49372b2 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man1_rgb24_in.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man1_rgb24_out.bmp b/ue2/imgsynth2/test/yellow_man1_rgb24_out.bmp new file mode 100644 index 0000000..340eab9 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man1_rgb24_out.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man1_rgb24_ref.bmp b/ue2/imgsynth2/test/yellow_man1_rgb24_ref.bmp new file mode 100644 index 0000000..340eab9 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man1_rgb24_ref.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man1_rgb555_in.bmp b/ue2/imgsynth2/test/yellow_man1_rgb555_in.bmp new file mode 100644 index 0000000..3824922 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man1_rgb555_in.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man1_rgb555_out.bmp b/ue2/imgsynth2/test/yellow_man1_rgb555_out.bmp new file mode 100644 index 0000000..1c35ab5 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man1_rgb555_out.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man1_rgb555_ref.bmp b/ue2/imgsynth2/test/yellow_man1_rgb555_ref.bmp new file mode 100644 index 0000000..1c35ab5 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man1_rgb555_ref.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man2_rgb24_in.bmp b/ue2/imgsynth2/test/yellow_man2_rgb24_in.bmp new file mode 100644 index 0000000..39193a4 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man2_rgb24_in.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man2_rgb24_out.bmp b/ue2/imgsynth2/test/yellow_man2_rgb24_out.bmp new file mode 100644 index 0000000..c859cda Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man2_rgb24_out.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man2_rgb24_ref.bmp b/ue2/imgsynth2/test/yellow_man2_rgb24_ref.bmp new file mode 100644 index 0000000..2ef73e5 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man2_rgb24_ref.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man2_rgb555_in.bmp b/ue2/imgsynth2/test/yellow_man2_rgb555_in.bmp new file mode 100644 index 0000000..812311d Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man2_rgb555_in.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man2_rgb555_out.bmp b/ue2/imgsynth2/test/yellow_man2_rgb555_out.bmp new file mode 100644 index 0000000..60588e3 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man2_rgb555_out.bmp differ diff --git a/ue2/imgsynth2/test/yellow_man2_rgb555_ref.bmp b/ue2/imgsynth2/test/yellow_man2_rgb555_ref.bmp new file mode 100644 index 0000000..1b63825 Binary files /dev/null and b/ue2/imgsynth2/test/yellow_man2_rgb555_ref.bmp differ diff --git a/ue2/imgsynth2/x1 b/ue2/imgsynth2/x1 new file mode 100644 index 0000000..5593645 --- /dev/null +++ b/ue2/imgsynth2/x1 @@ -0,0 +1,26 @@ +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 new file mode 100644 index 0000000..26cbf82 --- /dev/null +++ b/ue2/imgsynth2/x2 @@ -0,0 +1,26 @@ +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