Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

320 lignes
9.1 KiB

  1. /*
  2. * libpipi Proper image processing implementation library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software. It comes without any warranty, to
  10. * the extent permitted by applicable law. You can redistribute it
  11. * and/or modify it under the terms of the Do What The Fuck You Want
  12. * To Public License, Version 2, as published by Sam Hocevar. See
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. /*
  16. * floodfill.c: flood fill (4 neighbours) functions
  17. */
  18. #include "config.h"
  19. #include "common.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include "pipi.h"
  25. #include "pipi_internals.h"
  26. #define STACKSIZE (1<<20)
  27. static void pipi_flood_fill_stack_scanline_u32(pipi_pixels_t *dstp,
  28. int x, int y,
  29. uint32_t new, uint32_t old);
  30. static void pipi_flood_fill_stack_scanline_float(pipi_pixels_t *dstp,
  31. int x, int y,
  32. float nr, float ng, float nb, float na,
  33. float or, float og, float ob, float oa);
  34. static int pop (int *x,int *y, int h);
  35. static int push(int x, int y, int h);
  36. static void clear_stack(int h);
  37. static int stack[STACKSIZE];
  38. static int stack_pointer;
  39. static float get_max_color_diff(float r1, float g1, float b1,
  40. float r2, float g2, float b2);
  41. static int validate_pixel_f(float r1, float g1, float b1,
  42. float r2, float g2, float b2);
  43. /*
  44. Stack based flood fill.
  45. Instead of using system stack (calling recursively functions,
  46. which can lead to big problems as this stack is a fixed and small sized one),
  47. we create our own one.
  48. Additionnaly, we use a scanline trick to speed up the whole thing.
  49. This method is more or less the one described at
  50. http://student.kuleuven.be/~m0216922/CG/floodfill.html
  51. */
  52. /* Public function */
  53. int pipi_flood_fill(pipi_image_t *src,
  54. int px, int py,
  55. float r, float g, float b, float a)
  56. {
  57. pipi_image_t *dst = src;
  58. pipi_pixels_t *dstp;
  59. int w, h;
  60. if(!src) return -1;
  61. w = src->w;
  62. h = src->h;
  63. if((px >= src->w) || (py >= src->h) ||
  64. (px < 0) || (py < 0)) return -1;
  65. if(src->last_modified == PIPI_PIXELS_RGBA_C) {
  66. uint32_t *dstdata;
  67. unsigned char nr, ng, nb, na;
  68. /* Get ARGB32 pointer */
  69. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_C);
  70. dstdata = (uint32_t *)dstp->pixels;
  71. nr = r*255.0f;
  72. ng = g*255.0f;
  73. nb = b*255.0f;
  74. na = a*255.0f;
  75. dstp->w = w;
  76. dstp->h = h;
  77. pipi_flood_fill_stack_scanline_u32(dstp, px, py, (na<<24)|(nr<<16)|(ng<<8)|(nb), dstdata[px+py*w]);
  78. } else {
  79. int gray = (dst->last_modified == PIPI_PIXELS_Y_F);
  80. float *dstdata;
  81. float or, og, ob, oa;
  82. dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  83. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  84. dstdata = (float *)dstp->pixels;
  85. or = dstdata[(px+py*w)*4];
  86. og = dstdata[(px+py*w)*4 + 1];
  87. ob = dstdata[(px+py*w)*4 + 2];
  88. oa = dstdata[(px+py*w)*4 + 3];
  89. dstp->w = w;
  90. dstp->h = h;
  91. pipi_flood_fill_stack_scanline_float(dstp, px, py, r, g, b, a, or, og, ob, oa);
  92. }
  93. return 0;
  94. }
  95. /* ARGB32 */
  96. void pipi_flood_fill_stack_scanline_u32(pipi_pixels_t *dstp,
  97. int x, int y,
  98. uint32_t new, uint32_t old)
  99. {
  100. if(new==old) return;
  101. clear_stack(dstp->h);
  102. int yt;
  103. int left, right;
  104. uint32_t *dstdata = (uint32_t *)dstp->pixels;
  105. if(!push(x, y, dstp->h)) return;
  106. while(pop(&x, &y, dstp->h))
  107. {
  108. yt = y;
  109. while(yt >= 0 && (dstdata[x+yt*dstp->w] == old)) {
  110. yt--;
  111. }
  112. yt++;
  113. left = right = 0;
  114. while(yt < dstp->h && (dstdata[x+yt*dstp->w] == old))
  115. {
  116. uint32_t *cur_line = &dstdata[(yt*dstp->w)];
  117. int xp1 = (x+1);
  118. int xm1 = (x-1);
  119. cur_line[x] = new;
  120. if(!left && x > 0 && (cur_line[xm1] == old))
  121. {
  122. if(!push(x - 1, yt, dstp->h)) return;
  123. left = 1;
  124. }
  125. else if(left && x > 0 && (cur_line[xm1] != old))
  126. {
  127. left = 0;
  128. }
  129. if(!right && x < dstp->w - 1 && (cur_line[xp1] == old))
  130. {
  131. if(!push(x + 1, yt, dstp->h)) return;
  132. right = 1;
  133. }
  134. else if(right && x < dstp->w - 1 && (cur_line[xp1] != old))
  135. {
  136. right = 0;
  137. }
  138. yt++;
  139. }
  140. }
  141. }
  142. /* Float version. Much slower, but supports HDR and (soon antialiasing) */
  143. static void pipi_flood_fill_stack_scanline_float(pipi_pixels_t *dstp,
  144. int x, int y,
  145. float nr, float ng, float nb, float na,
  146. float or, float og, float ob, float oa)
  147. {
  148. if((nr==or) && (ng==og) && (nb==ob)) return;
  149. clear_stack(dstp->h);
  150. int yt;
  151. int left, right;
  152. float *dstdata = (float *)dstp->pixels;
  153. if(!push(x, y, dstp->h)) return;
  154. while(pop(&x, &y, dstp->h))
  155. {
  156. yt = y;
  157. while(yt >= 0 && validate_pixel_f(dstdata[(x+yt*dstp->w)*4] ,
  158. dstdata[(x+yt*dstp->w)*4 + 1] ,
  159. dstdata[(x+yt*dstp->w)*4 + 2] ,
  160. or, og, ob)) {
  161. yt--;
  162. }
  163. yt++;
  164. left = right = 0;
  165. while(yt < dstp->h && validate_pixel_f(dstdata[(x+yt*dstp->w)*4] ,
  166. dstdata[(x+yt*dstp->w)*4 + 1] ,
  167. dstdata[(x+yt*dstp->w)*4 + 2] ,
  168. or, og, ob))
  169. {
  170. float *cur_line = &dstdata[(yt*dstp->w)*4];
  171. int xp1 = (x+1)*4;
  172. int xm1 = (x-1)*4;
  173. cur_line[x*4] = nr;
  174. cur_line[x*4 + 1] = ng;
  175. cur_line[x*4 + 2] = nb;
  176. cur_line[x*4 + 3] = na;
  177. if(!left && x > 0 && validate_pixel_f(cur_line[xm1],
  178. cur_line[xm1 + 1],
  179. cur_line[xm1 + 2],
  180. or, og, ob))
  181. {
  182. if(!push(x - 1, yt, dstp->h)) return;
  183. left = 1;
  184. }
  185. else if(left && x > 0 && !validate_pixel_f(cur_line[xm1] ,
  186. cur_line[xm1 + 1] ,
  187. cur_line[xm1 + 2] ,
  188. or, og, ob))
  189. {
  190. left = 0;
  191. }
  192. if(!right && x < dstp->w - 1 && validate_pixel_f(cur_line[xp1] ,
  193. cur_line[xp1 + 1] ,
  194. cur_line[xp1 + 2] ,
  195. or, og, ob))
  196. {
  197. if(!push(x + 1, yt, dstp->h)) return;
  198. right = 1;
  199. }
  200. else if(right && x < dstp->w - 1 && !validate_pixel_f(cur_line[xp1] ,
  201. cur_line[xp1 + 1] ,
  202. cur_line[xp1 + 2],
  203. or, og, ob))
  204. {
  205. right = 0;
  206. }
  207. yt++;
  208. }
  209. }
  210. }
  211. /* Following functions are local */
  212. static int pop(int *x, int *y, int h)
  213. {
  214. if(stack_pointer > 0)
  215. {
  216. int p = stack[stack_pointer];
  217. *x = p / h;
  218. *y = p % h;
  219. stack_pointer--;
  220. return 1;
  221. }
  222. else
  223. {
  224. return 0;
  225. }
  226. }
  227. static int push(int x, int y, int h)
  228. {
  229. if(stack_pointer < STACKSIZE - 1)
  230. {
  231. stack_pointer++;
  232. stack[stack_pointer] = h * x + y;
  233. return 1;
  234. }
  235. else
  236. {
  237. return 0;
  238. }
  239. }
  240. static void clear_stack(int h)
  241. {
  242. int x, y;
  243. while(pop(&x, &y, h));
  244. }
  245. #define MAX(a, b) (b>a?b:a)
  246. /* FIXME : Paves the way to antialiasing, but could be only 3 tests */
  247. static float get_max_color_diff(float r1, float g1, float b1,
  248. float r2, float g2, float b2)
  249. {
  250. float r = abs(r2-r1);
  251. float g = abs(g2-g1);
  252. float b = abs(b2-b1);
  253. return MAX(MAX(r, g), b);
  254. }
  255. static int validate_pixel_f(float r1, float g1, float b1,
  256. float r2, float g2, float b2)
  257. {
  258. if(get_max_color_diff(r1, g1, b1,
  259. r2, g2, b2)
  260. == 0) return 1;
  261. else
  262. return 0;
  263. }