summaryrefslogtreecommitdiffstats
path: root/ue1/imgsynth
diff options
context:
space:
mode:
Diffstat (limited to 'ue1/imgsynth')
-rw-r--r--ue1/imgsynth/Makefile39
-rw-r--r--ue1/imgsynth/cbitmap.cpp208
-rw-r--r--ue1/imgsynth/cbitmap.h225
-rw-r--r--ue1/imgsynth/cfile.h121
-rw-r--r--ue1/imgsynth/cpixelformat.h106
-rw-r--r--ue1/imgsynth/cpixelformat_24.cpp47
-rw-r--r--ue1/imgsynth/cpixelformat_24.h78
-rw-r--r--ue1/imgsynth/cscriptparser.cpp208
-rw-r--r--ue1/imgsynth/cscriptparser.h181
-rw-r--r--ue1/imgsynth/imgsynth.cbp60
-rw-r--r--ue1/imgsynth/imgsynth.cpp84
-rw-r--r--ue1/imgsynth/imgsynth.layout7
-rw-r--r--ue1/imgsynth/test/input6
-rwxr-xr-xue1/imgsynth/test/test.sh12
-rw-r--r--ue1/imgsynth/test/yellow_man_in.bmpbin0 -> 530 bytes
-rw-r--r--ue1/imgsynth/test/yellow_man_out.bmpbin0 -> 530 bytes
-rw-r--r--ue1/imgsynth/test/yellow_man_ref.bmpbin0 -> 530 bytes
17 files changed, 1382 insertions, 0 deletions
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 @@
1# Makefile for imgsynth
2# Author: Manuel Mausz (0728348)
3# Created: 14.04.2009
4
5CC= g++
6LD= $(CC)
7DEBUGFLAGS=
8CFLAGS= -O -ansi -pedantic-errors -Wall $(DEBUGFLAGS)
9LDFLAGS=
10LIBS= -lboost_program_options
11
12BIN= imgsynth
13OBJS= cpixelformat_24.o cbitmap.o cscriptparser.o imgsynth.o
14HEADERS= cpixelformat.h cpixelformat_24.h cfile.h cbitmap.h cscriptparser.h
15
16.SUFFIXES: .cpp .o
17
18all: $(BIN)
19
20.cpp.o:
21 $(CC) $(CFLAGS) -c $< -o $@
22
23$(OBJS): $(HEADERS)
24
25$(BIN): $(OBJS)
26 $(LD) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
27
28debug:
29 @$(MAKE) all "DEBUGFLAGS=-DDEBUG -g"
30
31clean:
32 rm -f $(OBJS) $(BIN)
33
34run test: all
35 @./test/test.sh
36
37.PHONY: clean
38
39# 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 @@
1/**
2 * @module cbitmap
3 * @author Manuel Mausz, 0728348
4 * @brief Implementation of CFile handling Windows Bitmaps.
5 * @date 17.04.2009
6 */
7
8#include <boost/lexical_cast.hpp>
9#include <boost/numeric/conversion/cast.hpp>
10#ifdef DEBUG
11# include <iostream>
12#endif
13#include "cbitmap.h"
14#include "cpixelformat_24.h"
15
16using namespace std;
17
18CBitmap::~CBitmap()
19{
20 if (m_pixeldata != NULL)
21 delete[] m_pixeldata;
22 m_pixeldata = NULL;
23
24 if (m_pixelformat != NULL)
25 delete m_pixelformat;
26 m_pixelformat = NULL;
27}
28
29/*----------------------------------------------------------------------------*/
30
31void CBitmap::read(std::ifstream& in)
32{
33 /* read and check file header */
34 in.read(reinterpret_cast<char *>(&m_fileheader), sizeof(m_fileheader));
35
36 if (m_fileheader.bfType[0] != 'B' || m_fileheader.bfType[1] != 'M')
37 throw FileError("Imagefile has invalid Bitmap header.");
38 /* bfSize is unreliable (http://de.wikipedia.org/wiki/Windows_Bitmap) */
39 if (m_fileheader.bfSize < 0)
40 throw FileError("Bitmap filesize is less than zero?");
41
42 /* read and check info header */
43 in.read(reinterpret_cast<char *>(&m_infoheader), sizeof(m_infoheader));
44
45 if (m_infoheader.biSize != 40)
46 throw FileError("Bitmap info header size is invalid.");
47 if (m_infoheader.biPlanes != 1)
48 throw FileError("Bitmap color planes is not set to 1.");
49 if (m_infoheader.biCompression != 0)
50 throw FileError("Bitmap compression is set but not supported.");
51 if (m_infoheader.biSizeImage < 0)
52 throw FileError("Bitmap image size is less than zero?");
53 if (m_infoheader.biClrUsed != 0 || m_infoheader.biClrImportant != 0)
54 throw FileError("Bitmap colortable is used but not supported.");
55
56 /* currently only 24bit */
57 if (m_infoheader.biBitCount != 24)
58 throw FileError("Bitmap bitcount is not supported.");
59
60 /* read pixel data using separate class */
61 if (m_infoheader.biSizeImage > 0)
62 {
63 if (m_pixeldata != NULL)
64 delete[] m_pixeldata;
65 m_pixeldata = new uint8_t[m_infoheader.biSizeImage];
66 in.read(reinterpret_cast<char *>(m_pixeldata), m_infoheader.biSizeImage);
67 }
68
69 /* create pixelformat instance */
70 if (m_pixelformat != NULL)
71 delete m_pixelformat;
72 m_pixelformat = NULL;
73 if (m_infoheader.biBitCount == 24)
74 m_pixelformat = new CPixelFormat_24(this);
75}
76
77/*----------------------------------------------------------------------------*/
78
79void CBitmap::write(std::ofstream& out)
80{
81 /* set header values */
82 m_fileheader.bfSize = m_infoheader.biSizeImage + sizeof(m_infoheader) + sizeof(m_fileheader);
83
84 /* write file header */
85 out.write(reinterpret_cast<char *>(&m_fileheader), sizeof(m_fileheader));
86
87 /* write info header */
88 out.write(reinterpret_cast<char *>(&m_infoheader), sizeof(m_infoheader));
89
90 /* write pixel data */
91 if (m_pixeldata != NULL)
92 out.write(reinterpret_cast<char *>(m_pixeldata), m_infoheader.biSizeImage);
93}
94
95/*----------------------------------------------------------------------------*/
96
97void CBitmap::callFunc(const std::string& func, const std::list<std::string>& params)
98{
99 if (func.empty())
100 throw FileError("Function name is empty.");
101
102 if (func == "fillrect")
103 fillrect(params);
104 else
105 throw FileError("Unknown function '" + func + "'.");
106}
107
108/*----------------------------------------------------------------------------*/
109
110void CBitmap::fillrect(std::list<std::string> params)
111{
112 /* check prerequirements */
113 if (params.size() != 7)
114 throw FileError("Invalid number of function parameters (must be 7).");
115
116 /* convert parameters */
117 uint32_t pparams[7];
118 int i = 0;
119 try
120 {
121 for(i = 0; i < 7; i++)
122 {
123 pparams[i] = boost::lexical_cast<uint32_t>(params.front());
124 params.pop_front();
125 }
126 }
127 catch(boost::bad_lexical_cast& ex)
128 {
129 throw FileError("Invalid parameter (" + params.front() + ").");
130 }
131
132 /* width and height can be negativ */
133 uint32_t width = static_cast<uint32_t>(abs(m_infoheader.biWidth));
134 uint32_t height = static_cast<uint32_t>(abs(m_infoheader.biHeight));
135
136 /* check parameter values are in range */
137 if (pparams[0] < 0 || pparams[0] > width
138 || pparams[1] < 0 || pparams[1] > height)
139 throw FileError("At least one x/y-parameter is out of range.");
140
141 if (pparams[2] < 0 || pparams[2] + pparams[0] > width
142 || pparams[3] < 0 || pparams[3] + pparams[1] > height)
143 throw FileError("At least one w/h-parameter is out of range.");
144
145 if (pparams[4] < 0 || pparams[4] > 255
146 || pparams[5] < 0 || pparams[5] > 255
147 || pparams[6] < 0 || pparams[6] > 255)
148 throw FileError("At least one pixel color parameter is out of range.");
149
150 /* call setPixel for every pixel in the rectangel */
151 if (m_pixeldata != NULL && m_pixelformat != NULL)
152 {
153 for(uint32_t i = pparams[0]; i < pparams[2] + pparams[0]; i++)
154 {
155 for(uint32_t j = pparams[1]; j < pparams[3] + pparams[1]; j++)
156 {
157 try
158 {
159 m_pixelformat->setPixel(&pparams[4], i, j);
160 }
161 catch(CPixelFormat::PixelFormatError& ex)
162 {
163 stringstream errstr;
164 errstr << "Can't set pixel (pos=" << i << "," << j << " col="
165 << pparams[4] << "," << pparams[5] << "," << pparams[6] << "): "
166 << ex.what();
167 throw FileError(errstr.str());
168 }
169 }
170 }
171 }
172}
173
174/*----------------------------------------------------------------------------*/
175
176#ifdef DEBUG
177void CBitmap::dump(std::ostream& out)
178{
179 out
180 << "Bitmap File Header:" << endl
181 << " bfType=" << m_fileheader.bfType[0] << m_fileheader.bfType[1]
182 << ", bfSize=" << m_fileheader.bfSize
183 << ", bfReserved=" << m_fileheader.bfReserved
184 << ", bfOffBits=" << m_fileheader.bfOffBits
185 << endl;
186
187 out
188 << "Bitmap Info Header:" << endl
189 << " biSize=" << m_infoheader.biSize
190 << ", biWidth=" << m_infoheader.biWidth
191 << ", biHeight=" << m_infoheader.biHeight
192 << ", biPlanes=" << m_infoheader.biPlanes
193 << endl
194
195 << " biBitCount=" << m_infoheader.biBitCount
196 << ", biCompression=" << m_infoheader.biCompression
197 << ", biSizeImage=" << m_infoheader.biSizeImage
198 << endl
199
200 << " biXPelsPerMeter=" << m_infoheader.biXPelsPerMeter
201 << ", biYPelsPerMeter=" << m_infoheader.biYPelsPerMeter
202 << ", biClrUsed=" << m_infoheader.biClrUsed
203 << ", biClrImportant=" << m_infoheader.biClrImportant
204 << endl;
205}
206#endif
207
208/* 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 @@
1/**
2 * @module cbitmap
3 * @author Manuel Mausz, 0728348
4 * @brief Implementation of CFile handling Windows Bitmaps.
5 * @date 17.04.2009
6 */
7
8#ifndef CBITMAP_H
9#define CBITMAP_H
10
11#include "cfile.h"
12
13class CPixelFormat;
14#include "cpixelformat.h"
15
16/**
17 * @class CBitmap
18 * @brief Implementation of CFile handling Windows Bitmaps.
19 *
20 * In order to support operations on bitmaps with different color bitcounts
21 * different implementations of CPixelFormat are used. These classes are
22 * allowed to modify the bitmap headers and pixelbuffer directly.
23 *
24 * On error CFile::FileError is thrown.
25 */
26class CBitmap : public CFile
27{
28 public:
29 /**
30 * @method CBitmap
31 * @brief Default ctor
32 * @param -
33 * @return -
34 * @globalvars none
35 * @exception none
36 * @conditions none
37 */
38 CBitmap()
39 : m_pixeldata(NULL), m_pixelformat(NULL)
40 {
41 m_types.insert("BMP");
42 }
43
44 /**
45 * @method ~CBitmap
46 * @brief Default dtor
47 * @param -
48 * @return -
49 * @globalvars none
50 * @exception none
51 * @conditions none
52 */
53 ~CBitmap();
54
55 /**
56 * @method read
57 * @brief Reads Windows Bitmap from filestream.
58 * On error an exception is thrown.
59 * @param in filestream to read data from
60 * @return -
61 * @globalvars none
62 * @exception CFile::FileError
63 * @exception bad_alloc
64 * @conditions none
65 */
66 void read(std::ifstream& in);
67
68 /**
69 * @method write
70 * @brief Writes Windows Bitmap to filestream.
71 * @param out filestream to read data from
72 * @return -
73 * @globalvars none
74 * @exception FileError
75 * @exception bad_alloc
76 * @conditions none
77 */
78 void write(std::ofstream& out);
79
80 /**
81 * @method callFunc
82 * @brief Delegates the function and its parameters to the correct
83 * internal method
84 * @param func function name
85 * @param params function parameters as list
86 * @return -
87 * @globalvars none
88 * @exception ParserError
89 * @conditions none
90 */
91 void callFunc(const std::string& func, const std::list<std::string>& params);
92
93#ifdef DEBUG
94 /**
95 * @method dump
96 * @brief Dumps the Windows Bitmap file headers to ostream
97 * @param out output stream
98 * @return -
99 * @globalvars
100 * @exception
101 * @conditions
102 */
103 void dump(std::ostream& out);
104#endif
105
106 /**
107 * @brief Windows Bitmap File Header structure
108 */
109#pragma pack(push,1)
110 typedef struct
111 {
112 /** the magic number used to identify the BMP file */
113 uint8_t bfType[2];
114 /** the size of the BMP file in bytes */
115 uint32_t bfSize;
116 /** reserved */
117 uint32_t bfReserved;
118 /** the offset of the byte where the bitmap data can be found */
119 uint32_t bfOffBits;
120 } BITMAP_FILEHEADER;
121#pragma pack(pop)
122
123 /**
124 * @brief Windows Bitmap Info Header structure
125 */
126#pragma pack(push,1)
127 typedef struct
128 {
129 /** the size of this header (40 bytes) */
130 uint32_t biSize;
131 /** the bitmap width in pixels (signed integer) */
132 int32_t biWidth;
133 /** the bitmap height in pixels (signed integer) */
134 int32_t biHeight;
135 /** the number of color planes being used. Must be set to 1 */
136 uint16_t biPlanes;
137 /** the number of bits per pixel, which is the color depth of the image */
138 uint16_t biBitCount;
139 /** the compression method being used */
140 uint32_t biCompression;
141 /** the image size */
142 uint32_t biSizeImage;
143 /** the horizontal resolution of the image (pixel per meter) */
144 int32_t biXPelsPerMeter;
145 /** the vertical resolution of the image (pixel per meter) */
146 int32_t biYPelsPerMeter;
147 /** the number of colors in the color palette, or 0 to default to 2^n */
148 uint32_t biClrUsed;
149 /** the number of important colors used, or 0 when every color is
150 * important; generally ignored. */
151 uint32_t biClrImportant;
152 } BITMAP_INFOHEADER;
153#pragma pack(pop)
154
155 /**
156 * @method getFileHeader
157 * @brief Returns reference to fileheader structure of bitmap
158 * @param -
159 * @return reference to fileheader structure
160 * @globalvars none
161 * @exception none
162 * @conditions none
163 */
164 BITMAP_FILEHEADER &getFileHeader()
165 {
166 return m_fileheader;
167 }
168
169 /**
170 * @method getInfoHeader
171 * @brief Returns reference to infoheader structure of bitmap
172 * @param -
173 * @return reference to infoheader structure
174 * @globalvars none
175 * @exception none
176 * @conditions none
177 */
178 BITMAP_INFOHEADER &getInfoHeader()
179 {
180 return m_infoheader;
181 }
182
183 /**
184 * @method getPixelData
185 * @brief Returns pointer to pixelbuffer
186 * @param -
187 * @return pointer to pixelbuffer
188 * @globalvars none
189 * @exception none
190 * @conditions none
191 */
192 uint8_t *getPixelData()
193 {
194 return m_pixeldata;
195 }
196
197 protected:
198 /**
199 * @method fillrect
200 * @brief Fills rectangle in image starting on position x, y
201 * width size width, height and color red, green, blue.
202 * @param params function parameters as list
203 * @return -
204 * @globalvars none
205 * @exception FileError
206 * @conditions none
207 *
208 * Scriptfile syntax: fillrect(x, y, width, height, red, green, blue)
209 */
210 void fillrect(std::list<std::string> params);
211
212 /* members */
213 /** fileheader */
214 BITMAP_FILEHEADER m_fileheader;
215 /** infoheader */
216 BITMAP_INFOHEADER m_infoheader;
217 /** pointer to pixelbuffer */
218 uint8_t *m_pixeldata;
219 /** pointer to CPixelFormat implementation */
220 CPixelFormat *m_pixelformat;
221};
222
223#endif
224
225/* 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 @@
1/**
2 * @module cfile
3 * @author Manuel Mausz, 0728348
4 * @brief Abstract class for handling files.
5 * Needed for generic use in CScriptparser.
6 * @date 17.04.2009
7 */
8
9#ifndef CFILE_H
10#define CFILE_H
11
12#include <string>
13#include <set>
14#include <list>
15#include <fstream>
16#include <stdexcept>
17
18/**
19 * @class CFile
20 * @brief Abstract class for handling files. Needed for generic use in
21 * CScriptparser.
22 *
23 * In order for CScriptparser to determine which instance of CFile supports
24 * which filetype, every implemententation need to insert their filetypes to
25 * the member m_types in their constructor.
26 *
27 * On error throw FileError.
28 */
29class CFile
30{
31 public:
32 /**
33 * @class FileError
34 * @brief Exception thrown by implemententations of CFile
35 */
36 class FileError : public std::invalid_argument {
37 public:
38 /**
39 * @method FileError
40 * @brief Default exception ctor
41 * @param what message to pass along
42 * @return -
43 * @globalvars none
44 * @exception none
45 * @conditions none
46 */
47 FileError(const std::string& what)
48 : std::invalid_argument(what)
49 {}
50 };
51
52 /**
53 * @method ~CFile
54 * @brief Default dtor (virtual)
55 * @param -
56 * @return -
57 * @globalvars none
58 * @exception none
59 * @conditions none
60 */
61 virtual ~CFile()
62 {};
63
64 /**
65 * @method read
66 * @brief Pure virtual method (interface). Should read data from filestream.
67 * @param in filestream to read data from
68 * @return -
69 * @globalvars none
70 * @exception FileError
71 * @conditions none
72 */
73 virtual void read(std::ifstream& in) = 0;
74
75 /**
76 * @method write
77 * @brief Pure virtual method (interface). Should write data to filestream.
78 * @param out filestream to write data to
79 * @return -
80 * @globalvars none
81 * @exception FileError
82 * @conditions none
83 */
84 virtual void write(std::ofstream& out) = 0;
85
86 /**
87 * @method callFunc
88 * @brief Pure virtual method (interface). Should delegate the function
89 * and its parameters to the correct internal method.
90 * @param func function name
91 * @param params function parameters as list
92 * @return -
93 * @globalvars none
94 * @exception FileError
95 * @conditions none
96 */
97 virtual void callFunc(const std::string& func, const std::list<std::string>& params) = 0;
98
99 /**
100 * @method supportsType
101 * @brief Check if filetype is supported by this implementation.
102 * @param type filetype
103 * @return true if filetype is supported. false otherwise
104 * @globalvars none
105 * @exception none
106 * @conditions none
107 */
108 bool supportsType(const std::string& type)
109 {
110 return (m_types.find(type) == m_types.end()) ? false : true;
111 }
112
113 protected:
114 /* members */
115 /** set of filetypes suppported by this implementation */
116 std::set<std::string> m_types;
117};
118
119#endif
120
121/* 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 @@
1/**
2 * @module cpixelformat
3 * @author Manuel Mausz, 0728348
4 * @brief Abstract class for handling different color bitcount of Bitmaps.
5 * Needed for generic use in CBitmap.
6 * @date 18.04.2009
7 */
8
9#ifndef CPIXELFORMAT_H
10#define CPIXELFORMAT_H
11
12#include <fstream>
13#include <stdexcept>
14
15class CBitmap;
16#include "cbitmap.h"
17
18/**
19 * @class CPixelFormat
20 * @brief Abstract class for handling different color bitcount of Bitmaps.
21 *
22 * Needed for generic use in CBitmap.
23 *
24 * On error throw PixelFormatError.
25 */
26class CPixelFormat
27{
28 public:
29 /**
30 * @class PixelFormatError
31 * @brief Exception thrown by implemententations of CPixelFormat
32 */
33 class PixelFormatError : public std::invalid_argument {
34 public:
35 /**
36 * @method PixelFormatError
37 * @brief Default exception ctor
38 * @param what message to pass along
39 * @return -
40 * @globalvars none
41 * @exception none
42 * @conditions none
43 */
44 PixelFormatError(const std::string& what)
45 : std::invalid_argument(what)
46 {}
47 };
48
49 /**
50 * @method CBitmap
51 * @brief Default ctor
52 * @param bitmap pointer to CBitmap instance
53 * @return -
54 * @globalvars none
55 * @exception none
56 * @conditions none
57 */
58 CPixelFormat(CBitmap *bitmap)
59 : m_bitmap(bitmap)
60 {}
61
62 /**
63 * @method ~CPixelFormat
64 * @brief Default dtor (virtual)
65 * @param -
66 * @return -
67 * @globalvars none
68 * @exception none
69 * @conditions none
70 */
71 virtual ~CPixelFormat()
72 {};
73
74 /**
75 * @method setPixel
76 * @brief Modifies pixel at coordinates x, y
77 * @param pixel pointer to new pixel data
78 * @param x x-coordinate
79 * @param y y-coordinate
80 * @return -
81 * @globalvars none
82 * @exception PixelFormatError
83 * @conditions none
84 */
85 virtual void setPixel(const uint32_t *pixel, const uint32_t x, const uint32_t y) = 0;
86
87 /**
88 * @method getBitCount
89 * @brief returns color bitcount supported by this class
90 * @param -
91 * @return color bitcount supported by this class
92 * @globalvars none
93 * @exception none
94 * @conditions none
95 */
96 virtual uint32_t getBitCount() = 0;
97
98 protected:
99 /* members */
100 /** pointer to CBitmap instance */
101 CBitmap *m_bitmap;
102};
103
104#endif
105
106/* 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 @@
1/**
2 * @module cpixelformat_24
3 * @author Manuel Mausz, 0728348
4 * @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps.
5 * @date 18.04.2009
6 */
7
8#include <boost/numeric/conversion/cast.hpp>
9#include "cpixelformat_24.h"
10
11using namespace std;
12
13void CPixelFormat_24::setPixel(const uint32_t *pixel, uint32_t x, uint32_t y)
14{
15 if (m_bitmap->getPixelData() == NULL)
16 throw PixelFormatError("No pixelbuffer allocated.");
17
18 uint32_t rowsize = 4 * static_cast<uint32_t>(
19 ((CPixelFormat_24::getBitCount() * abs(m_bitmap->getInfoHeader().biWidth)) + 31) / 32
20 );
21
22 /* if height is positive the y-coordinates are mirrored */
23 if (m_bitmap->getInfoHeader().biHeight > 0)
24 y = m_bitmap->getInfoHeader().biHeight - y - 1;
25 uint32_t offset = y * rowsize + x * (4 * getBitCount() / 32);
26
27 /* boundary check */
28 if (offset + 3 * sizeof(uint8_t) > m_bitmap->getInfoHeader().biSizeImage)
29 throw PixelFormatError("Pixel position is out of range.");
30
31 /* convert color values to correct types */
32 uint8_t data[3];
33 try
34 {
35 data[0] = boost::numeric_cast<uint8_t>(pixel[2]);
36 data[1] = boost::numeric_cast<uint8_t>(pixel[1]);
37 data[2] = boost::numeric_cast<uint8_t>(pixel[0]);
38 }
39 catch(boost::numeric::bad_numeric_cast& ex)
40 {
41 throw PixelFormatError("Unable to convert pixelcolor to correct size: " + string(ex.what()));
42 }
43
44 copy(data, data + 3, m_bitmap->getPixelData() + offset);
45}
46
47/* 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 @@
1/**
2 * @module cpixelformat_24
3 * @author Manuel Mausz, 0728348
4 * @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps.
5 * @date 18.04.2009
6 */
7
8#ifndef CPIXELFORMAT_24_H
9#define CPIXELFORMAT_24_H
10
11#include <fstream>
12#include "cpixelformat.h"
13
14/**
15 * @class CPixelFormat_24
16 * @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps.
17 *
18 * On error CPixelFormat::PixelFormatError is thrown.
19 */
20class CPixelFormat_24 : public CPixelFormat
21{
22 public:
23 /**
24 * @method CPixelFormat_24
25 * @brief Default ctor
26 * @param bitmap pointer to CBitmap instance
27 * @return -
28 * @globalvars none
29 * @exception none
30 * @conditions none
31 */
32 CPixelFormat_24(CBitmap *bitmap)
33 : CPixelFormat(bitmap)
34 {}
35
36 /**
37 * @method ~CPixelFormat_24
38 * @brief Default dtor
39 * @param -
40 * @return -
41 * @globalvars none
42 * @exception none
43 * @conditions none
44 */
45 ~CPixelFormat_24()
46 {}
47
48 /**
49 * @method setPixel
50 * @brief Modifies pixel at coordinates x, y
51 * @param pixel pointer to new pixel data
52 * @param x x-coordinate
53 * @param y y-coordinate
54 * @return -
55 * @globalvars none
56 * @exception PixelFormatError
57 * @conditions none
58 */
59 void setPixel(const uint32_t *pixel, uint32_t x, uint32_t y);
60
61 /**
62 * @method getBitCount
63 * @brief returns color bitcount supported by this class
64 * @param -
65 * @return color bitcount supported by this class
66 * @globalvars none
67 * @exception none
68 * @conditions none
69 */
70 uint32_t getBitCount()
71 {
72 return 24;
73 }
74};
75
76#endif
77
78/* 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 @@
1/**
2 * @module cscriptparser
3 * @author Manuel Mausz, 0728348
4 * @brief class for parsing simple scriptfiles
5 * @date 17.04.2009
6 */
7
8#include <fstream>
9#include <boost/tokenizer.hpp>
10#include <boost/algorithm/string.hpp>
11#include "cscriptparser.h"
12#include "cbitmap.h"
13
14using namespace std;
15using namespace boost;
16
17CScriptparser::CScriptparser(const std::string& scriptfile)
18 : m_scriptfile(scriptfile), m_handler(NULL)
19{
20 /* add our handlers */
21 m_handlers.insert(new CBitmap);
22}
23
24/*----------------------------------------------------------------------------*/
25
26CScriptparser::~CScriptparser()
27{
28 /* delete image handlers */
29 set<CFile *>::iterator it;
30 for (it = m_handlers.begin(); it != m_handlers.end(); it++)
31 delete *it;
32}
33
34/*----------------------------------------------------------------------------*/
35
36void CScriptparser::parse()
37{
38 /* open and read file */
39 ifstream file(m_scriptfile.c_str(), ios::in);
40 if (!file)
41 throw ParserError("Unable to open scriptfile '" + m_scriptfile + "'.");
42
43 while (!file.eof() && file.good())
44 {
45 /* read file pre line */
46 getline(file, m_curline);
47 if (m_curline.empty())
48 continue;
49
50 trim(m_curline);
51
52 /* ignore comments */
53 if (m_curline.find_first_of('#') == 0)
54 continue;
55
56 /* line has no function call */
57 size_t pos1 = m_curline.find_first_of('(');
58 size_t pos2 = m_curline.find_last_of(')');
59 if (pos1 == string::npos || pos2 == string::npos)
60 continue;
61
62 /* first parse function name and tokenize all parameters */
63 string func = m_curline.substr(0, pos1);
64 string params = m_curline.substr(pos1 + 1, pos2 - pos1 - 1);
65 list<string> funcparams;
66 tokenizer< char_separator<char> > tokens(params, char_separator<char>(","));
67 /* BOOST_FOREACH isn't available on OOP-servers... */
68 for (tokenizer< char_separator<char> >::iterator it = tokens.begin();
69 it != tokens.end();
70 it++)
71 {
72 string tok(*it);
73 trim(tok);
74 if (tok.find_first_of(' ') != string::npos)
75 {
76 if (tok.find_first_of('"') == string::npos)
77 throw ParserError("Invalid syntax", m_curline);
78 }
79 trim_if(tok, is_any_of("\""));
80 funcparams.push_back(tok);
81 }
82
83 /* then call the corresponding function */
84 callFunc(func, funcparams);
85 }
86
87 file.close();
88}
89
90/*----------------------------------------------------------------------------*/
91
92void CScriptparser::callFunc(const std::string& func, const std::list<std::string>& funcparams)
93{
94 if (func.empty())
95 throw ParserError("Function name is empty.", m_curline);
96
97 if (func == "read")
98 read(funcparams);
99 else if (func == "write")
100 write(funcparams);
101 else
102 {
103 if (m_handler == NULL)
104 throw ParserError("No image is being processed.", m_curline);
105
106 /* call function from handler */
107 try
108 {
109 m_handler->callFunc(func, funcparams);
110 }
111 catch(CFile::FileError& ex)
112 {
113 throw ParserError(ex.what(), m_curline);
114 }
115 }
116}
117
118/*----------------------------------------------------------------------------*/
119
120void CScriptparser::read(std::list<std::string> funcparams)
121{
122 /* check prerequirements */
123 if (funcparams.size() != 2)
124 throw ParserError("Invalid number of function parameters (must be 2).", m_curline);
125 if (m_handler != NULL)
126 throw ParserError("An image is already being processed. Unable to open another.", m_curline);
127
128 string type = funcparams.front();
129 to_upper(type);
130 funcparams.pop_front();
131 string filename = funcparams.front();
132
133 /* fetch image handler supporting requested filetype */
134 m_handler = NULL;
135 set<CFile *>::iterator it;
136 for (it = m_handlers.begin(); it != m_handlers.end(); it++)
137 {
138 if ((*it)->supportsType(type))
139 {
140 m_handler = *it;
141 break;
142 }
143 }
144 if (m_handler == NULL)
145 throw ParserError("Unknown filetype.", m_curline);
146
147 /* open file in binary mode */
148 ifstream file(filename.c_str(), ios::in | ios::binary);
149 if (!file)
150 throw ParserError("Unable to read file.", m_curline);
151
152 /* let handlers read() parse the file */
153 try
154 {
155 m_handler->read(file);
156 if (!file.good())
157 throw ParserError("Error while reading image file.", m_curline);
158 file.close();
159 }
160 catch(CFile::FileError& ex)
161 {
162 file.close();
163 throw ParserError(ex.what(), m_curline);
164 }
165}
166
167/*----------------------------------------------------------------------------*/
168
169void CScriptparser::write(std::list<std::string> funcparams)
170{
171 /* check prerequirements */
172 if (funcparams.size() != 2)
173 throw ParserError("Invalid number of function parameters (must be 2).", m_curline);
174 if (m_handler == NULL)
175 throw ParserError("No image is being processed.", m_curline);
176
177 string type = funcparams.front();
178 to_upper(type);
179 funcparams.pop_front();
180 string filename = funcparams.front();
181
182 /* do we have an image handler supporting the filetype? */
183 if (!m_handler->supportsType(type))
184 throw ParserError("Unknown filetype.", m_curline);
185
186 /* open file in binary mode */
187 ofstream file(filename.c_str(), ios::out | ios::binary);
188 if (!file)
189 throw ParserError("Unable to open file.", m_curline);
190
191 /* let handlers write() parse the file */
192 try
193 {
194 m_handler->write(file);
195 if (!file.good())
196 throw ParserError("Error while writing image file.", m_curline);
197 file.close();
198 m_handler = NULL;
199 }
200 catch(CFile::FileError& ex)
201 {
202 file.close();
203 m_handler = NULL;
204 throw ParserError(ex.what(), m_curline);
205 }
206}
207
208/* 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 @@
1/**
2 * @module cscriptparser
3 * @author Manuel Mausz, 0728348
4 * @brief class for parsing simple scriptfiles
5 * @date 17.04.2009
6 */
7
8#ifndef CSCRIPTPARSER_H
9#define CSCRIPTPARSER_H
10
11#include <stdexcept>
12#include <string>
13#include <list>
14#include <set>
15#include "cfile.h"
16
17/**
18 * @class CScriptparser
19 *
20 * Parses a simple line based scriptfile with some limitations:
21 * first function (starting a block) must be a read-command,
22 * last must be a write-command (ending this block).
23 *
24 * read- and write-commands have hard coded parameters, number#1 being a filetype.
25 * Classes handling certain filetypes must be of type CFile.
26 * Custom functions will be passed to CFile::callFunc().
27 *
28 * On error ParserError will be thrown.
29 */
30class CScriptparser
31{
32 public:
33 /**
34 * @class ParserError
35 * @brief Exception thrown by CScriptparser
36 */
37 class ParserError : public std::invalid_argument {
38 public:
39 /**
40 * @method ParserError
41 * @brief Default exception ctor
42 * @param what message to pass along
43 * @return -
44 * @globalvars none
45 * @exception none
46 * @conditions none
47 */
48 ParserError(const std::string& what)
49 : std::invalid_argument(what), m_line("")
50 {}
51
52 /**
53 * @method ParserError
54 * @brief Custom exception ctor
55 * @param what message to pass along
56 * @param line scriptline which is currently being parsed
57 * @return -
58 * @globalvars none
59 * @exception none
60 * @conditions none
61 */
62 ParserError(const std::string& what, const std::string& line)
63 : std::invalid_argument(what), m_line(line)
64 {}
65
66 /**
67 * @method ~ParserError
68 * @brief Default dtor
69 * @param -
70 * @return -
71 * @globalvars none
72 * @exception not allowed
73 * @conditions none
74 */
75 ~ParserError() throw()
76 {}
77
78 /**
79 * @method getLine
80 * @brief returns reference to currently parsed scriptline (if set)
81 * @return reference to currently parsed scriptline (maybe empty string)
82 * @globalvars none
83 * @exception none
84 * @conditions none
85 */
86 const std::string &getLine()
87 {
88 return m_line;
89 }
90
91 private:
92 /* members*/
93 std::string m_line;
94 };
95
96 /**
97 * @method CScriptparser
98 * @brief Default ctor
99 * @param scriptfile filename of script to parse
100 * @return -
101 * @globalvars none
102 * @exception bad_alloc
103 * @conditions none
104 */
105 CScriptparser(const std::string& scriptfile);
106
107 /**
108 * @method ~CScriptparser
109 * @brief Default dtor
110 * @param -
111 * @return -
112 * @globalvars none
113 * @exception none
114 * @conditions none
115 */
116 ~CScriptparser();
117
118 /**
119 * @method parse
120 * @brief Start parsing the scriptfile
121 * @param -
122 * @return -
123 * @globalvars none
124 * @exception ParserError
125 * @conditions none
126 */
127 void parse();
128
129 protected:
130 /**
131 * @method callFunc
132 * @brief Delegates the function and its parameters to the correct
133 * method (internal or handler)
134 * @param func function name
135 * @param funcparams function parameters as list
136 * @return -
137 * @globalvars none
138 * @exception ParserError
139 * @conditions none
140 */
141 void callFunc(const std::string& func, const std::list<std::string>& funcparams);
142
143 /**
144 * @method read
145 * @brief Handles/wrappes read-command. according to the filetype the
146 * read-method of the corresponding handler will be called inside.
147 * @param funcparams function parameters as list
148 * @return -
149 * @globalvars none
150 * @exception ParserError
151 * @conditions none
152 *
153 * Scriptfile syntax: read(<FILETYPE>, <FILENAME>)
154 */
155 void read(std::list<std::string> funcparams);
156
157 /**
158 * @method write
159 * @brief Handles/wrappes write-command. according to the filetype the
160 * write-method of the corresponding handler will be called inside.
161 * @param funcparams function parameters as list
162 * @return -
163 * @globalvars none
164 * @exception ParserError
165 * @conditions none
166 *
167 * Scriptfile syntax: write(<FILETYPE>, <FILENAME>)
168 */
169 void write(std::list<std::string> funcparams);
170
171 private:
172 /* members */
173 std::set<CFile *> m_handlers;
174 std::string m_scriptfile;
175 std::string m_curline;
176 CFile *m_handler;
177};
178
179#endif
180
181/* 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 @@
1<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2<CodeBlocks_project_file>
3 <FileVersion major="1" minor="6" />
4 <Project>
5 <Option title="imgsynth" />
6 <Option makefile_is_custom="1" />
7 <Option pch_mode="2" />
8 <Option compiler="gcc" />
9 <MakeCommands>
10 <Build command="$make -f $makefile $target" />
11 <CompileFile command="$make -f $makefile $file" />
12 <Clean command="$make -f $makefile clean" />
13 <DistClean command="$make -f $makefile clean" />
14 </MakeCommands>
15 <Build>
16 <Target title="all">
17 <Option output="imgsynth" prefix_auto="1" extension_auto="1" />
18 <Option object_output="obj/Release/" />
19 <Option type="1" />
20 <Option compiler="gcc" />
21 <Compiler>
22 <Add option="-O2" />
23 </Compiler>
24 <Linker>
25 <Add option="-s" />
26 </Linker>
27 <MakeCommands>
28 <Build command="$make -f $makefile $target" />
29 <CompileFile command="$make -f $makefile $file" />
30 <Clean command="$make -f $makefile clean" />
31 <DistClean command="$make -f $makefile clean" />
32 </MakeCommands>
33 </Target>
34 <Target title="debug">
35 <Option output="imgsynth" prefix_auto="1" extension_auto="1" />
36 <Option type="1" />
37 <Option compiler="gcc" />
38 <MakeCommands>
39 <Build command="$make -f $makefile $target" />
40 <CompileFile command="$make -f $makefile $file" />
41 <Clean command="$make -f $makefile clean" />
42 <DistClean command="$make -f $makefile clean" />
43 </MakeCommands>
44 </Target>
45 </Build>
46 <Compiler>
47 <Add option="-pedantic-errors" />
48 <Add option="-Wall" />
49 <Add option="-ansi" />
50 <Add option="-fexceptions" />
51 </Compiler>
52 <Unit filename="imgsynth.cpp">
53 <Option target="all" />
54 </Unit>
55 <Extensions>
56 <code_completion />
57 <debugger />
58 </Extensions>
59 </Project>
60</CodeBlocks_project_file>
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 @@
1/**
2 * @module imgsynth
3 * @author Manuel Mausz, 0728348
4 * @brief imgsynth reads a scriptfile given as commandline option
5 * and executes all known function inside.
6 * On error (e.g. unknown function) the program will terminate
7 * @date 17.04.2009
8 * @par Exercise
9 * 1
10 */
11
12#include <iostream>
13#include <boost/program_options.hpp>
14#include "cscriptparser.h"
15
16using namespace std;
17namespace po = boost::program_options;
18
19/**
20 * @func main
21 * @brief program entry point
22 * @param argc standard parameter of main
23 * @param argv standard parameter of main
24 * @return 0 on success, not 0 otherwise
25 * @globalvars none
26 * @exception none
27 * @conditions none
28 *
29 * setup commandline options, parse them and pass scriptfile to scriptparser
30 * instance. On error print error message to stderr.
31 * Unknown commandline options will print a usage message.
32 */
33int main(int argc, char* argv[])
34{
35 string me(argv[0]);
36
37 /* define commandline options */
38 po::options_description desc("Allowed options");
39 desc.add_options()
40 ("help,h", "this help message")
41 ("input,i", po::value<string>(), "input scriptfile");
42
43 /* parse commandline options */
44 po::variables_map vm;
45 try
46 {
47 po::store(po::parse_command_line(argc, argv, desc), vm);
48 po::notify(vm);
49 }
50 catch(po::error& ex)
51 {
52 cerr << "Error: " << ex.what() << endl;
53 }
54
55 /* print usage upon request or missing params */
56 if (vm.count("help") || !vm.count("input"))
57 {
58 cout << "Usage: " << me << " -i <scriptfile>" << endl;
59 cout << desc << endl;
60 return 0;
61 }
62
63 CScriptparser parser(vm["input"].as<string>());
64 try
65 {
66 parser.parse();
67 }
68 catch(CScriptparser::ParserError& ex)
69 {
70 cerr << me << ": Error while processing scriptfile: " << ex.what() << endl;
71 if (!ex.getLine().empty())
72 cerr << "Scriptline: '" << ex.getLine() << "'" << endl;
73 return 1;
74 }
75 catch(exception& ex)
76 {
77 cerr << me << ": Unexpected exception: " << ex.what() << endl;
78 return 1;
79 }
80
81 return 0;
82}
83
84/* 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 @@
1<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2<CodeBlocks_layout_file>
3 <ActiveTarget name="all" />
4 <File name="imgsynth.cpp" open="1" top="1" tabpos="1">
5 <Cursor position="184" topLine="0" />
6 </File>
7</CodeBlocks_layout_file>
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 @@
1read(BMP, test/yellow_man_in.bmp)
2fillrect(0,3,6,5,0,255,0)
3fillrect(2,13,7,4,0,0,255)
4write(BMP, "test/yellow_man_out.bmp")
5
6
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 @@
1#!/bin/sh
2
3rm -f test/yellow_man_out.bmp
4./imgsynth -i test/input
5if [ "$(md5sum < test/yellow_man_out.bmp)" != "$(md5sum < test/yellow_man_ref.bmp)" ]
6then
7 echo "ERROR: test/yellow_man_ref.bmp and test/yellow_man_ref.bmp differ"
8 exit 1
9else
10 echo "Test successful"
11fi
12
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
--- /dev/null
+++ b/ue1/imgsynth/test/yellow_man_in.bmp
Binary files 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
--- /dev/null
+++ b/ue1/imgsynth/test/yellow_man_out.bmp
Binary files 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
--- /dev/null
+++ b/ue1/imgsynth/test/yellow_man_ref.bmp
Binary files differ