TGACompiler.cpp 5.9 KB

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