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.
 
 
 
 
 
 

171 lines
4.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. * dilate.c: dilate and erode 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. /* FIXME: these functions are almost the same, try to merge them
  25. * somewhat efficiently. */
  26. /* TODO: - dilate by k (Manhattan distance)
  27. * - dilate by r (euclidian distance, with non-integer r) */
  28. pipi_image_t *pipi_dilate(pipi_image_t *src)
  29. {
  30. pipi_image_t *dst;
  31. pipi_pixels_t *srcp, *dstp;
  32. float *srcdata, *dstdata;
  33. int x, y, w, h, i, gray;
  34. w = src->w;
  35. h = src->h;
  36. gray = (src->last_modified == PIPI_PIXELS_Y_F);
  37. srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  38. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  39. srcdata = (float *)srcp->pixels;
  40. dst = pipi_new(w, h);
  41. dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  42. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  43. dstdata = (float *)dstp->pixels;
  44. for(y = 0; y < h; y++)
  45. {
  46. for(x = 0; x < w; x++)
  47. {
  48. double t;
  49. int x2, y2, x3, y3;
  50. y2 = y - 1;
  51. if(y2 < 0) y2 = h - 1;
  52. y3 = y + 1;
  53. if(y3 >= h) y3 = 0;
  54. x2 = x - 1;
  55. if(x2 < 0) x2 = w - 1;
  56. x3 = x + 1;
  57. if(x3 >= w) x3 = 0;
  58. if(gray)
  59. {
  60. t = srcdata[y * w + x];
  61. if(srcdata[y2 * w + x] > t) t = srcdata[y2 * w + x];
  62. if(srcdata[y3 * w + x] > t) t = srcdata[y3 * w + x];
  63. if(srcdata[y * w + x2] > t) t = srcdata[y * w + x2];
  64. if(srcdata[y * w + x3] > t) t = srcdata[y * w + x3];
  65. dstdata[y * w + x] = t;
  66. }
  67. else
  68. {
  69. for(i = 0; i < 4; i++)
  70. {
  71. t = srcdata[4 * (y * w + x) + i];
  72. if(srcdata[4 * (y2 * w + x) + i] > t)
  73. t = srcdata[4 * (y2 * w + x) + i];
  74. if(srcdata[4 * (y3 * w + x) + i] > t)
  75. t = srcdata[4 * (y3 * w + x) + i];
  76. if(srcdata[4 * (y * w + x2) + i] > t)
  77. t = srcdata[4 * (y * w + x2) + i];
  78. if(srcdata[4 * (y * w + x3) + i] > t)
  79. t = srcdata[4 * (y * w + x3) + i];
  80. dstdata[4 * (y * w + x) + i] = t;
  81. }
  82. }
  83. }
  84. }
  85. return dst;
  86. }
  87. pipi_image_t *pipi_erode(pipi_image_t *src)
  88. {
  89. pipi_image_t *dst;
  90. pipi_pixels_t *srcp, *dstp;
  91. float *srcdata, *dstdata;
  92. int x, y, w, h, i, gray;
  93. w = src->w;
  94. h = src->h;
  95. gray = (src->last_modified == PIPI_PIXELS_Y_F);
  96. srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  97. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  98. srcdata = (float *)srcp->pixels;
  99. dst = pipi_new(w, h);
  100. dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  101. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  102. dstdata = (float *)dstp->pixels;
  103. for(y = 0; y < h; y++)
  104. {
  105. for(x = 0; x < w; x++)
  106. {
  107. double t;
  108. int x2, y2, x3, y3;
  109. y2 = y - 1;
  110. if(y2 < 0) y2 = h - 1;
  111. y3 = y + 1;
  112. if(y3 >= h) y3 = 0;
  113. x2 = x - 1;
  114. if(x2 < 0) x2 = w - 1;
  115. x3 = x + 1;
  116. if(x3 >= w) x3 = 0;
  117. if(gray)
  118. {
  119. t = srcdata[y * w + x];
  120. if(srcdata[y2 * w + x] < t) t = srcdata[y2 * w + x];
  121. if(srcdata[y3 * w + x] < t) t = srcdata[y3 * w + x];
  122. if(srcdata[y * w + x2] < t) t = srcdata[y * w + x2];
  123. if(srcdata[y * w + x3] < t) t = srcdata[y * w + x3];
  124. dstdata[y * w + x] = t;
  125. }
  126. else
  127. {
  128. for(i = 0; i < 4; i++)
  129. {
  130. t = srcdata[4 * (y * w + x) + i];
  131. if(srcdata[4 * (y2 * w + x) + i] < t)
  132. t = srcdata[4 * (y2 * w + x) + i];
  133. if(srcdata[4 * (y3 * w + x) + i] < t)
  134. t = srcdata[4 * (y3 * w + x) + i];
  135. if(srcdata[4 * (y * w + x2) + i] < t)
  136. t = srcdata[4 * (y * w + x2) + i];
  137. if(srcdata[4 * (y * w + x3) + i] < t)
  138. t = srcdata[4 * (y * w + x3) + i];
  139. dstdata[4 * (y * w + x) + i] = t;
  140. }
  141. }
  142. }
  143. }
  144. return dst;
  145. }