VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grimagedecoder.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file grimagedecoder.h
19 ///
20 
21 #ifndef GR_IMAGE_DECODER__H
22 #define GR_IMAGE_DECODER__H
23 
24 #include "gdef.h"
25 #include "grimagetype.h"
26 #include "grimagedata.h"
27 #include "grpng.h"
28 #include "grjpeg.h"
29 #include "grpnm.h"
30 #include "gpath.h"
31 #include "gexception.h"
32 #include <vector>
33 
34 namespace Gr
35 {
36  class ImageDecoder ;
37 }
38 
39 /// \class Gr::ImageDecoder
40 /// An interface for decoding encoded images into a raw format. The input
41 /// images can be in raw, png, jpeg or pnm formats.
42 ///
43 /// Note that raw format is not self-describing, so raw images often use a
44 /// binary pnm format when stored to file and these files are trivially
45 /// decoded into the raw format as they are read in.
46 ///
48 {
49 public:
50  G_EXCEPTION( Error , "image decoding error" ) ;
51 
52  struct ScaleToFit /// Describes scale-to-fit target dimensions.
53  {
54  ScaleToFit() ;
55  ScaleToFit( int dx , int dy , int fudge_factor ) ;
56  int operator()( const ImageType & ) const ;
57  operator bool() const ;
58  int dx ;
59  int dy ;
60  int ff ;
61  } ;
62 
63  ImageDecoder() ;
64  ///< Default constructor.
65 
66  void setup( int scale , bool monochrome_out ) ;
67  ///< Sets the scale factor.
68 
69  ImageType decode( const G::Path & path_in ,
70  ImageData & out , const ScaleToFit & = ScaleToFit() ) ;
71  ///< Decodes a file, with a scale-to-fit option.
72  ///< Scale-to-fit scaling takes precedence over the decoder's default scale factor.
73  ///< Returns the raw image type. Throws on error.
74 
75  ImageType decode( const ImageType & type_in , const G::Path & path_in ,
76  ImageData & out , ScaleToFit = ScaleToFit() ) ;
77  ///< Decodes a file with the given image type, with a scale-to-fit option.
78  ///< This is a more efficient overload if the file type is already known.
79  ///< Scale-to-fit scaling overrides the decoder's default scale factor.
80  ///< Returns the raw image type. Throws on error.
81 
82  ImageType decode( const ImageType & type_in , const char * data_in , size_t ,
83  ImageData & out , const ScaleToFit & = ScaleToFit() ) ;
84  ///< Decodes the image buffer and returns the raw image type.
85  ///< Throws on error.
86 
87  ImageType decode( const ImageType & type_in , const std::vector<char> & data_in ,
88  ImageData & out , const ScaleToFit & = ScaleToFit() ) ;
89  ///< Decodes the image buffer and returns the raw image type.
90  ///< Throws on error.
91 
92  ImageType decode( const ImageType & type_in , const ImageBuffer & data_in ,
93  ImageData & out , const ScaleToFit & = ScaleToFit() ) ;
94  ///< Decodes the image buffer and returns the raw image type.
95  ///< Throws on error.
96 
97  static ImageType readType( const G::Path & path , bool do_throw = true ) ;
98  ///< A convenience function to read a file's image type.
99  ///< Throws on error, by default.
100 
101  ImageType decodeInPlace( ImageType type_in , char * & p , size_t size_in , ImageData & store ) ;
102  ///< Decodes an image buffer with raw decoding done in-place.
103  ///< The image pointer is passed by reference and is either unchanged
104  ///< after an in-place decode, or it is set to point into the supplied
105  ///< contiguous ImageData object.
106 
107 private:
108  ImageDecoder( const ImageDecoder & ) ;
109  void operator=( const ImageDecoder & ) ;
110  ImageType decodeFile( ImageType , const G::Path & , ImageData & , const ScaleToFit & ) ;
111  ImageType decodeBuffer( ImageType , const char * p , size_t n , ImageData & , const ScaleToFit & ) ;
112  static bool jpegAvailable() ;
113  static bool pngAvailable() ;
114 
115 private:
116  int m_scale ;
117  bool m_monochrome_out ;
118  JpegReader m_jpeg ;
119  PngReader m_png ;
120  PnmReader m_pnm ;
121 } ;
122 
123 
124 inline
125 Gr::ImageDecoder::ScaleToFit::ScaleToFit() :
126  dx(0) ,
127  dy(0) ,
128  ff(0)
129 {
130 }
131 
132 inline
133 Gr::ImageDecoder::ScaleToFit::ScaleToFit( int dx_ , int dy_ , int ff_ ) :
134  dx(dx_) ,
135  dy(dy_) ,
136  ff(ff_)
137 {
138 }
139 
140 #endif
ImageType decodeInPlace(ImageType type_in, char *&p, size_t size_in, ImageData &store)
Decodes an image buffer with raw decoding done in-place.
A read interface for libpng.
Definition: grpng.h:111
A holder for image data, having eight bits per sample and one or three channels.
Definition: grimagedata.h:46
void setup(int scale, bool monochrome_out)
Sets the scale factor.
An encapsulation of image type, including width, height and number of channels, with support for a st...
Definition: grimagetype.h:43
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
Definition: grimagebuffer.h:47
An interface for decoding encoded images into a raw format.
ImageDecoder()
Default constructor.
A read interface for libjpeg.
Definition: grjpeg.h:102
static ImageType readType(const G::Path &path, bool do_throw=true)
A convenience function to read a file's image type.
ImageType decode(const G::Path &path_in, ImageData &out, const ScaleToFit &=ScaleToFit())
Decodes a file, with a scale-to-fit option.
Describes scale-to-fit target dimensions.
A static interface for reading portable-anymap (pnm) files.
Definition: grpnm.h:110
A Path object represents a file system path.
Definition: gpath.h:72