138 行
4.2 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 cmpint(void const *i1, void const *i2)
  25. {
  26. /* On Linux amd64, this is 20 to 30 % faster than using a real
  27. * comparison (which wouldn't work on Windows since it expects both
  28. * negative and positive values), a ternary operator, or floats instead
  29. * of doubles. */
  30. union { int32_t i; double d; } u;
  31. u.d = *(double const *)i1 - *(double const *)i2;
  32. return u.i;
  33. }
  34. pipi_image_t *pipi_median(pipi_image_t *src, int radius)
  35. {
  36. return pipi_median_ext(src, radius, radius);
  37. }
  38. /* FIXME: this is in desperate want of optimisation. Here is what could
  39. * be done to improve the performance:
  40. * - prefetch the neighbourhood; most neighbours are the same as the
  41. * previous pixels.
  42. * - use a better sort algorithm; bubble sort is ridiculous
  43. * - even better, use state-of-the art median selection, ie. O(3n), for
  44. * most common combinations (9, 25, 49, 81). */
  45. pipi_image_t *pipi_median_ext(pipi_image_t *src, int rx, int ry)
  46. {
  47. pipi_image_t *dst;
  48. pipi_pixels_t *srcp, *dstp;
  49. float *srcdata, *dstdata;
  50. double *list;
  51. int x, y, w, h, i, j, size, gray;
  52. w = src->w;
  53. h = src->h;
  54. size = (2 * rx + 1) * (2 * ry + 1);
  55. gray = (src->last_modified == PIPI_PIXELS_Y_F32);
  56. srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32)
  57. : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  58. srcdata = (float *)srcp->pixels;
  59. dst = pipi_new(w, h);
  60. dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32)
  61. : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  62. dstdata = (float *)dstp->pixels;
  63. list = malloc(size * (gray ? 1 : 4) * sizeof(double));
  64. for(y = 0; y < h; y++)
  65. {
  66. for(x = 0; x < w; x++)
  67. {
  68. double *parser = list;
  69. /* Make a list of neighbours */
  70. for(j = -ry; j <= ry; j++)
  71. {
  72. int y2 = y + j;
  73. if(y2 < 0) y2 = h - 1 - ((-y2 - 1) % h);
  74. else if(y2 > 0) y2 = y2 % h;
  75. for(i = -rx; i <= rx; i++)
  76. {
  77. int x2 = x + i;
  78. if(x2 < 0) x2 = w - 1 - ((-x2 - 1) % w);
  79. else if(x2 > 0) x2 = x2 % w;
  80. if(gray)
  81. {
  82. *parser++ = srcdata[y2 * w + x2];
  83. }
  84. else
  85. {
  86. parser[0] = srcdata[4 * (y2 * w + x2)];
  87. parser[size * 1] = srcdata[4 * (y2 * w + x2) + 1];
  88. parser[size * 2] = srcdata[4 * (y2 * w + x2) + 2];
  89. parser[size * 3] = srcdata[4 * (y2 * w + x2) + 3];
  90. parser++;
  91. }
  92. }
  93. }
  94. /* Sort the list */
  95. qsort(list, size, sizeof(double), cmpint);
  96. if(!gray)
  97. {
  98. qsort(list + size, size, sizeof(double), cmpint);
  99. qsort(list + 2 * size, size, sizeof(double), cmpint);
  100. qsort(list + 3 * size, size, sizeof(double), cmpint);
  101. }
  102. /* Store the median value */
  103. if(gray)
  104. {
  105. dstdata[y * w + x] = list[size / 2];
  106. }
  107. else
  108. {
  109. dstdata[4 * (y * w + x)] = list[size / 2];
  110. dstdata[4 * (y * w + x) + 1] = list[size / 2 + size * 1];
  111. dstdata[4 * (y * w + x) + 2] = list[size / 2 + size * 2];
  112. dstdata[4 * (y * w + x) + 3] = list[size / 2 + size * 3];
  113. }
  114. }
  115. }
  116. return dst;
  117. }