jpeg_conversion
jpeg_conversion.hpp
Go to the documentation of this file.
1 /*
2  * \file jpeg_conversion.hpp
3  *
4  * \brief Jpeg-frame conversion using the IJG library.
5  *
6  * \details Converts MODE_JPEG to MODE_GRAYSCALE, MODE_RGB or MODE_BGR and vice versa.
7  *
8  * German Research Center for Artificial Intelligence\n
9  * Project: Rimres
10  *
11  * \date 07.12.11
12  *
13  * \author Stefan.Haase@dfki.de
14  */
15 
16 #ifndef _JPEG_CONVERSION_H_
17 #define _JPEG_CONVERSION_H_
18 
19 #include <iostream>
20 #include <exception>
21 #include <stdexcept>
22 #include <string>
23 
24 #include "jpeglib.h"
25 
26 #include <base/samples/Frame.hpp>
27 
28 #include <base-logging/Logging.hpp>
29 
30 namespace conversion {
31 
38 
39  public: // STATICs
40  const static int JPEG_QUALITY = 80;
41 
42  public:
43  JpegConversion() : mBuffer(NULL), mBufferSize(0), mFlipBuffer(NULL), mFlipBufferSize(0),
44  mJpegQuality(JPEG_QUALITY) {
45  }
46 
47  JpegConversion(unsigned int jpeg_quality) : mBuffer(NULL), mBufferSize(0),
48  mFlipBuffer(NULL), mFlipBufferSize(0), mJpegQuality(jpeg_quality){
49  if(mJpegQuality > 100)
50  mJpegQuality = 100;
51  }
52 
54  if(mBuffer != NULL) {
55  delete mBuffer;
56  mBuffer = NULL;
57  }
58  }
59 
69  void compress(base::samples::frame::Frame const& frame_input, base::samples::frame::Frame& frame_output);
70 
82  void decompress(base::samples::frame::Frame const& frame_input,
83  base::samples::frame::frame_mode_t const decompress_to,
84  base::samples::frame::Frame& frame_output);
85 
96  void decompress(uint8_t const* src,
97  size_t src_size,
98  int width,
99  int height,
100  base::samples::frame::frame_mode_t const decompress_to,
101  uint8_t* dst);
102 
110  J_COLOR_SPACE getJpegColorspace(base::samples::frame::frame_mode_t const frame_mode,
111  bool& flip_rgb, bool& is_jpeg);
112 
123  bool storeFrame(std::string filename, base::samples::frame::Frame const& frame,
124  std::string* used_filename=NULL);
125 
129  bool loadJpeg(std::string const& filename, uint32_t const width, uint32_t const height,
130  base::samples::frame::Frame& frame);
131 
132  private:
139  void switchRB(uint32_t const img_size, uint8_t const* input_img,
140  uint8_t* output_img);
141 
145  void switchRB(uint32_t const img_size, uint8_t* input_img);
146 
147  private: // REQUIRED jpeg methods.
148  // COMPRESS
152  static void init_buffer(jpeg_compress_struct* cinfo) {}
153 
158  static boolean empty_buffer(jpeg_compress_struct* cinfo) {
159  return TRUE;
160  }
161 
165  static void term_buffer(jpeg_compress_struct* cinfo) {}
166 
167  // DECOMPRESS
168  static void my_init_source(jpeg_decompress_struct* cinfo) {
169  LOG_DEBUG("jpeg my_init_source()");
170  }
171  static boolean my_fill_input_buffer(jpeg_decompress_struct* cinfo) {
172  LOG_DEBUG("jpeg my_fill_input_buffer");
173  return true;
174  }
175  static void my_skip_input_data(jpeg_decompress_struct* cinfo, long num_bytes) {
176  LOG_DEBUG("jpeg my_skip_input_data");
177  }
178  static boolean my_resync_to_restart(jpeg_decompress_struct* cinfo, int desired) {
179  LOG_DEBUG("jpeg my_resync_to_restart");
180  return true;
181  }
182  static void my_term_source(jpeg_decompress_struct* cinfo) {
183  LOG_DEBUG("jpeg my_term_source");
184  }
185 
186  private:
187  unsigned char* mBuffer;
188  unsigned int mBufferSize;
189 
190  unsigned char* mFlipBuffer; // Used to convert BGR to RGB.
191  unsigned int mFlipBufferSize;
192 
193  int mJpegQuality;
194 };
195 } // end namespace conversion
196 #endif
Definition: jpeg_conversion.hpp:37
void decompress(base::samples::frame::Frame const &frame_input, base::samples::frame::frame_mode_t const decompress_to, base::samples::frame::Frame &frame_output)
Definition: jpeg_conversion.cpp:135
JpegConversion(unsigned int jpeg_quality)
Definition: jpeg_conversion.hpp:47
static const int JPEG_QUALITY
Definition: jpeg_conversion.hpp:40
bool storeFrame(std::string filename, base::samples::frame::Frame const &frame, std::string *used_filename=NULL)
Definition: jpeg_conversion.cpp:323
JpegConversion()
Definition: jpeg_conversion.hpp:43
void compress(base::samples::frame::Frame const &frame_input, base::samples::frame::Frame &frame_output)
Definition: jpeg_conversion.cpp:6
Definition: jpeg_conversion.cpp:4
J_COLOR_SPACE getJpegColorspace(base::samples::frame::frame_mode_t const frame_mode, bool &flip_rgb, bool &is_jpeg)
Definition: jpeg_conversion.cpp:294
~JpegConversion()
Definition: jpeg_conversion.hpp:53
bool loadJpeg(std::string const &filename, uint32_t const width, uint32_t const height, base::samples::frame::Frame &frame)
Definition: jpeg_conversion.cpp:396