2
0

ZoomImage.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Image/ZoomImage.h>
  5. #include <Image/Surface.h>
  6. #include <Image/BlitSurface.h>
  7. #include <Jolt/Core/Profiler.h>
  8. //////////////////////////////////////////////////////////////////////////////////////////
  9. // ImageFilter
  10. //
  11. // Abstract class and some implementations of a filter, essentially an 1D weighting function
  12. // which is not zero for t e [-GetSupport(), GetSupport()] and zero for all other t
  13. // The integrand is usually 1 although it is not required for this implementation,
  14. // since the filter is renormalized when it is sampled.
  15. //////////////////////////////////////////////////////////////////////////////////////////
  16. class ImageFilter
  17. {
  18. public:
  19. // Destructor
  20. virtual ~ImageFilter() = default;
  21. // Get support of this filter (+/- the range the filter function is not zero)
  22. virtual float GetSupport() const = 0;
  23. // Sample filter function at a certain point
  24. virtual float GetValue(float t) const = 0;
  25. };
  26. class ImageFilterBox : public ImageFilter
  27. {
  28. virtual float GetSupport() const override
  29. {
  30. return 0.5f;
  31. }
  32. virtual float GetValue(float t) const override
  33. {
  34. if (abs(t) <= 0.5f)
  35. return 1.0f;
  36. else
  37. return 0.0f;
  38. }
  39. };
  40. class ImageFilterTriangle : public ImageFilter
  41. {
  42. virtual float GetSupport() const override
  43. {
  44. return 1.0f;
  45. }
  46. virtual float GetValue(float t) const override
  47. {
  48. t = abs(t);
  49. if (t < 1.0f)
  50. return 1.0f - t;
  51. else
  52. return 0.0f;
  53. }
  54. };
  55. class ImageFilterBell : public ImageFilter
  56. {
  57. virtual float GetSupport() const override
  58. {
  59. return 1.5f;
  60. }
  61. virtual float GetValue(float t) const override
  62. {
  63. t = abs(t);
  64. if (t < 0.5f)
  65. return 0.75f - t * t;
  66. else if (t < 1.5f)
  67. {
  68. t = t - 1.5f;
  69. return 0.5f * t * t;
  70. }
  71. else
  72. return 0.0f;
  73. }
  74. };
  75. class ImageFilterBSpline : public ImageFilter
  76. {
  77. virtual float GetSupport() const override
  78. {
  79. return 2.0f;
  80. }
  81. virtual float GetValue(float t) const override
  82. {
  83. t = abs(t);
  84. if (t < 1.0f)
  85. {
  86. float tt = t * t;
  87. return (0.5f * tt * t) - tt + (2.0f / 3.0f);
  88. }
  89. else if (t < 2.0f)
  90. {
  91. t = 2.0f - t;
  92. return (1.0f / 6.0f) * (t * t * t);
  93. }
  94. else
  95. return 0.0f;
  96. }
  97. };
  98. class ImageFilterLanczos3 : public ImageFilter
  99. {
  100. virtual float GetSupport() const override
  101. {
  102. return 3.0f;
  103. }
  104. virtual float GetValue(float t) const override
  105. {
  106. t = abs(t);
  107. if (t < 3.0f)
  108. return Sinc(t) * Sinc(t / 3.0f);
  109. else
  110. return 0.0f;
  111. }
  112. private:
  113. static float Sinc(float x)
  114. {
  115. x *= JPH_PI;
  116. if (abs(x) < 1.0e-5f)
  117. return 1.0f;
  118. return Sin(x) / x;
  119. }
  120. };
  121. class ImageFilterMitchell : public ImageFilter
  122. {
  123. virtual float GetSupport() const override
  124. {
  125. return 2.0f;
  126. }
  127. virtual float GetValue(float t) const override
  128. {
  129. float tt = t * t;
  130. t = abs(t);
  131. if (t < 1.0f)
  132. return (7.0f * (t * tt) - 12.0f * tt + (16.0f / 3.0f)) / 6.0f;
  133. else if (t < 2.0f)
  134. return ((-7.0f / 3.0f) * (t * tt) + 12.0f * tt + -20.0f * t + (32.0f / 3.0f)) / 6.0f;
  135. else
  136. return 0.0f;
  137. }
  138. };
  139. static const ImageFilter &GetFilter(EFilter inFilter)
  140. {
  141. static ImageFilterBox box;
  142. static ImageFilterTriangle triangle;
  143. static ImageFilterBell bell;
  144. static ImageFilterBSpline bspline;
  145. static ImageFilterLanczos3 lanczos3;
  146. static ImageFilterMitchell mitchell;
  147. switch (inFilter)
  148. {
  149. case FilterBox: return box;
  150. case FilterTriangle: return triangle;
  151. case FilterBell: return bell;
  152. case FilterBSpline: return bspline;
  153. case FilterLanczos3: return lanczos3;
  154. case FilterMitchell: return mitchell;
  155. default: JPH_ASSERT(false); return mitchell;
  156. }
  157. }
  158. //////////////////////////////////////////////////////////////////////////////////////////
  159. // ZoomSettings
  160. //////////////////////////////////////////////////////////////////////////////////////////
  161. const ZoomSettings ZoomSettings::sDefault;
  162. ZoomSettings::ZoomSettings() :
  163. mFilter(FilterMitchell),
  164. mWrapFilter(true),
  165. mBlur(1.0f)
  166. {
  167. }
  168. bool ZoomSettings::operator == (const ZoomSettings &inRHS) const
  169. {
  170. return mFilter == inRHS.mFilter
  171. && mWrapFilter == inRHS.mWrapFilter
  172. && mBlur == inRHS.mBlur;
  173. }
  174. //////////////////////////////////////////////////////////////////////////////////////////
  175. // Resizing a surface
  176. //////////////////////////////////////////////////////////////////////////////////////////
  177. // Structure used for zooming
  178. struct Contrib
  179. {
  180. int mOffset; // Offset of this pixel (relative to start of scanline)
  181. int mWeight; // Weight of this pixel in 0.12 fixed point format
  182. };
  183. static void sPrecalculateFilter(const ZoomSettings &inZoomSettings, int inOldLength, int inNewLength, int inOffsetFactor, Array<Array<Contrib>> &outContrib)
  184. {
  185. JPH_PROFILE("PrecalculateFilter");
  186. // Get filter
  187. const ImageFilter &filter = GetFilter(inZoomSettings.mFilter);
  188. // Get scale
  189. float scale = float(inNewLength) / inOldLength;
  190. float fwidth, fscale;
  191. if (scale < 1.0f)
  192. {
  193. // Minify, broaden filter
  194. fwidth = filter.GetSupport() / scale;
  195. fscale = scale;
  196. }
  197. else
  198. {
  199. // Enlarge, filter is always used as is
  200. fwidth = filter.GetSupport();
  201. fscale = 1.0f;
  202. }
  203. // Adjust filter for blur
  204. fwidth *= inZoomSettings.mBlur;
  205. fscale /= inZoomSettings.mBlur;
  206. float min_fwidth = 1.0f;
  207. if (fwidth < min_fwidth)
  208. {
  209. fwidth = min_fwidth;
  210. fscale = filter.GetSupport() / min_fwidth;
  211. }
  212. // Make room for a whole scanline
  213. outContrib.resize(inNewLength);
  214. // Loop over the whole scanline
  215. for (int i = 0; i < inNewLength; ++i)
  216. {
  217. // Compute center and left- and rightmost pixels affected
  218. float center = float(i) / scale;
  219. int left = int(floor(center - fwidth));
  220. int right = int(ceil(center + fwidth));
  221. // Reserve required elements
  222. Array<Contrib> &a = outContrib[i];
  223. a.reserve(right - left + 1);
  224. // Total sum of all weights, for renormalization of the filter
  225. int filter_sum = 0;
  226. // Compute the contributions for each
  227. for (int source = left; source <= right; ++source)
  228. {
  229. Contrib c;
  230. // Initialize the offset
  231. c.mOffset = source;
  232. // Compute weight at this position in 0.12 fixed point
  233. c.mWeight = int(4096.0f * filter.GetValue(fscale * (center - source)));
  234. if (c.mWeight == 0) continue;
  235. // Add weight to filter total
  236. filter_sum += c.mWeight;
  237. // Reflect the filter at the edges if the filter is not to be wrapped (clamp)
  238. if (!inZoomSettings.mWrapFilter && (c.mOffset < 0 || c.mOffset >= inOldLength))
  239. c.mOffset = -c.mOffset - 1;
  240. // Wrap the offset so that it falls within the image
  241. c.mOffset = (c.mOffset % inOldLength + inOldLength) % inOldLength;
  242. // Check that the offset falls within the image
  243. JPH_ASSERT(c.mOffset >= 0 && c.mOffset < inOldLength);
  244. // Multiply the offset with the specified factor
  245. c.mOffset *= inOffsetFactor;
  246. // Add the filter element
  247. a.push_back(c);
  248. }
  249. // Normalize the filter to 0.12 fixed point
  250. if (filter_sum != 0)
  251. for (uint n = 0; n < a.size(); ++n)
  252. a[n].mWeight = (a[n].mWeight * 4096) / filter_sum;
  253. }
  254. }
  255. static void sZoomHorizontal(RefConst<Surface> inSrc, Ref<Surface> ioDst, const ZoomSettings &inZoomSettings)
  256. {
  257. JPH_PROFILE("ZoomHorizontal");
  258. // Check zoom parameters
  259. JPH_ASSERT(inSrc->GetHeight() == ioDst->GetHeight());
  260. JPH_ASSERT(inSrc->GetFormat() == ioDst->GetFormat());
  261. const int width = ioDst->GetWidth();
  262. const int height = ioDst->GetHeight();
  263. const int components = ioDst->GetNumberOfComponents();
  264. const int delta_s = -components;
  265. const int delta_d = ioDst->GetBytesPerPixel() - components;
  266. // Pre-calculate filter contributions for a row
  267. Array<Array<Contrib>> contrib;
  268. sPrecalculateFilter(inZoomSettings, inSrc->GetWidth(), ioDst->GetWidth(), inSrc->GetBytesPerPixel(), contrib);
  269. // Do the zoom
  270. for (int y = 0; y < height; ++y)
  271. {
  272. const uint8 *s = inSrc->GetScanLine(y);
  273. uint8 *d = ioDst->GetScanLine(y);
  274. for (int x = 0; x < width; ++x)
  275. {
  276. const Array<Contrib> &line = contrib[x];
  277. const size_t line_size_min_one = line.size() - 1;
  278. int c = components;
  279. do
  280. {
  281. int pixel = 0;
  282. // Apply the filter for one color component
  283. size_t j = line_size_min_one;
  284. do
  285. {
  286. const Contrib &cmp = line[j];
  287. pixel += cmp.mWeight * s[cmp.mOffset];
  288. }
  289. while (j--);
  290. // Clamp the pixel value
  291. if (pixel <= 0)
  292. *d = 0;
  293. else if (pixel >= (255 << 12))
  294. *d = 255;
  295. else
  296. *d = uint8(pixel >> 12);
  297. ++s;
  298. ++d;
  299. }
  300. while (--c);
  301. // Skip unused components if there are any
  302. s += delta_s;
  303. d += delta_d;
  304. }
  305. }
  306. }
  307. static void sZoomVertical(RefConst<Surface> inSrc, Ref<Surface> ioDst, const ZoomSettings &inZoomSettings)
  308. {
  309. JPH_PROFILE("ZoomVertical");
  310. // Check zoom parameters
  311. JPH_ASSERT(inSrc->GetWidth() == ioDst->GetWidth());
  312. JPH_ASSERT(inSrc->GetFormat() == ioDst->GetFormat());
  313. const int width = ioDst->GetWidth();
  314. const int height = ioDst->GetHeight();
  315. const int components = ioDst->GetNumberOfComponents();
  316. const int delta_s = inSrc->GetBytesPerPixel() - components;
  317. const int delta_d = ioDst->GetBytesPerPixel() - components;
  318. // Pre-calculate filter contributions for a row
  319. Array<Array<Contrib>> contrib;
  320. sPrecalculateFilter(inZoomSettings, inSrc->GetHeight(), ioDst->GetHeight(), inSrc->GetStride(), contrib);
  321. // Do the zoom
  322. for (int y = 0; y < height; ++y)
  323. {
  324. const uint8 *s = inSrc->GetScanLine(0);
  325. uint8 *d = ioDst->GetScanLine(y);
  326. const Array<Contrib> &line = contrib[y];
  327. const size_t line_size_min_one = line.size() - 1;
  328. for (int x = 0; x < width; ++x)
  329. {
  330. int c = components;
  331. do
  332. {
  333. int pixel = 0;
  334. // Apply the filter for one color component
  335. size_t j = line_size_min_one;
  336. do
  337. {
  338. const Contrib &cmp = line[j];
  339. pixel += cmp.mWeight * s[cmp.mOffset];
  340. }
  341. while (j--);
  342. // Clamp the pixel value
  343. if (pixel <= 0)
  344. *d = 0;
  345. else if (pixel >= (255 << 12))
  346. *d = 255;
  347. else
  348. *d = uint8(pixel >> 12);
  349. ++s;
  350. ++d;
  351. }
  352. while (--c);
  353. // Skip unused components if there are any
  354. s += delta_s;
  355. d += delta_d;
  356. }
  357. }
  358. }
  359. bool ZoomImage(RefConst<Surface> inSrc, Ref<Surface> ioDst, const ZoomSettings &inZoomSettings)
  360. {
  361. JPH_PROFILE("ZoomImage");
  362. // Get filter
  363. const ImageFilter &filter = GetFilter(inZoomSettings.mFilter);
  364. // Determine the temporary format that will require the least amount of components to be zoomed and the least amount of bytes pushed around
  365. ESurfaceFormat tmp_format;
  366. ESurfaceFormat src_format = inSrc->GetClosest8BitFormat();
  367. ESurfaceFormat dst_format = ioDst->GetClosest8BitFormat();
  368. const FormatDescription &src_desc = GetFormatDescription(src_format);
  369. const FormatDescription &dst_desc = GetFormatDescription(dst_format);
  370. if (src_desc.GetNumberOfComponents() < dst_desc.GetNumberOfComponents())
  371. tmp_format = src_format;
  372. else if (src_desc.GetNumberOfComponents() > dst_desc.GetNumberOfComponents())
  373. tmp_format = dst_format;
  374. else if (src_desc.GetBytesPerPixel() < dst_desc.GetBytesPerPixel())
  375. tmp_format = src_format;
  376. else
  377. tmp_format = dst_format;
  378. // Create temporary source buffer if nessecary
  379. RefConst<Surface> src = inSrc;
  380. if (inSrc->GetFormat() != tmp_format)
  381. {
  382. Ref<Surface> tmp = new SoftwareSurface(inSrc->GetWidth(), inSrc->GetHeight(), tmp_format);
  383. if (!BlitSurface(inSrc, tmp))
  384. return false;
  385. src = tmp;
  386. }
  387. // Create temporary destination buffer if nessecary
  388. Ref<Surface> dst = ioDst;
  389. if (ioDst->GetFormat() != tmp_format)
  390. dst = new SoftwareSurface(ioDst->GetWidth(), ioDst->GetHeight(), tmp_format);
  391. src->Lock(ESurfaceLockMode::Read);
  392. dst->Lock(ESurfaceLockMode::Write);
  393. if (src->GetWidth() == dst->GetWidth())
  394. {
  395. // Only vertical zoom required
  396. sZoomVertical(src, dst, inZoomSettings);
  397. }
  398. else if (src->GetHeight() == dst->GetHeight())
  399. {
  400. // Only horizontal zoom required
  401. sZoomHorizontal(src, dst, inZoomSettings);
  402. }
  403. else
  404. {
  405. // Determine most optimal order
  406. float operations_vh = float(dst->GetWidth()) * (filter.GetSupport() * src->GetHeight() + filter.GetSupport() * dst->GetHeight());
  407. float operations_hv = float(dst->GetHeight()) * (filter.GetSupport() * src->GetWidth() + filter.GetSupport() * dst->GetWidth());
  408. if (operations_vh < operations_hv)
  409. {
  410. // Create temporary buffer to hold the vertical scale
  411. Ref<Surface> tmp = new SoftwareSurface(src->GetWidth(), dst->GetHeight(), tmp_format);
  412. tmp->Lock(ESurfaceLockMode::ReadWrite);
  413. // First scale vertically then horizontally
  414. sZoomVertical(src, tmp, inZoomSettings);
  415. sZoomHorizontal(tmp, dst, inZoomSettings);
  416. tmp->UnLock();
  417. }
  418. else
  419. {
  420. // Create temporary buffer to hold the horizontal scale
  421. Ref<Surface> tmp = new SoftwareSurface(dst->GetWidth(), src->GetHeight(), tmp_format);
  422. tmp->Lock(ESurfaceLockMode::ReadWrite);
  423. // First scale horizontally then vertically
  424. sZoomHorizontal(src, tmp, inZoomSettings);
  425. sZoomVertical(tmp, dst, inZoomSettings);
  426. tmp->UnLock();
  427. }
  428. }
  429. src->UnLock();
  430. dst->UnLock();
  431. // Convert to destination if required
  432. if (dst != ioDst)
  433. if (!BlitSurface(dst, ioDst))
  434. return false;
  435. return true;
  436. }