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.
 
 
 
 
 
 

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