bitmapSTB.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/stream/fileStream.h"
  24. #include "core/stream/memStream.h"
  25. #include "core/strings/stringFunctions.h"
  26. #include "gfx/bitmap/gBitmap.h"
  27. #ifdef __clang__
  28. #define STBIWDEF static inline
  29. #endif
  30. #define STB_IMAGE_IMPLEMENTATION
  31. #define STB_IMAGE_STATIC
  32. #include "stb_image.h"
  33. #define STB_IMAGE_WRITE_IMPLEMENTATION
  34. #define STB_IMAGE_WRITE_STATIC
  35. #include "stb_image_write.h"
  36. static bool sReadSTB(const Torque::Path& path, GBitmap* bitmap);
  37. static bool sWriteSTB(const Torque::Path& path, GBitmap* bitmap, U32 compressionLevel);
  38. static struct _privateRegisterSTB
  39. {
  40. _privateRegisterSTB()
  41. {
  42. GBitmap::Registration reg;
  43. reg.priority = 100;
  44. reg.extensions.push_back("png");
  45. reg.extensions.push_back("bmp");
  46. reg.extensions.push_back("jpg");
  47. reg.extensions.push_back("jpeg");
  48. reg.extensions.push_back("psd");
  49. reg.extensions.push_back("hdr");
  50. reg.extensions.push_back("tga");
  51. reg.readFunc = sReadSTB;
  52. reg.writeFunc = sWriteSTB;
  53. // for png only.
  54. reg.defaultCompression = 6;
  55. GBitmap::sRegisterFormat(reg);
  56. }
  57. } sStaticRegisterSTB;
  58. bool sReadSTB(const Torque::Path& path, GBitmap* bitmap)
  59. {
  60. PROFILE_SCOPE(sReadSTB);
  61. S32 x, y, n, channels;
  62. bool is16 = false;
  63. bool isHdr = false;
  64. U32 prevWaterMark = FrameAllocator::getWaterMark();
  65. if (!stbi_info(path.getFullPath().c_str(), &x, &y, &channels))
  66. {
  67. FrameAllocator::setWaterMark(prevWaterMark);
  68. return false;
  69. }
  70. //// do this to map one channel to 3 and 2 channels to 4
  71. //if (channels == 2)
  72. // channels = 4;
  73. if (stbi_is_16_bit(path.getFullPath().c_str()))
  74. {
  75. is16 = true;
  76. channels = 4;
  77. }
  78. // stbi treats hdrs as 32f (all float are 32f from stbi so no 16f =/ )
  79. if (stbi_is_hdr(path.getFullPath().c_str()))
  80. {
  81. isHdr = true;
  82. is16 = false;
  83. channels = 4;
  84. }
  85. unsigned char* data = stbi_load(path.getFullPath().c_str(), &x, &y, &n, channels);
  86. bitmap->deleteImage();
  87. GFXFormat format;
  88. switch (n) {
  89. case 1:
  90. if (is16)
  91. {
  92. format = GFXFormatL16;
  93. break;
  94. }
  95. format = GFXFormatA8;
  96. break;
  97. case 2:
  98. format = GFXFormatA8L8;
  99. break;
  100. case 3:
  101. format = GFXFormatR8G8B8;
  102. break;
  103. case 4:
  104. if (isHdr)
  105. {
  106. format = GFXFormatR32G32B32A32F;
  107. break;
  108. }
  109. if (is16)
  110. {
  111. format = GFXFormatR16G16B16A16;
  112. break;
  113. }
  114. format = GFXFormatR8G8B8A8;
  115. break;
  116. default:
  117. FrameAllocator::setWaterMark(prevWaterMark);
  118. return false;
  119. }
  120. // actually allocate the bitmap space...
  121. bitmap->allocateBitmap(x, y,
  122. false, // don't extrude miplevels...
  123. format); // use determined format...
  124. U8* pBase = (U8*)bitmap->getBits();
  125. U32 rowBytes = y * x * n;
  126. dMemcpy(pBase, data, rowBytes);
  127. stbi_image_free(data);
  128. // Check this bitmap for transparency
  129. if (channels == 4 && !is16)
  130. bitmap->checkForTransparency();
  131. FrameAllocator::setWaterMark(prevWaterMark);
  132. return true;
  133. }
  134. bool sWriteSTB(const Torque::Path& path, GBitmap* bitmap, U32 compressionLevel)
  135. {
  136. PROFILE_SCOPE(sWriteSTB);
  137. // get our data to be saved.
  138. U32 width = bitmap->getWidth();
  139. U32 height = bitmap->getHeight();
  140. U32 bytes = bitmap->getBytesPerPixel();
  141. GFXFormat format = bitmap->getFormat();
  142. String ext = path.getExtension();
  143. U32 stride = width * bytes;
  144. // we always have at least 1
  145. U32 comp = 1;
  146. if (format == GFXFormatR8G8B8)
  147. {
  148. comp = 3;
  149. }
  150. else if (format == GFXFormatR8G8B8A8 || format == GFXFormatR8G8B8X8 || format == GFXFormatR8G8B8A8_LINEAR_FORCE)
  151. {
  152. comp = 4;
  153. }
  154. if (ext.equal("png"))
  155. {
  156. stbi_write_png_compression_level = compressionLevel;
  157. if (stbi_write_png(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits(), 0))
  158. return true;
  159. }
  160. if (ext.equal("tga"))
  161. {
  162. if (stbi_write_tga(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits()))
  163. return true;
  164. }
  165. if (ext.equal("bmp"))
  166. {
  167. if (stbi_write_bmp(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits()))
  168. return true;
  169. }
  170. if (ext.equal("jpg") || ext.equal("jpeg"))
  171. {
  172. if (stbi_write_jpg(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits(), 90))
  173. return true;
  174. }
  175. if (ext.equal("hdr"))
  176. {
  177. if (stbi_write_hdr(path.getFullPath().c_str(), width, height, comp, (const F32*)bitmap->getWritableBits()))
  178. return true;
  179. }
  180. return false;
  181. }