g4tools  5.4.0
Classes | Typedefs | Functions
tools::image Namespace Reference

Classes

class  ireader
 
class  iterator
 
class  iterator_reader
 
class  readers
 

Typedefs

typedef unsigned char *(* file_reader) (std::ostream &, const std::string &, unsigned int &, unsigned int &, unsigned int &)
 
typedef unsigned char *(* reader) (FILE *, unsigned int &, unsigned int &)
 
typedef bool(* writer) (FILE *, unsigned char *, unsigned int, unsigned int)
 

Functions

unsigned char * concatenate (unsigned char **a_buffers, unsigned int a_w, unsigned int a_h, unsigned int a_bpp, unsigned int a_cols, unsigned int a_rows, unsigned int a_bw, unsigned int a_bh, unsigned char a_bc, unsigned int &a_rw, unsigned int &a_rh)
 
unsigned char * concatenate (std::ostream &a_out, const std::vector< std::string > &a_files, unsigned int a_cols, unsigned int a_rows, unsigned int a_bw, unsigned int a_bh, unsigned char a_bc, file_reader a_file_reader, unsigned int &a_w, unsigned int &a_h, unsigned int &a_bpp)
 
bool convert (std::ostream &a_out, const std::string &a_sin, reader a_reader, const std::string &a_sout, writer a_writer)
 

Typedef Documentation

◆ file_reader

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

Definition at line 77 of file image.

◆ reader

typedef unsigned char*(* tools::image::reader) (FILE *, unsigned int &, unsigned int &)

Definition at line 200 of file image.

◆ writer

typedef bool(* tools::image::writer) (FILE *, unsigned char *, unsigned int, unsigned int)

Definition at line 201 of file image.

Function Documentation

◆ concatenate() [1/2]

unsigned char* tools::image::concatenate ( std::ostream &  a_out,
const std::vector< std::string > &  a_files,
unsigned int  a_cols,
unsigned int  a_rows,
unsigned int  a_bw,
unsigned int  a_bh,
unsigned char  a_bc,
file_reader  a_file_reader,
unsigned int &  a_w,
unsigned int &  a_h,
unsigned int &  a_bpp 
)
inline

Definition at line 79 of file image.

86  {
87  // a_files[0] is cols=0, rows=0
88  // a_files[1] is cols=1, rows=0
89  // a_files[2] is cols=2, rows=0
90  // ...
91  // and row=0 is bottom of big image.
92 
93  unsigned int number = a_cols*a_rows;
94  if(number!=a_files.size()) {
95  a_out << "tools::image::concatenate :"
96  << " bad number of files. " << number << " expected."
97  << std::endl;
98  a_bpp = a_w = a_h = 0;
99  return 0;
100  }
101  if(a_files.empty()) {
102  a_out << "tools::image::concatenate :"
103  << " list of files is empty."
104  << std::endl;
105  a_bpp = a_w = a_h = 0;
106  return 0;
107  }
108 
109  unsigned int w1 = 0;
110  unsigned int h1 = 0;
111  unsigned int bpp1 = 0;
112 
113  typedef unsigned char* buffer_t;
114  buffer_t* bs = new buffer_t[number];
115  {for(unsigned int i=0;i<number;i++) {bs[i] = 0;}}
116 
117  bool read_failed = false;
118  unsigned int index = 0;
119  for(unsigned int j=0;j<a_rows;j++) {
120  for(unsigned int i=0;i<a_cols;i++) {
121  const std::string& file = a_files[index];
122  unsigned int w,h,bpp;
123  unsigned char* b = a_file_reader(a_out,file,w,h,bpp);
124  if(!b) {
125  a_out << "tools::image::concatenate :"
126  << " can't read " << file << " expected."
127  << std::endl;
128  read_failed = true;
129  break;
130  }
131  if(!index) {
132  w1 = w;
133  h1 = h;
134  bpp1 = bpp;
135  } else {
136  if(w!=w1) {
137  a_out << "tools::image::concatenate :"
138  << " file " << file
139  << " does not have same width image as the first file one."
140  << " (" << w << "," << w1 << ")."
141  << std::endl;
142  delete [] b;
143  read_failed = true;
144  break;
145  }
146  if(h!=h1) {
147  a_out << "tools::image::concatenate :"
148  << " file " << file
149  << " does not have same height image as the first file one."
150  << " (" << h << "," << h1 << ")."
151  << std::endl;
152  delete [] b;
153  read_failed = true;
154  break;
155  }
156  if(bpp!=bpp1) {
157  a_out << "tools::image::concatenate :"
158  << " file " << file
159  << " does not have same bytes per pixel as the first file one."
160  << " (" << h << "," << h1 << ")."
161  << std::endl;
162  delete [] b;
163  read_failed = true;
164  break;
165  }
166  }
167 
168  bs[index] = b;
169 
170  index++;
171  }
172  if(read_failed) break;
173  }
174 
175  if(read_failed) {
176  {for(unsigned int i=0;i<number;i++) {delete [] bs[i];}}
177  a_bpp = a_w = a_h = 0;
178  return 0;
179  }
180 
181  unsigned int wa,ha;
182  unsigned char* ba = image::concatenate(bs,w1,h1,bpp1,a_cols,a_rows,a_bw,a_bh,a_bc,wa,ha);
183  if(!ba) {
184  a_out << "tools::image::concatenate :"
185  << " failed to concatenate all buffers."
186  << std::endl;
187  {for(unsigned int i=0;i<number;i++) {delete [] bs[i];}}
188  a_bpp = a_w = a_h = 0;
189  return 0;
190  }
191 
192  {for(unsigned int i=0;i<number;i++) {delete [] bs[i];}}
193 
194  a_w = wa;
195  a_h = ha;
196  a_bpp = bpp1;
197  return ba;
198 }

◆ concatenate() [2/2]

unsigned char* tools::image::concatenate ( unsigned char **  a_buffers,
unsigned int  a_w,
unsigned int  a_h,
unsigned int  a_bpp,
unsigned int  a_cols,
unsigned int  a_rows,
unsigned int  a_bw,
unsigned int  a_bh,
unsigned char  a_bc,
unsigned int &  a_rw,
unsigned int &  a_rh 
)
inline

Definition at line 18 of file image.

24  {
25 
26  // assume a_buffers has a_cols*a_rows entries.
27  // a_buffers[0] is bottom-left
28  // a_row=0 is bottom.
29 
30  unsigned int wbw = a_w + 2*a_bw;
31  unsigned int hbh = a_h + 2*a_bh;
32 
33  a_rw = wbw * a_cols;
34  a_rh = hbh * a_rows;
35 
36  //printf("debug : %d %d\n",a_rw,a_rh);
37 
38  // on big concatenated image the below may fail :
39  unsigned char* rb = new unsigned char[a_rh*a_rw*a_bpp];
40  if(!rb) {a_rw = 0;a_rh = 0;return 0;}
41 
42  unsigned int wbw3 = wbw*a_bpp;
43  unsigned int aw3 = a_w*a_bpp;
44 
45  //copy tiles :
46  unsigned int index = 0;
47  for(unsigned int j=0;j<a_rows;j++) {
48  for(unsigned int i=0;i<a_cols;i++) {
49  unsigned char* tile = a_buffers[index];
50 
51  for(unsigned int r=0;r<hbh;r++) {
52  unsigned char* pos = rb + (j*hbh+r)*a_rw*a_bpp + i*wbw*a_bpp;
53  ::memset(pos,a_bc,wbw3);
54  }
55 
56  for(unsigned int r=0;r<a_h;r++) {
57  unsigned char* pos = rb + (j*hbh+r+a_bh)*a_rw*a_bpp + (i*wbw+a_bw)*a_bpp;
58  unsigned char* ptile = tile+r*aw3;
59  for(unsigned int c=0;c<aw3;c++,pos++,ptile++) *pos = *ptile;
60  }
61 
62  index++;
63  }
64  }
65 
66  return rb;
67 }

◆ convert()

bool tools::image::convert ( std::ostream &  a_out,
const std::string &  a_sin,
reader  a_reader,
const std::string &  a_sout,
writer  a_writer 
)
inline

Definition at line 203 of file image.

207  {
208 
209  FILE* fin = ::fopen(a_sin.c_str(),"rb");
210  if(!fin) {
211  a_out << "tools::image::convert :"
212  << " can't open " << a_sin
213  << std::endl;
214  return false;
215  }
216 
217  unsigned int w,h;
218  unsigned char* b = a_reader(fin,w,h);
219  if(!b) {
220  a_out << "tools::image::convert :"
221  << " can't read " << a_sin
222  << std::endl;
223  ::fclose(fin);
224  return false;
225  }
226  ::fclose(fin);
227 
228  FILE* fout = ::fopen(a_sout.c_str(),"wb");
229  if(!fout) {
230  a_out << "tools::image::convert :"
231  << " can't open " << a_sout
232  << std::endl;
233  delete [] b;
234  return false;
235  }
236 
237  if(!a_writer(fout,b,w,h)) {
238  a_out << "tools::image::convert :"
239  << " can't write " << a_sout
240  << std::endl;
241  ::fclose(fout);
242  delete [] b;
243  return false;
244  }
245  ::fclose(fout);
246  delete [] b;
247  return true;
248 }
tools::image::concatenate
unsigned char * concatenate(std::ostream &a_out, const std::vector< std::string > &a_files, unsigned int a_cols, unsigned int a_rows, unsigned int a_bw, unsigned int a_bh, unsigned char a_bc, file_reader a_file_reader, unsigned int &a_w, unsigned int &a_h, unsigned int &a_bpp)
Definition: image:79