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

Classes

struct  error_mgr
 
class  reader
 

Typedefs

typedef struct error_mgrerror_ptr
 

Functions

bool write_file (FILE *a_file, int a_quality, unsigned char *a_buffer, unsigned int a_width, unsigned int a_height, unsigned int a_bpp)
 
bool write_file_100 (FILE *a_file, unsigned char *a_buffer, unsigned int a_width, unsigned int a_height, unsigned int a_bpp)
 
void tools_jpeg_error_exit (j_common_ptr cinfo)
 
bool infos_file (FILE *a_file, unsigned int &a_width, unsigned int &a_height, unsigned int &a_bpp)
 
unsigned char * read_file (FILE *a_file, unsigned int &a_width, unsigned int &a_height, unsigned int &a_bpp)
 
bool is (const std::string &a_file)
 
bool infos (std::ostream &a_out, const std::string &a_file, unsigned int &a_width, unsigned int &a_height, unsigned int &a_bpp)
 
unsigned char * read (std::ostream &a_out, const std::string &a_file, unsigned int &a_width, unsigned int &a_height, unsigned int &a_bpp)
 
unsigned char * read_part (std::ostream &a_out, const std::string &a_file, unsigned int a_sx, unsigned int a_sy, unsigned int a_sw, unsigned int a_sh, unsigned int &a_rw, unsigned int &a_rh, unsigned int &a_rbpp)
 
bool write (std::ostream &a_out, const std::string &a_file, unsigned char *a_buffer, unsigned int a_width, unsigned int a_height, unsigned int a_bpp, int a_quality)
 
bool to_xpm (std::ostream &a_out, const std::string &a_file, const std::string &a_name, bool a_verbose=false)
 
bool 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, const std::string &a_file, int a_quality)
 

Typedef Documentation

◆ error_ptr

Definition at line 70 of file jpeg.

Function Documentation

◆ concatenate()

bool tools::jpeg::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,
const std::string &  a_file,
int  a_quality 
)
inline

Definition at line 501 of file jpeg.

506  {
507  unsigned int wa,ha,bppa;
508  unsigned char* ba =
510  (a_out,a_files,a_cols,a_rows,a_bw,a_bh,a_bc,read,wa,ha,bppa);
511  if(!ba) {
512  a_out << "tools::jpeg::concatenate :"
513  << " failed to concatenate all buffers."
514  << std::endl;
515  delete [] ba;
516  return false;
517  }
518 
519  FILE* file = ::fopen(a_file.c_str(),"wb");
520  if(!file) {
521  a_out << "tools::jpeg::concatenate :"
522  << " can't open " << file << " for writing."
523  << std::endl;
524  delete [] ba;
525  return false;
526  }
527 
528  if(!write_file(file,a_quality,ba,wa,ha,bppa)) {
529  a_out << "tools::jpeg::concatenate :"
530  << " can't write " << a_file << "."
531  << std::endl;
532  delete [] ba;
533  ::fclose(file);
534  return false;
535  }
536 
537  delete [] ba;
538  ::fclose(file);
539  return true;
540 }

◆ infos()

bool tools::jpeg::infos ( std::ostream &  a_out,
const std::string &  a_file,
unsigned int &  a_width,
unsigned int &  a_height,
unsigned int &  a_bpp 
)
inline

Definition at line 230 of file jpeg.

230  {
231  if(!is(a_file)) {
232  a_out << "tools::jpeg::infos :"
233  << " file " << a_file << " is not jpeg."
234  << std::endl;
235  a_width = 0;
236  a_height = 0;
237  a_bpp = 0;
238  return false;
239  }
240 
241  FILE* file = ::fopen(a_file.c_str(),"rb");
242  if(!file) {
243  a_out << "tools::jpeg::infos :"
244  << " can't open " << a_file
245  << std::endl;
246  a_width = 0;
247  a_height = 0;
248  a_bpp = 0;
249  return false;
250  }
251 
252  unsigned int w,h,bpp;
253  bool status = infos_file(file,w,h,bpp);
254 
255  ::fclose(file);
256 
257  if(!status) {
258  a_out << "tools::jpeg::infos :"
259  << " problem reading " << a_file
260  << std::endl;
261  a_width = 0;
262  a_height = 0;
263  a_bpp = 0;
264  return false;
265  }
266 
267  a_width = w;
268  a_height = h;
269  a_bpp = bpp;
270  return true;
271 }

◆ infos_file()

bool tools::jpeg::infos_file ( FILE *  a_file,
unsigned int &  a_width,
unsigned int &  a_height,
unsigned int &  a_bpp 
)
inline

Definition at line 82 of file jpeg.

82  {
83  //NOTE : G.Barrand : not sure that in case of error, we cleanup properly !
84 
85  struct jpeg_decompress_struct cinfo;
86  struct error_mgr jerr;
87 
88  cinfo.err = ::jpeg_std_error(&jerr.pub);
89  jerr.pub.error_exit = tools_jpeg_error_exit;
90  if(::setjmp(jerr.setjmp_buffer)) {
91  //printf("debug : out of setjmp\n");
92  a_width = 0;
93  a_height = 0;
94  a_bpp = 0;
95  return false;
96  }
97 
98  ::jpeg_create_decompress(&cinfo);
99  ::jpeg_stdio_src(&cinfo,a_file);
100  ::jpeg_read_header(&cinfo, TRUE);
101 
102  ::jpeg_start_decompress(&cinfo);
103 
104  a_width = cinfo.output_width;
105  a_height = cinfo.output_height;
106  a_bpp = cinfo.output_components;
107 
108  ::jpeg_abort_decompress(&cinfo);
109  ::jpeg_destroy_decompress(&cinfo);
110 
111  return true;
112 }

◆ is()

bool tools::jpeg::is ( const std::string &  a_file)
inline

Definition at line 216 of file jpeg.

216  {
217  FILE* file = ::fopen(a_file.c_str(),"rb");
218  if(!file) return false; //if file does not exist, then it is not a jpeg !
219  unsigned char head[4];
220  size_t nitem = ::fread(head,1,4,file);
221  ::fclose(file);
222  if(nitem!=4) return false;
223  if(head[0]!=255) return false;
224  if(head[1]!=216) return false;
225  if(head[2]!=255) return false;
226  //if(head[3]!=224) return false; //LRI.jpg is 225 !
227  return true;
228 }

◆ read()

unsigned char* tools::jpeg::read ( std::ostream &  a_out,
const std::string &  a_file,
unsigned int &  a_width,
unsigned int &  a_height,
unsigned int &  a_bpp 
)
inline

Definition at line 273 of file jpeg.

273  {
274  if(!is(a_file)) {
275  a_out << "tools::jpeg::read :"
276  << " file " << a_file << " is not jpeg."
277  << std::endl;
278  a_width = 0;
279  a_height = 0;
280  a_bpp = 0;
281  return 0;
282  }
283 
284  FILE* file = ::fopen(a_file.c_str(),"rb");
285  if(!file) {
286  a_out << "tools::jpeg::read :"
287  << " can't open " << a_file
288  << std::endl;
289  a_width = 0;
290  a_height = 0;
291  a_bpp = 0;
292  return 0;
293  }
294 
295  unsigned int w,h,bpp;
296  unsigned char* buffer = read_file(file,w,h,bpp);
297 
298  ::fclose(file);
299 
300  if(!buffer) {
301  a_out << "tools::jpeg::read :"
302  << " problem reading " << a_file
303  << std::endl;
304  a_width = 0;
305  a_height = 0;
306  a_bpp = 0;
307  return 0;
308  }
309 
310  a_width = w;
311  a_height = h;
312  a_bpp = bpp;
313  return buffer;
314 }

◆ read_file()

unsigned char* tools::jpeg::read_file ( FILE *  a_file,
unsigned int &  a_width,
unsigned int &  a_height,
unsigned int &  a_bpp 
)
inline

Definition at line 114 of file jpeg.

114  {
115  // the returned buffer must be delete with "delete []".
116 
117  //NOTE : G.Barrand : not sure that in case of error, we cleanup properly !
118 
119  struct jpeg_decompress_struct cinfo;
120  struct error_mgr jerr;
121 
122  cinfo.err = ::jpeg_std_error(&jerr.pub);
123  jerr.pub.error_exit = tools_jpeg_error_exit;
124  if(::setjmp(jerr.setjmp_buffer)) {
125 #ifdef TOOLS_JPEG_OUT_ERR
126  ::printf("tools::jpeg::read_file : out of setjmp\n");
127 #endif
128  //NOTE : how to be sure to delete the below new if having been done ?
129  a_width = 0;
130  a_height = 0;
131  a_bpp = 0;
132  return 0;
133  }
134 
135  ::jpeg_create_decompress(&cinfo);
136  ::jpeg_stdio_src(&cinfo,a_file);
137  ::jpeg_read_header(&cinfo, TRUE);
138 
139  ::jpeg_start_decompress(&cinfo);
140 
141  int row_stride = cinfo.output_width * cinfo.output_components;
142 
143  if((!cinfo.output_width)||
144  (!cinfo.output_height)||
145  (cinfo.output_components<=0)) {
146 #ifdef TOOLS_JPEG_OUT_ERR
147  ::printf("tools::jpeg::read_file : bad size, w %u h %u cpnts %d\n",
148  cinfo.output_width,cinfo.output_height,cinfo.output_components);
149 #endif
150  ::jpeg_abort_decompress(&cinfo);
151  ::jpeg_destroy_decompress(&cinfo);
152  a_width = 0;
153  a_height = 0;
154  a_bpp = 0;
155  return 0;
156  }
157 
158  a_width = cinfo.output_width;
159  a_height = cinfo.output_height;
160  a_bpp = cinfo.output_components;
161 
162  unsigned char* image_buffer = new unsigned char[row_stride * cinfo.output_height];
163  if(!image_buffer) {
164 #ifdef TOOLS_JPEG_OUT_ERR
165  ::printf("tools::jpeg::read_file : alloc of %d bytes failed\n",
166  row_stride * cinfo.output_height);
167 #endif
168  ::jpeg_abort_decompress(&cinfo);
169  ::jpeg_destroy_decompress(&cinfo);
170  a_width = 0;
171  a_height = 0;
172  a_bpp = 0;
173  return 0;
174  }
175 
176  //FIXME : iPad : the below crashes on LHCb_artist_16384_4096_80.jpg.
177 
178  {JSAMPROW buffer[1];
179  unsigned char* pos = image_buffer + row_stride * cinfo.output_height;
180  while (cinfo.output_scanline<cinfo.output_height) {
181  pos -= row_stride;
182  buffer[0] = pos;
183  if(::jpeg_read_scanlines(&cinfo, buffer, 1)!=1) {
184 #ifdef TOOLS_JPEG_OUT_ERR
185  ::printf("tools::jpeg::read_file : jpeg_read_scanlines failed\n");
186 #endif
187  ::jpeg_abort_decompress(&cinfo);
188  ::jpeg_destroy_decompress(&cinfo);
189  a_width = 0;
190  a_height = 0;
191  a_bpp = 0;
192  delete [] image_buffer;
193  return 0;
194  }
195  }}
196 
197  if(cinfo.output_scanline<cinfo.output_height)
198  ::jpeg_abort_decompress(&cinfo);
199  else
200  ::jpeg_finish_decompress(&cinfo);
201 
202  ::jpeg_destroy_decompress(&cinfo);
203 
204  return image_buffer;
205 }

◆ read_part()

unsigned char* tools::jpeg::read_part ( std::ostream &  a_out,
const std::string &  a_file,
unsigned int  a_sx,
unsigned int  a_sy,
unsigned int  a_sw,
unsigned int  a_sh,
unsigned int &  a_rw,
unsigned int &  a_rh,
unsigned int &  a_rbpp 
)
inline

Definition at line 316 of file jpeg.

321  {
322  if(!is(a_file)) {
323  a_out << "tools::jpeg::read_part :"
324  << " file " << a_file << " is not jpeg."
325  << std::endl;
326  a_rw = 0;
327  a_rh = 0;
328  a_rbpp = 0;
329  return 0;
330  }
331 
332  FILE* file = ::fopen(a_file.c_str(),"rb");
333  if(!file) {
334  a_out << "tools::jpeg::read_part :"
335  << " can't open " << a_file
336  << std::endl;
337  a_rw = 0;
338  a_rh = 0;
339  a_rbpp = 0;
340  return 0;
341  }
342 
343  struct jpeg_decompress_struct cinfo;
344  struct error_mgr jerr;
345 
346  cinfo.err = ::jpeg_std_error(&jerr.pub);
347  jerr.pub.error_exit = tools_jpeg_error_exit;
348  if(::setjmp(jerr.setjmp_buffer)) {
349  //printf("debug : out of setjmp\n");
350  ::fclose(file);
351  //NOTE : how to be sure to delete the below news if having been done ?
352  a_rw = 0;
353  a_rh = 0;
354  a_rbpp = 0;
355  return 0;
356  }
357 
358  ::jpeg_create_decompress(&cinfo);
359  ::jpeg_stdio_src(&cinfo,file);
360  ::jpeg_read_header(&cinfo,TRUE);
361 
362  ::jpeg_start_decompress(&cinfo);
363 
364  int row_stride = cinfo.output_width * cinfo.output_components;
365 
366  unsigned int w = cinfo.output_width;
367  unsigned int h = cinfo.output_height;
368 
369  if((a_sx>=w)||(a_sy>=h)){
370  ::jpeg_abort_decompress(&cinfo);
371  ::jpeg_destroy_decompress(&cinfo);
372  ::fclose(file);
373  a_rw = 0;
374  a_rh = 0;
375  a_rbpp = 0;
376  return 0;
377  }
378 
379  // 012345
380  a_rw = tools::mn<unsigned int>(w-a_sx,a_sw);
381  a_rh = tools::mn<unsigned int>(h-a_sy,a_sh);
382  a_rbpp = cinfo.output_components;
383 
384  if(!a_rw||!a_rh||!a_rbpp){
385  ::jpeg_abort_decompress(&cinfo);
386  ::jpeg_destroy_decompress(&cinfo);
387  ::fclose(file);
388  a_rw = 0;
389  a_rh = 0;
390  a_rbpp = 0;
391  return 0;
392  }
393 
394  unsigned char* image_buffer = new unsigned char[a_rw*a_rh*a_rbpp];
395  if(!image_buffer) {
396  ::jpeg_abort_decompress(&cinfo);
397  ::jpeg_destroy_decompress(&cinfo);
398  ::fclose(file);
399  a_rw = 0;
400  a_rh = 0;
401  a_rbpp = 0;
402  return 0;
403  }
404 
405  unsigned char* line = new unsigned char[row_stride];
406  if(!line) {
407  ::jpeg_abort_decompress(&cinfo);
408  ::jpeg_destroy_decompress(&cinfo);
409  ::fclose(file);
410  delete [] image_buffer;
411  a_rw = 0;
412  a_rh = 0;
413  a_rbpp = 0;
414  return 0;
415  }
416 
417  //FIXME : iPad : the below crashes on LHCb_artist_16384_4096_80.jpg.
418 
419  unsigned int hbeg = a_sy;
420  unsigned int hend = a_sy+a_rh;
421 
422  {JSAMPROW buffer[1];
423  buffer[0] = line;
424  unsigned char* pos = image_buffer+a_rh*a_rw*a_rbpp;
425  while(cinfo.output_scanline<cinfo.output_height) {
426  if(jpeg_read_scanlines(&cinfo, buffer, 1)!=1) {
427  ::jpeg_abort_decompress(&cinfo);
428  ::jpeg_destroy_decompress(&cinfo);
429  ::fclose(file);
430  delete [] line;
431  delete [] image_buffer;
432  a_rw = 0;
433  a_rh = 0;
434  a_rbpp = 0;
435  return 0;
436  }
437  if((hbeg<=cinfo.output_scanline)&&(cinfo.output_scanline<hend)){
438  pos -= a_rw*a_rbpp;
439  ::memcpy(pos,line+a_sx*a_rbpp,a_rw*a_rbpp);
440  }
441  }}
442 
443  if(cinfo.output_scanline<cinfo.output_height)
444  ::jpeg_abort_decompress(&cinfo);
445  else
446  ::jpeg_finish_decompress(&cinfo);
447 
448  ::jpeg_destroy_decompress(&cinfo);
449 
450  ::fclose(file);
451 
452  delete [] line;
453 
454  return image_buffer;
455 }

◆ to_xpm()

bool tools::jpeg::to_xpm ( std::ostream &  a_out,
const std::string &  a_file,
const std::string &  a_name,
bool  a_verbose = false 
)
inline

Definition at line 489 of file jpeg.

489  {
490  return tools::xpm::to_xpm(a_out,a_file,a_name,read,a_verbose);
491 }

◆ tools_jpeg_error_exit()

void tools::jpeg::tools_jpeg_error_exit ( j_common_ptr  cinfo)
inline

Definition at line 73 of file jpeg.

73  {
74  error_ptr myerr = (error_ptr) cinfo->err;
75  //(*cinfo->err->output_message) (cinfo);
76  //printf("debug : error_exit\n");
77  //NSLog(@"debug : jpeg::error_exit");
78  ::longjmp(myerr->setjmp_buffer, 1);
79 }

◆ write()

bool tools::jpeg::write ( std::ostream &  a_out,
const std::string &  a_file,
unsigned char *  a_buffer,
unsigned int  a_width,
unsigned int  a_height,
unsigned int  a_bpp,
int  a_quality 
)
inline

Definition at line 457 of file jpeg.

463  {
464  FILE* file = ::fopen(a_file.c_str(),"wb");
465  if(!file) {
466  a_out << "tools::jpeg::write :"
467  << " can't open file " << tools::sout(a_file) << "."
468  << std::endl;
469  return false;
470  }
471  if(!write_file(file,a_quality,a_buffer,a_width,a_height,a_bpp)) {
472  ::fclose(file);
473  a_out << "tools::jpeg::write :"
474  << " can't write file " << tools::sout(a_file) << "."
475  << std::endl;
476  return false;
477  }
478  ::fclose(file);
479  return true;
480 }

◆ write_file()

bool tools::jpeg::write_file ( FILE *  a_file,
int  a_quality,
unsigned char *  a_buffer,
unsigned int  a_width,
unsigned int  a_height,
unsigned int  a_bpp 
)
inline

Definition at line 26 of file jpeg.

26  {
27 
28  struct jpeg_compress_struct cinfo;
29 
30  struct jpeg_error_mgr jerr;
31  cinfo.err = ::jpeg_std_error(&jerr);
32 
33  ::jpeg_create_compress(&cinfo);
34 
35  ::jpeg_stdio_dest(&cinfo,a_file);
36 
37  cinfo.image_width = a_width;
38  cinfo.image_height = a_height;
39  cinfo.input_components = a_bpp; // # of color components per pixel.
40  cinfo.in_color_space = JCS_RGB; // colorspace of input image.
41 
42  ::jpeg_set_defaults(&cinfo);
43  ::jpeg_set_quality(&cinfo,a_quality,TRUE);
44 
45  ::jpeg_start_compress(&cinfo,TRUE);
46 
47  int row_stride = a_width * cinfo.input_components;
48 
49  while (cinfo.next_scanline < cinfo.image_height) {
50  JSAMPROW row_pointer =
51  a_buffer+(cinfo.image_height-1-cinfo.next_scanline) * row_stride;
52  ::jpeg_write_scanlines(&cinfo, &row_pointer, 1);
53  }
54 
55  ::jpeg_finish_compress(&cinfo);
56  ::jpeg_destroy_compress(&cinfo);
57 
58  return true;
59 }

◆ write_file_100()

bool tools::jpeg::write_file_100 ( FILE *  a_file,
unsigned char *  a_buffer,
unsigned int  a_width,
unsigned int  a_height,
unsigned int  a_bpp 
)
inline

Definition at line 61 of file jpeg.

61  {
62  return tools::jpeg::write_file(a_file,100,a_buffer,a_width,a_height,a_bpp);
63 }
tools::jpeg::is
bool is(const std::string &a_file)
Definition: jpeg:216
tools::image::concatenate
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)
Definition: image:18
tools::jpeg::read_file
unsigned char * read_file(FILE *a_file, unsigned int &a_width, unsigned int &a_height, unsigned int &a_bpp)
Definition: jpeg:114
tools::jpeg::write_file
bool write_file(FILE *a_file, int a_quality, unsigned char *a_buffer, unsigned int a_width, unsigned int a_height, unsigned int a_bpp)
Definition: jpeg:26
tools::sout
Definition: sout:17
tools::jpeg::infos_file
bool infos_file(FILE *a_file, unsigned int &a_width, unsigned int &a_height, unsigned int &a_bpp)
Definition: jpeg:82
tools::xpm::to_xpm
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)
Definition: xpm:19
tools::jpeg::error_ptr
struct error_mgr * error_ptr
Definition: jpeg:70
tools::jpeg::tools_jpeg_error_exit
void tools_jpeg_error_exit(j_common_ptr cinfo)
Definition: jpeg:73
tools::jpeg::read
unsigned char * read(std::ostream &a_out, const std::string &a_file, unsigned int &a_width, unsigned int &a_height, unsigned int &a_bpp)
Definition: jpeg:273