Exemplo n.º 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;
}
Exemplo n.º 2
0
Arquivo: zoic.cpp Projeto: jonntd/zoic
// 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;
}
void OpenImageIOReaderPlugin::getClipPreferences( OFX::ClipPreferencesSetter& clipPreferences )
{
	ReaderPlugin::getClipPreferences( clipPreferences );

	const std::string filename( getAbsoluteFirstFilename() );

	// if no filename
	if( filename.size() == 0 )
	{
		clipPreferences.setClipBitDepth( *this->_clipDst, OFX::eBitDepthFloat );
		clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGBA );
		clipPreferences.setPixelAspectRatio( *this->_clipDst, 1.0 );
		return;
	}

	OpenImageIO::ImageInput *in = OpenImageIO::ImageInput::create( filename );

	if( !in )
	{
		BOOST_THROW_EXCEPTION( exception::Unknown()
			<< exception::user( "OIIO Reader: " + in->geterror () )
			<< exception::filename( filename ) );
	}

	OpenImageIO::ImageSpec spec;
	if( ! in->open( filename, spec ) )
	{
		BOOST_THROW_EXCEPTION( exception::Unknown()
			<< exception::user( "OIIO Reader: " + in->geterror () )
			<< exception::filename( filename ) );
	}

	if( getExplicitBitDepthConversion() == eParamReaderBitDepthAuto )
	{
		OFX::EBitDepth bd = OFX::eBitDepthNone;
		switch( spec.format.basetype )
		{
			//			case TypeDesc::UCHAR:
			case OpenImageIO::TypeDesc::UINT8:
			//			case TypeDesc::CHAR:
			case OpenImageIO::TypeDesc::INT8:
				bd = OFX::eBitDepthUByte;
				break;
			case OpenImageIO::TypeDesc::HALF:
			//			case TypeDesc::USHORT:
			case OpenImageIO::TypeDesc::UINT16:
			//			case TypeDesc::SHORT:
			case OpenImageIO::TypeDesc::INT16:
				bd = OFX::eBitDepthUShort;
				break;
			//			case TypeDesc::UINT:
			case OpenImageIO::TypeDesc::UINT32:
			//			case TypeDesc::INT:
			case OpenImageIO::TypeDesc::INT32:
			//			case TypeDesc::ULONGLONG:
			case OpenImageIO::TypeDesc::UINT64:
			//			case TypeDesc::LONGLONG:
			case OpenImageIO::TypeDesc::INT64:
			case OpenImageIO::TypeDesc::FLOAT:
			case OpenImageIO::TypeDesc::DOUBLE:
				bd = OFX::eBitDepthFloat;
				break;
			case OpenImageIO::TypeDesc::STRING:
			case OpenImageIO::TypeDesc::PTR:
			case OpenImageIO::TypeDesc::LASTBASE:
			case OpenImageIO::TypeDesc::UNKNOWN:
			case OpenImageIO::TypeDesc::NONE:
			default:
			{
				in->close();
				BOOST_THROW_EXCEPTION( exception::ImageFormat()
									   << exception::user("bad input format") );
			}
		}
		clipPreferences.setClipBitDepth( *this->_clipDst, bd );
	}

	if( getExplicitChannelConversion() == eParamReaderChannelAuto )
	{
		switch( spec.nchannels )
		{
			case 1 :
				clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentAlpha );
				break;
			case 3 :
				if( OFX::getImageEffectHostDescription()->supportsPixelComponent( OFX::ePixelComponentRGB ) )
				{
					clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGB );
				}
				else
				{
					clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGBA );
				}
				break;
			case 4 :
				clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGBA );
				break;
			default:
				clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGBA );
				break;
		}
	}

	clipPreferences.setPixelAspectRatio( *this->_clipDst, 1.0 );
	in->close();
}