COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
jpeg_rw.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jpeglib.h"
#include <setjmp.h>
Include dependency graph for jpeg_rw.cpp:

Classes

struct  my_error_mgr
 

Typedefs

typedef struct my_error_mgrmy_error_ptr
 

Functions

void write_JPEG_file (char *filename, int quality, J_COLOR_SPACE in_color_space, int image_width, int image_height, JSAMPLE *image_buffer)
 
static void my_error_exit (j_common_ptr cinfo)
 
int read_JPEG_file (const char *filename, JSAMPLE *image_buffer)
 
int read_JPEG_file (const char *filename, vector< vector< uint8_t > > &image_buffer)
 

Typedef Documentation

typedef struct my_error_mgr* my_error_ptr

Function Documentation

void write_JPEG_file ( char *  filename,
int  quality,
J_COLOR_SPACE  in_color_space,
int  image_width,
int  image_height,
JSAMPLE image_buffer 
)
75 {
76  /* This struct contains the JPEG compression parameters and pointers to
77  * working space (which is allocated as needed by the JPEG library).
78  * It is possible to have several such structures, representing multiple
79  * compression/decompression processes, in existence at once. We refer
80  * to any one struct (and its associated working data) as a "JPEG object".
81  */
82  struct jpeg_compress_struct cinfo;
83  /* This struct represents a JPEG error handler. It is declared separately
84  * because applications often want to supply a specialized error handler
85  * (see the second half of this file for an example). But here we just
86  * take the easy way out and use the standard error handler, which will
87  * print a message on stderr and call exit() if compression fails.
88  * Note that this struct must live as long as the main JPEG parameter
89  * struct, to avoid dangling-pointer problems.
90  */
91  struct jpeg_error_mgr jerr;
92  /* More stuff */
93  FILE * outfile; /* target file */
94  JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
95  int row_stride; /* physical row width in image buffer */
96 
97  /* Step 1: allocate and initialize JPEG compression object */
98 
99  /* We have to set up the error handler first, in case the initialization
100  * step fails. (Unlikely, but it could happen if you are out of memory.)
101  * This routine fills in the contents of struct jerr, and returns jerr's
102  * address which we place into the link field in cinfo.
103  */
104  cinfo.err = jpeg_std_error(&jerr);
105  /* Now we can initialize the JPEG compression object. */
106  jpeg_create_compress(&cinfo);
107 
108  /* Step 2: specify data destination (eg, a file) */
109  /* Note: steps 2 and 3 can be done in either order. */
110 
111  /* Here we use the library-supplied code to send compressed data to a
112  * stdio stream. You can also write your own code to do something else.
113  * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
114  * requires it in order to write binary files.
115  */
116  if ((outfile = fopen(filename, "wb")) == NULL) {
117  fprintf(stderr, "can't open %s\n", filename);
118  exit(1);
119  }
120  jpeg_stdio_dest(&cinfo, outfile);
121 
122  /* Step 3: set parameters for compression */
123 
124  /* First we supply a description of the input image.
125  * Four fields of the cinfo struct must be filled in:
126  */
127  cinfo.image_width = image_width; /* image width and height, in pixels */
128  cinfo.image_height = image_height;
129  if(in_color_space==JCS_RGB) {
130  cinfo.input_components = 3; /* # of color components per pixel */
131  } else {
132  cinfo.input_components = 1; /* # of color components per pixel */
133  }
134  cinfo.in_color_space = in_color_space; /* colorspace of input image */
135  /* Now use the library's routine to set default compression parameters.
136  * (You must set at least cinfo.in_color_space before calling this,
137  * since the defaults depend on the source color space.)
138  */
139  jpeg_set_defaults(&cinfo);
140  /* Now you can set any non-default parameters you wish to.
141  * Here we just illustrate the use of quality (quantization table) scaling:
142  */
143  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
144 
145  /* Step 4: Start compressor */
146 
147  /* TRUE ensures that we will write a complete interchange-JPEG file.
148  * Pass TRUE unless you are very sure of what you're doing.
149  */
150  jpeg_start_compress(&cinfo, TRUE);
151 
152  /* Step 5: while (scan lines remain to be written) */
153  /* jpeg_write_scanlines(...); */
154 
155  /* Here we use the library's state variable cinfo.next_scanline as the
156  * loop counter, so that we don't have to keep track ourselves.
157  * To keep things simple, we pass one scanline per call; you can pass
158  * more if you wish, though.
159  */
160  if(in_color_space==JCS_RGB) {
161  row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
162  } else {
163  row_stride = image_width; /* JSAMPLEs per row in image_buffer */
164  }
165  while (cinfo.next_scanline < cinfo.image_height) {
166  /* jpeg_write_scanlines expects an array of pointers to scanlines.
167  * Here the array is only one element long, but you could pass
168  * more than one scanline at a time if that's more convenient.
169  */
170  row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
171  (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
172  }
173 
174  /* Step 6: Finish compression */
175 
176  jpeg_finish_compress(&cinfo);
177  /* After finish_compress, we can close the output file. */
178  fclose(outfile);
179 
180  /* Step 7: release JPEG compression object */
181 
182  /* This is an important step since it will release a good deal of memory. */
183  jpeg_destroy_compress(&cinfo);
184 
185  /* And we're done! */
186 }
void jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile)
Definition: jdatadst.cpp:130
Definition: jpeglib.h:648
struct jpeg_error_mgr * jpeg_std_error(struct jpeg_error_mgr *err)
Definition: jerror.cpp:232
JSAMPLE * JSAMPROW
Definition: jpeglib.h:71
Definition: jpeglib.h:214
void jpeg_start_compress(j_compress_ptr cinfo, boolean write_all_tables)
Definition: jcapistd.cpp:38
#define TRUE
Definition: jpleph.cpp:68
void jpeg_finish_compress(j_compress_ptr cinfo)
Definition: jcapimin.cpp:147
Definition: jpeglib.h:273
JDIMENSION jpeg_write_scanlines(j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines)
Definition: jcapistd.cpp:77
void jpeg_set_quality(j_compress_ptr cinfo, int quality, boolean force_baseline)
Definition: jcparam.cpp:132
void jpeg_destroy_compress(j_compress_ptr cinfo)
Definition: jcapimin.cpp:88
void jpeg_set_defaults(j_compress_ptr cinfo)
Definition: jcparam.cpp:268
#define jpeg_create_compress(cinfo)
Definition: jpeglib.h:899
static void my_error_exit ( j_common_ptr  cinfo)
static
273 {
274  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
275  my_error_ptr myerr = (my_error_ptr) cinfo->err;
276 
277  /* Always display the message. */
278  /* We could postpone this until after returning, if we chose. */
279  (*cinfo->err->output_message) (cinfo);
280 
281  /* Return control to the setjmp point */
282  longjmp(myerr->setjmp_buffer, 1);
283 }
void(* output_message)()
Definition: jpeglib.h:654
jmp_buf setjmp_buffer
Definition: jpeg_rw.cpp:262
struct jpeg_error_mgr * err
Definition: jpeglib.h:259
Definition: jpeg_rw.cpp:259
struct my_error_mgr * my_error_ptr
Definition: jpeg_rw.cpp:265
int read_JPEG_file ( const char *  filename,
JSAMPLE image_buffer 
)
293 {
294  FILE *infile;
295 /* This struct contains the JPEG decompression parameters and pointers to
296  * working space (which is allocated as needed by the JPEG library).
297  */
298  struct jpeg_decompress_struct cinfo;
299  /* We use our private extension JPEG error handler.
300  * Note that this struct must live as long as the main JPEG parameter
301  * struct, to avoid dangling-pointer problems.
302  */
303  struct my_error_mgr jerr;
304  /* More stuff */
305  JSAMPARRAY buffer; /* Output row buffer */
306  int row_stride; /* physical row width in output buffer */
307 
308  /* In this example we want to open the input file before doing anything else,
309  * so that the setjmp() error recovery below can assume the file is open.
310  * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
311  * requires it in order to read binary files.
312  */
313 
314  if ((infile = fopen(filename, "rb")) == NULL) {
315  fprintf(stderr, "can't open %s\n", filename);
316  return 0;
317  }
318 
319  /* Step 1: allocate and initialize JPEG decompression object */
320 
321  /* We set up the normal JPEG error routines, then override error_exit. */
322  cinfo.err = jpeg_std_error(&jerr.pub);
323  jerr.pub.error_exit = my_error_exit;
324  /* Establish the setjmp return context for my_error_exit to use. */
325  if (setjmp(jerr.setjmp_buffer))
326  {
327  /* If we get here, the JPEG code has signaled an error.
328  * We need to clean up the JPEG object, close the input file, and return.
329  */
330  jpeg_destroy_decompress(&cinfo);
331  fclose(infile);
332  return 0;
333  }
334  /* Now we can initialize the JPEG decompression object. */
335  jpeg_create_decompress(&cinfo);
336 
337  /* Step 2: specify data source (eg, a file) */
338 
339  jpeg_stdio_src(&cinfo, infile);
340 
341  /* Step 3: read file parameters with jpeg_read_header() */
342 
343  (void) jpeg_read_header(&cinfo, TRUE);
344  /* We can ignore the return value from jpeg_read_header since
345  * (a) suspension is not possible with the stdio data source, and
346  * (b) we passed TRUE to reject a tables-only JPEG file as an error.
347  * See libjpeg.doc for more info.
348  */
349 
350  /* Step 4: set parameters for decompression */
351 
352  /* In this example, we don't need to change any of the defaults set by
353  * jpeg_read_header(), so we do nothing here.
354  */
355 
356  /* Step 5: Start decompressor */
357 
358  (void) jpeg_start_decompress(&cinfo);
359  /* We can ignore the return value since suspension is not possible
360  * with the stdio data source.
361  */
362 
363  /* We may need to do some setup of our own at this point before reading
364  * the data. After jpeg_start_decompress() we have the correct scaled
365  * output image dimensions available, as well as the output colormap
366  * if we asked for color quantization.
367  * In this example, we need to make an output work buffer of the right size.
368  */
369  /* JSAMPLEs per row in output buffer */
370  row_stride = cinfo.output_width * cinfo.output_components;
371  /* Make a one-row-high sample array that will go away when done with image */
372  buffer = (*cinfo.mem->alloc_sarray)
373  ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
374 
375  /* Step 6: while (scan lines remain to be read) */
376  /* jpeg_read_scanlines(...); */
377 
378  /* Here we use the library's state variable cinfo.output_scanline as the
379  * loop counter, so that we don't have to keep track ourselves.
380  */
381  while (cinfo.output_scanline < cinfo.output_height) {
382  /* jpeg_read_scanlines expects an array of pointers to scanlines.
383  * Here the array is only one element long, but you could ask for
384  * more than one scanline at a time if that's more convenient.
385  */
386  (void) jpeg_read_scanlines(&cinfo, buffer, 1);
387  /* Assume put_scanline_someplace wants a pointer and sample count. */
388  //put_scanline_someplace(buffer[0], row_stride);
389  memcpy((void *)image_buffer, (const void *)buffer[0], row_stride);
390  image_buffer += row_stride;
391  }
392 
393  /* Step 7: Finish decompression */
394 
395  (void) jpeg_finish_decompress(&cinfo);
396  /* We can ignore the return value since suspension is not possible
397  * with the stdio data source.
398  */
399 
400  /* Step 8: Release JPEG decompression object */
401 
402  /* This is an important step since it will release a good deal of memory. */
403  jpeg_destroy_decompress(&cinfo);
404 
405  /* After finish_decompress, we can close the input file.
406  * Here we postpone it until after no more JPEG errors are possible,
407  * so as to simplify the setjmp error logic above. (Actually, I don't
408  * think that jpeg_destroy can do an error exit, but why assume anything...)
409  */
410  fclose(infile);
411 
412  /* At this point you may want to check to see whether any corrupt-data
413  * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
414  */
415 
416  /* And we're done! */
417  return 1;
418 }
int jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
Definition: jdapimin.cpp:241
void jpeg_stdio_src(j_decompress_ptr cinfo, FILE *infile)
Definition: jdatasrc.cpp:182
void jpeg_destroy_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:91
struct jpeg_error_mgr * jpeg_std_error(struct jpeg_error_mgr *err)
Definition: jerror.cpp:232
Definition: jpeglib.h:258
#define jpeg_create_decompress(cinfo)
Definition: jpeglib.h:902
#define JPOOL_IMAGE
Definition: jpeglib.h:754
static char buffer[255]
Definition: propagator_simple.cpp:60
Definition: jpeg_rw.cpp:259
#define TRUE
Definition: jpleph.cpp:68
boolean jpeg_start_decompress(j_decompress_ptr cinfo)
Definition: jdapistd.cpp:38
JSAMPROW * JSAMPARRAY
Definition: jpeglib.h:72
JDIMENSION jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines)
Definition: jdapistd.cpp:152
Definition: jpeglib.h:416
static void my_error_exit(j_common_ptr cinfo)
Definition: jpeg_rw.cpp:272
boolean jpeg_finish_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:369
int read_JPEG_file ( const char *  filename,
vector< vector< uint8_t > > &  image_buffer 
)
422 {
423  FILE *infile;
424 /* This struct contains the JPEG decompression parameters and pointers to
425  * working space (which is allocated as needed by the JPEG library).
426  */
427  struct jpeg_decompress_struct cinfo;
428  /* We use our private extension JPEG error handler.
429  * Note that this struct must live as long as the main JPEG parameter
430  * struct, to avoid dangling-pointer problems.
431  */
432  struct my_error_mgr jerr;
433  /* More stuff */
434  JSAMPARRAY buffer; /* Output row buffer */
435  int row_stride; /* physical row width in output buffer */
436 
437  /* In this example we want to open the input file before doing anything else,
438  * so that the setjmp() error recovery below can assume the file is open.
439  * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
440  * requires it in order to read binary files.
441  */
442 
443  if ((infile = fopen(filename, "rb")) == NULL) {
444  fprintf(stderr, "can't open %s\n", filename);
445  return 0;
446  }
447 
448  /* Step 1: allocate and initialize JPEG decompression object */
449 
450  /* We set up the normal JPEG error routines, then override error_exit. */
451  cinfo.err = jpeg_std_error(&jerr.pub);
452  jerr.pub.error_exit = my_error_exit;
453  /* Establish the setjmp return context for my_error_exit to use. */
454  if (setjmp(jerr.setjmp_buffer))
455  {
456  /* If we get here, the JPEG code has signaled an error.
457  * We need to clean up the JPEG object, close the input file, and return.
458  */
459  jpeg_destroy_decompress(&cinfo);
460  fclose(infile);
461  return 0;
462  }
463  /* Now we can initialize the JPEG decompression object. */
464  jpeg_create_decompress(&cinfo);
465 
466  /* Step 2: specify data source (eg, a file) */
467 
468  jpeg_stdio_src(&cinfo, infile);
469 
470  /* Step 3: read file parameters with jpeg_read_header() */
471 
472  (void) jpeg_read_header(&cinfo, TRUE);
473  /* We can ignore the return value from jpeg_read_header since
474  * (a) suspension is not possible with the stdio data source, and
475  * (b) we passed TRUE to reject a tables-only JPEG file as an error.
476  * See libjpeg.doc for more info.
477  */
478 
479  /* Step 4: set parameters for decompression */
480 
481  /* In this example, we don't need to change any of the defaults set by
482  * jpeg_read_header(), so we do nothing here.
483  */
484 
485  /* Step 5: Start decompressor */
486 
487  (void) jpeg_start_decompress(&cinfo);
488  /* We can ignore the return value since suspension is not possible
489  * with the stdio data source.
490  */
491 
492  /* We may need to do some setup of our own at this point before reading
493  * the data. After jpeg_start_decompress() we have the correct scaled
494  * output image dimensions available, as well as the output colormap
495  * if we asked for color quantization.
496  * In this example, we need to make an output work buffer of the right size.
497  */
498  /* JSAMPLEs per row in output buffer */
499  row_stride = cinfo.output_width * cinfo.output_components;
500  /* Make a one-row-high sample array that will go away when done with image */
501  buffer = (*cinfo.mem->alloc_sarray)
502  ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
503 
504  /* Step 6: while (scan lines remain to be read) */
505  /* jpeg_read_scanlines(...); */
506 
507  /* Here we use the library's state variable cinfo.output_scanline as the
508  * loop counter, so that we don't have to keep track ourselves.
509  */
510 // while (cinfo.output_scanline < cinfo.output_height)
511  image_buffer.resize(cinfo.output_height);
512  for (size_t ih=0; ih<cinfo.output_height; ++ih)
513  {
514  /* jpeg_read_scanlines expects an array of pointers to scanlines.
515  * Here the array is only one element long, but you could ask for
516  * more than one scanline at a time if that's more convenient.
517  */
518  (void) jpeg_read_scanlines(&cinfo, buffer, 1);
519  /* Assume put_scanline_someplace wants a pointer and sample count. */
520  //put_scanline_someplace(buffer[0], row_stride);
521  image_buffer[ih].resize(cinfo.output_width * cinfo.output_components);
522  memcpy((void *)image_buffer[ih].data(), (const void *)buffer[0], row_stride);
523  }
524 
525  /* Step 7: Finish decompression */
526 
527  (void) jpeg_finish_decompress(&cinfo);
528  /* We can ignore the return value since suspension is not possible
529  * with the stdio data source.
530  */
531 
532  /* Step 8: Release JPEG decompression object */
533 
534  /* This is an important step since it will release a good deal of memory. */
535  jpeg_destroy_decompress(&cinfo);
536 
537  /* After finish_decompress, we can close the input file.
538  * Here we postpone it until after no more JPEG errors are possible,
539  * so as to simplify the setjmp error logic above. (Actually, I don't
540  * think that jpeg_destroy can do an error exit, but why assume anything...)
541  */
542  fclose(infile);
543 
544  /* At this point you may want to check to see whether any corrupt-data
545  * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
546  */
547 
548  /* And we're done! */
549  return 1;
550 }
int jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
Definition: jdapimin.cpp:241
void jpeg_stdio_src(j_decompress_ptr cinfo, FILE *infile)
Definition: jdatasrc.cpp:182
void jpeg_destroy_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:91
struct jpeg_error_mgr * jpeg_std_error(struct jpeg_error_mgr *err)
Definition: jerror.cpp:232
Definition: jpeglib.h:258
#define jpeg_create_decompress(cinfo)
Definition: jpeglib.h:902
#define JPOOL_IMAGE
Definition: jpeglib.h:754
static char buffer[255]
Definition: propagator_simple.cpp:60
Definition: jpeg_rw.cpp:259
#define TRUE
Definition: jpleph.cpp:68
boolean jpeg_start_decompress(j_decompress_ptr cinfo)
Definition: jdapistd.cpp:38
JSAMPROW * JSAMPARRAY
Definition: jpeglib.h:72
JDIMENSION jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines)
Definition: jdapistd.cpp:152
Definition: jpeglib.h:416
static void my_error_exit(j_common_ptr cinfo)
Definition: jpeg_rw.cpp:272
boolean jpeg_finish_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:369