From 31fec9b526f2257b40180ee20a6afcbcd978b347 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 20 Aug 2008 22:00:17 +0000 Subject: [PATCH] * Add pipi_dilate() / pipi_erode() and --dilate / --erode. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2746 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/Makefile.am | 3 +- pipi/context.c | 18 +++++ pipi/filter/dilate.c | 171 +++++++++++++++++++++++++++++++++++++++++++ pipi/pipi.h | 2 + src/pipi.c | 10 +++ 5 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 pipi/filter/dilate.c diff --git a/pipi/Makefile.am b/pipi/Makefile.am index c21bbda..458a731 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -55,7 +55,8 @@ filter_sources = \ filter/blur.c \ filter/convolution.c filter/convolution_template.h \ filter/color.c \ - filter/median.c + filter/median.c \ + filter/dilate.c quantize_sources = \ quantize/reduce.c diff --git a/pipi/context.c b/pipi/context.c index 80317c2..666c4e4 100644 --- a/pipi/context.c +++ b/pipi/context.c @@ -478,6 +478,24 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...) ctx->images[ctx->nimages - 1] = pipi_invert(tmp); pipi_free(tmp); } + else if(!strcmp(cmd, "dilate")) + { + pipi_image_t *tmp; + if(ctx->nimages < 1) + return -1; + tmp = ctx->images[ctx->nimages - 1]; + ctx->images[ctx->nimages - 1] = pipi_dilate(tmp); + pipi_free(tmp); + } + else if(!strcmp(cmd, "erode")) + { + pipi_image_t *tmp; + if(ctx->nimages < 1) + return -1; + tmp = ctx->images[ctx->nimages - 1]; + ctx->images[ctx->nimages - 1] = pipi_erode(tmp); + pipi_free(tmp); + } else if(!strcmp(cmd, "gray")) { if(ctx->nimages < 1) diff --git a/pipi/filter/dilate.c b/pipi/filter/dilate.c new file mode 100644 index 0000000..b6dc362 --- /dev/null +++ b/pipi/filter/dilate.c @@ -0,0 +1,171 @@ +/* + * libpipi Proper image processing implementation library + * Copyright (c) 2004-2008 Sam Hocevar + * 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. + */ + +/* + * dilate.c: dilate and erode functions + */ + +#include "config.h" +#include "common.h" + +#include +#include +#include +#include + +#include "pipi.h" +#include "pipi_internals.h" + +/* FIXME: these functions are almost the same, try to merge them + * somewhat efficiently. */ +/* TODO: - dilate by k (Manhattan distance) + * - dilate by r (euclidian distance, with non-integer r) */ +pipi_image_t *pipi_dilate(pipi_image_t *src) +{ + pipi_image_t *dst; + pipi_pixels_t *srcp, *dstp; + float *srcdata, *dstdata; + int x, y, w, h, i, gray; + + w = src->w; + h = src->h; + + gray = (src->last_modified == PIPI_PIXELS_Y_F); + + srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F) + : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); + srcdata = (float *)srcp->pixels; + + dst = pipi_new(w, h); + dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) + : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); + dstdata = (float *)dstp->pixels; + + for(y = 0; y < h; y++) + { + for(x = 0; x < w; x++) + { + double t; + int x2, y2, x3, y3; + + y2 = y - 1; + if(y2 < 0) y2 = h - 1; + y3 = y + 1; + if(y3 >= h) y3 = 0; + + x2 = x - 1; + if(x2 < 0) x2 = w - 1; + x3 = x + 1; + if(x3 >= w) x3 = 0; + + if(gray) + { + t = srcdata[y * w + x]; + if(srcdata[y2 * w + x] > t) t = srcdata[y2 * w + x]; + if(srcdata[y3 * w + x] > t) t = srcdata[y3 * w + x]; + if(srcdata[y * w + x2] > t) t = srcdata[y * w + x2]; + if(srcdata[y * w + x3] > t) t = srcdata[y * w + x3]; + dstdata[y * w + x] = t; + } + else + { + for(i = 0; i < 4; i++) + { + t = srcdata[4 * (y * w + x) + i]; + if(srcdata[4 * (y2 * w + x) + i] > t) + t = srcdata[4 * (y2 * w + x) + i]; + if(srcdata[4 * (y3 * w + x) + i] > t) + t = srcdata[4 * (y3 * w + x) + i]; + if(srcdata[4 * (y * w + x2) + i] > t) + t = srcdata[4 * (y * w + x2) + i]; + if(srcdata[4 * (y * w + x3) + i] > t) + t = srcdata[4 * (y * w + x3) + i]; + dstdata[4 * (y * w + x) + i] = t; + } + } + } + } + + return dst; +} + +pipi_image_t *pipi_erode(pipi_image_t *src) +{ + pipi_image_t *dst; + pipi_pixels_t *srcp, *dstp; + float *srcdata, *dstdata; + int x, y, w, h, i, gray; + + w = src->w; + h = src->h; + + gray = (src->last_modified == PIPI_PIXELS_Y_F); + + srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F) + : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); + srcdata = (float *)srcp->pixels; + + dst = pipi_new(w, h); + dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) + : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); + dstdata = (float *)dstp->pixels; + + for(y = 0; y < h; y++) + { + for(x = 0; x < w; x++) + { + double t; + int x2, y2, x3, y3; + + y2 = y - 1; + if(y2 < 0) y2 = h - 1; + y3 = y + 1; + if(y3 >= h) y3 = 0; + + x2 = x - 1; + if(x2 < 0) x2 = w - 1; + x3 = x + 1; + if(x3 >= w) x3 = 0; + + if(gray) + { + t = srcdata[y * w + x]; + if(srcdata[y2 * w + x] < t) t = srcdata[y2 * w + x]; + if(srcdata[y3 * w + x] < t) t = srcdata[y3 * w + x]; + if(srcdata[y * w + x2] < t) t = srcdata[y * w + x2]; + if(srcdata[y * w + x3] < t) t = srcdata[y * w + x3]; + dstdata[y * w + x] = t; + } + else + { + for(i = 0; i < 4; i++) + { + t = srcdata[4 * (y * w + x) + i]; + if(srcdata[4 * (y2 * w + x) + i] < t) + t = srcdata[4 * (y2 * w + x) + i]; + if(srcdata[4 * (y3 * w + x) + i] < t) + t = srcdata[4 * (y3 * w + x) + i]; + if(srcdata[4 * (y * w + x2) + i] < t) + t = srcdata[4 * (y * w + x2) + i]; + if(srcdata[4 * (y * w + x3) + i] < t) + t = srcdata[4 * (y * w + x3) + i]; + dstdata[4 * (y * w + x) + i] = t; + } + } + } + } + + return dst; +} + diff --git a/pipi/pipi.h b/pipi/pipi.h index 71496e7..9ce37ba 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -128,6 +128,8 @@ extern pipi_image_t *pipi_autocontrast(pipi_image_t *); extern pipi_image_t *pipi_invert(pipi_image_t *); extern pipi_image_t *pipi_median(pipi_image_t *, int); extern pipi_image_t *pipi_median_ext(pipi_image_t *, int, int); +extern pipi_image_t *pipi_dilate(pipi_image_t *); +extern pipi_image_t *pipi_erode(pipi_image_t *); extern pipi_image_t *pipi_tile(pipi_image_t *, int, int); extern int pipi_flood_fill(pipi_image_t *, diff --git a/src/pipi.c b/src/pipi.c index 81e9cfb..664e9e3 100644 --- a/src/pipi.c +++ b/src/pipi.c @@ -112,6 +112,16 @@ int main(int argc, char *argv[]) if(pipi_command(ctx, "invert") != 0) return EXIT_FAILURE; } + else if(!strcmp(argv[0], "--dilate")) + { + if(pipi_command(ctx, "dilate") != 0) + return EXIT_FAILURE; + } + else if(!strcmp(argv[0], "--erode")) + { + if(pipi_command(ctx, "erode") != 0) + return EXIT_FAILURE; + } else if(!strcmp(argv[0], "--wrap")) { if(pipi_command(ctx, "wrap") != 0)