summaryrefslogtreecommitdiffstats
path: root/ue2/imgsynth2/cpixmap.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ue2/imgsynth2/cpixmap.cpp')
-rw-r--r--ue2/imgsynth2/cpixmap.cpp269
1 files changed, 269 insertions, 0 deletions
diff --git a/ue2/imgsynth2/cpixmap.cpp b/ue2/imgsynth2/cpixmap.cpp
new file mode 100644
index 0000000..6f8c8d6
--- /dev/null
+++ b/ue2/imgsynth2/cpixmap.cpp
@@ -0,0 +1,269 @@
1/**
2 * @module CPixmap
3 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
4 * @brief Implementation of CFile handling Windows Bitmaps.
5 * @date 27.04.2009
6 */
7
8#include <boost/lexical_cast.hpp>
9#include <boost/numeric/conversion/cast.hpp>
10
11#ifdef DEBUG
12# include <iostream>
13#endif
14#include <cstdlib>
15#include <stdint.h>
16# include <string>
17# include <map>
18
19
20
21#include "cpixmap.h"
22#include "cpixelformat_indexed8.h"
23
24using namespace std;
25
26CPixmap::CPixmap()
27{
28 m_types.insert("XPM");
29
30 /* add our handlers */
31 m_handlers.insert(new CPixelFormat_Indexed8(this));
32}
33
34/*----------------------------------------------------------------------------*/
35
36void CPixmap::read(std::ifstream& in)
37{
38
39 string str;
40 m_fileheader._XPMEXT = false;
41 m_fileheader._HOTSPOT = false;
42
43 /* parse the values section */
44 getline( in, str, '"');
45 getline( in, str, '"');
46 stringstream istr (str );
47
48 istr >> m_fileheader.xpmWidth ;
49 istr >> m_fileheader.xpmHeight ;
50 istr >> m_fileheader.nColor ;
51 istr >> m_fileheader.nChar ;
52
53 /*optional values */
54 if (istr.good())
55 {
56 string tmp;
57 istr >> tmp;
58 if(tmp.compare("XPMEXT") == 0)
59 m_fileheader._XPMEXT = true;
60 else
61 {
62 m_fileheader._HOTSPOT = true;
63 m_fileheader.xHotspot = static_cast<unsigned int>(strtoul(tmp.c_str(), NULL, 16));
64 istr >> m_fileheader.yHotspot;
65 }
66 if (istr.good())
67 {
68 istr >> tmp;
69 if (tmp.compare("XPMEXT") == 0)
70 m_fileheader._XPMEXT = true;
71 }
72 }
73
74
75
76 /* parse color table*/
77 getline( in, str, '"');
78 string character, mode, colors;
79 for (unsigned int i = 0; i < m_fileheader.nColor; i++ )
80 {
81 getline( in, str, '"' );
82 stringstream istr2 (str );
83
84 istr2 >> character;
85 istr2 >> mode;
86 istr2 >> colors;
87
88 if ( colors.size() != 7)
89 throw FileError("Pixmap color tabel is invalid.");
90 if ( colors.at(0) != '#')
91 throw FileError("Pixmap color tabel value is not hexadecimal.");
92
93 xpmColors[character][mode] = new (nothrow) unsigned int[3];
94 if (xpmColors[character][mode] == 0)
95 throw FileError("Bad color table allocation.");
96
97 xpmColors[character][mode][0] =
98 static_cast<unsigned int>(strtoul(colors.substr(1,2).c_str(), NULL, 16));
99 xpmColors[character][mode][1] =
100 static_cast<unsigned int>(strtoul(colors.substr(3,2).c_str(), NULL, 16));
101 xpmColors[character][mode][2] =
102 static_cast<unsigned int>(strtoul(colors.substr(5,2).c_str(), NULL, 16));
103
104
105
106
107
108
109 getline( in, str, '"' );
110 }
111
112 /* read pixel data using separate class */
113 if (getPixelDataSize() > 0)
114 {
115 if (m_pixeldata != NULL)
116 delete[] m_pixeldata;
117 m_pixeldata = new uint8_t[getPixelDataSize()];
118
119 for (unsigned int y = 0; y < getHeight(); y++ )
120 {
121 for (unsigned int x = 0; x < getWidth(); x++ )
122 m_pixeldata[y * getWidth() + x] = static_cast<uint8_t>(in.get());
123
124 getline( in, str);
125 if ( y != ( getHeight() - 1 ))
126 in.get();
127
128 }
129 }
130
131 /* parse extension */
132 if ( m_fileheader._XPMEXT )
133 getline( in, m_fileheader.extension, '}' );
134 /* debug*/
135 CPixmap::dump (cout);
136
137 /* get pixelformat instance */
138 m_pixelformat = NULL;
139 set<CPixelFormat *>::iterator it;
140 for (it = m_handlers.begin(); it != m_handlers.end(); it++)
141 {
142 /* check color mode */
143 //TODO if (mode.compare((*it)->getColorMode()) == 0)
144 if (mode == "c")
145 {
146 m_pixelformat = *it;
147 break;
148 }
149 }
150 if (m_pixelformat == NULL)
151 throw FileError("Pixmap color mode \""+mode+"\" is not supported.");
152}
153
154/*----------------------------------------------------------------------------*/
155
156void CPixmap::write(std::ofstream& out, std::string& filename)
157{
158
159 /* header comment */
160 out<<"/* XPM */"<<endl;
161
162 /* variables*/
163 out<<"static char * "
164 <<filename.substr(filename.find_last_of("/\\") + 1, filename.size())
165 <<"[] = {"<<endl;
166 out<<"\""<<m_fileheader.xpmWidth<<" "<<m_fileheader.xpmHeight
167 <<" "<<m_fileheader.nColor<<" "<<m_fileheader.nChar;
168
169 /*optional values*/
170 if( m_fileheader._HOTSPOT )
171 out<<" "<<m_fileheader.xHotspot<<" "<<m_fileheader.yHotspot;
172 if( m_fileheader._XPMEXT )
173 out<<" "<<"XPMEXT";
174 out <<"\","<<endl;
175
176 /* color table */
177 map<string, map<string, uint32_t* > >::iterator it1;
178 map<string, uint32_t* >::iterator it2;
179
180 for ( it1= xpmColors.begin() ; it1 != xpmColors.end(); it1++ )
181 {
182 out << "\""<<(*it1).first;
183 for ( it2=(*it1).second.begin() ; it2 != (*it1).second.end(); it2++ )
184 {
185
186 out<<"\t" <<(*it2).first<<"\t#";
187
188 for (int i = 0 ; i < 3; i++ )
189 {
190 out.width(2);
191 out << hex <<uppercase<<(*it2).second[i];
192 out.fill('0');
193 }
194 }
195 out<<"\","<<endl;
196 }
197
198 /* pixel data */
199 for (unsigned int y = 0; y < getHeight(); y++ ){
200 out <<"\"";
201 for (unsigned int x = 0; x < getWidth(); x++ )
202 out << m_pixeldata[y * getWidth() + x];
203 out <<"\"";
204 if ( y != ( getHeight() - 1 ))
205 out <<",";
206 out <<endl;
207 }
208
209 /* extension */
210 if (m_fileheader._XPMEXT)
211 out << m_fileheader.extension;
212
213 out <<"};";
214
215}
216
217
218/*----------------------------------------------------------------------------*/
219
220#ifdef DEBUG
221void CPixmap::dump(std::ostream& out)
222{
223
224 /* pixeldata */
225 cout<<endl<<"(_Pixel data:_)"<<endl;
226 for (unsigned int y = 0; y < getHeight(); y++ ){
227 for (unsigned int x = 0; x < getWidth(); x++ )
228 out << m_pixeldata[y * getWidth() + x];
229 out << endl;
230 }
231
232 /* values*/
233 cout<<endl<<"(_Values:_)"<<endl;
234 out << "XPM Header:" << endl
235 << "xpmWidth=" << m_fileheader.xpmWidth<< endl
236 << "xpmHeight=" << m_fileheader.xpmHeight<< endl
237 << "nColor=" << m_fileheader.nColor<< endl
238 << "nChar=" << m_fileheader.nChar<< endl
239 << "Hotspot=" << m_fileheader.xHotspot<< endl
240 << "yHotspot=" << m_fileheader.yHotspot<< endl
241 << "_HOTSPOT=" << m_fileheader._HOTSPOT<< endl
242 << "_XPMEXT=" << m_fileheader._XPMEXT<< endl
243 << "extension=" << m_fileheader.extension<< endl
244 << endl;
245
246 /* colors*/
247 map<string, map<string, uint32_t* > >::iterator it1;
248 map<string, uint32_t* >::iterator it2;
249 cout<<"(_Color table:_)"<<endl;
250 for ( it1= xpmColors.begin() ; it1 != xpmColors.end(); it1++ )
251 {
252 out << (*it1).first;
253 for ( it2=(*it1).second.begin() ; it2 != (*it1).second.end(); it2++ )
254 {
255 out <<" "<< (*it2).first <<" ";
256 for (int i = 0 ; i < 3; i++ )
257 {
258 out.width(3);
259 out << (*it2).second[i]<<" ";
260 out.fill('0');
261 }
262 out<<endl;
263 }
264 }
265
266}
267#endif
268
269/* vim: set et sw=2 ts=2: */