From 25cdf35a86d413e52c9d578d1e006bc854acee93 Mon Sep 17 00:00:00 2001
From: sam <sam@92316355-f0b4-4df1-b90c-862c8a59935f>
Date: Mon, 11 Aug 2008 20:02:34 +0000
Subject: [PATCH]   * jajuni.c: add Jarvis-Judice-Ninke dithering. It's a
 Floyd-Steinberg-like     error diffusion method that has fewer high
 resolution structural artifacts     at the expense of resolution.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2700 92316355-f0b4-4df1-b90c-862c8a59935f
---
 pipi/Makefile.am     |  1 +
 pipi/context.c       |  4 ++
 pipi/dither/jajuni.c | 87 ++++++++++++++++++++++++++++++++++++++++++++
 pipi/pipi.h          |  1 +
 4 files changed, 93 insertions(+)
 create mode 100644 pipi/dither/jajuni.c

diff --git a/pipi/Makefile.am b/pipi/Makefile.am
index 5051d02..8c9a0dc 100644
--- a/pipi/Makefile.am
+++ b/pipi/Makefile.am
@@ -45,6 +45,7 @@ filter_sources = \
 
 dither_sources = \
 	dither/floydsteinberg.c \
+	dither/jajuni.c \
 	dither/ordered.c \
 	dither/ostromoukhov.c \
 	dither/dbs.c \
diff --git a/pipi/context.c b/pipi/context.c
index 0c0f6d9..ccfdd18 100644
--- a/pipi/context.c
+++ b/pipi/context.c
@@ -88,6 +88,10 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...)
             dst = pipi_dither_floydsteinberg(src, 0);
         else if(!strcmp(method, "sfs"))
             dst = pipi_dither_floydsteinberg(src, 1);
+        else if(!strcmp(method, "jajuni"))
+            dst = pipi_dither_jajuni(src, 0);
+        else if(!strcmp(method, "sjajuni"))
+            dst = pipi_dither_jajuni(src, 1);
         else if(!strcmp(method, "ost"))
             dst = pipi_dither_ostromoukhov(src, 0);
         else if(!strcmp(method, "sost"))
diff --git a/pipi/dither/jajuni.c b/pipi/dither/jajuni.c
new file mode 100644
index 0000000..45f9aa0
--- /dev/null
+++ b/pipi/dither/jajuni.c
@@ -0,0 +1,87 @@
+/*
+ *  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.
+ */
+
+/*
+ * jajuni.c: Jarvis-Judice-Ninke dithering functions
+ */
+
+#include "config.h"
+#include "common.h"
+
+#include "pipi.h"
+#include "pipi_internals.h"
+
+pipi_image_t *pipi_dither_jajuni(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;
+
+            e = (p - q) / 48.;
+            if(x < w - 1)
+                dstdata[y * w + x2 + s] += e * 7;
+            if(x < w - 2)
+                dstdata[y * w + x2 + s + s] += e * 5;
+            if(y < h - 1)
+            {
+                if(x > 1)
+                    dstdata[(y + 1) * w + x2 - s - s] += e * 3;
+                if(x > 0)
+                    dstdata[(y + 1) * w + x2 - s] += e * 5;
+                dstdata[(y + 1) * w + x2] += e * 7;
+                if(x < w - 1)
+                    dstdata[(y + 1) * w + x2 + s] += e * 5;
+                if(x < w - 2)
+                    dstdata[(y + 1) * w + x2 + s + s] += e * 3;
+            }
+            if(y < h - 2)
+            {
+                if(x > 1)
+                    dstdata[(y + 2) * w + x2 - s - s] += e;
+                if(x > 0)
+                    dstdata[(y + 2) * w + x2 - s] += e * 3;
+                dstdata[(y + 2) * w + x2] += e * 5;
+                if(x < w - 1)
+                    dstdata[(y + 2) * w + x2 + s] += e * 3;
+                if(x < w - 2)
+                    dstdata[(y + 2) * w + x2 + s + s] += e;
+            }
+        }
+    }
+
+    return dst;
+}
+
diff --git a/pipi/pipi.h b/pipi/pipi.h
index 3d74c52..0c734b9 100644
--- a/pipi/pipi.h
+++ b/pipi/pipi.h
@@ -97,6 +97,7 @@ extern int pipi_flood_fill(pipi_image_t *,
                            int, int, float, float, float, float);
 
 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_ordered(pipi_image_t *, 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);