TGACompiler.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include "Log.h"
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. TGACompiler::TGACompiler(const char* root_path, const char* dest_path) :
  31. Compiler(root_path, dest_path, TEXTURE_TYPE),
  32. m_image_format(PF_UNKNOWN),
  33. m_image_channels(0),
  34. m_image_size(0),
  35. m_image_data(NULL)
  36. {
  37. memset(&m_tga_header, 0, sizeof(TGAHeader));
  38. }
  39. //-----------------------------------------------------------------------------
  40. TGACompiler::~TGACompiler()
  41. {
  42. cleanup_impl();
  43. }
  44. //-----------------------------------------------------------------------------
  45. size_t TGACompiler::read_header_impl(FileStream* in_file)
  46. {
  47. // Read the header
  48. in_file->read(&m_tga_header, sizeof(TGAHeader));
  49. // Skip TGA ID
  50. in_file->skip(m_tga_header.id_length);
  51. return sizeof(TGAHeader) + m_tga_header.id_length;
  52. }
  53. //-----------------------------------------------------------------------------
  54. size_t TGACompiler::read_resource_impl(FileStream* in_file)
  55. {
  56. // Compute color channels
  57. m_image_channels = m_tga_header.pixel_depth / 8;
  58. // Compute image size
  59. m_image_size = m_tga_header.width * m_tga_header.height;
  60. // Select the appropriate pixel format and allocate
  61. // resource data based on tga size and channels
  62. switch (m_image_channels)
  63. {
  64. case 2:
  65. case 3:
  66. {
  67. m_image_format = PF_RGB_8;
  68. m_image_data = new uint8_t[(uint32_t)(m_image_size * 3)];
  69. break;
  70. }
  71. case 4:
  72. {
  73. m_image_format = PF_RGBA_8;
  74. m_image_data = new uint8_t[(uint32_t)(m_image_size * m_image_channels)];
  75. break;
  76. }
  77. default:
  78. {
  79. Log::e("Unable to determine TGA channels.");
  80. return 0;
  81. }
  82. }
  83. // Determine image type (compressed/uncompressed) and call proper function to load TGA
  84. switch (m_tga_header.image_type)
  85. {
  86. case 0:
  87. {
  88. Log::e("Fatal: The resource does not contain image data.");
  89. return 0;
  90. }
  91. case 2:
  92. {
  93. load_uncompressed(in_file);
  94. break;
  95. }
  96. case 10:
  97. {
  98. load_compressed(in_file);
  99. break;
  100. }
  101. default:
  102. {
  103. Log::e("Fatal: Image type not supported.");
  104. return 0;
  105. }
  106. }
  107. // Return the total resource size
  108. return m_image_size * m_image_channels + sizeof(PixelFormat) + sizeof(uint16_t) + sizeof(uint16_t);
  109. }
  110. //-----------------------------------------------------------------------------
  111. void TGACompiler::write_header_impl(FileStream* out_file)
  112. {
  113. // Write the texture header
  114. out_file->write(&m_image_format, sizeof(PixelFormat));
  115. out_file->write(&m_tga_header.width, sizeof(uint16_t));
  116. out_file->write(&m_tga_header.height, sizeof(uint16_t));
  117. }
  118. //-----------------------------------------------------------------------------
  119. void TGACompiler::write_resource_impl(FileStream* out_file)
  120. {
  121. // Write out the data
  122. out_file->write(m_image_data, m_image_size * m_image_channels);
  123. }
  124. //-----------------------------------------------------------------------------
  125. void TGACompiler::cleanup_impl()
  126. {
  127. if (m_image_data)
  128. {
  129. delete[] m_image_data;
  130. m_image_data = NULL;
  131. }
  132. }
  133. //-----------------------------------------------------------------------------
  134. void TGACompiler::load_uncompressed(FileStream* in_file)
  135. {
  136. uint64_t size = m_tga_header.width * m_tga_header.height;
  137. if (m_image_channels == 2)
  138. {
  139. int32_t j = 0;
  140. for (uint64_t i = 0; i < size * m_image_channels; i++)
  141. {
  142. uint16_t pixel_data;
  143. in_file->read(&pixel_data, sizeof(pixel_data));
  144. m_image_data[j + 0] = (pixel_data & 0x7c) >> 10;
  145. m_image_data[j + 1] = (pixel_data & 0x3e) >> 5;
  146. m_image_data[j + 2] = (pixel_data & 0x1f);
  147. j += 3;
  148. }
  149. }
  150. else
  151. {
  152. in_file->read(m_image_data, (size_t)(size * m_image_channels));
  153. swap_red_blue();
  154. }
  155. }
  156. //-----------------------------------------------------------------------------
  157. void TGACompiler::load_compressed(FileStream* in_file)
  158. {
  159. uint8_t rle_id = 0;
  160. uint32_t i = 0;
  161. uint32_t colors_read = 0;
  162. uint64_t size = m_tga_header.width * m_tga_header.height;
  163. // Can't be more than 4 channels
  164. uint8_t colors[4];
  165. while (i < size)
  166. {
  167. in_file->read(&rle_id, sizeof(uint8_t));
  168. // If MSB == 1
  169. if (rle_id & 0x80)
  170. {
  171. rle_id -= 127;
  172. in_file->read(&colors, m_image_channels);
  173. while (rle_id)
  174. {
  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. else
  188. {
  189. rle_id++;
  190. while (rle_id)
  191. {
  192. in_file->read(colors, m_image_channels);
  193. m_image_data[colors_read + 0] = colors[2];
  194. m_image_data[colors_read + 1] = colors[1];
  195. m_image_data[colors_read + 2] = colors[0];
  196. if (m_image_channels == 4)
  197. {
  198. m_image_data[colors_read + 3] = colors[3];
  199. }
  200. rle_id--;
  201. colors_read += m_image_channels;
  202. i++;
  203. }
  204. }
  205. }
  206. }
  207. //-----------------------------------------------------------------------------
  208. void TGACompiler::swap_red_blue()
  209. {
  210. for (uint64_t i = 0; i < m_image_size * m_image_channels; i += m_image_channels)
  211. {
  212. m_image_data[i + 0] ^= m_image_data[i + 2];
  213. m_image_data[i + 2] ^= m_image_data[i + 0];
  214. m_image_data[i + 0] ^= m_image_data[i + 2];
  215. }
  216. }
  217. } // namespace crown