Exemple #1
0
//----------------------------------------------------------------------------------------------------------------------
// Open Image I/O loading routines
//----------------------------------------------------------------------------------------------------------------------
bool Image::load( const std::string &_fname  ) noexcept
{
#ifdef IMAGE_DEBUG_ON
  std::cerr<<"loading with OpenImageIO"<<std::endl;
#endif
  OpenImageIO::ImageInput *in = OpenImageIO::ImageInput::open (_fname);
  if (! in)
  {
    return false;
  }
  const OpenImageIO::ImageSpec &spec = in->spec();
  m_width = spec.width;
  m_height = spec.height;
  m_channels = spec.nchannels;
  if(m_channels==3)
    m_format=GL_RGB;
  else if(m_channels==4)
    m_format=GL_RGBA;
  m_data.reset(new unsigned char[ m_width*m_height*m_channels]);
  // this will read an flip the pixel for OpenGL
  int scanlinesize = spec.width * spec.nchannels * sizeof(m_data[0]);
  in->read_image (OpenImageIO::TypeDesc::UINT8,
  (char *)m_data.get() + (m_height-1)*scanlinesize, // offset to last
  OpenImageIO::AutoStride, // default x stride
  -scanlinesize, // special y stride
  OpenImageIO::AutoStride); // default z stride
  //in->read_image (OpenImageIO::TypeDesc::UINT8, &m_data[0]);
  in->close ();
  delete in;
  return true;
}
Exemple #2
0
// Read bokeh image
imageData* readImage(char const *bokeh_kernel_filename){

    imageData* img = new imageData;

    AiMsgInfo("Reading image using OpenImageIO: %s", bokeh_kernel_filename);

    //Search for an ImageIO plugin that is capable of reading the file ("foo.jpg"), first by
    //trying to deduce the correct plugin from the file extension, but if that fails, by opening
    //every ImageIO plugin it can find until one will open the file without error. When it finds
    //the right plugin, it creates a subclass instance of ImageInput that reads the right kind of
    //file format, and tries to fully open the file.
    OpenImageIO::ImageInput *in = OpenImageIO::ImageInput::open (bokeh_kernel_filename);
    if (! in){
        return nullptr; // Return a null pointer if we have issues
    }

    const OpenImageIO::ImageSpec &spec = in->spec();
    img->x = spec.width;
    img->y = spec.height;
    img->nchannels = spec.nchannels;

    img->pixelData.clear();
    img->pixelData.reserve(img->x * img->y * img->nchannels);
    in->read_image (OpenImageIO::TypeDesc::UINT8, &img->pixelData[0]);
    in->close ();
    delete in;

    AiMsgInfo("Image Width: %d", img->x);
    AiMsgInfo("Image Height: %d", img->y);
    AiMsgInfo("Image Channels: %d", img->nchannels);
    AiMsgInfo("Total amount of pixels to process: %d", img->x * img->y);

    if (debug == true){
        // print out raw pixel data
        for (int i = 0; i < img->x * img->y * img->nchannels; i++){
            int j = 0;
            if(img->nchannels == 3){
               if (j == 0){
                   std::cout << "[";
                    std::cout << (int)img->pixelData[i];
                   std::cout << ", ";
                    j += 1;
                }
                if (j == 1){
                    std::cout << (int)img->pixelData[i];
                    std::cout << ", ";
                    j += 1;
                }
               if (j == 2){
                    std::cout << (int)img->pixelData[i];
                    std::cout << "], ";
                    j = 0;
                }
            }

            else if(img->nchannels == 4){
                if (j == 0){
                    std::cout << "[";
                    std::cout << (int)img->pixelData[i];
                    std::cout << ", ";
                    j += 1;
                }
                if (j == 1){
                    std::cout << (int)img->pixelData[i];
                    std::cout << ", ";
                    j += 1;
                }
                if (j == 2){
                    std::cout <<  (int)img->pixelData[i];
                    std::cout << ", ";
                    j += 1;
                }
                if (j == 3){
                    std::cout << (int)img->pixelData[i];
                    std::cout << "], ";
                   j = 0;
                }
            }

        }

        std::cout << "----------------------------------------------" << std::endl;
        std::cout << "----------------------------------------------" << std::endl;
    }

    return img;
}