mediaFilters.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2019 to 2022 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "mediaFilters.h"
  24. #include "../base/simd.h"
  25. #include "../api/drawAPI.h"
  26. using namespace dsr;
  27. template <typename T, typename U>
  28. static void assertSameSize(const T& imageA, const U& imageB) {
  29. if (!image_exists(imageA) || !image_exists(imageB)) {
  30. if (image_exists(imageA)) {
  31. // Left side exists, so there's no right side
  32. throwError("Media filter: Non-existing right side input image.\n");
  33. } else if (image_exists(imageB)) {
  34. // Right side exists, so there's no left side
  35. throwError("Media filter: Non-existing left side input image.\n");
  36. } else {
  37. // Neither input exists
  38. throwError("Media filter: Non-existing input images.\n");
  39. }
  40. } else if (image_getWidth(imageA) != image_getWidth(imageB)
  41. || image_getHeight(imageA) != image_getHeight(imageB)) {
  42. throwError("Media filter: Taking input images of different dimensions, ", image_getWidth(imageA), "x", image_getHeight(imageA), " and ", image_getWidth(imageB), "x", image_getHeight(imageB), ".\n");
  43. }
  44. }
  45. template <typename T>
  46. static void assertExisting(const T& image) {
  47. if (!image_exists(image)) {
  48. throwError("Media filter: Non-existing input image.\n");
  49. }
  50. }
  51. template <typename T>
  52. static void removeIfShared(T& targetImage) {
  53. if (image_useCount(targetImage) > 1) {
  54. targetImage = AlignedImageU8();
  55. }
  56. }
  57. template <typename T, typename U>
  58. static void allocateToSameSize(T& targetImage, const U& inputImage) {
  59. if (!image_exists(targetImage) || image_getWidth(targetImage) != image_getWidth(inputImage) || image_getHeight(targetImage) != image_getHeight(inputImage)) {
  60. if (!image_exists(inputImage)) {
  61. throwError("Media filter: Cannot allocate to size of non-existing input image.\n");
  62. }
  63. targetImage = image_create_U8(image_getWidth(inputImage), image_getHeight(inputImage));
  64. }
  65. }
  66. void dsr::media_filter_add(AlignedImageU8& targetImage, AlignedImageU8 imageA, AlignedImageU8 imageB) {
  67. assertSameSize(imageA, imageB);
  68. removeIfShared(targetImage);
  69. allocateToSameSize(targetImage, imageA);
  70. SafePointer<uint8_t> targetRow = image_getSafePointer(targetImage);
  71. SafePointer<uint8_t> sourceRowA = image_getSafePointer(imageA);
  72. SafePointer<uint8_t> sourceRowB = image_getSafePointer(imageB);
  73. int32_t targetStride = image_getStride(targetImage);
  74. int32_t sourceStrideA = image_getStride(imageA);
  75. int32_t sourceStrideB = image_getStride(imageB);
  76. for (int32_t y = 0; y < image_getHeight(targetImage); y++) {
  77. SafePointer<uint8_t> targetPixel = targetRow;
  78. SafePointer<uint8_t> sourcePixelA = sourceRowA;
  79. SafePointer<uint8_t> sourcePixelB = sourceRowB;
  80. for (int32_t x = 0; x < image_getWidth(targetImage); x += 16) {
  81. U8x16 colorA = U8x16::readAligned(sourcePixelA, "media_filter_add (sourcePixelA)");
  82. U8x16 colorB = U8x16::readAligned(sourcePixelB, "media_filter_add (sourcePixelB)");
  83. U8x16 result = saturatedAddition(colorA, colorB);
  84. result.writeAligned(targetPixel, "media_filter_add (targetPixel)");
  85. targetPixel += 16;
  86. sourcePixelA += 16;
  87. sourcePixelB += 16;
  88. }
  89. targetRow.increaseBytes(targetStride);
  90. sourceRowA.increaseBytes(sourceStrideA);
  91. sourceRowB.increaseBytes(sourceStrideB);
  92. }
  93. }
  94. void dsr::media_filter_add(AlignedImageU8& targetImage, AlignedImageU8 image, int32_t luma) {
  95. assertExisting(image);
  96. removeIfShared(targetImage);
  97. allocateToSameSize(targetImage, image);
  98. if (luma < 0) luma = 0;
  99. if (luma > 255) luma = 255;
  100. SafePointer<uint8_t> targetRow = image_getSafePointer(targetImage);
  101. SafePointer<uint8_t> sourceRowA = image_getSafePointer(image);
  102. int32_t targetStride = image_getStride(targetImage);
  103. int32_t sourceStride = image_getStride(image);
  104. U8x16 repeatedLuma = U8x16(luma);
  105. for (int32_t y = 0; y < image_getHeight(targetImage); y++) {
  106. SafePointer<uint8_t> targetPixel = targetRow;
  107. SafePointer<uint8_t> sourcePixel = sourceRowA;
  108. for (int32_t x = 0; x < image_getWidth(targetImage); x += 16) {
  109. U8x16 colorA = U8x16::readAligned(sourcePixel, "media_filter_add (sourcePixel)");
  110. U8x16 result = saturatedAddition(colorA, repeatedLuma);
  111. result.writeAligned(targetPixel, "media_filter_add (targetPixel)");
  112. targetPixel += 16;
  113. sourcePixel += 16;
  114. }
  115. targetRow.increaseBytes(targetStride);
  116. sourceRowA.increaseBytes(sourceStride);
  117. }
  118. }
  119. void dsr::media_filter_add(AlignedImageU8& targetImage, AlignedImageU8 image, FixedPoint luma) {
  120. media_filter_add(targetImage, image, fixedPoint_round(luma));
  121. }
  122. void dsr::media_filter_sub(AlignedImageU8& targetImage, AlignedImageU8 imageA, AlignedImageU8 imageB) {
  123. assertSameSize(imageA, imageB);
  124. removeIfShared(targetImage);
  125. allocateToSameSize(targetImage, imageA);
  126. SafePointer<uint8_t> targetRow = image_getSafePointer(targetImage);
  127. SafePointer<uint8_t> sourceRowA = image_getSafePointer(imageA);
  128. SafePointer<uint8_t> sourceRowB = image_getSafePointer(imageB);
  129. int32_t targetStride = image_getStride(targetImage);
  130. int32_t sourceStrideA = image_getStride(imageA);
  131. int32_t sourceStrideB = image_getStride(imageB);
  132. for (int32_t y = 0; y < image_getHeight(targetImage); y++) {
  133. SafePointer<uint8_t> targetPixel = targetRow;
  134. SafePointer<uint8_t> sourcePixelA = sourceRowA;
  135. SafePointer<uint8_t> sourcePixelB = sourceRowB;
  136. for (int32_t x = 0; x < image_getWidth(targetImage); x += 16) {
  137. U8x16 colorA = U8x16::readAligned(sourcePixelA, "media_filter_sub (sourcePixelA)");
  138. U8x16 colorB = U8x16::readAligned(sourcePixelB, "media_filter_sub (sourcePixelB)");
  139. U8x16 result = saturatedSubtraction(colorA, colorB);
  140. result.writeAligned(targetPixel, "media_filter_sub (targetPixel)");
  141. targetPixel += 16;
  142. sourcePixelA += 16;
  143. sourcePixelB += 16;
  144. }
  145. targetRow.increaseBytes(targetStride);
  146. sourceRowA.increaseBytes(sourceStrideA);
  147. sourceRowB.increaseBytes(sourceStrideB);
  148. }
  149. }
  150. void dsr::media_filter_sub(AlignedImageU8& targetImage, AlignedImageU8 image, int32_t luma) {
  151. assertExisting(image);
  152. removeIfShared(targetImage);
  153. allocateToSameSize(targetImage, image);
  154. if (luma < 0) luma = 0;
  155. if (luma > 255) luma = 255;
  156. SafePointer<uint8_t> targetRow = image_getSafePointer(targetImage);
  157. SafePointer<uint8_t> sourceRowA = image_getSafePointer(image);
  158. int32_t targetStride = image_getStride(targetImage);
  159. int32_t sourceStride = image_getStride(image);
  160. U8x16 repeatedLuma = U8x16(luma);
  161. for (int32_t y = 0; y < image_getHeight(targetImage); y++) {
  162. SafePointer<uint8_t> targetPixel = targetRow;
  163. SafePointer<uint8_t> sourcePixel = sourceRowA;
  164. for (int32_t x = 0; x < image_getWidth(targetImage); x += 16) {
  165. U8x16 colorA = U8x16::readAligned(sourcePixel, "media_filter_sub (sourcePixel)");
  166. U8x16 result = saturatedSubtraction(colorA, repeatedLuma);
  167. result.writeAligned(targetPixel, "media_filter_sub (targetPixel)");
  168. targetPixel += 16;
  169. sourcePixel += 16;
  170. }
  171. targetRow.increaseBytes(targetStride);
  172. sourceRowA.increaseBytes(sourceStride);
  173. }
  174. }
  175. void dsr::media_filter_sub(AlignedImageU8& targetImage, int32_t luma, AlignedImageU8 image) {
  176. assertExisting(image);
  177. removeIfShared(targetImage);
  178. allocateToSameSize(targetImage, image);
  179. if (luma < 0) luma = 0;
  180. if (luma > 255) luma = 255;
  181. SafePointer<uint8_t> targetRow = image_getSafePointer(targetImage);
  182. SafePointer<uint8_t> sourceRowA = image_getSafePointer(image);
  183. int32_t targetStride = image_getStride(targetImage);
  184. int32_t sourceStride = image_getStride(image);
  185. U8x16 repeatedLuma = U8x16(luma);
  186. for (int32_t y = 0; y < image_getHeight(targetImage); y++) {
  187. SafePointer<uint8_t> targetPixel = targetRow;
  188. SafePointer<uint8_t> sourcePixel = sourceRowA;
  189. for (int32_t x = 0; x < image_getWidth(targetImage); x += 16) {
  190. U8x16 colorA = U8x16::readAligned(sourcePixel, "media_filter_sub (sourcePixel)");
  191. U8x16 result = saturatedSubtraction(repeatedLuma, colorA);
  192. result.writeAligned(targetPixel, "media_filter_sub (targetPixel)");
  193. targetPixel += 16;
  194. sourcePixel += 16;
  195. }
  196. targetRow.increaseBytes(targetStride);
  197. sourceRowA.increaseBytes(sourceStride);
  198. }
  199. }
  200. void dsr::media_filter_sub(AlignedImageU8& targetImage, AlignedImageU8 image, FixedPoint luma) {
  201. media_filter_sub(targetImage, image, fixedPoint_round(luma));
  202. }
  203. void dsr::media_filter_sub(AlignedImageU8& targetImage, FixedPoint luma, AlignedImageU8 image) {
  204. media_filter_sub(targetImage, fixedPoint_round(luma), image);
  205. }
  206. void dsr::media_filter_mul(AlignedImageU8& targetImage, AlignedImageU8 image, FixedPoint luma) {
  207. assertExisting(image);
  208. removeIfShared(targetImage);
  209. allocateToSameSize(targetImage, image);
  210. // Reference implementation
  211. int64_t mantissa = luma.getMantissa();
  212. if (mantissa < 0) { mantissa = 0; } // At least zero, because negative clamps to zero
  213. if (mantissa > 16711680) { mantissa = 16711680; } // At most 255 whole integers, became more makes no difference
  214. SafePointer<uint8_t> targetRow = image_getSafePointer(targetImage);
  215. SafePointer<uint8_t> sourceRow = image_getSafePointer(image);
  216. int32_t targetStride = image_getStride(targetImage);
  217. int32_t sourceStride = image_getStride(image);
  218. for (int32_t y = 0; y < image_getHeight(targetImage); y++) {
  219. SafePointer<uint8_t> targetPixel = targetRow;
  220. SafePointer<uint8_t> sourcePixel = sourceRow;
  221. for (int32_t x = 0; x < image_getWidth(targetImage); x++) {
  222. int64_t result = ((int64_t)(*sourcePixel) * mantissa) / 65536;
  223. if (result < 0) { result = 0; }
  224. if (result > 255) { result = 255; }
  225. *targetPixel = result;
  226. targetPixel += 1;
  227. sourcePixel += 1;
  228. }
  229. targetRow.increaseBytes(targetStride);
  230. sourceRow.increaseBytes(sourceStride);
  231. }
  232. }
  233. void dsr::media_filter_mul(AlignedImageU8& targetImage, AlignedImageU8 imageA, AlignedImageU8 imageB, FixedPoint luma) {
  234. assertSameSize(imageA, imageB);
  235. removeIfShared(targetImage);
  236. allocateToSameSize(targetImage, imageA);
  237. // Reference implementation
  238. int64_t mantissa = luma.getMantissa();
  239. if (mantissa < 0) { mantissa = 0; } // At least zero, because negative clamps to zero
  240. if (mantissa > 16711680) { mantissa = 16711680; } // At most 255 whole integers, became more makes no difference
  241. SafePointer<uint8_t> targetRow = image_getSafePointer(targetImage);
  242. SafePointer<uint8_t> sourceRowA = image_getSafePointer(imageA);
  243. SafePointer<uint8_t> sourceRowB = image_getSafePointer(imageB);
  244. int32_t targetStride = image_getStride(targetImage);
  245. int32_t sourceStrideA = image_getStride(imageA);
  246. int32_t sourceStrideB = image_getStride(imageB);
  247. for (int32_t y = 0; y < image_getHeight(targetImage); y++) {
  248. SafePointer<uint8_t> targetPixel = targetRow;
  249. SafePointer<uint8_t> sourcePixelA = sourceRowA;
  250. SafePointer<uint8_t> sourcePixelB = sourceRowB;
  251. for (int32_t x = 0; x < image_getWidth(targetImage); x++) {
  252. int64_t result = (((uint64_t)*sourcePixelA) * ((uint64_t)*sourcePixelB) * mantissa) / 65536;
  253. if (result < 0) { result = 0; }
  254. if (result > 255) { result = 255; }
  255. *targetPixel = result;
  256. targetPixel += 1;
  257. sourcePixelA += 1;
  258. sourcePixelB += 1;
  259. }
  260. targetRow.increaseBytes(targetStride);
  261. sourceRowA.increaseBytes(sourceStrideA);
  262. sourceRowB.increaseBytes(sourceStrideB);
  263. }
  264. }
  265. void dsr::media_fade_region_linear(ImageU8& targetImage, const IRect& viewport, FixedPoint x1, FixedPoint y1, FixedPoint luma1, FixedPoint x2, FixedPoint y2, FixedPoint luma2) {
  266. assertExisting(targetImage);
  267. IRect safeBound = IRect::cut(viewport, image_getBound(targetImage));
  268. // Saturate luma in advance
  269. if (luma1 < 0) { luma1 = FixedPoint::zero(); }
  270. if (luma1 > 255) { luma1 = FixedPoint::fromWhole(255); }
  271. if (luma2 < 0) { luma2 = FixedPoint::zero(); }
  272. if (luma2 > 255) { luma2 = FixedPoint::fromWhole(255); }
  273. // Subtracting half a pixel in the fade line is equivalent to adding half a pixel on X and Y during sampling
  274. int64_t startX = x1.getMantissa() - 32768;
  275. int64_t startY = y1.getMantissa() - 32768;
  276. int64_t endX = x2.getMantissa() - 32768;
  277. int64_t endY = y2.getMantissa() - 32768;
  278. int64_t diffX = endX - startX; // x2 - x1 * 65536
  279. int64_t diffY = endY - startY; // y2 - y1 * 65536
  280. // You don't need to get the linear lengths nor distance.
  281. // By both generating a squared length and using a dot product, no square root is required.
  282. // This is because length(v)² = dot(v, v)
  283. int64_t squareLength = ((diffX * diffX) + (diffY * diffY)) / 65536; // length² * 65536
  284. // Limit to at least one pixel's length, both to get anti-aliasing and prevent overflow.
  285. if (squareLength < 65536) { squareLength = 65536; }
  286. // Calculate ratios for 3 pixels using dot products
  287. int64_t offsetX = -startX; // First pixel relative to x1
  288. int64_t offsetY = -startY; // First pixel relative to y1
  289. int64_t offsetX_right = 65536 - startX; // Right pixel relative to x1
  290. int64_t offsetY_down = 65536 - startY; // Down pixel relative to y1
  291. int64_t dotProduct = ((offsetX * diffX) + (offsetY * diffY)) / 65536; // dot(offset, diff) * 65536
  292. int64_t dotProduct_right = ((offsetX_right * diffX) + (offsetY * diffY)) / 65536; // dot(offsetRight, diff) * 65536
  293. int64_t dotProduct_down = ((offsetX * diffX) + (offsetY_down * diffY)) / 65536; // dot(offsetDown, diff) * 65536
  294. int64_t startRatio = (dotProduct * 65536 / squareLength); // The color mix ratio at the first pixel in a scale from 0 to 65536
  295. int64_t ratioDx = (dotProduct_right * 65536 / squareLength) - startRatio; // The color mix difference when going right
  296. int64_t ratioDy = (dotProduct_down * 65536 / squareLength) - startRatio; // The color mix difference when going down
  297. SafePointer<uint8_t> targetRow = image_getSafePointer(targetImage, safeBound.top()) + safeBound.left();
  298. int32_t targetStride = image_getStride(targetImage);
  299. if (ratioDx == 0) {
  300. if (ratioDy == 0) {
  301. // No direction at all. Fill the whole rectangle with luma1.
  302. draw_rectangle(targetImage, safeBound, fixedPoint_round(luma1));
  303. } else {
  304. // Up or down using memset per row.
  305. int32_t widthInBytes = safeBound.width();
  306. for (int32_t y = safeBound.top(); y < safeBound.bottom(); y++) {
  307. int64_t saturatedRatio = startRatio;
  308. if (saturatedRatio < 0) { saturatedRatio = 0; }
  309. if (saturatedRatio > 65536) { saturatedRatio = 65536; }
  310. int64_t mixedColor = ((luma1.getMantissa() * (65536 - saturatedRatio)) + (luma2.getMantissa() * saturatedRatio) + 2147483648ll) / 4294967296ll;
  311. if (mixedColor < 0) { mixedColor = 0; }
  312. if (mixedColor > 255) { mixedColor = 255; }
  313. safeMemorySet<uint8_t>(targetRow, mixedColor, widthInBytes);
  314. targetRow.increaseBytes(targetStride);
  315. startRatio += ratioDy;
  316. }
  317. }
  318. } else {
  319. if (ratioDy == 0) {
  320. // Left or right using memcpy per row.
  321. SafePointer<uint8_t> sourceRow = targetRow;
  322. SafePointer<uint8_t> targetPixel = targetRow;
  323. int64_t ratio = startRatio;
  324. int32_t widthInBytes = safeBound.width();
  325. // Evaluate the first line.
  326. for (int32_t x = viewport.left(); x < viewport.right(); x++) {
  327. int64_t saturatedRatio = ratio;
  328. if (saturatedRatio < 0) { saturatedRatio = 0; }
  329. if (saturatedRatio > 65536) { saturatedRatio = 65536; }
  330. int64_t mixedColor = ((luma1.getMantissa() * (65536 - saturatedRatio)) + (luma2.getMantissa() * saturatedRatio) + 2147483648ll) / 4294967296ll;
  331. if (mixedColor < 0) { mixedColor = 0; }
  332. if (mixedColor > 255) { mixedColor = 255; }
  333. *targetPixel = mixedColor;
  334. targetPixel += 1;
  335. ratio += ratioDx;
  336. }
  337. // Copy the rest from the first line.
  338. for (int32_t y = viewport.top() + 1; y < viewport.bottom(); y++) {
  339. targetRow.increaseBytes(targetStride);
  340. safeMemoryCopy<uint8_t>(targetRow, sourceRow, widthInBytes);
  341. }
  342. } else {
  343. // Each pixel needs to be evaluated in this fade.
  344. for (int32_t y = viewport.top(); y < viewport.bottom(); y++) {
  345. SafePointer<uint8_t> targetPixel = targetRow;
  346. for (int32_t x = viewport.left(); x < viewport.right(); x++) {
  347. int64_t saturatedRatio = startRatio;
  348. if (saturatedRatio < 0) { saturatedRatio = 0; }
  349. if (saturatedRatio > 65536) { saturatedRatio = 65536; }
  350. int64_t mixedColor = ((luma1.getMantissa() * (65536 - saturatedRatio)) + (luma2.getMantissa() * saturatedRatio) + 2147483648ll) / 4294967296ll;
  351. if (mixedColor < 0) { mixedColor = 0; }
  352. if (mixedColor > 255) { mixedColor = 255; }
  353. *targetPixel = mixedColor;
  354. targetPixel += 1;
  355. }
  356. targetRow.increaseBytes(targetStride);
  357. startRatio += ratioDy;
  358. }
  359. }
  360. }
  361. }
  362. void dsr::media_fade_linear(ImageU8& targetImage, FixedPoint x1, FixedPoint y1, FixedPoint luma1, FixedPoint x2, FixedPoint y2, FixedPoint luma2) {
  363. media_fade_region_linear(targetImage, image_getBound(targetImage), x1, y1, luma1, x2, y2, luma2);
  364. }
  365. void dsr::media_fade_region_radial(ImageU8& targetImage, const IRect& viewport, FixedPoint centerX, FixedPoint centerY, FixedPoint innerRadius, FixedPoint innerLuma, FixedPoint outerRadius, FixedPoint outerLuma) {
  366. assertExisting(targetImage);
  367. IRect safeBound = IRect::cut(viewport, image_getBound(targetImage));
  368. if (innerLuma < 0) { innerLuma = FixedPoint::zero(); }
  369. if (innerLuma > 255) { innerLuma = FixedPoint::fromWhole(255); }
  370. if (outerLuma < 0) { outerLuma = FixedPoint::zero(); }
  371. if (outerLuma > 255) { outerLuma = FixedPoint::fromWhole(255); }
  372. // Subtracting half a pixel in the fade line is equivalent to adding half a pixel on X and Y.
  373. int64_t originX = centerX.getMantissa() + safeBound.left() * 65536 - 32768;
  374. int64_t originY = centerY.getMantissa() + safeBound.top() * 65536 - 32768;
  375. // Let outerRadius be slightly outside of innerRadius to prevent division by zero and get anti-aliasing.
  376. if (outerRadius <= innerRadius + FixedPoint::one()) {
  377. outerRadius = innerRadius + FixedPoint::one();
  378. }
  379. int64_t fadeSize = (outerRadius.getMantissa() - innerRadius.getMantissa());
  380. int64_t fadeSlope = 4294967296ll / fadeSize;
  381. SafePointer<uint8_t> targetRow = image_getSafePointer(targetImage, safeBound.top()) + safeBound.left();
  382. int32_t targetStride = image_getStride(targetImage);
  383. for (int64_t y = safeBound.top(); y < safeBound.bottom(); y++) {
  384. SafePointer<uint8_t> targetPixel = targetRow;
  385. for (int64_t x = safeBound.left(); x < safeBound.right(); x++) {
  386. int64_t diffX = (x * 65536) - originX;
  387. int64_t diffY = (y * 65536) - originY;
  388. // Double's square root is guaranteed to be exact for integers fitting inside of its mantissa.
  389. int64_t length = sqrt(((diffX * diffX) + (diffY * diffY)));
  390. // Using a 64-bit integer division per pixel for good quality and high range.
  391. int64_t ratio = ((length - innerRadius.getMantissa()) * fadeSlope) / 65536;
  392. int64_t saturatedRatio = ratio;
  393. if (saturatedRatio < 0) { saturatedRatio = 0; }
  394. if (saturatedRatio > 65536) { saturatedRatio = 65536; }
  395. int64_t mixedColor = ((innerLuma.getMantissa() * (65536 - saturatedRatio)) + (outerLuma.getMantissa() * saturatedRatio) + 2147483648ll) / 4294967296ll;
  396. if (mixedColor < 0) { mixedColor = 0; }
  397. if (mixedColor > 255) { mixedColor = 255; }
  398. *targetPixel = mixedColor;
  399. targetPixel += 1;
  400. }
  401. targetRow.increaseBytes(targetStride);
  402. }
  403. }
  404. void dsr::media_fade_radial(ImageU8& targetImage, FixedPoint centerX, FixedPoint centerY, FixedPoint innerRadius, FixedPoint innerLuma, FixedPoint outerRadius, FixedPoint outerLuma) {
  405. media_fade_region_radial(targetImage, image_getBound(targetImage), centerX, centerY, innerRadius, innerLuma, outerRadius, outerLuma);
  406. }