summaryrefslogtreecommitdiffstats
path: root/ue1/imgsynth/cbitmap.cpp
diff options
context:
space:
mode:
authormanuel <manuel@nc8430.lan>2009-04-27 00:24:16 +0200
committermanuel <manuel@nc8430.lan>2009-04-27 00:24:16 +0200
commit384539f7cc9feaa7ef7cee385cce472c6966c843 (patch)
tree42d3cbc96d44087c0b6bbe8d41710e5c5f1efced /ue1/imgsynth/cbitmap.cpp
downloadooprog-384539f7cc9feaa7ef7cee385cce472c6966c843.tar.gz
ooprog-384539f7cc9feaa7ef7cee385cce472c6966c843.tar.bz2
ooprog-384539f7cc9feaa7ef7cee385cce472c6966c843.zip
Adding ue1
Diffstat (limited to 'ue1/imgsynth/cbitmap.cpp')
-rw-r--r--ue1/imgsynth/cbitmap.cpp208
1 files changed, 208 insertions, 0 deletions
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: */