From b0442de485dcb6328366d9b05a62af345e5fa39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Neuwirth?= Date: Fri, 1 May 2009 14:58:58 +0200 Subject: Adding cpixelformat_indexd8.h and .cpp. Adding virtual methode getColorMode to CPixelFormat. Adding color table to CBitmap. Adding implementation of CPixmap --- ue2/imgsynth2/cpixmap.cpp | 226 +++++++++++++++++++++++++++++++++------------- 1 file changed, 163 insertions(+), 63 deletions(-) (limited to 'ue2/imgsynth2/cpixmap.cpp') 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 @@ /** - * @module CPixMap + * @module CPixmap * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) * @brief Implementation of CFile handling Windows Bitmaps. * @date 27.04.2009 @@ -11,14 +11,19 @@ #ifdef DEBUG # include #endif +#include +#include # include # include + + + #include "cpixmap.h" #include "cpixelformat_indexed8.h" using namespace std; -CPixMap::CPixMap() +CPixmap::CPixmap() { m_types.insert("XPM"); @@ -31,10 +36,12 @@ CPixMap::CPixMap() /*----------------------------------------------------------------------------*/ -void CPixMap::read(std::ifstream& in) +void CPixmap::read(std::ifstream& in) { string str; + m_fileheader._XPMEXT = false; + m_fileheader._HOTSPOT = false; /* parse the values section */ getline( in, str, '"'); @@ -46,62 +53,73 @@ void CPixMap::read(std::ifstream& in) istr >> m_fileheader.nColor ; istr >> m_fileheader.nChar ; - /* if there are optional values */ - if (in.good()) - { - istr >> m_fileheader.xHotspot; - istr >> m_fileheader.yHotspot; - } - else + /*optional values */ + if (istr.good()) { - m_fileheader.xHotspot = NULL; - m_fileheader.yHotspot = NULL; + string tmp; + istr >> tmp; + if(tmp.compare("XPMEXT") == 0) + m_fileheader._XPMEXT = true; + else + { + m_fileheader._HOTSPOT = true; + m_fileheader.xHotspot = + static_cast(strtoul(tmp.c_str(), NULL, 16)); + istr >> m_fileheader.yHotspot; + } + if (istr.good()) + { + istr >> tmp; + if (tmp.compare("XPMEXT") == 0) + m_fileheader._XPMEXT = true; + } } + - /* parse the colors section */ + /* parse color table*/ getline( in, str, '"'); + string character, mode, colors; for (unsigned int i = 0; i < m_fileheader.nColor; i++ ) { getline( in, str, '"' ); stringstream istr2 (str ); - char character; - string mode, colors; - + istr2 >> character; istr2 >> mode; istr2 >> colors; - //m_fileheader.xpmColors[character][mode] = colors; - m_fileheader.xpmColors[character] = colors; + + if ( colors.size() != 7) + throw FileError("Pixmap color tabel is invalid."); + if ( colors.at(0) != '#') + throw FileError("Pixmap color tabel value is not hexadecimal."); + + xpmColors[character][mode] = new (nothrow) unsigned int[3]; + if (xpmColors[character][mode] == 0) + throw FileError("Bad color table allocation."); + + xpmColors[character][mode][0] = + static_cast(strtoul(colors.substr(1,2).c_str(), NULL, 16)); + xpmColors[character][mode][1] = + static_cast(strtoul(colors.substr(3,2).c_str(), NULL, 16)); + xpmColors[character][mode][2] = + static_cast(strtoul(colors.substr(5,2).c_str(), NULL, 16)); + + + + + getline( in, str, '"' ); } - -/* colors.replace(0,1,"0x"); - colors.insert(4, " 0x"); - colors.insert(9," 0x"); - - stringstream istr3 (colors, stringstream::out|stringstream::in ); - istr3 >>hex>> r; - istr3 >>hex>> g; - istr3 >>hex>> b; - //?????????????ß - cout<(r); - m_fileheader.xpmColors[c1]['g'] = static_cast(g); - m_fileheader.xpmColors[c1]['b'] = static_cast(b);*/ - - - - /* read pixel data using separate class */ + /* read pixel data using separate class */ if (getPixelDataSize() > 0) { if (m_pixeldata != NULL) delete[] m_pixeldata; m_pixeldata = new uint8_t[getPixelDataSize()]; - /* parse the pixel data */ for (unsigned int y = 0; y < getHeight(); y++ ) { for (unsigned int x = 0; x < getWidth(); x++ ) @@ -114,58 +132,140 @@ void CPixMap::read(std::ifstream& in) } } + /* parse extension */ + if ( m_fileheader._XPMEXT ) + getline( in, m_fileheader.extension, '}' ); /* debug*/ - CPixMap::dump (cout); + CPixmap::dump (cout); - + /* get pixelformat instance */ + m_pixelformat = NULL; + set::iterator it; + for (it = m_handlers.begin(); it != m_handlers.end(); it++) + { + /* check color mode */ + if (mode.compare((*it)->getColorMode()) == 0) + { + m_pixelformat = *it; + break; + } + } + if (m_pixelformat == NULL) + throw FileError("Pixmap color mode \""+mode+"\" is not supported."); } /*----------------------------------------------------------------------------*/ -void CPixMap::write(std::ofstream& out) +void CPixmap::write(std::ofstream& out, std::string& filename) { + + /* header comment */ out<<"/* XPM */"< >::iterator it1; + map::iterator it2; + + for ( it1= xpmColors.begin() ; it1 != xpmColors.end(); it1++ ) + { + out << "\""<<(*it1).first; + for ( it2=(*it1).second.begin() ; it2 != (*it1).second.end(); it2++ ) + { + + out<<"\t" <<(*it2).first<<"\t#"; + + for (int i = 0 ; i < 3; i++ ) + { + out.width(2); + out << hex < "<< (*it2).second<