imageUtils.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2016 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 "gfx/bitmap/imageUtils.h"
  24. #include "gfx/bitmap/ddsFile.h"
  25. #include "platform/threads/threadPool.h"
  26. #include "squish/squish.h"
  27. namespace ImageUtil
  28. {
  29. // get squish quality flag
  30. S32 _getSquishQuality(const CompressQuality quality)
  31. {
  32. switch (quality)
  33. {
  34. case LowQuality:
  35. return squish::kColourRangeFit;
  36. case MediumQuality:
  37. return squish::kColourClusterFit;
  38. case HighQuality:
  39. return squish::kColourIterativeClusterFit;
  40. default:
  41. return squish::kColourRangeFit;//default is low quality
  42. }
  43. }
  44. // get squish compression flag
  45. S32 _getSquishFormat(const GFXFormat compressFormat)
  46. {
  47. switch (compressFormat)
  48. {
  49. case GFXFormatBC1:
  50. return squish::kDxt1;
  51. case GFXFormatBC2:
  52. return squish::kDxt3;
  53. case GFXFormatBC3:
  54. return squish::kDxt5;
  55. case GFXFormatBC4:
  56. return squish::kBc4;
  57. case GFXFormatBC5:
  58. return squish::kBc5;
  59. default:
  60. return squish::kDxt1;
  61. }
  62. }
  63. //Thread work job for compression
  64. struct CompressJob : public ThreadPool::WorkItem
  65. {
  66. S32 width;
  67. S32 height;
  68. S32 flags;
  69. const U8 *pSrc;
  70. U8 *pDst;
  71. GFXFormat format;
  72. CompressQuality quality;
  73. CompressJob(const U8 *srcRGBA, U8 *dst, const S32 w, const S32 h, const GFXFormat compressFormat, const CompressQuality compressQuality)
  74. : pSrc(srcRGBA),pDst(dst), width(w), height(h), format(compressFormat),quality(compressQuality) {}
  75. protected:
  76. virtual void execute()
  77. {
  78. rawCompress(pSrc,pDst, width, height, format,quality);
  79. }
  80. };
  81. // compress raw pixel data, expects rgba format
  82. bool rawCompress(const U8 *srcRGBA, U8 *dst, const S32 width, const S32 height, const GFXFormat compressFormat, const CompressQuality compressQuality)
  83. {
  84. if (!isCompressedFormat(compressFormat))
  85. return false;
  86. S32 squishFlags = _getSquishQuality(compressQuality);
  87. S32 squishFormat = _getSquishFormat(compressFormat);
  88. squishFlags |= squishFormat;
  89. squish::CompressImage(srcRGBA, width,height,dst,squishFlags);
  90. return true;
  91. }
  92. // compress DDSFile
  93. bool ddsCompress(DDSFile *srcDDS, const GFXFormat compressFormat,const CompressQuality compressQuality)
  94. {
  95. if (srcDDS->mBytesPerPixel != 4)
  96. {
  97. Con::errorf("ImageCompress::ddsCompress: data must be 32bit");
  98. return false;
  99. }
  100. //can't compress DDSFile if it is already compressed
  101. if (ImageUtil::isCompressedFormat(srcDDS->mFormat))
  102. {
  103. Con::errorf("ImageCompress::ddsCompress: file is already compressed");
  104. return false;
  105. }
  106. const bool cubemap = srcDDS->mFlags.test(DDSFile::CubeMapFlag);
  107. const U32 mipCount = srcDDS->mMipMapCount;
  108. // We got this far, so assume we can finish (gosh I hope so)
  109. srcDDS->mFormat = compressFormat;
  110. srcDDS->mFlags.set(DDSFile::CompressedData);
  111. //grab global thread pool
  112. ThreadPool* pThreadPool = &ThreadPool::GLOBAL();
  113. if (cubemap)
  114. {
  115. static U32 nCubeFaces = 6;
  116. Vector<U8*> dstDataStore;
  117. dstDataStore.setSize(nCubeFaces * mipCount);
  118. for (S32 cubeFace = 0; cubeFace < nCubeFaces; cubeFace++)
  119. {
  120. DDSFile::SurfaceData *srcSurface = srcDDS->mSurfaces[cubeFace];
  121. for (U32 currentMip = 0; currentMip < mipCount; currentMip++)
  122. {
  123. const U32 dataIndex = cubeFace * mipCount + currentMip;
  124. const U8 *srcBits = srcSurface->mMips[currentMip];
  125. const U32 mipSz = srcDDS->getSurfaceSize(currentMip);
  126. U8 *dstBits = new U8[mipSz];
  127. dstDataStore[dataIndex] = dstBits;
  128. ThreadSafeRef<CompressJob> item(new CompressJob(srcBits, dstBits, srcDDS->getWidth(currentMip), srcDDS->getHeight(currentMip), compressFormat, compressQuality));
  129. pThreadPool->queueWorkItem(item);
  130. }
  131. }
  132. //wait for work items to finish
  133. pThreadPool->waitForAllItems();
  134. for (S32 cubeFace = 0; cubeFace < nCubeFaces; cubeFace++)
  135. {
  136. DDSFile::SurfaceData *pSrcSurface = srcDDS->mSurfaces[cubeFace];
  137. for (U32 currentMip = 0; currentMip < mipCount; currentMip++)
  138. {
  139. const U32 dataIndex = cubeFace * mipCount + currentMip;
  140. delete[] pSrcSurface->mMips[currentMip];
  141. pSrcSurface->mMips[currentMip] = dstDataStore[dataIndex];
  142. }
  143. }
  144. }
  145. else
  146. {
  147. // The source surface is the original surface of the file
  148. DDSFile::SurfaceData *pSrcSurface = srcDDS->mSurfaces.last();
  149. // Create a new surface, this will be the DXT compressed surface. Once we
  150. // are done, we can discard the old surface, and replace it with this one.
  151. DDSFile::SurfaceData *pNewSurface = new DDSFile::SurfaceData();
  152. //no point using threading if only 1 mip
  153. const bool useThreading = bool(mipCount > 1);
  154. for (U32 currentMip = 0; currentMip < mipCount; currentMip++)
  155. {
  156. const U8 *pSrcBits = pSrcSurface->mMips[currentMip];
  157. const U32 mipSz = srcDDS->getSurfaceSize(currentMip);
  158. U8 *pDstBits = new U8[mipSz];
  159. pNewSurface->mMips.push_back(pDstBits);
  160. if (useThreading)
  161. {
  162. // Create CompressJob item
  163. ThreadSafeRef<CompressJob> item(new CompressJob(pSrcBits, pDstBits, srcDDS->getWidth(currentMip), srcDDS->getHeight(currentMip), compressFormat, compressQuality));
  164. pThreadPool->queueWorkItem(item);
  165. }
  166. else
  167. rawCompress(pSrcBits, pDstBits, srcDDS->getWidth(currentMip), srcDDS->getHeight(currentMip), compressFormat, compressQuality);
  168. }
  169. //block and wait for CompressJobs to finish
  170. if(useThreading)
  171. pThreadPool->waitForAllItems();
  172. // Now delete the source surface and replace with new compressed surface
  173. srcDDS->mSurfaces.pop_back();
  174. delete pSrcSurface;
  175. srcDDS->mSurfaces.push_back(pNewSurface);
  176. }
  177. return true;
  178. }
  179. bool decompress(const U8 *src, U8 *dstRGBA,const S32 width,const S32 height, const GFXFormat srcFormat)
  180. {
  181. if (!isCompressedFormat(srcFormat))
  182. return false;
  183. S32 squishFlag = _getSquishFormat(srcFormat);
  184. squish::DecompressImage(dstRGBA, width, height, src, squishFlag);
  185. return true;
  186. }
  187. void swizzleDDS(DDSFile *srcDDS, const Swizzle<U8, 4> &swizzle)
  188. {
  189. if (srcDDS->mFlags.test(DDSFile::CubeMapFlag))
  190. {
  191. for (S32 cubeFace = 0; cubeFace < DDSFile::Cubemap_Surface_Count; cubeFace++)
  192. {
  193. for (S32 i = 0; i < srcDDS->mMipMapCount; i++)
  194. {
  195. swizzle.InPlace(srcDDS->mSurfaces[cubeFace]->mMips[i], srcDDS->getSurfaceSize(i));
  196. }
  197. }
  198. }
  199. else
  200. {
  201. for (S32 i = 0; i < srcDDS->mMipMapCount; i++)
  202. {
  203. swizzle.InPlace(srcDDS->mSurfaces.last()->mMips[i], srcDDS->getSurfaceSize(i));
  204. }
  205. }
  206. }
  207. bool isCompressedFormat(const GFXFormat format)
  208. {
  209. if (format >= GFXFormatBC1 && format <= GFXFormatBC3_SRGB)
  210. return true;
  211. else
  212. return false;
  213. }
  214. bool isAlphaFormat(const GFXFormat format)
  215. {
  216. switch (format)
  217. {
  218. case GFXFormatA8:
  219. case GFXFormatA4L4:
  220. case GFXFormatA8L8:
  221. case GFXFormatR5G5B5A1:
  222. case GFXFormatR8G8B8A8:
  223. case GFXFormatB8G8R8A8:
  224. case GFXFormatR16G16B16A16F:
  225. case GFXFormatR32G32B32A32F:
  226. case GFXFormatR10G10B10A2:
  227. //case GFXFormatBC1://todo BC1 can store alpha
  228. case GFXFormatBC2:
  229. case GFXFormatBC3:
  230. return true;
  231. default:
  232. return false;
  233. }
  234. }
  235. bool isSRGBFormat(const GFXFormat format)
  236. {
  237. switch (format)
  238. {
  239. case GFXFormatR8G8B8_SRGB:
  240. case GFXFormatR8G8B8A8_SRGB:
  241. case GFXFormatBC1_SRGB:
  242. case GFXFormatBC2_SRGB:
  243. case GFXFormatBC3_SRGB:
  244. return true;
  245. default:
  246. return false;
  247. };
  248. }
  249. GFXFormat toSRGBFormat(const GFXFormat format)
  250. {
  251. switch (format)
  252. {
  253. case GFXFormatR8G8B8:
  254. return GFXFormatR8G8B8_SRGB;
  255. case GFXFormatR8G8B8X8:
  256. case GFXFormatR8G8B8A8:
  257. return GFXFormatR8G8B8A8_SRGB;
  258. case GFXFormatBC1:
  259. return GFXFormatBC1_SRGB;
  260. case GFXFormatBC2:
  261. return GFXFormatBC2_SRGB;
  262. case GFXFormatBC3:
  263. return GFXFormatBC3_SRGB;
  264. default:
  265. return format;
  266. };
  267. }
  268. U32 getMaxMipCount(const U32 width, const U32 height)
  269. {
  270. return mFloor(mLog2(mMax(width, height))) + 1;
  271. }
  272. }