Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ImageUtil.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_image_util_h_
00003 #define INCLUDED_image_util_h_
00004 
00005 #include <string>
00006 
00007 struct jpeg_compress_struct;
00008 
00009 //! Provides a variety of straightforward calls to compress or decompress images in JPEG or PNG formats
00010 namespace image_util {
00011   
00012   //! @name Generic File Utilities
00013   
00014   //! loads a file into memory, returns true if successful, storing resulting buffer information in @a buf and @a size
00015   /*! uses mmap unless LOADFILE_NO_MMAP is defined, in which case it uses 'new' and fread() */
00016   bool loadFile(const std::string& file, char*& buf, size_t& size);
00017   //! releases memory from a previous call to loadFile, triggering munmap() or 'delete' as appropriate
00018   void releaseFile(char* buf, size_t size);
00019   
00020   //@}
00021   
00022   
00023   
00024   //! @name Decode Header Information (from memory buffer)
00025   
00026   //! decodes an image header already in memory -- if it looks like a PNG decodePNG() will be called, otherwise decodeJPEG(); returns true if successful
00027   /*! @param inbuf input memory buffer containing compressed image
00028    *  @param inbufSize the size of @a inbuf allocation
00029    *  @param width the image width
00030    *  @param height the image height
00031    *  @param channels the number of color channels*/
00032   bool decodeImage(char* inbuf, size_t inbufSize, size_t& width, size_t& height, size_t& channels);
00033   
00034   //! decodes a JPEG header already in memory, returns true if successful
00035   /*! @param inbuf input memory buffer containing compressed image
00036    *  @param inbufSize the size of @a inbuf allocation
00037    *  @param width the image width
00038    *  @param height the image height
00039    *  @param channels the number of color channels */
00040   bool decodeJPEG(char* inbuf, size_t inbufSize, size_t& width, size_t& height, size_t& channels);
00041 
00042   //! decodes a PNG header already in memory, returns true if successful
00043   /*! @param inbuf input memory buffer containing compressed image
00044    *  @param inbufSize the size of @a inbuf allocation
00045    *  @param width the image width
00046    *  @param height the image height
00047    *  @param channels the number of color channels */
00048   bool decodePNG(char* inbuf, size_t inbufSize, size_t& width, size_t& height, size_t& channels);
00049   
00050   //@}
00051   
00052   
00053   
00054   //! @name Decompress From Memory Buffer
00055   
00056   //! decodes an image already in memory -- if it looks like a PNG decodePNG() will be called, otherwise decodeJPEG(); returns true if successful
00057   /*! @param inbuf input memory buffer containing compressed image
00058    *  @param inbufSize the size of @a inbuf allocation
00059    *  @param width the image width
00060    *  @param height the image height
00061    *  @param channels the number of color channels
00062    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00063    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00064    *
00065    * If @a outbuf is pre-allocated and outbufSize is less than width*height*channels, the function will return false.
00066    * (This is a handy way to read the image header only -- pass ((char*)NULL)-1 as @a outbuf and 0 for @a outbufSize,
00067    * the function will return false after reading the header and filling in width, height, and channels)
00068    * The image will be written in row order, with channels interleaved. */
00069   bool decodeImage(char* inbuf, size_t inbufSize, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize);
00070   
00071   //! decodes a JPEG image already in memory, returns true if successful
00072   /*! @param inbuf input memory buffer containing compressed image
00073    *  @param inbufSize the size of @a inbuf allocation
00074    *  @param width the image width
00075    *  @param height the image height
00076    *  @param channels the number of color channels
00077    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00078    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00079    *  @param filename optional parameter, used if warnings are raised
00080    *
00081    * If @a outbuf is pre-allocated and outbufSize is less than width*height*channels, the function will return false.
00082    * The image will be written in row order, with channels interleaved. */
00083   bool decodeJPEG(char* inbuf, size_t inbufSize, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize, const std::string& filename="");
00084 
00085   //! decodes a PNG image already in memory, returns true if successful
00086   /*! @param inbuf input memory buffer containing compressed image
00087    *  @param inbufSize the size of @a inbuf allocation
00088    *  @param width the image width
00089    *  @param height the image height
00090    *  @param channels the number of color channels
00091    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00092    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00093    *
00094    * If @a outbuf is pre-allocated and outbufSize is less than width*height*channels, the function will return false.
00095    * The image will be written in row order, with channels interleaved. */
00096   bool decodePNG(char* inbuf, size_t inbufSize, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize);
00097   
00098   //@}
00099   
00100   
00101   
00102   //! @name Decode Header Information (from stream)
00103   
00104   //! decodes an image header already in memory -- if it looks like a PNG decodePNG() will be called, otherwise decodeJPEG(); returns true if successful
00105   /*! @param inbuf input memory buffer containing compressed image
00106    *  @param inbufSize the size of @a inbuf allocation
00107    *  @param width the image width
00108    *  @param height the image height
00109    *  @param channels the number of color channels*/
00110   bool decodeImage(std::istream& inStream, size_t& width, size_t& height, size_t& channels);
00111   
00112   //! decodes a JPEG header already in memory, returns true if successful
00113   /*! @param inbuf input memory buffer containing compressed image
00114    *  @param inbufSize the size of @a inbuf allocation
00115    *  @param width the image width
00116    *  @param height the image height
00117    *  @param channels the number of color channels */
00118   bool decodeJPEG(std::istream& inStream, size_t& width, size_t& height, size_t& channels);
00119   
00120   //! decodes a PNG header already in memory, returns true if successful
00121   /*! @param inbuf input memory buffer containing compressed image
00122    *  @param inbufSize the size of @a inbuf allocation
00123    *  @param width the image width
00124    *  @param height the image height
00125    *  @param channels the number of color channels */
00126   bool decodePNG(std::istream& inStream, size_t& width, size_t& height, size_t& channels);
00127   
00128   //@}
00129   
00130   
00131   
00132   //! @name Decompress From Input Stream
00133   
00134   //! decodes am image from a standard library input stream -- if it looks like a PNG decodePNG() will be called, otherwise decodeJPEG(); returns true if successful
00135   /*! @param inStream input stream from which to read images
00136    *  @param width the image width
00137    *  @param height the image height
00138    *  @param channels the number of color channels
00139    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00140    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00141    *
00142    * If @a outbuf is pre-allocated and outbufSize is less than width*height*channels, the function will return false.
00143    * The image will be written in row order, with channels interleaved. */
00144   bool decodeImage(std::istream& inStream, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize);
00145   
00146   //! decodes a JPEG image from a standard library input stream, returns true if successful
00147   /*! @param inStream input stream providing image
00148    *  @param width the image width
00149    *  @param height the image height
00150    *  @param channels the number of color channels
00151    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00152    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00153    *  @param filename optional parameter, used if warnings are raised
00154    *
00155    * If @a outbuf is pre-allocated and outbufSize is less than width*height*channels, the function will return false.
00156    * The image will be written in row order, with channels interleaved. */
00157   bool decodeJPEG(std::istream& inStream, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize, const std::string& filename="");
00158 
00159   //! decodes a PNG image from a standard library input stream, returns true if successful
00160   /*! @param inStream input stream from which to read images
00161    *  @param width the image width
00162    *  @param height the image height
00163    *  @param channels the number of color channels
00164    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00165    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00166    *
00167    * If @a outbuf is pre-allocated and outbufSize is less than width*height*channels, the function will return false.
00168    * The image will be written in row order, with channels interleaved. */
00169   bool decodePNG(std::istream& inStream, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize);
00170   
00171   //@}
00172   
00173   bool decodePNGToDepth(std::istream& inStream, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize);
00174   
00175   //! @name Decompress from File
00176   
00177   //! decodes an image from file on disk -- if it looks like a PNG decodePNG() will be called, otherwise decodeJPEG(); returns true if successful
00178   /*! @param file path to file to load
00179    *  @param width the image width
00180    *  @param height the image height
00181    *  @param channels the number of color channels
00182    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00183    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00184    *
00185    * If @a outbuf is pre-allocated and outbufSize is less than width*height*channels, the function will return false.
00186    * The image will be written in row order, with channels interleaved. */
00187   bool loadImage(const std::string& file, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize);
00188   
00189   //! decodes a JPEG from disk, returns true if successful
00190   /*! @param file path to file to load
00191    *  @param width the image width
00192    *  @param height the image height
00193    *  @param channels the number of color channels
00194    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00195    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00196    *
00197    * If @a outbuf is pre-allocated and outbufSize is less than width*height*channels, the function will return false.
00198    * The image will be written in row order, with channels interleaved. */
00199   bool loadJPEG(const std::string& file, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize);
00200   
00201   //! decodes a PNG from disk, returns true if successful
00202   /*! @param file path to file to load
00203    *  @param width the image width
00204    *  @param height the image height
00205    *  @param channels the number of color channels
00206    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00207    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00208    *
00209    * If @a outbuf is pre-allocated and outbufSize is less than width*height*channels, the function will return false.
00210    * The image will be written in row order, with channels interleaved. */
00211   bool loadPNG(const std::string& file, size_t& width, size_t& height, size_t& channels, char*& outbuf, size_t& outbufSize);
00212   
00213   //@}
00214   
00215   
00216   
00217   //! @name Compression (In-Memory only)
00218   
00219   //! encodes a JPEG from a pixel buffer into another memory buffer, returns number of bytes used, 0 if error
00220   /*! @param inbuf input memory buffer containing the image
00221    *  @param inbufSize the size of @a inbuf allocation
00222    *  @param width the image width
00223    *  @param height the image height
00224    *  @param inbufChannels the number of color channels in the source image; either 1 (grayscale), 3 (YUV), or -3U (RGB)
00225    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00226    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00227    *  @param outbufChannels the number of color channels desired in the destination image (only downsample from color to grayscale)
00228    *  @param quality how well to reproduce the image, 0-100
00229    *
00230    *  If @a outbuf is NULL, one of size @f$ width \cdot height \cdot outbufChannels \cdot (quality/2+25)/100+500 @f$ will be allocated for you.
00231    *  (just a heuristic size... note that this won't all be used, and can't entirely guarantee it'll be enough!)
00232    *
00233    *  If @a inbufChannels is 3, @a outbufChannels can be either 3 or 1.  If 1, the first channel of
00234    *  @a inbuf is used.  (pre-increment @a inbuf to use a different channel...)  If @a inbufChannels
00235    *  is 1, outbufChannels must also be 1. */
00236   size_t encodeJPEG(const char* inbuf, size_t inbufSize, size_t width, size_t height, size_t inbufChannels, char*& outbuf, size_t& outbufSize, size_t outbufChannels, int quality);
00237   
00238   //! encodes a JPEG from a pixel buffer into another memory buffer, returns number of bytes used, 0 if error
00239   /*! @param inbuf input memory buffer containing the image
00240    *  @param inbufSize the size of @a inbuf allocation
00241    *  @param width the image width
00242    *  @param height the image height
00243    *  @param inbufChannels the number of color channels in the source image; either 1 (grayscale), 3 (YUV), or -3U (RGB)
00244    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00245    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00246    *  @param outbufChannels the number of color channels desired in the destination image (only downsample from color to grayscale)
00247    *  @param quality how well to reproduce the image, 0-100
00248    *  @param cinfo allows you to use a pre-allocated jpeg structure instead of having the function recreate it each time
00249    *
00250    *  If @a outbuf is NULL, one of size @f$ width \cdot height \cdot outbufChannels \cdot (quality/2+25)/100+500 @f$ will be allocated for you.
00251    *  (just a heuristic size... note that this won't all be used, and can't entirely guarantee it'll be enough!)
00252    *
00253    *  If @a inbufChannels is 3, @a outbufChannels can be either 3 or 1.  If 1, the first channel of
00254    *  @a inbuf is used.  (pre-increment @a inbuf to use a different channel...)  If @a inbufChannels
00255    *  is 1, outbufChannels must also be 1. */
00256   size_t encodeJPEG(const char* inbuf, size_t inbufSize, size_t width, size_t height, size_t inbufChannels, char*& outbuf, size_t& outbufSize, size_t outbufChannels, int quality, jpeg_compress_struct& cinfo);
00257   
00258   //! encodes a JPEG from a pixel buffer into another memory buffer, returns number of bytes used, 0 if error
00259   /*! @param inbuf input memory buffer containing the image
00260    *  @param inbufSize the size of @a inbuf allocation
00261    *  @param width the image width
00262    *  @param height the image height
00263    *  @param inbufChannels the number of color channels in the source image; either 1 (grayscale), 3 (YUV), or -3U (RGB)
00264    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00265    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00266    *  @param outbufChannels the number of color channels desired in the destination image (only downsample from color to grayscale)
00267    *  @param quality how well to reproduce the image, 0-100
00268    *  @param yskip increment for the y channel
00269    *  @param uvskip increment for the u and v channels
00270    *  @param cinfo allows you to use a pre-allocated jpeg structure instead of having the function recreate it each time
00271    *
00272    *  If @a outbuf is NULL, one of size @f$ width \cdot height \cdot outbufChannels \cdot (quality/2+25)/100+500 @f$ will be allocated for you.
00273    *  (just a heuristic size... note that this won't all be used, and can't entirely guarantee it'll be enough!)
00274    *
00275    *  If @a inbufChannels is 3, @a outbufChannels can be either 3 or 1.  If 1, the first channel of
00276    *  @a inbuf is used.  (pre-increment @a inbuf to use a different channel...)  If @a inbufChannels
00277    *  is 1, outbufChannels must also be 1. */
00278   size_t encodeJPEG(const char* inbuf, size_t inbufSize, size_t width, size_t height, size_t inbufChannels, char*& outbuf, size_t& outbufSize, size_t outbufChannels, int quality, unsigned int yskip, unsigned int uvskip, jpeg_compress_struct& cinfo);
00279 
00280   //! encodes a PNG from a pixel buffer into another memory buffer, returns number of bytes used, 0 if error
00281   /*! @param inbuf input memory buffer containing the image
00282    *  @param inbufSize the size of @a inbuf allocation
00283    *  @param width the image width
00284    *  @param height the image height
00285    *  @param inbufChannels the number of color channels in the source image; either 1 (grayscale), or 3 (color); must match @a outbufChannels
00286    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00287    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00288    *  @param outbufChannels the number of color channels desired in the destination image; must match @a inbufChannels
00289    *
00290    *  If @a outbuf is NULL, one of size @f$ width \cdot height \cdot outbufChannels + 500 @f$ will be allocated for you.
00291    *  (just a heuristic size... note that this won't all be used, and can't entirely guarantee it'll be enough!)
00292    *
00293    *  Uses the default compression level, which gets most of the compression available without encurring excessive
00294    *  computation.  According to libpng documentation, is equivalent to passing '6' as compressionLevel to the other version of the function.
00295    *
00296    *  Currently doesn't support changes in channels, so @a inbufChannels must match @a outbufChannels */
00297   size_t encodePNG(const char* inbuf, size_t inbufSize, size_t width, size_t height, size_t inbufChannels, char*& outbuf, size_t& outbufSize, size_t outbufChannels);
00298   
00299   //! encodes a PNG from a pixel buffer into another memory buffer, returns number of bytes used, 0 if error
00300   /*! @param inbuf input memory buffer containing the image
00301    *  @param inbufSize the size of @a inbuf allocation
00302    *  @param width the image width
00303    *  @param height the image height
00304    *  @param inbufChannels the number of color channels in the source image; either 1 (grayscale), or 3 (color); must match @a outbufChannels
00305    *  @param outbuf on input, a buffer to use for decompression; if NULL, a new buffer will be allocated and assigned
00306    *  @param outbufSize if @a outbuf is non-NULL, this should indicate the size of @a outbuf
00307    *  @param outbufChannels the number of color channels desired in the destination image; must match @a inbufChannels
00308    *  @param compressionLevel a value 0 (none, grows slightly) through 9 (slow, but smallest) to pass to zlib for size vs. speed.  Compression is always lossless, so this does not affect image quality.
00309    *
00310    *  If @a outbuf is NULL, one of size @f$ width \cdot height \cdot outbufChannels + 500 @f$ will be allocated for you.
00311    *  (just a heuristic size... note that this won't all be used, and can't entirely guarantee it'll be enough!)
00312    *
00313    *  Currently doesn't support changes in channels, so @a inbufChannels must match @a outbufChannels */
00314   size_t encodePNG(const char* inbuf, size_t inbufSize, size_t width, size_t height, size_t inbufChannels, char*& outbuf, size_t& outbufSize, size_t outbufChannels, int compressionLevel);
00315   
00316   //@}
00317   
00318 };
00319 
00320 /*! @file
00321  * @brief 
00322  * @author Ethan Tira-Thompson (ejt) (Creator)
00323  */
00324 
00325 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:42 2016 by Doxygen 1.6.3