FIR-Weights.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <math.h>
  9. #include "FIR-Weights.h"
  10. #include <AzCore/Debug/Trace.h>
  11. /* ####################################################################################################################
  12. */
  13. namespace ImageProcessingAtom
  14. {
  15. float round(float x)
  16. {
  17. return ((x) >= 0.f) ? floor((x) + 0.5f) : ceil((x) - 0.5f);
  18. }
  19. void calculateFilterRange(unsigned int srcFactor, int& srcFirst, int& srcLast,
  20. unsigned int dstFactor, int dstFirst, int dstLast,
  21. double blurFactor, class IWindowFunction<double>* windowFunction)
  22. {
  23. double s, t, u, scaleFactor; /* scale factors */
  24. double srcRadius, srcCenter; /* window position and size */
  25. #define s0 0
  26. #define s1 srcFactor
  27. #define d0 0
  28. #define d1 dstFactor
  29. /* the mapping from discrete destination coordinates to continuous source coordinates: */
  30. #define MAP(b, scaleFactor, offset) ((b) + (offset)) / (scaleFactor)
  31. /* relation of dstFactor to srcFactor */
  32. s = (double)dstFactor / srcFactor;
  33. t = d0 - s * (s0 - 0.5) - 0.5;
  34. /* compute offsets for MAP */
  35. u = d0 - s * (s0 - 0.5) - t;
  36. /* find scale of filter
  37. * when minifying, scaleFactor = 1/s, but when magnifying, scaleFactor = 1
  38. */
  39. scaleFactor = (blurFactor == 0.0 ? 1.0 : (blurFactor > 0.0 ? (1.0 + blurFactor) : 1.0 / (1.0 - blurFactor))) * maximum(1., 1. / s);
  40. /* find support radius of scaled filter
  41. * if the window's length is <= 0.5 then we've got point sampling.
  42. */
  43. srcRadius = maximum(0.5, scaleFactor * windowFunction->getLength());
  44. /* sample the continuous filter, scaled by scaleFactor and
  45. * positioned at continuous source coordinate srcCenter
  46. */
  47. {
  48. srcCenter = MAP(dstFirst + 0, s, u);
  49. /* find the source coordinate range of this positioned filter window */
  50. srcFirst = int(floor(srcCenter - srcRadius + 0.5));
  51. }
  52. {
  53. srcCenter = MAP(dstLast - 1, s, u);
  54. /* find the source coordinate range of this positioned filter window */
  55. srcLast = int(floor(srcCenter + srcRadius + 0.5));
  56. }
  57. }
  58. template<>
  59. FilterWeights<signed short int>* calculateFilterWeights<signed short int>(unsigned int srcFactor, int srcFirst, int srcLast,
  60. unsigned int dstFactor, int dstFirst, int dstLast, signed short int numRepetitions,
  61. double blurFactor, class IWindowFunction<double>* windowFunction,
  62. bool peaknorm, bool& plusminus)
  63. {
  64. #define WEIGHTBITS 15
  65. #define WEIGHTONE (1 << WEIGHTBITS) /* filter weight of one */
  66. double s, t, u, scaleFactor; /* scale factors */
  67. double srcRadius, srcCenter; /* window position and size */
  68. double sumfWeights, neg, pos, nrmWeights, fWeight; /* window position and size */
  69. int i, i0, i1; /* window position and size */
  70. int dstPosition;
  71. signed short int n;
  72. bool trimZeros = true, stillzero;
  73. int lastnonzero = 0, hWeight, highest = 0;
  74. signed int sumiWeights, iWeight;
  75. signed short int* weightsPtr;
  76. signed short int* weightsMem;
  77. FilterWeights<signed short int>* weightsObjs;
  78. bool pm, pma = false;
  79. /* pre-calculate filter window solutions for all rows
  80. */
  81. weightsObjs = new FilterWeights<signed short int>[dstLast - dstFirst];
  82. #define s0 0
  83. #define s1 srcFactor
  84. #define d0 0
  85. #define d1 dstFactor
  86. /* relation of dstFactor to srcFactor */
  87. s = (double)dstFactor / srcFactor;
  88. t = d0 - s * (s0 - 0.5) - 0.5;
  89. /* compute offsets for MAP */
  90. u = d0 - s * (s0 - 0.5) - t;
  91. /* find scale of filter
  92. * when minifying, scaleFactor = 1/s, but when magnifying, scaleFactor = 1
  93. */
  94. scaleFactor = (blurFactor == 0.0 ? 1.0 : (blurFactor > 0.0 ? (1.0 + blurFactor) : 1.0 / (1.0 - blurFactor))) * maximum(1., 1. / s);
  95. /* find support radius of scaled filter
  96. * if the window's length is <= 0.5 then we've got point sampling.
  97. */
  98. srcRadius = maximum(0.5, scaleFactor * windowFunction->getLength());
  99. /* sample the continuous filter, scaled by ap->scaleFactor and
  100. * positioned at continuous source coordinate srcCenter, for source coordinates in
  101. * the range [0..len-1], writing the weights into wtab.
  102. * Scale the weights so they sum up to WEIGHTONE, and trim leading and trailing
  103. * zeros if trimZeros is true.
  104. */
  105. #undef NORMALIZE_SUMMED_PEAK
  106. #define NORMALIZE_MAXXED_PEAK
  107. for (dstPosition = dstFirst, pm = false; dstPosition < dstLast; dstPosition++)
  108. {
  109. srcCenter = MAP(dstPosition, s, u);
  110. /* find the source coordinate range of this positioned filter window */
  111. i0 = int(floor(srcCenter - srcRadius + 0.5));
  112. i1 = int(floor(srcCenter + srcRadius + 0.5));
  113. /* clip against the source-range */
  114. if (i0 < srcFirst)
  115. {
  116. i0 = srcFirst;
  117. }
  118. if (i1 > srcLast)
  119. {
  120. i1 = srcLast;
  121. }
  122. /* this is possible if we hit the final line */
  123. if (i1 <= i0)
  124. {
  125. if (i1 >= srcLast)
  126. {
  127. i0 = i1 - 1;
  128. }
  129. else
  130. {
  131. i1 = i0 + 1;
  132. }
  133. }
  134. AZ_Assert(i0 >= srcFirst, "%s: Invalid source coordinate range!", __FUNCTION__);
  135. AZ_Assert(i1 <= srcLast, "%s: Invalid source coordinate range!", __FUNCTION__);
  136. AZ_Assert(i0 < i1, "%s: Invalid source coordinate range!", __FUNCTION__);
  137. /* find maximum peak to normalize the filter */
  138. for (sumfWeights = 0, pos = 0, neg = 0, i = i0; i < i1; i++)
  139. {
  140. /* evaluate the filter function: */
  141. fWeight = (*windowFunction)((i + 0.5 - srcCenter) / scaleFactor);
  142. #if defined(NORMALIZE_SUMMED_PEAK)
  143. /* get positive and negative summed peaks */
  144. if (fWeight >= 0)
  145. {
  146. pos += fWeight;
  147. }
  148. else
  149. {
  150. neg += fWeight;
  151. }
  152. #elif defined(NORMALIZE_MAXXED_PEAK)
  153. /* get positive and negative maximum peaks */
  154. minmax(fWeight, neg, pos);
  155. #endif
  156. sumfWeights += fWeight;
  157. }
  158. /* the range of source samples to buffer: */
  159. weightsMem = new signed short int[(i1 - i0) * abs(numRepetitions)];
  160. /* set nrmWeights so that sumWeights of windowFunction() is approximately WEIGHTONE
  161. * this needs to be adjusted because the maximum weight-coefficient
  162. * is NOT allowed to leave [-32768,32767]
  163. * a case like {+1.25,-0.25} does produce a sumWeights of 1.0 BUT
  164. * produced a weight much too high (-40000)
  165. */
  166. #if defined(NORMALIZE_SUMMED_PEAK)
  167. sumfWeights = maximum(-neg, pos);
  168. #elif defined(NORMALIZE_MAXXED_PEAK)
  169. sumfWeights = maximum(sumfWeights, maximum(-neg, pos));
  170. #endif
  171. if (!peaknorm)
  172. {
  173. nrmWeights = (sumfWeights == 0. ? WEIGHTONE : (-neg > pos ? WEIGHTONE - 1 : WEIGHTONE) / sumfWeights);
  174. }
  175. else
  176. {
  177. nrmWeights = (sumfWeights == 0. ? WEIGHTONE : (-neg > pos ? WEIGHTONE - 1 : WEIGHTONE) / maximum(-neg, pos));
  178. }
  179. /* compute the discrete, sampled filter coefficients */
  180. stillzero = trimZeros;
  181. for (sumiWeights = 0, hWeight = -WEIGHTONE, weightsPtr = weightsMem, i = i0; i < i1; i++)
  182. {
  183. /* evaluate the filter function: */
  184. fWeight = (*windowFunction)((i + 0.5 - srcCenter) / scaleFactor);
  185. /* normalize against the peak sumWeights, because the sums are not allowed to leave -32768/32767 */
  186. fWeight = fWeight * nrmWeights;
  187. iWeight = int(round(static_cast<float>(fWeight)));
  188. /* find first nonzero */
  189. if (stillzero && (iWeight == 0))
  190. {
  191. i0++;
  192. }
  193. else
  194. {
  195. AZ_Assert((-fWeight >= -32768.5) && (-fWeight <= 32767.5), "%s:The weight exceeded the maximum weight-coefficient.", __FUNCTION__);
  196. if (!peaknorm)
  197. {
  198. sumiWeights += iWeight;
  199. }
  200. else
  201. {
  202. sumiWeights = maximum(sumiWeights, iWeight);
  203. }
  204. #define sgnextend(n, iWeight) (n & 1 ? (iWeight < 0 ? -1 : 0) : iWeight)
  205. if (numRepetitions < 0)
  206. {
  207. /* add weight to table, interleaved sign */
  208. for (n = 0; n < -numRepetitions; n++)
  209. {
  210. *weightsPtr++ = static_cast<signed short int>(sgnextend(n, -iWeight));
  211. }
  212. }
  213. else
  214. {
  215. /* add weight to table */
  216. for (n = 0; n < numRepetitions; n++)
  217. {
  218. *weightsPtr++ = static_cast<signed short int>(-iWeight);
  219. }
  220. }
  221. stillzero = false;
  222. /* find last nonzero */
  223. if (iWeight != 0)
  224. {
  225. lastnonzero = i;
  226. }
  227. /* check for negative values */
  228. if (iWeight < 0)
  229. {
  230. pm = pma = true;
  231. }
  232. /* find most influential value */
  233. if (iWeight >= hWeight)
  234. {
  235. highest = i;
  236. hWeight = iWeight;
  237. }
  238. }
  239. }
  240. if (sumiWeights == 0)
  241. {
  242. i0 = (i0 + i1) >> 1;
  243. i1 = (i0 + 1);
  244. for (n = 0, weightsPtr = weightsMem; n < numRepetitions; n++)
  245. {
  246. *weightsPtr++ = -WEIGHTONE;
  247. }
  248. }
  249. else
  250. {
  251. /* skip leading and trailing zeros */
  252. if (trimZeros)
  253. {
  254. /* set i1 to the nonzero support of the filter */
  255. i1 = lastnonzero + 1;
  256. }
  257. if (sumiWeights != WEIGHTONE)
  258. {
  259. /* Fudge with the highest value */
  260. i = highest;
  261. /* fudge srcCenter sample */
  262. iWeight = WEIGHTONE - sumiWeights;
  263. for (n = 0, weightsPtr = weightsMem + (i - i0) * numRepetitions; n < numRepetitions; n++)
  264. {
  265. *weightsPtr++ -= static_cast<signed short int>(iWeight);
  266. }
  267. }
  268. }
  269. /* the new adjusted range of source samples to buffer: */
  270. weightsObjs[dstPosition].first = i0;
  271. weightsObjs[dstPosition].last = i1;
  272. weightsObjs[dstPosition].hasNegativeWeights = pm;
  273. weightsObjs[dstPosition].weights = weightsMem;
  274. }
  275. plusminus = pma;
  276. return weightsObjs;
  277. }
  278. }