g4tools  5.4.0
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
tools::image::iterator_reader Class Referenceabstract
Inheritance diagram for tools::image::iterator_reader:
Inheritance graph
[legend]
Collaboration diagram for tools::image::iterator_reader:
Collaboration graph
[legend]

Public Member Functions

virtual unsigned char * read_image (unsigned int &a_w, unsigned int &a_h, unsigned int &a_bpp)
 
virtual bool begin_file (std::string &)=0
 
virtual bool end_file (const std::string &)=0
 
 iterator_reader (std::ostream &a_out, unsigned int a_limit)
 
virtual ~iterator_reader ()
 
 iterator_reader (const iterator_reader &a_from)
 
iterator_readeroperator= (const iterator_reader &a_from)
 
void add_reader (ireader *a_reader)
 
- Public Member Functions inherited from tools::image::iterator
virtual ~iterator ()
 
virtual unsigned int entries () const =0
 
virtual bool next ()=0
 
virtual bool rewind ()=0
 
virtual unsigned int index () const =0
 

Protected Member Functions

unsigned char * _open (const image::ireader &a_reader, const std::string &a_path, unsigned int &a_w, unsigned int &a_h, unsigned int &a_bpp)
 
void _clean ()
 
void _copy (const std::vector< ireader * > &a_from)
 

Protected Attributes

std::ostream & m_out
 
unsigned int m_limit
 
std::vector< ireader * > m_readers
 

Detailed Description

Definition at line 279 of file image.

Constructor & Destructor Documentation

◆ iterator_reader() [1/2]

tools::image::iterator_reader::iterator_reader ( std::ostream &  a_out,
unsigned int  a_limit 
)
inline

Definition at line 309 of file image.

310  :m_out(a_out)
311  ,m_limit(a_limit)
312  {
313 #ifdef TOOLS_MEM
314  mem::increment(s_class().c_str());
315 #endif
316  }

◆ ~iterator_reader()

virtual tools::image::iterator_reader::~iterator_reader ( )
inlinevirtual

Definition at line 317 of file image.

317  {
318  _clean();
319 #ifdef TOOLS_MEM
320  mem::decrement(s_class().c_str());
321 #endif
322  }

◆ iterator_reader() [2/2]

tools::image::iterator_reader::iterator_reader ( const iterator_reader a_from)
inline

Definition at line 324 of file image.

325  :iterator(a_from)
326  ,m_out(a_from.m_out)
327  ,m_limit(a_from.m_limit)
328  {
329 #ifdef TOOLS_MEM
330  mem::increment(s_class().c_str());
331 #endif
332  _copy(a_from.m_readers);
333  }

Member Function Documentation

◆ _clean()

void tools::image::iterator_reader::_clean ( )
inlineprotected

Definition at line 407 of file image.

407  {
408  std::vector<ireader*>::iterator it;
409  for(it=m_readers.begin();it!=m_readers.end();) {
410  ireader* obj = *it;
411  it = m_readers.erase(it);
412  delete obj;
413  }
414  }

◆ _copy()

void tools::image::iterator_reader::_copy ( const std::vector< ireader * > &  a_from)
inlineprotected

Definition at line 415 of file image.

415  {
416  _clean();
417  tools_vforcit(ireader*,a_from,it) m_readers.push_back((*it)->copy());
418  }

◆ _open()

unsigned char* tools::image::iterator_reader::_open ( const image::ireader a_reader,
const std::string &  a_path,
unsigned int &  a_w,
unsigned int &  a_h,
unsigned int &  a_bpp 
)
inlineprotected

Definition at line 345 of file image.

348  {
349  // permit to put in common code for jpeg and png.
350 
351  unsigned char* buffer = 0;
352 
353  if(m_limit) {
354  if(!a_reader.infos(m_out,a_path,a_w,a_h,a_bpp)) {
355  m_out << "tools::image::iterator_reader::_open :"
356  << " for file " << sout(a_path)
357  << " ireader::infos failed."
358  << std::endl;
359  a_w = 0;a_h = 0;a_bpp = 0;
360  return 0;
361  }
362  if((a_w*a_h*a_bpp)>m_limit) {
363 
364  //unsigned int sw;
365  //unsigned int sh;
366  //float aspect = float(w)/float(h);
367  //if(aspect>=1) {
368  // sh = 2;
369  // sw = aspect*sh;
370  //} else {
371  // sw = 2;
372  // sh = sw/aspect;
373  //}
374 
375  unsigned int sw = 2;
376  unsigned int sh = 2;
377  while(true) {
378  if(((2*sw)*(2*sh)*a_bpp)>m_limit) break;
379  sw *=2;
380  sh *=2;
381  }
382 
383  unsigned int sx = (a_w-sw)/2;
384  unsigned int sy = (a_h-sh)/2;
385 
386  buffer = a_reader.read_part
387  (m_out,a_path,sx,sy,sw,sh,a_w,a_h,a_bpp);
388  } else {
389  buffer = a_reader.read(m_out,a_path,a_w,a_h,a_bpp);
390  }
391  } else {
392  buffer = a_reader.read(m_out,a_path,a_w,a_h,a_bpp);
393  }
394 
395  if(!buffer) {
396  m_out << "tools::image::iterator_reader::_open :"
397  << " for file " << sout(a_path)
398  << " read failed."
399  << std::endl;
400  a_w = 0;a_h = 0;a_bpp = 0;
401  return 0;
402  }
403 
404  return buffer;
405  }

◆ add_reader()

void tools::image::iterator_reader::add_reader ( ireader a_reader)
inline

Definition at line 341 of file image.

341  {
342  m_readers.push_back(a_reader); //take ownership.
343  }

◆ begin_file()

virtual bool tools::image::iterator_reader::begin_file ( std::string &  )
pure virtual

◆ end_file()

virtual bool tools::image::iterator_reader::end_file ( const std::string &  )
pure virtual

◆ operator=()

iterator_reader& tools::image::iterator_reader::operator= ( const iterator_reader a_from)
inline

Definition at line 334 of file image.

334  {
335  if(&a_from==this) return *this;
336  m_limit = a_from.m_limit;
337  _copy(a_from.m_readers);
338  return *this;
339  }

◆ read_image()

virtual unsigned char* tools::image::iterator_reader::read_image ( unsigned int &  a_w,
unsigned int &  a_h,
unsigned int &  a_bpp 
)
inlinevirtual

Implements tools::image::iterator.

Definition at line 288 of file image.

289  {
290 
291  std::string file;
292  if(begin_file(file)) {
293  tools_vforcit(image::ireader*,m_readers,it) {
294  if((*it)->is(file)) {
295  unsigned char* buffer = _open(*(*it),file,a_w,a_h,a_bpp);
296  end_file(file);
297  return buffer;
298  }
299  }
300  end_file(file);
301  }
302  a_w = 0;a_h = 0;a_bpp = 0;
303  return 0;
304  }

Member Data Documentation

◆ m_limit

unsigned int tools::image::iterator_reader::m_limit
protected

Definition at line 421 of file image.

◆ m_out

std::ostream& tools::image::iterator_reader::m_out
protected

Definition at line 420 of file image.

◆ m_readers

std::vector<ireader*> tools::image::iterator_reader::m_readers
protected

Definition at line 422 of file image.


The documentation for this class was generated from the following file:
tools::image::iterator_reader::_copy
void _copy(const std::vector< ireader * > &a_from)
Definition: image:415
tools::image::iterator_reader::begin_file
virtual bool begin_file(std::string &)=0
tools::image::iterator_reader::_clean
void _clean()
Definition: image:407
tools::image::iterator_reader::m_readers
std::vector< ireader * > m_readers
Definition: image:422
tools::image::iterator_reader::_open
unsigned char * _open(const image::ireader &a_reader, const std::string &a_path, unsigned int &a_w, unsigned int &a_h, unsigned int &a_bpp)
Definition: image:345
tools::image::iterator_reader::m_limit
unsigned int m_limit
Definition: image:421
tools::image::iterator_reader::m_out
std::ostream & m_out
Definition: image:420
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::image::iterator_reader::end_file
virtual bool end_file(const std::string &)=0