Exemplo n.º 1
0
void hdr_writer_t::do_write_image( const boost::filesystem::path& p,
				const image::const_image_view_t& view,
				const adobe::dictionary_t& params) const
{
    std::auto_ptr<OIIO::ImageOutput> out( OIIO::ImageOutput::create( filesystem::file_string( p)));

    if( !out.get())
		throw exception( "Write HDR: Can't open output file");

    OIIO::ImageSpec spec( view.width(), view.height(), 3, OIIO::TypeDesc::FLOAT);
	add_common_attributes( spec);

    if( !out->open( filesystem::file_string( p), spec))
		throw exception( "Write HDR: Can't open output file");

    char *pixels = (char *) boost::gil::interleaved_view_get_raw_data( view);
    std::size_t xstride = 4 * sizeof( float);
    std::size_t ystride = xstride * view.width();

    if( !out->write_image( OIIO::TypeDesc::FLOAT, pixels, xstride, ystride, OIIO::AutoStride))
		throw exception( "Write HDR: Can't write pixels");

    if( !out->close())
		throw exception( "Write HDR: Can't close file");
}
Exemplo n.º 2
0
void cineon_writer_t::write_pixels( std::ofstream& out, const image::const_image_view_t& view) const
{
    std::vector<boost::uint32_t> buffer( view.width());

    for( int y = 0; y < view.height(); ++y)
    {
		image::const_image_view_t::x_iterator src_it( view.row_begin( y));
	
		for( int x = 0; x < view.width(); ++x)
		{
			boost::uint32_t pix = 0;
	
			float r = boost::gil::get_color( *src_it, boost::gil::red_t());
			float g = boost::gil::get_color( *src_it, boost::gil::green_t());
			float b = boost::gil::get_color( *src_it, boost::gil::blue_t());
	
			boost::uint32_t ri = adobe::clamp( r, 0.0f, 1.0f) * 1023;
			boost::uint32_t gi = adobe::clamp( g, 0.0f, 1.0f) * 1023;
			boost::uint32_t bi = adobe::clamp( b, 0.0f, 1.0f) * 1023;
			pix = ( ri << 22) | ( gi << 12) | ( bi << 2);
	
			buffer[x] = IECore::asBigEndian( pix);
			++src_it;
		}
	
		// write vector
		out.write( reinterpret_cast<char *>( &buffer[0]), view.width() * sizeof( boost::uint32_t));
		
		if ( out.fail())
			throw exception( "error while writting image");
    }
}
Exemplo n.º 3
0
void tga_writer_t::do_write_image( const boost::filesystem::path& p,
				const image::const_image_view_t& view,
				const adobe::dictionary_t& params) const
{
    int channels    = adobe::get_value( params, adobe::name_t( "channels")).cast<int>();
    int compress    = adobe::get_value( params, adobe::name_t( "compress")).cast<int>();
    
    std::auto_ptr<OIIO::ImageOutput> out( OIIO::ImageOutput::create( filesystem::file_string( p)));

    if( !out.get())
		throw exception( "Write TGA: Can't open output file");

    if( channels)
		channels = 3;
    else
		channels = 4;

    OIIO::ImageSpec spec( view.width(), view.height(), channels, OIIO::TypeDesc::UINT8);
	add_common_attributes( spec);

	switch( compress)
	{
		case none_compression:
		break;
		
		case rle_compression:
			spec.attribute( "compression", "rle");
		break;
		
		default:
			throw unsupported_image( "unsupported compression");
	}

    if( !out->open( filesystem::file_string( p), spec))
		throw exception( "Can't open output file");

    std::vector<image::pixel_t> scanline( view.width());

    for( int y = 0; y < view.height(); ++y)
    {
		std::copy( view.row_begin( y), view.row_end( y), scanline.begin());
		clamp( scanline.begin(), scanline.end());
	
		if( !out->write_scanline( y, 0, OIIO::TypeDesc::FLOAT, (void *) &( *scanline.begin()), sizeof( image::pixel_t)))
			throw exception( "Write image: Can't write pixels");
    }

    if( !out->close())
		throw exception( "Write image: Can't close file");
}
Exemplo n.º 4
0
void write_half_exr( const boost::filesystem::path& p, Imf::Header& header,
			    const image::const_image_view_t& view, bool write_alpha)
{
    boost::gil::rgba16f_image_t img( view.width(), view.height());
    boost::gil::copy_and_convert_pixels( view, boost::gil::view( img));

    header.channels().insert( "R", Imf::HALF);
    header.channels().insert( "G", Imf::HALF);
    header.channels().insert( "B", Imf::HALF);

    if( write_alpha)
        header.channels().insert( "A", Imf::HALF);

    Imf::FrameBuffer frameBuffer;

    char *ptr = (char *) boost::gil::interleaved_view_get_raw_data( boost::gil::view( img));
    std::size_t xstride = 4 * sizeof(half);
    std::size_t ystride = xstride * img.width();

    frameBuffer.insert( "R", Imf::Slice( Imf::HALF, ptr, xstride, ystride)); ptr += sizeof(half);
    frameBuffer.insert( "G", Imf::Slice( Imf::HALF, ptr, xstride, ystride)); ptr += sizeof(half);
    frameBuffer.insert( "B", Imf::Slice( Imf::HALF, ptr, xstride, ystride)); ptr += sizeof(half);

    if( write_alpha)
        frameBuffer.insert( "A", Imf::Slice( Imf::HALF, ptr, xstride, ystride));

    Imf::OutputFile out_file( p.external_file_string().c_str(), header);
    out_file.setFrameBuffer( frameBuffer);
    out_file.writePixels( img.height());
}
Exemplo n.º 5
0
void png_writer_t::do_write_image( const boost::filesystem::path& p,
				const image::const_image_view_t& view,
				const adobe::dictionary_t& params) const
{
    int channels = adobe::get_value( params, adobe::name_t( "channels")).cast<int>();

    std::auto_ptr<OpenImageIO::ImageOutput> out( OpenImageIO::ImageOutput::create( p.external_file_string()));

    if( !out.get())
	throw std::runtime_error( "Write PNG: Can't open output file");

    if( channels)
	channels = 3;
    else
	channels = 4;

    OpenImageIO::ImageSpec spec( view.width(), view.height(), channels, TypeDesc::UINT8);
    spec.quant_dither = 0.0f;

    if( !out->open( p.external_file_string(), spec))
	throw( std::runtime_error( "Can't open output file"));
    
    std::vector<image::pixel_t> scanline( view.width());

    for( int y = 0; y < view.height(); ++y)
    {
	std::copy( view.row_begin( y), view.row_end( y), scanline.begin());
	apply_gamma( 1.0f / 2.2f, scanline.begin(), scanline.end());
	clamp( scanline.begin(), scanline.end());

	if( !out->write_scanline( y, 0, TypeDesc::FLOAT, (void *) &( *scanline.begin()), sizeof( image::pixel_t)))
	    throw( std::runtime_error( "Write image: Can't write pixels"));
    }

    if( !out->close())
	throw std::runtime_error( "Write image: Can't close file");
}
Exemplo n.º 6
0
void png_writer_t::do_write_image( const boost::filesystem::path& p,
									const image::const_image_view_t& view,
                                    const core::dictionary_t& params) const
{
    int channels = core::get<int>( params, core::name_t( "channels"));

    std::auto_ptr<OIIO::ImageOutput> out( OIIO::ImageOutput::create( filesystem::file_string( p)));

    if( !out.get())
		throw exception( "Write PNG: Can't open output file");

    if( channels)
		channels = 3;
    else
		channels = 4;

    OIIO::ImageSpec spec( view.width(), view.height(), channels, OIIO::TypeDesc::UINT8);
	add_common_attributes( spec);

    if( !out->open( filesystem::file_string( p), spec))
		throw exception( "Can't open output file");
    
    std::vector<image::pixel_t> scanline( view.width());

    for( int y = 0; y < view.height(); ++y)
    {
		std::copy( view.row_begin( y), view.row_end( y), scanline.begin());
		clamp( scanline.begin(), scanline.end());

		if( !out->write_scanline( y, 0, OIIO::TypeDesc::FLOAT, (void *) &( *scanline.begin()), sizeof( image::pixel_t)))
		    throw exception( "Write image: Can't write pixels");
    }

    if( !out->close())
		throw exception( "Write image: Can't close file");
}
Exemplo n.º 7
0
void write_half_rgb_exr( Imf::OStream& os, Imf::Header& header, const image::const_image_view_t& view)
{
    boost::gil::rgba16f_image_t img( view.width(), view.height());
    boost::gil::copy_and_convert_pixels( view, boost::gil::view( img));

    header.channels().insert( "R", Imf::HALF);
    header.channels().insert( "G", Imf::HALF);
    header.channels().insert( "B", Imf::HALF);

    Imf::FrameBuffer frameBuffer;

    char *ptr = (char *) boost::gil::interleaved_view_get_raw_data( boost::gil::view( img));
    std::size_t xstride = 4 * sizeof(half);
    std::size_t ystride = xstride * img.width();

    frameBuffer.insert( "R", Imf::Slice( Imf::HALF, ptr, xstride, ystride)); ptr += sizeof(half);
    frameBuffer.insert( "G", Imf::Slice( Imf::HALF, ptr, xstride, ystride)); ptr += sizeof(half);
    frameBuffer.insert( "B", Imf::Slice( Imf::HALF, ptr, xstride, ystride)); ptr += sizeof(half);

    Imf::OutputFile out_file( os, header);
    out_file.setFrameBuffer( frameBuffer);
    out_file.writePixels( img.height());
}
Exemplo n.º 8
0
void cineon_writer_t::do_write_image( const boost::filesystem::path& p,
				const image::const_image_view_t& view,
				const adobe::dictionary_t& params) const
{
    std::ofstream out( filesystem::file_cstring( p), std::ios_base::binary);

    if( !out)
		throw exception( "can't open file");

    CineonFileInformation fi = { 0 };

    fi.magic = IECore::asBigEndian( (boost::uint32_t) 0x802a5fd7);
    fi.section_header_length = 0;
    fi.industry_header_length = 0;
    fi.variable_header_length = 0;

    strcpy( fi.version, "V4.5");

    strncpy( (char *) fi.file_name, filesystem::file_cstring( p), sizeof( fi.file_name ));

    // compute the current date and time
    boost::posix_time::ptime localTime = boost::posix_time::second_clock::local_time();
    boost::gregorian::date date = localTime.date();
    boost::posix_time::time_duration time = localTime.time_of_day();

    #if defined(WIN32) || defined(WIN64)
        sprintf_s( fi.creation_date, sizeof( fi.creation_date ), "%04d-%02d-%02d",
                static_cast<int>( date.year()), static_cast<int>( date.month()),
                static_cast<int>( date.day()));

        sprintf_s( fi.creation_time, sizeof( fi.creation_time ), "%02d:%02d:%02d", time.hours(),
                    time.minutes(), time.seconds());
    #else
        snprintf( fi.creation_date, sizeof( fi.creation_date ), "%04d-%02d-%02d",
                static_cast<int>( date.year()), static_cast<int>( date.month()),
                static_cast<int>( date.day()));

        snprintf( fi.creation_time, sizeof( fi.creation_time ), "%02d:%02d:%02d", time.hours(),
                    time.minutes(), time.seconds());
    #endif

    CineonImageInformation ii = { 0 };
    ii.orientation = 0;
    ii.channel_count = 3;

    for( int c = 0; c < 8; ++c)
    {
		CineonImageInformationChannelInformation& ci = ii.channel_information[c];
	
		ci.byte_0 = 0;
	
		if( c == 0)
			ci.byte_1 = 1;
	
		if( c == 1)
			ci.byte_1 = 2;
	
		if( c == 2)
			ci.byte_1 = 3;
	
	
		ci.byte_1 = 0;
		ci.bpp = 10;
	
		if( c < 3)
		{
			ci.pixels_per_line = IECore::asBigEndian( (boost::uint32_t) view.width());
			ci.lines_per_image = IECore::asBigEndian( (boost::uint32_t) view.height());
	
			ci.min_data_value	= IECore::asBigEndian( 0.0f);
			ci.min_quantity	= IECore::asBigEndian( 0.0f);
			ci.max_data_value	= IECore::asBigEndian( 1023.0f);
			ci.max_quantity	= IECore::asBigEndian( 2.046f);
		}
		else
		{
			ci.pixels_per_line = 0;
			ci.lines_per_image = 0;
		}
    }

    CineonImageDataFormatInformation idfi = { 0 };
    idfi.interleave = 0;               // pixel interleave
    idfi.packing = 5;                  // 32 bit left-aligned with 2 waste bits
    idfi.data_signed = 0;              // unsigned data
    idfi.sense = 0;                    // positive image sense
    idfi.eol_padding = 0;              // no end-of-line padding
    idfi.eoc_padding = 0;              // no end-of-data padding

    // \todo Complete filling in this structure
    CineonImageOriginationInformation ioi = { 0 };
    ioi.x_offset = 0;                  // could be dataWindow min.x
    ioi.y_offset = 0;                  // could be dataWindow min.y
    ioi.gamma = IECore::asBigEndian( (boost::uint32_t) 0x7f800000);

    // compute data offsets
    fi.image_data_offset = IECore::asBigEndian( (boost::uint32_t) 1024);

    // file size is 1024 (header) + image data size
    // image data size is 32 bits times width*height
    fi.total_file_size = 1024 + sizeof( boost::uint32_t) * view.width() * view.height();
    fi.total_file_size = IECore::asBigEndian( fi.total_file_size);

    out.write( reinterpret_cast<char *>( &fi), sizeof(fi));
    if ( out.fail())
		throw exception( "can't write header");

    out.write(reinterpret_cast<char *>(&ii),   sizeof(ii));
    if ( out.fail())
		throw exception( "can't write header");

    out.write(reinterpret_cast<char *>(&idfi), sizeof(idfi));
    if ( out.fail())
		throw exception( "can't write header");

    out.write(reinterpret_cast<char *>(&ioi),  sizeof(ioi));
    if ( out.fail())
		throw exception( "can't write header");

    write_pixels( out, view);
}