Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_ImageConvolutionKernel.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. ImageConvolutionKernel::ImageConvolutionKernel (int sizeToUse)
  21. : values ((size_t) (sizeToUse * sizeToUse)),
  22. size (sizeToUse)
  23. {
  24. clear();
  25. }
  26. ImageConvolutionKernel::~ImageConvolutionKernel()
  27. {
  28. }
  29. //==============================================================================
  30. float ImageConvolutionKernel::getKernelValue (const int x, const int y) const noexcept
  31. {
  32. if (isPositiveAndBelow (x, size) && isPositiveAndBelow (y, size))
  33. return values [x + y * size];
  34. jassertfalse;
  35. return 0;
  36. }
  37. void ImageConvolutionKernel::setKernelValue (const int x, const int y, const float value) noexcept
  38. {
  39. if (isPositiveAndBelow (x, size) && isPositiveAndBelow (y, size))
  40. {
  41. values [x + y * size] = value;
  42. }
  43. else
  44. {
  45. jassertfalse;
  46. }
  47. }
  48. void ImageConvolutionKernel::clear()
  49. {
  50. for (int i = size * size; --i >= 0;)
  51. values[i] = 0;
  52. }
  53. void ImageConvolutionKernel::setOverallSum (const float desiredTotalSum)
  54. {
  55. double currentTotal = 0.0;
  56. for (int i = size * size; --i >= 0;)
  57. currentTotal += values[i];
  58. rescaleAllValues ((float) (desiredTotalSum / currentTotal));
  59. }
  60. void ImageConvolutionKernel::rescaleAllValues (const float multiplier)
  61. {
  62. for (int i = size * size; --i >= 0;)
  63. values[i] *= multiplier;
  64. }
  65. //==============================================================================
  66. void ImageConvolutionKernel::createGaussianBlur (const float radius)
  67. {
  68. const double radiusFactor = -1.0 / (radius * radius * 2);
  69. const int centre = size >> 1;
  70. for (int y = size; --y >= 0;)
  71. {
  72. for (int x = size; --x >= 0;)
  73. {
  74. auto cx = x - centre;
  75. auto cy = y - centre;
  76. values [x + y * size] = (float) std::exp (radiusFactor * (cx * cx + cy * cy));
  77. }
  78. }
  79. setOverallSum (1.0f);
  80. }
  81. //==============================================================================
  82. void ImageConvolutionKernel::applyToImage (Image& destImage,
  83. const Image& sourceImage,
  84. const Rectangle<int>& destinationArea) const
  85. {
  86. if (sourceImage == destImage)
  87. {
  88. destImage.duplicateIfShared();
  89. }
  90. else
  91. {
  92. if (sourceImage.getWidth() != destImage.getWidth()
  93. || sourceImage.getHeight() != destImage.getHeight()
  94. || sourceImage.getFormat() != destImage.getFormat())
  95. {
  96. jassertfalse;
  97. return;
  98. }
  99. }
  100. auto area = destinationArea.getIntersection (destImage.getBounds());
  101. if (area.isEmpty())
  102. return;
  103. auto right = area.getRight();
  104. auto bottom = area.getBottom();
  105. const Image::BitmapData destData (destImage, area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  106. Image::BitmapData::writeOnly);
  107. uint8* line = destData.data;
  108. const Image::BitmapData srcData (sourceImage, Image::BitmapData::readOnly);
  109. if (destData.pixelStride == 4)
  110. {
  111. for (int y = area.getY(); y < bottom; ++y)
  112. {
  113. uint8* dest = line;
  114. line += destData.lineStride;
  115. for (int x = area.getX(); x < right; ++x)
  116. {
  117. float c1 = 0;
  118. float c2 = 0;
  119. float c3 = 0;
  120. float c4 = 0;
  121. for (int yy = 0; yy < size; ++yy)
  122. {
  123. const int sy = y + yy - (size >> 1);
  124. if (sy >= srcData.height)
  125. break;
  126. if (sy >= 0)
  127. {
  128. int sx = x - (size >> 1);
  129. const uint8* src = srcData.getPixelPointer (sx, sy);
  130. for (int xx = 0; xx < size; ++xx)
  131. {
  132. if (sx >= srcData.width)
  133. break;
  134. if (sx >= 0)
  135. {
  136. const float kernelMult = values [xx + yy * size];
  137. c1 += kernelMult * *src++;
  138. c2 += kernelMult * *src++;
  139. c3 += kernelMult * *src++;
  140. c4 += kernelMult * *src++;
  141. }
  142. else
  143. {
  144. src += 4;
  145. }
  146. ++sx;
  147. }
  148. }
  149. }
  150. *dest++ = (uint8) jmin (0xff, roundToInt (c1));
  151. *dest++ = (uint8) jmin (0xff, roundToInt (c2));
  152. *dest++ = (uint8) jmin (0xff, roundToInt (c3));
  153. *dest++ = (uint8) jmin (0xff, roundToInt (c4));
  154. }
  155. }
  156. }
  157. else if (destData.pixelStride == 3)
  158. {
  159. for (int y = area.getY(); y < bottom; ++y)
  160. {
  161. uint8* dest = line;
  162. line += destData.lineStride;
  163. for (int x = area.getX(); x < right; ++x)
  164. {
  165. float c1 = 0;
  166. float c2 = 0;
  167. float c3 = 0;
  168. for (int yy = 0; yy < size; ++yy)
  169. {
  170. const int sy = y + yy - (size >> 1);
  171. if (sy >= srcData.height)
  172. break;
  173. if (sy >= 0)
  174. {
  175. int sx = x - (size >> 1);
  176. const uint8* src = srcData.getPixelPointer (sx, sy);
  177. for (int xx = 0; xx < size; ++xx)
  178. {
  179. if (sx >= srcData.width)
  180. break;
  181. if (sx >= 0)
  182. {
  183. const float kernelMult = values [xx + yy * size];
  184. c1 += kernelMult * *src++;
  185. c2 += kernelMult * *src++;
  186. c3 += kernelMult * *src++;
  187. }
  188. else
  189. {
  190. src += 3;
  191. }
  192. ++sx;
  193. }
  194. }
  195. }
  196. *dest++ = (uint8) roundToInt (c1);
  197. *dest++ = (uint8) roundToInt (c2);
  198. *dest++ = (uint8) roundToInt (c3);
  199. }
  200. }
  201. }
  202. else if (destData.pixelStride == 1)
  203. {
  204. for (int y = area.getY(); y < bottom; ++y)
  205. {
  206. uint8* dest = line;
  207. line += destData.lineStride;
  208. for (int x = area.getX(); x < right; ++x)
  209. {
  210. float c1 = 0;
  211. for (int yy = 0; yy < size; ++yy)
  212. {
  213. const int sy = y + yy - (size >> 1);
  214. if (sy >= srcData.height)
  215. break;
  216. if (sy >= 0)
  217. {
  218. int sx = x - (size >> 1);
  219. const uint8* src = srcData.getPixelPointer (sx, sy);
  220. for (int xx = 0; xx < size; ++xx)
  221. {
  222. if (sx >= srcData.width)
  223. break;
  224. if (sx >= 0)
  225. {
  226. const float kernelMult = values [xx + yy * size];
  227. c1 += kernelMult * *src++;
  228. }
  229. else
  230. {
  231. src += 3;
  232. }
  233. ++sx;
  234. }
  235. }
  236. }
  237. *dest++ = (uint8) roundToInt (c1);
  238. }
  239. }
  240. }
  241. }
  242. } // namespace juce