TextureCompiler.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "TextureCompiler.h"
  24. #include "PixelFormat.h"
  25. #include "Allocator.h"
  26. #include "Filesystem.h"
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. TextureCompiler::TextureCompiler() :
  31. m_texture_data_size(0),
  32. m_texture_data(NULL)
  33. {
  34. }
  35. //-----------------------------------------------------------------------------
  36. TextureCompiler::~TextureCompiler()
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. size_t TextureCompiler::compile_impl(Filesystem& fs, const char* resource_path)
  41. {
  42. File* in_file = fs.open(resource_path, FOM_READ);
  43. if (!in_file)
  44. {
  45. Log::e("Unable to open file: %s", resource_path);
  46. return 0;
  47. }
  48. // Read the header
  49. in_file->read((char*)(char*)&m_tga_header, sizeof(TGAHeader));
  50. // Skip TGA ID
  51. in_file->skip(m_tga_header.id_length);
  52. // Compute color channels
  53. m_tga_channels = m_tga_header.pixel_depth / 8;
  54. m_tga_size = m_tga_header.width * m_tga_header.height;
  55. m_texture_header.version = TEXTURE_VERSION;
  56. m_texture_header.width = m_tga_header.width;
  57. m_texture_header.height = m_tga_header.height;
  58. // Select the appropriate pixel format and allocate
  59. // resource data based on tga size and channels
  60. switch (m_tga_channels)
  61. {
  62. case 2:
  63. case 3:
  64. {
  65. m_texture_header.format = PF_RGB_8;
  66. m_texture_data_size = m_tga_size * 3;
  67. m_texture_data = (uint8_t*)default_allocator().allocate(m_texture_data_size);
  68. break;
  69. }
  70. case 4:
  71. {
  72. m_texture_header.format = PF_RGBA_8;
  73. m_texture_data_size = m_tga_size * m_tga_channels;
  74. m_texture_data = (uint8_t*)default_allocator().allocate(m_texture_data_size);
  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("The file does not contain image data: %s", resource_path);
  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("Image type not supported.");
  104. return 0;
  105. }
  106. }
  107. fs.close(in_file);
  108. // Return the total resource size
  109. return sizeof(TextureHeader) + m_texture_data_size;
  110. }
  111. //-----------------------------------------------------------------------------
  112. void TextureCompiler::write_impl(File* out_file)
  113. {
  114. out_file->write((char*)&m_texture_header, sizeof(TextureHeader));
  115. out_file->write((char*)m_texture_data, m_texture_data_size);
  116. if (m_texture_data)
  117. {
  118. default_allocator().deallocate(m_texture_data);
  119. m_texture_data_size = 0;
  120. m_texture_data = NULL;
  121. }
  122. }
  123. //-----------------------------------------------------------------------------
  124. void TextureCompiler::load_uncompressed(File* in_file)
  125. {
  126. uint64_t size = m_tga_header.width * m_tga_header.height;
  127. if (m_tga_channels == 2)
  128. {
  129. int32_t j = 0;
  130. for (uint64_t i = 0; i < size * m_tga_channels; i++)
  131. {
  132. uint16_t pixel_data;
  133. in_file->read((char*)&pixel_data, sizeof(pixel_data));
  134. m_texture_data[j + 0] = (pixel_data & 0x7c) >> 10;
  135. m_texture_data[j + 1] = (pixel_data & 0x3e) >> 5;
  136. m_texture_data[j + 2] = (pixel_data & 0x1f);
  137. j += 3;
  138. }
  139. }
  140. else
  141. {
  142. in_file->read((char*)m_texture_data, (size_t)(size * m_tga_channels));
  143. swap_red_blue();
  144. }
  145. }
  146. //-----------------------------------------------------------------------------
  147. void TextureCompiler::load_compressed(File* in_file)
  148. {
  149. uint8_t rle_id = 0;
  150. uint32_t i = 0;
  151. uint32_t colors_read = 0;
  152. uint64_t size = m_tga_header.width * m_tga_header.height;
  153. // Can't be more than 4 channels
  154. uint8_t colors[4];
  155. while (i < size)
  156. {
  157. in_file->read((char*)&rle_id, sizeof(uint8_t));
  158. // If MSB == 1
  159. if (rle_id & 0x80)
  160. {
  161. rle_id -= 127;
  162. in_file->read((char*)&colors, m_tga_channels);
  163. while (rle_id)
  164. {
  165. m_texture_data[colors_read + 0] = colors[2];
  166. m_texture_data[colors_read + 1] = colors[1];
  167. m_texture_data[colors_read + 2] = colors[0];
  168. if (m_tga_channels == 4)
  169. {
  170. m_texture_data[colors_read + 3] = colors[3];
  171. }
  172. rle_id--;
  173. colors_read += m_tga_channels;
  174. i++;
  175. }
  176. }
  177. else
  178. {
  179. rle_id++;
  180. while (rle_id)
  181. {
  182. in_file->read((char*)colors, m_tga_channels);
  183. m_texture_data[colors_read + 0] = colors[2];
  184. m_texture_data[colors_read + 1] = colors[1];
  185. m_texture_data[colors_read + 2] = colors[0];
  186. if (m_tga_channels == 4)
  187. {
  188. m_texture_data[colors_read + 3] = colors[3];
  189. }
  190. rle_id--;
  191. colors_read += m_tga_channels;
  192. i++;
  193. }
  194. }
  195. }
  196. }
  197. //-----------------------------------------------------------------------------
  198. void TextureCompiler::swap_red_blue()
  199. {
  200. for (uint64_t i = 0; i < m_tga_size * m_tga_channels; i += m_tga_channels)
  201. {
  202. m_texture_data[i + 0] ^= m_texture_data[i + 2];
  203. m_texture_data[i + 2] ^= m_texture_data[i + 0];
  204. m_texture_data[i + 0] ^= m_texture_data[i + 2];
  205. }
  206. }
  207. } // namespace crown