1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * Horizontal center and vertical center. 29 * @constant 30 * @type Number 31 */ 32 cc.ALIGN_CENTER = 0x33; 33 34 /** 35 * Horizontal center and vertical top. 36 * @constant 37 * @type Number 38 */ 39 cc.ALIGN_TOP = 0x13; 40 41 /** 42 * Horizontal right and vertical top. 43 * @constant 44 * @type Number 45 */ 46 cc.ALIGN_TOP_RIGHT = 0x12; 47 48 /** 49 * Horizontal right and vertical center. 50 * @constant 51 * @type Number 52 */ 53 cc.ALIGN_RIGHT = 0x32; 54 55 /** 56 * Horizontal right and vertical bottom. 57 * @constant 58 * @type Number 59 */ 60 cc.ALIGN_BOTTOM_RIGHT = 0x22; 61 62 /** 63 * Horizontal center and vertical bottom. 64 * @constant 65 * @type Number 66 */ 67 cc.ALIGN_BOTTOM = 0x23; 68 69 /** 70 * Horizontal left and vertical bottom. 71 * @constant 72 * @type Number 73 */ 74 cc.ALIGN_BOTTOM_LEFT = 0x21; 75 76 /** 77 * Horizontal left and vertical center. 78 * @constant 79 * @type Number 80 */ 81 cc.ALIGN_LEFT = 0x31; 82 83 /** 84 * Horizontal left and vertical top. 85 * @constant 86 * @type Number 87 */ 88 cc.ALIGN_TOP_LEFT = 0x11; 89 90 /** 91 * premultiply alpha, or the effect will wrong when want to use other pixel format in CCTexture2D, 92 * such as RGB888, RGB5A1 93 * @param {Number} vr 94 * @param {Number} vg 95 * @param {Number} vb 96 * @param {Number} va 97 * @return {Number} 98 * @constructor 99 */ 100 cc.RGB_PREMULTIPLY_APLHA = function (vr, vg, vb, va) { 101 return ((vr * (va + 1)) >> 8) | ((vg * (va + 1) >> 8) << 8) | ((vb * (va + 1) >> 8) << 16) | ((va) << 24) 102 } 103 104 /** 105 * image source 106 * @Class 107 * @Construct 108 * @param {Array||String} data 109 * @param {Number} size 110 * @param {Number} offset 111 */ 112 cc.tImageSource = function (data, size, offset) { 113 this.data = data; 114 this.size = size || 0; 115 this.offset = offset || 0; 116 }; 117 118 cc.pngReadCallback = function (png_ptr, data, length) { 119 var isource = new cc.tImageSource(); 120 isource = cc.png_get_io_ptr(png_ptr); 121 122 if (isource.offset + length <= isource.size) { 123 cc.memcpy(data, isource.data + isource.offset, length); 124 isource.offset += length; 125 } 126 else { 127 cc.png_error(png_ptr, "pngReaderCallback failed"); 128 } 129 }; 130 131 /** 132 * Image 133 * @class 134 * @extends cc.Class 135 */ 136 cc.Image = cc.Class.extend(/** @lends cc.Image# */{ 137 _width: 0, 138 _height: 0, 139 _bitsPerComponent: 0, 140 _data: 0, 141 _hasAlpha: false, 142 _preMulti: false, 143 144 /** 145 * Load the image from the specified path. 146 * @param {String} strPath the absolute file path 147 * @param {Number} imageType the type of image, now only support tow types. 148 * @return {Boolean} true if load correctly 149 */ 150 initWithImageFile: function (strPath, imageType) { 151 var data = cc.FileUtils.getInstance().getFileData(strPath, "rb"); 152 var size = data.length; 153 if (data != null && data.length > 0) 154 return this.initWithImageData(data, data.length, imageType); 155 return false; 156 }, 157 158 /** 159 * The same meaning as initWithImageFile, but it is thread safe. It is casued by loadImage() in cc.TextureCache. 160 * @param {String} fullpath full path of the file 161 * @param {Number} imageType the type of image, now only support tow types. 162 * @return {Boolean} true if load correctly 163 */ 164 initWithImageFileThreadSafe: function (fullpath, imageType) { 165 return this.initWithImageFile(fullpath, imageType); 166 }, 167 168 /** 169 * Load image from stream buffer. 170 * @warning FMT_RAWDATA only support RGBA8888 171 * @param {Array} data stream buffer that hold the image data 172 * @param {Number} dataLen the length of data(managed in byte) 173 * @param {Number} eFmt 174 * @param {Number} width 175 * @param {Number} height 176 * @param {Number} bitsPerComponent 177 * @return {Boolean} true if load correctly 178 */ 179 initWithImageData: function (data, dataLen, eFmt, width, height, bitsPerComponent) { 180 bitsPerComponent = bitsPerComponent || 8; 181 width = width || 0; 182 height = height || 0; 183 eFmt = eFmt || cc.FMT_UNKNOWN; 184 185 if (!data || dataLen <= 0) 186 return false; 187 188 if (cc.FMT_PNG == eFmt) 189 return this._initWithPngData(data, dataLen); 190 else if (cc.FMT_JPG == eFmt) 191 return this._initWithJpgData(data, dataLen); 192 else if (cc.FMT_TIFF == eFmt) 193 return this._initWithTiffData(data, dataLen); 194 else if (cc.FMT_RAWDATA == eFmt) 195 return this._initWithRawData(data, dataLen, width, height, bitsPerComponent); 196 else { 197 // if it is a png file buffer. 198 if (dataLen > 8) { 199 if (data[0] == 0x89 200 && data[1] == 0x50 201 && data[2] == 0x4E 202 && data[3] == 0x47 203 && data[4] == 0x0D 204 && data[5] == 0x0A 205 && data[6] == 0x1A 206 && data[7] == 0x0A) { 207 return this._initWithPngData(data, dataLen); 208 } 209 } 210 211 // if it is a tiff file buffer. 212 if (dataLen > 2) { 213 if ((data[0] == 0x49 && data[1] == 0x49) 214 || (data[0] == 0x4d && data[1] == 0x4d)) { 215 return this._initWithTiffData(data, dataLen); 216 } else if (data[0] == 0xff && data[1] == 0xd8) { 217 return this._initWithTiffData(data, dataLen); 218 } 219 } 220 } 221 return false; 222 }, 223 224 getData: function () { 225 return this._data; 226 }, 227 228 getDataLen: function () { 229 return this._width * this._height; 230 }, 231 232 hasAlpha: function () { 233 return this._hasAlpha; 234 }, 235 236 isPremultipliedAlpha: function () { 237 return this._preMulti; 238 }, 239 240 getWidth: function () { 241 return this._width; 242 }, 243 244 getHeight: function () { 245 return this._height; 246 }, 247 248 getBitsPerComponent: function () { 249 return this._bitsPerComponent; 250 }, 251 252 /** 253 * Save the CCImage data to specified file with specified format. 254 * @param {String} filePath the file's absolute path, including file subfix 255 * @param {Boolean} isToRGB if the image is saved as RGB format 256 * @return {Boolean} 257 */ 258 saveToFile: function (filePath, isToRGB) { 259 // 260 cc.log("doesn't support saveToFile on Cocos2d-Html5"); 261 return false; 262 }, 263 264 /*protected:*/ 265 _initWithJpgData: function (data, dataLen) { 266 return false; 267 }, 268 269 _initWithPngData: function (data, datalen) { 270 return false; 271 }, 272 273 _initWithTiffData: function (data, dataLen) { 274 return false; 275 }, 276 277 // @warning FMT_RAWDATA only support RGBA8888 278 _initWithRawData: function (data, datalen, width, height, bitsPerComponent) { 279 return false; 280 }, 281 282 _saveImageToPNG: function (filePath, isToRGB) { 283 return false; 284 }, 285 286 _saveImageToJPG: function (filePath) { 287 return false; 288 }, 289 290 /** 291 * Create image with specified string. 292 * @param {String} text the text which the image show, nil cause init fail 293 * @param {Number} width the image width, if 0, the width match the text's width 294 * @param {Number} height the image height, if 0, the height match the text's height 295 * @param {Number} eAlignMask the test Alignment 296 * @param {String} fontName the name of the font which use to draw the text. If nil, use the default system font. 297 * @param {Number} size the font size, if 0, use the system default size. 298 * @return {Boolean} 299 */ 300 initWithString: function (text, width, height, eAlignMask, fontName, size) { 301 return false; 302 } 303 }); 304