convolve.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2011, Tom Distler (http://tdistler.com)
  3. * All rights reserved.
  4. *
  5. * The BSD License
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * - Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * - Neither the name of the tdistler.com nor the names of its contributors may
  18. * be used to endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifndef _CONVOLVE_H_
  34. #define _CONVOLVE_H_
  35. typedef float (*_iqa_get_pixel)(const float *img, int w, int h, int x, int y, float bnd_const);
  36. /** Out-of-bounds array values are a mirrored reflection of the border values*/
  37. float KBND_SYMMETRIC(const float *img, int w, int h, int x, int y, float bnd_const);
  38. /** Out-of-bounds array values are set to the nearest border value */
  39. float KBND_REPLICATE(const float *img, int w, int h, int x, int y, float bnd_const);
  40. /** Out-of-bounds array values are set to 'bnd_const' */
  41. float KBND_CONSTANT(const float *img, int w, int h, int x, int y, float bnd_const);
  42. /** Defines a convolution kernel */
  43. struct _kernel {
  44. float *kernel; /**< Pointer to the kernel values */
  45. int w; /**< The kernel width */
  46. int h; /**< The kernel height */
  47. int normalized; /**< 1 if the kernel values add up to 1. 0 otherwise */
  48. _iqa_get_pixel bnd_opt; /**< Defines how out-of-bounds image values are handled */
  49. float bnd_const; /**< If 'bnd_opt' is KBND_CONSTANT, this specifies the out-of-bounds value */
  50. };
  51. /**
  52. * @brief Applies the specified kernel to the image.
  53. * The kernel will be applied to all areas where it fits completely within
  54. * the image. The resulting image will be smaller by half the kernel width
  55. * and height (w - kw/2 and h - kh/2).
  56. *
  57. * @param img Image to modify
  58. * @param w Image width
  59. * @param h Image height
  60. * @param k The kernel to apply
  61. * @param result Buffer to hold the resulting image ((w-kw)*(h-kh), where kw
  62. * and kh are the kernel width and height). If 0, the result
  63. * will be written to the original image buffer.
  64. * @param rw Optional. The width of the resulting image will be stored here.
  65. * @param rh Optional. The height of the resulting image will be stored here.
  66. */
  67. void _iqa_convolve(float *img, int w, int h, const struct _kernel *k, float *result, int *rw, int *rh);
  68. /**
  69. * The same as _iqa_convolve() except the kernel is applied to the entire image.
  70. * In other words, the kernel is applied to all areas where the top-left corner
  71. * of the kernel is in the image. Out-of-bound pixel value (off the right and
  72. * bottom edges) are chosen based on the 'bnd_opt' and 'bnd_const' members of
  73. * the kernel structure. The resulting array is the same size as the input
  74. * image.
  75. *
  76. * @param img Image to modify
  77. * @param w Image width
  78. * @param h Image height
  79. * @param k The kernel to apply
  80. * @param result Buffer to hold the resulting image ((w-kw)*(h-kh), where kw
  81. * and kh are the kernel width and height). If 0, the result
  82. * will be written to the original image buffer.
  83. * @return 0 if successful. Non-zero otherwise.
  84. */
  85. int _iqa_img_filter(float *img, int w, int h, const struct _kernel *k, float *result);
  86. /**
  87. * Returns the filtered version of the specified pixel. If no kernel is given,
  88. * the raw pixel value is returned.
  89. *
  90. * @param img Source image
  91. * @param w Image width
  92. * @param h Image height
  93. * @param x The x location of the pixel to filter
  94. * @param y The y location of the pixel to filter
  95. * @param k Optional. The convolution kernel to apply to the pixel.
  96. * @param kscale The scale of the kernel (for normalization). 1 for normalized
  97. * kernels. Required if 'k' is not null.
  98. * @return The filtered pixel value.
  99. */
  100. float _iqa_filter_pixel(const float *img, int w, int h, int x, int y, const struct _kernel *k, const float kscale);
  101. #endif /*_CONVOLVE_H_*/