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.

pcx.cpp 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*!
  2. * \file src/utils/pcx.cpp
  3. * \brief PCX image reader
  4. *
  5. * Based on official technical documentation from ZSoft:
  6. * http://bespin.org/~qz/pc-gpe/pcx.txt
  7. *
  8. * \author xythobuz
  9. */
  10. #include <fstream>
  11. #include "global.h"
  12. #include "utils/pcx.h"
  13. #include "Console.h"
  14. #define pcxPrint getConsole().print
  15. int pcxCheck(const char *filename) {
  16. assert(filename != NULL);
  17. assert(filename[0] != '\0');
  18. std::ifstream file(filename, std::ios::in | std::ios::binary);
  19. // Read raw PCX header, 128 bytes
  20. unsigned char *header = new unsigned char[128];
  21. // Basic validation
  22. if (!file.read((char *)(&header[0]), 128)) {
  23. pcxPrint("File not big enough for valid PCX header!");
  24. delete [] header;
  25. return -1;
  26. }
  27. if (header[0] != 0x0A) {
  28. pcxPrint("Magic number at file start is wrong (0x%X != 0x0A)", header[0]);
  29. delete [] header;
  30. return -2;
  31. }
  32. if ((header[1] != 0) && ((header[1] < 2) || (header[1] > 5))) {
  33. // Valid: 0, 2, 3, 4, 5
  34. pcxPrint("Unknown PCX file format version (%d)", header[1]);
  35. delete [] header;
  36. return -3;
  37. }
  38. if ((header[2] != 0) && (header[2] != 1)) {
  39. pcxPrint("Unknown PCX file encoding (%d)", header[2]);
  40. delete [] header;
  41. return -4;
  42. }
  43. if (header[3] != 8) {
  44. pcxPrint("Only supporting 8bit (%dbit)", header[3]);
  45. delete [] header;
  46. return -5;
  47. }
  48. if (header[64] != 0) {
  49. pcxPrint("Reserved field is used (%d != 0)", header[64]);
  50. delete [] header;
  51. return -6;
  52. }
  53. delete [] header;
  54. return 0;
  55. }
  56. int pcxLoad(const char *filename, unsigned char **image,
  57. unsigned int *width, unsigned int *height, ColorMode *mode, unsigned int *bpp) {
  58. assert(filename != NULL);
  59. assert(filename[0] != '\0');
  60. assert(image != NULL);
  61. assert(width != NULL);
  62. assert(height != NULL);
  63. assert(mode != NULL);
  64. assert(bpp != NULL);
  65. std::ifstream file(filename, std::ios::in | std::ios::binary);
  66. // Read raw PCX header, 128 bytes
  67. unsigned char *header = new unsigned char[128];
  68. // Basic validation
  69. if (!file.read((char *)(&header[0]), 128)) {
  70. pcxPrint("File not big enough for valid PCX header!");
  71. delete [] header;
  72. return -1;
  73. }
  74. if (header[0] != 0x0A) {
  75. pcxPrint("Magic number at file start is wrong (0x%X != 0x0A)", header[0]);
  76. delete [] header;
  77. return -2;
  78. }
  79. if ((header[1] != 0) && ((header[1] < 2) || (header[1] > 5))) {
  80. // Valid: 0, 2, 3, 4, 5
  81. pcxPrint("Unknown PCX file format version (%d)", header[1]);
  82. delete [] header;
  83. return -3;
  84. }
  85. if ((header[2] != 0) && (header[2] != 1)) {
  86. pcxPrint("Unknown PCX file encoding (%d)", header[2]);
  87. delete [] header;
  88. return -4;
  89. }
  90. if (header[3] != 8) {
  91. pcxPrint("Only supporting 8bit (%dbit)", header[3]);
  92. delete [] header;
  93. return -5;
  94. }
  95. if (header[64] != 0) {
  96. pcxPrint("Reserved field is used (%d != 0)", header[64]);
  97. delete [] header;
  98. return -6;
  99. }
  100. // Read header informations
  101. bool versionFive = (header[1] == 5);
  102. bool compressed = (header[2] == 1);
  103. //unsigned char bitsPerPixel = header[3];
  104. unsigned int xMin = header[4] | (header[5] << 8);
  105. unsigned int yMin = header[6] | (header[7] << 8);
  106. unsigned int xMax = header[8] | (header[9] << 8);
  107. unsigned int yMax = header[10] | (header[11] << 8);
  108. //unsigned int hDPI = header[12] | (header[13] << 8);
  109. //unsigned int vDPI = header[14] | (header[15] << 8);
  110. //unsigned char *colormap = header + 16;
  111. unsigned char nPlanes = header[65];
  112. unsigned int bytesPerLine = header[66] | (header[67] << 8);
  113. //unsigned int paletteInfo = header[68] | (header[69] << 8);
  114. //unsigned int hScreenSize = header[70] | (header[71] << 8); // Only in some versionFive files
  115. //unsigned int vScreenSize = header[72] | (header[73] << 8); // Only in some versionFive files
  116. delete [] header;
  117. // Calculations
  118. *width = xMax - xMin + 1;
  119. *height = yMax - yMin + 1;
  120. unsigned long totalBytes = nPlanes * bytesPerLine; // total bytes per scan line
  121. unsigned long imageSize = totalBytes * *height;
  122. unsigned char *buffer = new unsigned char[imageSize];
  123. unsigned long b = 0;
  124. // Read encoded pixel data
  125. for (unsigned long i = 0; i < imageSize;) {
  126. unsigned int n = 1; // Run-length-encoding assumes 1
  127. int c = file.get();
  128. if (!file) {
  129. pcxPrint("Could not read data (%lu%s)", i, (file.eof() ? " EOF" : ""));
  130. delete [] buffer;
  131. return -7;
  132. }
  133. // Run-Length-Encoding
  134. if (compressed) {
  135. if ((c & 0xC0) == 0xC0) {
  136. n = c & 0x3F;
  137. c = file.get();
  138. if (!file) {
  139. pcxPrint("Could not read data rle (%lu%s)", i, (file.eof() ? " EOF" : ""));
  140. delete [] buffer;
  141. return -8;
  142. }
  143. }
  144. }
  145. for (unsigned int j = 0; j < n; j++)
  146. buffer[b++] = (unsigned char)c;
  147. i += n;
  148. }
  149. // Read color palette
  150. unsigned char *palette = NULL;
  151. if (versionFive) {
  152. int c = file.get();
  153. if ((c == 12) && file) {
  154. palette = new unsigned char[768];
  155. for (unsigned int i = 0; i < 768; i++) {
  156. palette[i] = (unsigned char)file.get();
  157. if (!file) {
  158. pcxPrint("Could not read 256 color palette (%d)", i);
  159. delete [] buffer;
  160. delete [] palette;
  161. return -9;
  162. }
  163. }
  164. }
  165. }
  166. // Bring buffer into preferred format
  167. unsigned long size = *width * *height * 4;
  168. *image = new unsigned char[size];
  169. for (unsigned int y = 0; y < *height; y++) {
  170. for (unsigned int x = 0; x < *width; x++) {
  171. unsigned long baseIndex = (x + (y * *width)) * 4;
  172. unsigned char alpha = 255, red = 0, green = 0, blue = 0;
  173. if (palette != NULL) {
  174. if (nPlanes == 1) {
  175. red = palette[buffer[(y * totalBytes) + x] * 3];
  176. green = palette[(buffer[(y * totalBytes) + x] * 3) + 1];
  177. blue = palette[(buffer[(y * totalBytes) + x] * 3) + 2];
  178. } else {
  179. pcxPrint("Unsupported number of planes (%d)", nPlanes);
  180. delete [] buffer;
  181. delete [] palette;
  182. delete [] *image;
  183. *image = NULL;
  184. return -10;
  185. }
  186. } else {
  187. if ((nPlanes == 3) || (nPlanes == 4)) {
  188. red = buffer[(y * totalBytes) + x];
  189. green = buffer[(y * totalBytes) + *width + x];
  190. blue = buffer[(y * totalBytes) + (2 * *width) + x];
  191. if (nPlanes == 4)
  192. alpha = buffer[(y * totalBytes) + (3 * *width) + x];
  193. } else if (nPlanes == 1) {
  194. red = green = blue = buffer[(y * totalBytes) + x];
  195. } else {
  196. pcxPrint("Unsupported number of planes (%d)", nPlanes);
  197. delete [] buffer;
  198. delete [] palette;
  199. delete [] *image;
  200. *image = NULL;
  201. return -11;
  202. }
  203. }
  204. (*image)[baseIndex + 0] = red;
  205. (*image)[baseIndex + 1] = green;
  206. (*image)[baseIndex + 2] = blue;
  207. (*image)[baseIndex + 3] = alpha;
  208. }
  209. }
  210. *mode = RGBA;
  211. *bpp = 32;
  212. delete [] buffer;
  213. delete [] palette;
  214. return 0;
  215. }