12 #ifndef TOOLS_USE_OUREX_JPEG
15 #include <ourex_jpeglib.h>
26 inline 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) {
28 struct jpeg_compress_struct cinfo;
30 struct jpeg_error_mgr jerr;
31 cinfo.err = ::jpeg_std_error(&jerr);
33 ::jpeg_create_compress(&cinfo);
35 ::jpeg_stdio_dest(&cinfo,a_file);
37 cinfo.image_width = a_width;
38 cinfo.image_height = a_height;
39 cinfo.input_components = a_bpp;
40 cinfo.in_color_space = JCS_RGB;
42 ::jpeg_set_defaults(&cinfo);
43 ::jpeg_set_quality(&cinfo,a_quality,TRUE);
45 ::jpeg_start_compress(&cinfo,TRUE);
47 int row_stride = a_width * cinfo.input_components;
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);
55 ::jpeg_finish_compress(&cinfo);
56 ::jpeg_destroy_compress(&cinfo);
61 inline bool write_file_100(FILE* a_file,
unsigned char* a_buffer,
unsigned int a_width,
unsigned int a_height,
unsigned int a_bpp) {
66 struct jpeg_error_mgr
pub;
82 inline bool infos_file(FILE* a_file,
unsigned int& a_width,
unsigned int& a_height,
unsigned int& a_bpp) {
85 struct jpeg_decompress_struct cinfo;
88 cinfo.err = ::jpeg_std_error(&jerr.
pub);
98 ::jpeg_create_decompress(&cinfo);
99 ::jpeg_stdio_src(&cinfo,a_file);
100 ::jpeg_read_header(&cinfo, TRUE);
102 ::jpeg_start_decompress(&cinfo);
104 a_width = cinfo.output_width;
105 a_height = cinfo.output_height;
106 a_bpp = cinfo.output_components;
108 ::jpeg_abort_decompress(&cinfo);
109 ::jpeg_destroy_decompress(&cinfo);
114 inline unsigned char*
read_file(FILE* a_file,
unsigned int& a_width,
unsigned int& a_height,
unsigned int& a_bpp) {
119 struct jpeg_decompress_struct cinfo;
122 cinfo.err = ::jpeg_std_error(&jerr.
pub);
125 #ifdef TOOLS_JPEG_OUT_ERR
126 ::printf(
"tools::jpeg::read_file : out of setjmp\n");
135 ::jpeg_create_decompress(&cinfo);
136 ::jpeg_stdio_src(&cinfo,a_file);
137 ::jpeg_read_header(&cinfo, TRUE);
139 ::jpeg_start_decompress(&cinfo);
141 int row_stride = cinfo.output_width * cinfo.output_components;
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);
150 ::jpeg_abort_decompress(&cinfo);
151 ::jpeg_destroy_decompress(&cinfo);
158 a_width = cinfo.output_width;
159 a_height = cinfo.output_height;
160 a_bpp = cinfo.output_components;
162 unsigned char* image_buffer =
new unsigned char[row_stride * cinfo.output_height];
164 #ifdef TOOLS_JPEG_OUT_ERR
165 ::printf(
"tools::jpeg::read_file : alloc of %d bytes failed\n",
166 row_stride * cinfo.output_height);
168 ::jpeg_abort_decompress(&cinfo);
169 ::jpeg_destroy_decompress(&cinfo);
179 unsigned char* pos = image_buffer + row_stride * cinfo.output_height;
180 while (cinfo.output_scanline<cinfo.output_height) {
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");
187 ::jpeg_abort_decompress(&cinfo);
188 ::jpeg_destroy_decompress(&cinfo);
192 delete [] image_buffer;
197 if(cinfo.output_scanline<cinfo.output_height)
198 ::jpeg_abort_decompress(&cinfo);
200 ::jpeg_finish_decompress(&cinfo);
202 ::jpeg_destroy_decompress(&cinfo);
216 inline bool is(
const std::string& a_file){
217 FILE* file = ::fopen(a_file.c_str(),
"rb");
218 if(!file)
return false;
219 unsigned char head[4];
220 size_t nitem = ::fread(head,1,4,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;
230 inline bool infos(std::ostream& a_out,
const std::string& a_file,
unsigned int& a_width,
unsigned int& a_height,
unsigned int& a_bpp) {
232 a_out <<
"tools::jpeg::infos :"
233 <<
" file " << a_file <<
" is not jpeg."
241 FILE* file = ::fopen(a_file.c_str(),
"rb");
243 a_out <<
"tools::jpeg::infos :"
244 <<
" can't open " << a_file
252 unsigned int w,h,bpp;
258 a_out <<
"tools::jpeg::infos :"
259 <<
" problem reading " << a_file
273 inline unsigned char*
read(std::ostream& a_out,
const std::string& a_file,
unsigned int& a_width,
unsigned int& a_height,
unsigned int& a_bpp) {
275 a_out <<
"tools::jpeg::read :"
276 <<
" file " << a_file <<
" is not jpeg."
284 FILE* file = ::fopen(a_file.c_str(),
"rb");
286 a_out <<
"tools::jpeg::read :"
287 <<
" can't open " << a_file
295 unsigned int w,h,bpp;
296 unsigned char* buffer =
read_file(file,w,h,bpp);
301 a_out <<
"tools::jpeg::read :"
302 <<
" problem reading " << a_file
317 const std::string& a_file,
318 unsigned int a_sx,
unsigned int a_sy,
319 unsigned int a_sw,
unsigned int a_sh,
320 unsigned int& a_rw,
unsigned int& a_rh,
321 unsigned int& a_rbpp) {
323 a_out <<
"tools::jpeg::read_part :"
324 <<
" file " << a_file <<
" is not jpeg."
332 FILE* file = ::fopen(a_file.c_str(),
"rb");
334 a_out <<
"tools::jpeg::read_part :"
335 <<
" can't open " << a_file
343 struct jpeg_decompress_struct cinfo;
346 cinfo.err = ::jpeg_std_error(&jerr.
pub);
358 ::jpeg_create_decompress(&cinfo);
359 ::jpeg_stdio_src(&cinfo,file);
360 ::jpeg_read_header(&cinfo,TRUE);
362 ::jpeg_start_decompress(&cinfo);
364 int row_stride = cinfo.output_width * cinfo.output_components;
366 unsigned int w = cinfo.output_width;
367 unsigned int h = cinfo.output_height;
369 if((a_sx>=w)||(a_sy>=h)){
370 ::jpeg_abort_decompress(&cinfo);
371 ::jpeg_destroy_decompress(&cinfo);
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;
384 if(!a_rw||!a_rh||!a_rbpp){
385 ::jpeg_abort_decompress(&cinfo);
386 ::jpeg_destroy_decompress(&cinfo);
394 unsigned char* image_buffer =
new unsigned char[a_rw*a_rh*a_rbpp];
396 ::jpeg_abort_decompress(&cinfo);
397 ::jpeg_destroy_decompress(&cinfo);
405 unsigned char*
line =
new unsigned char[row_stride];
407 ::jpeg_abort_decompress(&cinfo);
408 ::jpeg_destroy_decompress(&cinfo);
410 delete [] image_buffer;
419 unsigned int hbeg = a_sy;
420 unsigned int hend = a_sy+a_rh;
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);
431 delete [] image_buffer;
437 if((hbeg<=cinfo.output_scanline)&&(cinfo.output_scanline<hend)){
439 ::memcpy(pos,
line+a_sx*a_rbpp,a_rw*a_rbpp);
443 if(cinfo.output_scanline<cinfo.output_height)
444 ::jpeg_abort_decompress(&cinfo);
446 ::jpeg_finish_decompress(&cinfo);
448 ::jpeg_destroy_decompress(&cinfo);
457 inline bool write(std::ostream& a_out,
458 const std::string& a_file,
459 unsigned char* a_buffer,
460 unsigned int a_width,
461 unsigned int a_height,
464 FILE* file = ::fopen(a_file.c_str(),
"wb");
466 a_out <<
"tools::jpeg::write :"
467 <<
" can't open file " <<
tools::sout(a_file) <<
"."
471 if(!
write_file(file,a_quality,a_buffer,a_width,a_height,a_bpp)) {
473 a_out <<
"tools::jpeg::write :"
474 <<
" can't write file " <<
tools::sout(a_file) <<
"."
489 inline bool to_xpm(std::ostream& a_out,
const std::string& a_file,
const std::string& a_name,
bool a_verbose =
false) {
502 const std::vector<std::string>& a_files,
503 unsigned int a_cols,
unsigned int a_rows,
504 unsigned int a_bw,
unsigned int a_bh,
506 const std::string& a_file,
int a_quality) {
507 unsigned int wa,ha,bppa;
510 (a_out,a_files,a_cols,a_rows,a_bw,a_bh,a_bc,
read,wa,ha,bppa);
512 a_out <<
"tools::jpeg::concatenate :"
513 <<
" failed to concatenate all buffers."
519 FILE* file = ::fopen(a_file.c_str(),
"wb");
521 a_out <<
"tools::jpeg::concatenate :"
522 <<
" can't open " << file <<
" for writing."
528 if(!
write_file(file,a_quality,ba,wa,ha,bppa)) {
529 a_out <<
"tools::jpeg::concatenate :"
530 <<
" can't write " << a_file <<
"."
545 virtual bool is(
const std::string& a_file)
const {
548 virtual bool infos(std::ostream& a_out,
const std::string& a_file,
549 unsigned int& a_width,
unsigned int& a_height,
550 unsigned int& a_bpp)
const {
553 virtual unsigned char*
read(std::ostream& a_out,
const std::string& a_file,
554 unsigned int& a_width,
unsigned int& a_height,
555 unsigned int& a_bpp)
const {
559 const std::string& a_file,
560 unsigned int a_sx,
unsigned int a_sy,
561 unsigned int a_sw,
unsigned int a_sh,
562 unsigned int& a_rw,
unsigned int& a_rh,
563 unsigned int& a_rbpp)
const {