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 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. ImageConvolutionKernel::ImageConvolutionKernel (const int size_)
  18. : values ((size_t) (size_ * size_)),
  19. size (size_)
  20. {
  21. clear();
  22. }
  23. ImageConvolutionKernel::~ImageConvolutionKernel()
  24. {
  25. }
  26. //==============================================================================
  27. float ImageConvolutionKernel::getKernelValue (const int x, const int y) const noexcept
  28. {
  29. if (isPositiveAndBelow (x, size) && isPositiveAndBelow (y, size))
  30. return values [x + y * size];
  31. jassertfalse;
  32. return 0;
  33. }
  34. void ImageConvolutionKernel::setKernelValue (const int x, const int y, const float value) noexcept
  35. {
  36. if (isPositiveAndBelow (x, size) && isPositiveAndBelow (y, size))
  37. {
  38. values [x + y * size] = value;
  39. }
  40. else
  41. {
  42. jassertfalse;
  43. }
  44. }
  45. void ImageConvolutionKernel::clear()
  46. {
  47. for (int i = size * size; --i >= 0;)
  48. values[i] = 0;
  49. }
  50. void ImageConvolutionKernel::setOverallSum (const float desiredTotalSum)
  51. {
  52. double currentTotal = 0.0;
  53. for (int i = size * size; --i >= 0;)
  54. currentTotal += values[i];
  55. rescaleAllValues ((float) (desiredTotalSum / currentTotal));
  56. }
  57. void ImageConvolutionKernel::rescaleAllValues (const float multiplier)
  58. {
  59. for (int i = size * size; --i >= 0;)
  60. values[i] *= multiplier;
  61. }
  62. //==============================================================================
  63. void ImageConvolutionKernel::createGaussianBlur (const float radius)
  64. {
  65. const double radiusFactor = -1.0 / (radius * radius * 2);
  66. const int centre = size >> 1;
  67. for (int y = size; --y >= 0;)
  68. {
  69. for (int x = size; --x >= 0;)
  70. {
  71. const int cx = x - centre;
  72. const int cy = y - centre;
  73. values [x + y * size] = (float) exp (radiusFactor * (cx * cx + cy * cy));
  74. }
  75. }
  76. setOverallSum (1.0f);
  77. }
  78. //==============================================================================
  79. void ImageConvolutionKernel::applyToImage (Image& destImage,
  80. const Image& sourceImage,
  81. const Rectangle<int>& destinationArea) const
  82. {
  83. if (sourceImage == destImage)
  84. {
  85. destImage.duplicateIfShared();
  86. }
  87. else
  88. {
  89. if (sourceImage.getWidth() != destImage.getWidth()
  90. || sourceImage.getHeight() != destImage.getHeight()
  91. || sourceImage.getFormat() != destImage.getFormat())
  92. {
  93. jassertfalse;
  94. return;
  95. }
  96. }
  97. const Rectangle<int> area (destinationArea.getIntersection (destImage.getBounds()));
  98. if (area.isEmpty())
  99. return;
  100. const int right = area.getRight();
  101. const int bottom = area.getBottom();
  102. const Image::BitmapData destData (destImage, area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  103. Image::BitmapData::writeOnly);
  104. uint8* line = destData.data;
  105. const Image::BitmapData srcData (sourceImage, Image::BitmapData::readOnly);
  106. if (destData.pixelStride == 4)
  107. {
  108. for (int y = area.getY(); y < bottom; ++y)
  109. {
  110. uint8* dest = line;
  111. line += destData.lineStride;
  112. for (int x = area.getX(); x < right; ++x)
  113. {
  114. float c1 = 0;
  115. float c2 = 0;
  116. float c3 = 0;
  117. float c4 = 0;
  118. for (int yy = 0; yy < size; ++yy)
  119. {
  120. const int sy = y + yy - (size >> 1);
  121. if (sy >= srcData.height)
  122. break;
  123. if (sy >= 0)
  124. {
  125. int sx = x - (size >> 1);
  126. const uint8* src = srcData.getPixelPointer (sx, sy);
  127. for (int xx = 0; xx < size; ++xx)
  128. {
  129. if (sx >= srcData.width)
  130. break;
  131. if (sx >= 0)
  132. {
  133. const float kernelMult = values [xx + yy * size];
  134. c1 += kernelMult * *src++;
  135. c2 += kernelMult * *src++;
  136. c3 += kernelMult * *src++;
  137. c4 += kernelMult * *src++;
  138. }
  139. else
  140. {
  141. src += 4;
  142. }
  143. ++sx;
  144. }
  145. }
  146. }
  147. *dest++ = (uint8) jmin (0xff, roundToInt (c1));
  148. *dest++ = (uint8) jmin (0xff, roundToInt (c2));
  149. *dest++ = (uint8) jmin (0xff, roundToInt (c3));
  150. *dest++ = (uint8) jmin (0xff, roundToInt (c4));
  151. }
  152. }
  153. }
  154. else if (destData.pixelStride == 3)
  155. {
  156. for (int y = area.getY(); y < bottom; ++y)
  157. {
  158. uint8* dest = line;
  159. line += destData.lineStride;
  160. for (int x = area.getX(); x < right; ++x)
  161. {
  162. float c1 = 0;
  163. float c2 = 0;
  164. float c3 = 0;
  165. for (int yy = 0; yy < size; ++yy)
  166. {
  167. const int sy = y + yy - (size >> 1);
  168. if (sy >= srcData.height)
  169. break;
  170. if (sy >= 0)
  171. {
  172. int sx = x - (size >> 1);
  173. const uint8* src = srcData.getPixelPointer (sx, sy);
  174. for (int xx = 0; xx < size; ++xx)
  175. {
  176. if (sx >= srcData.width)
  177. break;
  178. if (sx >= 0)
  179. {
  180. const float kernelMult = values [xx + yy * size];
  181. c1 += kernelMult * *src++;
  182. c2 += kernelMult * *src++;
  183. c3 += kernelMult * *src++;
  184. }
  185. else
  186. {
  187. src += 3;
  188. }
  189. ++sx;
  190. }
  191. }
  192. }
  193. *dest++ = (uint8) roundToInt (c1);
  194. *dest++ = (uint8) roundToInt (c2);
  195. *dest++ = (uint8) roundToInt (c3);
  196. }
  197. }
  198. }
  199. else if (destData.pixelStride == 1)
  200. {
  201. for (int y = area.getY(); y < bottom; ++y)
  202. {
  203. uint8* dest = line;
  204. line += destData.lineStride;
  205. for (int x = area.getX(); x < right; ++x)
  206. {
  207. float c1 = 0;
  208. for (int yy = 0; yy < size; ++yy)
  209. {
  210. const int sy = y + yy - (size >> 1);
  211. if (sy >= srcData.height)
  212. break;
  213. if (sy >= 0)
  214. {
  215. int sx = x - (size >> 1);
  216. const uint8* src = srcData.getPixelPointer (sx, sy);
  217. for (int xx = 0; xx < size; ++xx)
  218. {
  219. if (sx >= srcData.width)
  220. break;
  221. if (sx >= 0)
  222. {
  223. const float kernelMult = values [xx + yy * size];
  224. c1 += kernelMult * *src++;
  225. }
  226. else
  227. {
  228. src += 3;
  229. }
  230. ++sx;
  231. }
  232. }
  233. }
  234. *dest++ = (uint8) roundToInt (c1);
  235. }
  236. }
  237. }
  238. }