Merge pull request #7056 from visiblelight/render_texture

Use RGBA when saving a file from render texture
This commit is contained in:
minggo 2014-06-16 10:44:30 +08:00
commit 111e08062f
3 changed files with 30 additions and 10 deletions

View File

@ -409,29 +409,47 @@ void RenderTexture::visit(Renderer *renderer, const Mat4 &parentTransform, uint3
_orderOfArrival = 0;
}
bool RenderTexture::saveToFile(const std::string& filename)
bool RenderTexture::saveToFile(const std::string& filename, bool isRGBA)
{
return saveToFile(filename,Image::Format::JPG);
std::string basename(filename);
std::transform(basename.begin(), basename.end(), basename.begin(), ::tolower);
if (basename.find(".png") != std::string::npos)
{
return saveToFile(filename, Image::Format::PNG, isRGBA);
}
else if (basename.find(".jpg") != std::string::npos)
{
if (isRGBA) CCLOG("RGBA is not supported for JPG format.");
return saveToFile(filename, Image::Format::JPG, false);
}
else
{
CCLOG("Only PNG and JPG format are supported now!");
}
return saveToFile(filename, Image::Format::JPG, false);
}
bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format)
bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format, bool isRGBA)
{
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");
std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName;
_saveToFileCommand.init(_globalZOrder);
_saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile,this,fullpath);
_saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA);
Director::getInstance()->getRenderer()->addCommand(&_saveToFileCommand);
return true;
}
void RenderTexture::onSaveToFile(const std::string& filename)
void RenderTexture::onSaveToFile(const std::string& filename, bool isRGBA)
{
Image *image = newImage(true);
if (image)
{
image->saveToFile(filename.c_str(), true);
image->saveToFile(filename.c_str(), !isRGBA);
}
CC_SAFE_DELETE(image);

View File

@ -103,12 +103,12 @@ public:
/** 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.
*/
bool saveToFile(const std::string& filename);
bool saveToFile(const std::string& filename, bool isRGBA = true);
/** 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.
*/
bool saveToFile(const std::string& filename, Image::Format format);
bool saveToFile(const std::string& filename, Image::Format format, bool isRGBA = true);
/** Listen "come to background" message, and save render texture.
It only has effect on Android.
@ -222,7 +222,7 @@ protected:
void onClear();
void onClearDepth();
void onSaveToFile(const std::string& fileName);
void onSaveToFile(const std::string& fileName, bool isRGBA = true);
Mat4 _oldTransMatrix, _oldProjMatrix;
Mat4 _transformMatrix, _projectionMatrix;

View File

@ -42,7 +42,9 @@ bool cocos2d::Image::saveToFile(const std::string& filename, bool isToRGB)
bool saveToPNG = false;
bool needToCopyPixels = false;
if (std::string::npos != filename.find(".png"))
std::string basename(filename);
std::transform(basename.begin(), basename.end(), basename.begin(), ::tolower);
if (std::string::npos != basename.find(".png"))
{
saveToPNG = true;
}