2011-02-23 18:22:05 +08:00
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
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.
****************************************************************************/
2011-08-01 18:22:47 +08:00
#import "CCImage.h"
#import "CCFileUtils.h"
2013-03-05 07:32:24 +08:00
#import "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>
2013-04-26 09:22:26 +08:00
2011-02-23 18:22:05 +08:00
typedef struct
{
2011-03-04 17:02:22 +08:00
unsigned int height;
unsigned int width;
2013-04-26 09:22:26 +08:00
int bitsPerComponent;
bool hasAlpha;
bool isPremultipliedAlpha;
bool hasShadow;
CGSize shadowOffset;
float shadowBlur;
float shadowOpacity;
bool hasStroke;
float strokeColorR;
float strokeColorG;
float strokeColorB;
float strokeSize;
2013-05-01 07:36:14 +08:00
float tintColorR;
float tintColorG;
float tintColorB;
2013-04-26 09:22:26 +08:00
2011-03-04 17:02:22 +08:00
unsigned char* data;
2013-04-26 09:22:26 +08:00
2011-02-23 18:22:05 +08:00
} tImageInfo;
2012-04-27 15:53:29 +08:00
static bool _initWithImage(CGImageRef cgImage, tImageInfo *pImageinfo)
2011-02-23 18:22:05 +08:00
{
2012-04-27 15:53:29 +08:00
if(cgImage == NULL)
2011-08-08 11:01:47 +08:00
{
2012-04-27 15:53:29 +08:00
return false;
2011-03-04 17:02:22 +08:00
}
2012-04-27 15:53:29 +08:00
// get image info
2011-03-04 17:02:22 +08:00
2012-04-27 15:53:29 +08:00
pImageinfo->width = CGImageGetWidth(cgImage);
pImageinfo->height = CGImageGetHeight(cgImage);
2011-03-04 17:02:22 +08:00
2012-04-27 15:53:29 +08:00
CGImageAlphaInfo info = CGImageGetAlphaInfo(cgImage);
pImageinfo->hasAlpha = (info == kCGImageAlphaPremultipliedLast)
|| (info == kCGImageAlphaPremultipliedFirst)
|| (info == kCGImageAlphaLast)
|| (info == kCGImageAlphaFirst);
2011-03-04 17:02:22 +08:00
2013-05-05 01:14:23 +08:00
// If OS version < 5.x, add condition to support jpg
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if(systemVersion < 5.0f)
{
pImageinfo->hasAlpha = (pImageinfo->hasAlpha || (info == kCGImageAlphaNoneSkipLast));
}
2012-04-27 15:53:29 +08:00
CGColorSpaceRef colorSpace = CGImageGetColorSpace(cgImage);
if (colorSpace)
2011-08-08 11:01:47 +08:00
{
2012-04-27 15:53:29 +08:00
if (pImageinfo->hasAlpha)
2011-08-08 11:01:47 +08:00
{
2012-04-27 15:53:29 +08:00
info = kCGImageAlphaPremultipliedLast;
2012-04-27 18:37:11 +08:00
pImageinfo->isPremultipliedAlpha = true;
2011-08-08 11:01:47 +08:00
}
2012-04-27 15:53:29 +08:00
else
2011-08-08 11:01:47 +08:00
{
2012-04-27 15:53:29 +08:00
info = kCGImageAlphaNoneSkipLast;
2012-04-27 18:37:11 +08:00
pImageinfo->isPremultipliedAlpha = false;
2011-08-08 11:01:47 +08:00
}
2011-03-04 17:02:22 +08:00
}
2012-04-27 15:53:29 +08:00
else
2011-08-08 11:01:47 +08:00
{
2011-03-04 17:02:22 +08:00
return false;
}
2012-04-27 15:53:29 +08:00
// change to RGBA8888
pImageinfo->hasAlpha = true;
pImageinfo->bitsPerComponent = 8;
2012-12-30 02:14:30 +08:00
pImageinfo->data = new unsigned char[pImageinfo->width * pImageinfo->height * 4];
2012-04-27 15:53:29 +08:00
colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pImageinfo->data,
pImageinfo->width,
pImageinfo->height,
8,
4 * pImageinfo->width,
colorSpace,
info | kCGBitmapByteOrder32Big);
CGContextClearRect(context, CGRectMake(0, 0, pImageinfo->width, pImageinfo->height));
2012-04-27 18:37:11 +08:00
//CGContextTranslateCTM(context, 0, 0);
2012-04-27 15:53:29 +08:00
CGContextDrawImage(context, CGRectMake(0, 0, pImageinfo->width, pImageinfo->height), cgImage);
2011-03-04 17:02:22 +08:00
2012-04-27 15:53:29 +08:00
CGContextRelease(context);
2012-08-21 07:48:07 +08:00
CFRelease(colorSpace);
2011-03-04 17:02:22 +08:00
return true;
2011-02-23 18:22:05 +08:00
}
static bool _initWithData(void * pBuffer, int length, tImageInfo *pImageinfo)
{
2011-03-04 17:02:22 +08:00
bool ret = false;
2011-08-08 11:01:47 +08:00
if (pBuffer)
{
CGImageRef CGImage;
NSData *data;
2011-03-04 17:02:22 +08:00
data = [NSData dataWithBytes:pBuffer length:length];
CGImage = [[UIImage imageWithData:data] CGImage];
ret = _initWithImage(CGImage, pImageinfo);
}
return ret;
2011-02-23 18:22:05 +08:00
}
2012-10-15 12:03:15 +08:00
static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize)
2011-08-23 16:19:31 +08:00
{
NSArray *listItems = [str componentsSeparatedByString: @"\n"];
2011-10-14 14:33:08 +08:00
CGSize dim = CGSizeZero;
2012-03-02 11:15:44 +08:00
CGSize textRect = CGSizeZero;
textRect.width = constrainSize->width > 0 ? constrainSize->width
: 0x7fffffff;
textRect.height = constrainSize->height > 0 ? constrainSize->height
: 0x7fffffff;
2011-08-23 16:19:31 +08:00
for (NSString *s in listItems)
{
2012-10-15 12:03:15 +08:00
CGSize tmp = [s sizeWithFont:font constrainedToSize:textRect];
2011-08-23 16:19:31 +08:00
2011-10-14 14:33:08 +08:00
if (tmp.width > dim.width)
{
dim.width = tmp.width;
}
2012-03-02 11:15:44 +08:00
dim.height += tmp.height;
2011-08-23 16:19:31 +08:00
}
return dim;
}
2013-06-20 14:13:12 +08:00
// refer Image::ETextAlign
2012-06-13 16:20:58 +08:00
#define ALIGN_TOP 1
#define ALIGN_CENTER 3
#define ALIGN_BOTTOM 2
2013-07-25 23:09:18 +08:00
static bool _initWithString(const char * pText, cocos2d::Image::TextAlign eAlign, const char * pFontName, int nSize, tImageInfo* pInfo)
2011-02-23 18:22:05 +08:00
{
2011-03-04 17:02:22 +08:00
bool bRet = false;
do
{
2011-03-09 16:19:20 +08:00
CC_BREAK_IF(! pText || ! pInfo);
2011-03-04 17:02:22 +08:00
2013-05-02 05:47:47 +08:00
NSString * str = [NSString stringWithUTF8String:pText];
NSString * fntName = [NSString stringWithUTF8String:pFontName];
2011-10-14 14:33:08 +08:00
CGSize dim, constrainSize;
2013-05-02 05:47:47 +08:00
constrainSize.width = pInfo->width;
constrainSize.height = pInfo->height;
2011-03-04 17:02:22 +08:00
2013-03-05 07:32:24 +08:00
// On iOS custom fonts must be listed beforehand in the App info.plist (in order to be usable) and referenced only the by the font family name itself when
// calling [UIFont fontWithName]. Therefore even if the developer adds 'SomeFont.ttf' or 'fonts/SomeFont.ttf' to the App .plist, the font must
// be referenced as 'SomeFont' when calling [UIFont fontWithName]. Hence we strip out the folder path components and the extension here in order to get just
// the font family name itself. This stripping step is required especially for references to user fonts stored in CCB files; CCB files appear to store
// the '.ttf' extensions when referring to custom fonts.
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
2011-08-01 18:22:47 +08:00
// create the font
2013-03-05 07:32:24 +08:00
id font = [UIFont fontWithName:fntName size:nSize];
2011-07-29 15:57:27 +08:00
if (font)
{
2012-10-15 12:03:15 +08:00
dim = _calculateStringSize(str, font, &constrainSize);
2013-03-05 07:32:24 +08:00
}
2012-10-15 12:03:15 +08:00
else
2011-02-23 18:22:05 +08:00
{
2013-03-05 07:32:24 +08:00
if (!font)
2012-08-22 15:51:25 +08:00
{
font = [UIFont systemFontOfSize:nSize];
}
2011-07-29 15:57:27 +08:00
2012-08-22 15:51:25 +08:00
if (font)
{
2012-10-15 12:03:15 +08:00
dim = _calculateStringSize(str, font, &constrainSize);
2013-03-05 07:32:24 +08:00
}
2011-02-23 18:22:05 +08:00
}
2011-07-29 15:57:27 +08:00
CC_BREAK_IF(! font);
2011-03-04 17:02:22 +08:00
2012-03-02 14:17:22 +08:00
// compute start point
int startH = 0;
if (constrainSize.height > dim.height)
{
2012-06-13 16:20:58 +08:00
// vertical alignment
2013-07-26 15:22:45 +08:00
unsigned int vAlignment = ((int)eAlign >> 4) & 0x0F;
2012-06-13 16:20:58 +08:00
if (vAlignment == ALIGN_TOP)
{
startH = 0;
}
else if (vAlignment == ALIGN_CENTER)
{
startH = (constrainSize.height - dim.height) / 2;
}
else
{
startH = constrainSize.height - dim.height;
}
2012-03-02 14:17:22 +08:00
}
// adjust text rect
if (constrainSize.width > 0 && constrainSize.width > dim.width)
{
dim.width = constrainSize.width;
}
if (constrainSize.height > 0 && constrainSize.height > dim.height)
{
dim.height = constrainSize.height;
2012-12-29 13:58:02 +08:00
}
2013-04-26 09:22:26 +08:00
2013-05-11 05:15:02 +08:00
// compute the padding needed by shadow and stroke
2013-04-26 09:22:26 +08:00
float shadowStrokePaddingX = 0.0f;
float shadowStrokePaddingY = 0.0f;
if ( pInfo->hasStroke )
{
2013-05-11 05:16:37 +08:00
shadowStrokePaddingX = ceilf(pInfo->strokeSize);
shadowStrokePaddingY = ceilf(pInfo->strokeSize);
2013-04-26 09:22:26 +08:00
}
2013-05-02 05:47:47 +08:00
2013-04-26 09:22:26 +08:00
if ( pInfo->hasShadow )
{
2013-06-13 19:16:33 +08:00
shadowStrokePaddingX = std::max(shadowStrokePaddingX, (float)fabs(pInfo->shadowOffset.width));
shadowStrokePaddingY = std::max(shadowStrokePaddingY, (float)fabs(pInfo->shadowOffset.height));
2013-04-26 09:22:26 +08:00
}
2013-05-11 05:15:02 +08:00
// add the padding (this could be 0 if no shadow and no stroke)
dim.width += shadowStrokePaddingX;
2013-04-26 09:22:26 +08:00
dim.height += shadowStrokePaddingY;
2013-05-11 05:15:02 +08:00
2012-12-30 02:14:30 +08:00
unsigned char* data = new unsigned char[(int)(dim.width * dim.height * 4)];
2011-10-14 14:33:08 +08:00
memset(data, 0, (int)(dim.width * dim.height * 4));
2011-03-04 17:02:22 +08:00
2011-07-29 15:57:27 +08:00
// draw text
2013-05-11 05:15:02 +08:00
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(data,
dim.width,
dim.height,
8,
2013-06-12 03:11:05 +08:00
(int)(dim.width) * 4,
2013-05-11 05:15:02 +08:00
colorSpace,
2013-07-10 06:20:40 +08:00
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
2013-05-11 05:15:02 +08:00
if (!context)
2012-04-19 14:35:52 +08:00
{
2013-07-10 06:20:40 +08:00
CGColorSpaceRelease(colorSpace);
2012-12-30 02:12:02 +08:00
delete[] data;
2012-06-13 16:20:58 +08:00
break;
2012-04-19 14:35:52 +08:00
}
2013-04-26 09:22:26 +08:00
2013-05-01 07:36:14 +08:00
// text color
CGContextSetRGBFillColor(context, pInfo->tintColorR, pInfo->tintColorG, pInfo->tintColorB, 1);
2013-05-11 05:15:02 +08:00
// move Y rendering to the top of the image
CGContextTranslateCTM(context, 0.0f, (dim.height - shadowStrokePaddingY) );
2011-02-23 18:22:05 +08:00
CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
2013-05-11 05:15:02 +08:00
// store the current context
2011-02-23 18:22:05 +08:00
UIGraphicsPushContext(context);
2011-03-04 17:02:22 +08:00
2011-07-29 15:57:27 +08:00
// measure text size with specified font and determine the rectangle to draw text in
2013-07-26 15:22:45 +08:00
unsigned uHoriFlag = (int)eAlign & 0x0f;
2012-07-09 13:37:19 +08:00
UITextAlignment align = (UITextAlignment)((2 == uHoriFlag) ? UITextAlignmentRight
2011-03-04 17:02:22 +08:00
: (3 == uHoriFlag) ? UITextAlignmentCenter
2012-07-09 13:37:19 +08:00
: UITextAlignmentLeft);
2013-04-26 09:22:26 +08:00
2011-07-29 10:15:10 +08:00
2013-04-26 09:22:26 +08:00
// take care of stroke if needed
if ( pInfo->hasStroke )
2012-06-16 05:06:01 +08:00
{
2013-04-26 09:22:26 +08:00
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSetRGBStrokeColor(context, pInfo->strokeColorR, pInfo->strokeColorG, pInfo->strokeColorB, 1);
CGContextSetLineWidth(context, pInfo->strokeSize);
2012-06-16 05:06:01 +08:00
}
2013-04-26 09:22:26 +08:00
// take care of shadow if needed
if ( pInfo->hasShadow )
2012-06-16 05:06:01 +08:00
{
2013-04-26 09:22:26 +08:00
CGSize offset;
offset.height = pInfo->shadowOffset.height;
offset.width = pInfo->shadowOffset.width;
2013-07-10 06:20:40 +08:00
CGFloat shadowColorValues[] = {0, 0, 0, pInfo->shadowOpacity};
CGColorRef shadowColor = CGColorCreate (colorSpace, shadowColorValues);
CGContextSetShadowWithColor(context, offset, pInfo->shadowBlur, shadowColor);
CGColorRelease (shadowColor);
2012-06-16 05:06:01 +08:00
}
2013-04-26 09:22:26 +08:00
2013-07-10 06:20:40 +08:00
CGColorSpaceRelease(colorSpace);
2013-05-11 05:15:02 +08:00
2013-04-26 09:22:26 +08:00
// normal fonts
//if( [font isKindOfClass:[UIFont class] ] )
//{
// [str drawInRect:CGRectMake(0, startH, dim.width, dim.height) withFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap alignment:align];
//}
//else // ZFont class
//{
// [FontLabelStringDrawingHelper drawInRect:str rect:CGRectMake(0, startH, dim.width, dim.height) withZFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap
////alignment:align];
//}
2013-05-11 05:15:02 +08:00
// compute the rect used for rendering the text
// based on wether shadows or stroke are enabled
float textOriginX = 0.0;
float textOrigingY = 0.0;
float textWidth = dim.width - shadowStrokePaddingX;
float textHeight = dim.height - shadowStrokePaddingY;
if ( pInfo->shadowOffset.width < 0 )
{
textOriginX = shadowStrokePaddingX;
}
else
{
textOriginX = 0.0;
}
if (pInfo->shadowOffset.height > 0)
{
textOrigingY = startH;
}
else
{
textOrigingY = startH - shadowStrokePaddingY;
}
2013-07-03 05:45:07 +08:00
CGRect rect = CGRectMake(textOriginX, textOrigingY, textWidth, textHeight);
2013-05-11 05:15:02 +08:00
2013-07-03 05:45:07 +08:00
CGContextBeginTransparencyLayerWithRect(context, rect, NULL);
2013-05-11 05:15:02 +08:00
// actually draw the text in the context
2013-06-12 03:11:05 +08:00
// XXX: ios7 casting
2013-07-03 05:45:07 +08:00
[str drawInRect: rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:(NSTextAlignment)align];
CGContextEndTransparencyLayer(context);
2011-03-04 17:02:22 +08:00
2013-05-02 05:47:47 +08:00
// pop the context
2011-02-23 18:22:05 +08:00
UIGraphicsPopContext();
2011-03-04 17:02:22 +08:00
2013-05-02 05:47:47 +08:00
// release the context
2011-02-23 18:22:05 +08:00
CGContextRelease(context);
2011-03-04 17:02:22 +08:00
2011-02-23 18:22:05 +08:00
// output params
2013-05-11 05:15:02 +08:00
pInfo->data = data;
pInfo->hasAlpha = true;
2011-02-23 18:22:05 +08:00
pInfo->isPremultipliedAlpha = true;
2013-05-11 05:15:02 +08:00
pInfo->bitsPerComponent = 8;
pInfo->width = dim.width;
pInfo->height = dim.height;
bRet = true;
2013-04-26 09:22:26 +08:00
2011-03-04 17:02:22 +08:00
} while (0);
return bRet;
2011-02-23 18:22:05 +08:00
}
2012-04-19 11:56:22 +08:00
NS_CC_BEGIN
2011-02-23 18:22:05 +08:00
2013-06-20 14:13:12 +08:00
Image::Image()
2013-06-15 14:03:30 +08:00
: _width(0)
, _height(0)
, _bitsPerComponent(0)
, _data(0)
, _hasAlpha(false)
, _preMulti(false)
2011-02-23 18:22:05 +08:00
{
2011-03-04 17:02:22 +08:00
2011-02-23 18:22:05 +08:00
}
2013-06-20 14:13:12 +08:00
Image::~Image()
2011-03-28 13:41:14 +08:00
{
2013-06-15 14:03:30 +08:00
CC_SAFE_DELETE_ARRAY(_data);
2011-03-28 13:41:14 +08:00
}
2013-07-25 20:29:53 +08:00
bool Image::initWithImageFile(const char * strPath, Format eImgFmt/* = eFmtPng*/)
2011-02-23 18:22:05 +08:00
{
2012-06-19 16:31:26 +08:00
bool bRet = false;
unsigned long nSize = 0;
2013-07-12 06:24:23 +08:00
unsigned char* pBuffer = FileUtils::getInstance()->getFileData(
FileUtils::getInstance()->fullPathForFilename(strPath).c_str(),
2012-06-19 16:31:26 +08:00
"rb",
&nSize);
if (pBuffer != NULL && nSize > 0)
{
bRet = initWithImageData(pBuffer, nSize, eImgFmt);
}
CC_SAFE_DELETE_ARRAY(pBuffer);
return bRet;
2011-02-23 18:22:05 +08:00
}
2013-07-25 20:29:53 +08:00
bool Image::initWithImageFileThreadSafe(const char *fullpath, Format imageType)
2011-11-25 10:09:29 +08:00
{
2012-06-19 16:31:26 +08:00
/*
2013-06-26 16:42:11 +08:00
* FileUtils::fullPathFromRelativePath() is not thread-safe.
2012-06-19 16:31:26 +08:00
*/
bool bRet = false;
unsigned long nSize = 0;
2013-07-12 06:24:23 +08:00
unsigned char* pBuffer = FileUtils::getInstance()->getFileData(fullpath, "rb", &nSize);
2012-06-19 16:31:26 +08:00
if (pBuffer != NULL && nSize > 0)
{
bRet = initWithImageData(pBuffer, nSize, imageType);
}
CC_SAFE_DELETE_ARRAY(pBuffer);
return bRet;
2011-11-25 10:09:29 +08:00
}
2013-06-20 14:13:12 +08:00
bool Image::initWithImageData(void * pData,
2011-07-29 10:15:10 +08:00
int nDataLen,
2013-07-25 20:29:53 +08:00
Format eFmt,
2011-07-29 10:15:10 +08:00
int nWidth,
int nHeight,
int nBitsPerComponent)
2011-02-23 18:22:05 +08:00
{
2011-03-04 17:02:22 +08:00
bool bRet = false;
tImageInfo info = {0};
2013-04-26 09:22:26 +08:00
info.hasShadow = false;
info.hasStroke = false;
2011-03-04 17:02:22 +08:00
do
{
2011-03-09 16:19:20 +08:00
CC_BREAK_IF(! pData || nDataLen <= 0);
2013-07-26 05:49:43 +08:00
if (eFmt == Format::RAW_DATA)
2011-08-08 11:01:47 +08:00
{
2013-06-19 15:08:09 +08:00
bRet = initWithRawData(pData, nDataLen, nWidth, nHeight, nBitsPerComponent, false);
2011-08-08 11:01:47 +08:00
}
2013-07-26 05:49:43 +08:00
else if (eFmt == Format::WEBP)
2012-12-29 13:58:02 +08:00
{
2013-07-26 05:49:43 +08:00
bRet = initWithWebpData(pData, nDataLen);
2012-12-29 13:58:02 +08:00
}
2011-08-08 11:01:47 +08:00
else // init with png or jpg file data
{
bRet = _initWithData(pData, nDataLen, &info);
if (bRet)
{
2013-06-15 14:03:30 +08:00
_height = (short)info.height;
_width = (short)info.width;
_bitsPerComponent = info.bitsPerComponent;
_hasAlpha = info.hasAlpha;
_preMulti = info.isPremultipliedAlpha;
_data = info.data;
2011-08-08 11:01:47 +08:00
}
}
2011-03-04 17:02:22 +08:00
} while (0);
2011-08-08 11:01:47 +08:00
2011-03-04 17:02:22 +08:00
return bRet;
}
2011-02-23 18:22:05 +08:00
2013-06-20 14:13:12 +08:00
bool Image::initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent, bool bPreMulti)
2011-08-08 11:01:47 +08:00
{
bool bRet = false;
2012-04-19 14:35:52 +08:00
do
{
CC_BREAK_IF(0 == nWidth || 0 == nHeight);
2013-06-15 14:03:30 +08:00
_bitsPerComponent = nBitsPerComponent;
_height = (short)nHeight;
_width = (short)nWidth;
_hasAlpha = true;
2012-04-19 14:35:52 +08:00
2012-09-16 05:19:14 +08:00
// only RGBA8888 supported
2012-04-19 14:35:52 +08:00
int nBytesPerComponent = 4;
int nSize = nHeight * nWidth * nBytesPerComponent;
2013-06-15 14:03:30 +08:00
_data = new unsigned char[nSize];
CC_BREAK_IF(! _data);
memcpy(_data, pData, nSize);
2012-04-19 14:35:52 +08:00
bRet = true;
} while (0);
return bRet;
2011-08-08 11:01:47 +08:00
}
2013-07-26 05:49:43 +08:00
bool Image::initWithJpgData(void *pData, int nDatalen)
2011-08-08 11:01:47 +08:00
{
assert(0);
2012-06-09 04:52:40 +08:00
return false;
2011-08-08 11:01:47 +08:00
}
2013-07-26 05:49:43 +08:00
bool Image::initWithPngData(void *pData, int nDatalen)
2011-08-08 11:01:47 +08:00
{
assert(0);
2012-06-09 04:52:40 +08:00
return false;
2011-08-08 11:01:47 +08:00
}
2013-07-26 05:49:43 +08:00
bool Image::saveImageToPNG(const char *pszFilePath, bool bIsToRGB)
2011-08-08 11:01:47 +08:00
{
assert(0);
2012-06-09 04:52:40 +08:00
return false;
2011-08-08 11:01:47 +08:00
}
2013-07-26 05:49:43 +08:00
bool Image::saveImageToJPG(const char *pszFilePath)
2011-08-08 11:01:47 +08:00
{
assert(0);
2012-06-09 04:52:40 +08:00
return false;
2011-08-08 11:01:47 +08:00
}
2013-06-20 14:13:12 +08:00
bool Image::initWithString(
2013-04-26 09:22:26 +08:00
const char * pText,
int nWidth /* = 0 */,
int nHeight /* = 0 */,
2013-07-25 23:09:18 +08:00
TextAlign eAlignMask /* = kAlignCenter */,
2013-04-26 09:22:26 +08:00
const char * pFontName /* = nil */,
int nSize /* = 0 */)
{
return initWithStringShadowStroke(pText, nWidth, nHeight, eAlignMask , pFontName, nSize);
}
2013-06-20 14:13:12 +08:00
bool Image::initWithStringShadowStroke(
2013-04-26 09:22:26 +08:00
const char * pText,
int nWidth ,
int nHeight ,
2013-07-25 23:09:18 +08:00
TextAlign eAlignMask ,
2013-04-26 09:22:26 +08:00
const char * pFontName ,
int nSize ,
2013-05-01 07:36:14 +08:00
float textTintR,
float textTintG,
float textTintB,
2013-04-26 09:22:26 +08:00
bool shadow,
float shadowOffsetX,
float shadowOffsetY,
float shadowOpacity,
float shadowBlur,
bool stroke,
float strokeR,
float strokeG,
float strokeB,
float strokeSize)
2011-03-04 17:02:22 +08:00
{
2013-04-26 09:22:26 +08:00
2011-03-04 17:02:22 +08:00
tImageInfo info = {0};
2013-04-26 09:22:26 +08:00
info.width = nWidth;
info.height = nHeight;
info.hasShadow = shadow;
info.shadowOffset.width = shadowOffsetX;
info.shadowOffset.height = shadowOffsetY;
info.shadowBlur = shadowBlur;
info.shadowOpacity = shadowOpacity;
info.hasStroke = stroke;
info.strokeColorR = strokeR;
info.strokeColorG = strokeG;
info.strokeColorB = strokeB;
info.strokeSize = strokeSize;
2013-05-01 07:36:14 +08:00
info.tintColorR = textTintR;
info.tintColorG = textTintG;
info.tintColorB = textTintB;
2013-04-26 09:22:26 +08:00
2011-03-04 17:02:22 +08:00
if (! _initWithString(pText, eAlignMask, pFontName, nSize, &info))
{
return false;
}
2013-06-15 14:03:30 +08:00
_height = (short)info.height;
_width = (short)info.width;
_bitsPerComponent = info.bitsPerComponent;
_hasAlpha = info.hasAlpha;
_preMulti = info.isPremultipliedAlpha;
_data = info.data;
2013-04-26 09:22:26 +08:00
2011-03-04 17:02:22 +08:00
return true;
2011-02-23 18:22:05 +08:00
}
2013-04-26 09:22:26 +08:00
2013-06-20 14:13:12 +08:00
bool Image::saveToFile(const char *pszFilePath, bool bIsToRGB)
2011-07-29 15:57:27 +08:00
{
2012-04-19 14:35:52 +08:00
bool saveToPNG = false;
bool needToCopyPixels = false;
std::string filePath(pszFilePath);
if (std::string::npos != filePath.find(".png"))
{
saveToPNG = true;
}
int bitsPerComponent = 8;
2013-06-15 14:03:30 +08:00
int bitsPerPixel = _hasAlpha ? 32 : 24;
2012-04-19 14:35:52 +08:00
if ((! saveToPNG) || bIsToRGB)
{
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-06-15 14:03:30 +08:00
if ((saveToPNG && _hasAlpha && bIsToRGB)
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-06-15 14:03:30 +08:00
if (saveToPNG && _hasAlpha && (! bIsToRGB))
2012-04-19 14:35:52 +08:00
{
bitmapInfo |= kCGImageAlphaPremultipliedLast;
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, myDataLength, NULL);
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,
NULL, false,
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);
}
[data writeToFile:[NSString stringWithUTF8String:pszFilePath] atomically:YES];
[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