MRPT  1.9.9
CImage_JPEG_streams.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "img-precomp.h" // Precompiled headers
11 
12 #include <mrpt/img/CImage.h>
13 #include <mrpt/io/CStream.h>
14 #include "CImage_impl.h"
15 
16 // Universal include for all versions of OpenCV
17 #include <mrpt/3rdparty/do_opencv_includes.h>
18 
19 using namespace mrpt;
20 using namespace mrpt::img;
21 
22 // ---------------------------------------------------------------------------------------
23 // START OF JPEG FUNCTIONS PART
24 // ---------------------------------------------------------------------------------------
25 /* Expanded data destination object for stdio output */
26 
27 //#undef INT32
28 #undef FAR
29 #define XMD_H
30 
31 #include <cstdio>
32 
33 #include <jpeglib.h>
34 #define mrpt_jpeg_source_mgr jpeg_source_mgr
35 
36 using mrpt::io::CStream;
37 
38 typedef struct
39 {
40  struct jpeg_destination_mgr pub; /* public fields */
41 
42  CStream* out; /* target stream */
43  JOCTET* buffer; /* start of buffer */
45 
47 
48 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
49 
50 /*
51  * Initialize destination --- called by jpeg_start_compress
52  * before any data is actually written.
53  */
54 
55 METHODDEF(void)
57 {
58  auto dest = reinterpret_cast<mrpt_dest_ptr>(cinfo->dest);
59 
60  /* Allocate the output buffer --- it will be released when done with image
61  */
62  dest->buffer = reinterpret_cast<JOCTET*>(((*cinfo->mem->alloc_small)(
63  reinterpret_cast<j_common_ptr>(cinfo), JPOOL_IMAGE,
64  OUTPUT_BUF_SIZE * sizeof(JOCTET))));
65 
66  dest->pub.next_output_byte = dest->buffer;
67  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
68 }
69 
70 /*
71  * Empty the output buffer --- called whenever buffer fills up.
72  *
73  * In typical applications, this should write the entire output buffer
74  * (ignoring the current state of next_output_byte & free_in_buffer),
75  * reset the pointer & count to the start of the buffer, and return TRUE
76  * indicating that the buffer has been dumped.
77  *
78  * In applications that need to be able to suspend compression due to output
79  * overrun, a FALSE return indicates that the buffer cannot be emptied now.
80  * In this situation, the compressor will return to its caller (possibly with
81  * an indication that it has not accepted all the supplied scanlines). The
82  * application should resume compression after it has made more room in the
83  * output buffer. Note that there are substantial restrictions on the use of
84  * suspension --- see the documentation.
85  *
86  * When suspending, the compressor will back up to a convenient restart point
87  * (typically the start of the current MCU). next_output_byte & free_in_buffer
88  * indicate where the restart point will be if the current call returns FALSE.
89  * Data beyond this point will be regenerated after resumption, so do not
90  * write it out when emptying the buffer externally.
91  */
92 
93 METHODDEF(boolean)
95 {
96  auto dest = (mrpt_dest_ptr)cinfo->dest;
97 
98  dest->out->Write(dest->buffer, OUTPUT_BUF_SIZE);
99 
100  dest->pub.next_output_byte = dest->buffer;
101  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
102 
103  return TRUE;
104 }
105 
106 /*
107  * Terminate destination --- called by jpeg_finish_compress
108  * after all data has been written. Usually needs to flush buffer.
109  *
110  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
111  * application must deal with any cleanup that should happen even
112  * for error exit.
113  */
114 
115 METHODDEF(void)
117 {
118  auto dest = (mrpt_dest_ptr)cinfo->dest;
119  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
120 
121  /* Write any data remaining in the buffer */
122  if (datacount > 0) dest->out->Write(dest->buffer, (int)datacount);
123 }
124 
125 GLOBAL(void)
127 {
128  mrpt_dest_ptr dest;
129 
130  /* The destination object is made permanent so that multiple JPEG images
131  * can be written to the same file without re-executing jpeg_stdio_dest.
132  * This makes it dangerous to use this manager and a different destination
133  * manager serially with the same JPEG object, because their private object
134  * sizes may be different. Caveat programmer.
135  */
136  if (cinfo->dest == nullptr)
137  { /* first time for this JPEG object? */
138  cinfo->dest = (jpeg_destination_mgr*)(*cinfo->mem->alloc_small)(
140  }
141 
142  dest = (mrpt_dest_ptr)cinfo->dest;
143  dest->pub.init_destination = init_destination;
144  dest->pub.empty_output_buffer = empty_output_buffer;
145  dest->pub.term_destination = term_destination;
146  dest->out = out;
147 }
148 
149 // -------------------------------------------------------------
150 
151 /* Expanded data source object for stdio input */
152 
153 typedef struct
154 {
155  mrpt_jpeg_source_mgr pub; /* public fields */
156  CStream* in; /* source stream */
157  JOCTET* buffer; /* start of buffer */
158  boolean start_of_file; /* have we gotten any data yet? */
159 } my_source_mgr;
160 
162 
163 #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
164 
165 /*
166  * Initialize source --- called by jpeg_read_header
167  * before any data is actually read.
168  */
169 
170 METHODDEF(void)
172 {
173  auto src = (my_src_ptr)cinfo->src;
174 
175  /* We reset the empty-input-file flag for each image,
176  * but we don't clear the input buffer.
177  * This is correct behavior for reading a series of images from one source.
178  */
179  src->start_of_file = TRUE;
180 }
181 
182 /*
183  * Fill the input buffer --- called whenever buffer is emptied.
184  *
185  * In typical applications, this should read fresh data into the buffer
186  * (ignoring the current state of next_input_byte & bytes_in_buffer),
187  * reset the pointer & count to the start of the buffer, and return TRUE
188  * indicating that the buffer has been reloaded. It is not necessary to
189  * fill the buffer entirely, only to obtain at least one more byte.
190  *
191  * There is no such thing as an EOF return. If the end of the file has been
192  * reached, the routine has a choice of ERREXIT() or inserting fake data into
193  * the buffer. In most cases, generating a warning message and inserting a
194  * fake EOI marker is the best course of action --- this will allow the
195  * decompressor to output however much of the image is there. However,
196  * the resulting error message is misleading if the real problem is an empty
197  * input file, so we handle that case specially.
198  *
199  * In applications that need to be able to suspend compression due to input
200  * not being available yet, a FALSE return indicates that no more data can be
201  * obtained right now, but more may be forthcoming later. In this situation,
202  * the decompressor will return to its caller (with an indication of the
203  * number of scanlines it has read, if any). The application should resume
204  * decompression after it has loaded more data into the input buffer. Note
205  * that there are substantial restrictions on the use of suspension --- see
206  * the documentation.
207  *
208  * When suspending, the decompressor will back up to a convenient restart point
209  * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
210  * indicate where the restart point will be if the current call returns FALSE.
211  * Data beyond this point must be rescanned after resumption, so move it to
212  * the front of the buffer rather than discarding it.
213  */
214 
215 METHODDEF(boolean)
217 {
218  auto src = (my_src_ptr)cinfo->src;
219  size_t nbytes;
220 
221  nbytes = src->in->Read(src->buffer, INPUT_BUF_SIZE);
222 
223  if (nbytes <= 0)
224  {
225  if (src->start_of_file) /* Treat empty input file as fatal error */
226  {
227  THROW_EXCEPTION("Error looking for JPEG start data!");
228  }
229 
230  /* Insert a fake EOI marker */
231  src->buffer[0] = (JOCTET)0xFF;
232  src->buffer[1] = (JOCTET)JPEG_EOI;
233  nbytes = 2;
234  }
235 
236  src->pub.next_input_byte = src->buffer;
237  src->pub.bytes_in_buffer = nbytes;
238  src->start_of_file = FALSE;
239 
240  return TRUE;
241 }
242 
243 /*
244  * Skip data --- used to skip over a potentially large amount of
245  * uninteresting data (such as an APPn marker).
246  *
247  * Writers of suspendable-input applications must note that skip_input_data
248  * is not granted the right to give a suspension return. If the skip extends
249  * beyond the data currently in the buffer, the buffer can be marked empty so
250  * that the next read will cause a fill_input_buffer call that can suspend.
251  * Arranging for additional bytes to be discarded before reloading the input
252  * buffer is the application writer's problem.
253  */
254 
255 METHODDEF(void)
256 skip_input_data(j_decompress_ptr cinfo, long num_bytes)
257 {
258  auto src = (my_src_ptr)cinfo->src;
259 
260  /* Just a dumb implementation for now. Could use fseek() except
261  * it doesn't work on pipes. Not clear that being smart is worth
262  * any trouble anyway --- large skips are infrequent.
263  */
264  if (num_bytes > 0)
265  {
266  while (num_bytes > (long)src->pub.bytes_in_buffer)
267  {
268  num_bytes -= (long)src->pub.bytes_in_buffer;
269  (void)fill_input_buffer(cinfo);
270  /* note we assume that fill_input_buffer will never return FALSE,
271  * so suspension need not be handled.
272  */
273  }
274  src->pub.next_input_byte += (size_t)num_bytes;
275  src->pub.bytes_in_buffer -= (size_t)num_bytes;
276  }
277 }
278 
279 /*
280  * An additional method that can be provided by data source modules is the
281  * resync_to_restart method for error recovery in the presence of RST markers.
282  * For the moment, this source module just uses the default resync method
283  * provided by the JPEG library. That method assumes that no backtracking
284  * is possible.
285  */
286 
287 /*
288  * Terminate source --- called by jpeg_finish_decompress
289  * after all data has been read. Often a no-op.
290  *
291  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
292  * application must deal with any cleanup that should happen even
293  * for error exit.
294  */
295 
296 METHODDEF(void)
298 {
299  MRPT_UNUSED_PARAM(cinfo);
300  /* no work necessary here */
301 }
302 
303 /*
304  * Prepare for input from a stdio stream.
305  * The caller must have already opened the stream, and is responsible
306  * for closing it after finishing decompression.
307  */
308 
309 GLOBAL(void)
311 {
312  my_src_ptr src;
313 
314  /* The source object and input buffer are made permanent so that a series
315  * of JPEG images can be read from the same file by calling jpeg_stdio_src
316  * only before the first one. (If we discarded the buffer at the end of
317  * one image, we'd likely lose the start of the next one.)
318  * This makes it unsafe to use this manager and a different source
319  * manager serially with the same JPEG object. Caveat programmer.
320  */
321  if (cinfo->src == nullptr)
322  { /* first time for this JPEG object? */
323  cinfo->src = (mrpt_jpeg_source_mgr*)(*cinfo->mem->alloc_small)(
324  (j_common_ptr)cinfo, JPOOL_PERMANENT, sizeof(my_source_mgr));
325  src = (my_src_ptr)cinfo->src;
326  src->buffer = (JOCTET*)(*cinfo->mem->alloc_small)(
328  INPUT_BUF_SIZE * sizeof(JOCTET));
329  }
330 
331  src = (my_src_ptr)cinfo->src;
332  src->pub.init_source = init_source;
333  src->pub.fill_input_buffer = fill_input_buffer;
334  src->pub.skip_input_data = skip_input_data;
335  src->pub.resync_to_restart =
336  jpeg_resync_to_restart; /* use default method */
337  src->pub.term_source = term_source;
338  src->in = in;
339  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
340  src->pub.next_input_byte = nullptr; /* until buffer loaded */
341 }
342 
343 // ---------------------------------------------------------------------------------------
344 // END OF JPEG FUNCTIONS PART
345 // ---------------------------------------------------------------------------------------
346 
347 void CImage::saveToStreamAsJPEG(CStream& out, const int jpeg_quality) const
348 {
349 #if MRPT_HAS_OPENCV
350  MRPT_START
351 
352  makeSureImageIsLoaded(); // For delayed loaded images stored externally
353 
354  MRPT_TODO("Port to cv::encode()");
355 
356  struct jpeg_compress_struct cinfo;
357  struct jpeg_error_mgr jerr;
358 
359  const auto& img = m_impl->img;
360 
361  const unsigned int nCols = img.cols, nRows = img.rows;
362  const bool is_color = (img.channels() == 3);
363 
364  // Some previous verification:
365  ASSERT_(nCols >= 1 && nRows >= 1);
366  ASSERT_(img.channels() == 1 || img.channels() == 3);
367 
368  // 1) Initialization of the JPEG compresion object:
369  // --------------------------------------------------
370  cinfo.err = jpeg_std_error(&jerr);
371  jpeg_create_compress(&cinfo);
372 
373  // 2) Set the destination of jpeg data:
374  // --------------------------------------------------
375  jpeg_stdio_dest(&cinfo, &out);
376 
377  // 3) Set parameters for compression:
378  // --------------------------------------------------
379  cinfo.image_width = nCols;
380  cinfo.image_height = nRows;
381  cinfo.input_components = is_color ? 3 : 1;
382  cinfo.in_color_space = is_color ? JCS_RGB : JCS_GRAYSCALE;
383 
384  jpeg_set_defaults(&cinfo);
385  /* Make optional parameter settings here */
386  /* Now you can set any non-default parameters you wish to.
387  * Here we just illustrate the use of quality (quantization table) scaling:
388  */
389  jpeg_set_quality(
390  &cinfo, jpeg_quality /* quality per cent */,
391  TRUE /* limit to baseline-JPEG values */);
392 
393  // 4) Start:
394  // --------------------------------------------------
395  jpeg_start_compress(&cinfo, TRUE);
396 
397  // 5) Write scan lines:
398  // --------------------------------------------------
399  if (is_color)
400  {
401  JSAMPROW row_pointer[1]; /* pointer to a single row */
402  row_pointer[0] = new uint8_t[img.step[0]];
403 
404  for (unsigned int row = 0; row < nRows; row++)
405  {
406  // Flip RGB bytes order!
407  const uint8_t* src = img.ptr<uint8_t>(row);
408  uint8_t* target = row_pointer[0];
409  for (unsigned int col = 0; col < nCols; col++)
410  {
411  target[0] = src[2];
412  target[1] = src[1];
413  target[2] = src[0];
414 
415  target += 3;
416  src += 3;
417  }
418 
419  if (1 != jpeg_write_scanlines(&cinfo, row_pointer, 1))
420  {
421  THROW_EXCEPTION("jpeg_write_scanlines: didn't work!!");
422  }
423  }
424 
425  delete[] row_pointer[0];
426  } // end "color"
427  else
428  { // Is grayscale:
429  JSAMPROW row_pointer[1]; /* pointer to a single row */
430 
431  for (unsigned int row = 0; row < nRows; row++)
432  {
433  row_pointer[0] = const_cast<JSAMPROW>(img.ptr<uint8_t>(row));
434 
435  // Gray scale:
436  if (1 != jpeg_write_scanlines(&cinfo, row_pointer, 1))
437  {
438  THROW_EXCEPTION("jpeg_write_scanlines: didn't work!!");
439  }
440  }
441  }
442 
443  // 6) Compress and finish:
444  // --------------------------------------------------
445  jpeg_finish_compress(&cinfo);
446  jpeg_destroy_compress(&cinfo);
447 
448  // DONE!
449  MRPT_END
450 #endif
451 }
452 
454 {
455 #if MRPT_HAS_OPENCV
456  MRPT_START
457 
458  MRPT_TODO("Port to cv::imdecode()");
459 
460  struct jpeg_decompress_struct cinfo;
461  struct jpeg_error_mgr jerr;
462 
463  /* Step 1: allocate and initialize JPEG decompression object */
464 
465  /* We set up the normal JPEG error routines, then override error_exit. */
466  cinfo.err = jpeg_std_error(&jerr);
467 
468  /* Now we can initialize the JPEG decompression object. */
469  jpeg_create_decompress(&cinfo);
470 
471  /* Step 2: specify data source (eg, a file) */
472  jpeg_stdio_src(&cinfo, &in);
473 
474  /* Step 3: read file parameters with jpeg_read_header() */
475  jpeg_read_header(&cinfo, TRUE);
476 
477  /* Step 4: set parameters for decompression */
478 
479  /* Step 5: Start decompressor */
480  jpeg_start_decompress(&cinfo);
481 
482  /* We may need to do some setup of our own at this point before reading
483  * the data. After jpeg_start_decompress() we have the correct scaled
484  * output image dimensions available, as well as the output colormap
485  * if we asked for color quantization.
486  * In this example, we need to make an output work buffer of the right size.
487  */
488  /* JSAMPLEs per row in output buffer */
489  /* physical row width in output buffer */
490  const int row_stride = cinfo.output_width * cinfo.output_components;
491  /* Make a one-row-high sample array that will go away when done with image
492  */
493  /* Output row buffer */
494  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)(
495  (j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1);
496 
497  // Resize the CImage now:
498  this->resize(
499  cinfo.output_width, cinfo.output_height,
500  cinfo.out_color_components == 1 ? CH_GRAY : CH_RGB);
501  auto& img = m_impl->img;
502 
503  /* Step 6: while (scan lines remain to be read) */
504  /* jpeg_read_scanlines(...); */
505 
506  /* Here we use the library's state variable cinfo.output_scanline as the
507  * loop counter, so that we don't have to keep track ourselves.
508  */
509  const unsigned int nCols = cinfo.output_width, nRows = cinfo.output_height;
510 
511  for (unsigned int row = 0; row < nRows; row++)
512  {
513  /* jpeg_read_scanlines expects an array of pointers to scanlines.
514  * Here the array is only one element long, but you could ask for
515  * more than one scanline at a time if that's more convenient.
516  */
517  jpeg_read_scanlines(&cinfo, buffer, 1);
518 
519  /* Copy into the CImage object */
520  if (isColor())
521  {
522  // Flip RGB bytes order!
523  auto target = img.ptr<uint8_t>(row);
524  const auto* src = buffer[0];
525  for (unsigned int col = 0; col < nCols; col++)
526  {
527  target[0] = src[2];
528  target[1] = src[1];
529  target[2] = src[0];
530 
531  target += 3;
532  src += 3;
533  }
534  }
535  else
536  {
537  // Gray scale:
538  std::memcpy(img.ptr<uint8_t>(row), buffer[0], row_stride);
539  }
540  }
541 
542  /* Step 7: Finish decompression */
543 
544  jpeg_finish_decompress(&cinfo);
545  /* We can ignore the return value since suspension is not possible
546  * with the stdio data source.
547  */
548 
549  /* Step 8: Release JPEG decompression object */
550 
551  /* This is an important step since it will release a good deal of memory. */
552  jpeg_destroy_decompress(&cinfo);
553 
554  // DONE!
555  MRPT_END
556 #endif
557 }
#define FALSE
my_source_mgr * my_src_ptr
init_destination(j_compress_ptr cinfo)
#define MRPT_START
Definition: exceptions.h:241
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
GLuint buffer
Definition: glext.h:3928
#define JPOOL_PERMANENT
Definition: mrpt_jpeglib.h:751
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:260
JDIMENSION image_height
Definition: mrpt_jpeglib.h:279
void makeSureImageIsLoaded() const
Checks if the image is of type "external storage", and if so and not loaded yet, load it...
Definition: CImage.cpp:1535
JSAMPLE FAR * JSAMPROW
Definition: mrpt_jpeglib.h:59
mrpt::io::CStream CStream
Definition: utils/CStream.h:5
GLuint src
Definition: glext.h:7397
init_source(j_decompress_ptr cinfo)
fill_input_buffer(j_decompress_ptr cinfo)
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:28
mrpt::pimpl< Impl > m_impl
Definition: img/CImage.h:1015
mrpt_jpeg_source_mgr pub
J_COLOR_SPACE in_color_space
Definition: mrpt_jpeglib.h:281
#define OUTPUT_BUF_SIZE
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
jpeg_stdio_src(j_decompress_ptr cinfo, CStream *in)
#define TRUE
void resize(std::size_t width, std::size_t height, TImageChannels nChannels, PixelDepth depth=PixelDepth::D8U)
Changes the size of the image, erasing previous contents (does NOT scale its current content...
Definition: CImage.cpp:254
term_source(j_decompress_ptr cinfo)
struct jpeg_destination_mgr pub
GLint GLvoid * img
Definition: glext.h:3769
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:752
void saveToStreamAsJPEG(mrpt::io::CStream &out, const int jpeg_quality=95) const
Save image to binary stream as a JPEG (.jpg) compressed format.
JSAMPROW * JSAMPARRAY
Definition: mrpt_jpeglib.h:60
term_destination(j_compress_ptr cinfo)
#define JPEG_EOI
void loadFromStreamAsJPEG(mrpt::io::CStream &in)
Reads the image from a binary stream containing a binary jpeg file.
#define jpeg_create_decompress(cinfo)
Definition: mrpt_jpeglib.h:895
#define mrpt_jpeg_source_mgr
jpeg_stdio_dest(j_compress_ptr cinfo, CStream *out)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MRPT_TODO(x)
Definition: common.h:129
bool isColor() const
Returns true if the image is RGB, false if it is grayscale.
Definition: CImage.cpp:888
mrpt::vision::TStereoCalibResults out
GLenum GLenum GLvoid * row
Definition: glext.h:3580
#define MRPT_END
Definition: exceptions.h:245
GLuint in
Definition: glext.h:7391
#define INPUT_BUF_SIZE
mrpt_destination_mgr * mrpt_dest_ptr
#define jpeg_create_compress(cinfo)
Definition: mrpt_jpeglib.h:891
empty_output_buffer(j_compress_ptr cinfo)
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:358
skip_input_data(j_decompress_ptr cinfo, long num_bytes)
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
JDIMENSION image_width
Definition: mrpt_jpeglib.h:278



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 4363012a5 Tue Nov 19 10:55:26 2019 +0100 at mar nov 19 11:00:13 CET 2019