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