/** * @module CPixmap * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) * @brief Implementation of CFile handling Windows Bitmaps. * @date 27.04.2009 */ #include #include #ifdef DEBUG # include #endif #include #include # include # include #include "cpixmap.h" #include "cpixelformat_indexed8.h" using namespace std; CPixmap::CPixmap() { m_types.insert("XPM"); /* add our handlers */ m_handlers.insert(new CPixelFormat_Indexed8(this)); } /*----------------------------------------------------------------------------*/ void CPixmap::read(std::ifstream& in) { string str; m_fileheader._XPMEXT = false; m_fileheader._HOTSPOT = false; /* parse the values section */ getline( in, str, '"'); getline( in, str, '"'); stringstream istr (str ); istr >> m_fileheader.xpmWidth ; istr >> m_fileheader.xpmHeight ; istr >> m_fileheader.nColor ; istr >> m_fileheader.nChar ; /*optional values */ if (istr.good()) { 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 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 ); istr2 >> character; istr2 >> mode; istr2 >> 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, '"' ); } /* read pixel data using separate class */ if (getPixelDataSize() > 0) { if (m_pixeldata != NULL) delete[] m_pixeldata; m_pixeldata = new uint8_t[getPixelDataSize()]; for (unsigned int y = 0; y < getHeight(); y++ ) { for (unsigned int x = 0; x < getWidth(); x++ ) m_pixeldata[y * getWidth() + x] = static_cast(in.get()); getline( in, str); if ( y != ( getHeight() - 1 )) in.get(); } } /* parse extension */ if ( m_fileheader._XPMEXT ) getline( in, m_fileheader.extension, '}' ); /* debug*/ 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 */ //TODO if (mode.compare((*it)->getColorMode()) == 0) if (mode == "c") { m_pixelformat = *it; break; } } if (m_pixelformat == NULL) throw FileError("Pixmap color mode \""+mode+"\" is not supported."); } /*----------------------------------------------------------------------------*/ 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 <