Exemplo n.º 1
0
/* kdGetImageATX, kdGetImageFromStreamATX: Read and decode an image from a file or stream, returning a decoded image object. */
KD_API KDImageATX KD_APIENTRY kdGetImageATX(const KDchar *pathname, KDint format, KDint flags)
{
    KDFile *file = kdFopen(pathname, "rb");
    if(file == KD_NULL)
    {
        kdSetError(KD_EIO);
        return KD_NULL;
    }
    KDImageATX image = kdGetImageFromStreamATX(file, format, flags);
    kdFclose(file);
    return image;
}
Exemplo n.º 2
0
KDbool Image::initWithImageData ( const KDubyte* pData, KDint32 nDataLen )
{
	KDbool	bRet = false;

	do
	{
		CC_BREAK_IF ( !pData || nDataLen <= 0 );

		KDubyte*	pUnpackedData = nullptr;
		KDint		nUnpackedLen = 0;

		// detecgt and unzip the compress file
		if ( ZipUtils::isCCZBuffer ( pData, nDataLen ) )
		{
			nUnpackedLen = ZipUtils::inflateCCZBuffer ( pData, nDataLen, &pUnpackedData );
		}
		else if ( ZipUtils::isGZipBuffer ( pData, nDataLen ) )
		{
			nUnpackedLen = ZipUtils::inflateMemory ( const_cast<KDubyte*> ( pData ), nDataLen, &pUnpackedData );
		}
		else
		{
			pUnpackedData = const_cast<KDubyte*> ( pData );
			nUnpackedLen = nDataLen;
		}

		KDFile*		pFile   = kdFmemopen ( pUnpackedData, nUnpackedLen, "rb" );
		KDoff		uOffset = kdFtell ( pFile );
		KDImageATX	pImage  = KD_NULL;

		do 
		{
			Texture2D::PixelFormat  eFormat = Texture2D::getDefaultAlphaPixelFormat ( );
			
			if ( eFormat == Texture2D::PixelFormat::AUTO )
			{			
				pImage = kdGetImageInfoFromStreamATX ( pFile );
				CC_BREAK_IF ( !pImage );

				KDint  nFormat = kdGetImageIntATX ( pImage, KD_IMAGE_FORMAT_ATX );
				KDint  nBpp    = kdGetImageIntATX ( pImage, KD_IMAGE_BITSPERPIXEL_ATX );
				KDint  nAlpha  = kdGetImageIntATX ( pImage, KD_IMAGE_ALPHA_ATX );
				KDint  nType   = kdGetImageIntATX ( pImage, KD_IMAGE_TYPE );

				m_eFileType = (Format) nType;
		
				kdFreeImageATX ( pImage );
				kdFseek ( pFile, uOffset, KD_SEEK_SET );

				switch ( nFormat )
				{
					case KD_IMAGE_FORMAT_COMPRESSED_ATX :	
						if ( ( nType == KD_IMAGE_TYPE_PVR   && !Configuration::getInstance ( )->supportsPVRTC ( ) ) ||
							 ( nType == KD_IMAGE_TYPE_S3TC  && !Configuration::getInstance ( )->supportsS3TC  ( ) ) ||
							 ( nType == KD_IMAGE_TYPE_ATITC && !Configuration::getInstance ( )->supportsATITC ( ) ) ||
							 ( nType == KD_IMAGE_TYPE_ETC   && !Configuration::getInstance ( )->supportsETC   ( ) ) )
						{
							eFormat = Texture2D::PixelFormat::RGBA8888;
						}
						else
						{
							eFormat = Texture2D::PixelFormat::COMPRESSED;
						}
						break;

					case KD_IMAGE_FORMAT_ALPHA8_ATX:
						eFormat = Texture2D::PixelFormat::A8;
						break;

					case KD_IMAGE_FORMAT_LUMINANCE_ATX:
						eFormat = Texture2D::PixelFormat::I8;
						break;

					case KD_IMAGE_FORMAT_LUMALPHA_ATX:
						eFormat = Texture2D::PixelFormat::AI88;
						break;

					case KD_IMAGE_FORMAT_PALETTED_ATX:
						eFormat = Texture2D::PixelFormat::RGB5A1;
						break;

					case KD_IMAGE_FORMAT_RGB_ATX:
						if ( nBpp == 24 || nBpp == 48 )
						{
							eFormat = Texture2D::PixelFormat::RGB888;
						}
						else
						{
							eFormat = Texture2D::PixelFormat::RGB565;
						}						
						break;

					case KD_IMAGE_FORMAT_RGBA_ATX:
						if ( nBpp == 16 )
						{
							eFormat = ( nAlpha == 4 ) ? Texture2D::PixelFormat::RGBA4444 : Texture2D::PixelFormat::RGB5A1;
						}
						else
						{
							eFormat = Texture2D::PixelFormat::RGBA8888;
						}
						break;

					default:
						break; break;
				}
			}
	
			pImage = kdGetImageFromStreamATX ( pFile, (KDint) eFormat, KD_IMAGE_FLAG_PREMULTIPLIED_ALPHA );
			CC_BREAK_IF ( !pImage );
			
			m_eRenderFormat				= (Texture2D::PixelFormat) kdGetImageIntATX ( pImage, KD_IMAGE_FORMAT_ATX );
			m_nWidth					= kdGetImageIntATX ( pImage, KD_IMAGE_WIDTH_ATX );
			m_nHeight					= kdGetImageIntATX ( pImage, KD_IMAGE_HEIGHT_ATX );
			m_bHasPremultipliedAlpha	= kdGetImageIntATX ( pImage, KD_IMAGE_FLAG_PREMULTIPLIED_ALPHA ) ? KD_TRUE : KD_FALSE;
			m_nDataLen					= kdGetImageIntATX ( pImage, KD_IMAGE_DATASIZE_ATX );
			m_nNumberOfMipmaps			= kdGetImageIntATX ( pImage, KD_IMAGE_LEVELS_ATX );
			m_pData						= (KDubyte*) kdGetImagePointerATX ( pImage, KD_IMAGE_POINTER_BUFFER_ATX );
			m_pImageAtx					= pImage;

			for ( KDint i = 0; i < m_nNumberOfMipmaps; i++ )
			{
				m_aMipmaps [ i ].address = m_pData + kdGetImageLevelIntATX ( pImage, KD_IMAGE_BUFFEROFFSET_ATX, i );
				m_aMipmaps [ i ].len     = kdGetImageLevelIntATX ( pImage, KD_IMAGE_DATASIZE_ATX, i );
			}

			bRet = true;

		} while ( 0 );

		if ( pFile )
		{
			kdFclose ( pFile );
		}
	
		if ( pUnpackedData != pData )
		{
			kdFree ( pUnpackedData );
		}

	} while ( 0 );

	return bRet;
}