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.
 
 
 
 
 
 

133 lines
3.9 KiB

  1. /*
  2. * libpipi Pathetic image processing interface 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. static int cmpint(void const *i1, void const *i2)
  26. {
  27. return *(double const *)i1 < *(double const *)i2;
  28. }
  29. pipi_image_t *pipi_median(pipi_image_t *src, int radius)
  30. {
  31. return pipi_median_ext(src, radius, radius);
  32. }
  33. /* FIXME: this is in desperate want of optimisation. Here is what could
  34. * be done to improve the performance:
  35. * - prefetch the neighbourhood; most neighbours are the same as the
  36. * previous pixels.
  37. * - use a better sort algorithm; bubble sort is ridiculous
  38. * - even better, use state-of-the art median selection, ie. O(3n), for
  39. * most common combinations (9, 25, 49, 81). */
  40. pipi_image_t *pipi_median_ext(pipi_image_t *src, int rx, int ry)
  41. {
  42. pipi_image_t *dst;
  43. pipi_pixels_t *srcp, *dstp;
  44. float *srcdata, *dstdata;
  45. double *list;
  46. int x, y, w, h, i, j, size, gray;
  47. w = src->w;
  48. h = src->h;
  49. size = (2 * rx + 1) * (2 * ry + 1);
  50. gray = (src->last_modified == PIPI_PIXELS_Y_F);
  51. srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  52. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  53. srcdata = (float *)srcp->pixels;
  54. dst = pipi_new(w, h);
  55. dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  56. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  57. dstdata = (float *)dstp->pixels;
  58. list = malloc(size * (gray ? 1 : 4) * sizeof(double));
  59. for(y = 0; y < h; y++)
  60. {
  61. for(x = 0; x < w; x++)
  62. {
  63. double *parser = list;
  64. /* Make a list of neighbours */
  65. for(j = -ry; j <= ry; j++)
  66. {
  67. int y2 = y + j;
  68. if(y2 < 0) y2 = h - 1 - ((-y2 - 1) % h);
  69. else if(y2 > 0) y2 = y2 % h;
  70. for(i = -rx; i <= rx; i++)
  71. {
  72. int x2 = x + i;
  73. if(x2 < 0) x2 = w - 1 - ((-x2 - 1) % w);
  74. else if(x2 > 0) x2 = x2 % w;
  75. if(gray)
  76. {
  77. *parser++ = srcdata[y2 * w + x2];
  78. }
  79. else
  80. {
  81. parser[0] = srcdata[4 * (y2 * w + x2)];
  82. parser[size * 1] = srcdata[4 * (y2 * w + x2) + 1];
  83. parser[size * 2] = srcdata[4 * (y2 * w + x2) + 2];
  84. parser[size * 3] = srcdata[4 * (y2 * w + x2) + 3];
  85. parser++;
  86. }
  87. }
  88. }
  89. /* Sort the list */
  90. qsort(list, size, sizeof(double), cmpint);
  91. if(!gray)
  92. {
  93. qsort(list + size, size, sizeof(double), cmpint);
  94. qsort(list + 2 * size, size, sizeof(double), cmpint);
  95. qsort(list + 3 * size, size, sizeof(double), cmpint);
  96. }
  97. /* Store the median value */
  98. if(gray)
  99. {
  100. dstdata[y * w + x] = list[size / 2];
  101. }
  102. else
  103. {
  104. dstdata[4 * (y * w + x)] = list[size / 2];
  105. dstdata[4 * (y * w + x) + 1] = list[size / 2 + size * 1];
  106. dstdata[4 * (y * w + x) + 2] = list[size / 2 + size * 2];
  107. dstdata[4 * (y * w + x) + 3] = list[size / 2 + size * 3];
  108. }
  109. }
  110. }
  111. return dst;
  112. }