inlib  1.2.0
Typedefs | Functions
inlib::image Namespace Reference

Typedefs

typedef unsigned char *(* file_reader )(std::ostream &, const std::string &, unsigned int &, unsigned int &, unsigned int &)
typedef unsigned char *(* reader )(FILE *, unsigned int &, unsigned int &)
typedef bool(* writer )(FILE *, unsigned char *, unsigned int, unsigned int)

Functions

unsigned char * concatenate (unsigned char **a_buffers, unsigned int a_w, unsigned int a_h, unsigned int a_bpp, unsigned int a_cols, unsigned int a_rows, unsigned int a_bw, unsigned int a_bh, unsigned char a_bc, unsigned int &a_rw, unsigned int &a_rh)
unsigned char * concatenate (std::ostream &a_out, const std::vector< std::string > &a_files, unsigned int a_cols, unsigned int a_rows, unsigned int a_bw, unsigned int a_bh, unsigned char a_bc, file_reader a_file_reader, unsigned int &a_w, unsigned int &a_h, unsigned int &a_bpp)
bool convert (std::ostream &a_out, const std::string &a_sin, reader a_reader, const std::string &a_sout, writer a_writer)

Typedef Documentation

typedef unsigned char*(* inlib::image::file_reader)(std::ostream &, const std::string &, unsigned int &, unsigned int &, unsigned int &)

Definition at line 191 of file image.

typedef unsigned char*(* inlib::image::reader)(FILE *, unsigned int &, unsigned int &)

Definition at line 319 of file image.

typedef bool(* inlib::image::writer)(FILE *, unsigned char *, unsigned int, unsigned int)

Definition at line 320 of file image.


Function Documentation

unsigned char* inlib::image::concatenate ( unsigned char **  a_buffers,
unsigned int  a_w,
unsigned int  a_h,
unsigned int  a_bpp,
unsigned int  a_cols,
unsigned int  a_rows,
unsigned int  a_bw,
unsigned int  a_bh,
unsigned char  a_bc,
unsigned int &  a_rw,
unsigned int &  a_rh 
) [inline]

Definition at line 17 of file image.

                                                                        {

  // assume a_buffers has a_cols*a_rows entries.
  // a_buffers[0] is bottom-left
  // a_row=0 is bottom.

  unsigned wbw = a_w + 2*a_bw;
  unsigned hbh = a_h + 2*a_bh;

  a_rw = wbw * a_cols;
  a_rh = hbh * a_rows;

  //printf("debug : %d %d\n",a_rw,a_rh);

  // on big concatenated image the below may fail :
  unsigned char* rb = new unsigned char[a_rh*a_rw*a_bpp];
  if(!rb) {a_rw = 0;a_rh = 0;return 0;}

  unsigned int wbw3 = wbw*a_bpp;
  unsigned int aw3 = a_w*a_bpp;
  
  //copy tiles :  
  unsigned int index = 0;
  for(unsigned int j=0;j<a_rows;j++) {
    for(unsigned int i=0;i<a_cols;i++) {
      unsigned char* tile = a_buffers[index];

      for(unsigned int r=0;r<hbh;r++) {
        unsigned char* pos = rb + (j*hbh+r)*a_rw*a_bpp + i*wbw*a_bpp;
        ::memset(pos,a_bc,wbw3);
      }

      for(unsigned int r=0;r<a_h;r++) {
        unsigned char* pos = rb + (j*hbh+r+a_bh)*a_rw*a_bpp + 
          (i*wbw+a_bw)*a_bpp;
        unsigned char* ptile = tile+r*aw3;        
        for(unsigned int c=0;c<aw3;c++) *pos++ = *ptile++;
      }

      index++;
    }
  }

  return rb;
}
unsigned char* inlib::image::concatenate ( std::ostream &  a_out,
const std::vector< std::string > &  a_files,
unsigned int  a_cols,
unsigned int  a_rows,
unsigned int  a_bw,
unsigned int  a_bh,
unsigned char  a_bc,
file_reader  a_file_reader,
unsigned int &  a_w,
unsigned int &  a_h,
unsigned int &  a_bpp 
) [inline]

Definition at line 193 of file image.

                                                       {
  // a_files[0] is cols=0, rows=0
  // a_files[1] is cols=1, rows=0
  // a_files[2] is cols=2, rows=0
  // ...
  // and row=0 is bottom of big image.

  unsigned int number = a_cols*a_rows;
  if(number!=a_files.size()) {
    a_out << "inlib::image::concatenate :"
              << " bad number of files. " << number << " expected."
              << std::endl;
    a_bpp = a_w = a_h = 0;
    return 0;
  }
  if(a_files.empty()) {
    a_out << "inlib::image::concatenate :"
          << " list of files is empty."
          << std::endl;
    a_bpp = a_w = a_h = 0;
    return 0;
  }

  unsigned int w1 = 0;
  unsigned int h1 = 0;
  unsigned int bpp1 = 0;
   
  typedef unsigned char* buffer_t;
  buffer_t* bs = new buffer_t[number];
 {for(unsigned int i=0;i<number;i++) {bs[i] = 0;}}

  bool read_failed = false;
  unsigned int index = 0;
  for(unsigned int j=0;j<a_rows;j++) {
    for(unsigned int i=0;i<a_cols;i++) {
      const std::string& file = a_files[index];
      unsigned int w,h,bpp;
      unsigned char* b = a_file_reader(a_out,file,w,h,bpp);
      if(!b) {
        a_out << "inlib::image::concatenate :"
              << " can't read " << file << " expected."
              << std::endl;
        read_failed = true;
        break;
      }
      if(!index) {
        w1 = w;
        h1 = h;
        bpp1 = bpp;
      } else {
        if(w!=w1) {
          a_out << "inlib::image::concatenate :"
                << " file " << file
                << " does not have same width image as the first file one."
                << " (" << w << "," << w1 << ")."
                << std::endl;
          delete [] b;
          read_failed = true;
          break;
        }
        if(h!=h1) {
          a_out << "inlib::image::concatenate :"
                << " file " << file
                << " does not have same height image as the first file one."
                << " (" << h << "," << h1 << ")."
                << std::endl;
          delete [] b;
          read_failed = true;
          break;
        }
        if(bpp!=bpp1) {
          a_out << "inlib::image::concatenate :"
                << " file " << file
                << " does not have same bytes per pixel as the first file one."
                << " (" << h << "," << h1 << ")."
                << std::endl;
          delete [] b;
          read_failed = true;
          break;
        }
      }

      bs[index] = b;

      index++;
    }
    if(read_failed) break;
  }

  if(read_failed) {
    {for(unsigned int i=0;i<number;i++) {delete [] bs[i];}}
    a_bpp = a_w = a_h = 0;
    return 0;
  }

  unsigned int wa,ha;
  unsigned char* ba = 
    inlib::image::concatenate(bs,w1,h1,bpp1,
                              a_cols,a_rows,a_bw,a_bh,a_bc,wa,ha);
  if(!ba) {
    a_out << "inlib::image::concatenate :"
          << " failed to concatenate all buffers."
          << std::endl;
    {for(unsigned int i=0;i<number;i++) {delete [] bs[i];}}
    a_bpp = a_w = a_h = 0;
    return 0;
  } 

  {for(unsigned int i=0;i<number;i++) {delete [] bs[i];}}

  a_w = wa;
  a_h = ha;
  a_bpp = bpp1;
  return ba;
}
bool inlib::image::convert ( std::ostream &  a_out,
const std::string &  a_sin,
reader  a_reader,
const std::string &  a_sout,
writer  a_writer 
) [inline]

Definition at line 322 of file image.

                                    {
  
  FILE* fin = ::fopen(a_sin.c_str(),"rb");
  if(!fin) {
    a_out << "inlib::image::convert :"
          << " can't open " << a_sin
          << std::endl;
    return false;
  }

  unsigned int w,h;
  unsigned char* b = a_reader(fin,w,h);
  if(!b) {
    a_out << "inlib::image::convert :"
          << " can't read " << a_sin
          << std::endl;
    ::fclose(fin);
    return false;
  }
  ::fclose(fin);

  FILE* fout = ::fopen(a_sout.c_str(),"wb");
  if(!fout) {
    a_out << "inlib::image::convert :"
          << " can't open " << a_sout
          << std::endl;
    delete [] b;
    return false;
  }

  if(!a_writer(fout,b,w,h)) {
    a_out << "inlib::image::convert :"
          << " can't write " << a_sout
          << std::endl;
    ::fclose(fout);
    delete [] b;
    return false;
  }
  ::fclose(fout);
  delete [] b;
  return true;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines