TGAData.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "TGAData.h"
  2. USING_NS_BF;
  3. ///
  4. bool TGAData::ReadData()
  5. {
  6. size_t step = sizeof(unsigned char) * 2;
  7. //CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
  8. #pragma pack(push, 1)
  9. struct Header
  10. {
  11. char mIdLength;
  12. char mColourMapType;
  13. char mDataTypeCode;
  14. short int mColourMapOrigin;
  15. short int mColourMapLength;
  16. char mColourMapDepth;
  17. short int mXOrigin;
  18. short int mYOrigin;
  19. short mWidth;
  20. short mHeight;
  21. char mBitsPerPixel;
  22. char mImageDescriptor;
  23. };
  24. #pragma pack(pop)
  25. Header* hdr = (Header*)mSrcData;
  26. /*memcpy(&aType, mSrcData, sizeof(unsigned char));
  27. step += sizeof(unsigned char) * 2;
  28. step += sizeof(signed short) * 4;
  29. //CC_BREAK_IF((step + sizeof(signed short) * 2 + sizeof(unsigned char)) > bufSize);
  30. memcpy(&width, mSrcData + step, sizeof(signed short));
  31. memcpy(&height, mSrcData + step + sizeof(signed short), sizeof(signed short));
  32. memcpy(&pixelDepth, mSrcData + step + sizeof(signed short) * 2, sizeof(unsigned char));
  33. step += sizeof(unsigned char);
  34. step += sizeof(signed short) * 2;
  35. //CC_BREAK_IF((step + sizeof(unsigned char)) > bufSize);
  36. unsigned char cGarbage;
  37. memcpy(&cGarbage, mSrcData + step, sizeof(unsigned char));
  38. bool flipped = (cGarbage & 0x20) != 0;*/
  39. bool flipped = (hdr->mImageDescriptor & 0x20) != 0;
  40. mWidth = hdr->mWidth;
  41. mHeight = hdr->mHeight;
  42. mBits = new uint32[mWidth * mHeight];
  43. if (hdr->mDataTypeCode == 10) // RLE
  44. {
  45. int total;
  46. size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
  47. // mode equal the number of components for each pixel
  48. int aMode = hdr->mBitsPerPixel / 8;
  49. // total is the number of unsigned chars we'll have to read
  50. total = mHeight * mWidth * aMode;
  51. size_t dataSize = sizeof(unsigned char) * total;
  52. //CC_BREAK_IF((step + dataSize) > bufSize);
  53. uint8* srcPtr = mSrcData + step;
  54. uint32* destPtr = mBits;
  55. int destAdd = 0;
  56. if (!flipped)
  57. {
  58. destPtr = mBits + mWidth*(mHeight - 1);
  59. destAdd = -mWidth * 2;
  60. }
  61. if (aMode == 4)
  62. {
  63. int y = 0;
  64. int x = 0;
  65. readSpanHeader:
  66. int spanLen = 0;
  67. uint32 spanColor = 0;
  68. uint8 spanHeader = *(srcPtr++);
  69. spanLen = (spanHeader & 0x7F) + 1;
  70. if ((spanHeader & 0x80) != 0)
  71. {
  72. // Repeat color
  73. int b = *(srcPtr++);
  74. int g = *(srcPtr++);
  75. int r = *(srcPtr++);
  76. int a = *(srcPtr++);
  77. if (mWantsAlphaPremultiplied)
  78. {
  79. r = (r * a) / 255;
  80. g = (g * a) / 255;
  81. b = (b * a) / 255;
  82. }
  83. spanColor = (a << 24) | (b << 16) | (g << 8) | r;
  84. for (; y < mHeight; y++)
  85. {
  86. for (; x < mWidth; x++)
  87. {
  88. if (spanLen == 0)
  89. goto readSpanHeader;
  90. *(destPtr++) = spanColor;
  91. spanLen--;
  92. }
  93. x = 0;
  94. destPtr += destAdd;
  95. }
  96. }
  97. else
  98. {
  99. for (; y < mHeight; y++)
  100. {
  101. for (; x < mWidth; x++)
  102. {
  103. if (spanLen == 0)
  104. goto readSpanHeader;
  105. int b = *(srcPtr++);
  106. int g = *(srcPtr++);
  107. int r = *(srcPtr++);
  108. int a = *(srcPtr++);
  109. if (mWantsAlphaPremultiplied)
  110. {
  111. r = (r * a) / 255;
  112. g = (g * a) / 255;
  113. b = (b * a) / 255;
  114. }
  115. *(destPtr++) = (a << 24) | (b << 16) | (g << 8) | r;
  116. spanLen--;
  117. }
  118. x = 0;
  119. destPtr += destAdd;
  120. }
  121. }
  122. NOP;
  123. }
  124. else if (aMode == 3)
  125. {
  126. int y = 0;
  127. int x = 0;
  128. readSpanHeader3:
  129. int spanLen = 0;
  130. uint32 spanColor = 0;
  131. uint8 spanHeader = *(srcPtr++);
  132. spanLen = (spanHeader & 0x7F) + 1;
  133. if ((spanHeader & 0x80) != 0)
  134. {
  135. // Repeat color
  136. int b = *(srcPtr++);
  137. int g = *(srcPtr++);
  138. int r = *(srcPtr++);
  139. int a = 255;
  140. spanColor = (a << 24) | (b << 16) | (g << 8) | r;
  141. for (; y < mHeight; y++)
  142. {
  143. for (; x < mWidth; x++)
  144. {
  145. if (spanLen == 0)
  146. goto readSpanHeader3;
  147. *(destPtr++) = spanColor;
  148. spanLen--;
  149. }
  150. x = 0;
  151. destPtr += destAdd;
  152. }
  153. }
  154. else
  155. {
  156. for (; y < mHeight; y++)
  157. {
  158. for (; x < mWidth; x++)
  159. {
  160. if (spanLen == 0)
  161. goto readSpanHeader3;
  162. int b = *(srcPtr++);
  163. int g = *(srcPtr++);
  164. int r = *(srcPtr++);
  165. int a = 255;
  166. *(destPtr++) = (a << 24) | (b << 16) | (g << 8) | r;
  167. spanLen--;
  168. }
  169. x = 0;
  170. destPtr += destAdd;
  171. }
  172. }
  173. NOP;
  174. }
  175. }
  176. else
  177. {
  178. int total;
  179. size_t step = (sizeof(unsigned char) + sizeof(signed short)) * 6;
  180. // mode equal the number of components for each pixel
  181. int aMode = hdr->mBitsPerPixel / 8;
  182. // total is the number of unsigned chars we'll have to read
  183. total = mHeight * mWidth * aMode;
  184. size_t dataSize = sizeof(unsigned char) * total;
  185. //CC_BREAK_IF((step + dataSize) > bufSize);
  186. uint8* srcPtr = mSrcData + step;
  187. uint32* destPtr = mBits;
  188. int destAdd = 0;
  189. if (!flipped)
  190. {
  191. destPtr = mBits + mWidth*(mHeight - 1);
  192. destAdd = -mWidth*2;
  193. }
  194. if (aMode == 4)
  195. {
  196. for (int y = 0; y < mHeight; y++)
  197. {
  198. for (int x = 0; x < mWidth; x++)
  199. {
  200. int b = *(srcPtr++);
  201. int g = *(srcPtr++);
  202. int r = *(srcPtr++);
  203. int a = *(srcPtr++);
  204. if (mWantsAlphaPremultiplied)
  205. {
  206. r = (r * a) / 255;
  207. g = (g * a) / 255;
  208. b = (b * a) / 255;
  209. }
  210. *(destPtr++) = (a << 24) | (b << 16) | (g << 8) | r;
  211. }
  212. destPtr += destAdd;
  213. }
  214. }
  215. else if (aMode == 3)
  216. {
  217. for (int y = 0; y < mHeight; y++)
  218. {
  219. for (int x = 0; x < mWidth; x++)
  220. {
  221. int b = *(srcPtr++);
  222. int g = *(srcPtr++);
  223. int r = *(srcPtr++);
  224. int a = 255;
  225. *(destPtr++) = (a << 24) | (b << 16) | (g << 8) | r;
  226. }
  227. destPtr += destAdd;
  228. }
  229. }
  230. else if (aMode == 1)
  231. {
  232. for (int y = 0; y < mHeight; y++)
  233. {
  234. for (int x = 0; x < mWidth; x++)
  235. {
  236. int a = *(srcPtr++);
  237. #ifdef BF_PLATFORM_WINDOWS
  238. // Only windows has alpha correction for colored fonts (ATM)
  239. //int r = (int) (pow(a / 255.0f, 0.7f) * 255.0f);
  240. int r = (int)(pow(a / 255.0f, 1.2f) * 255.0f);
  241. //int r = a;
  242. int g = 255;
  243. int b = 255;
  244. #else
  245. int r = a;
  246. int g = a;
  247. int b = a;
  248. #endif
  249. *(destPtr++) = (a << 24) | (b << 16) | (g << 8) | r;
  250. }
  251. destPtr += destAdd;
  252. }
  253. }
  254. //memcpy(psInfo->imageData, Buffer + step, dataSize);
  255. // mode=3 or 4 implies that the image is RGB(A). However TGA
  256. // stores it as BGR(A) so we'll have to swap R and B.
  257. /*if (mode >= 3)
  258. {
  259. for (i=0; i < total; i+= mode)
  260. {
  261. aux = psInfo->imageData[i];
  262. psInfo->imageData[i] = psInfo->imageData[i+2];
  263. psInfo->imageData[i+2] = aux;
  264. }
  265. }*/
  266. }
  267. mAlphaPremultiplied = mWantsAlphaPremultiplied;
  268. return true;
  269. }