inlib  1.2.0
Functions
inlib::ivrgb Namespace Reference

Functions

bool write_short (FILE *a_file, unsigned short a_val)
bool write_file (FILE *a_file, unsigned char *a_buffer, unsigned int a_w, unsigned int a_h, unsigned int a_bpp)
bool read_short (FILE *a_file, unsigned short &a_val)
unsigned char * read_file (FILE *a_file, unsigned int &a_w, unsigned int &a_h, unsigned int &a_bpp)
bool is (const std::string &a_file)
unsigned char * read (std::ostream &a_out, const std::string &a_file, unsigned int &a_width, unsigned int &a_height, unsigned int &a_bpp)
bool 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, const std::string &a_file)

Function Documentation

bool inlib::ivrgb::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,
const std::string &  a_file 
) [inline]

Definition at line 251 of file ivrgb.

                                                 {
  unsigned int wa,ha,bppa;
  unsigned char* ba = 
    image::concatenate(a_out,a_files,a_cols,a_rows,
                       a_bw,a_bh,a_bc,
                       read,wa,ha,bppa);
  if(!ba) {
    a_out << "inlib::ivrgb::concatenate :"
          << " failed to concatenate all files."
          << std::endl;
    delete [] ba;
    return false;
  } 

  FILE* file = ::fopen(a_file.c_str(),"wb");
  if(!file) {
    a_out << "inlib::ivrgb::concatenate :"
          << " can't open " << file << " for writing."
          << std::endl;
    delete [] ba;
    return false;
  }

#ifdef INLIB_IVRGB_DEBUG
  ::printf("debug : inlib::ivrgb::concatenate : %d %d\n",wa,ha);
#endif

  if(!write_file(file,ba,wa,ha,bppa)) {
    a_out << "inlib::ivrgb::concatenate :"
          << " can't write " << a_file 
          << " w " << wa
          << " h " << ha
          << "."
          << std::endl;
    delete [] ba;
    ::fclose(file);
    return false;
  }

  delete [] ba;
  ::fclose(file);
  return true;
}
bool inlib::ivrgb::is ( const std::string &  a_file) [inline]

Definition at line 189 of file ivrgb.

                                       {
  FILE* file = ::fopen(a_file.c_str(),"rb");
  if(!file) return false; //if file does not exist, then it is not a ivrgb !
  unsigned short imagic;
  if(!read_short(file,imagic)) {::fclose(file);return false;}
  ::fclose(file);
  if(imagic!=0x01da) return false;
  return true;
}
unsigned char* inlib::ivrgb::read ( std::ostream &  a_out,
const std::string &  a_file,
unsigned int &  a_width,
unsigned int &  a_height,
unsigned int &  a_bpp 
) [inline]

Definition at line 199 of file ivrgb.

                                                                                                                                       {
  if(!is(a_file)) {
    a_out << "inlib::ivrgb::read :"
          << " file " << a_file << " is not an inventor rgb image file."
          << std::endl;
    a_width = 0;
    a_height = 0;
    a_bpp = 0;
    return 0;
  } 

  FILE* file = ::fopen(a_file.c_str(),"rb");
  if(!file) {
    a_out << "inlib::ivrgb::read :"
          << " can't open " << a_file
          << std::endl;
    a_width = 0;
    a_height = 0;
    a_bpp = 0;
    return 0;
  }

  unsigned int w,h,bpp;
  unsigned char* buffer = read_file(file,w,h,bpp);

  ::fclose(file);

  if(!buffer) {
    a_out << "inlib::ivrgb::read :"
              << " problem reading " << a_file
              << std::endl;
    a_width = 0;
    a_height = 0;
    a_bpp = 0;
    return 0;
  }

  a_width = w;
  a_height = h;
  a_bpp = bpp;
  return buffer;  
}
unsigned char* inlib::ivrgb::read_file ( FILE *  a_file,
unsigned int &  a_w,
unsigned int &  a_h,
unsigned int &  a_bpp 
) [inline]

Definition at line 92 of file ivrgb.

                                                                                                      {
  // the returned buffer must be delete with "delete []".
  unsigned short imagic;
  if(!read_short(a_file,imagic)) {a_w = a_h = 0;a_bpp=0;return 0;}
  if(imagic!=0x01da) {
#ifdef INLIB_IVRGB_DEBUG
    ::printf("debug : inlib::ivrgb::read_file : bad magic\n");
#endif
    a_w = a_h = 0;a_bpp=0;return 0;
  }
  unsigned short raw;
  if(!read_short(a_file,raw)) {a_w = a_h = 0;a_bpp=0;return 0;}
  if(raw!=0x0001) {
#ifdef INLIB_IVRGB_DEBUG
    ::printf("debug : inlib::ivrgb::read_file : bad raw\n");
#endif
    a_w = a_h = 0;a_bpp=0;return 0;
  }

  unsigned short dim;
  if(!read_short(a_file,dim)) {a_w = a_h = 0;a_bpp=0;return 0;}
  //if((dim!=0x0002)&&(dim!=0x0003)) {a_w = a_h = 0;a_bpp=0;return 0;}
  if(dim!=0x0003) {
#ifdef INLIB_IVRGB_DEBUG
    ::printf("debug : inlib::ivrgb::read_file : bad dim\n");
#endif
    a_w = a_h = 0;a_bpp=0;return 0;
  }

  unsigned short w,h,nrcomponents;
  if(!read_short(a_file,w)) {a_w = a_h = 0;a_bpp=0;return 0;}
  if(!read_short(a_file,h)) {a_w = a_h = 0;a_bpp=0;return 0;}
  if(!read_short(a_file,nrcomponents)) {a_w = a_h = 0;a_bpp=0;return 0;}

#ifdef INLIB_IVRGB_DEBUG
  ::printf("debug : inlib::ivrgb::read_file : w %d h %d nrc %d\n",
    w,h,nrcomponents);
#endif

  unsigned char buf[500];
  if(::fread(buf,1,500,a_file)!=500) {
#ifdef INLIB_IVRGB_DEBUG
    ::printf("debug : inlib::ivrgb::read_file : read 500 header failed\n");
#endif
    a_bpp = a_w = a_h = 0;
    return 0;
  }

  unsigned char* b = new unsigned char[w*h*nrcomponents];
  if(!b) {
#ifdef INLIB_IVRGB_DEBUG
    ::printf("debug : inlib::ivrgb::read_file : can't alloc mem %d\n",
      w*h*nrcomponents);
#endif
    a_bpp = a_w = a_h = 0;
    return 0;
  }

  unsigned char* line = new unsigned char[w];
  if(!line) {
    delete [] b;
    a_bpp = a_w = a_h = 0;
    return 0;
  }

  for (unsigned int c = 0; c < nrcomponents; c++) {
    for (unsigned int y = 0; y < h; y++) {
      if(::fread(line,1,w,a_file)!=w) {
#ifdef INLIB_IVRGB_DEBUG
        ::printf("debug : inlib::ivrgb::read_file : read line %d %d failed.\n",y,c);
#endif
        delete [] line;
        delete [] b;
        a_bpp = a_w = a_h = 0;
        return 0;
      }
      for (unsigned int x = 0; x < w; x++) {
        b[(x + y * w) * nrcomponents + c] = line[x];
      }
    }
  }

  delete [] line;
  a_w = w;
  a_h = h;
  a_bpp = nrcomponents;
  return b;
}
bool inlib::ivrgb::read_short ( FILE *  a_file,
unsigned short &  a_val 
) [inline]

Definition at line 78 of file ivrgb.

                                                           {
  unsigned char b[2];
  if(::fread(b,2,1,a_file)!=1) {
#ifdef INLIB_IVRGB_DEBUG
    ::printf("debug : inlib::ivrgb::read_short : failed\n");
#endif
    a_val = 0;return false;
  }
  a_val = b[1];
  a_val += b[0] <<8;
  //::printf("debug : read_short %d %x : %d %d\n",a_val,a_val,b[0],b[1]);
  return true;
}
bool inlib::ivrgb::write_file ( FILE *  a_file,
unsigned char *  a_buffer,
unsigned int  a_w,
unsigned int  a_h,
unsigned int  a_bpp 
)

Definition at line 32 of file ivrgb.

                                                                      {

  if(!write_short(a_file,0x01da)) return false;
  if(!write_short(a_file,0x0001)) return false;

  unsigned int nrcomponents = a_bpp;
  if(!write_short(a_file,0x0003)) return false;

  if(!write_short(a_file,(unsigned short)a_w)) return false;
  if(!write_short(a_file,(unsigned short)a_h)) return false;
  if(!write_short(a_file,(unsigned short)nrcomponents)) return false;

  unsigned char buf[500];
  ::memset(buf,0,500);
  buf[7] = 255; // set maximum pixel value to 255
  ::strcpy((char*)buf+8,"http://www.coin3d.org");
  if(::fwrite(buf,1,500,a_file)!=500) {
#ifdef INLIB_IVRGB_DEBUG
    ::printf("debug : inlib::ivrgb::write_file : write 500 failed\n");
#endif
    return false;
  }

  unsigned char* line = new unsigned char[a_w];

  for (unsigned int c = 0; c < nrcomponents; c++) {
    for (unsigned int y = 0; y < a_h; y++) {
      for (unsigned int x = 0; x < a_w; x++) {
        line[x] = a_buffer[(x + y * a_w) * nrcomponents + c];
      }
      if(::fwrite(line,1,a_w,a_file)!=a_w) {
#ifdef INLIB_IVRGB_DEBUG
        ::printf("debug : inlib::ivrgb::write_file : write line %d %d failed.\n",y,c);
#endif
        delete [] line;
        return false;
      }
    }
  }

  delete [] line;
  return true;
}
bool inlib::ivrgb::write_short ( FILE *  a_file,
unsigned short  a_val 
) [inline]

Definition at line 19 of file ivrgb.

                                                          {
  unsigned char b[2];
  b[0] = (unsigned char)(a_val >> 8);
  b[1] = (unsigned char)(a_val & 0xff);
  if(::fwrite(b,2,1,a_file)!=1) {
#ifdef INLIB_IVRGB_DEBUG
    ::printf("debug : inlib::ivrgb::write_short : failed\n");
#endif
    return false;
  }
  return true;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines