2
0

TextureResource.cpp 6.2 KB

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