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.

245 lines
7.7KB

  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. ImageConvolutionKernel::ImageConvolutionKernel (const int size_)
  19. : values ((size_t) (size_ * size_)),
  20. size (size_)
  21. {
  22. clear();
  23. }
  24. ImageConvolutionKernel::~ImageConvolutionKernel()
  25. {
  26. }
  27. //==============================================================================
  28. float ImageConvolutionKernel::getKernelValue (const int x, const int y) const noexcept
  29. {
  30. if (isPositiveAndBelow (x, size) && isPositiveAndBelow (y, size))
  31. return values [x + y * size];
  32. jassertfalse;
  33. return 0;
  34. }
  35. void ImageConvolutionKernel::setKernelValue (const int x, const int y, const float value) noexcept
  36. {
  37. if (isPositiveAndBelow (x, size) && isPositiveAndBelow (y, size))
  38. {
  39. values [x + y * size] = value;
  40. }
  41. else
  42. {
  43. jassertfalse;
  44. }
  45. }
  46. void ImageConvolutionKernel::clear()
  47. {
  48. for (int i = size * size; --i >= 0;)
  49. values[i] = 0;
  50. }
  51. void ImageConvolutionKernel::setOverallSum (const float desiredTotalSum)
  52. {
  53. double currentTotal = 0.0;
  54. for (int i = size * size; --i >= 0;)
  55. currentTotal += values[i];
  56. rescaleAllValues ((float) (desiredTotalSum / currentTotal));
  57. }
  58. void ImageConvolutionKernel::rescaleAllValues (const float multiplier)
  59. {
  60. for (int i = size * size; --i >= 0;)
  61. values[i] *= multiplier;
  62. }
  63. //==============================================================================
  64. void ImageConvolutionKernel::createGaussianBlur (const float radius)
  65. {
  66. const double radiusFactor = -1.0 / (radius * radius * 2);
  67. const int centre = size >> 1;
  68. for (int y = size; --y >= 0;)
  69. {
  70. for (int x = size; --x >= 0;)
  71. {
  72. const int cx = x - centre;
  73. const int cy = y - centre;
  74. values [x + y * size] = (float) exp (radiusFactor * (cx * cx + cy * cy));
  75. }
  76. }
  77. setOverallSum (1.0f);
  78. }
  79. //==============================================================================
  80. void ImageConvolutionKernel::applyToImage (Image& destImage,
  81. const Image& sourceImage,
  82. const Rectangle<int>& destinationArea) const
  83. {
  84. if (sourceImage == destImage)
  85. {
  86. destImage.duplicateIfShared();
  87. }
  88. else
  89. {
  90. if (sourceImage.getWidth() != destImage.getWidth()
  91. || sourceImage.getHeight() != destImage.getHeight()
  92. || sourceImage.getFormat() != destImage.getFormat())
  93. {
  94. jassertfalse;
  95. return;
  96. }
  97. }
  98. const Rectangle<int> area (destinationArea.getIntersection (destImage.getBounds()));
  99. if (area.isEmpty())
  100. return;
  101. const int right = area.getRight();
  102. const int bottom = area.getBottom();
  103. const Image::BitmapData destData (destImage, area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  104. Image::BitmapData::writeOnly);
  105. uint8* line = destData.data;
  106. const Image::BitmapData srcData (sourceImage, Image::BitmapData::readOnly);
  107. if (destData.pixelStride == 4)
  108. {
  109. for (int y = area.getY(); y < bottom; ++y)
  110. {
  111. uint8* dest = line;
  112. line += destData.lineStride;
  113. for (int x = area.getX(); x < right; ++x)
  114. {
  115. float c1 = 0;
  116. float c2 = 0;
  117. float c3 = 0;
  118. float c4 = 0;
  119. for (int yy = 0; yy < size; ++yy)
  120. {
  121. const int sy = y + yy - (size >> 1);
  122. if (sy >= srcData.height)
  123. break;
  124. if (sy >= 0)
  125. {
  126. int sx = x - (size >> 1);
  127. const uint8* src = srcData.getPixelPointer (sx, sy);
  128. for (int xx = 0; xx < size; ++xx)
  129. {
  130. if (sx >= srcData.width)
  131. break;
  132. if (sx >= 0)
  133. {
  134. const float kernelMult = values [xx + yy * size];
  135. c1 += kernelMult * *src++;
  136. c2 += kernelMult * *src++;
  137. c3 += kernelMult * *src++;
  138. c4 += kernelMult * *src++;
  139. }
  140. else
  141. {
  142. src += 4;
  143. }
  144. ++sx;
  145. }
  146. }
  147. }
  148. *dest++ = (uint8) jmin (0xff, roundToInt (c1));
  149. *dest++ = (uint8) jmin (0xff, roundToInt (c2));
  150. *dest++ = (uint8) jmin (0xff, roundToInt (c3));
  151. *dest++ = (uint8) jmin (0xff, roundToInt (c4));
  152. }
  153. }
  154. }
  155. else if (destData.pixelStride == 3)
  156. {
  157. for (int y = area.getY(); y < bottom; ++y)
  158. {
  159. uint8* dest = line;
  160. line += destData.lineStride;
  161. for (int x = area.getX(); x < right; ++x)
  162. {
  163. float c1 = 0;
  164. float c2 = 0;
  165. float c3 = 0;
  166. for (int yy = 0; yy < size; ++yy)
  167. {
  168. const int sy = y + yy - (size >> 1);
  169. if (sy >= srcData.height)
  170. break;
  171. if (sy >= 0)
  172. {
  173. int sx = x - (size >> 1);
  174. const uint8* src = srcData.getPixelPointer (sx, sy);
  175. for (int xx = 0; xx < size; ++xx)
  176. {
  177. if (sx >= srcData.width)
  178. break;
  179. if (sx >= 0)
  180. {
  181. const float kernelMult = values [xx + yy * size];
  182. c1 += kernelMult * *src++;
  183. c2 += kernelMult * *src++;
  184. c3 += kernelMult * *src++;
  185. }
  186. else
  187. {
  188. src += 3;
  189. }
  190. ++sx;
  191. }
  192. }
  193. }
  194. *dest++ = (uint8) roundToInt (c1);
  195. *dest++ = (uint8) roundToInt (c2);
  196. *dest++ = (uint8) roundToInt (c3);
  197. }
  198. }
  199. }
  200. }