Skip to content

Commit 064cd8d

Browse files
committed
Merge pull request cocos2d#14722 from newnon/v3.10_webp_improvement
WebP loading improvements WebP loaded as premultiplied alpha if it has
2 parents 9347335 + a342866 commit 064cd8d

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

cocos/platform/CCImage.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -2121,19 +2121,19 @@ bool Image::initWithWebpData(const unsigned char * data, ssize_t dataLen)
21212121
if (WebPGetFeatures(static_cast<const uint8_t*>(data), dataLen, &config.input) != VP8_STATUS_OK) break;
21222122
if (config.input.width == 0 || config.input.height == 0) break;
21232123

2124-
config.output.colorspace = MODE_RGBA;
2125-
_renderFormat = Texture2D::PixelFormat::RGBA8888;
2124+
config.output.colorspace = config.input.has_alpha?MODE_rgbA:MODE_RGB;
2125+
_renderFormat = config.input.has_alpha?Texture2D::PixelFormat::RGBA8888:Texture2D::PixelFormat::RGB888;
21262126
_width = config.input.width;
21272127
_height = config.input.height;
21282128

2129-
//webp doesn't have premultipliedAlpha
2130-
_hasPremultipliedAlpha = false;
2129+
//we ask webp to give data with premultiplied alpha
2130+
_hasPremultipliedAlpha = config.input.has_alpha;
21312131

2132-
_dataLen = _width * _height * 4;
2132+
_dataLen = _width * _height * (config.input.has_alpha?4:3);
21332133
_data = static_cast<unsigned char*>(malloc(_dataLen * sizeof(unsigned char)));
21342134

21352135
config.output.u.RGBA.rgba = static_cast<uint8_t*>(_data);
2136-
config.output.u.RGBA.stride = _width * 4;
2136+
config.output.u.RGBA.stride = _width * (config.input.has_alpha?4:3);
21372137
config.output.u.RGBA.size = _dataLen;
21382138
config.output.is_external_memory = 1;
21392139

tests/cpp-tests/Classes/Texture2dTest/Texture2dTest.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Texture2DTests::Texture2DTests()
8181
ADD_TEST_CASE(TextureTGA);
8282
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
8383
ADD_TEST_CASE(TextureWEBP);
84+
ADD_TEST_CASE(TextureWEBPNoAlpha)
8485
#endif
8586
ADD_TEST_CASE(TexturePixelFormat);
8687
ADD_TEST_CASE(TextureBlend);
@@ -261,11 +262,36 @@ void TextureWEBP::onEnter()
261262
img->setPosition(Vec2( s.width/2.0f, s.height/2.0f));
262263
addChild(img);
263264
log("%s\n", Director::getInstance()->getTextureCache()->getCachedTextureInfo().c_str());
265+
Texture2D* texture = Director::getInstance()->getTextureCache()->getTextureForKey("Images/test_image.webp");
266+
log("pixel format:%d, premultiplied alpha:%d\n", texture->getPixelFormat(), texture->hasPremultipliedAlpha());
264267
}
265268

266269
std::string TextureWEBP::title() const
267270
{
268-
return "WEBP Test";
271+
return "WEBP with alpha Test";
272+
}
273+
274+
//------------------------------------------------------------------
275+
//
276+
// TextureWEBPNoAlpha
277+
//
278+
//------------------------------------------------------------------
279+
void TextureWEBPNoAlpha::onEnter()
280+
{
281+
TextureDemo::onEnter();
282+
auto s = Director::getInstance()->getWinSize();
283+
284+
auto img = Sprite::create("Images/test_image_no_alpha.webp");
285+
img->setPosition(Vec2( s.width/2.0f, s.height/2.0f));
286+
addChild(img);
287+
log("%s\n", Director::getInstance()->getTextureCache()->getCachedTextureInfo().c_str());
288+
Texture2D* texture = Director::getInstance()->getTextureCache()->getTextureForKey("Images/test_image_no_alpha.webp");
289+
log("pixel format:%d, premultiplied alpha:%d\n", texture->getPixelFormat(), texture->hasPremultipliedAlpha());
290+
}
291+
292+
std::string TextureWEBPNoAlpha::title() const
293+
{
294+
return "WEBP without alpha Test";
269295
}
270296

271297
//------------------------------------------------------------------

tests/cpp-tests/Classes/Texture2dTest/Texture2dTest.h

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ class TextureWEBP : public TextureDemo
7979
virtual void onEnter() override;
8080
};
8181

82+
class TextureWEBPNoAlpha : public TextureDemo
83+
{
84+
public:
85+
CREATE_FUNC(TextureWEBPNoAlpha);
86+
virtual std::string title() const override;
87+
virtual void onEnter() override;
88+
};
89+
8290
class TextureMipMap : public TextureDemo
8391
{
8492
public:
Binary file not shown.

0 commit comments

Comments
 (0)