Skip to content

Added support for saving non-premultiplied alpha images from RenderTexture #19990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions cocos/2d/CCRenderTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,28 @@ void RenderTexture::visit(Renderer *renderer, const Mat4 &parentTransform, uint3
// setOrderOfArrival(0);
}

bool RenderTexture::saveToFileAsNonPMA(const std::string& filename, bool isRGBA, std::function<void(RenderTexture*, const std::string&)> callback)
{
std::string basename(filename);
std::transform(basename.begin(), basename.end(), basename.begin(), ::tolower);

if (basename.find(".png") != std::string::npos)
{
return saveToFileAsNonPMA(filename, Image::Format::PNG, isRGBA, callback);
}
else if (basename.find(".jpg") != std::string::npos)
{
if (isRGBA) CCLOG("RGBA is not supported for JPG format.");
return saveToFileAsNonPMA(filename, Image::Format::JPG, false, callback);
}
else
{
CCLOG("Only PNG and JPG format are supported now!");
}

return saveToFileAsNonPMA(filename, Image::Format::JPG, false, callback);
}

bool RenderTexture::saveToFile(const std::string& filename, bool isRGBA, std::function<void (RenderTexture*, const std::string&)> callback)
{
std::string basename(filename);
Expand All @@ -376,6 +398,23 @@ bool RenderTexture::saveToFile(const std::string& filename, bool isRGBA, std::fu
return saveToFile(filename, Image::Format::JPG, false, callback);
}

bool RenderTexture::saveToFileAsNonPMA(const std::string& fileName, Image::Format format, bool isRGBA, std::function<void(RenderTexture*, const std::string&)> callback)
{
CCASSERT(format == Image::Format::JPG || format == Image::Format::PNG,
"the image can only be saved as JPG or PNG format");
if (isRGBA && format == Image::Format::JPG) CCLOG("RGBA is not supported for JPG format");

_saveFileCallback = callback;

std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName;
_saveToFileCommand.init(_globalZOrder);
_saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA, true);

Director::getInstance()->getRenderer()->addCommand(&_saveToFileCommand);
return true;
}


bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format, bool isRGBA, std::function<void (RenderTexture*, const std::string&)> callback)
{
CCASSERT(format == Image::Format::JPG || format == Image::Format::PNG,
Expand All @@ -386,17 +425,21 @@ bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format

std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName;
_saveToFileCommand.init(_globalZOrder);
_saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA);
_saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA, false);

Director::getInstance()->getRenderer()->addCommand(&_saveToFileCommand);
return true;
}

void RenderTexture::onSaveToFile(const std::string& filename, bool isRGBA)
void RenderTexture::onSaveToFile(const std::string& filename, bool isRGBA, bool forceNonPMA)
{
auto callbackFunc = [&, filename, isRGBA](Image* image){
auto callbackFunc = [&, filename, isRGBA, forceNonPMA](Image* image){
if (image)
{
if (forceNonPMA && image->hasPremultipliedAlpha())
{
image->reversePremultipliedAlpha();
}
image->saveToFile(filename, !isRGBA);
}
if(_saveFileCallback)
Expand Down Expand Up @@ -429,7 +472,7 @@ void RenderTexture::newImage(std::function<void(Image*)> imageCallback, bool fli
Image *image = new (std::nothrow) Image();

auto initCallback = [&, savedBufferWidth, savedBufferHeight, imageCallback](Image* image, const unsigned char* tempData){
image->initWithRawData(tempData, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8);
image->initWithRawData(tempData, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8, true);
imageCallback(image);
};
auto callback = std::bind(initCallback, image, std::placeholders::_1);
Expand Down
26 changes: 25 additions & 1 deletion cocos/2d/CCRenderTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ class CC_DLL RenderTexture : public Node
*/
void newImage(std::function<void(Image*)> imageCallback, bool flipImage = true);

/** Saves the texture into a file using JPEG format. The file will be saved in the Documents folder.
* Returns true if the operation is successful.
*
* @param filename The file name.
* @param isRGBA The file is RGBA or not.
* @param callback When the file is save finished,it will callback this function.
* @return Returns true if the operation is successful.
*/
bool saveToFileAsNonPMA(const std::string& filename, bool isRGBA = true, std::function<void(RenderTexture*, const std::string&)> callback = nullptr);

/** Saves the texture into a file using JPEG format. The file will be saved in the Documents folder.
* Returns true if the operation is successful.
*
Expand All @@ -170,6 +180,20 @@ class CC_DLL RenderTexture : public Node
*/
bool saveToFile(const std::string& filename, bool isRGBA = true, std::function<void (RenderTexture*, const std::string&)> callback = nullptr);

/** saves the texture into a file in non-PMA. The format could be JPG or PNG. The file will be saved in the Documents folder.
Returns true if the operation is successful.
* Notes: since v3.x, saveToFile will generate a custom command, which will be called in the following render->render().
* So if this function is called in a event handler, the actual save file will be called in the next frame. If we switch to a different scene, the game will crash.
* To solve this, add Director::getInstance()->getRenderer()->render(); after this function.
*
* @param filename The file name.
* @param format The image format.
* @param isRGBA The file is RGBA or not.
* @param callback When the file is save finished,it will callback this function.
* @return Returns true if the operation is successful.
*/
bool saveToFileAsNonPMA(const std::string& fileName, Image::Format format, bool isRGBA, std::function<void(RenderTexture*, const std::string&)> callback);

/** saves the texture into a file. The format could be JPG or PNG. The file will be saved in the Documents folder.
Returns true if the operation is successful.
* Notes: since v3.x, saveToFile will generate a custom command, which will be called in the following render->render().
Expand Down Expand Up @@ -322,7 +346,7 @@ class CC_DLL RenderTexture : public Node
void onEnd();
void clearColorAttachment();

void onSaveToFile(const std::string& fileName, bool isRGBA = true);
void onSaveToFile(const std::string& fileName, bool isRGBA = true, bool forceNonPMA = false);

bool _keepMatrix = false;
Rect _rtTextureRect;
Expand Down
27 changes: 25 additions & 2 deletions cocos/platform/CCImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ bool Image::initWithPngData(const unsigned char * data, ssize_t dataLen)
{
if (PNG_PREMULTIPLIED_ALPHA_ENABLED)
{
premultipliedAlpha();
premultiplyAlpha();
}
else
{
Expand Down Expand Up @@ -2341,7 +2341,7 @@ bool Image::saveImageToJPG(const std::string& filePath)
#endif // CC_USE_JPEG
}

void Image::premultipliedAlpha()
void Image::premultiplyAlpha()
{
#if CC_ENABLE_PREMULTIPLIED_ALPHA == 0
_hasPremultipliedAlpha = false;
Expand All @@ -2360,6 +2360,29 @@ void Image::premultipliedAlpha()
#endif
}

static inline unsigned char clamp(int x) {
return (unsigned char)(x >= 0 ? (x < 255 ? x : 255) : 0);
}

void Image::reversePremultipliedAlpha()
{
CCASSERT(_pixelFormat == backend::PixelFormat::RGBA8888, "The pixel format should be RGBA8888!");

unsigned int* fourBytes = (unsigned int*)_data;
for (int i = 0; i < _width * _height; i++)
{
unsigned char* p = _data + i * 4;
if (p[3] > 0)
{
fourBytes[i] = clamp(int(std::ceil((p[0] * 255.0f) / p[3]))) |
clamp(int(std::ceil((p[1] * 255.0f) / p[3]))) << 8 |
clamp(int(std::ceil((p[2] * 255.0f) / p[3]))) << 16 |
p[3] << 24;
}
}

_hasPremultipliedAlpha = false;
}

void Image::setPVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied)
{
Expand Down
4 changes: 3 additions & 1 deletion cocos/platform/CCImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class CC_DLL Image : public Ref
@param isToRGB whether the image is saved as RGB format.
*/
bool saveToFile(const std::string &filename, bool isToRGB = true);
void premultiplyAlpha();
void reversePremultipliedAlpha();

protected:
bool initWithJpgData(const unsigned char * data, ssize_t dataLen);
Expand All @@ -173,7 +175,7 @@ class CC_DLL Image : public Ref
bool saveImageToPNG(const std::string& filePath, bool isToRGB = true);
bool saveImageToJPG(const std::string& filePath);

void premultipliedAlpha();


protected:
/**
Expand Down
9 changes: 8 additions & 1 deletion cocos/platform/ios/CCImage-ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ of this software and associated documentation files (the "Software"), to deal
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
if (saveToPNG && hasAlpha() && (! isToRGB))
{
bitmapInfo |= kCGImageAlphaPremultipliedLast;
if (_hasPremultipliedAlpha)
{
bitmapInfo |= kCGImageAlphaPremultipliedLast;
}
else
{
bitmapInfo |= kCGImageAlphaLast;
}
}
CGDataProviderRef provider = CGDataProviderCreateWithData(nullptr, pixels, myDataLength, nullptr);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
Expand Down
64 changes: 57 additions & 7 deletions tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ RenderTextureSave::RenderTextureSave()

// Save Image menu
MenuItemFont::setFontSize(16);
auto item1 = MenuItemFont::create("Save Image", CC_CALLBACK_1(RenderTextureSave::saveImage, this));
auto item2 = MenuItemFont::create("Clear", CC_CALLBACK_1(RenderTextureSave::clearImage, this));
auto menu = Menu::create(item1, item2, nullptr);
auto item1 = MenuItemFont::create("Save Image PMA", CC_CALLBACK_1(RenderTextureSave::saveImageWithPremultipliedAlpha, this));
auto item2 = MenuItemFont::create("Save Image Non-PMA", CC_CALLBACK_1(RenderTextureSave::saveImageWithNonPremultipliedAlpha, this));
auto item3 = MenuItemFont::create("Add Image", CC_CALLBACK_1(RenderTextureSave::addImage, this));
auto item4 = MenuItemFont::create("Clear to Random", CC_CALLBACK_1(RenderTextureSave::clearImage, this));
auto item5 = MenuItemFont::create("Clear to Transparent", CC_CALLBACK_1(RenderTextureSave::clearImageTransparent, this));
auto menu = Menu::create(item1, item2, item3, item4, item5, nullptr);
this->addChild(menu);
menu->alignItemsVertically();
menu->setPosition(Vec2(VisibleRect::rightTop().x - 80, VisibleRect::rightTop().y - 30));
menu->setPosition(Vec2(VisibleRect::rightTop().x - 80, VisibleRect::rightTop().y - 100));
}

std::string RenderTextureSave::title() const
Expand All @@ -85,12 +88,43 @@ void RenderTextureSave::clearImage(cocos2d::Ref *sender)
_target->clear(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
}

void RenderTextureSave::saveImage(cocos2d::Ref *sender)
void RenderTextureSave::clearImageTransparent(cocos2d::Ref* sender)
{
_target->clear(0, 0, 0, 0);
}

void RenderTextureSave::saveImageWithPremultipliedAlpha(cocos2d::Ref* sender)
{
static int counter = 0;

char png[20];
sprintf(png, "image-pma-%d.png", counter);

auto callback = [&](RenderTexture* rt, const std::string& path)
{
auto sprite = Sprite::create(path);
addChild(sprite);
sprite->setScale(0.3f);
sprite->setPosition(Vec2(40, 40));
sprite->setRotation(counter * 3);
_target->release();
};

_target->retain();
_target->saveToFile(png, Image::Format::PNG, true, callback);
//Add this function to avoid crash if we switch to a new scene.
Director::getInstance()->getRenderer()->render();
CCLOG("Image saved %s", png);

counter++;
}

void RenderTextureSave::saveImageWithNonPremultipliedAlpha(cocos2d::Ref *sender)
{
static int counter = 0;

char png[20];
sprintf(png, "image-%d.png", counter);
sprintf(png, "image-no-pma-%d.png", counter);

auto callback = [&](RenderTexture* rt, const std::string& path)
{
Expand All @@ -103,14 +137,30 @@ void RenderTextureSave::saveImage(cocos2d::Ref *sender)
};

_target->retain();
_target->saveToFile(png, Image::Format::PNG, true, callback);
_target->saveToFileAsNonPMA(png, Image::Format::PNG, true, callback);

//Add this function to avoid crash if we switch to a new scene.
Director::getInstance()->getRenderer()->render();
CCLOG("Image saved %s", png);

counter++;
}

void RenderTextureSave::addImage(cocos2d::Ref* sender)
{
auto s = Director::getInstance()->getWinSize();

// begin drawing to the render texture
_target->begin();

Sprite* sprite = Sprite::create("Images/test-rgba1.png");
sprite->setPosition(sprite->getContentSize().width + CCRANDOM_0_1() * (s.width - sprite->getContentSize().width), sprite->getContentSize().height + CCRANDOM_0_1() * (s.height - sprite->getContentSize().height));
sprite->visit();

// finish drawing and return context back to the screen
_target->end();
}

RenderTextureSave::~RenderTextureSave()
{
_target->release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ class RenderTextureSave : public RenderTextureTest
virtual std::string subtitle() const override;
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
void clearImage(cocos2d::Ref* pSender);
void saveImage(cocos2d::Ref* pSender);
void clearImageTransparent(cocos2d::Ref* sender);
void saveImageWithPremultipliedAlpha(cocos2d::Ref* pSender);
void saveImageWithNonPremultipliedAlpha(cocos2d::Ref* pSender);
void addImage(cocos2d::Ref* sender);

private:
cocos2d::RenderTexture* _target;
Expand Down