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.

290 lines
8.7KB

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