示例#1
0
unsigned char * LoadPVRBuffer( const char * fileName, int & width, int & height )
{
	width = 0;
	height = 0;

	MemBufferFile bufferFile( fileName );

	MemBuffer buffer = bufferFile.ToMemBuffer( );

	if ( buffer.Length < ( int )( sizeof( OVR_PVR_HEADER ) ) )
	{
		LOG( "Invalid PVR file" );
		buffer.FreeData( );
		return NULL;
	}

	const OVR_PVR_HEADER & header = *( OVR_PVR_HEADER * )buffer.Buffer;
	if ( header.Version != 0x03525650 )
	{
		LOG( "Invalid PVR file version" );
		buffer.FreeData( );
		return NULL;
	}

	int format = 0;
	switch ( header.PixelFormat )
	{
 		case 578721384203708274llu:	format = Texture_RGBA;		break;
		default:
			LOG( "Unknown PVR texture format %llu, size %ix%i", header.PixelFormat, width, height );
			buffer.FreeData( );
			return NULL;
	}

	// skip the metadata
	const UInt32 startTex = sizeof( OVR_PVR_HEADER ) + header.MetaDataSize;
	if ( ( startTex < sizeof( OVR_PVR_HEADER ) ) || ( startTex >= buffer.Length ) )
	{
		LOG( "Invalid PVR header sizes" );
		buffer.FreeData( );
		return NULL;
	}

	size_t mipSize = GetOvrTextureSize( format, header.Width, header.Height );
	
	const int outBufferSizeBytes = buffer.Length - startTex;

	if ( mipSize > outBufferSizeBytes )
	{
		buffer.FreeData();
		return NULL;
	}

	width = header.Width;
	height = header.Height;

	// skip the metadata
	unsigned char * outBuffer = ( unsigned char * )malloc( outBufferSizeBytes );
	memcpy( outBuffer, ( unsigned char * )buffer.Buffer + startTex, outBufferSizeBytes );
	buffer.FreeData( );
	
	return outBuffer;
}