31 size_t size_of( shared_ptr<const Gr::ImageBuffer> ptr )
33 return ptr.get() ==
nullptr ? 0U : Gr::imagebuffer::size_of(*ptr) ;
45 if( type.
isRaw() && size_of(ptr) != type.
size() )
47 G_ASSERT( !
"invalid raw image buffer size" ) ;
50 if( ptr.get() == nullptr )
56 G_ASSERT( type.
isRaw() ) ;
58 throw std::runtime_error(
"invalid type for blank image" ) ;
60 shared_ptr<ImageBuffer> ptr = image.
recycle() ;
65 ptr->at(0).resize( type.
size() ) ;
69 size_t rowsize = type.
rowsize() ;
70 const int dy = type.
dy() ;
72 for(
int y = 0 ; y < dy ; y++ )
73 ptr->at(y).resize( rowsize ) ;
76 image =
Image( ptr , type ) ;
82 return !empty() && m_type.valid() ;
92 return m_ptr.get() == nullptr ;
103 if( empty() )
return 0U ;
104 return size_of(m_ptr) ;
119 if( m_ptr.get() !=
nullptr && m_ptr.unique() )
120 return const_pointer_cast<ImageBuffer>(m_ptr) ;
122 return shared_ptr<ImageBuffer>(
new ImageBuffer ) ;
128 throw ReadError( help_text ) ;
131 G_DEBUG(
"Gr::Image::read: type=[" << image_type_in <<
"]" ) ;
133 shared_ptr<ImageBuffer> ptr = image.
recycle() ;
135 if( image_type_in.
isPnm() )
138 imagebuffer::resize( image_buffer , image_type_in.
dx() , image_type_in.
dy() , image_type_in.
channels() ) ;
139 ImageData image_data( image_buffer , image_type_in.
dx() , image_type_in.
dy() , image_type_in.
channels() ) ;
141 reader.
decode( image_data , in ) ;
150 throw ReadError( help_text ) ;
152 image =
Image( ptr , image_type_in ) ;
157 return receiveImp(
false , channel , image , type_str ) ;
162 return receiveImp(
true , channel , image , type_str ) ;
167 shared_ptr<ImageBuffer> ptr = image.
recycle() ;
169 std::vector<char> & buffer = ptr->at(0) ;
171 bool ok = peek ? channel.
peek(buffer,&type_str) : channel.
receive(buffer,&type_str) ;
176 image =
Image( ptr , image_type ) ;
bool isRaw() const
Returns true if a raw image type.
bool receive(std::vector< char > &buffer, std::string *type_p=nullptr, G::EpochTime *time_p=nullptr)
Does a read for new publish()ed data.
shared_ptr< const ImageBuffer > ptr() const
Returns the image data shared pointer.
A holder for image data, having eight bits per sample and one or three channels.
size_t size() const
Returns the image data total size.
int channels() const
Returns the number of channels.
static bool receive(G::PublisherSubscription &channel, Image &, std::string &type_str)
Reads a publication channel into an image.
void clear()
Clears the image as if default constructed.
An encapsulation of image type, including width, height and number of channels, with support for a st...
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
An easy-to-use combination of a G::PublisherChannel object and a single G::PublisherSubscriber.
bool peek(std::vector< char > &buffer, std::string *type_p=nullptr, G::EpochTime *time_p=nullptr)
Does a receive() but without requiring a publication event.
static ImageType raw(int dx, int dy, int channels)
Factory function for a raw image type.
bool empty() const
Returns true if constructed with no image data.
A class holding shared read-only image data (Gr::ImageBuffer) and its associated image type (Gr::Imag...
size_t size() const
Returns the product of dx, dy and channels.
static void read(std::istream &, Image &, const std::string &exception_help_text=std::string())
Reads an input stream into an image. Throws on error.
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...
int dy() const
Returns the image height.
void decode(ImageData &out, const G::Path &in)
Decodes a pnm file into an image. Throws on error.
bool valid() const
Returns !empty() && type().valid().
size_t rowsize() const
Returns the product of dx and channels.
ImageType type() const
Returns the image type.
const ImageBuffer & data() const
Returns the image data.
int dx() const
Returns the image width.
shared_ptr< ImageBuffer > recycle()
Creates a new shared pointer that can be deposited into a new Image, but if the current Image is not ...
A static interface for reading portable-anymap (pnm) files.
Image()
Default constructor for an empty() image with an in-valid() type() and a null ptr().
static bool peek(G::PublisherSubscription &channel, Image &, std::string &type_str)
A variant on receive() that does a channel peek().
bool isPnm() const
Returns true if a pnm image type.