Пример #1
0
template <typename T> static
void im_load_color(struct jpeg_decompress_struct *cinfo, bob::io::base::array::interface& b) {
  const bob::io::base::array::typeinfo& info = b.type();

  long unsigned int frame_size = info.shape[1] * info.shape[2];
  T *element_r = static_cast<T*>(b.ptr());
  T *element_g = element_r+frame_size;
  T *element_b = element_g+frame_size;

  const int row_stride = cinfo->output_width * cinfo->output_components;
  JSAMPROW buffer_pptr[1];
  boost::shared_array<JSAMPLE> buffer(new JSAMPLE[row_stride]);
  buffer_pptr[0] = buffer.get();
  while (cinfo->output_scanline < cinfo->output_height) {
    jpeg_read_scanlines(cinfo, buffer_pptr, 1);
    if (cinfo->output_components == 3)
      imbuffer_to_rgb<T>(info.shape[2], reinterpret_cast<T*>(buffer_pptr[0]), element_r, element_g, element_b);
    else
      cmyk_imbuffer_to_rgb<T>(info.shape[2], reinterpret_cast<T*>(buffer_pptr[0]), element_r, element_g, element_b, cinfo->saw_Adobe_marker);

    element_r += cinfo->output_width;
    element_g += cinfo->output_width;
    element_b += cinfo->output_width;
  }
}
Пример #2
0
void bob::io::image::TIFFFile::read(bob::io::base::array::interface& buffer, size_t index) {
  if (m_newfile)
    throw std::runtime_error("uninitialized image file cannot be read");

  if (!buffer.type().is_compatible(m_type)) buffer.set(m_type);

  if (index != 0)
    throw std::runtime_error("cannot read image with index > 0 -- there is only one image in an image file");

  if(!buffer.type().is_compatible(m_type)) buffer.set(m_type);
  im_load(m_filename, buffer);
}
Пример #3
0
template <typename T> static
void im_load_gray(struct jpeg_decompress_struct *cinfo, bob::io::base::array::interface& b) {
  const bob::io::base::array::typeinfo& info = b.type();

  T *element = static_cast<T*>(b.ptr());
  const int row_stride = info.shape[1];
  JSAMPROW buffer_pptr[1];
  while (cinfo->output_scanline < cinfo->image_height) {
    buffer_pptr[0] = element;
    jpeg_read_scanlines(cinfo, buffer_pptr, 1);
    element += row_stride;
  }
}
Пример #4
0
static void im_save_gray(const bob::io::base::array::interface& b, struct jpeg_compress_struct *cinfo) {
  const bob::io::base::array::typeinfo& info = b.type();

  const T* element = static_cast<const T*>(b.ptr());

  // pointer to a single row  (JSAMPLE is a typedef to unsigned char or char)
  JSAMPROW row_pointer[1];
  int row_stride = info.shape[1]; // JSAMPLEs per row in image_buffer
  while(cinfo->next_scanline < cinfo->image_height) {
    row_pointer[0] = const_cast<T*>(element);
    jpeg_write_scanlines(cinfo, row_pointer, 1);
    element += row_stride;
  }
}
Пример #5
0
static void im_load(const std::string& filename, bob::io::base::array::interface& b)
{
  // 1. TIFF file opening
  boost::shared_ptr<TIFF> in_file = make_cfile(filename.c_str(), "r");

  // 2. Read content
  const bob::io::base::array::typeinfo& info = b.type();
  if(info.dtype == bob::io::base::array::t_uint8) {
    if(info.nd == 2) im_load_gray<uint8_t>(in_file, b);
    else if( info.nd == 3) im_load_color<uint8_t>(in_file, b);
    else {
      boost::format m("TIFF: cannot read object of type `%s' from file `%s'");
      m % info.str() % filename;
      throw std::runtime_error(m.str());
    }
  }
  else if(info.dtype == bob::io::base::array::t_uint16) {
    if(info.nd == 2) im_load_gray<uint16_t>(in_file, b);
    else if( info.nd == 3) im_load_color<uint16_t>(in_file, b);
    else {
      boost::format m("TIFF: cannot read object of type `%s' from file `%s'");
      m % info.str() % filename;
      throw std::runtime_error(m.str());
    }
  }
  else {
    boost::format m("TIFF: cannot read object of type `%s' from file `%s'");
    m % info.str() % filename;
    throw std::runtime_error(m.str());
  }
}
Пример #6
0
size_t bob::io::image::JPEGFile::append(const bob::io::base::array::interface& buffer) {
  if (m_newfile) {
    im_save(m_filename, buffer);
    m_type = buffer.type();
    m_newfile = false;
    m_length = 1;
    return 0;
  }

  throw std::runtime_error("image files only accept a single array");
}
Пример #7
0
static void im_save (const std::string& filename, const bob::io::base::array::interface& array) {
  const bob::io::base::array::typeinfo& info = array.type();

  // 1. JPEG structures
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  cinfo.err = jpeg_std_error(&jerr);
  jerr.error_exit = my_error_exit;
  jerr.output_message = my_output_message;
  // set image name as client data; used for warning and error messages
  cinfo.client_data = const_cast<char*>(filename.c_str());
  jpeg_create_compress(&cinfo);

  // 2. JPEG opening
  boost::shared_ptr<std::FILE> out_file = make_cfile(filename.c_str(), "wb");
  jpeg_stdio_dest(&cinfo, out_file.get());

  // 3. Set compression parameters
  cinfo.image_height = (info.nd == 2 ? info.shape[0] : info.shape[1]);
  cinfo.image_width = (info.nd == 2 ? info.shape[1] : info.shape[2]);
  cinfo.input_components = (info.nd == 2 ? 1 : 3);
  cinfo.in_color_space = (info.nd == 2 ? JCS_GRAYSCALE : JCS_RGB); // colorspace of input image
  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, s_jpeg_quality, TRUE);

  // 4.
  jpeg_start_compress(&cinfo, TRUE);

  // Writes content
  if(info.dtype == bob::io::base::array::t_uint8) {

    if(info.nd == 2) im_save_gray<uint8_t>(array, &cinfo);
    else if(info.nd == 3) {
      if(info.shape[0] != 3) throw std::runtime_error("color image does not have 3 planes on 1st. dimension");
      im_save_color<uint8_t>(array, &cinfo);
    }
    else {
      boost::format m("the image array to be written at file `%s' has a number of dimensions this jpeg codec has no support for: %s");
      m % filename % info.str();
      throw std::runtime_error(m.str());
    }
  }
  else {
    boost::format m("the image array to be written at file `%s' has a data type this jpeg codec has no support for: %s");
    m % filename % info.str();
    throw std::runtime_error(m.str());
  }

  // 6.
  jpeg_finish_compress(&cinfo);

  // 7.
  jpeg_destroy_compress(&cinfo);
}
Пример #8
0
static void im_save(const std::string& filename, const bob::io::base::array::interface& array)
{
  // 1. Open the file
  boost::shared_ptr<TIFF> out_file = make_cfile(filename.c_str(), "w");

  // 2. Set the image information here:
  const bob::io::base::array::typeinfo& info = array.type();
  const int height = (info.nd == 2 ? info.shape[0] : info.shape[1]);
  const int width = (info.nd == 2 ? info.shape[1] : info.shape[2]);
  TIFFSetField(out_file.get(), TIFFTAG_IMAGELENGTH, height);
  TIFFSetField(out_file.get(), TIFFTAG_IMAGEWIDTH, width);
  TIFFSetField(out_file.get(), TIFFTAG_BITSPERSAMPLE, (info.dtype == bob::io::base::array::t_uint8 ? 8 : 16));
  TIFFSetField(out_file.get(), TIFFTAG_SAMPLESPERPIXEL, (info.nd == 2 ? 1 : 3));

  TIFFSetField(out_file.get(), TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  TIFFSetField(out_file.get(), TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
  if(info.nd == 3)
    TIFFSetField(out_file.get(), TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  TIFFSetField(out_file.get(), TIFFTAG_PHOTOMETRIC, (info.nd == 2 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB));

  // 3. Writes content
  if(info.dtype == bob::io::base::array::t_uint8) {
    if(info.nd == 2) im_save_gray<uint8_t>(array, out_file);
    else if(info.nd == 3) {
      if(info.shape[0] != 3)
        throw std::runtime_error("color image does not have 3 planes on 1st. dimension");
      im_save_color<uint8_t>(array, out_file);
    }
    else {
      boost::format m("TIFF: cannot write object of type `%s' to file `%s'");
      m % info.str() % filename;
      throw std::runtime_error(m.str());
    }
  }
  else if(info.dtype == bob::io::base::array::t_uint16) {
    if(info.nd == 2) im_save_gray<uint16_t>(array, out_file);
    else if(info.nd == 3) {
      if(info.shape[0] != 3)
        throw std::runtime_error("color image does not have 3 planes on 1st. dimension");
      im_save_color<uint16_t>(array, out_file);
    }
    else {
      boost::format m("TIFF: cannot write object of type `%s' to file `%s'");
      m % info.str() % filename;
      throw std::runtime_error(m.str());
    }
  }
  else {
    boost::format m("TIFF: cannot write object of type `%s' to file `%s'");
    m % info.str() % filename;
    throw std::runtime_error(m.str());
  }
}
Пример #9
0
static void im_save_color(const bob::io::base::array::interface& b, boost::shared_ptr<TIFF> out_file)
{
  const bob::io::base::array::typeinfo& info = b.type();
  const size_t height = info.shape[1];
  const size_t width = info.shape[2];
  const size_t frame_size = height * width;

  // Allocate array for a row as an RGB-like array
  boost::shared_array<T> row(new T[3*width*height]);
  unsigned char* row_pointer = reinterpret_cast<unsigned char*>(row.get());

  // pointer to a single row (tiff_bytep is a typedef to unsigned char or char)
  const T *element_r = static_cast<const T*>(b.ptr());
  const T *element_g = element_r + frame_size;
  const T *element_b = element_g + frame_size;
  rgb_to_imbuffer(frame_size, element_r, element_g, element_b, row.get());

  // Write the information to the file
  const size_t data_size = 3 * height * width * sizeof(T);
  TIFFWriteEncodedStrip(out_file.get(), 0, row_pointer, data_size);
}
Пример #10
0
static void im_save_gray(const bob::io::base::array::interface& b, boost::shared_ptr<TIFF> out_file)
{
  const bob::io::base::array::typeinfo& info = b.type();
  const size_t height = info.shape[0];
  const size_t width = info.shape[1];

  unsigned char* row_pointer = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(b.ptr()));
  const size_t data_size = height * width * sizeof(T);

  // Write the information to the file
  TIFFWriteEncodedStrip(out_file.get(), 0, row_pointer, data_size);
}
Пример #11
0
static void im_save_color(const bob::io::base::array::interface& b, struct jpeg_compress_struct *cinfo) {
  const bob::io::base::array::typeinfo& info = b.type();

  long unsigned int frame_size = info.shape[1] * info.shape[2];

  const T *element_r = static_cast<const T*>(b.ptr());
  const T *element_g = element_r + frame_size;
  const T *element_b = element_g + frame_size;

  // pointer to a single row  (JSAMPLE is a typedef to unsigned char or char)
  boost::shared_array<JSAMPLE> row(new JSAMPLE[3*info.shape[2]]);
  JSAMPROW array_ptr[1];
  array_ptr[0] = row.get();
  int row_color_stride = info.shape[2]; // JSAMPLEs per row in image_buffer
  while(cinfo->next_scanline < cinfo->image_height) {
    rgb_to_imbuffer(row_color_stride, element_r, element_g, element_b, reinterpret_cast<T*>(array_ptr[0]));
    jpeg_write_scanlines(cinfo, array_ptr, 1);
    element_r += row_color_stride;
    element_g += row_color_stride;
    element_b += row_color_stride;
  }
}
Пример #12
0
static void im_load(const std::string& filename, bob::io::base::array::interface& b) {
  // 1. JPEG structures
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;
  cinfo.err = jpeg_std_error(&jerr);
  jerr.error_exit = my_error_exit;
  jerr.output_message = my_output_message;
  // set image name as client data; used for warning and error messages
  cinfo.client_data = const_cast<char*>(filename.c_str());
  jpeg_create_decompress(&cinfo);

  // 2. JPEG file opening
  boost::shared_ptr<std::FILE> in_file = make_cfile(filename.c_str(), "rb");
  jpeg_stdio_src(&cinfo, in_file.get());

  // 3. Read header
  jpeg_read_header(&cinfo, TRUE);

  // 4. Set parameters for decompression
  if (cinfo.output_components == 4){
    // assure to get CMYK output
    cinfo.out_color_space = JCS_CMYK;
  }

  // 5. Start decompression and get information
  jpeg_start_decompress(&cinfo);

  // 6. Read content
  const bob::io::base::array::typeinfo& info = b.type();
  if(info.dtype == bob::io::base::array::t_uint8) {
    if(info.nd == 2) im_load_gray<uint8_t>(&cinfo, b);
    else if( info.nd == 3) im_load_color<uint8_t>(&cinfo, b);
    else {
      boost::format m("the image in file `%s' has a number of dimensions this jpeg codec has no support for: %s");
      m % filename % info.str();
      throw std::runtime_error(m.str());
    }
  }
  else {
    boost::format m("the image in file `%s' has a data type this jpeg codec has no support for: %s");
    m % filename % info.str();
    throw std::runtime_error(m.str());
  }

  // 7. Finish decompression
  jpeg_finish_decompress(&cinfo);

  // 8. Release JPEG decompression object
  jpeg_destroy_decompress(&cinfo);
}
Пример #13
0
void bob::io::audio::Writer::append(const bob::io::base::array::interface& data) {

  if (!m_opened) {
    boost::format m("audio writer for file `%s' is closed and cannot be written to");
    m % m_filename;
    throw std::runtime_error(m.str());
  }

  const bob::io::base::array::typeinfo& type = data.type();

  if (type.dtype != bob::io::base::array::t_float64) {
    boost::format m("input data type = `%s' does not conform to the specified input specifications (1 or 2D array of type `%s'), while writing data to file `%s'");
    m % type.str() % m_typeinfo.item_str() % m_filename;
    throw std::runtime_error(m.str());
  }

  if (type.nd == 1) { //appends single sample
    blitz::TinyVector<int,1> shape;
    shape = type.shape[0];
    blitz::Array<double,1> tmp(const_cast<double*>(static_cast<const double*>(data.ptr())), shape, blitz::neverDeleteData);
    this->append(tmp);
  }

  else if (type.nd == 2) { //appends multiple frames
    blitz::TinyVector<int,2> shape;
    shape = type.shape[0], type.shape[1];
    blitz::Array<double,2> tmp(const_cast<double*>(static_cast<const double*>(data.ptr())), shape, blitz::neverDeleteData);
    this->append(tmp);
  }

  else {
    boost::format m("input data type information = `%s' does not conform to the specified input specifications (1 or 2D array of type = `%s'), while writing data to file `%s'");
    m % type.str() % m_typeinfo.item_str() % m_filename;
  }

}
Пример #14
0
template <typename T> static
void im_load_gray(boost::shared_ptr<TIFF> in_file, bob::io::base::array::interface& b)
{
  const bob::io::base::array::typeinfo& info = b.type();
  const size_t height = info.shape[0];
  const size_t width = info.shape[1];

  // Read in the possibly multiple strips
  tsize_t strip_size = TIFFStripSize(in_file.get());
  tstrip_t n_strips = TIFFNumberOfStrips(in_file.get());

  unsigned long buffer_size = n_strips * strip_size;
  boost::shared_array<unsigned char> buffer_(new unsigned char[buffer_size]);
  unsigned char* buffer = buffer_.get();
  if(buffer == 0) throw std::runtime_error("TIFF: error while getting the color buffer");

  tsize_t result;
  tsize_t image_offset = 0;
  for(tstrip_t strip_count=0; strip_count<n_strips; ++strip_count)
  {
    if((result = TIFFReadEncodedStrip(in_file.get(), strip_count, buffer+image_offset, strip_size)) == -1)
      throw std::runtime_error("TIFF: error in function TIFFReadEncodedStrip()");
    image_offset += result;
  }

  //Comment just to document
  //PHOTOMETRIC_PALETTE: In this model, a color is described with a single component. The value of the component is used as an index into the red, green and blue curves in the ColorMap field to retrieve an RGB triplet that defines the color. When PhotometricInterpretation=3

  // Deal with photometric interpretations
  uint16 photo = PHOTOMETRIC_MINISBLACK;
  if(TIFFGetField(in_file.get(), TIFFTAG_PHOTOMETRIC, &photo) == 0 || (photo != PHOTOMETRIC_MINISBLACK && photo != PHOTOMETRIC_MINISWHITE && photo != PHOTOMETRIC_PALETTE)){
    throw std::runtime_error("TIFF: error in function TIFFGetField()");
  }

  if(photo == PHOTOMETRIC_MINISWHITE)
  {
    // Flip bits
    for(unsigned long count=0; count<buffer_size; ++count)
      buffer[count] = ~buffer[count];
  }

  // Deal with fillorder
  uint16 fillorder = FILLORDER_MSB2LSB;
  TIFFGetField(in_file.get(), TIFFTAG_FILLORDER, &fillorder);

  if(fillorder != FILLORDER_MSB2LSB) {
    // We need to swap bits -- ABCDEFGH becomes HGFEDCBA
    for(unsigned long count=0; count<buffer_size; ++count)
    {
      unsigned char tempbyte = 0;
      if(buffer[count] & 128) tempbyte += 1;
      if(buffer[count] & 64) tempbyte += 2;
      if(buffer[count] & 32) tempbyte += 4;
      if(buffer[count] & 16) tempbyte += 8;
      if(buffer[count] & 8) tempbyte += 16;
      if(buffer[count] & 4) tempbyte += 32;
      if(buffer[count] & 2) tempbyte += 64;
      if(buffer[count] & 1) tempbyte += 128;
      buffer[count] = tempbyte;
    }
  }

  // Copy to output array
  T *element = reinterpret_cast<T*>(b.ptr());
  T *b_in = reinterpret_cast<T*>(buffer);
  memcpy(element, b_in, height*width*sizeof(T));
}
Пример #15
0
template <typename T> static
void im_load_color(boost::shared_ptr<TIFF> in_file, bob::io::base::array::interface& b)
{
  const bob::io::base::array::typeinfo& info = b.type();
  const size_t height = info.shape[1];
  const size_t width = info.shape[2];
  const size_t frame_size = height*width;
  const size_t row_stride = width;
  const size_t row_color_stride = 3*width;

  // Read in the possibly multiple strips
  tsize_t strip_size = TIFFStripSize(in_file.get());
  tstrip_t n_strips = TIFFNumberOfStrips(in_file.get());

  unsigned long buffer_size = n_strips * strip_size;
  boost::shared_array<unsigned char> buffer_(new unsigned char[buffer_size]);
  unsigned char* buffer = buffer_.get();
  if(buffer == 0) throw std::runtime_error("TIFF: error while getting the color buffer");

  tsize_t result;
  tsize_t image_offset = 0;
  for(tstrip_t strip_count=0; strip_count<n_strips; ++strip_count)
  {
    if((result = TIFFReadEncodedStrip(in_file.get(), strip_count, buffer+image_offset, strip_size)) == -1)
      throw std::runtime_error("TIFF: error in function TIFFReadEncodedStrip()");

    image_offset += result;
  }

  // Deal with photometric interpretations
  uint16 photo = PHOTOMETRIC_RGB;
  if(TIFFGetField(in_file.get(), TIFFTAG_PHOTOMETRIC, &photo) == 0 || photo != PHOTOMETRIC_RGB)
    throw std::runtime_error("TIFF: error in function TIFFGetField()");

  // Deal with fillorder
  uint16 fillorder = FILLORDER_MSB2LSB;
  TIFFGetField(in_file.get(), TIFFTAG_FILLORDER, &fillorder);

  if(fillorder != FILLORDER_MSB2LSB) {
    // We need to swap bits -- ABCDEFGH becomes HGFEDCBA
    for(unsigned long count=0; count<(unsigned long)image_offset; ++count)
    {
      unsigned char tempbyte = 0;
      if(buffer[count] & 128) tempbyte += 1;
      if(buffer[count] & 64) tempbyte += 2;
      if(buffer[count] & 32) tempbyte += 4;
      if(buffer[count] & 16) tempbyte += 8;
      if(buffer[count] & 8) tempbyte += 16;
      if(buffer[count] & 4) tempbyte += 32;
      if(buffer[count] & 2) tempbyte += 64;
      if(buffer[count] & 1) tempbyte += 128;
      buffer[count] = tempbyte;
    }
  }

  // Read the image (one row at a time)
  // This can deal with interlacing
  T *element_r = reinterpret_cast<T*>(b.ptr());
  T *element_g = element_r + frame_size;
  T *element_b = element_g + frame_size;
  unsigned char *row_pointer = buffer;
  // Loop over the rows
  for(size_t y=0; y<height; ++y)
  {
    imbuffer_to_rgb(row_stride, reinterpret_cast<T*>(row_pointer), element_r, element_g, element_b);
    element_r += row_stride;
    element_g += row_stride;
    element_b += row_stride;
    row_pointer += row_color_stride * sizeof(T);
  }
}