VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grimagetype.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 grimagetype.h
19 ///
20 
21 #ifndef GR_IMAGE_TYPE__H
22 #define GR_IMAGE_TYPE__H
23 
24 #include "gdef.h"
25 #include "grimagebuffer.h"
26 #include <string>
27 #include <vector>
28 #include <iostream>
29 
30 namespace Gr
31 {
32  class ImageType ;
33 }
34 
35 /// \class Gr::ImageType
36 /// An encapsulation of image type, including width, height and number of channels,
37 /// with support for a string form that encodes the same information.
38 ///
39 /// The string format is like "image/png;xsize=640x480x3" where the last of the
40 /// three numbers is the number of channels (1 or 3), and the image type is
41 /// "x.raw", "jpeg", "png" or "x-portable-anymap".
42 ///
44 {
45 public:
46  struct String /// A small-string class used for stringised Gr::ImageType instances.
47  {
48  String() { s[0] = '\0' ; }
49  const char * c_str() const { return s ; }
50  std::string str() const ;
51  char s[60] ;
52  } ;
53 
54  ImageType() ;
55  ///< Default constructor for an in-valid() image type with dimensions
56  ///< of zero.
57 
58  explicit ImageType( std::istream & ) ;
59  ///< Constructor that examines the image data to determine its type.
60  ///< The stream position is not advanced. Throws if the stream is
61  ///< not seekable.
62 
63  explicit ImageType( const std::vector<char> & ) ;
64  ///< Constructor that examines the image data to determine its type.
65  ///< The image data should be for the complete image, not just the
66  ///< first section (because jpeg).
67 
68  explicit ImageType( const ImageBuffer & ) ;
69  ///< Constructor overload for ImageBuffer.
70 
71  ImageType( const char * p , size_t n ) ;
72  ///< Constructor overload for a char buffer.
73 
74  ImageType( const unsigned char * p , size_t n ) ;
75  ///< Constructor overload for an unsigned char buffer.
76 
77  explicit ImageType( const std::string & ) ;
78  ///< Constructor taking a stringised image type that includes image size
79  ///< information.
80 
81  static ImageType jpeg( int dx , int dy , int channels = 3 ) ;
82  ///< Factory function for a jpeg image type.
83 
84  static ImageType jpeg( ImageType , int scale = 1 , bool monochrome_out = false ) ;
85  ///< Factory function for a jpeg image type with the same dimensions
86  ///< as the given image type, optionally scaled.
87 
88  static ImageType png( int dx , int dy , int channels = 3 ) ;
89  ///< Factory function for a png image type.
90 
91  static ImageType png( ImageType , int scale = 1 , bool monochrome_out = false ) ;
92  ///< Factory function for a png image type with the same dimensions
93  ///< as the given image type, optionally scaled.
94 
95  static ImageType raw( int dx , int dy , int channels ) ;
96  ///< Factory function for a raw image type.
97 
98  static ImageType raw( ImageType , int scale = 1 , bool monochrome_out = false ) ;
99  ///< Factory function for a raw image type with the same dimensions
100  ///< as the given image type, optionally scaled.
101 
102  bool valid() const ;
103  ///< Returns true if valid.
104 
105  bool matches( const std::string & str ) const ;
106  ///< Returns true if this type matches the given type (including size decorations).
107 
108  std::string str() const ;
109  ///< Returns the image type string (including the size parameter).
110 
111  String & set( String & out ) const ;
112  ///< Returns str() by reference.
113 
114  std::string simple() const ;
115  ///< Returns the basic image type string, excluding the size parameter.
116 
117  bool isRaw() const ;
118  ///< Returns true if a raw image type.
119 
120  bool isJpeg() const ;
121  ///< Returns true if a jpeg image type.
122 
123  bool isPng() const ;
124  ///< Returns true if a png image type.
125 
126  bool isPnm() const ;
127  ///< Returns true if a pnm image type.
128 
129  int dx() const ;
130  ///< Returns the image width.
131 
132  int dy() const ;
133  ///< Returns the image height.
134 
135  int channels() const ;
136  ///< Returns the number of channels.
137 
138  size_t size() const ;
139  ///< Returns the product of dx, dy and channels.
140  ///< This is usually only meaningful for raw types.
141 
142  size_t rowsize() const ;
143  ///< Returns the product of dx and channels.
144  ///< This is usually only meaningful for raw types.
145 
146  bool operator==( const ImageType & ) const ;
147  ///< Comparison operator.
148 
149  bool operator!=( const ImageType & ) const ;
150  ///< Comparison operator.
151 
152  bool operator<( const ImageType & ) const ;
153  ///< Comparison operator.
154 
155  void streamOut( std::ostream & ) const ;
156  ///< Used by op<<().
157 
158 private:
159  enum Type { t_invalid , t_jpeg , t_png , t_raw , t_pnm } ;
160  ImageType( Type type_ , int dx_ , int dy_ , int channels_ ) ;
161  static Type typeFromSignature( const unsigned char * , size_t ) ;
162  void init( std::istream & ) ;
163  void init( const unsigned char * p , size_t n ) ;
164  void init( Type , int , int , int ) ;
165  void setsimple( String & ) const ;
166 
167 private:
168  Type m_type ;
169  int m_dx ;
170  int m_dy ;
171  int m_channels ;
172 } ;
173 
174 namespace Gr
175 {
176  inline
177  std::ostream & operator<<( std::ostream & stream , const ImageType & s )
178  {
179  s.streamOut( stream ) ;
180  return stream ;
181  }
182 }
183 
184 inline
185 int Gr::ImageType::dx() const
186 {
187  return m_dx ;
188 }
189 
190 inline
191 int Gr::ImageType::dy() const
192 {
193  return m_dy ;
194 }
195 
196 inline
198 {
199  return m_channels ;
200 }
201 
202 #endif
bool isRaw() const
Returns true if a raw image type.
bool isJpeg() const
Returns true if a jpeg image type.
bool operator<(const ImageType &) const
Comparison operator.
int channels() const
Returns the number of channels.
Definition: grimagetype.h:197
static ImageType jpeg(int dx, int dy, int channels=3)
Factory function for a jpeg image type.
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
String & set(String &out) const
Returns str() by reference.
static ImageType raw(int dx, int dy, int channels)
Factory function for a raw image type.
std::string str() const
Returns the image type string (including the size parameter).
size_t size() const
Returns the product of dx, dy and channels.
bool valid() const
Returns true if valid.
bool isPng() const
Returns true if a png image type.
int dy() const
Returns the image height.
Definition: grimagetype.h:191
void streamOut(std::ostream &) const
Used by op<<().
size_t rowsize() const
Returns the product of dx and channels.
bool matches(const std::string &str) const
Returns true if this type matches the given type (including size decorations).
int dx() const
Returns the image width.
Definition: grimagetype.h:185
std::string simple() const
Returns the basic image type string, excluding the size parameter.
bool operator!=(const ImageType &) const
Comparison operator.
ImageType()
Default constructor for an in-valid() image type with dimensions of zero.
Definition: grimagetype.cpp:91
A small-string class used for stringised Gr::ImageType instances.
Definition: grimagetype.h:46
bool operator==(const ImageType &) const
Comparison operator.
bool isPnm() const
Returns true if a pnm image type.
static ImageType png(int dx, int dy, int channels=3)
Factory function for a png image type.