17 typedef unsigned char* (*file_reader)(std::ostream&,
const std::string&,
unsigned int&,
unsigned int&,
unsigned int&);
19 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) {
24 unsigned char* buffer = a_file_reader(a_out,a_file,w,h,bpp);
26 a_out <<
"tools::xpm::to_xpm :"
27 <<
" read_file failed."
34 typedef unsigned int color;
35 std::vector<color> colors;
38 for(
unsigned int j=0;j<h;j++) {
39 unsigned char* pos = buffer + j * (w * 3);
40 for(
unsigned int i=0;i<w;i++) {
46 unsigned char* ca = (
unsigned char*)&c;
55 std::vector<color>::iterator it;
56 for(it=colors.begin();it!=colors.end();++it) {
62 if(!
found) colors.push_back(c);
67 unsigned int colorn = (
unsigned int)colors.size();
70 std::vector<unsigned char> chars;
71 {
for(
unsigned char c=0;c<255;c++) {
77 unsigned int base = (
unsigned int)chars.size();
79 unsigned int nchar = 1;
80 {
unsigned int i = colorn;
88 a_out <<
"colors " << colorn
94 std::string xpm = a_name+
".xpm";
96 FILE* out = ::fopen(xpm.c_str(),
"wb");
98 a_out <<
"tools::xpm::to_xpm :"
99 <<
" can't open " << xpm
105 ::fprintf(out,
"%s",
"/* XPM */\n");
106 ::fprintf(out,
"static const char *%s[] = {\n",a_name.c_str());
107 ::fprintf(out,
"\"%u %u %u %u\",\n",w,h,colorn,nchar);
110 {
for(
unsigned int index=0;index<colorn;index++) {
111 ::fprintf(out,
"%s",
"\"");
113 {
unsigned int ix = index;
114 for(
unsigned int ic=0;ic<nchar;ic++) {
115 ::fprintf(out,
"%c",chars[ix%base]);
119 ::fprintf(out,
"%s",
"\tc #");
121 color c = colors[index];
122 unsigned char* ca = (
unsigned char*)&c;
123 ::fprintf(out,
"%02x%02x%02x",ca[0],ca[1],ca[2]);
125 ::fprintf(out,
"%s",
"\",\n");
129 {
unsigned char r,g,b;
130 for(
unsigned int j=0;j<h;j++) {
131 unsigned char* pos = buffer + j * (w * 3);
132 ::fprintf(out,
"%s",
"\"");
133 for(
unsigned int i=0;i<w;i++) {
139 unsigned char* ca = (
unsigned char*)&c;
146 {
for(
unsigned int index=0;index<colorn;index++) {
149 if(c==colors[index]) {
151 {
unsigned int ix = index;
152 for(
unsigned int ic=0;ic<nchar;ic++) {
153 ::fprintf(out,
"%c",chars[ix%base]);
164 a_out <<
"tools::xpm::to_xpm :"
165 <<
" color not found in the colormap."
175 ::fprintf(out,
"%s\n",
"\"");
177 ::fprintf(out,
"%s\n",
"\",");
180 ::fprintf(out,
"%s",
"};");