コード例 #1
0
void ToMayaImageConverter::writeDepth( MImage &image, FloatVectorDataPtr channelData ) const
{
	assert( channelData );
	ConstImagePrimitivePtr toConvert = runTimeCast<const ImagePrimitive>( srcParameter()->getValidatedValue() );
	assert( toConvert );

	unsigned width, height;
	MStatus s = image.getSize( width, height );
	assert( s );

	const Imath::Box2i &dataWindow = toConvert->getDataWindow();
	const Imath::Box2i &displayWindow = toConvert->getDisplayWindow();

	unsigned int dataWidth = dataWindow.size().x + 1;
	unsigned int dataHeight = dataWindow.size().y + 1;

	Imath::V2i dataOffset = dataWindow.min - displayWindow.min ;

	boost::multi_array_ref< const float, 2 > src( &channelData->readable()[0], boost::extents[dataHeight][dataWidth] );

	std::vector<float> depth;
	depth.resize( height * width, 0 );
	boost::multi_array_ref< float, 2 > dst( &depth[0], boost::extents[ height ][ width ] );

	for ( unsigned x = 0; x < dataWidth; x++ )
	{
		for ( unsigned y = 0; y < dataHeight; y++ )
		{
			/// Vertical flip, to match Maya
			dst[ ( height - 1 ) - ( y + dataOffset.y ) ][ x + dataOffset.x ] = src[y][x];
		}
	}

	s = image.setDepthMap( &depth[0], width, height );
	assert( s );
	assert( image.depth() );
#ifndef NDEBUG
	unsigned depthWidth = 0, depthHeight = 0;
	s = image.getDepthMapSize( depthWidth, depthHeight );
	assert( s );
	assert( depthWidth == width );
	assert( depthHeight == height );
#endif
}