The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

250 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. ImageConvolutionKernel::ImageConvolutionKernel (const int size_)
  21. : values ((size_t) (size_ * size_)),
  22. size (size_)
  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. const int cx = x - centre;
  75. const int cy = y - centre;
  76. values [x + y * size] = (float) 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. const Rectangle<int> area (destinationArea.getIntersection (destImage.getBounds()));
  101. if (area.isEmpty())
  102. return;
  103. const int right = area.getRight();
  104. const int 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. }
  203. END_JUCE_NAMESPACE