CCImage.js 8.6 KB

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