From 0eda956bfc2e1cf84f58d3749a300079698c70ea Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 4 Aug 2008 21:49:57 +0000 Subject: [PATCH] * random.c: implement random dithering using a deterministic pseudo-RNG. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2671 92316355-f0b4-4df1-b90c-862c8a59935f --- examples/dither.c | 26 +++++++++++-------- pipi/Makefile.am | 1 + pipi/dither/random.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ pipi/pipi.h | 1 + 4 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 pipi/dither/random.c diff --git a/examples/dither.c b/examples/dither.c index 2dcbb67..e0c0561 100644 --- a/examples/dither.c +++ b/examples/dither.c @@ -17,11 +17,12 @@ int main(int argc, char *argv[]) fprintf(stderr, "%s: too few arguments\n", argv[0]); fprintf(stderr, "Usage: %s \n", argv[0]); fprintf(stderr, "Where is one of:\n"); - fprintf(stderr, " 1 Floyd-Steinberg (raster)\n"); - fprintf(stderr, " 2 Floyd-Steinberg (serpentine)\n"); - fprintf(stderr, " 3 Ostromoukhov (raster)\n"); - fprintf(stderr, " 4 Ostromoukhov (serpentine)\n"); - fprintf(stderr, " 5 Direct binary search\n"); + fprintf(stderr, " 1 random dithering\n"); + fprintf(stderr, " 2 Floyd-Steinberg (raster)\n"); + fprintf(stderr, " 3 Floyd-Steinberg (serpentine)\n"); + fprintf(stderr, " 4 Ostromoukhov (raster)\n"); + fprintf(stderr, " 5 Ostromoukhov (serpentine)\n"); + fprintf(stderr, " 6 Direct binary search\n"); return EXIT_FAILURE; } @@ -32,24 +33,27 @@ int main(int argc, char *argv[]) switch(atoi(argv[2])) { - case 6: + case 7: newimg = pipi_dither_dbs(img); break; - case 5: + case 6: newimg = pipi_dither_ostromoukhov(img, PIPI_SCAN_SERPENTINE); break; - case 4: + case 5: newimg = pipi_dither_ostromoukhov(img, PIPI_SCAN_RASTER); break; - case 3: + case 4: newimg = pipi_dither_floydsteinberg(img, PIPI_SCAN_SERPENTINE); break; - case 2: + case 3: newimg = pipi_dither_floydsteinberg(img, PIPI_SCAN_RASTER); break; + case 2: + newimg = pipi_dither_ordered(img); + break; case 1: default: - newimg = pipi_dither_ordered(img); + newimg = pipi_dither_random(img); break; } diff --git a/pipi/Makefile.am b/pipi/Makefile.am index 8fdc448..1ef472e 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -34,6 +34,7 @@ libpipi_la_SOURCES = \ dither/ordered.c \ dither/ostromoukhov.c \ dither/dbs.c \ + dither/random.c \ $(NULL) libpipi_la_CFLAGS = $(codec_cflags) libpipi_la_LDFLAGS = $(codec_libs) \ diff --git a/pipi/dither/random.c b/pipi/dither/random.c new file mode 100644 index 0000000..8e9144f --- /dev/null +++ b/pipi/dither/random.c @@ -0,0 +1,61 @@ +/* + * 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. + */ + +/* + * random.c: random dithering functions + */ + +#include "config.h" +#include "common.h" + +#include "pipi.h" +#include "pipi_internals.h" + +pipi_image_t *pipi_dither_random(pipi_image_t *img) +{ + pipi_image_t *dst; + pipi_pixels_t *dstp; + float *dstdata; + unsigned int ctx = 1; + 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++) + { + for(x = 0; x < w; x++) + { + long hi, lo; + float p, q; + + hi = ctx / 12773L; + lo = ctx % 12773L; + ctx = 16807L * lo - 2836L * hi; + if(ctx <= 0) + ctx += 0x7fffffffL; + + p = dstdata[y * w + x]; + q = p > (double)((ctx % 65536) / 65535.) ? 1. : 0.; + dstdata[y * w + x] = q; + } + } + + return dst; +} + diff --git a/pipi/pipi.h b/pipi/pipi.h index cd50168..12c6daf 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -84,6 +84,7 @@ extern pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *, extern pipi_image_t *pipi_dither_floydsteinberg(pipi_image_t *, pipi_scan_t); extern pipi_image_t *pipi_dither_ordered(pipi_image_t *); +extern pipi_image_t *pipi_dither_random(pipi_image_t *); extern pipi_image_t *pipi_dither_ostromoukhov(pipi_image_t *, pipi_scan_t); extern pipi_image_t *pipi_dither_dbs(pipi_image_t *); extern void pipi_dither_24to16(pipi_image_t *);