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-862c8a59935fremotes/tiles
@@ -64,6 +64,7 @@ quantize_sources = \ | |||||
quantize/reduce.c | quantize/reduce.c | ||||
dither_sources = \ | dither_sources = \ | ||||
dither/ediff.c \ | |||||
dither/floydsteinberg.c \ | dither/floydsteinberg.c \ | ||||
dither/jajuni.c \ | dither/jajuni.c \ | ||||
dither/atkinson.c \ | dither/atkinson.c \ | ||||
@@ -100,6 +100,14 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...) | |||||
dst = pipi_dither_ostromoukhov(src, 0); | dst = pipi_dither_ostromoukhov(src, 0); | ||||
else if(!strcmp(method, "sost")) | else if(!strcmp(method, "sost")) | ||||
dst = pipi_dither_ostromoukhov(src, 1); | 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")) | else if(!strcmp(method, "ordered")) | ||||
{ | { | ||||
if(ctx->nimages < 2) | if(ctx->nimages < 2) | ||||
@@ -0,0 +1,84 @@ | |||||
/* | |||||
* libpipi Proper image processing implementation library | |||||
* Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> | |||||
* 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; | |||||
} | |||||
@@ -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_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_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_jajuni(pipi_image_t *, pipi_scan_t); | ||||
extern pipi_image_t *pipi_dither_atkinson(pipi_image_t *, pipi_scan_t); | extern pipi_image_t *pipi_dither_atkinson(pipi_image_t *, pipi_scan_t); | ||||