tgainfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //---------------------------------------------------------------------------
  2. //
  3. // MechCommander 2
  4. //
  5. // TGA file Specifics
  6. //
  7. //---------------------------------------------------------------------------//
  8. // Copyright (C) Microsoft Corporation. All rights reserved. //
  9. //===========================================================================//
  10. #ifndef TGAINFO_H
  11. #include "tgainfo.h"
  12. #endif
  13. #include "EString.h"
  14. #include <string.h>
  15. #include <gameos.hpp>
  16. //---------------------------------------------------------------------------
  17. typedef struct _RGB
  18. {
  19. unsigned char r;
  20. unsigned char g;
  21. unsigned char b;
  22. } RGB;
  23. //---------------------------------------------------------------------------
  24. void tgaDecomp(MemoryPtr dest, MemoryPtr source, TGAFileHeader *tga_header)
  25. {
  26. //---------------------------------------------------------------------
  27. // Display Image based on Format 10 (0x0a). RLE true color.
  28. //----------------------------------------------
  29. // Parse RLE data -- Check for RLE/RAW byte
  30. MemoryPtr data_offset = source;
  31. long currentHeight = 0;
  32. long currentWidth = 0;
  33. while (currentHeight != tga_header->height)
  34. {
  35. while (currentWidth != tga_header->width)
  36. {
  37. byte rep_count = *data_offset;
  38. data_offset++;
  39. RGB rgb;
  40. //----------------------------------------------------------
  41. // Only an RLE packet if MSbit is 1
  42. if (rep_count >= 128)
  43. {
  44. rep_count ^= 0x80;
  45. rgb.b = *data_offset;
  46. data_offset++;
  47. rgb.g = *data_offset;
  48. data_offset++;
  49. rgb.r = *data_offset;
  50. data_offset++;
  51. //-------------------------
  52. // One more for Alpha Data
  53. // Use later for aliasing
  54. byte alpha = 0;
  55. if (tga_header->pixel_depth == 32)
  56. {
  57. alpha = *data_offset;
  58. data_offset++;
  59. }
  60. for (long i=0;i<rep_count+1;i++)
  61. {
  62. *dest = rgb.b;
  63. dest++;
  64. *dest = rgb.g;
  65. dest++;
  66. *dest = rgb.r;
  67. dest++;
  68. if (tga_header->pixel_depth == 32)
  69. {
  70. *dest = alpha;
  71. dest++;
  72. }
  73. else
  74. {
  75. //---------------------
  76. // Assume a color key.
  77. // r 0xff, g 0x00, b 0xff is alpha 0 (Hot Pink)
  78. // all else is alpha 0xff
  79. if ((rgb.r == 0xff) && (rgb.g == 0x0) && (rgb.b == 0xff))
  80. *dest = 0x0;
  81. else
  82. *dest = 0xff;
  83. dest++;
  84. }
  85. currentWidth++;
  86. }
  87. }
  88. else
  89. {
  90. for (int i=0;i<rep_count+1;i++)
  91. {
  92. rgb.b = *data_offset;
  93. data_offset++;
  94. rgb.g = *data_offset;
  95. data_offset++;
  96. rgb.r = *data_offset;
  97. data_offset++;
  98. //-------------------------
  99. // One more for Alpha Data
  100. // Use later for aliasing
  101. byte alpha = 0;
  102. if (tga_header->pixel_depth == 32)
  103. {
  104. alpha = *data_offset;
  105. data_offset++;
  106. }
  107. *dest = rgb.b;
  108. dest++;
  109. *dest = rgb.g;
  110. dest++;
  111. *dest = rgb.r;
  112. dest++;
  113. if (tga_header->pixel_depth == 32)
  114. {
  115. *dest = alpha;
  116. dest++;
  117. }
  118. else
  119. {
  120. //---------------------
  121. // Assume a color key.
  122. // r 0xff, g 0x00, b 0xff is alpha 0 (Hot Pink)
  123. // all else is alpha 0xff
  124. if ((rgb.r == 0xff) && (rgb.g == 0x0) && (rgb.b == 0xff))
  125. *dest = 0x0;
  126. else
  127. *dest = 0xff;
  128. dest++;
  129. }
  130. currentWidth++;
  131. }
  132. }
  133. }
  134. currentWidth = 0;
  135. currentHeight++;
  136. }
  137. }
  138. //---------------------------------------------------------------------------
  139. void flipTopToBottom (MemoryPtr buffer, BYTE depth, long width, long height)
  140. {
  141. //-----------------------------------------------------------
  142. // ScanLine by Scanline
  143. MemoryPtr tmpBuffer = (MemoryPtr)malloc(width * height * (depth>>3));
  144. MemoryPtr tmpPointer = tmpBuffer;
  145. //----------------------------------
  146. // Point to last scanline.
  147. long pixelWidth = width * (depth >> 3);
  148. MemoryPtr workBuffer = buffer + ((height - 1) * pixelWidth);
  149. for (long i=0;i<height;i++)
  150. {
  151. memcpy(tmpBuffer,workBuffer,pixelWidth);
  152. workBuffer -= pixelWidth;
  153. tmpBuffer += pixelWidth;
  154. }
  155. memcpy(buffer,tmpPointer,width * height * (depth>>3));
  156. free(tmpPointer);
  157. }
  158. //---------------------------------------------------------------------------
  159. void tgaCopy (MemoryPtr dest, MemoryPtr src, long size)
  160. {
  161. long numCopied = 0;
  162. while (numCopied != size)
  163. {
  164. *dest = *src;
  165. dest++;
  166. src++;
  167. *dest = *src;
  168. dest++;
  169. src++;
  170. *dest = *src;
  171. dest++;
  172. src++;
  173. *dest = 0xff;
  174. dest++;
  175. numCopied++;
  176. }
  177. }
  178. //---------------------------------------------------------------------------
  179. static const int g_textureCache_BufferSize = 16384/*64*64*sizeof(DWORD)*/;
  180. static BYTE g_textureCache_Buffer[g_textureCache_BufferSize];
  181. EString *g_textureCache_FilenameOfLastLoadedTexture = NULL;/*This is an (EString *) instead of an EString because apparently gos memory management has a problem with global static allocation of EStrings.*/
  182. static int g_textureCache_WidthOfLastLoadedTexture = 0;/*just to be sure*/
  183. static int g_textureCache_HeightOfLastLoadedTexture = 0;/*just to be sure*/
  184. static int g_textureCache_NumberOfConsecutiveLoads = 0;
  185. static bool g_textureCache_LastTextureIsCached = false;
  186. void loadTGATexture (FilePtr tgaFile, MemoryPtr ourRAM, long width, long height)
  187. {
  188. if (!g_textureCache_FilenameOfLastLoadedTexture)
  189. g_textureCache_FilenameOfLastLoadedTexture = new EString;
  190. if (width * height * sizeof(DWORD) <= g_textureCache_BufferSize)
  191. {
  192. if ((g_textureCache_FilenameOfLastLoadedTexture->Data())
  193. && (0 == strcmp(tgaFile->getFilename(), g_textureCache_FilenameOfLastLoadedTexture->Data()))
  194. && ((const int)width == g_textureCache_WidthOfLastLoadedTexture)
  195. && ((const int)height == g_textureCache_HeightOfLastLoadedTexture)
  196. )
  197. {
  198. if (g_textureCache_LastTextureIsCached)
  199. {
  200. g_textureCache_NumberOfConsecutiveLoads += 1;
  201. memcpy(ourRAM, g_textureCache_Buffer, width*height*sizeof(DWORD));
  202. return;
  203. }
  204. }
  205. }
  206. MemoryPtr tgaBuffer = (MemoryPtr)malloc(tgaFile->fileSize());
  207. tgaFile->read(tgaBuffer,tgaFile->fileSize());
  208. //---------------------------------------
  209. // Parse out TGAHeader.
  210. TGAFileHeader *header = (TGAFileHeader *)tgaBuffer;
  211. gosASSERT(header->width == width);
  212. gosASSERT(header->height == height);
  213. switch (header->image_type)
  214. {
  215. case UNC_PAL:
  216. {
  217. //------------------------------------------------
  218. // If palette, use entries to create 24Bit image.
  219. }
  220. break;
  221. case UNC_TRUE:
  222. {
  223. //------------------------------------------------
  224. // This is just a bitmap. Copy it into ourRAM.
  225. MemoryPtr image = tgaBuffer + sizeof(TGAFileHeader);
  226. if (header->pixel_depth == 32)
  227. memcpy(ourRAM,image,width * height * 4);
  228. else
  229. tgaCopy(ourRAM,image,width * height);
  230. //------------------------------------------------------------------------
  231. // Must check image_descriptor to see if we need to un upside down image.
  232. bool left = (header->image_descriptor & 16) != 0;
  233. bool top = (header->image_descriptor & 32) != 0;
  234. if (!top && !left)
  235. {
  236. //--------------------------------
  237. // Image is Upside down.
  238. flipTopToBottom(ourRAM,32,width,height);
  239. }
  240. else if (!top && left)
  241. {
  242. //flipTopToBottom(ourRAM,header->pixel_depth,width,height);
  243. //flipRightToLeft(ourRAM,header->pixel_depth,width,height);
  244. }
  245. else if (top && left)
  246. {
  247. //flipRightToLeft(ourRAM,header->pixel_depth,width,height);
  248. }
  249. }
  250. break;
  251. case RLE_PAL:
  252. {
  253. //------------------------------------------------
  254. // If palette, use entries to create 24Bit image.
  255. }
  256. break;
  257. case RLE_TRUE:
  258. {
  259. MemoryPtr image = tgaBuffer + sizeof(TGAFileHeader);
  260. tgaDecomp(ourRAM,image,header);
  261. //------------------------------------------------------------------------
  262. // Must check image_descriptor to see if we need to un upside down image.
  263. bool left = (header->image_descriptor & 16) != 0;
  264. bool top = (header->image_descriptor & 32) != 0;
  265. if (!top && !left)
  266. {
  267. //--------------------------------
  268. // Image is Upside down.
  269. flipTopToBottom(ourRAM,32,width,height);
  270. }
  271. else if (!top && left)
  272. {
  273. //flipTopToBottom(ourRAM,header->pixel_depth,width,height);
  274. //flipRightToLeft(ourRAM,header->pixel_depth,width,height);
  275. }
  276. else if (top && left)
  277. {
  278. //flipRightToLeft(ourRAM,header->pixel_depth,width,height);
  279. }
  280. }
  281. break;
  282. }
  283. free(tgaBuffer);
  284. if (width * height * sizeof(DWORD) <= g_textureCache_BufferSize)
  285. {
  286. if ((g_textureCache_FilenameOfLastLoadedTexture->Data())
  287. && (0 == strcmp(tgaFile->getFilename(), g_textureCache_FilenameOfLastLoadedTexture->Data()))
  288. && ((const int)width == g_textureCache_WidthOfLastLoadedTexture)
  289. && ((const int)height == g_textureCache_HeightOfLastLoadedTexture)
  290. )
  291. {
  292. g_textureCache_NumberOfConsecutiveLoads += 1;
  293. if (2 == g_textureCache_NumberOfConsecutiveLoads )
  294. {
  295. memcpy(g_textureCache_Buffer, ourRAM, width*height*sizeof(DWORD));
  296. (*g_textureCache_FilenameOfLastLoadedTexture) = tgaFile->getFilename();
  297. g_textureCache_WidthOfLastLoadedTexture = width;
  298. g_textureCache_HeightOfLastLoadedTexture = height;
  299. g_textureCache_LastTextureIsCached = true;
  300. }
  301. }
  302. else
  303. {
  304. (*g_textureCache_FilenameOfLastLoadedTexture) = tgaFile->getFilename();
  305. g_textureCache_WidthOfLastLoadedTexture = width;
  306. g_textureCache_HeightOfLastLoadedTexture = height;
  307. g_textureCache_NumberOfConsecutiveLoads = 1;
  308. g_textureCache_LastTextureIsCached = false;
  309. }
  310. }
  311. }
  312. void loadTGAMask (FilePtr tgaFile, MemoryPtr ourRAM, long width, long height)
  313. {
  314. MemoryPtr tgaBuffer = (MemoryPtr)malloc(tgaFile->fileSize());
  315. tgaFile->read(tgaBuffer,tgaFile->fileSize());
  316. //---------------------------------------
  317. // Parse out TGAHeader.
  318. TGAFileHeader *header = (TGAFileHeader *)tgaBuffer;
  319. gosASSERT(header->width == width);
  320. gosASSERT(header->height == height);
  321. gosASSERT(header->image_type != UNC_TRUE);
  322. gosASSERT(header->image_type != RLE_TRUE);
  323. gosASSERT(header->image_type != RLE_PAL);
  324. switch (header->image_type)
  325. {
  326. case UNC_PAL:
  327. case UNC_GRAY:
  328. {
  329. //------------------------------------------------
  330. // This is just a bitmap. Copy it into ourRAM.
  331. MemoryPtr image = tgaBuffer + sizeof(TGAFileHeader);
  332. if (header->color_map)
  333. image += header->cm_length * (header->cm_entry_size>>3);
  334. memcpy(ourRAM,image,width * height);
  335. //------------------------------------------------------------------------
  336. // Must check image_descriptor to see if we need to un upside down image.
  337. bool left = (header->image_descriptor & 16) != 0;
  338. bool top = (header->image_descriptor & 32) != 0;
  339. if (!top && !left)
  340. {
  341. //--------------------------------
  342. // Image is Upside down.
  343. flipTopToBottom(ourRAM,header->pixel_depth,width,height);
  344. }
  345. else if (!top && left)
  346. {
  347. flipTopToBottom(ourRAM,header->pixel_depth,width,height);
  348. //flipRightToLeft(ourRAM,header->pixel_depth,width,height);
  349. }
  350. else if (top && left)
  351. {
  352. //flipRightToLeft(ourRAM,header->pixel_depth,width,height);
  353. }
  354. }
  355. break;
  356. case RLE_PAL:
  357. {
  358. }
  359. break;
  360. }
  361. free(tgaBuffer);
  362. }
  363. //---------------------------------------------------------------------------