VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grjpeg.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 grjpeg.h
19 ///
20 
21 #ifndef GR_JPEG__H
22 #define GR_JPEG__H
23 
24 #include "gdef.h"
25 #include "grdef.h"
26 #include "grimagedata.h"
27 #include "grimagebuffer.h"
28 #include "gpath.h"
29 #include "gexception.h"
30 #include <map>
31 #include <string>
32 #include <cstdio>
33 #include <vector>
34 #include <iostream>
35 #include <utility>
36 
37 namespace Gr
38 {
39  typedef unsigned char jpeg_byte ; ///< Equivalent to libjpeg JSAMPLE. A static-assert checks the size.
40  class Jpeg ;
41  class JpegInfo ;
42  class JpegReaderImp ;
43  class JpegReader ;
44  class JpegWriterImp ;
45  class JpegWriter ;
46 }
47 
48 /// \class Gr::Jpeg
49 /// A scoping class for other jpeg classes.
50 ///
51 class Gr::Jpeg
52 {
53 public:
54  G_EXCEPTION( Error , "jpeg error" ) ;
55 
56  static bool available() ;
57  ///< Returns true if a jpeg library is available.
58 } ;
59 
60 /// \class Gr::JpegInfo
61 /// Provides some basic information about a jpeg image without full decoding.
62 ///
64 {
65 public:
66  JpegInfo( const unsigned char * p , size_t ) ;
67  ///< Constructor. This requires the whole file contents because
68  ///< the relevant SOF chunk can be anywhere, so prefer the
69  ///< istream interface.
70 
71  JpegInfo( const char * p , size_t ) ;
72  ///< Constructor overload for char.
73 
74  explicit JpegInfo( std::istream & ) ;
75  ///< Constructor overload for an istream.
76 
77  bool valid() const ;
78  ///< Returns true if constructed successfully.
79 
80  int dx() const ;
81  ///< Returns the image width.
82 
83  int dy() const ;
84  ///< Returns the image height.
85 
86  int channels() const ;
87  ///< Returns the number of channels (eg. 3).
88 
89 private:
90  JpegInfo( const JpegInfo & ) ;
91  void operator=( const JpegInfo & ) ;
92 
93 private:
94  int m_dx ;
95  int m_dy ;
96  int m_channels ;
97 } ;
98 
99 /// \class Gr::JpegReader
100 /// A read interface for libjpeg.
101 ///
103 {
104 public:
105  typedef Jpeg::Error Error ;
106 
107  JpegReader( int scale = 1 , bool monochrome_out = false ) ;
108  ///< Constructor.
109 
110  ~JpegReader() ;
111  ///< Destructor.
112 
113  void setup( int scale , bool monochrome_out = false ) ;
114  ///< Sets the decoding scale factor. In practice scale factors of
115  ///< 1, 2, 4 and 8 are supported directly by the jpeg library,
116  ///< and others scale factors are applied only after decoding
117  ///< are full scale.
118 
119  void decode( ImageData & out , const G::Path & in ) ;
120  ///< Decodes a jpeg file into an image. Throws on error.
121 
122  void decode( ImageData & out , const char * p_in , size_t n ) ;
123  ///< Decodes a jpeg buffer into an image. Throws on error.
124 
125  void decode( ImageData & out , const unsigned char * p_in , size_t n ) ;
126  ///< Decodes a jpeg buffer into an image. Throws on error.
127 
128  void decode( ImageData & out , const ImageBuffer & ) ;
129  ///< Decodes a jpeg buffer into an image. Throws on error.
130 
131 private:
132  JpegReader( const JpegReader & ) ;
133  void operator=( const JpegReader & ) ;
134 
135 private:
136  unique_ptr<JpegReaderImp> m_imp ;
137  int m_scale ;
138  bool m_monochrome_out ;
139 } ;
140 
141 /// \class Gr::JpegWriter
142 /// A write interface for libjpeg.
143 ///
145 {
146 public:
147  explicit JpegWriter( int scale = 1 , bool monochrome_out = false ) ;
148  ///< Constructor.
149 
150  ~JpegWriter() ;
151  ///< Destructor.
152 
153  void setup( int scale , bool monochrome_out = false ) ;
154  ///< Sets the encoding scale factor.
155 
156  void encode( const ImageData & in , const G::Path & path_out ) ;
157  ///< Encodes to a file.
158 
159  void encode( const ImageData & in , std::vector<char> & out ) ;
160  ///< Encodes to a buffer. Use an output buffer that is one byte bigger
161  ///< than the expected output size in order to avoid copying.
162 
163  void encode( const ImageData & in , ImageBuffer & out ) ;
164  ///< Encodes to an image buffer, allocated in reasonably-size chunks.
165 
166 private:
167  JpegWriter( const JpegWriter & ) ;
168  void operator=( const JpegWriter & ) ;
169 
170 private:
171  unique_ptr<JpegWriterImp> m_imp ;
172 } ;
173 
174 #endif
int dx() const
Returns the image width.
Definition: grjpeg.cpp:168
A holder for image data, having eight bits per sample and one or three channels.
Definition: grimagedata.h:46
void encode(const ImageData &in, const G::Path &path_out)
Encodes to a file.
A private pimple class for Gr::JpegWriter.
unsigned char jpeg_byte
Equivalent to libjpeg JSAMPLE. A static-assert checks the size.
Definition: grjpeg.h:39
int dy() const
Returns the image height.
Definition: grjpeg.cpp:173
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
Definition: grimagebuffer.h:47
void setup(int scale, bool monochrome_out=false)
Sets the decoding scale factor.
A scoping class for other jpeg classes.
Definition: grjpeg.h:51
~JpegReader()
Destructor.
static bool available()
Returns true if a jpeg library is available.
A private implementation class for Gr::JpegReader.
int channels() const
Returns the number of channels (eg. 3).
Definition: grjpeg.cpp:178
bool valid() const
Returns true if constructed successfully.
Definition: grjpeg.cpp:163
A read interface for libjpeg.
Definition: grjpeg.h:102
~JpegWriter()
Destructor.
void decode(ImageData &out, const G::Path &in)
Decodes a jpeg file into an image. Throws on error.
A write interface for libjpeg.
Definition: grjpeg.h:144
Provides some basic information about a jpeg image without full decoding.
Definition: grjpeg.h:63
JpegReader(int scale=1, bool monochrome_out=false)
Constructor.
void setup(int scale, bool monochrome_out=false)
Sets the encoding scale factor.
JpegWriter(int scale=1, bool monochrome_out=false)
Constructor.
A Path object represents a file system path.
Definition: gpath.h:72
JpegInfo(const unsigned char *p, size_t)
Constructor.
Definition: grjpeg.cpp:151