TGAlib.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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. #include <string.h>
  23. #include <stdlib.h>
  24. #include "base/TGAlib.h"
  25. #include "base/CCData.h"
  26. #include "platform/CCFileUtils.h"
  27. NS_CC_BEGIN
  28. static bool tgaLoadRLEImageData(unsigned char* Buffer, unsigned long bufSize, tImageTGA *info);
  29. void tgaFlipImage( tImageTGA *info );
  30. // load the image header field from stream
  31. bool tgaLoadHeader(unsigned char* buffer, unsigned long bufSize, tImageTGA *info)
  32. {
  33. bool ret = false;
  34. do
  35. {
  36. size_t step = sizeof(unsigned char) * 2;
  37. CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
  38. memcpy(&info->type, buffer + step, sizeof(unsigned char));
  39. step += sizeof(unsigned char) * 2;
  40. step += sizeof(signed short) * 4;
  41. CC_BREAK_IF((step + sizeof(signed short) * 2 + sizeof(unsigned char)) > bufSize);
  42. memcpy(&info->width, buffer + step, sizeof(signed short));
  43. memcpy(&info->height, buffer + step + sizeof(signed short), sizeof(signed short));
  44. memcpy(&info->pixelDepth, buffer + step + sizeof(signed short) * 2, sizeof(unsigned char));
  45. step += sizeof(unsigned char);
  46. step += sizeof(signed short) * 2;
  47. CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
  48. unsigned char cGarbage;
  49. memcpy(&cGarbage, buffer + step, sizeof(unsigned char));
  50. info->flipped = 0;
  51. if ( cGarbage & 0x20 )
  52. {
  53. info->flipped = 1;
  54. }
  55. ret = true;
  56. } while (0);
  57. return ret;
  58. }
  59. bool tgaLoadImageData(unsigned char *Buffer, unsigned long bufSize, tImageTGA *info)
  60. {
  61. bool ret = false;
  62. do
  63. {
  64. int mode,total,i;
  65. unsigned char aux;
  66. size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
  67. // mode equal the number of components for each pixel
  68. mode = info->pixelDepth / 8;
  69. // total is the number of unsigned chars we'll have to read
  70. total = info->height * info->width * mode;
  71. size_t dataSize = sizeof(unsigned char) * total;
  72. CC_BREAK_IF((step + dataSize) > bufSize);
  73. memcpy(info->imageData, Buffer + step, dataSize);
  74. // mode=3 or 4 implies that the image is RGB(A). However TGA
  75. // stores it as BGR(A) so we'll have to swap R and B.
  76. if (mode >= 3)
  77. {
  78. for (i=0; i < total; i+= mode)
  79. {
  80. aux = info->imageData[i];
  81. info->imageData[i] = info->imageData[i+2];
  82. info->imageData[i+2] = aux;
  83. }
  84. }
  85. ret = true;
  86. } while (0);
  87. return ret;
  88. }
  89. static bool tgaLoadRLEImageData(unsigned char* buffer, unsigned long bufSize, tImageTGA *info)
  90. {
  91. unsigned int mode,total,i, index = 0;
  92. unsigned char aux[4], runlength = 0;
  93. unsigned int skip = 0, flag = 0;
  94. size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
  95. // mode equal the number of components for each pixel
  96. mode = info->pixelDepth / 8;
  97. // total is the number of unsigned chars we'll have to read
  98. total = info->height * info->width;
  99. for( i = 0; i < total; i++ )
  100. {
  101. // if we have a run length pending, run it
  102. if ( runlength != 0 )
  103. {
  104. // we do, update the run length count
  105. runlength--;
  106. skip = (flag != 0);
  107. }
  108. else
  109. {
  110. // otherwise, read in the run length token
  111. CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
  112. memcpy(&runlength, buffer + step, sizeof(unsigned char));
  113. step += sizeof(unsigned char);
  114. // see if it's a RLE encoded sequence
  115. flag = runlength & 0x80;
  116. if ( flag )
  117. {
  118. runlength -= 128;
  119. }
  120. skip = 0;
  121. }
  122. // do we need to skip reading this pixel?
  123. if ( !skip )
  124. {
  125. // no, read in the pixel data
  126. CC_BREAK_IF((step + sizeof(unsigned char) * mode) > bufSize);
  127. memcpy(aux, buffer + step, sizeof(unsigned char) * mode);
  128. step += sizeof(unsigned char) * mode;
  129. // mode=3 or 4 implies that the image is RGB(A). However TGA
  130. // stores it as BGR(A) so we'll have to swap R and B.
  131. if ( mode >= 3 )
  132. {
  133. unsigned char tmp;
  134. tmp = aux[0];
  135. aux[0] = aux[2];
  136. aux[2] = tmp;
  137. }
  138. }
  139. // add the pixel to our image
  140. memcpy(&info->imageData[index], aux, mode);
  141. index += mode;
  142. }
  143. return true;
  144. }
  145. void tgaFlipImage( tImageTGA *info )
  146. {
  147. // mode equal the number of components for each pixel
  148. int mode = info->pixelDepth / 8;
  149. int rowbytes = info->width*mode;
  150. unsigned char *row = (unsigned char *)malloc(rowbytes);
  151. int y;
  152. if (row == nullptr) return;
  153. for( y = 0; y < (info->height/2); y++ )
  154. {
  155. memcpy(row, &info->imageData[y*rowbytes],rowbytes);
  156. memcpy(&info->imageData[y*rowbytes], &info->imageData[(info->height-(y+1))*rowbytes], rowbytes);
  157. memcpy(&info->imageData[(info->height-(y+1))*rowbytes], row, rowbytes);
  158. }
  159. free(row);
  160. info->flipped = 0;
  161. }
  162. tImageTGA* tgaLoadBuffer(unsigned char* buffer, long size)
  163. {
  164. int mode,total;
  165. tImageTGA *info = nullptr;
  166. do
  167. {
  168. CC_BREAK_IF(! buffer);
  169. info = (tImageTGA *)malloc(sizeof(tImageTGA));
  170. // get the file header info
  171. if (! tgaLoadHeader(buffer, size, info))
  172. {
  173. info->status = TGA_ERROR_MEMORY;
  174. break;
  175. }
  176. // check if the image is color indexed
  177. if (info->type == 1)
  178. {
  179. info->status = TGA_ERROR_INDEXED_COLOR;
  180. break;
  181. }
  182. // check for other types (compressed images)
  183. if ((info->type != 2) && (info->type !=3) && (info->type !=10) )
  184. {
  185. info->status = TGA_ERROR_COMPRESSED_FILE;
  186. break;
  187. }
  188. // mode equals the number of image components
  189. mode = info->pixelDepth / 8;
  190. // total is the number of unsigned chars to read
  191. total = info->height * info->width * mode;
  192. // allocate memory for image pixels
  193. info->imageData = (unsigned char *)malloc(sizeof(unsigned char) * total);
  194. // check to make sure we have the memory required
  195. if (info->imageData == nullptr)
  196. {
  197. info->status = TGA_ERROR_MEMORY;
  198. break;
  199. }
  200. bool bLoadImage = false;
  201. // finally load the image pixels
  202. if ( info->type == 10 )
  203. {
  204. bLoadImage = tgaLoadRLEImageData(buffer, size, info);
  205. }
  206. else
  207. {
  208. bLoadImage = tgaLoadImageData(buffer, size, info);
  209. }
  210. // check for errors when reading the pixels
  211. if (! bLoadImage)
  212. {
  213. info->status = TGA_ERROR_READING_FILE;
  214. break;
  215. }
  216. info->status = TGA_OK;
  217. if ( info->flipped )
  218. {
  219. tgaFlipImage( info );
  220. if ( info->flipped )
  221. {
  222. info->status = TGA_ERROR_MEMORY;
  223. }
  224. }
  225. } while(0);
  226. return info;
  227. }
  228. // this is the function to call when we want to load an image
  229. tImageTGA * tgaLoad(const char *filename)
  230. {
  231. Data data = FileUtils::getInstance()->getDataFromFile(filename);
  232. if (!data.isNull())
  233. {
  234. return tgaLoadBuffer(data.getBytes(), data.getSize());
  235. }
  236. return nullptr;
  237. }
  238. // converts RGB to grayscale
  239. void tgaRGBtogreyscale(tImageTGA *info) {
  240. int mode,i,j;
  241. unsigned char *newImageData;
  242. // if the image is already grayscale do nothing
  243. if (info->pixelDepth == 8)
  244. return;
  245. // compute the number of actual components
  246. mode = info->pixelDepth / 8;
  247. // allocate an array for the new image data
  248. newImageData = (unsigned char *)malloc(sizeof(unsigned char) *
  249. info->height * info->width);
  250. if (newImageData == nullptr) {
  251. return;
  252. }
  253. // convert pixels: grayscale = o.30 * R + 0.59 * G + 0.11 * B
  254. for (i = 0,j = 0; j < info->width * info->height; i +=mode, j++)
  255. newImageData[j] =
  256. (unsigned char)(0.30 * info->imageData[i] +
  257. 0.59 * info->imageData[i+1] +
  258. 0.11 * info->imageData[i+2]);
  259. //free old image data
  260. free(info->imageData);
  261. // reassign pixelDepth and type according to the new image type
  262. info->pixelDepth = 8;
  263. info->type = 3;
  264. // reassigning imageData to the new array.
  265. info->imageData = newImageData;
  266. }
  267. // releases the memory used for the image
  268. void tgaDestroy(tImageTGA *info) {
  269. if (info != nullptr) {
  270. if (info->imageData != nullptr)
  271. {
  272. free(info->imageData);
  273. }
  274. free(info);
  275. }
  276. }
  277. NS_CC_END