From 90c7527281aaa2a3b4a05c3380c926ba6b113554 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 23 Aug 2008 13:07:35 +0000 Subject: [PATCH] * Add pipi_dither_ediff(), a generic error diffusion dithering algorithm that uses an image as the error diffusion kernel. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2758 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/Makefile.am | 1 + pipi/context.c | 8 +++++ pipi/dither/ediff.c | 84 +++++++++++++++++++++++++++++++++++++++++++++ pipi/pipi.h | 2 ++ 4 files changed, 95 insertions(+) create mode 100644 pipi/dither/ediff.c diff --git a/pipi/Makefile.am b/pipi/Makefile.am index 7ceb1fd..b332630 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -64,6 +64,7 @@ quantize_sources = \ quantize/reduce.c dither_sources = \ + dither/ediff.c \ dither/floydsteinberg.c \ dither/jajuni.c \ dither/atkinson.c \ diff --git a/pipi/context.c b/pipi/context.c index 0435d06..e480eae 100644 --- a/pipi/context.c +++ b/pipi/context.c @@ -100,6 +100,14 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...) dst = pipi_dither_ostromoukhov(src, 0); else if(!strcmp(method, "sost")) dst = pipi_dither_ostromoukhov(src, 1); + else if(!strcmp(method, "ediff")) + { + if(ctx->nimages < 2) + return -1; + dst = pipi_dither_ediff(ctx->images[ctx->nimages - 2], src, 0); + pipi_free(ctx->images[ctx->nimages - 2]); + ctx->nimages--; + } else if(!strcmp(method, "ordered")) { if(ctx->nimages < 2) diff --git a/pipi/dither/ediff.c b/pipi/dither/ediff.c new file mode 100644 index 0000000..5e5ec08 --- /dev/null +++ b/pipi/dither/ediff.c @@ -0,0 +1,84 @@ +/* + * 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. + */ + +/* + * ed.c: generic error diffusion functions + */ + +#include "config.h" +#include "common.h" + +#include "pipi.h" +#include "pipi_internals.h" + +/* Perform a generic error diffusion dithering. The first non-zero + * element in ker is treated as the current pixel. All other non-zero + * elements are the error diffusion coefficients. */ +pipi_image_t *pipi_dither_ediff(pipi_image_t *img, pipi_image_t *ker, + pipi_scan_t scan) +{ + pipi_image_t *dst; + pipi_pixels_t *dstp, *kerp; + float *dstdata, *kerdata; + int x, y, w, h, i, j, kx, kw, kh; + + w = img->w; + h = img->h; + kw = ker->w; + kh = ker->h; + + dst = pipi_copy(img); + dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F); + dstdata = (float *)dstp->pixels; + + kerp = pipi_getpixels(ker, PIPI_PIXELS_Y_F); + kerdata = (float *)kerp->pixels; + for(kx = 0; kx < kw; kx++) + if(kerdata[kx] > 0) + break; + + for(y = 0; y < h; y++) + { + int reverse = (y & 1) && (scan == PIPI_SCAN_SERPENTINE); + + for(x = 0; x < w; x++) + { + float p, q, e; + int x2 = reverse ? w - 1 - x : x; + int s = reverse ? -1 : 1; + + p = dstdata[y * w + x2]; + q = p < 0.5 ? 0. : 1.; + dstdata[y * w + x2] = q; + + e = (p - q); + + for(j = 0; j < kh && y < h - j; j++) + for(i = 0; i < kw; i++) + { + if(j == 0 && i <= kx) + continue; + + if(x + i - kx < 0 || x + i - kx >= w) + continue; + + dstdata[(y + j) * w + x2 + (i - kx) * s] + += e * kerdata[j * kw + i]; + } + } + } + + return dst; +} + diff --git a/pipi/pipi.h b/pipi/pipi.h index 840b6ab..90a9794 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -147,6 +147,8 @@ extern int pipi_flood_fill(pipi_image_t *, extern pipi_image_t *pipi_reduce(pipi_image_t *, int, double const *); +extern pipi_image_t *pipi_dither_ediff(pipi_image_t *, pipi_image_t *, + pipi_scan_t); extern pipi_image_t *pipi_dither_floydsteinberg(pipi_image_t *, pipi_scan_t); extern pipi_image_t *pipi_dither_jajuni(pipi_image_t *, pipi_scan_t); extern pipi_image_t *pipi_dither_atkinson(pipi_image_t *, pipi_scan_t);