TGACompiler.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "TGACompiler.h"
  23. #include "FileStream.h"
  24. #include "PixelFormat.h"
  25. #include "Resource.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. TGACompiler::TGACompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
  30. Compiler(root_path, dest_path, resource, TEXTURE_TYPE, seed),
  31. m_image_format(PF_UNKNOWN),
  32. m_image_channels(0),
  33. m_image_size(0),
  34. m_image_data(NULL)
  35. {
  36. memset(&m_tga_header, 0, sizeof(TGAHeader));
  37. }
  38. //-----------------------------------------------------------------------------
  39. bool TGACompiler::compile()
  40. {
  41. FileStream* file = Compiler::source_file();
  42. // Read the header
  43. file->read(&m_tga_header, sizeof(TGAHeader));
  44. // Skip TGA ID
  45. file->skip(m_tga_header.id_length);
  46. // Compute color channels
  47. m_image_channels = m_tga_header.pixel_depth / 8;
  48. // Compute image size
  49. m_image_size = m_tga_header.width * m_tga_header.height;
  50. // Select the appropriate pixel format and allocate
  51. // resource data based on tga size and channels
  52. switch (m_image_channels)
  53. {
  54. case 2:
  55. case 3:
  56. {
  57. m_image_format = PF_RGB_8;
  58. m_image_data = new uint8_t[(uint32_t)(m_image_size * 3)];
  59. break;
  60. }
  61. case 4:
  62. {
  63. m_image_format = PF_RGBA_8;
  64. m_image_data = new uint8_t[(uint32_t)(m_image_size * m_image_channels)];
  65. break;
  66. }
  67. default:
  68. {
  69. printf("Fatal: Unable to determine TGA channels. Aborting.\n");
  70. return false;
  71. }
  72. }
  73. // Determine image type (compressed/uncompressed) and call proper function to load TGA
  74. switch (m_tga_header.image_type)
  75. {
  76. case 0:
  77. {
  78. printf("Fatal: The resource does not contain image data. Aborting.\n");
  79. return false;
  80. }
  81. case 2:
  82. {
  83. load_uncompressed();
  84. break;
  85. }
  86. case 10:
  87. {
  88. load_compressed();
  89. break;
  90. }
  91. default:
  92. {
  93. printf("Fatal: Image type not supported. Aborting.");
  94. return false;
  95. }
  96. }
  97. // Prepare for writing
  98. Compiler::prepare_header(m_image_size * m_image_channels +
  99. sizeof(PixelFormat) + sizeof(uint16_t) * 2);
  100. return true;
  101. }
  102. //-----------------------------------------------------------------------------
  103. void TGACompiler::write()
  104. {
  105. Compiler::write_header();
  106. FileStream* file = Compiler::destination_file();
  107. // Write out the data
  108. file->write(&m_image_format, sizeof(PixelFormat));
  109. file->write(&m_tga_header.width, sizeof(uint16_t));
  110. file->write(&m_tga_header.height, sizeof(uint16_t));
  111. file->write(m_image_data, m_image_size * m_image_channels);
  112. }
  113. //-----------------------------------------------------------------------------
  114. void TGACompiler::load_uncompressed()
  115. {
  116. FileStream* file = Compiler::source_file();
  117. uint64_t size = m_tga_header.width * m_tga_header.height;
  118. if (m_image_channels == 2)
  119. {
  120. int32_t j = 0;
  121. for (uint64_t i = 0; i < size * m_image_channels; i++)
  122. {
  123. uint16_t pixel_data;
  124. file->read(&pixel_data, sizeof(pixel_data));
  125. m_image_data[j + 0] = (pixel_data & 0x7c) >> 10;
  126. m_image_data[j + 1] = (pixel_data & 0x3e) >> 5;
  127. m_image_data[j + 2] = (pixel_data & 0x1f);
  128. j += 3;
  129. }
  130. }
  131. else
  132. {
  133. file->read(m_image_data, (size_t)(size * m_image_channels));
  134. swap_red_blue();
  135. }
  136. }
  137. //-----------------------------------------------------------------------------
  138. void TGACompiler::load_compressed()
  139. {
  140. FileStream* file = Compiler::source_file();
  141. uint8_t rle_id = 0;
  142. uint32_t i = 0;
  143. uint32_t colors_read = 0;
  144. uint64_t size = m_tga_header.width * m_tga_header.height;
  145. // Can't be more than 4 channels
  146. uint8_t colors[4];
  147. while (i < size)
  148. {
  149. file->read(&rle_id, sizeof(uint8_t));
  150. // If MSB == 1
  151. if (rle_id & 0x80)
  152. {
  153. rle_id -= 127;
  154. file->read(&colors, m_image_channels);
  155. while (rle_id)
  156. {
  157. m_image_data[colors_read + 0] = colors[2];
  158. m_image_data[colors_read + 1] = colors[1];
  159. m_image_data[colors_read + 2] = colors[0];
  160. if (m_image_channels == 4)
  161. {
  162. m_image_data[colors_read + 3] = colors[3];
  163. }
  164. rle_id--;
  165. colors_read += m_image_channels;
  166. i++;
  167. }
  168. }
  169. else
  170. {
  171. rle_id++;
  172. while (rle_id)
  173. {
  174. file->read(colors, m_image_channels);
  175. m_image_data[colors_read + 0] = colors[2];
  176. m_image_data[colors_read + 1] = colors[1];
  177. m_image_data[colors_read + 2] = colors[0];
  178. if (m_image_channels == 4)
  179. {
  180. m_image_data[colors_read + 3] = colors[3];
  181. }
  182. rle_id--;
  183. colors_read += m_image_channels;
  184. i++;
  185. }
  186. }
  187. }
  188. }
  189. //-----------------------------------------------------------------------------
  190. void TGACompiler::swap_red_blue()
  191. {
  192. for (uint64_t i = 0; i < m_image_size * m_image_channels; i += m_image_channels)
  193. {
  194. m_image_data[i + 0] ^= m_image_data[i + 2];
  195. m_image_data[i + 2] ^= m_image_data[i + 0];
  196. m_image_data[i + 0] ^= m_image_data[i + 2];
  197. }
  198. }
  199. } // namespace crown