TextureResource.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #include "ReaderWriter.h"
  28. #include "JSONParser.h"
  29. namespace crown
  30. {
  31. namespace texture_resource
  32. {
  33. struct ImageData
  34. {
  35. uint32_t width;
  36. uint32_t height;
  37. PixelFormat::Enum format;
  38. uint32_t num_mips;
  39. char* data;
  40. };
  41. //-----------------------------------------------------------------------------
  42. void swap_red_blue(uint32_t width, uint32_t height, uint8_t channels, char* data)
  43. {
  44. uint32_t i = 0;
  45. for (uint32_t h = 0; h < height; h++)
  46. {
  47. for (uint32_t w = 0; w < width; w++)
  48. {
  49. const uint8_t tmp = data[i + 0];
  50. data[i + 0] = data[i + 2];
  51. data[i + 2] = tmp;
  52. i += channels;
  53. }
  54. }
  55. }
  56. //-----------------------------------------------------------------------------
  57. void read_tga_uncompressed(BinaryReader& br, uint32_t width, uint32_t height, uint8_t channels, ImageData& image)
  58. {
  59. if (channels == 2)
  60. {
  61. uint32_t i = 0;
  62. for (uint32_t h = 0; h < height; h++)
  63. {
  64. for (uint32_t w = 0; w < width; w++)
  65. {
  66. uint16_t data;
  67. br.read(data);
  68. image.data[i + 0] = (data & 0x7c) >> 10;
  69. image.data[i + 1] = (data & 0x3e) >> 5;
  70. image.data[i + 2] = (data & 0x1f);
  71. i += 3;
  72. }
  73. }
  74. }
  75. else
  76. {
  77. br.read(image.data, width * height * channels);
  78. swap_red_blue(width, height, channels, image.data);
  79. }
  80. }
  81. //-----------------------------------------------------------------------------
  82. void read_tga_compressed(BinaryReader& br, uint32_t width, uint32_t height, uint8_t channels, ImageData& image)
  83. {
  84. uint8_t rle_id = 0;
  85. uint32_t i = 0;
  86. uint32_t colors_read = 0;
  87. // Can't be more than 4 channels
  88. uint8_t colors[4];
  89. while (i < width * height)
  90. {
  91. br.read(rle_id);
  92. // If MSB == 1
  93. if (rle_id & 0x80)
  94. {
  95. rle_id -= 127;
  96. br.read(colors[0]);
  97. br.read(colors[1]);
  98. br.read(colors[2]);
  99. if (channels == 4)
  100. br.read(colors[3]);
  101. while (rle_id)
  102. {
  103. image.data[colors_read + 0] = colors[2];
  104. image.data[colors_read + 1] = colors[1];
  105. image.data[colors_read + 2] = colors[0];
  106. if (channels == 4)
  107. image.data[colors_read + 3] = colors[3];
  108. rle_id--;
  109. colors_read += channels;
  110. i++;
  111. }
  112. }
  113. else
  114. {
  115. rle_id++;
  116. while (rle_id)
  117. {
  118. br.read(colors[0]);
  119. br.read(colors[1]);
  120. br.read(colors[2]);
  121. if (channels == 4)
  122. br.read(colors[3]);
  123. image.data[colors_read + 0] = colors[2];
  124. image.data[colors_read + 1] = colors[1];
  125. image.data[colors_read + 2] = colors[0];
  126. if (channels == 4)
  127. image.data[colors_read + 3] = colors[3];
  128. rle_id--;
  129. colors_read += channels;
  130. i++;
  131. }
  132. }
  133. }
  134. }
  135. //-----------------------------------------------------------------------------
  136. void parse_tga(BinaryReader& br, ImageData& image)
  137. {
  138. uint8_t id;
  139. br.read(id);
  140. uint8_t cmap_type;
  141. br.read(cmap_type);
  142. uint8_t image_type;
  143. br.read(image_type);
  144. uint8_t garbage;
  145. for (uint32_t i = 0; i < 5; i++)
  146. br.read(garbage);
  147. uint16_t x_offt;
  148. br.read(x_offt);
  149. uint16_t y_offt;
  150. br.read(y_offt);
  151. uint16_t width;
  152. br.read(width);
  153. uint16_t height;
  154. br.read(height);
  155. uint8_t depth;
  156. br.read(depth);
  157. uint8_t desc;
  158. br.read(desc);
  159. // Skip TGA ID
  160. br.skip(id);
  161. CE_ASSERT(image_type != 0, "TGA does not contain image data");
  162. CE_ASSERT(image_type == 2 || image_type == 10, "TGA image format not supported");
  163. const uint32_t channels = depth / 8;
  164. image.width = width;
  165. image.height = height;
  166. image.num_mips = 1;
  167. switch (channels)
  168. {
  169. case 2: image.format = PixelFormat::R8G8B8; break;
  170. case 3: image.format = PixelFormat::R8G8B8; break;
  171. case 4: image.format = PixelFormat::R8G8B8A8; break;
  172. default: CE_FATAL("TGA channels not supported"); break;
  173. }
  174. image.data = (char*) default_allocator().allocate(Pixel::bytes_per_pixel(image.format) * width * height);
  175. if (image_type == 2)
  176. {
  177. read_tga_uncompressed(br, width, height, channels, image);
  178. }
  179. else if (image_type == 10)
  180. {
  181. read_tga_compressed(br, width, height, channels, image);
  182. }
  183. return;
  184. }
  185. static DdsPixelFormat PIXEL_FORMAT_TO_DDSPF[PixelFormat::COUNT] =
  186. {
  187. { DDPF_HEADERSIZE, DDS_FOURCC, DDFP_FOURCC_DXT1, 0, 0, 0, 0, 0 }, // DXT1
  188. { DDPF_HEADERSIZE, DDS_FOURCC, DDFP_FOURCC_DXT3, 0, 0, 0, 0, 0 }, // DXT3
  189. { DDPF_HEADERSIZE, DDS_FOURCC, DDFP_FOURCC_DXT5, 0, 0, 0, 0, 0 }, // DXT5
  190. { DDPF_HEADERSIZE, DDS_RGB, 0, 24, 0xFF0000, 0xFF00, 0xFF, 0}, // R8G8B8
  191. { DDPF_HEADERSIZE, DDS_RGBA, 0, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF}, // R8G8B8A8
  192. { 0, 0, 0, 0, 0, 0, 0, 0}, // D16
  193. { 0, 0, 0, 0, 0, 0, 0, 0}, // D24
  194. { 0, 0, 0, 0, 0, 0, 0, 0}, // D32
  195. { 0, 0, 0, 0, 0, 0, 0, 0}, // D24S8
  196. };
  197. //-----------------------------------------------------------------------------
  198. void parse_dds(BinaryReader& br, ImageData& image)
  199. {
  200. // Read header
  201. uint32_t magic;
  202. br.read(magic);
  203. CE_ASSERT(magic == DDSD_MAGIC, "DDS bad magic number");
  204. uint32_t hsize;
  205. br.read(hsize);
  206. CE_ASSERT(hsize == DDSD_HEADERSIZE, "DDS bas header size");
  207. uint32_t flags;
  208. br.read(flags);
  209. CE_ASSERT(flags & (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT), "DDS bad header flags");
  210. uint32_t height;
  211. br.read(height);
  212. uint32_t width;
  213. br.read(width);
  214. uint32_t pitch;
  215. br.read(pitch);
  216. uint32_t depth;
  217. br.read(depth);
  218. uint32_t num_mips;
  219. br.read(num_mips);
  220. // Skip reserved bits
  221. br.skip(sizeof(uint32_t) * 11);
  222. // Read pixel format
  223. uint32_t pf_hsize;
  224. br.read(pf_hsize);
  225. CE_ASSERT(pf_hsize == DDPF_HEADERSIZE, "DDS bad pf header size");
  226. uint32_t pf_flags;
  227. br.read(pf_flags);
  228. uint32_t pf_fourcc;
  229. br.read(pf_fourcc);
  230. uint32_t pf_bitcount;
  231. br.read(pf_bitcount);
  232. uint32_t pf_rmask;
  233. br.read(pf_rmask);
  234. uint32_t pf_gmask;
  235. br.read(pf_gmask);
  236. uint32_t pf_bmask;
  237. br.read(pf_bmask);
  238. uint32_t pf_amask;
  239. br.read(pf_amask);
  240. uint32_t caps;
  241. br.read(caps);
  242. CE_ASSERT((caps & DDSCAPS_TEXTURE), "DDS bad caps");
  243. uint32_t caps2;
  244. br.read(caps2);
  245. uint32_t caps3;
  246. br.read(caps3);
  247. uint32_t caps4;
  248. br.read(caps4);
  249. uint32_t reserved2;
  250. br.read(reserved2);
  251. image.width = width;
  252. image.height = height;
  253. image.num_mips = (flags & DDSD_MIPMAPCOUNT) ? num_mips : 1;
  254. image.format = PixelFormat::COUNT;
  255. DdsPixelFormat ddspf;
  256. ddspf.size = pf_hsize;
  257. ddspf.flags = pf_flags;
  258. ddspf.fourcc = pf_fourcc;
  259. ddspf.bitcount = pf_bitcount;
  260. ddspf.rmask = pf_rmask;
  261. ddspf.gmask = pf_gmask;
  262. ddspf.bmask = pf_bmask;
  263. ddspf.amask = pf_amask;
  264. for (uint32_t i = 0; i < PixelFormat::COUNT; i++)
  265. {
  266. if (memcmp(&ddspf, &PIXEL_FORMAT_TO_DDSPF[(PixelFormat::Enum) i], sizeof(DdsPixelFormat)) == 0)
  267. {
  268. image.format = (PixelFormat::Enum) i;
  269. }
  270. }
  271. CE_ASSERT(image.format != PixelFormat::COUNT, "DDS pixel format not supported");
  272. }
  273. //-----------------------------------------------------------------------------
  274. void write_dds(BinaryWriter& bw, const ImageData& image)
  275. {
  276. bw.write(DDSD_MAGIC);
  277. // Header
  278. bw.write(DDSD_HEADERSIZE); // dwSize
  279. bw.write(DDS_HEADER_FLAGS_TEXTURE | DDSD_PITCH | DDSD_MIPMAPCOUNT); // dwFlags
  280. bw.write(image.height); // dwHeight
  281. bw.write(image.width); // dwWidth
  282. const uint32_t pitch = uint32_t(( image.width * 32 + 7 ) / 8);
  283. bw.write(pitch); // dwPitchOrLinearSize
  284. bw.write(DDSD_UNUSED); // dwDepth
  285. bw.write(image.num_mips); // dwMipMapCount;
  286. for (uint32_t i = 0; i < 11; i++)
  287. bw.write(DDSD_UNUSED); // dwReserved1[11];
  288. // PixelFormat
  289. bw.write(DDPF_HEADERSIZE); // dwSize;
  290. const uint32_t pf = DDPF_RGB | (image.format == PixelFormat::R8G8B8A8 ? DDPF_ALPHAPIXELS : 0);
  291. bw.write(pf); // dwFlags;
  292. bw.write(DDSD_UNUSED); // dwFourCC;
  293. bw.write(uint32_t(image.format == PixelFormat::R8G8B8A8 ? 32 : 24)); // dwRGBBitCount;
  294. bw.write(uint32_t(0x000000FF)); // dwRBitMask;
  295. bw.write(uint32_t(0x0000FF00)); // dwGBitMask;
  296. bw.write(uint32_t(0x00FF0000)); // dwBBitMask;
  297. bw.write(uint32_t(0xFF000000)); // dwABitMask;
  298. bw.write(DDSCAPS_TEXTURE); // dwCaps;
  299. bw.write(DDSD_UNUSED); // dwCaps2;
  300. bw.write(DDSD_UNUSED); // dwCaps3;
  301. bw.write(DDSD_UNUSED); // dwCaps4;
  302. bw.write(DDSD_UNUSED); // dwReserved2;
  303. const char* data = image.data;
  304. for (uint32_t i = 0; i < image.height; i++)
  305. {
  306. bw.write((const void*) data, pitch);
  307. data += pitch;
  308. }
  309. }
  310. //-----------------------------------------------------------------------------
  311. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  312. {
  313. File* in_file = fs.open(resource_path, FOM_READ);
  314. JSONParser json(*in_file);
  315. fs.close(in_file);
  316. // Read source file
  317. JSONElement root = json.root();
  318. DynamicString name;
  319. root.key("source").to_string(name);
  320. File* source = fs.open(name.c_str(), FOM_READ);
  321. BinaryReader br(*source);
  322. ImageData image;
  323. if (name.ends_with(".tga"))
  324. {
  325. parse_tga(br, image);
  326. }
  327. else if (name.ends_with(".dds"))
  328. {
  329. // parse_dds(br, image);
  330. CE_FATAL(".dds not supported");
  331. }
  332. else
  333. {
  334. CE_FATAL("Source image not supported");
  335. }
  336. fs.close(source);
  337. BinaryWriter bw(*out_file);
  338. // Write texture header
  339. bw.write(uint32_t(0x00000000));
  340. // Write DDS
  341. write_dds(bw, image);
  342. default_allocator().deallocate(image.data);
  343. }
  344. } // namespace texture_resource
  345. } // namespace crown