lpyramid.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Laplacian Pyramid
  3. Copyright (C) 2006 Yangli Hector Yee
  4. This program is free software; you can redistribute it and/or modify it under the terms of the
  5. GNU General Public License as published by the Free Software Foundation; either version 2 of the License,
  6. or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  8. without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with this program;
  11. if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
  12. */
  13. #include "lpyramid.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. struct _lpyramid {
  18. /* Successively blurred versions of the original image */
  19. float *levels[MAX_PYR_LEVELS];
  20. int width;
  21. int height;
  22. };
  23. static void
  24. convolve (lpyramid_t *pyramid, float *a, const float *b)
  25. /* convolves image b with the filter kernel and stores it in a */
  26. {
  27. int y,x,i,j;
  28. const float Kernel[] = {0.05f, 0.25f, 0.4f, 0.25f, 0.05f};
  29. int width = pyramid->width;
  30. int height = pyramid->height;
  31. for (y=0; y<height; y++) {
  32. for (x=0; x<width; x++) {
  33. float sum = 0.f;
  34. for (j=-2; j<=2; j++) {
  35. float sum_i = 0.f;
  36. int ny=y+j;
  37. if (ny<0) ny=-ny;
  38. if (ny>=height) ny=2*height - ny - 1;
  39. ny *= width;
  40. for (i=-2; i<=2; i++) {
  41. int nx=x+i;
  42. if (nx<0) nx=-nx;
  43. if (nx>=width) nx=2*width - nx - 1;
  44. sum_i += Kernel[i+2] * b[ny + nx];
  45. }
  46. sum += sum_i * Kernel[j+2];
  47. }
  48. *a++ = sum;
  49. }
  50. }
  51. }
  52. /*
  53. * Construction/Destruction
  54. */
  55. lpyramid_t *
  56. lpyramid_create (float *image, int width, int height)
  57. {
  58. lpyramid_t *pyramid;
  59. int i;
  60. pyramid = malloc (sizeof (lpyramid_t));
  61. if (pyramid == NULL) {
  62. fprintf (stderr, "Out of memory.\n");
  63. exit (1);
  64. }
  65. pyramid->width = width;
  66. pyramid->height = height;
  67. /* Make the Laplacian pyramid by successively
  68. * copying the earlier levels and blurring them */
  69. for (i=0; i<MAX_PYR_LEVELS; i++) {
  70. pyramid->levels[i] = malloc (width * height * sizeof (float));
  71. if (pyramid->levels[i] == NULL) {
  72. fprintf (stderr, "Out of memory.\n");
  73. exit (1);
  74. }
  75. if (i == 0) {
  76. memcpy (pyramid->levels[i], image, width * height * sizeof (float));
  77. } else {
  78. convolve(pyramid, pyramid->levels[i], pyramid->levels[i - 1]);
  79. }
  80. }
  81. return pyramid;
  82. }
  83. void
  84. lpyramid_destroy (lpyramid_t *pyramid)
  85. {
  86. int i;
  87. for (i=0; i<MAX_PYR_LEVELS; i++)
  88. free (pyramid->levels[i]);
  89. free (pyramid);
  90. }
  91. float
  92. lpyramid_get_value (lpyramid_t *pyramid, int x, int y, int level)
  93. {
  94. int index = x + y * pyramid->width;
  95. int l = level;
  96. if (l > MAX_PYR_LEVELS)
  97. l = MAX_PYR_LEVELS;
  98. return pyramid->levels[l][index];
  99. }