From 6a3309663e4c02afcef25db80e912d95593d51bc Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 4 Aug 2008 17:23:38 +0000 Subject: [PATCH] * ordered.c: implement Bayer dithering (pretty trivial). git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2665 92316355-f0b4-4df1-b90c-862c8a59935f --- examples/dither.c | 12 ++++---- pipi/Makefile.am | 1 + pipi/dither/ordered.c | 68 +++++++++++++++++++++++++++++++++++++++++++ pipi/pipi.h | 1 + 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 pipi/dither/ordered.c diff --git a/examples/dither.c b/examples/dither.c index 7bdd3e6..ece8206 100644 --- a/examples/dither.c +++ b/examples/dither.c @@ -32,17 +32,19 @@ int main(int argc, char *argv[]) switch(atoi(argv[2])) { - case 5: + case 6: newimg = pipi_dbs(img); break; - case 4: + case 5: newimg = pipi_ostromoukhov(img, PIPI_SCAN_SERPENTINE); break; - case 3: + case 4: newimg = pipi_ostromoukhov(img, PIPI_SCAN_RASTER); break; - case 2: + case 3: newimg = pipi_floydsteinberg(img, PIPI_SCAN_SERPENTINE); break; + case 2: + newimg = pipi_floydsteinberg(img, PIPI_SCAN_RASTER); break; case 1: default: - newimg = pipi_floydsteinberg(img, PIPI_SCAN_RASTER); break; + newimg = pipi_dither_ordered(img); break; } pipi_free(img); diff --git a/pipi/Makefile.am b/pipi/Makefile.am index 7c4cbb8..8fdc448 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -31,6 +31,7 @@ libpipi_la_SOURCES = \ filter/blur.c \ filter/convolution.c \ dither/floydsteinberg.c \ + dither/ordered.c \ dither/ostromoukhov.c \ dither/dbs.c \ $(NULL) diff --git a/pipi/dither/ordered.c b/pipi/dither/ordered.c new file mode 100644 index 0000000..def6508 --- /dev/null +++ b/pipi/dither/ordered.c @@ -0,0 +1,68 @@ +/* + * 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. + */ + +/* + * ordered.c: Bayer ordered dithering functions + */ + +#include "config.h" +#include "common.h" + +#include "pipi.h" +#include "pipi_internals.h" + +static const int kernel8x8[8 * 8] = +{ + 0, 32, 8, 40, 2, 34, 10, 42, + 48, 16, 56, 24, 50, 18, 58, 26, + 12, 44, 4, 36, 14, 46, 6, 38, + 60, 28, 52, 20, 62, 30, 54, 22, + 3, 35, 11, 43, 1, 33, 9, 41, + 51, 19, 59, 27, 49, 17, 57, 25, + 15, 47, 7, 39, 13, 45, 5, 37, + 63, 31, 55, 23, 61, 29, 53, 21, +}; + +pipi_image_t *pipi_dither_ordered(pipi_image_t *src) +{ + pipi_image_t *dst; + pipi_pixels_t *srcp, *dstp; + float *srcdata, *dstdata; + int x, y, w, h; + + w = src->w; + h = src->h; + + srcp = pipi_getpixels(src, PIPI_PIXELS_Y_F); + srcdata = (float *)srcp->pixels; + + dst = pipi_new(w, h); + dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F); + dstdata = (float *)dstp->pixels; + + for(y = 0; y < h; y++) + { + for(x = 0; x < w; x++) + { + float p, q; + + p = srcdata[y * w + x]; + q = p > (1. + kernel8x8[(y % 8) * 8 + (x % 8)]) / 65. ? 1. : 0.; + dstdata[y * w + x] = q; + } + } + + return dst; +} + diff --git a/pipi/pipi.h b/pipi/pipi.h index 1db03dd..2d219e1 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -79,6 +79,7 @@ extern pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *, float, float, float, float); extern pipi_image_t *pipi_floydsteinberg(pipi_image_t *, pipi_scan_t); +extern pipi_image_t *pipi_dither_ordered(pipi_image_t *); extern pipi_image_t *pipi_ostromoukhov(pipi_image_t *, pipi_scan_t); extern pipi_image_t *pipi_dbs(pipi_image_t *); extern void pipi_dither_24to16(pipi_image_t *);