From 290e151913c2e7db67943192ddf2dce1627e4564 Mon Sep 17 00:00:00 2001 From: jylam Date: Thu, 7 Aug 2008 15:21:17 +0000 Subject: [PATCH] * First shot of a floodfiller (both u32 and float, 4 neighbours) git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2676 92316355-f0b4-4df1-b90c-862c8a59935f --- examples/Makefile.am | 5 +- examples/floodfill.c | 42 ++++++ pipi/Makefile.am | 1 + pipi/fill/floodfill.c | 316 ++++++++++++++++++++++++++++++++++++++++++ pipi/pipi.h | 3 + 5 files changed, 366 insertions(+), 1 deletion(-) create mode 100644 examples/floodfill.c create mode 100644 pipi/fill/floodfill.c diff --git a/examples/Makefile.am b/examples/Makefile.am index ef5dcc8..5c4d29b 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -2,7 +2,7 @@ AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/pipi -bin_PROGRAMS = blur dither edd img2rubik sharpen +bin_PROGRAMS = blur dither edd img2rubik sharpen floodfill blur_SOURCES = blur.c blur_LDADD = ../pipi/libpipi.la @@ -19,3 +19,6 @@ img2rubik_LDADD = ../pipi/libpipi.la sharpen_SOURCES = sharpen.c sharpen_LDADD = ../pipi/libpipi.la +floodfill_SOURCES = floodfill.c +floodfill_LDADD = ../pipi/libpipi.la + diff --git a/examples/floodfill.c b/examples/floodfill.c new file mode 100644 index 0000000..cae5a27 --- /dev/null +++ b/examples/floodfill.c @@ -0,0 +1,42 @@ +#include "config.h" +#include "common.h" + +#include +#include +#include + +#include + +int main(int argc, char *argv[]) +{ + char *srcname = NULL, *dstname = NULL; + pipi_image_t *img, *newimg; + int ret = 0; + if(argc < 4) + { + fprintf(stderr, "%s: too few arguments\n", argv[0]); + fprintf(stderr, "Usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + srcname = argv[1]; + dstname = argv[4]; + + img = pipi_load(srcname); + + if(!img) { + fprintf(stderr, "Can't open %s for reading\n", srcname); + } + + newimg = pipi_copy(img); + pipi_free(img); + ret = pipi_flood_fill(newimg, atoi(argv[2]), atoi(argv[3]), 1, 0, 0); + + if(!ret) pipi_save(newimg, dstname); + + + pipi_free(newimg); + + return ret; +} + diff --git a/pipi/Makefile.am b/pipi/Makefile.am index 1ef472e..899640c 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -35,6 +35,7 @@ libpipi_la_SOURCES = \ dither/ostromoukhov.c \ dither/dbs.c \ dither/random.c \ + fill/floodfill.c \ $(NULL) libpipi_la_CFLAGS = $(codec_cflags) libpipi_la_LDFLAGS = $(codec_libs) \ diff --git a/pipi/fill/floodfill.c b/pipi/fill/floodfill.c new file mode 100644 index 0000000..025a22f --- /dev/null +++ b/pipi/fill/floodfill.c @@ -0,0 +1,316 @@ +/* + * libpipi Proper image processing implementation library + * Copyright (c) 2004-2008 Sam Hocevar + * 2008 Jean-Yves Lamoureux + * All Rights Reserved + * + * $Id$ + * + * This library is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +/* + * floodfill.c: flood fill (4 neighbours) functions + */ + +#include "config.h" +#include "common.h" + +#include +#include +#include +#include + +#include "pipi.h" +#include "pipi_internals.h" + +#define STACKSIZE (1<<20) + +static void pipi_flood_fill_stack_scanline_u32(pipi_pixels_t *dstp, + int x, int y, + uint32_t new, uint32_t old); +static void pipi_flood_fill_stack_scanline_float(pipi_pixels_t *dstp, + int x, int y, + float nr, float ng, float nb, + float or, float og, float ob); +static int pop (int *x,int *y, int h); +static int push(int x, int y, int h); +static void clear_stack(int h); +static int stack[STACKSIZE]; +static int stack_pointer; + +static float get_max_color_diff(float r1, float g1, float b1, + float r2, float g2, float b2); +static int validate_pixel_f(float r1, float g1, float b1, + float r2, float g2, float b2); + +/* + Stack based flood fill. + Instead of using system stack (calling recursively functions, + which can lead to big problems as this stack is a fixed and small sized one), + we create our own one. + Additionnaly, we use a scanline trick to speed up the whole thing. + + This method is more or less the one described at + http://student.kuleuven.be/~m0216922/CG/floodfill.html + +*/ + +/* Public function */ +int pipi_flood_fill(pipi_image_t *src, + int px, int py, + float r, float g, float b) +{ + pipi_image_t *dst = src; + pipi_pixels_t *dstp; + int w, h; + + if(!src) return -1; + + w = src->w; + h = src->h; + + if((px >= src->w) || (py >= src->h) || + (px < 0) || (py < 0)) return -1; + + + if(src->last_modified == PIPI_PIXELS_RGBA32) { + uint32_t *dstdata; + unsigned char nr, ng, nb; + + /* Get ARGB32 pointer */ + dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA32); + dstdata = (uint32_t *)dstp->pixels; + + nr = r*255.0f; + ng = g*255.0f; + nb = b*255.0f; + + dstp->w = w; + dstp->h = h; + pipi_flood_fill_stack_scanline_u32(dstp, px, py, (nr<<16)|(ng<<8)|(nb), dstdata[px+py*w]); + + } else { + int gray = (dst->last_modified == PIPI_PIXELS_Y_F); + float *dstdata; + float or, og, ob; + + dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) + : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); + + dstdata = (float *)dstp->pixels; + + or = dstdata[(px+py*w)*4]; + og = dstdata[(px+py*w)*4 + 1]; + ob = dstdata[(px+py*w)*4 + 2]; + + dstp->w = w; + dstp->h = h; + + pipi_flood_fill_stack_scanline_float(dstp, px, py, r, g, b, or, og, ob); + } + + return 0; +} + + +/* ARGB32 */ +void pipi_flood_fill_stack_scanline_u32(pipi_pixels_t *dstp, + int x, int y, + uint32_t new, uint32_t old) +{ + if(new==old) return; + + clear_stack(dstp->h); + + int yt; + int left, right; + + uint32_t *dstdata = (uint32_t *)dstp->pixels; + + if(!push(x, y, dstp->h)) return; + + while(pop(&x, &y, dstp->h)) + { + yt = y; + while(yt >= 0 && (dstdata[x+yt*dstp->w] == old)) { + yt--; + } + + yt++; + left = right = 0; + + while(yt < dstp->h && (dstdata[x+yt*dstp->w] == old)) + { + uint32_t *cur_line = &dstdata[(yt*dstp->w)]; + int xp1 = (x+1); + int xm1 = (x-1); + cur_line[x] = new; + + if(!left && x > 0 && (cur_line[xm1] == old)) + { + if(!push(x - 1, yt, dstp->h)) return; + left = 1; + } + else if(left && x > 0 && (cur_line[xm1] != old)) + { + left = 0; + } + if(!right && x < dstp->w - 1 && (cur_line[xp1] == old)) + { + if(!push(x + 1, yt, dstp->h)) return; + right = 1; + } + else if(right && x < dstp->w - 1 && (cur_line[xp1] != old)) + { + right = 0; + } + yt++; + } + } +} + +/* Float version. Much slower, but supports HDR and (soon antialiasing) */ +static void pipi_flood_fill_stack_scanline_float(pipi_pixels_t *dstp, + int x, int y, + float nr, float ng, float nb, + float or, float og, float ob) +{ + if((nr==or) && (ng==og) && (nb==ob)) return; + + clear_stack(dstp->h); + + int yt; + int left, right; + float *dstdata = (float *)dstp->pixels; + + if(!push(x, y, dstp->h)) return; + + while(pop(&x, &y, dstp->h)) + { + yt = y; + while(yt >= 0 && validate_pixel_f(dstdata[(x+yt*dstp->w)*4] , + dstdata[(x+yt*dstp->w)*4 + 1] , + dstdata[(x+yt*dstp->w)*4 + 2] , + or, og, ob)) { + yt--; + } + + yt++; + left = right = 0; + + while(yt < dstp->h && validate_pixel_f(dstdata[(x+yt*dstp->w)*4] , + dstdata[(x+yt*dstp->w)*4 + 1] , + dstdata[(x+yt*dstp->w)*4 + 2] , + or, og, ob)) + { + float *cur_line = &dstdata[(yt*dstp->w)*4]; + int xp1 = (x+1)*4; + int xm1 = (x-1)*4; + cur_line[x*4] = nr; + cur_line[x*4 + 1] = ng; + cur_line[x*4 + 2] = nb; + + if(!left && x > 0 && validate_pixel_f(cur_line[xm1], + cur_line[xm1 + 1], + cur_line[xm1 + 2], + or, og, ob)) + { + if(!push(x - 1, yt, dstp->h)) return; + left = 1; + } + else if(left && x > 0 && !validate_pixel_f(cur_line[xm1] , + cur_line[xm1 + 1] , + cur_line[xm1 + 2] , + or, og, ob)) + { + left = 0; + } + if(!right && x < dstp->w - 1 && validate_pixel_f(cur_line[xp1] , + cur_line[xp1 + 1] , + cur_line[xp1 + 2] , + or, og, ob)) + { + if(!push(x + 1, yt, dstp->h)) return; + right = 1; + } + else if(right && x < dstp->w - 1 && !validate_pixel_f(cur_line[xp1] , + cur_line[xp1 + 1] , + cur_line[xp1 + 2], + or, og, ob)) + { + right = 0; + } + yt++; + } + } +} + + +/* Following functions are local */ + +static int pop(int *x, int *y, int h) +{ + if(stack_pointer > 0) + { + int p = stack[stack_pointer]; + *x = p / h; + *y = p % h; + stack_pointer--; + return 1; + } + else + { + return 0; + } +} + +static int push(int x, int y, int h) +{ + if(stack_pointer < STACKSIZE - 1) + { + stack_pointer++; + stack[stack_pointer] = h * x + y; + return 1; + } + else + { + return 0; + } +} + +static void clear_stack(int h) +{ + int x, y; + while(pop(&x, &y, h)); +} + +#define MAX(a, b) (b>a?b:a) + + +/* FIXME : Paves the way to antialiasing, but could be only 3 tests */ +static float get_max_color_diff(float r1, float g1, float b1, + float r2, float g2, float b2) +{ + float r = abs(r2-r1); + float g = abs(g2-g1); + float b = abs(b2-b1); + + return MAX(MAX(r, g), b); +} + + +static int validate_pixel_f(float r1, float g1, float b1, + float r2, float g2, float b2) +{ + + if(get_max_color_diff(r1, g1, b1, + r2, g2, b2) + == 0) return 1; + else + return 0; +} diff --git a/pipi/pipi.h b/pipi/pipi.h index 12c6daf..aef91d7 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -82,6 +82,9 @@ extern pipi_image_t *pipi_gaussian_blur(pipi_image_t *, float); extern pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *, float, float, float, float); +extern int pipi_flood_fill(pipi_image_t *, + int, int, float, float, float); + extern pipi_image_t *pipi_dither_floydsteinberg(pipi_image_t *, pipi_scan_t); extern pipi_image_t *pipi_dither_ordered(pipi_image_t *); extern pipi_image_t *pipi_dither_random(pipi_image_t *);