// Copyright (C) 2010, Guy Barrand. All rights reserved. // See the file tools.license for terms. #ifndef tools_xpm #define tools_xpm #include //FILE,size_t #include #include #include #include "charmanip" namespace tools { namespace xpm { typedef unsigned char* (*file_reader)(std::ostream&,const std::string&,unsigned int&,unsigned int&,unsigned int&); inline bool to_xpm(std::ostream& a_out,const std::string& a_file,const std::string& a_name,file_reader a_file_reader,bool a_verbose) { // a_name is the xpm name. Then a string without a path and without // the .xpm suffix. unsigned int w,h,bpp; unsigned char* buffer = a_file_reader(a_out,a_file,w,h,bpp); if(!buffer) { a_out << "tools::xpm::to_xpm :" << " read_file failed." << std::endl; return false; } // get colormap : typedef unsigned int color; std::vector colors; {unsigned char r,g,b; for(unsigned int j=0;j::iterator it; for(it=colors.begin();it!=colors.end();++it) { if(*it==c) { found = true; break; } } if(!found) colors.push_back(c); } }} unsigned int colorn = (unsigned int)colors.size(); //get a set of non problematic characters (for exa avoid \). std::vector chars; {for(unsigned char c=0;c<255;c++) { if(is_letter(c)||is_digit(c)) { chars.push_back(c); } }} unsigned int base = (unsigned int)chars.size(); unsigned int nchar = 1; {unsigned int i = colorn; while(i>=base) { //unsigned int r = i%base; i /= base; nchar++; }} if(a_verbose) { a_out << "colors " << colorn << " base " << base << " nchar " << nchar << std::endl; } std::string xpm = a_name+".xpm"; FILE* out = ::fopen(xpm.c_str(),"wb"); if(!out) { a_out << "tools::xpm::to_xpm :" << " can't open " << xpm << std::endl; delete [] buffer; return false; } ::fprintf(out,"%s","/* XPM */\n"); ::fprintf(out,"static const char *%s[] = {\n",a_name.c_str()); ::fprintf(out,"\"%u %u %u %u\",\n",w,h,colorn,nchar); // write colormap : {for(unsigned int index=0;index