17 #include <mrpt/3rdparty/do_opencv_includes.h>
34 #define mrpt_jpeg_source_mgr jpeg_source_mgr
48 #define OUTPUT_BUF_SIZE 4096
62 dest->
buffer =
reinterpret_cast<JOCTET*
>(((*cinfo->mem->alloc_small)(
66 dest->pub.next_output_byte = dest->buffer;
100 dest->pub.next_output_byte = dest->buffer;
122 if (datacount > 0) dest->out->Write(dest->buffer, (
int)datacount);
136 if (cinfo->dest ==
nullptr)
163 #define INPUT_BUF_SIZE 4096
179 src->start_of_file = TRUE;
225 if (src->start_of_file)
231 src->buffer[0] = (JOCTET)0xFF;
236 src->pub.next_input_byte = src->buffer;
237 src->pub.bytes_in_buffer = nbytes;
238 src->start_of_file = FALSE;
266 while (num_bytes > (
long)src->pub.bytes_in_buffer)
268 num_bytes -= (long)src->pub.bytes_in_buffer;
274 src->pub.next_input_byte += (size_t)num_bytes;
275 src->pub.bytes_in_buffer -= (size_t)num_bytes;
320 if (cinfo->src ==
nullptr)
325 src->
buffer = (JOCTET*)(*cinfo->mem->alloc_small)(
334 src->
pub.resync_to_restart =
335 jpeg_resync_to_restart;
338 src->
pub.bytes_in_buffer = 0;
339 src->
pub.next_input_byte =
nullptr;
358 const auto& img =
m_impl->img;
360 const unsigned int nCols = img.cols, nRows = img.rows;
361 const bool is_color = (img.channels() == 3);
364 ASSERT_(nCols >= 1 && nRows >= 1);
365 ASSERT_(img.channels() == 1 || img.channels() == 3);
369 cinfo.err = jpeg_std_error(&jerr);
383 jpeg_set_defaults(&cinfo);
389 &cinfo, jpeg_quality ,
394 jpeg_start_compress(&cinfo, TRUE);
401 row_pointer[0] =
new uint8_t[img.step[0]];
403 for (
unsigned int row = 0; row < nRows; row++)
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++)
418 if (1 != jpeg_write_scanlines(&cinfo, row_pointer, 1))
424 delete[] row_pointer[0];
430 for (
unsigned int row = 0; row < nRows; row++)
432 row_pointer[0] =
const_cast<JSAMPROW>(img.ptr<uint8_t>(row));
435 if (1 != jpeg_write_scanlines(&cinfo, row_pointer, 1))
444 jpeg_finish_compress(&cinfo);
445 jpeg_destroy_compress(&cinfo);
465 cinfo.err = jpeg_std_error(&jerr);
474 jpeg_read_header(&cinfo, TRUE);
479 jpeg_start_decompress(&cinfo);
493 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)(
510 for (
unsigned int row = 0; row < nRows; row++)
516 jpeg_read_scanlines(&cinfo, buffer, 1);
522 auto target = img.ptr<uint8_t>(row);
523 const auto* src = buffer[0];
524 for (
unsigned int col = 0; col < nCols; col++)
537 std::memcpy(img.ptr<uint8_t>(row), buffer[0], row_stride);
543 jpeg_finish_decompress(&cinfo);
551 jpeg_destroy_decompress(&cinfo);