29 bool operator==( ImageInputConversion a , ImageInputConversion b )
31 return a.type == b.type && a.scale == b.scale && a.monochrome == b.monochrome ;
33 bool operator==(
const ImageInputTask & a ,
const ImageInputTask & b )
35 return a.m_conversion == b.m_conversion ;
43 m_converter(converter)
49 G_ASSERT( handlers() == 0U ) ;
60 for( TaskList::const_iterator task_p = m_tasks.begin() ; task_p != m_tasks.end() ; ++task_p )
62 if( (*task_p).m_handler !=
nullptr )
70 for( TaskList::iterator task_p = m_tasks.begin() ; task_p != m_tasks.end() ; ++task_p )
72 if( (*task_p).m_handler == &handler )
77 m_tasks.back().m_handler = &handler ;
82 for( TaskList::iterator task_p = m_tasks.begin() ; task_p != m_tasks.end() ; ++task_p )
84 if( (*task_p).m_handler == &handler )
85 (*task_p).m_handler = nullptr ;
92 for( TaskList::iterator task_p = m_tasks.begin() ; task_p != m_tasks.end() ; ++task_p )
94 if( (*task_p).m_handler !=
nullptr && (one_handler_p==
nullptr || (*task_p).m_handler==one_handler_p) )
95 (*task_p).m_handler->onNonImageInput( *
this , non_image_in , type_str ) ;
102 G_DEBUG(
"Gv::ImageInputSource::sendImageInput: type=[" << image_in.
type() <<
"](" << image_in.
type() <<
")" ) ;
105 for( TaskList::iterator task_p = m_tasks.begin() ; task_p != m_tasks.end() ; ++task_p )
107 if( (*task_p).m_handler !=
nullptr && (one_handler_p==
nullptr || (*task_p).m_handler==one_handler_p) )
109 (*task_p).m_conversion = (*task_p).m_handler->imageInputConversion(*
this) ;
115 for( TaskList::iterator task_p = m_tasks.begin() ; task_p != m_tasks.end() ; ++task_p )
117 if( one_handler_p !=
nullptr && (*task_p).m_handler == one_handler_p )
119 if( ! (*task_p).run(m_converter,image_in) )
122 else if( (*task_p).m_handler !=
nullptr )
124 TaskList::iterator previous = std::find( m_tasks.begin() , task_p , *task_p ) ;
125 if( previous != task_p )
126 (*task_p).m_image = (*previous).m_image ;
127 else if( ! (*task_p).run(m_converter,image_in) )
133 for( TaskList::iterator task_p = m_tasks.begin() ; task_p != m_tasks.end() ; ++task_p )
135 if( (*task_p).m_handler !=
nullptr && (one_handler_p==
nullptr || (*task_p).m_handler==one_handler_p) )
137 if( (*task_p).m_image.empty() )
138 (*task_p).m_handler->onImageInput( *
this , image_in ) ;
139 else if( (*task_p).m_image.valid() )
140 (*task_p).m_handler->onImageInput( *
this , (*task_p).m_image ) ;
148 void Gv::ImageInputSource::collectGarbage()
150 for( TaskList::iterator task_p = m_tasks.begin() ; task_p != m_tasks.end() ; )
152 if( (*task_p).m_handler ==
nullptr )
153 task_p = m_tasks.erase( task_p ) ;
163 m_channel(channel_name,lazy_open) ,
164 m_resend_timer(*this,&
ImageInput::onResendTimeout,*this) ,
175 G_ASSERT( m_channel.fd() == -1 ) ;
176 G_ASSERT( m_image.empty() ) ;
177 return m_channel.open() ;
182 return m_channel.info() ;
187 return m_channel.fd() ;
192 return receive(
false ) ;
195 bool Gv::ImageInput::receive(
bool peek )
208 if( m_image.valid() )
209 sendImageInput( m_image ) ;
210 else if( !m_image.empty() )
211 sendNonImageInput( m_image , m_type_str ) ;
218 if( m_channel.fd() != -1 )
223 G_ASSERT( m_channel.fd() == -1 ) ;
228 m_resend_timer.startTimer( 0U ) ;
229 m_resend_to = &handler ;
232 void Gv::ImageInput::onResendTimeout()
234 if( m_channel.fd() != -1 && m_resend_to !=
nullptr &&
237 ImageInputHandler * one_handler = m_resend_to ;
238 m_resend_to = nullptr ;
240 if( m_image.valid() )
241 sendImageInput( m_image , one_handler ) ;
242 else if( !m_image.empty() )
243 sendNonImageInput( m_image , m_type_str , one_handler ) ;
247 void Gv::ImageInput::onException( std::exception & )
274 monochrome(monochrome_)
287 const bool to_raw = m_conversion.type == ImageInputConversion::to_raw ;
288 const bool to_jpeg = m_conversion.type == ImageInputConversion::to_jpeg ;
289 const bool keep_type = m_conversion.type == ImageInputConversion::none ;
290 const bool is_jpeg = image_in.
type().
isJpeg() ;
291 const bool is_raw = image_in.
type().
isRaw() ;
294 G_DEBUG(
"Gv::ImageInputTask::run: conversion=[t=" << m_conversion.type <<
",m=" << m_conversion.monochrome <<
",s=" << m_conversion.scale <<
"]" ) ;
295 G_DEBUG(
"Gv::ImageInputTask::run: input=[" << image_in.
type() <<
"]" ) ;
297 if( ( keep_type || (to_jpeg && is_jpeg) || (to_raw && is_raw) ) &&
298 m_conversion.scale == 1 && (m_conversion.monochrome?1:3) == channels )
300 G_DEBUG(
"Gv::ImageInputTask::run: no-op" ) ;
304 else if( to_jpeg || (keep_type && is_jpeg) )
306 G_DEBUG(
"Gv::ImageInputTask::run: to-jpeg: s=" << m_conversion.scale <<
" m=" << m_conversion.monochrome ) ;
307 return converter.
toJpeg( image_in , m_image , m_conversion.scale , m_conversion.monochrome ) ;
309 else if( to_raw || (keep_type && is_raw) )
311 G_DEBUG(
"Gv::ImageInputTask::run: to-raw: s=" << m_conversion.scale <<
" m=" << m_conversion.monochrome ) ;
312 return converter.
toRaw( image_in , m_image , m_conversion.scale , m_conversion.monochrome ) ;
316 G_DEBUG(
"Gv::ImageInputTask::run: unsupported conversion" ) ;
bool isRaw() const
Returns true if a raw image type.
bool isJpeg() const
Returns true if a jpeg image type.
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.
bool toRaw(Image image_in, Image &image_out, int scale=1, bool monochrome_out=false)
Converts the image to raw format. Returns a false on error.
A class holding shared read-only image data (Gr::ImageBuffer) and its associated image type (Gr::Imag...
ImageType type() const
Returns the image type.
bool toJpeg(Image image_in, Image &image_out, int scale=1, bool monochrome_out=false)
Converts the image to jpeg format. Returns false on error.
An image format converter that can convert to and from the raw and jpeg formats (only), with scaling and monochrome options.
static bool peek(G::PublisherSubscription &channel, Image &, std::string &type_str)
A variant on receive() that does a channel peek().