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.

median.c 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * libpipi Proper image processing implementation library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * median.c: median filter functions
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include "pipi.h"
  24. #include "pipi_internals.h"
  25. pipi_image_t *pipi_median(pipi_image_t *src, int radius)
  26. {
  27. return pipi_median_ext(src, radius, radius);
  28. }
  29. /* FIXME: this is in desperate want of optimisation. Here is what could
  30. * be done to improve the performance:
  31. * - prefetch the neighbourhood; most neighbours are the same as the
  32. * previous pixels.
  33. * - use a better sort algorithm; bubble sort is ridiculous
  34. * - even better, use state-of-the art median selection, ie. O(3n), for
  35. * most common combinations (9, 25, 49, 81). */
  36. pipi_image_t *pipi_median_ext(pipi_image_t *src, int rx, int ry)
  37. {
  38. pipi_image_t *dst;
  39. pipi_pixels_t *srcp, *dstp;
  40. float *srcdata, *dstdata;
  41. double *list;
  42. int x, y, w, h, i, j, k, size, gray;
  43. w = src->w;
  44. h = src->h;
  45. size = (2 * rx + 1) * (2 * ry + 1);
  46. gray = (src->last_modified == PIPI_PIXELS_Y_F);
  47. srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  48. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  49. srcdata = (float *)srcp->pixels;
  50. dst = pipi_new(w, h);
  51. dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  52. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  53. dstdata = (float *)dstp->pixels;
  54. list = malloc(size * (gray ? 1 : 4) * sizeof(double));
  55. for(y = 0; y < h; y++)
  56. {
  57. for(x = 0; x < w; x++)
  58. {
  59. double tmp;
  60. double *parser = list;
  61. /* Make a list of neighbours */
  62. for(j = -ry; j <= ry; j++)
  63. {
  64. int y2 = y + j;
  65. if(y2 < 0) y2 = h - 1 - ((-y2 - 1) % h);
  66. else if(y2 > 0) y2 = y2 % h;
  67. for(i = -rx; i <= rx; i++)
  68. {
  69. int x2 = x + i;
  70. if(x2 < 0) x2 = w - 1 - ((-x2 - 1) % w);
  71. else if(x2 > 0) x2 = x2 % w;
  72. if(gray)
  73. {
  74. *parser++ = srcdata[y2 * w + x2];
  75. }
  76. else
  77. {
  78. *parser++ = srcdata[4 * (y2 * w + x2)];
  79. *parser++ = srcdata[4 * (y2 * w + x2) + 1];
  80. *parser++ = srcdata[4 * (y2 * w + x2) + 2];
  81. *parser++ = srcdata[4 * (y2 * w + x2) + 3];
  82. }
  83. }
  84. }
  85. /* Sort the list */
  86. for(i = 0; i < size; i++)
  87. {
  88. for(j = 0; j < i; j++)
  89. {
  90. if(gray)
  91. {
  92. if(list[i] < list[j])
  93. {
  94. tmp = list[i];
  95. list[i] = list[j];
  96. list[j] = tmp;
  97. }
  98. }
  99. else
  100. {
  101. for(k = 0; k < 4; k++)
  102. {
  103. if(list[4 * i + k] < list[4 * j + k])
  104. {
  105. tmp = list[4 * i + k];
  106. list[4 * i + k] = list[4 * j + k];
  107. list[4 * j + k] = tmp;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. /* Store the median value */
  114. if(gray)
  115. {
  116. dstdata[y * w + x] = list[size / 2];
  117. }
  118. else
  119. {
  120. dstdata[4 * (y * w + x)] = list[size / 2 * 4];
  121. dstdata[4 * (y * w + x) + 1] = list[size / 2 * 4 + 1];
  122. dstdata[4 * (y * w + x) + 2] = list[size / 2 * 4 + 2];
  123. dstdata[4 * (y * w + x) + 3] = list[size / 2 * 4 + 3];
  124. }
  125. }
  126. }
  127. return dst;
  128. }