Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tga.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*!
  2. * \file src/utils/tga.cpp
  3. * \brief TGA image reader/writer
  4. *
  5. * \todo type should pass more info (2 bits for RGBA|RGB|GREY; val for depth)
  6. *
  7. * \author Mongoose
  8. * \author xythobuz
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13. #include "global.h"
  14. #include "utils/tga.h"
  15. typedef enum {
  16. //TGA_TYPE__NO_DATA = 0,
  17. //TGA_TYPE__MAPPED = 1,
  18. TGA_TYPE__COLOR = 2,
  19. TGA_TYPE__GREYSCALE = 3,
  20. //TGA_TYPE__MAPPED_RLE = 9,
  21. TGA_TYPE__COLOR_RLE = 10
  22. } tga_type_t;
  23. typedef struct {
  24. unsigned char comment_lenght; //!< Number of bytes in comment
  25. unsigned char colormap_type; //!< 0 No colormap; 1 Has colormap
  26. unsigned char image_type; //!< 1 Colormapped; 2 Unmapped; 9 Colormapped RLE; 10 Unmapped RLE
  27. unsigned short colormap_index; //!< Index of first color map entry
  28. unsigned short colormap_lenght; //!< Number of color map entries
  29. unsigned char colormap_bbp; //!< 16, 24, or 32 bits per pixel format
  30. unsigned short origin_x; //!< X coor of lower-left corner
  31. unsigned short origin_y; //!< Y coor of lower-left corner
  32. unsigned short width; //!< Width in pixels
  33. unsigned short height; //!< Height in pixels
  34. unsigned char bpp; //!< Number of bits in a pixel index
  35. unsigned char desc_flags; //!< Various magic bits
  36. } tga_t;
  37. int tgaCheck(const char* filename) {
  38. char buffer[10];
  39. assert(filename != nullptr);
  40. assert(filename[0] != '\0');
  41. FILE* f = fopen(filename, "rb");
  42. if (!f) {
  43. printf("tgaCheck> File not found\n");
  44. return -1;
  45. }
  46. /* Read the header */
  47. fseek(f, 0, SEEK_SET);
  48. fread(buffer, 8, 1, f);
  49. // buffer[1] = 0 - Means not color mapped (1 would mean mapped)
  50. if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
  51. //buffer[2] == TGA_TYPE__GREYSCALE ||
  52. buffer[2] == TGA_TYPE__COLOR_RLE))) {
  53. printf("tgaCheck> Inavlid or unknown TGA format.\n");
  54. fclose(f);
  55. return -2;
  56. }
  57. fclose(f);
  58. return 0;
  59. }
  60. int tgaLoad(const char* filename, unsigned char** image, unsigned int* width, unsigned int* height,
  61. char* type) {
  62. tga_t header;
  63. char comment[256];
  64. unsigned char pixel[4];
  65. unsigned char* swap_row = nullptr;
  66. unsigned char tmp, packet;
  67. bool must_flip = 0;
  68. unsigned int size;
  69. unsigned int i, j;
  70. assert(filename != nullptr);
  71. assert(filename[0] != '\0');
  72. assert(image != nullptr);
  73. assert(width != nullptr);
  74. assert(height != nullptr);
  75. assert(type != nullptr);
  76. FILE* f = fopen(filename, "rb");
  77. if (!f) {
  78. printf("tgaLoad> File not found\n");
  79. return -1;
  80. }
  81. fseek(f, 0, SEEK_SET);
  82. // Read TGA header
  83. fread(&header.comment_lenght, 1, 1, f);
  84. fread(&header.colormap_type, 1, 1, f);
  85. fread(&header.image_type, 1, 1, f);
  86. fread(&header.colormap_index, 2, 1, f);
  87. fread(&header.colormap_lenght, 2, 1, f);
  88. fread(&header.colormap_bbp, 1, 1, f);
  89. fread(&tmp, 1, 1, f);
  90. header.origin_x = tmp;
  91. fread(&tmp, 1, 1, f);
  92. header.origin_x += tmp * 256;
  93. fread(&tmp, 1, 1, f);
  94. header.origin_y = tmp;
  95. fread(&tmp, 1, 1, f);
  96. header.origin_y += tmp * 256;
  97. fread(&tmp, 1, 1, f);
  98. header.width = tmp;
  99. fread(&tmp, 1, 1, f);
  100. header.width += tmp * 256;
  101. fread(&tmp, 1, 1, f);
  102. header.height = tmp;
  103. fread(&tmp, 1, 1, f);
  104. header.height += tmp * 256;
  105. fread(&header.bpp, 1, 1, f);
  106. fread(&header.desc_flags, 1, 1, f);
  107. *width = header.width;
  108. *height = header.height;
  109. switch (header.bpp) {
  110. case 32:
  111. *type = 2; //32;
  112. break;
  113. case 24:
  114. *type = 1; //24;
  115. break;
  116. case 8:
  117. default:
  118. *type = 0; //8;
  119. break;
  120. }
  121. #ifdef DEBUG_TGA
  122. printf("TGA [%ix%i@%ibpp, %it, %ix, %iy, %uf]\n",
  123. header.width, header.height, header.bpp, header.image_type,
  124. header.origin_x, header.origin_y,
  125. header.desc_flags);
  126. #endif
  127. // Comments can be 0 - 255
  128. if (header.comment_lenght) {
  129. fread(&comment, 1, header.comment_lenght, f);
  130. for (i = 0; i < 255; ++i) {
  131. if (!(comment[i] > 32 && comment[i] < 127))
  132. comment[i] = '.'; // print a dot for invalid text
  133. }
  134. comment[255] = 0;
  135. printf("Comment: '%s'\n", comment);
  136. }
  137. *image = nullptr;
  138. size = header.width * header.height;
  139. if (!size || (!(header.colormap_type == 0 && (header.image_type == 2
  140. || header.image_type == 10)))) {
  141. fprintf(stderr, "tgaLoad> Unknown image format.\n");
  142. fclose(f);
  143. return -2;
  144. }
  145. // Mongoose: Added 'screen origin bit' support back here
  146. if (!(header.desc_flags & 32)) {
  147. must_flip = true;
  148. }
  149. switch (header.bpp) {
  150. case 32:
  151. size *= 4;
  152. *image = new unsigned char [size];
  153. switch (header.image_type) {
  154. case TGA_TYPE__COLOR_RLE:
  155. for (i = 0; i < size;) {
  156. fread(&packet, 1, 1, f);
  157. if (packet & 0x80) { // Run Length
  158. packet = (packet & 0x7F) + 1;
  159. fread(&pixel, 4, 1, f);
  160. for (j = 0; j < packet; j++) {
  161. (*image)[i++] = pixel[2];
  162. (*image)[i++] = pixel[1];
  163. (*image)[i++] = pixel[0];
  164. (*image)[i++] = pixel[3];
  165. }
  166. } else { // RAW
  167. packet = (packet & 0x7F) + 1;
  168. for (j = 0; j < packet; j++) {
  169. fread(&pixel, 4, 1, f);
  170. (*image)[i++] = pixel[2];
  171. (*image)[i++] = pixel[1];
  172. (*image)[i++] = pixel[0];
  173. (*image)[i++] = pixel[3];
  174. }
  175. }
  176. }
  177. break;
  178. case TGA_TYPE__COLOR:
  179. if (fread((*image), size, 1, f) < 1) {
  180. fprintf(stderr, "tgaLoad> Image fread failed.\n");
  181. delete [] *image;
  182. fclose(f);
  183. return -4;
  184. }
  185. for (i = 0; i < size; i += 4) {
  186. tmp = (*image)[i];
  187. (*image)[i] = (*image)[i + 2];
  188. (*image)[i + 2] = tmp;
  189. }
  190. break;
  191. default:
  192. ;
  193. }
  194. if (must_flip) {
  195. swap_row = new unsigned char [header.width * 4];
  196. for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--) {
  197. memcpy(swap_row, &(*image)[i * header.width * 4], header.width * 4);
  198. memcpy(&(*image)[i * header.width * 4], &(*image)[j * header.width * 4],
  199. header.width * 4);
  200. memcpy(&(*image)[j * header.width * 4], swap_row, header.width * 4);
  201. }
  202. delete [] swap_row;
  203. }
  204. break;
  205. case 24:
  206. size *= 3;
  207. *image = new unsigned char [size];
  208. switch (header.image_type) {
  209. case TGA_TYPE__COLOR_RLE:
  210. for (i = 0; i < size;) {
  211. fread(&packet, 1, 1, f);
  212. if (packet & 0x80) { // Run Length
  213. packet = (packet & 0x7F) + 1;
  214. fread(&pixel, 3, 1, f);
  215. for (j = 0; j < packet; j++) {
  216. (*image)[i++] = pixel[2];
  217. (*image)[i++] = pixel[1];
  218. (*image)[i++] = pixel[0];
  219. }
  220. } else { // RAW
  221. packet = (packet & 0x7F) + 1;
  222. for (j = 0; j < packet; j++) {
  223. fread(&pixel, 3, 1, f);
  224. (*image)[i++] = pixel[2];
  225. (*image)[i++] = pixel[1];
  226. (*image)[i++] = pixel[0];
  227. }
  228. }
  229. }
  230. break;
  231. case TGA_TYPE__COLOR:
  232. if (fread((*image), size, 1, f) < 1) {
  233. fprintf(stderr, "tgaLoad> Image fread failed.\n");
  234. delete [] *image;
  235. fclose(f);
  236. return -4;
  237. }
  238. for (i = 0; i < size; i += 3) {
  239. tmp = (*image)[i];
  240. (*image)[i] = (*image)[i + 2];
  241. (*image)[i + 2] = tmp;
  242. }
  243. break;
  244. default:
  245. ;
  246. }
  247. if (must_flip) {
  248. swap_row = new unsigned char [header.width * 3];
  249. for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--) {
  250. memcpy(swap_row, &(*image)[i * header.width * 3], header.width * 3);
  251. memcpy(&(*image)[i * header.width * 3], &(*image)[j * header.width * 3],
  252. header.width * 3);
  253. memcpy(&(*image)[j * header.width * 3], swap_row, header.width * 3);
  254. }
  255. delete [] swap_row;
  256. }
  257. break;
  258. case 8:
  259. printf("tgaLoad> 8bpp Not implemented\n");
  260. break;
  261. default:
  262. ;
  263. }
  264. #ifdef DEBUG_TGA
  265. char c;
  266. printf("TGA Comment: ");
  267. while (fread(&c, 1, 1, f) == 1) {
  268. printf("%c", c);
  269. }
  270. printf("\n");
  271. #endif
  272. fclose(f);
  273. return 0;
  274. }
  275. int tgaSave(const char* filename, unsigned char* image, unsigned int width, unsigned int height,
  276. char type) {
  277. tga_t header;
  278. unsigned int size;
  279. char comment[64];
  280. //unsigned int i;
  281. //unsigned char tmp;
  282. assert(filename != nullptr);
  283. assert(filename[0] != '\0');
  284. assert(image != nullptr);
  285. assert(width > 0);
  286. assert(height > 0);
  287. FILE* f = fopen(filename, "wb");
  288. if (!f) {
  289. printf("tgaSave> File not found\n");
  290. return -1;
  291. }
  292. strncpy(comment, "OpenRaider TGA", 63);
  293. comment[63] = 0;
  294. header.comment_lenght = (unsigned char)strlen(comment);
  295. header.colormap_type = 0;
  296. // No colormaps
  297. header.colormap_index = 0;
  298. header.colormap_lenght = 0;
  299. header.colormap_bbp = 0;
  300. header.origin_x = header.origin_y = 0;
  301. header.width = (unsigned short)width;
  302. header.height = (unsigned short)height;
  303. header.desc_flags = 0;
  304. switch (type) {
  305. case 4:
  306. header.image_type = TGA_TYPE__COLOR;
  307. header.desc_flags = 32;
  308. header.bpp = 32;
  309. break;
  310. case 2:
  311. header.bpp = 32;
  312. break;
  313. case 1:
  314. header.image_type = TGA_TYPE__GREYSCALE;
  315. header.bpp = 8;
  316. break;
  317. default:
  318. header.image_type = TGA_TYPE__COLOR;
  319. header.bpp = 24;
  320. }
  321. // Write TGA header
  322. fwrite(&header.comment_lenght, 1, 1, f);
  323. fwrite(&header.colormap_type, 1, 1, f);
  324. fwrite(&header.image_type, 1, 1, f);
  325. fwrite(&header.colormap_index, 2, 1, f);
  326. fwrite(&header.colormap_lenght, 2, 1, f);
  327. fwrite(&header.colormap_bbp, 1, 1, f);
  328. fwrite(&header.origin_x, 2, 1, f);
  329. fwrite(&header.origin_y, 2, 1, f);
  330. fwrite(&header.width, 2, 1, f);
  331. fwrite(&header.height, 2, 1, f);
  332. fwrite(&header.bpp, 1, 1, f);
  333. fwrite(&header.desc_flags, 1, 1, f);
  334. // Write comment
  335. fwrite(&comment, 1, header.comment_lenght, f);
  336. switch (header.bpp) {
  337. case 32:
  338. size = header.width * header.height * 4;
  339. //for (i = 0; i < size; i += 4)
  340. //{
  341. // tmp = image[i];
  342. // image[i] = image[i + 2];
  343. // image[i + 2] = tmp;
  344. //}
  345. break;
  346. case 24:
  347. size = header.width * header.height * 3;
  348. //for (i = 0; i < size; i += 3)
  349. //{
  350. // tmp = image[i];
  351. // image[i] = image[i + 2];
  352. // image[i + 2] = tmp;
  353. //}
  354. break;
  355. case 8:
  356. default:
  357. size = header.width * header.height;
  358. break;
  359. }
  360. // Write image data
  361. if (fwrite(image, size, 1, f) < 1) {
  362. perror("tgaSave> Disk write failed.\n");
  363. fclose(f);
  364. return -2;
  365. }
  366. fclose(f);
  367. return 0;
  368. }