VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grimage.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 grimage.h
19 ///
20 
21 #ifndef GR_IMAGE__H
22 #define GR_IMAGE__H
23 
24 #include "gdef.h"
25 #include "grdef.h"
26 #include "grimagetype.h"
27 #include "grimagedata.h"
28 #include "grimagebuffer.h"
29 #include "gpublisher.h"
30 
31 namespace Gr
32 {
33  class Image ;
34 }
35 
36 /// \class Gr::Image
37 /// A class holding shared read-only image data (Gr::ImageBuffer)
38 /// and its associated image type (Gr::ImageType). Static methods
39 /// are used to read images from file or publication channel.
40 ///
41 class Gr::Image
42 {
43 public:
44  G_EXCEPTION( ReadError , "cannot read image" ) ;
45 
46  Image() ;
47  ///< Default constructor for an empty() image with an in-valid()
48  ///< type() and a null ptr().
49 
50  Image( shared_ptr<const ImageBuffer> ptr , Gr::ImageType ) ;
51  ///< Constructor, taking a shared pointer to the image data and its
52  ///< image type.
53  ///<
54  ///< The image type is allowed to be in-valid() while still having a
55  ///< non-empty image buffer; this allows Image object to hold non-image
56  ///< data or non-standard image types, with the image or non-image
57  ///< type information held separately.
58 
59  bool empty() const ;
60  ///< Returns true if constructed with no image data.
61 
62  void clear() ;
63  ///< Clears the image as if default constructed.
64  ///< Postcondition: empty()
65 
66  bool valid() const ;
67  ///< Returns !empty() && type().valid().
68 
69  ImageType type() const ;
70  ///< Returns the image type.
71 
72  size_t size() const ;
73  ///< Returns the image data total size.
74 
75  shared_ptr<ImageBuffer> recycle() ;
76  ///< Creates a new shared pointer that can be deposited into a new Image,
77  ///< but if the current Image is not empty() and not shared (unique()) then
78  ///< its buffer is returned instead. This can avoid memory allocation when
79  ///< processing a sequence of images all of the same size, while still
80  ///< allowing slow code to take copies.
81  ///< \code
82  ///<
83  ///< Image ImageFactory::newImage( Image & old_image ) {
84  ///< shared_ptr<ImageBuffer> ptr = old_image.recycle() ;
85  ///< resize_and_fill( *ptr , m_type ) ;
86  ///< return Image( ptr , m_type ) ;
87  ///< }
88  ///< \endcode
89 
90  shared_ptr<const ImageBuffer> ptr() const ;
91  ///< Returns the image data shared pointer.
92 
93  const ImageBuffer & data() const ;
94  ///< Returns the image data.
95  ///< Precondition: !empty()
96 
97  static void read( std::istream & , Image & , const std::string & exception_help_text = std::string() ) ;
98  ///< Reads an input stream into an image. Throws on error.
99 
100  static bool receive( G::PublisherSubscription & channel , Image & , std::string & type_str ) ;
101  ///< Reads a publication channel into an image. If a non-image is received
102  ///< then the non-image type will be deposited in the supplied string
103  ///< and the Image type() will be invalid(). Returns false iff the channel
104  ///< receive fails.
105 
106  static bool peek( G::PublisherSubscription & channel , Image & , std::string & type_str ) ;
107  ///< A variant on receive() that does a channel peek().
108 
109  static ImageBuffer * blank( Image & , ImageType raw_type , bool contiguous = false ) ;
110  ///< Factory function for a not-really-blank raw image that is temporarily writable
111  ///< via the returned image buffer pointer. The implementation recycle()s the
112  ///< image and resizes its image buffer to match the given image type and
113  ///< contiguity.
114  ///< Precondition: type().isRaw()
115 
116 private:
117  static bool receiveImp( bool , G::PublisherSubscription & , Image & , std::string & ) ;
118 
119 private:
120  shared_ptr<const ImageBuffer> m_ptr ;
121  ImageType m_type ;
122 } ;
123 
124 #endif
shared_ptr< const ImageBuffer > ptr() const
Returns the image data shared pointer.
Definition: grimage.cpp:107
size_t size() const
Returns the image data total size.
Definition: grimage.cpp:101
static bool receive(G::PublisherSubscription &channel, Image &, std::string &type_str)
Reads a publication channel into an image.
Definition: grimage.cpp:155
void clear()
Clears the image as if default constructed.
Definition: grimage.cpp:95
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 easy-to-use combination of a G::PublisherChannel object and a single G::PublisherSubscriber.
Definition: gpublisher.h:229
bool empty() const
Returns true if constructed with no image data.
Definition: grimage.cpp:90
A class holding shared read-only image data (Gr::ImageBuffer) and its associated image type (Gr::Imag...
Definition: grimage.h:41
static void read(std::istream &, Image &, const std::string &exception_help_text=std::string())
Reads an input stream into an image. Throws on error.
Definition: grimage.cpp:125
static ImageBuffer * blank(Image &, ImageType raw_type, bool contiguous=false)
Factory function for a not-really-blank raw image that is temporarily writable via the returned image...
Definition: grimage.cpp:54
bool valid() const
Returns !empty() && type().valid().
Definition: grimage.cpp:80
ImageType type() const
Returns the image type.
Definition: grimage.cpp:85
const ImageBuffer & data() const
Returns the image data.
Definition: grimage.cpp:112
shared_ptr< ImageBuffer > recycle()
Creates a new shared pointer that can be deposited into a new Image, but if the current Image is not ...
Definition: grimage.cpp:117
Image()
Default constructor for an empty() image with an in-valid() type() and a null ptr().
Definition: grimage.cpp:37
static bool peek(G::PublisherSubscription &channel, Image &, std::string &type_str)
A variant on receive() that does a channel peek().
Definition: grimage.cpp:160