VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grpnm.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 grpnm.h
19 ///
20 
21 #ifndef GR_PNM__H
22 #define GR_PNM__H
23 
24 #include "gdef.h"
25 #include "gpath.h"
26 #include "grimagedata.h"
27 #include "grimagebuffer.h"
28 #include "gexception.h"
29 #include <istream>
30 #include <utility>
31 #include <vector>
32 
33 namespace Gr
34 {
35  class PnmInfo ;
36  class PnmReader ;
37 }
38 
39 /// \class Gr::PnmInfo
40 /// A structure holding portable-anymap metadata.
41 ///
43 {
44 public:
45  PnmInfo() ;
46  ///< Default constructor for an invalid structure.
47 
48  explicit PnmInfo( std::istream & ) ;
49  ///< Constructor reading from an istream.
50 
51  explicit PnmInfo( const std::vector<char> & ) ;
52  ///< Constructor reading from a buffer.
53  ///<
54  ///< There is no limit on the header size (because of comments
55  ///< and whitespace) so the whole image should be supplied.
56 
57  explicit PnmInfo( const ImageBuffer & ) ;
58  ///< Constructor reading from a Gr::ImageBuffer.
59 
60  PnmInfo( const char * , size_t ) ;
61  ///< Constructor overload for a char buffer.
62 
63  PnmInfo( const unsigned char * , size_t ) ;
64  ///< Constructor overload for unsigned char.
65 
66  PnmInfo( int pn , int dx , int dy , int maxval , size_t offset ) ;
67  ///< Constructor taking the individual metadata items.
68 
69  bool valid() const ;
70  ///< Returns true if successfully constructed.
71 
72  bool binary() const ;
73  ///< Returns true if a binary format.
74 
75  bool binary8() const ;
76  ///< Returns true if an eight-bit binary format, ie. P5 or P6.
77 
78  int dx() const ;
79  ///< Returns the image width.
80 
81  int dy() const ;
82  ///< Returns the image height.
83 
84  int channels() const ;
85  ///< Returns the number of channels.
86 
87  int pn() const ;
88  ///< Returns the p-number.
89 
90  int maxval() const ;
91  ///< Returns the maximum value, or zero for bitmap formats (p1/p4).
92 
93  size_t offset() const ;
94  ///< Returns the size of the header.
95 
96  size_t rowsize() const ;
97  ///< Returns dx() * channels().
98 
99 private:
100  int m_pn ; ///< Px value, 1..6
101  int m_dx ;
102  int m_dy ;
103  int m_maxval ;
104  size_t m_offset ; ///< header size, ie. body offset
105 } ;
106 
107 /// \class Gr::PnmReader
108 /// A static interface for reading portable-anymap (pnm) files.
109 ///
111 {
112 public:
113  G_EXCEPTION( Error , "pnm image format error" ) ;
114 
115  explicit PnmReader( int scale = 1 , bool monochrome_out = false ) ;
116  ///< Constructor.
117 
118  void setup( int scale , bool monochrome_out = false ) ;
119  ///< Sets the decoding scale factor.
120 
121  void decode( ImageData & out , const G::Path & in ) ;
122  ///< Decodes a pnm file into an image. Throws on error.
123 
124  void decode( ImageData & out , std::istream & in ) ;
125  ///< Decodes a pnm stream into an image. Throws on error.
126 
127  void decode( ImageData & out , const char * p_in , size_t n_in ) ;
128  ///< Decodes a pnm buffer into an image. Throws on error.
129 
130  void decode( ImageData & out , const ImageBuffer & ) ;
131  ///< Decodes a pnm image buffer into an image. Throws on error.
132 
133  const PnmInfo & info() const ;
134  ///< Returns the pnm header info, ignoring scaling.
135 
136 private:
137  PnmReader( const PnmReader & ) ;
138  void operator=( const PnmReader & ) ;
139  static bool readBody( std::istream & , const PnmInfo & , ImageData & , int , bool ) ;
140 
141 private:
142  PnmInfo m_info ;
143  int m_scale ;
144  bool m_monochrome_out ;
145 } ;
146 
147 inline Gr::PnmInfo::PnmInfo() : m_pn(0) , m_dx(0) , m_dy(0) , m_maxval(0) , m_offset(0U) {}
148 inline bool Gr::PnmInfo::binary() const { return m_pn >= 4 ; }
149 inline bool Gr::PnmInfo::binary8() const { return m_pn >= 5 ; }
150 inline int Gr::PnmInfo::dx() const { return m_dx ; }
151 inline int Gr::PnmInfo::dy() const { return m_dy ; }
152 inline int Gr::PnmInfo::channels() const { return ( m_pn == 3 || m_pn == 6 ) ? 3 : 1 ; }
153 inline int Gr::PnmInfo::pn() const { return m_pn ; }
154 inline int Gr::PnmInfo::maxval() const { return m_maxval ; }
155 inline size_t Gr::PnmInfo::offset() const { return m_offset ; }
156 inline bool Gr::PnmInfo::valid() const { return m_dx > 0 && m_dy > 0 ; }
157 
158 inline const Gr::PnmInfo & Gr::PnmReader::info() const { return m_info ; }
159 
160 #endif
bool valid() const
Returns true if successfully constructed.
Definition: grpnm.h:156
int dx() const
Returns the image width.
Definition: grpnm.h:150
A holder for image data, having eight bits per sample and one or three channels.
Definition: grimagedata.h:46
PnmReader(int scale=1, bool monochrome_out=false)
Constructor.
Definition: grpnm.cpp:371
int maxval() const
Returns the maximum value, or zero for bitmap formats (p1/p4).
Definition: grpnm.h:154
int pn() const
Returns the p-number.
Definition: grpnm.h:153
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
Definition: grimagebuffer.h:47
int dy() const
Returns the image height.
Definition: grpnm.h:151
bool binary() const
Returns true if a binary format.
Definition: grpnm.h:148
void decode(ImageData &out, const G::Path &in)
Decodes a pnm file into an image. Throws on error.
Definition: grpnm.cpp:383
int channels() const
Returns the number of channels.
Definition: grpnm.h:152
bool binary8() const
Returns true if an eight-bit binary format, ie. P5 or P6.
Definition: grpnm.h:149
void setup(int scale, bool monochrome_out=false)
Sets the decoding scale factor.
Definition: grpnm.cpp:377
A structure holding portable-anymap metadata.
Definition: grpnm.h:42
PnmInfo()
Default constructor for an invalid structure.
Definition: grpnm.h:147
A static interface for reading portable-anymap (pnm) files.
Definition: grpnm.h:110
const PnmInfo & info() const
Returns the pnm header info, ignoring scaling.
Definition: grpnm.h:158
A Path object represents a file system path.
Definition: gpath.h:72
size_t rowsize() const
Returns dx() * channels().
Definition: grpnm.cpp:366
size_t offset() const
Returns the size of the header.
Definition: grpnm.h:155