TGAlib.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "base/TGAlib.h"
  24. #include "base/CCData.h"
  25. #include "platform/CCFileUtils.h"
  26. NS_CC_BEGIN
  27. static bool tgaLoadRLEImageData(unsigned char* Buffer, unsigned long bufSize, tImageTGA *info);
  28. void tgaFlipImage( tImageTGA *info );
  29. // load the image header field from stream
  30. bool tgaLoadHeader(unsigned char* buffer, unsigned long bufSize, tImageTGA *info)
  31. {
  32. bool ret = false;
  33. do
  34. {
  35. size_t step = sizeof(unsigned char) * 2;
  36. CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
  37. memcpy(&info->type, buffer + step, sizeof(unsigned char));
  38. step += sizeof(unsigned char) * 2;
  39. step += sizeof(signed short) * 4;
  40. CC_BREAK_IF((step + sizeof(signed short) * 2 + sizeof(unsigned char)) > bufSize);
  41. memcpy(&info->width, buffer + step, sizeof(signed short));
  42. memcpy(&info->height, buffer + step + sizeof(signed short), sizeof(signed short));
  43. memcpy(&info->pixelDepth, buffer + step + sizeof(signed short) * 2, sizeof(unsigned char));
  44. step += sizeof(unsigned char);
  45. step += sizeof(signed short) * 2;
  46. CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
  47. unsigned char cGarbage;
  48. memcpy(&cGarbage, buffer + step, sizeof(unsigned char));
  49. info->flipped = 0;
  50. if ( cGarbage & 0x20 )
  51. {
  52. info->flipped = 1;
  53. }
  54. ret = true;
  55. } while (0);
  56. return ret;
  57. }
  58. bool tgaLoadImageData(unsigned char *Buffer, unsigned long bufSize, tImageTGA *info)
  59. {
  60. bool ret = false;
  61. do
  62. {
  63. int mode,total,i;
  64. unsigned char aux;
  65. size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
  66. // mode equal the number of components for each pixel
  67. mode = info->pixelDepth / 8;
  68. // total is the number of unsigned chars we'll have to read
  69. total = info->height * info->width * mode;
  70. size_t dataSize = sizeof(unsigned char) * total;
  71. CC_BREAK_IF((step + dataSize) > bufSize);
  72. memcpy(info->imageData, Buffer + step, dataSize);
  73. // mode=3 or 4 implies that the image is RGB(A). However TGA
  74. // stores it as BGR(A) so we'll have to swap R and B.
  75. if (mode >= 3)
  76. {
  77. for (i=0; i < total; i+= mode)
  78. {
  79. aux = info->imageData[i];
  80. info->imageData[i] = info->imageData[i+2];
  81. info->imageData[i+2] = aux;
  82. }
  83. }
  84. ret = true;
  85. } while (0);
  86. return ret;
  87. }
  88. static bool tgaLoadRLEImageData(unsigned char* buffer, unsigned long bufSize, tImageTGA *info)
  89. {
  90. unsigned int mode,total,i, index = 0;
  91. unsigned char aux[4], runlength = 0;
  92. unsigned int skip = 0, flag = 0;
  93. size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
  94. // mode equal the number of components for each pixel
  95. mode = info->pixelDepth / 8;
  96. // total is the number of unsigned chars we'll have to read
  97. total = info->height * info->width;
  98. for( i = 0; i < total; i++ )
  99. {
  100. // if we have a run length pending, run it
  101. if ( runlength != 0 )
  102. {
  103. // we do, update the run length count
  104. runlength--;
  105. skip = (flag != 0);
  106. }
  107. else
  108. {
  109. // otherwise, read in the run length token
  110. CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
  111. memcpy(&runlength, buffer + step, sizeof(unsigned char));
  112. step += sizeof(unsigned char);
  113. // see if it's a RLE encoded sequence
  114. flag = runlength & 0x80;
  115. if ( flag )
  116. {
  117. runlength -= 128;
  118. }
  119. skip = 0;
  120. }
  121. // do we need to skip reading this pixel?
  122. if ( !skip )
  123. {
  124. // no, read in the pixel data
  125. CC_BREAK_IF((step + sizeof(unsigned char) * mode) > bufSize);
  126. memcpy(aux, buffer + step, sizeof(unsigned char) * mode);
  127. step += sizeof(unsigned char) * mode;
  128. // mode=3 or 4 implies that the image is RGB(A). However TGA
  129. // stores it as BGR(A) so we'll have to swap R and B.
  130. if ( mode >= 3 )
  131. {
  132. unsigned char tmp;
  133. tmp = aux[0];
  134. aux[0] = aux[2];
  135. aux[2] = tmp;
  136. }
  137. }
  138. // add the pixel to our image
  139. memcpy(&info->imageData[index], aux, mode);
  140. index += mode;
  141. }
  142. return true;
  143. }
  144. void tgaFlipImage( tImageTGA *info )
  145. {
  146. // mode equal the number of components for each pixel
  147. int mode = info->pixelDepth / 8;
  148. int rowbytes = info->width*mode;
  149. unsigned char *row = (unsigned char *)malloc(rowbytes);
  150. int y;
  151. if (row == nullptr) return;
  152. for( y = 0; y < (info->height/2); y++ )
  153. {
  154. memcpy(row, &info->imageData[y*rowbytes],rowbytes);
  155. memcpy(&info->imageData[y*rowbytes], &info->imageData[(info->height-(y+1))*rowbytes], rowbytes);
  156. memcpy(&info->imageData[(info->height-(y+1))*rowbytes], row, rowbytes);
  157. }
  158. free(row);
  159. info->flipped = 0;
  160. }
  161. tImageTGA* tgaLoadBuffer(unsigned char* buffer, long size)
  162. {
  163. int mode,total;
  164. tImageTGA *info = nullptr;
  165. do
  166. {
  167. CC_BREAK_IF(! buffer);
  168. info = (tImageTGA *)malloc(sizeof(tImageTGA));
  169. // get the file header info
  170. if (! tgaLoadHeader(buffer, size, info))
  171. {
  172. info->status = TGA_ERROR_MEMORY;
  173. break;
  174. }
  175. // check if the image is color indexed
  176. if (info->type == 1)
  177. {
  178. info->status = TGA_ERROR_INDEXED_COLOR;
  179. break;
  180. }
  181. // check for other types (compressed images)
  182. if ((info->type != 2) && (info->type !=3) && (info->type !=10) )
  183. {
  184. info->status = TGA_ERROR_COMPRESSED_FILE;
  185. break;
  186. }
  187. // mode equals the number of image components
  188. mode = info->pixelDepth / 8;
  189. // total is the number of unsigned chars to read
  190. total = info->height * info->width * mode;
  191. // allocate memory for image pixels
  192. info->imageData = (unsigned char *)malloc(sizeof(unsigned char) * total);
  193. // check to make sure we have the memory required
  194. if (info->imageData == nullptr)
  195. {
  196. info->status = TGA_ERROR_MEMORY;
  197. break;
  198. }
  199. bool bLoadImage = false;
  200. // finally load the image pixels
  201. if ( info->type == 10 )
  202. {
  203. bLoadImage = tgaLoadRLEImageData(buffer, size, info);
  204. }
  205. else
  206. {
  207. bLoadImage = tgaLoadImageData(buffer, size, info);
  208. }
  209. // check for errors when reading the pixels
  210. if (! bLoadImage)
  211. {
  212. info->status = TGA_ERROR_READING_FILE;
  213. break;
  214. }
  215. info->status = TGA_OK;
  216. if ( info->flipped )
  217. {
  218. tgaFlipImage( info );
  219. if ( info->flipped )
  220. {
  221. info->status = TGA_ERROR_MEMORY;
  222. }
  223. }
  224. } while(0);
  225. return info;
  226. }
  227. // this is the function to call when we want to load an image
  228. tImageTGA * tgaLoad(const char *filename)
  229. {
  230. Data data = FileUtils::getInstance()->getDataFromFile(filename);
  231. if (!data.isNull())
  232. {
  233. return tgaLoadBuffer(data.getBytes(), data.getSize());
  234. }
  235. return nullptr;
  236. }
  237. // converts RGB to grayscale
  238. void tgaRGBtogreyscale(tImageTGA *info) {
  239. int mode,i,j;
  240. unsigned char *newImageData;
  241. // if the image is already grayscale do nothing
  242. if (info->pixelDepth == 8)
  243. return;
  244. // compute the number of actual components
  245. mode = info->pixelDepth / 8;
  246. // allocate an array for the new image data
  247. newImageData = (unsigned char *)malloc(sizeof(unsigned char) *
  248. info->height * info->width);
  249. if (newImageData == nullptr) {
  250. return;
  251. }
  252. // convert pixels: grayscale = o.30 * R + 0.59 * G + 0.11 * B
  253. for (i = 0,j = 0; j < info->width * info->height; i +=mode, j++)
  254. newImageData[j] =
  255. (unsigned char)(0.30 * info->imageData[i] +
  256. 0.59 * info->imageData[i+1] +
  257. 0.11 * info->imageData[i+2]);
  258. //free old image data
  259. free(info->imageData);
  260. // reassign pixelDepth and type according to the new image type
  261. info->pixelDepth = 8;
  262. info->type = 3;
  263. // reassigning imageData to the new array.
  264. info->imageData = newImageData;
  265. }
  266. // releases the memory used for the image
  267. void tgaDestroy(tImageTGA *info) {
  268. if (info != nullptr) {
  269. if (info->imageData != nullptr)
  270. {
  271. free(info->imageData);
  272. }
  273. free(info);
  274. }
  275. }
  276. NS_CC_END