Esempio n. 1
0
////////////////////////////////////////////////////////////////
//
// CPixelsManager::GetPixelsFormat
//
// Auto detect PNG, JPEG or PLAIN
//
////////////////////////////////////////////////////////////////
EPixelsFormatType CPixelsManager::GetPixelsFormat ( const CPixels& pixels )
{
    uint uiDataSize = pixels.GetSize ();
    const char* pData = pixels.GetData ();

    // Check if png
    if ( IsPng ( pData, uiDataSize ) )
        return EPixelsFormat::PNG;

    // Check if jpeg
    if ( IsJpeg ( pData, uiDataSize ) )
        return EPixelsFormat::JPEG;

    // Check if plain
    if ( uiDataSize >= 8 )
    {
        uint uiWidth, uiHeight;
        if ( GetPlainDimensions ( pixels, uiWidth, uiHeight ) )
            return EPixelsFormat::PLAIN;
    }

    return EPixelsFormat::UNKNOWN;
}
Bitmap PngFormatter::Decode(const vector<Byte> &data) const
{
	if (!IsPng(data))
	{
		LU_LOG_E(LU_TAG "Decode", "Not PNG data");
		return {};
	}

	PngReadRes res;
	res.png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr,
			nullptr);
	if (!res.png)
	{
		LU_LOG_E(LU_TAG "Decode", "Failed while png_create_read_struct");
		return {};
	}
	res.info = png_create_info_struct(res.png);
	if (!res.info)
	{
		LU_LOG_E(LU_TAG "Decode", "Failed while png_create_info_struct");
		return {};
	}

	vector<Byte> decode;
	vector<Byte*> decode_rows;

	if (setjmp(png_jmpbuf(res.png)))
	{
		LU_LOG_E(LU_TAG "Decode", "Unknown error");
		return {};
	}

	auto it = data.cbegin();
	png_set_read_fn(res.png, &it, &PngReadData);
	png_read_info(res.png, res.info);
	ConfigureReadPng(res.png, res.info);

	Size size(png_get_image_width(res.png, res.info),
			png_get_image_height(res.png, res.info));
	const Uint color_depth = png_get_bit_depth(res.png, res.info)
			* png_get_channels(res.png, res.info);
	if (color_depth != 32)
	{
		LU_LOG_E(LU_TAG "Decode", "Wrong pixel_size");
		return {};
	}

	const Uint row_size = size.w * 4; // 32 / 8 = 4
	decode.resize(row_size * size.h);
	decode_rows.reserve(size.h);
	for (Uint i = 0; i < size.h; ++i)
	{
		decode_rows.push_back(decode.data() + (row_size * i));
	}

	if (setjmp(png_jmpbuf(res.png)))
	{
		LU_LOG_E(LU_TAG "Decode", "Unknown error");
		return {};
	}

	png_read_image(res.png, decode_rows.data());
	png_read_end(res.png, nullptr);
	return Bitmap(std::move(decode), size);
}
Esempio n. 3
0
bool XSpriteBatchNode::initWithFile( const char* fileImage, unsigned int capacity )
{
	XFUNC_START();
	bool bRes;
	do 
	{
		//¼ÓÈë¼ÓÔضÓÁÐ
		int nLen = strlen(fileImage);
		if (IsPng(fileImage, nLen) || IsJpg(fileImage, nLen) || IsJpeg(fileImage, nLen))
		{
			unsigned short wResId = XResLoader::Instance().AddResLoad(XResType::ePng, (XEventObj*)this, 
				(FUNC_HANDEL)&XSpriteBatchNode::ResHandle, fileImage, NULL);
			if (wResId == 0xFFFF)
			{
				break;
			}
			XResType* pResType = XResLoader::Instance().GetResByID(wResId);
			if (!pResType)
			{
				break;
			}
			XResPNG* pRes = dynamic_cast<XResPNG*>(pResType);
			if (!pRes)
			{
				break;
			}			
			XTexture2D* &pTex = pRes->m_pTexture;
			if (!pTex)
			{
				break;
			}
			bRes = initWithTexture(pTex, capacity);
			if (!bRes)
			{
				break;
			}
			//printf("[ref:%d]%s\n", pTex->retainCount(), fileImage);
		}
		else
		{
			break;
			//// ѹËõÎÆÀí
			//unsigned short wResId = XResLoader::Instance().AddResLoad(XResType::eTexCompressed, (XEventObj*)this, 
			//	(FUNC_HANDEL)&XSpriteBatchNode::ResHandle, fileImage, NULL);
			//if (wResId == 0xFFFF)
			//{
			//	break;
			//}
			//XResType* pResType = XResLoader::Instance().GetResByID(wResId);
			//if (!pResType)
			//{
			//	break;
			//}
			//XResCompressed* pRes = dynamic_cast<XResCompressed*>(pResType);
			//if (!pRes)
			//{
			//	break;
			//}			
			//XTexture2D* &pTex = pRes->m_pTexture;
			//if (!pTex)
			//{
			//	break;
			//}
			//bRes = initWithTexture(pTex, capacity);
			//if (!bRes)
			//{
			//	break;
			//}
		}

		//static unsigned long dwReq = 0;
		//CCLOG("XSpriteBatchNode::initWithFile [%d] %s", dwReq++, fileImage);

		_bResLoaded = false;
		return true;
		
	} while (false);
	CCLOG("[ERROR]XSpriteBatchNode::initWithFile %s failed!", fileImage);
	XFUNC_END_RETURN(bool, res, false);
}