Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CompressJpeg.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 #ifndef __JPEGHANDLER_HH__
25 #define __JPEGHANDLER_HH__
26 
27 // Include I/O handling and streams
28 #include <stdio.h>
29 #include <sstream>
30 #include <string.h>
31 
32 // Include BIAS image handling
33 #include <Base/Image/ImageIO.hh>
34 #include <Base/Image/ImageConvert.hh>
35 
36 namespace BIAS {
37 
38  /**
39  @class JpegHandler
40 
41  @brief Wrapper for fast libjpeg methods
42 
43  Class for memory-to-memory JPEG compression. Compress a given BIAS::Image
44  (unsigned char) to data array in shared memory or decompress a given JPEG
45  file located in user memory into a BIAS::Image (unsigned char).
46 
47  @author Arne Petersen
48  @date 15/08/2005
49  */
50  class BIASImageBase_EXPORT JpegHandler
51  {
52  public:
53 
54  /** @brief Constructor with empty internal buffer */
55  JpegHandler();
56 
57  /** @brief Constructor with initialized internal buffer */
58  JpegHandler(long size);
59 
60  /** @brief Destructor releases internal buffer */
61  virtual ~JpegHandler();
62 
63  /**
64  @brief Initialize object with standard values and limit shared
65  memory area to given size (in byte, default is 10 MByte).
66  Re-initialization is only required for changing buffer size.
67  @attention Initialize buffer before first use!
68  @return Returns < 0 in case of errors, 0 otherwise.
69  */
70  int Init(long size = 10485760);
71 
72  /**
73  @brief Returns if an internal buffer has been initialized.
74  */
75  bool IsInitialized() { return data_ != NULL; }
76 
77  /**
78  @brief Releases allocated memory. Use it to regain memory but call
79  Init() again bevor using object afterwards!
80  */
81  void Release();
82 
83  /**
84  @brief Start compression for given BIAS::Image into buffer.
85  @return Returns < 0 in case of errors, 0 otherwise.
86  */
87  int Compress(BIAS::Image<unsigned char> &srcImg, int quality = 60);
88 
89  /**
90  @brief Start compression for given image data from buffer.
91  @return Returns < 0 in case of errors, 0 otherwise.
92  */
93  int Compress(unsigned int width, unsigned int height, unsigned int channels,
94  unsigned char* data, int quality = 60);
95 
96  /**
97  @brief Start decompression of previously given data (from ReadJPEG() or
98  SetCompressedData()), results will be stored in destination image.
99  Decompress() will initialize destination image properly if needed.
100  @return Returns < 0 in case of errors, 0 otherwise.
101  */
102  int Decompress(BIAS::Image<unsigned char> &destImg, bool readComment = false);
103 
104  /**
105  @brief Start decompression of previously given data (from ReadJPEG() or
106  SetCompressedData()), results will be stored in data, width, ...
107  New memory for data will only be allocated if data == NULL, else
108  old allocated memory will be used, so set data = NULL if you're
109  not sure about it's size!
110  @return Returns < 0 in case of errors, 0 otherwise.
111  */
112  int Decompress(unsigned int &width, unsigned int &height,
113  unsigned int &channels, unsigned char* &data);
114 
115  /**
116  @brief Start compression for given image data to given output buffer.
117  The internal buffer is not used here, hence it is allowed to be
118  uninitialized.
119  @return Returns < 0 in case of errors, 0 otherwise.
120  */
121  int Compress(unsigned int width, unsigned int height, unsigned int channels,
122  unsigned char* inData, unsigned char *outData, long &outBytes,
123  int quality = 60);
124 
125  /**
126  @brief Start decompression of given data and write result to given output
127  buffer. The internal buffer is not used here, hence it is allowed
128  to be uninitialized.
129  @return Returns < 0 in case of errors, 0 otherwise.
130  */
131  int Decompress(unsigned int &width, unsigned int &height, unsigned int &channels,
132  unsigned char* inData, long inBytes, unsigned char* outData);
133 
134  /**
135  @brief Get pointer to memory where to find compressed data and
136  its length in byte.
137  @attention No copy will be used, so make sure it's still valid when
138  starting Decompress() or reusing this!
139  */
140  inline void GetCompressedData(void* &data, long &length) {
141  data = data_;
142  length = dataLength_;
143  }
144 
145  /**
146  @brief Return compressed data length in bytes.
147  */
148  inline long GetCompressedDataSize() { return dataLength_; }
149 
150  /**
151  @brief Copy memory area in which compressed data can be found
152  and set its length in byte.
153  */
154  inline void SetCompressedData(void* data, long length) {
155  dataLength_ = length;
156  memcpy(data_, data, dataLength_);
157  }
158 
159  /**
160  @brief Write previously compressed data to file named filename.
161  @return Returns < 0 in case of errors, 0 otherwise.
162  */
163  int WriteJPEG(const std::string &filename);
164 
165  /**
166  @brief Read JPEG file named filename and store its informations in
167  compressed data field.
168  @return Returns < 0 in case of errors, 0 otherwise.
169  */
170  int ReadJPEG(const std::string &filename);
171 
172  /**
173  @brief Print error messages to standard output or keep quiet?
174  */
175  void SetVerbose(const bool verbose) { verbose_ = verbose; }
176 
177  protected:
178 
179  /** @brief Pointer to compression buffer (compressed data) */
180  void* data_;
181 
182  /** @brief Length in byte of compressed data */
184 
185  /** @brief Length in byte of compression buffer (max. data size) */
187 
188  /** @brief Print error messages or keep quiet? */
189  bool verbose_;
190 
191  };
192 
193 }
194 
195 #endif //__JPEGHANDLER_HH__
Wrapper for fast libjpeg methods.
Definition: CompressJpeg.hh:50
long bufferSize_
Length in byte of compression buffer (max.
bool verbose_
Print error messages or keep quiet?
void GetCompressedData(void *&data, long &length)
Get pointer to memory where to find compressed data and its length in byte.
long dataLength_
Length in byte of compressed data.
void SetVerbose(const bool verbose)
Print error messages to standard output or keep quiet?
bool IsInitialized()
Returns if an internal buffer has been initialized.
Definition: CompressJpeg.hh:75
long GetCompressedDataSize()
Return compressed data length in bytes.
void SetCompressedData(void *data, long length)
Copy memory area in which compressed data can be found and set its length in byte.
void * data_
Pointer to compression buffer (compressed data)