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.
 
 
 
 
 
 

172 line
5.0 KiB

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