/* * 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. */ /* * floydsteinberg.c: Floyd-Steinberg dithering functions */ #include "config.h" #include "common.h" #include "pipi.h" #include "pipi_internals.h" pipi_image_t *pipi_dither_floydsteinberg(pipi_image_t *img, pipi_scan_t scan) { pipi_image_t *dst; pipi_pixels_t *dstp; float *dstdata; int x, y, w, h; w = img->w; h = img->h; dst = pipi_copy(img); dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F); dstdata = (float *)dstp->pixels; 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; /* FIXME: according to our 2008 paper, [7 4 5 0] is a better * error diffusion kernel for serpentine scan than [7 3 5 1]. */ e = (p - q) / 16; if(x < w - 1) dstdata[y * w + x2 + s] += e * 7; if(y < h - 1) { if(x > 0) dstdata[(y + 1) * w + x2 - s] += e * 3; dstdata[(y + 1) * w + x2] += e * 5; if(x < w - 1) dstdata[(y + 1) * w + x2 + s] += e; } } } return dst; }