bitmapSTB.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "console/console.h"
  24. #include "core/stream/fileStream.h"
  25. #include "core/stream/memStream.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include "gfx/bitmap/gBitmap.h"
  28. #ifdef __clang__
  29. #define STBIWDEF static inline
  30. #endif
  31. #define STB_IMAGE_IMPLEMENTATION
  32. #define STB_IMAGE_STATIC
  33. #include "stb_image.h"
  34. #define STB_IMAGE_WRITE_IMPLEMENTATION
  35. #define STB_IMAGE_WRITE_STATIC
  36. #include "stb_image_write.h"
  37. static bool sReadSTB(const Torque::Path& path, GBitmap* bitmap);
  38. static bool sReadStreamSTB(Stream& stream, GBitmap* bitmap, U32 len);
  39. static bool sWriteSTB(const Torque::Path& path, GBitmap* bitmap, U32 compressionLevel);
  40. static bool sWriteStreamSTB(Stream& stream, GBitmap* bitmap, U32 compressionLevel);
  41. static struct _privateRegisterSTB
  42. {
  43. _privateRegisterSTB()
  44. {
  45. GBitmap::Registration reg;
  46. reg.priority = 100;
  47. reg.extensions.push_back("png");
  48. reg.extensions.push_back("bmp");
  49. reg.extensions.push_back("jpg");
  50. reg.extensions.push_back("jpeg");
  51. reg.extensions.push_back("psd");
  52. reg.extensions.push_back("hdr");
  53. reg.extensions.push_back("tga");
  54. reg.readFunc = sReadSTB;
  55. reg.readStreamFunc = sReadStreamSTB;
  56. reg.writeFunc = sWriteSTB;
  57. reg.writeStreamFunc = sWriteStreamSTB;
  58. // for png only.
  59. reg.defaultCompression = 6;
  60. GBitmap::sRegisterFormat(reg);
  61. }
  62. } sStaticRegisterSTB;
  63. bool sReadSTB(const Torque::Path& path, GBitmap* bitmap)
  64. {
  65. PROFILE_SCOPE(sReadSTB);
  66. S32 x, y, n, channels;
  67. String ext = path.getExtension();
  68. U32 prevWaterMark = FrameAllocator::getWaterMark();
  69. if (!stbi_info(path.getFullPath().c_str(), &x, &y, &channels))
  70. {
  71. FrameAllocator::setWaterMark(prevWaterMark);
  72. return false;
  73. }
  74. // do this to map 2 channels to 4, 2 channel not supported by gbitmap yet..
  75. if (channels == 2)
  76. channels = 4;
  77. if (!ext.equal("png"))
  78. {
  79. if (stbi_is_16_bit(path.getFullPath().c_str()))
  80. {
  81. U16* data = stbi_load_16(path.getFullPath().c_str(), &x, &y, &n, channels);
  82. // if succesful deal make the bitmap, else try other loaders.
  83. if (data)
  84. {
  85. GFXFormat format;
  86. if (n == 1)
  87. format = GFXFormatL16;
  88. else
  89. format = GFXFormatR16G16B16A16; // not sure if this is correct.
  90. bitmap->deleteImage();
  91. // actually allocate the bitmap space...
  92. bitmap->allocateBitmap(x, y,
  93. false, // don't extrude miplevels...
  94. format); // use determined format...
  95. U16* pBase = (U16*)bitmap->getBits();
  96. U32 rowBytes = bitmap->getByteSize();
  97. dMemcpy(pBase, data, rowBytes);
  98. stbi_image_free(data);
  99. FrameAllocator::setWaterMark(prevWaterMark);
  100. return true;
  101. }
  102. }
  103. }
  104. if (ext.equal("hdr"))
  105. {
  106. // force load to 4 channel.
  107. float* data = stbi_loadf(path.getFullPath().c_str(), &x, &y, &n, 4);
  108. unsigned char* dataChar = stbi__hdr_to_ldr(data, x, y, 4);
  109. bitmap->deleteImage();
  110. // actually allocate the bitmap space...
  111. bitmap->allocateBitmap(x, y,
  112. false,
  113. GFXFormatR8G8B8A8);
  114. U8* pBase = (U8*)bitmap->getBits();
  115. U32 rowBytes = x * y * 4;
  116. dMemcpy(pBase, dataChar, rowBytes);
  117. stbi_image_free(dataChar);
  118. FrameAllocator::setWaterMark(prevWaterMark);
  119. return true;
  120. }
  121. unsigned char* data = stbi_load(path.getFullPath().c_str(), &x, &y, &n, channels);
  122. bitmap->deleteImage();
  123. GFXFormat format;
  124. switch (channels) {
  125. case 1:
  126. format = GFXFormatA8;
  127. break;
  128. case 2:
  129. format = GFXFormatA8L8;
  130. break;
  131. case 3:
  132. format = GFXFormatR8G8B8;
  133. break;
  134. case 4:
  135. format = GFXFormatR8G8B8A8;
  136. break;
  137. default:
  138. FrameAllocator::setWaterMark(prevWaterMark);
  139. return false;
  140. }
  141. // actually allocate the bitmap space...
  142. bitmap->allocateBitmap(x, y,
  143. false, // don't extrude miplevels...
  144. format); // use determined format...
  145. U8* pBase = (U8*)bitmap->getBits();
  146. U32 rowBytes = bitmap->getByteSize();
  147. dMemcpy(pBase, data, rowBytes);
  148. stbi_image_free(data);
  149. // Check this bitmap for transparency
  150. if (channels == 4)
  151. bitmap->checkForTransparency();
  152. FrameAllocator::setWaterMark(prevWaterMark);
  153. return true;
  154. }
  155. bool sReadStreamSTB(Stream& stream, GBitmap* bitmap, U32 len)
  156. {
  157. PROFILE_SCOPE(sReadStreamSTB);
  158. // only used for font at the moment.
  159. U8* data = new U8[len];
  160. stream.read(len, data);
  161. S32 width, height, comp = 0;
  162. unsigned char* pixelData = stbi_load_from_memory((const U8*)data, (int)len, &width, &height, &comp, 1);
  163. if (!pixelData)
  164. {
  165. const char* stbErr = stbi_failure_reason();
  166. if (!stbErr)
  167. stbErr = "Unknown Error!";
  168. Con::printf("sReadStreamSTB Error: %s", stbErr);
  169. return false;
  170. }
  171. bitmap->deleteImage();
  172. bitmap->allocateBitmap(256, 256, false, GFXFormatA8);
  173. U8* pBase = bitmap->getWritableBits(0);
  174. U32 rowBytes = bitmap->getByteSize();
  175. dMemcpy(pBase, pixelData, rowBytes);
  176. dFree(data);
  177. dFree(pixelData);
  178. return true;
  179. }
  180. /**
  181. * Write bitmap to an image file.
  182. *
  183. * @param[in] path Destination image file path. File name extension determines image format.
  184. * ".bmp" for Microsoft Bitmap.
  185. * ".hdr" for High Dynamic Range (HDR).
  186. * ".jpg" or ".jpeg" for Joint Photographic Experts Group (JPEG).
  187. * ".png" for Portable Network Graphics (PNG).
  188. * ".tga" for Truevision TGA (TARGA).
  189. *
  190. *
  191. * @param[in] bitmap Source bitmap to encode image from.
  192. * @param compressionLevel Image format specific compression level.
  193. * For JPEG sets the quality level percentage, range 0 to 100.
  194. * Not used for other image formats.
  195. */
  196. bool sWriteSTB(const Torque::Path& path, GBitmap* bitmap, U32 compressionLevel)
  197. {
  198. PROFILE_SCOPE(sWriteSTB);
  199. // get our data to be saved.
  200. U32 width = bitmap->getWidth();
  201. U32 height = bitmap->getHeight();
  202. U32 bytes = bitmap->getBytesPerPixel();
  203. GFXFormat format = bitmap->getFormat();
  204. String ext = path.getExtension();
  205. U32 stride = width * bytes;
  206. // we always have at least 1
  207. U32 comp = 1;
  208. if (format == GFXFormatR8G8B8)
  209. {
  210. comp = 3;
  211. }
  212. else if (format == GFXFormatR8G8B8A8 || format == GFXFormatR8G8B8X8 || format == GFXFormatR8G8B8A8_LINEAR_FORCE)
  213. {
  214. comp = 4;
  215. }
  216. if (ext.equal("png"))
  217. {
  218. stbi_write_png_compression_level = compressionLevel;
  219. if (stbi_write_png(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits(), 0))
  220. return true;
  221. }
  222. if (ext.equal("tga"))
  223. {
  224. if (stbi_write_tga(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits()))
  225. return true;
  226. }
  227. if (ext.equal("bmp"))
  228. {
  229. if (stbi_write_bmp(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits()))
  230. return true;
  231. }
  232. if (ext.equal("jpg") || ext.equal("jpeg"))
  233. {
  234. if (stbi_write_jpg(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits(), compressionLevel))
  235. return true;
  236. }
  237. if (ext.equal("hdr"))
  238. {
  239. if (stbi_write_hdr(path.getFullPath().c_str(), width, height, comp, (const F32*)bitmap->getWritableBits()))
  240. return true;
  241. }
  242. return false;
  243. }
  244. bool sWriteStreamSTB(Stream& stream, GBitmap* bitmap, U32 compressionLevel)
  245. {
  246. S32 len;
  247. const U8* pData = bitmap->getBits();
  248. unsigned char* png = stbi_write_png_to_mem(pData, 0, bitmap->getWidth(), bitmap->getHeight(), 1, &len);
  249. if (!png)
  250. {
  251. const char* stbErr = stbi_failure_reason();
  252. if (!stbErr)
  253. stbErr = "Unknown Error!";
  254. Con::printf("sReadStreamSTB Error: %s", stbErr);
  255. return false;
  256. }
  257. stream.write(len);
  258. stream.write(len, png);
  259. dFree(png);
  260. return true;
  261. }