bool CCImage::_initWithPngData(void * pData, int nDatalen)
{
	IW_CALLSTACK("CCImage::_initWithPngData");
	
    bool bRet = false;
	
	s3eFile* pFile = s3eFileOpenFromMemory(pData, nDatalen);
	
	IwAssert(GAME, pFile);
	
	CIwImage    *image		= NULL;
	image = new CIwImage;
	
	image->ReadFile( pFile);
	
    s3eFileClose(pFile);
	
	// init image info
	m_bPreMulti	= true;
	m_bHasAlpha = image->HasAlpha();
	
	unsigned int bytesPerComponent = 3;
	if (m_bHasAlpha)
	{
		bytesPerComponent = 4;
	} 
	m_nHeight = (unsigned int)image->GetHeight();
	m_nWidth = (unsigned int)image->GetWidth();
	m_nBitsPerComponent = (unsigned int)image->GetBitDepth()/bytesPerComponent;
	
	tImageSource imageSource;
	
	imageSource.data    = (unsigned char*)pData;
	imageSource.size    = nDatalen;
	imageSource.offset  = 0;
	
	m_pData = new unsigned char[m_nHeight * m_nWidth * bytesPerComponent];
	
	unsigned int bytesPerRow = m_nWidth * bytesPerComponent;

	if(m_bHasAlpha)
	{
		unsigned char *src = NULL;
		src = (unsigned char *)image->GetTexels();
		
		unsigned char *tmp = (unsigned char *) m_pData;
		
		for(unsigned int i = 0; i < m_nHeight*bytesPerRow; i += bytesPerComponent)
		{
			*(tmp + i + 0)	=  (*(src + i + 0) * *(src + i + 3) + 1) >> 8;
			*(tmp + i + 1)	=  (*(src + i + 1) * *(src + i + 3) + 1) >> 8;					
			*(tmp + i + 2)	=  (*(src + i + 2) * *(src + i + 3) + 1) >> 8;
			*(tmp + i + 3)	=   *(src + i + 3);
		}
		
	}
	else
	{
		for (int j = 0; j < (m_nHeight); ++j)
Exemplo n.º 2
0
CIwMaterial* ResourceManager::load(const char* name, int* w, int* h) {
	int width = 0, height = 0;
	char res[MAX_RES_NAME] = {0};
	sprintf(res, "images/%s/%s", desktop.getDevPath(), name);
	IIter p = imgs->find(res);
	if (p != imgs->end()) {
		if (w != NULL) {
			*w = p->second.width;
		}
		if (h != NULL) {
			*h = p->second.height;
		}
		return p->second.mat;
	}
	CIwTexture* texture = new CIwTexture;
    CIwImage image;
    s3eFile* pFile = s3eFileOpen(res, "rb");
    if (pFile) {
        image.ReadFile(pFile);
		width = image.GetWidth();
		height = image.GetHeight();
        s3eFileClose(pFile);
        texture->CopyFromImage(&image);
        texture->Upload();
    } else {
		delete texture;
		texture = NULL;
	}
	CIwMaterial* mat = new CIwMaterial;
	mat->SetTexture(texture);
	SImg s;
	s.texture = texture;
	s.mat = mat;
	s.width = width;
	s.height = height;
	imgs->insert(IPair(string(res), s));
	if (w != NULL) {
		*w = width;
	}
	if (h != NULL) {
		*h = height;
	}
	return mat;
}