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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "utils/tga.h"
  14. int tga_check(FILE *f) {
  15. char buffer[10];
  16. if (!f) {
  17. perror("tga_check> Passed invalid file.\n");
  18. return -1;
  19. }
  20. /* Read the header */
  21. fseek(f, 0, SEEK_SET);
  22. fread(buffer, 8, 1, f);
  23. // buffer[1] = 0 - Means not color mapped (1 would mean mapped)
  24. if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
  25. //buffer[2] == TGA_TYPE__GREYSCALE ||
  26. buffer[2] == TGA_TYPE__COLOR_RLE))) {
  27. printf("tga_check> Inavlid or unknown TGA format.\n");
  28. return -2;
  29. }
  30. return 0;
  31. }
  32. int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
  33. tga_t header;
  34. char comment[256];
  35. unsigned char pixel[4];
  36. unsigned char *swap_row = NULL;
  37. unsigned char tmp, packet;
  38. bool must_flip = 0;
  39. unsigned int size;
  40. unsigned int i, j;
  41. if (!f) {
  42. fprintf(stderr, "tga_load> Invalid parameters.\n");
  43. return -1;
  44. }
  45. fseek(f, 0, SEEK_SET);
  46. // Read TGA header
  47. fread(&header.comment_lenght, 1, 1, f);
  48. fread(&header.colormap_type, 1, 1, f);
  49. fread(&header.image_type, 1, 1, f);
  50. fread(&header.colormap_index, 2, 1, f);
  51. fread(&header.colormap_lenght, 2, 1, f);
  52. fread(&header.colormap_bbp, 1, 1, f);
  53. fread(&tmp, 1, 1, f);
  54. header.origin_x = tmp;
  55. fread(&tmp, 1, 1, f);
  56. header.origin_x += tmp * 256;
  57. fread(&tmp, 1, 1, f);
  58. header.origin_y = tmp;
  59. fread(&tmp, 1, 1, f);
  60. header.origin_y += tmp * 256;
  61. fread(&tmp, 1, 1, f);
  62. header.width = tmp;
  63. fread(&tmp, 1, 1, f);
  64. header.width += tmp * 256;
  65. fread(&tmp, 1, 1, f);
  66. header.height = tmp;
  67. fread(&tmp, 1, 1, f);
  68. header.height += tmp * 256;
  69. fread(&header.bpp, 1, 1, f);
  70. fread(&header.desc_flags, 1, 1, f);
  71. *width = header.width;
  72. *height = header.height;
  73. switch (header.bpp) {
  74. case 32:
  75. *type = 2; //32;
  76. break;
  77. case 24:
  78. *type = 1; //24;
  79. break;
  80. case 8:
  81. default:
  82. *type = 0; //8;
  83. break;
  84. }
  85. #ifdef DEBUG_TGA
  86. printf("TGA [%ix%i@%ibpp, %it, %ix, %iy, %uf]\n",
  87. header.width, header.height, header.bpp, header.image_type,
  88. header.origin_x, header.origin_y,
  89. header.desc_flags);
  90. #endif
  91. // Comments can be 0 - 255
  92. if (header.comment_lenght) {
  93. fread(&comment, 1, header.comment_lenght, f);
  94. for (i = 0; i < 255; ++i) {
  95. if (!(comment[i] > 32 && comment[i] < 127))
  96. comment[i] = 183; // print a dot for invalid text
  97. }
  98. comment[255] = 0;
  99. printf("Comment: '%s'\n", comment);
  100. }
  101. *image = NULL;
  102. size = header.width * header.height;
  103. if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
  104. fprintf(stderr, "tga_load> Unknown image format.\n");
  105. return -2;
  106. }
  107. // Mongoose: Added 'screen origin bit' support back here
  108. if (!(header.desc_flags & 32)) {
  109. must_flip = true;
  110. }
  111. switch (header.bpp) {
  112. case 32:
  113. size *= 4;
  114. *image = new unsigned char [size];
  115. switch (header.image_type) {
  116. case TGA_TYPE__COLOR_RLE:
  117. for (i = 0; i < size;) {
  118. fread(&packet, 1, 1, f);
  119. if (packet & 0x80) { // Run Length
  120. packet = (packet &0x7F) + 1;
  121. fread(&pixel, 4, 1, f);
  122. for (j = 0; j < packet; j++) {
  123. (*image)[i++] = pixel[2];
  124. (*image)[i++] = pixel[1];
  125. (*image)[i++] = pixel[0];
  126. (*image)[i++] = pixel[3];
  127. }
  128. } else { // RAW
  129. packet = (packet &0x7F) + 1;
  130. for (j = 0; j < packet; j++) {
  131. fread(&pixel, 4, 1, f);
  132. (*image)[i++] = pixel[2];
  133. (*image)[i++] = pixel[1];
  134. (*image)[i++] = pixel[0];
  135. (*image)[i++] = pixel[3];
  136. }
  137. }
  138. }
  139. break;
  140. case TGA_TYPE__COLOR:
  141. if (fread((*image), size, 1, f) < 1) {
  142. fprintf(stderr, "tga_load> Image fread failed.\n");
  143. delete [] *image;
  144. return -4;
  145. }
  146. for (i = 0; i < size; i += 4) {
  147. tmp = (*image)[i];
  148. (*image)[i] = (*image)[i + 2];
  149. (*image)[i + 2] = tmp;
  150. }
  151. break;
  152. default:
  153. ;
  154. }
  155. if (must_flip) {
  156. swap_row = new unsigned char [header.width * 4];
  157. for (i = 0, j = header.height-1; (int)i < header.height/2; i++, j--) {
  158. memcpy(swap_row, &(*image)[i*header.width*4], header.width*4);
  159. memcpy(&(*image)[i*header.width*4], &(*image)[j*header.width*4],
  160. header.width*4);
  161. memcpy(&(*image)[j*header.width*4], swap_row, header.width*4);
  162. }
  163. delete [] swap_row;
  164. }
  165. break;
  166. case 24:
  167. size *= 3;
  168. *image = new unsigned char [size];
  169. switch (header.image_type) {
  170. case TGA_TYPE__COLOR_RLE:
  171. for (i = 0; i < size;) {
  172. fread(&packet, 1, 1, f);
  173. if (packet & 0x80) { // Run Length
  174. packet = (packet &0x7F) + 1;
  175. fread(&pixel, 3, 1, f);
  176. for (j = 0; j < packet; j++) {
  177. (*image)[i++] = pixel[2];
  178. (*image)[i++] = pixel[1];
  179. (*image)[i++] = pixel[0];
  180. }
  181. } else { // RAW
  182. packet = (packet &0x7F) + 1;
  183. for (j = 0; j < packet; j++) {
  184. fread(&pixel, 3, 1, f);
  185. (*image)[i++] = pixel[2];
  186. (*image)[i++] = pixel[1];
  187. (*image)[i++] = pixel[0];
  188. }
  189. }
  190. }
  191. break;
  192. case TGA_TYPE__COLOR:
  193. if (fread((*image), size, 1, f) < 1) {
  194. fprintf(stderr, "tga_load> Image fread failed.\n");
  195. delete [] *image;
  196. return -4;
  197. }
  198. for (i = 0; i < size; i += 3) {
  199. tmp = (*image)[i];
  200. (*image)[i] = (*image)[i + 2];
  201. (*image)[i + 2] = tmp;
  202. }
  203. break;
  204. default:
  205. ;
  206. }
  207. if (must_flip) {
  208. swap_row = new unsigned char [header.width * 3];
  209. for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--) {
  210. memcpy(swap_row, &(*image)[i*header.width*3], header.width*3);
  211. memcpy(&(*image)[i*header.width*3], &(*image)[j*header.width*3],
  212. header.width*3);
  213. memcpy(&(*image)[j*header.width*3], swap_row, header.width*3);
  214. }
  215. delete [] swap_row;
  216. }
  217. break;
  218. case 8:
  219. printf("tga_load> 8bpp Not implemented\n");
  220. break;
  221. default:
  222. ;
  223. }
  224. #ifdef DEBUG_TGA
  225. char c;
  226. printf("TGA Comment: ");
  227. while (fread(&c, 1, 1, f) == 1) {
  228. printf("%c", c);
  229. }
  230. printf("\n");
  231. #endif
  232. return 0;
  233. }
  234. int tga_save(FILE *f, unsigned char *image, unsigned int width, unsigned int height, char type) {
  235. tga_t header;
  236. unsigned int size;
  237. char comment[64];
  238. //unsigned int i;
  239. //unsigned char tmp;
  240. if (!f || !image || !width || !height) {
  241. fprintf(stderr, "tga_save> Invalid parameters.\n");
  242. return -1;
  243. }
  244. strncpy(comment, "OpenRaider TGA", 63);
  245. comment[63] = 0;
  246. header.comment_lenght = (unsigned char)strlen(comment);
  247. header.colormap_type = 0;
  248. // No colormaps
  249. header.colormap_index = 0;
  250. header.colormap_lenght = 0;
  251. header.colormap_bbp = 0;
  252. header.origin_x = header.origin_y = 0;
  253. header.width = (unsigned short)width;
  254. header.height = (unsigned short)height;
  255. header.desc_flags = 0;
  256. switch (type) {
  257. case 4:
  258. header.image_type = TGA_TYPE__COLOR;
  259. header.desc_flags = 32;
  260. header.bpp = 32;
  261. break;
  262. case 2:
  263. header.bpp = 32;
  264. break;
  265. case 1:
  266. header.image_type = TGA_TYPE__GREYSCALE;
  267. header.bpp = 8;
  268. break;
  269. default:
  270. header.image_type = TGA_TYPE__COLOR;
  271. header.bpp = 24;
  272. }
  273. // Write TGA header
  274. fwrite(&header.comment_lenght, 1, 1, f);
  275. fwrite(&header.colormap_type, 1, 1, f);
  276. fwrite(&header.image_type, 1, 1, f);
  277. fwrite(&header.colormap_index, 2, 1, f);
  278. fwrite(&header.colormap_lenght, 2, 1, f);
  279. fwrite(&header.colormap_bbp, 1, 1, f);
  280. fwrite(&header.origin_x, 2, 1, f);
  281. fwrite(&header.origin_y, 2, 1, f);
  282. fwrite(&header.width, 2, 1, f);
  283. fwrite(&header.height, 2, 1, f);
  284. fwrite(&header.bpp, 1, 1, f);
  285. fwrite(&header.desc_flags, 1, 1, f);
  286. // Write comment
  287. fwrite(&comment, 1, header.comment_lenght, f);
  288. switch (header.bpp) {
  289. case 32:
  290. size = header.width * header.height * 4;
  291. //for (i = 0; i < size; i += 4)
  292. //{
  293. // tmp = image[i];
  294. // image[i] = image[i + 2];
  295. // image[i + 2] = tmp;
  296. //}
  297. break;
  298. case 24:
  299. size = header.width * header.height * 3;
  300. //for (i = 0; i < size; i += 3)
  301. //{
  302. // tmp = image[i];
  303. // image[i] = image[i + 2];
  304. // image[i + 2] = tmp;
  305. //}
  306. break;
  307. case 8:
  308. default:
  309. size = header.width * header.height;
  310. break;
  311. }
  312. // Write image data
  313. if (fwrite(image, size, 1, f) < 1) {
  314. perror("tga_save> Disk write failed.\n");
  315. return -2;
  316. }
  317. return 0;
  318. }
  319. int tga_save_filename(unsigned char *image, unsigned int width, unsigned int height, char type, char *s, ...) {
  320. char buffer[1024];
  321. FILE *f;
  322. int v;
  323. va_list args;
  324. va_start(args, s);
  325. vsnprintf(buffer, 1023, s, args);
  326. va_end(args);
  327. f = fopen(buffer, "wb");
  328. if (!f) {
  329. perror(buffer);
  330. return -1;
  331. }
  332. v = tga_save(f, image, width, height, type);
  333. fclose(f);
  334. return v;
  335. }