2011-02-23 18:22:05 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2011-02-23 18:22:05 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-01-28 08:44:10 +08:00
|
|
|
|
|
|
|
#include "CCPlatformConfig.h"
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
|
2011-08-01 18:22:47 +08:00
|
|
|
#import "CCImage.h"
|
2013-08-22 11:12:09 +08:00
|
|
|
#import "platform/CCCommon.h"
|
2011-08-01 18:22:47 +08:00
|
|
|
#import <string>
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
2013-05-02 05:47:47 +08:00
|
|
|
#include<math.h>
|
|
|
|
|
2012-04-19 11:56:22 +08:00
|
|
|
NS_CC_BEGIN
|
2011-02-23 18:22:05 +08:00
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
bool cocos2d::Image::saveToFile(const std::string& filename, bool isToRGB)
|
2011-07-29 15:57:27 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool saveToPNG = false;
|
|
|
|
bool needToCopyPixels = false;
|
2013-11-15 09:19:16 +08:00
|
|
|
|
2013-11-16 04:04:47 +08:00
|
|
|
if (std::string::npos != filename.find(".png"))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
saveToPNG = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bitsPerComponent = 8;
|
2013-07-26 17:34:44 +08:00
|
|
|
int bitsPerPixel = hasAlpha() ? 32 : 24;
|
2013-12-24 15:49:58 +08:00
|
|
|
if ((! saveToPNG) || isToRGB)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
bitsPerPixel = 24;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
int bytesPerRow = (bitsPerPixel/8) * _width;
|
|
|
|
int myDataLength = bytesPerRow * _height;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned char *pixels = _data;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// The data has alpha channel, and want to save it with an RGB png file,
|
2011-08-08 11:01:47 +08:00
|
|
|
// or want to save as jpg, remove the alpha channel.
|
2013-12-24 15:49:58 +08:00
|
|
|
if ((saveToPNG && hasAlpha() && isToRGB)
|
2011-08-08 11:01:47 +08:00
|
|
|
|| (! saveToPNG))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pixels = new unsigned char[myDataLength];
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (int i = 0; i < _height; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (int j = 0; j < _width; ++j)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
pixels[(i * _width + j) * 3] = _data[(i * _width + j) * 4];
|
|
|
|
pixels[(i * _width + j) * 3 + 1] = _data[(i * _width + j) * 4 + 1];
|
|
|
|
pixels[(i * _width + j) * 3 + 2] = _data[(i * _width + j) * 4 + 2];
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
needToCopyPixels = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make data provider with data.
|
|
|
|
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
|
2013-12-24 15:49:58 +08:00
|
|
|
if (saveToPNG && hasAlpha() && (! isToRGB))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
bitmapInfo |= kCGImageAlphaPremultipliedLast;
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CGDataProviderRef provider = CGDataProviderCreateWithData(nullptr, pixels, myDataLength, nullptr);
|
2012-04-19 14:35:52 +08:00
|
|
|
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
|
2013-06-15 14:03:30 +08:00
|
|
|
CGImageRef iref = CGImageCreate(_width, _height,
|
2012-04-19 14:35:52 +08:00
|
|
|
bitsPerComponent, bitsPerPixel, bytesPerRow,
|
|
|
|
colorSpaceRef, bitmapInfo, provider,
|
2013-12-18 17:47:20 +08:00
|
|
|
nullptr, false,
|
2012-04-19 14:35:52 +08:00
|
|
|
kCGRenderingIntentDefault);
|
|
|
|
|
|
|
|
UIImage* image = [[UIImage alloc] initWithCGImage:iref];
|
|
|
|
|
|
|
|
CGImageRelease(iref);
|
|
|
|
CGColorSpaceRelease(colorSpaceRef);
|
|
|
|
CGDataProviderRelease(provider);
|
|
|
|
|
|
|
|
NSData *data;
|
|
|
|
|
|
|
|
if (saveToPNG)
|
|
|
|
{
|
|
|
|
data = UIImagePNGRepresentation(image);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = UIImageJPEGRepresentation(image, 1.0f);
|
|
|
|
}
|
|
|
|
|
2013-11-16 04:04:47 +08:00
|
|
|
[data writeToFile:[NSString stringWithUTF8String:filename.c_str()] atomically:YES];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
[image release];
|
|
|
|
|
|
|
|
if (needToCopyPixels)
|
|
|
|
{
|
|
|
|
delete [] pixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-07-29 15:57:27 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 11:56:22 +08:00
|
|
|
NS_CC_END
|
2011-02-23 18:22:05 +08:00
|
|
|
|
2014-01-28 08:44:10 +08:00
|
|
|
#endif // CC_PLATFORM_IOS
|
|
|
|
|