MRPT  2.0.4
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-2020, 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)
297 term_source([[maybe_unused]] j_decompress_ptr cinfo)
298 {
299  /* no work necessary here */
300 }
301 
302 /*
303  * Prepare for input from a stdio stream.
304  * The caller must have already opened the stream, and is responsible
305  * for closing it after finishing decompression.
306  */
307 
308 GLOBAL(void)
310 {
311  my_src_ptr src;
312 
313  /* The source object and input buffer are made permanent so that a series
314  * of JPEG images can be read from the same file by calling jpeg_stdio_src
315  * only before the first one. (If we discarded the buffer at the end of
316  * one image, we'd likely lose the start of the next one.)
317  * This makes it unsafe to use this manager and a different source
318  * manager serially with the same JPEG object. Caveat programmer.
319  */
320  if (cinfo->src == nullptr)
321  { /* first time for this JPEG object? */
322  cinfo->src = (mrpt_jpeg_source_mgr*)(*cinfo->mem->alloc_small)(
323  (j_common_ptr)cinfo, JPOOL_PERMANENT, sizeof(my_source_mgr));
324  src = (my_src_ptr)cinfo->src;
325  src->buffer = (JOCTET*)(*cinfo->mem->alloc_small)(
327  INPUT_BUF_SIZE * sizeof(JOCTET));
328  }
329 
330  src = (my_src_ptr)cinfo->src;
331  src->pub.init_source = init_source;
332  src->pub.fill_input_buffer = fill_input_buffer;
333  src->pub.skip_input_data = skip_input_data;
334  src->pub.resync_to_restart =
335  jpeg_resync_to_restart; /* use default method */
336  src->pub.term_source = term_source;
337  src->in = in;
338  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
339  src->pub.next_input_byte = nullptr; /* until buffer loaded */
340 }
341 
342 // ---------------------------------------------------------------------------------------
343 // END OF JPEG FUNCTIONS PART
344 // ---------------------------------------------------------------------------------------
345 
346 void CImage::saveToStreamAsJPEG(CStream& out, const int jpeg_quality) const
347 {
348 #if MRPT_HAS_OPENCV
349  MRPT_START
350 
351  makeSureImageIsLoaded(); // For delayed loaded images stored externally
352 
353  MRPT_TODO("Port to cv::encode()");
354 
355  struct jpeg_compress_struct cinfo;
356  struct jpeg_error_mgr jerr;
357 
358  const auto& img = m_impl->img;
359 
360  const unsigned int nCols = img.cols, nRows = img.rows;
361  const bool is_color = (img.channels() == 3);
362 
363  // Some previous verification:
364  ASSERT_(nCols >= 1 && nRows >= 1);
365  ASSERT_(img.channels() == 1 || img.channels() == 3);
366 
367  // 1) Initialization of the JPEG compresion object:
368  // --------------------------------------------------
369  cinfo.err = jpeg_std_error(&jerr);
370  jpeg_create_compress(&cinfo);
371 
372  // 2) Set the destination of jpeg data:
373  // --------------------------------------------------
374  jpeg_stdio_dest(&cinfo, &out);
375 
376  // 3) Set parameters for compression:
377  // --------------------------------------------------
378  cinfo.image_width = nCols;
379  cinfo.image_height = nRows;
380  cinfo.input_components = is_color ? 3 : 1;
381  cinfo.in_color_space = is_color ? JCS_RGB : JCS_GRAYSCALE;
382 
383  jpeg_set_defaults(&cinfo);
384  /* Make optional parameter settings here */
385  /* Now you can set any non-default parameters you wish to.
386  * Here we just illustrate the use of quality (quantization table) scaling:
387  */
388  jpeg_set_quality(
389  &cinfo, jpeg_quality /* quality per cent */,
390  TRUE /* limit to baseline-JPEG values */);
391 
392  // 4) Start:
393  // --------------------------------------------------
394  jpeg_start_compress(&cinfo, TRUE);
395 
396  // 5) Write scan lines:
397  // --------------------------------------------------
398  if (is_color)
399  {
400  JSAMPROW row_pointer[1]; /* pointer to a single row */
401  row_pointer[0] = new uint8_t[img.step[0]];
402 
403  for (unsigned int row = 0; row < nRows; row++)
404  {
405  // Flip RGB bytes order!
406  const uint8_t* src = img.ptr<uint8_t>(row);
407  uint8_t* target = row_pointer[0];
408  for (unsigned int col = 0; col < nCols; col++)
409  {
410  target[0] = src[2];
411  target[1] = src[1];
412  target[2] = src[0];
413 
414  target += 3;
415  src += 3;
416  }
417 
418  if (1 != jpeg_write_scanlines(&cinfo, row_pointer, 1))
419  {
420  THROW_EXCEPTION("jpeg_write_scanlines: didn't work!!");
421  }
422  }
423 
424  delete[] row_pointer[0];
425  } // end "color"
426  else
427  { // Is grayscale:
428  JSAMPROW row_pointer[1]; /* pointer to a single row */
429 
430  for (unsigned int row = 0; row < nRows; row++)
431  {
432  row_pointer[0] = const_cast<JSAMPROW>(img.ptr<uint8_t>(row));
433 
434  // Gray scale:
435  if (1 != jpeg_write_scanlines(&cinfo, row_pointer, 1))
436  {
437  THROW_EXCEPTION("jpeg_write_scanlines: didn't work!!");
438  }
439  }
440  }
441 
442  // 6) Compress and finish:
443  // --------------------------------------------------
444  jpeg_finish_compress(&cinfo);
445  jpeg_destroy_compress(&cinfo);
446 
447  // DONE!
448  MRPT_END
449 #endif
450 }
451 
453 {
454 #if MRPT_HAS_OPENCV
455  MRPT_START
456 
457  MRPT_TODO("Port to cv::imdecode()");
458 
459  struct jpeg_decompress_struct cinfo;
460  struct jpeg_error_mgr jerr;
461 
462  /* Step 1: allocate and initialize JPEG decompression object */
463 
464  /* We set up the normal JPEG error routines, then override error_exit. */
465  cinfo.err = jpeg_std_error(&jerr);
466 
467  /* Now we can initialize the JPEG decompression object. */
468  jpeg_create_decompress(&cinfo);
469 
470  /* Step 2: specify data source (eg, a file) */
471  jpeg_stdio_src(&cinfo, &in);
472 
473  /* Step 3: read file parameters with jpeg_read_header() */
474  jpeg_read_header(&cinfo, TRUE);
475 
476  /* Step 4: set parameters for decompression */
477 
478  /* Step 5: Start decompressor */
479  jpeg_start_decompress(&cinfo);
480 
481  /* We may need to do some setup of our own at this point before reading
482  * the data. After jpeg_start_decompress() we have the correct scaled
483  * output image dimensions available, as well as the output colormap
484  * if we asked for color quantization.
485  * In this example, we need to make an output work buffer of the right size.
486  */
487  /* JSAMPLEs per row in output buffer */
488  /* physical row width in output buffer */
489  const int row_stride = cinfo.output_width * cinfo.output_components;
490  /* Make a one-row-high sample array that will go away when done with image
491  */
492  /* Output row buffer */
493  JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)(
494  (j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1);
495 
496  // Resize the CImage now:
497  this->resize(
498  cinfo.output_width, cinfo.output_height,
499  cinfo.out_color_components == 1 ? CH_GRAY : CH_RGB);
500  auto& img = m_impl->img;
501 
502  /* Step 6: while (scan lines remain to be read) */
503  /* jpeg_read_scanlines(...); */
504 
505  /* Here we use the library's state variable cinfo.output_scanline as the
506  * loop counter, so that we don't have to keep track ourselves.
507  */
508  const unsigned int nCols = cinfo.output_width, nRows = cinfo.output_height;
509 
510  for (unsigned int row = 0; row < nRows; row++)
511  {
512  /* jpeg_read_scanlines expects an array of pointers to scanlines.
513  * Here the array is only one element long, but you could ask for
514  * more than one scanline at a time if that's more convenient.
515  */
516  jpeg_read_scanlines(&cinfo, buffer, 1);
517 
518  /* Copy into the CImage object */
519  if (isColor())
520  {
521  // Flip RGB bytes order!
522  auto target = img.ptr<uint8_t>(row);
523  const auto* src = buffer[0];
524  for (unsigned int col = 0; col < nCols; col++)
525  {
526  target[0] = src[2];
527  target[1] = src[1];
528  target[2] = src[0];
529 
530  target += 3;
531  src += 3;
532  }
533  }
534  else
535  {
536  // Gray scale:
537  std::memcpy(img.ptr<uint8_t>(row), buffer[0], row_stride);
538  }
539  }
540 
541  /* Step 7: Finish decompression */
542 
543  jpeg_finish_decompress(&cinfo);
544  /* We can ignore the return value since suspension is not possible
545  * with the stdio data source.
546  */
547 
548  /* Step 8: Release JPEG decompression object */
549 
550  /* This is an important step since it will release a good deal of memory. */
551  jpeg_destroy_decompress(&cinfo);
552 
553  // DONE!
554  MRPT_END
555 #endif
556 }
term_source([[maybe_unused]] j_decompress_ptr cinfo)
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
#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:1591
JSAMPLE FAR * JSAMPROW
Definition: mrpt_jpeglib.h:59
mrpt::io::CStream CStream
Definition: utils/CStream.h:5
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:1027
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)
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:250
struct jpeg_destination_mgr pub
#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.
bool isColor() const
Returns true if the image is RGB, false if it is grayscale.
Definition: CImage.cpp:865
mrpt::vision::TStereoCalibResults out
#define MRPT_END
Definition: exceptions.h:245
#define INPUT_BUF_SIZE
mrpt_destination_mgr * mrpt_dest_ptr
MRPT_TODO("toPointCloud / calibration")
#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".
skip_input_data(j_decompress_ptr cinfo, long num_bytes)
JDIMENSION image_width
Definition: mrpt_jpeglib.h:278



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020