bitmapSTB.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. String ext = path.getExtension();
  63. U32 prevWaterMark = FrameAllocator::getWaterMark();
  64. if (!stbi_info(path.getFullPath().c_str(), &x, &y, &channels))
  65. {
  66. FrameAllocator::setWaterMark(prevWaterMark);
  67. return false;
  68. }
  69. // do this to map 2 channels to 4, 2 channel not supported by gbitmap yet..
  70. if (channels == 2)
  71. channels = 4;
  72. if (!ext.equal("png"))
  73. {
  74. if (stbi_is_16_bit(path.getFullPath().c_str()))
  75. {
  76. U16* data = stbi_load_16(path.getFullPath().c_str(), &x, &y, &n, channels);
  77. // if succesful deal make the bitmap, else try other loaders.
  78. if (data)
  79. {
  80. GFXFormat format;
  81. if (n == 1)
  82. format = GFXFormatL16;
  83. else
  84. format = GFXFormatR16G16B16A16; // not sure if this is correct.
  85. bitmap->deleteImage();
  86. // actually allocate the bitmap space...
  87. bitmap->allocateBitmap(x, y,
  88. false, // don't extrude miplevels...
  89. format); // use determined format...
  90. U16* pBase = (U16*)bitmap->getBits();
  91. U32 rowBytes = bitmap->getByteSize();
  92. dMemcpy(pBase, data, rowBytes);
  93. stbi_image_free(data);
  94. FrameAllocator::setWaterMark(prevWaterMark);
  95. return true;
  96. }
  97. }
  98. }
  99. if (ext.equal("hdr"))
  100. {
  101. // force load to 4 channel.
  102. float* data = stbi_loadf(path.getFullPath().c_str(), &x, &y, &n, 4);
  103. unsigned char* dataChar = stbi__hdr_to_ldr(data, x, y, 4);
  104. bitmap->deleteImage();
  105. // actually allocate the bitmap space...
  106. bitmap->allocateBitmap(x, y,
  107. false,
  108. GFXFormatR8G8B8A8);
  109. U8* pBase = (U8*)bitmap->getBits();
  110. U32 rowBytes = x * y * 4;
  111. dMemcpy(pBase, dataChar, rowBytes);
  112. stbi_image_free(dataChar);
  113. FrameAllocator::setWaterMark(prevWaterMark);
  114. return true;
  115. }
  116. unsigned char* data = stbi_load(path.getFullPath().c_str(), &x, &y, &n, channels);
  117. bitmap->deleteImage();
  118. GFXFormat format;
  119. switch (channels) {
  120. case 1:
  121. format = GFXFormatA8;
  122. break;
  123. case 2:
  124. format = GFXFormatA8L8;
  125. break;
  126. case 3:
  127. format = GFXFormatR8G8B8;
  128. break;
  129. case 4:
  130. format = GFXFormatR8G8B8A8;
  131. break;
  132. default:
  133. FrameAllocator::setWaterMark(prevWaterMark);
  134. return false;
  135. }
  136. // actually allocate the bitmap space...
  137. bitmap->allocateBitmap(x, y,
  138. false, // don't extrude miplevels...
  139. format); // use determined format...
  140. U8* pBase = (U8*)bitmap->getBits();
  141. U32 rowBytes = bitmap->getByteSize();
  142. dMemcpy(pBase, data, rowBytes);
  143. stbi_image_free(data);
  144. // Check this bitmap for transparency
  145. if (channels == 4)
  146. bitmap->checkForTransparency();
  147. FrameAllocator::setWaterMark(prevWaterMark);
  148. return true;
  149. }
  150. /**
  151. * Write bitmap to an image file.
  152. *
  153. * @param[in] path Destination image file path. File name extension determines image format.
  154. * ".bmp" for Microsoft Bitmap.
  155. * ".hdr" for High Dynamic Range (HDR).
  156. * ".jpg" or ".jpeg" for Joint Photographic Experts Group (JPEG).
  157. * ".png" for Portable Network Graphics (PNG).
  158. * ".tga" for Truevision TGA (TARGA).
  159. *
  160. *
  161. * @param[in] bitmap Source bitmap to encode image from.
  162. * @param compressionLevel Image format specific compression level.
  163. * For JPEG sets the quality level percentage, range 0 to 100.
  164. * Not used for other image formats.
  165. */
  166. bool sWriteSTB(const Torque::Path& path, GBitmap* bitmap, U32 compressionLevel)
  167. {
  168. PROFILE_SCOPE(sWriteSTB);
  169. // get our data to be saved.
  170. U32 width = bitmap->getWidth();
  171. U32 height = bitmap->getHeight();
  172. U32 bytes = bitmap->getBytesPerPixel();
  173. GFXFormat format = bitmap->getFormat();
  174. String ext = path.getExtension();
  175. U32 stride = width * bytes;
  176. // we always have at least 1
  177. U32 comp = 1;
  178. if (format == GFXFormatR8G8B8)
  179. {
  180. comp = 3;
  181. }
  182. else if (format == GFXFormatR8G8B8A8 || format == GFXFormatR8G8B8X8 || format == GFXFormatR8G8B8A8_LINEAR_FORCE)
  183. {
  184. comp = 4;
  185. }
  186. if (ext.equal("png"))
  187. {
  188. stbi_write_png_compression_level = compressionLevel;
  189. if (stbi_write_png(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits(), 0))
  190. return true;
  191. }
  192. if (ext.equal("tga"))
  193. {
  194. if (stbi_write_tga(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits()))
  195. return true;
  196. }
  197. if (ext.equal("bmp"))
  198. {
  199. if (stbi_write_bmp(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits()))
  200. return true;
  201. }
  202. if (ext.equal("jpg") || ext.equal("jpeg"))
  203. {
  204. if (stbi_write_jpg(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits(), compressionLevel))
  205. return true;
  206. }
  207. if (ext.equal("hdr"))
  208. {
  209. if (stbi_write_hdr(path.getFullPath().c_str(), width, height, comp, (const F32*)bitmap->getWritableBits()))
  210. return true;
  211. }
  212. return false;
  213. }