TGACompiler.cpp 6.1 KB

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