g4tools  5.4.0
Typedefs | Functions
tools::xpm Namespace Reference

Typedefs

typedef unsigned char *(* file_reader) (std::ostream &, const std::string &, unsigned int &, unsigned int &, unsigned int &)
 

Functions

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)
 

Typedef Documentation

◆ file_reader

typedef unsigned char*(* tools::xpm::file_reader) (std::ostream &, const std::string &, unsigned int &, unsigned int &, unsigned int &)

Definition at line 17 of file xpm.

Function Documentation

◆ to_xpm()

bool tools::xpm::to_xpm ( std::ostream &  a_out,
const std::string &  a_file,
const std::string &  a_name,
file_reader  a_file_reader,
bool  a_verbose 
)
inline

Definition at line 19 of file xpm.

19  {
20  // a_name is the xpm name. Then a string without a path and without
21  // the .xpm suffix.
22 
23  unsigned int w,h,bpp;
24  unsigned char* buffer = a_file_reader(a_out,a_file,w,h,bpp);
25  if(!buffer) {
26  a_out << "tools::xpm::to_xpm :"
27  << " read_file failed."
28  << std::endl;
29  return false;
30  }
31 
32  // get colormap :
33 
34  typedef unsigned int color;
35  std::vector<color> colors;
36 
37  {unsigned char r,g,b;
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++) {
41  r = *pos;pos++;
42  g = *pos;pos++;
43  b = *pos;pos++;
44 
45  color c = 0;
46  unsigned char* ca = (unsigned char*)&c;
47  ca[0] = r;
48  ca[1] = g;
49  ca[2] = b;
50  ca[3] = 0;
51 
52  //printf("debug : get : %d %d %d : %lu\n",r,g,b,colors.size());
53 
54  bool found = false;
55  std::vector<color>::iterator it;
56  for(it=colors.begin();it!=colors.end();++it) {
57  if(*it==c) {
58  found = true;
59  break;
60  }
61  }
62  if(!found) colors.push_back(c);
63 
64  }
65  }}
66 
67  unsigned int colorn = (unsigned int)colors.size();
68 
69  //get a set of non problematic characters (for exa avoid \‍).
70  std::vector<unsigned char> chars;
71  {for(unsigned char c=0;c<255;c++) {
72  if(is_letter(c)||is_digit(c)) {
73  chars.push_back(c);
74  }
75  }}
76 
77  unsigned int base = (unsigned int)chars.size();
78 
79  unsigned int nchar = 1;
80  {unsigned int i = colorn;
81  while(i>=base) {
82  //unsigned int r = i%base;
83  i /= base;
84  nchar++;
85  }}
86 
87  if(a_verbose) {
88  a_out << "colors " << colorn
89  << " base " << base
90  << " nchar " << nchar
91  << std::endl;
92  }
93 
94  std::string xpm = a_name+".xpm";
95 
96  FILE* out = ::fopen(xpm.c_str(),"wb");
97  if(!out) {
98  a_out << "tools::xpm::to_xpm :"
99  << " can't open " << xpm
100  << std::endl;
101  delete [] buffer;
102  return false;
103  }
104 
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);
108 
109  // write colormap :
110  {for(unsigned int index=0;index<colorn;index++) {
111  ::fprintf(out,"%s","\"");
112 
113  {unsigned int ix = index;
114  for(unsigned int ic=0;ic<nchar;ic++) {
115  ::fprintf(out,"%c",chars[ix%base]);
116  ix /= base;
117  }}
118 
119  ::fprintf(out,"%s","\tc #");
120 
121  color c = colors[index];
122  unsigned char* ca = (unsigned char*)&c;
123  ::fprintf(out,"%02x%02x%02x",ca[0],ca[1],ca[2]);
124 
125  ::fprintf(out,"%s","\",\n");
126  }}
127 
128  //pixmap :
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++) {
134  r = *pos;pos++;
135  g = *pos;pos++;
136  b = *pos;pos++;
137 
138  color c;
139  unsigned char* ca = (unsigned char*)&c;
140  ca[0] = r;
141  ca[1] = g;
142  ca[2] = b;
143  ca[3] = 0;
144 
145  bool found = false;
146  {for(unsigned int index=0;index<colorn;index++) {
147  //unsigned char c1 = index%base;
148  //unsigned char c2 = index/base;
149  if(c==colors[index]) {
150 
151  {unsigned int ix = index;
152  for(unsigned int ic=0;ic<nchar;ic++) {
153  ::fprintf(out,"%c",chars[ix%base]);
154  ix /= base;
155  }}
156 
157  //::fprintf(out,"%c%c",chars[c1],chars[c2]);
158 
159  found = true;
160  break;
161  }
162  }}
163  if(!found) {
164  a_out << "tools::xpm::to_xpm :"
165  << " color not found in the colormap."
166  << std::endl;
167  ::fclose(out);
168  ::remove(xpm.c_str());
169  delete [] buffer;
170  return false;
171  }
172 
173  }
174  if(j==(h-1))
175  ::fprintf(out,"%s\n","\"");
176  else
177  ::fprintf(out,"%s\n","\",");
178  }}
179 
180  ::fprintf(out,"%s","};");
181 
182  ::fclose(out);
183  delete [] buffer;
184 
185  return true;
186 }
tools::remove
bool remove(std::vector< T > &a_vals, const T &a_elem)
Definition: vmanip:119
tools::is_letter
bool is_letter(char a_char)
Definition: charmanip:101
tools::is_digit
bool is_digit(char a_char)
Definition: charmanip:84
tools::file::found
bool found(const std::string &a_file, const std::string &a_what, std::vector< std::string > &a_found)
Definition: file:507