BlitSurface.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Image/BlitSurface.h>
  5. #include <Image/Surface.h>
  6. #include <Jolt/Core/Color.h>
  7. #include <Jolt/Core/Profiler.h>
  8. //////////////////////////////////////////////////////////////////////////////////////////
  9. // BlitSettings
  10. //////////////////////////////////////////////////////////////////////////////////////////
  11. const BlitSettings BlitSettings::sDefault;
  12. BlitSettings::BlitSettings() :
  13. mConvertRGBToAlpha(false),
  14. mConvertAlphaToRGB(false),
  15. mConvertToGrayScale(false),
  16. mInvertAlpha(false),
  17. mColorKeyAlpha(false),
  18. mColorKeyStart(240, 0, 240),
  19. mColorKeyEnd(255, 15, 255)
  20. {
  21. }
  22. bool BlitSettings::operator == (const BlitSettings &inRHS) const
  23. {
  24. return mConvertRGBToAlpha == inRHS.mConvertRGBToAlpha
  25. && mConvertAlphaToRGB == inRHS.mConvertAlphaToRGB
  26. && mConvertToGrayScale == inRHS.mConvertToGrayScale
  27. && mInvertAlpha == inRHS.mInvertAlpha
  28. && mColorKeyAlpha == inRHS.mColorKeyAlpha
  29. && mColorKeyStart == inRHS.mColorKeyStart
  30. && mColorKeyEnd == inRHS.mColorKeyEnd
  31. && mZoomSettings == inRHS.mZoomSettings;
  32. }
  33. //////////////////////////////////////////////////////////////////////////////////////////
  34. // Converting from one format to another
  35. //////////////////////////////////////////////////////////////////////////////////////////
  36. // The macro COL(s) converts color s to another color given the mapping tables
  37. #define CMP(s, c) map[256 * c + ((s & src_mask[c]) >> src_shift[c])]
  38. #define COL(s) (CMP(s, 0) + CMP(s, 1) + CMP(s, 2) + CMP(s, 3))
  39. static void sComputeTranslationTable(const FormatDescription & inSrcDesc, const FormatDescription & inDstDesc, uint32 *outMask, uint32 *outShift, uint32 *outMap)
  40. {
  41. JPH_PROFILE("sComputeTranslationTable");
  42. // Compute translation tables for each color component
  43. uint32 written_mask = 0;
  44. for (int c = 0; c < 4; ++c)
  45. {
  46. outMask[c] = inSrcDesc.GetComponentMask(c);
  47. outShift[c] = CountTrailingZeros(outMask[c]);
  48. uint32 src_shifted_mask = outMask[c] >> outShift[c];
  49. uint32 dst_mask = inDstDesc.GetComponentMask(c);
  50. uint32 dst_shift = CountTrailingZeros(dst_mask);
  51. uint32 dst_shifted_mask = dst_mask >> dst_shift;
  52. if ((written_mask & dst_mask) != 0)
  53. {
  54. dst_mask = 0;
  55. dst_shift = 0;
  56. dst_shifted_mask = 0;
  57. }
  58. else
  59. written_mask |= dst_mask;
  60. float scale = float(dst_shifted_mask) / src_shifted_mask;
  61. uint32 entry = 0;
  62. if (src_shifted_mask != 0)
  63. for (; entry <= src_shifted_mask; ++entry)
  64. outMap[256 * c + entry] = uint32(round(scale * entry)) << dst_shift;
  65. for (; entry < 256; ++entry)
  66. outMap[256 * c + entry] = dst_mask;
  67. }
  68. }
  69. static bool sConvertImageDifferentTypes(RefConst<Surface> inSrc, Ref<Surface> ioDst)
  70. {
  71. JPH_PROFILE("sConvertImageDifferentTypes");
  72. // Get image properties
  73. int sbpp = inSrc->GetBytesPerPixel();
  74. int dbpp = ioDst->GetBytesPerPixel();
  75. int width = inSrc->GetWidth();
  76. int height = inSrc->GetHeight();
  77. JPH_ASSERT(width == ioDst->GetWidth());
  78. JPH_ASSERT(height == ioDst->GetHeight());
  79. // Compute conversion map
  80. uint32 src_mask[4];
  81. uint32 src_shift[4];
  82. uint32 map[4 * 256];
  83. sComputeTranslationTable(inSrc->GetFormatDescription(), ioDst->GetFormatDescription(), src_mask, src_shift, map);
  84. inSrc->Lock(ESurfaceLockMode::Read);
  85. ioDst->Lock(ESurfaceLockMode::Write);
  86. // Convert the image
  87. for (int y = 0; y < height; ++y)
  88. {
  89. const uint8 *s = inSrc->GetScanLine(y);
  90. const uint8 *s_end = inSrc->GetScanLine(y) + width * sbpp;
  91. uint8 *d = ioDst->GetScanLine(y);
  92. while (s < s_end)
  93. {
  94. uint32 src = 0;
  95. memcpy(&src, s, sbpp);
  96. uint32 dst = COL(src);
  97. memcpy(d, &dst, dbpp);
  98. s += sbpp;
  99. d += dbpp;
  100. }
  101. }
  102. inSrc->UnLock();
  103. ioDst->UnLock();
  104. return true;
  105. }
  106. static bool sConvertImageSameTypes(RefConst<Surface> inSrc, Ref<Surface> ioDst)
  107. {
  108. JPH_PROFILE("sConvertImageSameTypes");
  109. // Get image properties
  110. int dbpp = ioDst->GetBytesPerPixel();
  111. int width = inSrc->GetWidth();
  112. int height = inSrc->GetHeight();
  113. JPH_ASSERT(inSrc->GetFormat() == ioDst->GetFormat());
  114. JPH_ASSERT(dbpp == inSrc->GetBytesPerPixel());
  115. JPH_ASSERT(width == ioDst->GetWidth());
  116. JPH_ASSERT(height == ioDst->GetHeight());
  117. inSrc->Lock(ESurfaceLockMode::Read);
  118. ioDst->Lock(ESurfaceLockMode::Write);
  119. // Copy the image line by line to compensate for stride
  120. for (int y = 0; y < height; ++y)
  121. memcpy(ioDst->GetScanLine(y), inSrc->GetScanLine(y), width * dbpp);
  122. inSrc->UnLock();
  123. ioDst->UnLock();
  124. return true;
  125. }
  126. static bool sConvertImage(RefConst<Surface> inSrc, Ref<Surface> ioDst)
  127. {
  128. JPH_PROFILE("sConvertImage");
  129. if (inSrc->GetFormat() == ioDst->GetFormat())
  130. return sConvertImageSameTypes(inSrc, ioDst);
  131. else
  132. return sConvertImageDifferentTypes(inSrc, ioDst);
  133. }
  134. //////////////////////////////////////////////////////////////////////////////////////////
  135. // Special color conversions
  136. //////////////////////////////////////////////////////////////////////////////////////////
  137. static void sConvertRGBToAlpha(Ref<Surface> ioSurface)
  138. {
  139. JPH_PROFILE("sConvertRGBToAlpha");
  140. // Check surface format
  141. JPH_ASSERT(ioSurface->GetFormat() == ESurfaceFormat::A8R8G8B8);
  142. // Get dimensions of image
  143. int width = ioSurface->GetWidth();
  144. int height = ioSurface->GetHeight();
  145. // Convert RGB values to alpha values
  146. for (int y = 0; y < height; ++y)
  147. {
  148. Color *c = (Color *)ioSurface->GetScanLine(y);
  149. Color *c_end = (Color *)(ioSurface->GetScanLine(y) + width * sizeof(Color));
  150. while (c < c_end)
  151. {
  152. c->a = c->GetIntensity();
  153. ++c;
  154. }
  155. }
  156. }
  157. static void sConvertAlphaToRGB(Ref<Surface> ioSurface)
  158. {
  159. JPH_PROFILE("sConvertAlphaToRGB");
  160. // Check surface format
  161. JPH_ASSERT(ioSurface->GetFormat() == ESurfaceFormat::A8R8G8B8);
  162. // Get dimensions of image
  163. int width = ioSurface->GetWidth();
  164. int height = ioSurface->GetHeight();
  165. // Convert alpha values to RGB values
  166. for (int y = 0; y < height; ++y)
  167. {
  168. Color *c = (Color *)ioSurface->GetScanLine(y);
  169. Color *c_end = (Color *)(ioSurface->GetScanLine(y) + width * sizeof(Color));
  170. while (c < c_end)
  171. {
  172. c->r = c->g = c->b = c->a;
  173. ++c;
  174. }
  175. }
  176. }
  177. static void sConvertToGrayScale(Ref<Surface> ioSurface)
  178. {
  179. JPH_PROFILE("sConvertToGrayScale");
  180. // Check surface format
  181. JPH_ASSERT(ioSurface->GetFormat() == ESurfaceFormat::A8R8G8B8);
  182. // Get dimensions of image
  183. int width = ioSurface->GetWidth();
  184. int height = ioSurface->GetHeight();
  185. // Convert RGB values to grayscale values
  186. for (int y = 0; y < height; ++y)
  187. {
  188. Color *c = (Color *)ioSurface->GetScanLine(y);
  189. Color *c_end = (Color *)(ioSurface->GetScanLine(y) + width * sizeof(Color));
  190. while (c < c_end)
  191. {
  192. uint8 intensity = c->GetIntensity();
  193. c->r = intensity;
  194. c->g = intensity;
  195. c->b = intensity;
  196. ++c;
  197. }
  198. }
  199. }
  200. static void sInvertAlpha(Ref<Surface> ioSurface)
  201. {
  202. JPH_PROFILE("sInvertAlpha");
  203. // Check surface format
  204. JPH_ASSERT(ioSurface->GetFormat() == ESurfaceFormat::A8R8G8B8);
  205. // Get dimensions of image
  206. int width = ioSurface->GetWidth();
  207. int height = ioSurface->GetHeight();
  208. // Invert all alpha values
  209. for (int y = 0; y < height; ++y)
  210. {
  211. Color *c = (Color *)ioSurface->GetScanLine(y);
  212. Color *c_end = (Color *)(ioSurface->GetScanLine(y) + width * sizeof(Color));
  213. while (c < c_end)
  214. {
  215. c->a = uint8(255 - c->a);
  216. ++c;
  217. }
  218. }
  219. }
  220. static void sColorKeyAlpha(Ref<Surface> ioSurface, ColorArg inStart, ColorArg inEnd)
  221. {
  222. JPH_PROFILE("sColorKeyAlpha");
  223. // Check surface format
  224. JPH_ASSERT(ioSurface->GetFormat() == ESurfaceFormat::A8R8G8B8);
  225. // Get dimensions of image
  226. int width = ioSurface->GetWidth();
  227. int height = ioSurface->GetHeight();
  228. // Set alpha values
  229. for (int y = 0; y < height; ++y)
  230. {
  231. Color *c = (Color *)ioSurface->GetScanLine(y);
  232. Color *c_end = (Color *)(ioSurface->GetScanLine(y) + width * sizeof(Color));
  233. while (c < c_end)
  234. {
  235. if (c->r >= inStart.r && c->r <= inEnd.r && c->g >= inStart.g && c->g <= inEnd.g && c->b >= inStart.b && c->b <= inEnd.b)
  236. c->a = 0;
  237. else
  238. c->a = 255;
  239. ++c;
  240. }
  241. }
  242. }
  243. //////////////////////////////////////////////////////////////////////////////////////////
  244. // BlitSurface
  245. //////////////////////////////////////////////////////////////////////////////////////////
  246. bool BlitSurface(RefConst<Surface> inSrc, Ref<Surface> ioDst, const BlitSettings &inBlitSettings)
  247. {
  248. JPH_PROFILE("BlitSurface");
  249. // Do extra conversion options
  250. RefConst<Surface> src = inSrc;
  251. if (inBlitSettings.mConvertRGBToAlpha || inBlitSettings.mConvertAlphaToRGB || inBlitSettings.mConvertToGrayScale || inBlitSettings.mInvertAlpha || inBlitSettings.mColorKeyAlpha)
  252. {
  253. // Do them on A8R8G8B8 format so the conversion routines are simple
  254. Ref<Surface> tmp = new SoftwareSurface(inSrc->GetWidth(), inSrc->GetHeight(), ESurfaceFormat::A8R8G8B8);
  255. sConvertImage(inSrc, tmp);
  256. src = tmp;
  257. // Perform all optional conversions
  258. tmp->Lock(ESurfaceLockMode::ReadWrite);
  259. if (inBlitSettings.mConvertRGBToAlpha)
  260. sConvertRGBToAlpha(tmp);
  261. if (inBlitSettings.mConvertAlphaToRGB)
  262. sConvertAlphaToRGB(tmp);
  263. if (inBlitSettings.mConvertToGrayScale)
  264. sConvertToGrayScale(tmp);
  265. if (inBlitSettings.mInvertAlpha)
  266. sInvertAlpha(tmp);
  267. if (inBlitSettings.mColorKeyAlpha)
  268. sColorKeyAlpha(tmp, inBlitSettings.mColorKeyStart, inBlitSettings.mColorKeyEnd);
  269. tmp->UnLock();
  270. }
  271. if (src->GetWidth() != ioDst->GetWidth() || src->GetHeight() != ioDst->GetHeight())
  272. {
  273. // Zoom the image if the destination size is not equal to the source size
  274. if (!ZoomImage(src, ioDst, inBlitSettings.mZoomSettings))
  275. return false;
  276. }
  277. else
  278. {
  279. // Convert the image if the sizes are equal
  280. if (!sConvertImage(src, ioDst))
  281. return false;
  282. }
  283. return true;
  284. }