Skip to content
14 changes: 9 additions & 5 deletions cocos/2d/CCRenderTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ bool RenderTexture::initWithWidthAndHeight(int w, int h, Texture2D::PixelFormat
_texture = new (std::nothrow) Texture2D();
if (_texture)
{
_texture->initWithData(data, dataLen, (Texture2D::PixelFormat)_pixelFormat, powW, powH, Size((float)w, (float)h));
_texture->initWithData(data, dataLen, (Texture2D::PixelFormat)_pixelFormat, powW, powH, Size((float)w, (float)h), CC_ENABLE_PREMULTIPLIED_ALPHA != 0);
}
else
{
Expand All @@ -267,7 +267,7 @@ bool RenderTexture::initWithWidthAndHeight(int w, int h, Texture2D::PixelFormat
_textureCopy = new (std::nothrow) Texture2D();
if (_textureCopy)
{
_textureCopy->initWithData(data, dataLen, (Texture2D::PixelFormat)_pixelFormat, powW, powH, Size((float)w, (float)h));
_textureCopy->initWithData(data, dataLen, (Texture2D::PixelFormat)_pixelFormat, powW, powH, Size((float)w, (float)h), CC_ENABLE_PREMULTIPLIED_ALPHA != 0);
}
else
{
Expand Down Expand Up @@ -303,7 +303,11 @@ bool RenderTexture::initWithWidthAndHeight(int w, int h, Texture2D::PixelFormat
_texture->release();
_sprite->setFlippedY(true);

_sprite->setBlendFunc( BlendFunc::ALPHA_PREMULTIPLIED );
#if CC_ENABLE_PREMULTIPLIED_ALPHA != 0
_sprite->setBlendFunc(BlendFunc::ALPHA_PREMULTIPLIED);
#else
_sprite->setBlendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED);
#endif
_sprite->setOpacityModifyRGB(true);

glBindRenderbuffer(GL_RENDERBUFFER, oldRBO);
Expand Down Expand Up @@ -664,11 +668,11 @@ Image* RenderTexture::newImage(bool flipImage)
savedBufferWidth * 4);
}

image->initWithRawData(buffer, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8, true);
image->initWithRawData(buffer, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8, _texture->hasPremultipliedAlpha());
}
else
{
image->initWithRawData(tempData, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8, true);
image->initWithRawData(tempData, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8, _texture->hasPremultipliedAlpha());
}

} while (0);
Expand Down
29 changes: 7 additions & 22 deletions cocos/renderer/CCTexture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,21 +554,19 @@ bool Texture2D::hasPremultipliedAlpha() const
return _hasPremultipliedAlpha;
}

bool Texture2D::initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& /*contentSize*/)
bool Texture2D::initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& /*contentSize*/, bool preMultipliedAlpha)
{
CCASSERT(dataLen>0 && pixelsWide>0 && pixelsHigh>0, "Invalid size");

//if data has no mipmaps, we will consider it has only one mipmap
MipmapInfo mipmap;
mipmap.address = (unsigned char*)data;
mipmap.len = static_cast<int>(dataLen);
return initWithMipmaps(&mipmap, 1, pixelFormat, pixelsWide, pixelsHigh);
return initWithMipmaps(&mipmap, 1, pixelFormat, pixelsWide, pixelsHigh, preMultipliedAlpha);
}

bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, PixelFormat pixelFormat, int pixelsWide, int pixelsHigh)
bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, bool preMultipliedAlpha)
{


//the pixelFormat must be a certain value
CCASSERT(pixelFormat != PixelFormat::NONE && pixelFormat != PixelFormat::AUTO, "the \"pixelFormat\" param must be a certain value!");
CCASSERT(pixelsWide>0 && pixelsHigh>0, "Invalid size");
Expand All @@ -579,7 +577,6 @@ bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, PixelFormat
return false;
}


auto formatItr = _pixelFormatInfoTables.find(pixelFormat);
if(formatItr == _pixelFormatInfoTables.end())
{
Expand Down Expand Up @@ -706,7 +703,7 @@ bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, PixelFormat
_maxS = 1;
_maxT = 1;

_hasPremultipliedAlpha = false;
_hasPremultipliedAlpha = preMultipliedAlpha;
_hasMipmaps = mipmapsNum > 1;

// shader
Expand Down Expand Up @@ -764,18 +761,14 @@ bool Texture2D::initWithImage(Image *image, PixelFormat format)
PixelFormat renderFormat = image->getRenderFormat();
size_t tempDataLen = image->getDataLen();


if (image->getNumberOfMipmaps() > 1)
{
if (pixelFormat != image->getRenderFormat())
{
CCLOG("cocos2d: WARNING: This image has more than 1 mipmaps and we will not convert the data format");
}

initWithMipmaps(image->getMipmaps(), image->getNumberOfMipmaps(), image->getRenderFormat(), imageWidth, imageHeight);

// set the premultiplied tag
_hasPremultipliedAlpha = image->hasPremultipliedAlpha();
initWithMipmaps(image->getMipmaps(), image->getNumberOfMipmaps(), image->getRenderFormat(), imageWidth, imageHeight, image->hasPremultipliedAlpha());

return true;
}
Expand All @@ -786,10 +779,7 @@ bool Texture2D::initWithImage(Image *image, PixelFormat format)
CCLOG("cocos2d: WARNING: This image is compressed and we can't convert it for now");
}

initWithData(tempData, tempDataLen, image->getRenderFormat(), imageWidth, imageHeight, imageSize);

// set the premultiplied tag
_hasPremultipliedAlpha = image->hasPremultipliedAlpha();
initWithData(tempData, tempDataLen, image->getRenderFormat(), imageWidth, imageHeight, imageSize, image->hasPremultipliedAlpha());

return true;
}
Expand All @@ -800,18 +790,13 @@ bool Texture2D::initWithImage(Image *image, PixelFormat format)

pixelFormat = convertDataToFormat(tempData, tempDataLen, renderFormat, pixelFormat, &outTempData, &outTempDataLen);

initWithData(outTempData, outTempDataLen, pixelFormat, imageWidth, imageHeight, imageSize);

initWithData(outTempData, outTempDataLen, pixelFormat, imageWidth, imageHeight, imageSize, image->hasPremultipliedAlpha());

if (outTempData != nullptr && outTempData != tempData)
{

free(outTempData);
}

// set the premultiplied tag
_hasPremultipliedAlpha = image->hasPremultipliedAlpha();

return true;
}
}
Expand Down
6 changes: 4 additions & 2 deletions cocos/renderer/CCTexture2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,11 @@ class CC_DLL Texture2D : public Ref
@param pixelsWide The image width.
@param pixelsHigh The image height.
@param contentSize The image content size.
@param preMultipliedAlpha The texture has premultiplied alpha
* @js NA
* @lua NA
*/
bool initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& contentSize);
bool initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& contentSize, bool preMultipliedAlpha = false);

/** Initializes with mipmaps.

Expand All @@ -238,8 +239,9 @@ class CC_DLL Texture2D : public Ref
@param pixelFormat The image pixelFormat.
@param pixelsWide The image width.
@param pixelsHigh The image height.
@param preMultipliedAlpha The texture has premultiplied alpha
*/
bool initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh);
bool initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, bool preMultipliedAlpha = false);

/** Update with texture data.

Expand Down