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"
|
|
|
|
#import <string>
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
2011-07-29 10:15:10 +08:00
|
|
|
// FontLabel support
|
2011-08-01 18:22:47 +08:00
|
|
|
#import "FontLabel/FontManager.h"
|
|
|
|
#import "FontLabel/FontLabelStringDrawing.h"
|
2011-02-23 18:22:05 +08:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2011-03-04 17:02:22 +08:00
|
|
|
unsigned int height;
|
|
|
|
unsigned int width;
|
|
|
|
int bitsPerComponent;
|
|
|
|
bool hasAlpha;
|
|
|
|
bool isPremultipliedAlpha;
|
|
|
|
unsigned char* data;
|
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
|
|
|
|
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;
|
|
|
|
pImageinfo->data = new unsigned char[pImageinfo->width * pImageinfo->height * 4];
|
|
|
|
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 _initWithFile(const char* path, tImageInfo *pImageinfo)
|
|
|
|
{
|
2011-03-04 17:02:22 +08:00
|
|
|
CGImageRef CGImage;
|
|
|
|
UIImage *jpg;
|
|
|
|
UIImage *png;
|
|
|
|
bool ret;
|
|
|
|
|
2011-08-08 11:01:47 +08:00
|
|
|
// convert jpg to png before loading the texture
|
2011-03-04 17:02:22 +08:00
|
|
|
|
|
|
|
NSString *fullPath = [NSString stringWithUTF8String:path];
|
|
|
|
jpg = [[UIImage alloc] initWithContentsOfFile: fullPath];
|
|
|
|
png = [[UIImage alloc] initWithData:UIImagePNGRepresentation(jpg)];
|
|
|
|
CGImage = png.CGImage;
|
|
|
|
|
|
|
|
ret = _initWithImage(CGImage, pImageinfo);
|
|
|
|
|
|
|
|
[png release];
|
|
|
|
[jpg release];
|
|
|
|
|
|
|
|
return ret;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static bool _isValidFontName(const char *fontName)
|
|
|
|
{
|
2011-03-04 17:02:22 +08:00
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
NSString *fontNameNS = [NSString stringWithUTF8String:fontName];
|
|
|
|
|
2011-08-08 11:01:47 +08:00
|
|
|
for (NSString *familiName in [UIFont familyNames])
|
|
|
|
{
|
|
|
|
if ([familiName isEqualToString:fontNameNS])
|
|
|
|
{
|
2011-03-04 17:02:22 +08:00
|
|
|
ret = true;
|
|
|
|
goto out;
|
2011-02-23 18:22:05 +08:00
|
|
|
}
|
|
|
|
|
2011-08-08 11:01:47 +08:00
|
|
|
for(NSString *font in [UIFont fontNamesForFamilyName: familiName])
|
|
|
|
{
|
|
|
|
if ([font isEqualToString: fontNameNS])
|
|
|
|
{
|
2011-03-04 17:02:22 +08:00
|
|
|
ret = true;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
2011-02-23 18:22:05 +08:00
|
|
|
}
|
|
|
|
|
2012-05-07 10:15:57 +08:00
|
|
|
static CGSize _calculateStringSizeWithFontOrZFont(NSString *str, id font, CGSize *constrainSize, bool isZfont)
|
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)
|
|
|
|
{
|
|
|
|
CGSize tmp;
|
|
|
|
if (isZfont)
|
|
|
|
{
|
2012-05-07 10:15:57 +08:00
|
|
|
tmp = [FontLabelStringDrawingHelper sizeWithZFont:s zfont:font constrainedToSize:textRect];
|
2011-08-23 16:19:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-02 11:15:44 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:20:58 +08:00
|
|
|
// refer CCImage::ETextAlign
|
|
|
|
#define ALIGN_TOP 1
|
|
|
|
#define ALIGN_CENTER 3
|
|
|
|
#define ALIGN_BOTTOM 2
|
|
|
|
|
2011-03-09 16:19:20 +08:00
|
|
|
static bool _initWithString(const char * pText, cocos2d::CCImage::ETextAlign 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
|
|
|
|
2011-08-01 18:22:47 +08:00
|
|
|
NSString * str = [NSString stringWithUTF8String:pText];
|
2011-07-29 10:15:10 +08:00
|
|
|
NSString * fntName = [NSString stringWithUTF8String:pFontName];
|
2011-10-14 14:33:08 +08:00
|
|
|
CGSize dim, constrainSize;
|
|
|
|
constrainSize.width = pInfo->width;
|
|
|
|
constrainSize.height = pInfo->height;
|
2011-03-04 17:02:22 +08:00
|
|
|
|
2011-08-01 18:22:47 +08:00
|
|
|
// create the font
|
|
|
|
id font;
|
|
|
|
font = [UIFont fontWithName:fntName size:nSize];
|
2011-07-29 15:57:27 +08:00
|
|
|
if (font)
|
|
|
|
{
|
2012-05-07 10:15:57 +08:00
|
|
|
dim = _calculateStringSizeWithFontOrZFont(str, font, &constrainSize, false);
|
2011-07-29 15:57:27 +08:00
|
|
|
}
|
2011-07-29 10:15:10 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (! font)
|
|
|
|
{
|
|
|
|
font = [[FontManager sharedManager] zFontWithName:fntName pointSize:nSize];
|
|
|
|
if (font)
|
2011-07-29 15:57:27 +08:00
|
|
|
{
|
2012-06-16 05:06:01 +08:00
|
|
|
dim =_calculateStringSizeWithFontOrZFont(str, font, &constrainSize, true);
|
2011-07-29 15:57:27 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-07-29 10:15:10 +08:00
|
|
|
|
|
|
|
if (! font)
|
2011-02-23 18:22:05 +08:00
|
|
|
{
|
2011-07-29 10:15:10 +08:00
|
|
|
fntName = _isValidFontName(pFontName) ? fntName : @"MarkerFelt-Wide";
|
|
|
|
font = [UIFont fontWithName:fntName size:nSize];
|
|
|
|
|
|
|
|
if (! font)
|
|
|
|
{
|
|
|
|
font = [UIFont systemFontOfSize:nSize];
|
|
|
|
}
|
2011-07-29 15:57:27 +08:00
|
|
|
|
|
|
|
if (font)
|
|
|
|
{
|
2012-05-07 10:15:57 +08:00
|
|
|
dim = _calculateStringSizeWithFontOrZFont(str, font, &constrainSize, false);
|
2011-07-29 15:57:27 +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
|
|
|
|
unsigned int vAlignment = (eAlign >> 4) & 0x0F;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-14 14:33:08 +08:00
|
|
|
unsigned char* data = new unsigned char[(int)(dim.width * dim.height * 4)];
|
|
|
|
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
|
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGContextRef context = CGBitmapContextCreate(data, dim.width, dim.height, 8, dim.width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
|
2011-02-23 18:22:05 +08:00
|
|
|
CGColorSpaceRelease(colorSpace);
|
2011-07-29 15:57:27 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (! context)
|
|
|
|
{
|
2012-06-13 16:20:58 +08:00
|
|
|
delete[] data;
|
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-03-04 17:02:22 +08:00
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
|
2011-07-29 15:57:27 +08:00
|
|
|
CGContextTranslateCTM(context, 0.0f, dim.height);
|
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
|
|
|
|
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
|
|
|
|
unsigned uHoriFlag = 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);
|
2011-07-29 10:15:10 +08:00
|
|
|
|
|
|
|
// normal fonts
|
2012-06-16 05:06:01 +08:00
|
|
|
if( [font isKindOfClass:[UIFont class] ] )
|
|
|
|
{
|
2012-07-11 12:01:32 +08:00
|
|
|
[str drawInRect:CGRectMake(0, startH, dim.width, dim.height) withFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap alignment:align];
|
2012-06-16 05:06:01 +08:00
|
|
|
}
|
|
|
|
else // ZFont class
|
|
|
|
{
|
2012-07-09 13:37:19 +08:00
|
|
|
[FontLabelStringDrawingHelper drawInRect:str rect:CGRectMake(0, startH, dim.width, dim.height) withZFont:font lineBreakMode:(UILineBreakMode)UILineBreakModeWordWrap alignment:align];
|
2012-06-16 05:06:01 +08:00
|
|
|
}
|
2011-03-04 17:02:22 +08:00
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
UIGraphicsPopContext();
|
2011-03-04 17:02:22 +08:00
|
|
|
|
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
|
|
|
|
pInfo->data = data;
|
|
|
|
pInfo->hasAlpha = true;
|
|
|
|
pInfo->isPremultipliedAlpha = true;
|
|
|
|
pInfo->bitsPerComponent = 8;
|
2011-10-14 14:33:08 +08:00
|
|
|
pInfo->width = dim.width;
|
|
|
|
pInfo->height = dim.height;
|
2011-03-04 17:02:22 +08:00
|
|
|
bRet = true;
|
|
|
|
} 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
|
|
|
|
2011-03-09 16:19:20 +08:00
|
|
|
CCImage::CCImage()
|
2011-02-23 18:22:05 +08:00
|
|
|
: m_nWidth(0)
|
|
|
|
, m_nHeight(0)
|
|
|
|
, m_nBitsPerComponent(0)
|
2011-03-28 13:41:14 +08:00
|
|
|
, m_pData(0)
|
2011-02-23 18:22:05 +08:00
|
|
|
, m_bHasAlpha(false)
|
|
|
|
, m_bPreMulti(false)
|
|
|
|
{
|
2011-03-04 17:02:22 +08:00
|
|
|
|
2011-02-23 18:22:05 +08:00
|
|
|
}
|
|
|
|
|
2011-03-28 13:41:14 +08:00
|
|
|
CCImage::~CCImage()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE_ARRAY(m_pData);
|
|
|
|
}
|
|
|
|
|
2011-03-09 16:19:20 +08:00
|
|
|
bool CCImage::initWithImageFile(const char * strPath, EImageFormat 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;
|
|
|
|
unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(
|
|
|
|
CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(strPath),
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
2011-11-25 10:09:29 +08:00
|
|
|
bool CCImage::initWithImageFileThreadSafe(const char *fullpath, EImageFormat imageType)
|
|
|
|
{
|
2012-06-19 16:31:26 +08:00
|
|
|
/*
|
|
|
|
* CCFileUtils::fullPathFromRelativePath() is not thread-safe, it use autorelease().
|
|
|
|
*/
|
|
|
|
bool bRet = false;
|
|
|
|
unsigned long nSize = 0;
|
|
|
|
unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(fullpath, "rb", &nSize);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-29 10:15:10 +08:00
|
|
|
bool CCImage::initWithImageData(void * pData,
|
|
|
|
int nDataLen,
|
|
|
|
EImageFormat eFmt,
|
|
|
|
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};
|
|
|
|
do
|
|
|
|
{
|
2011-03-09 16:19:20 +08:00
|
|
|
CC_BREAK_IF(! pData || nDataLen <= 0);
|
2011-08-08 11:01:47 +08:00
|
|
|
if (eFmt == kFmtRawData)
|
|
|
|
{
|
|
|
|
bRet = _initWithRawData(pData, nDataLen, nWidth, nHeight, nBitsPerComponent);
|
|
|
|
}
|
|
|
|
else // init with png or jpg file data
|
|
|
|
{
|
|
|
|
bRet = _initWithData(pData, nDataLen, &info);
|
|
|
|
if (bRet)
|
|
|
|
{
|
|
|
|
m_nHeight = (short)info.height;
|
|
|
|
m_nWidth = (short)info.width;
|
|
|
|
m_nBitsPerComponent = info.bitsPerComponent;
|
|
|
|
m_bHasAlpha = info.hasAlpha;
|
|
|
|
m_bPreMulti = info.isPremultipliedAlpha;
|
|
|
|
m_pData = info.data;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2011-08-08 11:01:47 +08:00
|
|
|
bool CCImage::_initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(0 == nWidth || 0 == nHeight);
|
|
|
|
|
|
|
|
m_nBitsPerComponent = nBitsPerComponent;
|
|
|
|
m_nHeight = (short)nHeight;
|
|
|
|
m_nWidth = (short)nWidth;
|
|
|
|
m_bHasAlpha = true;
|
|
|
|
|
|
|
|
// only RGBA8888 surported
|
|
|
|
int nBytesPerComponent = 4;
|
|
|
|
int nSize = nHeight * nWidth * nBytesPerComponent;
|
|
|
|
m_pData = new unsigned char[nSize];
|
|
|
|
CC_BREAK_IF(! m_pData);
|
|
|
|
memcpy(m_pData, pData, nSize);
|
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
2011-08-08 11:01:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCImage::_initWithJpgData(void *pData, int nDatalen)
|
|
|
|
{
|
|
|
|
assert(0);
|
2012-06-09 04:52:40 +08:00
|
|
|
return false;
|
2011-08-08 11:01:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCImage::_initWithPngData(void *pData, int nDatalen)
|
|
|
|
{
|
|
|
|
assert(0);
|
2012-06-09 04:52:40 +08:00
|
|
|
return false;
|
2011-08-08 11:01:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCImage::_saveImageToPNG(const char *pszFilePath, bool bIsToRGB)
|
|
|
|
{
|
|
|
|
assert(0);
|
2012-06-09 04:52:40 +08:00
|
|
|
return false;
|
2011-08-08 11:01:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCImage::_saveImageToJPG(const char *pszFilePath)
|
|
|
|
{
|
|
|
|
assert(0);
|
2012-06-09 04:52:40 +08:00
|
|
|
return false;
|
2011-08-08 11:01:47 +08:00
|
|
|
}
|
|
|
|
|
2011-03-09 16:19:20 +08:00
|
|
|
bool CCImage::initWithString(
|
2011-03-04 17:02:22 +08:00
|
|
|
const char * pText,
|
|
|
|
int nWidth /* = 0 */,
|
|
|
|
int nHeight /* = 0 */,
|
|
|
|
ETextAlign eAlignMask /* = kAlignCenter */,
|
|
|
|
const char * pFontName /* = nil */,
|
|
|
|
int nSize /* = 0 */)
|
|
|
|
{
|
|
|
|
tImageInfo info = {0};
|
|
|
|
info.width = nWidth;
|
|
|
|
info.height = nHeight;
|
|
|
|
|
|
|
|
if (! _initWithString(pText, eAlignMask, pFontName, nSize, &info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-28 13:41:14 +08:00
|
|
|
m_nHeight = (short)info.height;
|
|
|
|
m_nWidth = (short)info.width;
|
2011-03-04 17:02:22 +08:00
|
|
|
m_nBitsPerComponent = info.bitsPerComponent;
|
|
|
|
m_bHasAlpha = info.hasAlpha;
|
|
|
|
m_bPreMulti = info.isPremultipliedAlpha;
|
2011-03-28 13:41:14 +08:00
|
|
|
m_pData = info.data;
|
2011-03-04 17:02:22 +08:00
|
|
|
|
|
|
|
return true;
|
2011-02-23 18:22:05 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 15:57:27 +08:00
|
|
|
bool CCImage::saveToFile(const char *pszFilePath, bool bIsToRGB)
|
|
|
|
{
|
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;
|
|
|
|
int bitsPerPixel = m_bHasAlpha ? 32 : 24;
|
|
|
|
if ((! saveToPNG) || bIsToRGB)
|
|
|
|
{
|
|
|
|
bitsPerPixel = 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bytesPerRow = (bitsPerPixel/8) * m_nWidth;
|
|
|
|
int myDataLength = bytesPerRow * m_nHeight;
|
|
|
|
|
|
|
|
unsigned char *pixels = m_pData;
|
|
|
|
|
|
|
|
// 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.
|
2012-04-19 14:35:52 +08:00
|
|
|
if ((saveToPNG && m_bHasAlpha && bIsToRGB)
|
2011-08-08 11:01:47 +08:00
|
|
|
|| (! saveToPNG))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pixels = new unsigned char[myDataLength];
|
|
|
|
|
|
|
|
for (int i = 0; i < m_nHeight; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < m_nWidth; ++j)
|
|
|
|
{
|
|
|
|
pixels[(i * m_nWidth + j) * 3] = m_pData[(i * m_nWidth + j) * 4];
|
|
|
|
pixels[(i * m_nWidth + j) * 3 + 1] = m_pData[(i * m_nWidth + j) * 4 + 1];
|
|
|
|
pixels[(i * m_nWidth + j) * 3 + 2] = m_pData[(i * m_nWidth + j) * 4 + 2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
needToCopyPixels = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make data provider with data.
|
|
|
|
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
|
|
|
|
if (saveToPNG && m_bHasAlpha && (! bIsToRGB))
|
|
|
|
{
|
|
|
|
bitmapInfo |= kCGImageAlphaPremultipliedLast;
|
|
|
|
}
|
|
|
|
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, myDataLength, NULL);
|
|
|
|
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGImageRef iref = CGImageCreate(m_nWidth, m_nHeight,
|
|
|
|
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
|
|
|
|