From bd54059e606131571869ff336cabd070376c6048 Mon Sep 17 00:00:00 2001
From: sam <sam@92316355-f0b4-4df1-b90c-862c8a59935f>
Date: Sun, 31 Aug 2008 23:05:55 +0000
Subject: [PATCH]   * Add a scale parameter to pipi_dither_ordered_ext().   *
 Reimplement pipi_dither_halftone() using pipi_dither_ordered_ext().

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2812 92316355-f0b4-4df1-b90c-862c8a59935f
---
 pipi/Makefile.am       |  1 -
 pipi/context.c         | 17 ++++++---
 pipi/dither/halftone.c | 83 ------------------------------------------
 pipi/dither/ordered.c  | 21 +++++++++--
 pipi/pipi.h            |  2 +-
 5 files changed, 30 insertions(+), 94 deletions(-)
 delete mode 100644 pipi/dither/halftone.c

diff --git a/pipi/Makefile.am b/pipi/Makefile.am
index aa77553..27a5d1b 100644
--- a/pipi/Makefile.am
+++ b/pipi/Makefile.am
@@ -76,7 +76,6 @@ quantize_sources = \
 dither_sources = \
 	dither/ediff.c \
 	dither/ordered.c \
-	dither/halftone.c \
 	dither/ostromoukhov.c \
 	dither/dbs.c \
 	dither/random.c
diff --git a/pipi/context.c b/pipi/context.c
index ab8a34c..266c282 100644
--- a/pipi/context.c
+++ b/pipi/context.c
@@ -106,14 +106,21 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...)
         }
         else if(!strncmp(method, "ordered", 7))
         {
-            double angle = .0;
-            method = strchr(method, ':');
-            if(method)
-                angle = atof(method + 1);
+            double scale = 1., angle = .0;
             if(ctx->nimages < 2)
                 return -1;
+            method = strchr(method, ':');
+            if(method)
+            {
+                scale = atof(method + 1);
+                method = strchr(method + 1, ':');
+                if(method)
+                    angle = atof(method + 1);
+            }
+            if(scale <= 0.)
+                scale = 1.;
             dst = pipi_dither_ordered_ext(ctx->images[ctx->nimages - 2], src,
-                                          angle);
+                                          scale, angle);
             pipi_free(ctx->images[ctx->nimages - 2]);
             ctx->nimages--;
         }
diff --git a/pipi/dither/halftone.c b/pipi/dither/halftone.c
deleted file mode 100644
index f50580e..0000000
--- a/pipi/dither/halftone.c
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- *  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.
- */
-
-/*
- * halftone.c: screening halftone dithering functions
- */
-
-#include "config.h"
-#include "common.h"
-
-#include <math.h>
-
-#include "pipi.h"
-#include "pipi_internals.h"
-
-pipi_image_t *pipi_dither_halftone(pipi_image_t *img, double r, double angle)
-{
-    double sint, cost;
-    pipi_image_t *dst;
-    pipi_pixels_t *dstp;
-    float *dstdata;
-    int x, y, w, h;
-
-    w = img->w;
-    h = img->h;
-
-    cost = cos(angle * (M_PI / 180));
-    sint = sin(angle * (M_PI / 180));
-
-    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++)
-        {
-            double x2, y2, i, j, dist;
-            float p, threshold = .5;
-            int invert = 0;
-
-            x2 = x + .5;
-            y2 = y + .5;
-            i = (cost * x2 - sint * y2) / r;
-            j = (cost * y2 + sint * x2) / r;
-            i = i - (int)i + (i < 0.);
-            j = j - (int)j + (j < 0.);
-
-            if(j > 0.5)
-	    {
-                i = 1. - i;
-                j = 1. - j;
-            }
-            if(i > 0.5)
-            {
-                invert = 1;
-                i = 1. - i;
-            }
-
-            dist = (i - .25) * (i - .25) + (j - .25) * (j - .25);
-            threshold = dist * 4;
-            if(invert)
-                threshold = 1. - threshold;
-
-            p = dstdata[y * w + x];
-            dstdata[y * w + x] = p > threshold ? 1. : 0.;
-        }
-    }
-
-    return dst;
-}
-
diff --git a/pipi/dither/ordered.c b/pipi/dither/ordered.c
index 34a6d2d..f91a722 100644
--- a/pipi/dither/ordered.c
+++ b/pipi/dither/ordered.c
@@ -25,13 +25,26 @@
 #include "pipi.h"
 #include "pipi_internals.h"
 
+pipi_image_t *pipi_dither_halftone(pipi_image_t *img, double r, double angle)
+{
+#define PRECISION 4.
+    pipi_image_t *ret, *kernel;
+    int k = (r * PRECISION / sqrt(2.) + .5);
+
+    kernel = pipi_render_halftone(k, k);
+    ret = pipi_dither_ordered_ext(img, kernel, 1. / PRECISION, angle + 45.);
+    pipi_free(kernel);
+
+    return ret;
+}
+
 pipi_image_t *pipi_dither_ordered(pipi_image_t *img, pipi_image_t *kernel)
 {
-    return pipi_dither_ordered_ext(img, kernel, 0.0);
+    return pipi_dither_ordered_ext(img, kernel, 1.0, 0.0);
 }
 
 pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *img, pipi_image_t *kernel,
-                                      double angle)
+                                      double scale, double angle)
 {
     double sint, cost;
     pipi_image_t *dst;
@@ -60,8 +73,8 @@ pipi_image_t *pipi_dither_ordered_ext(pipi_image_t *img, pipi_image_t *kernel,
         {
             float p, q;
 
-            kx = (int)(cost * x - sint * y + 2 * w * h) % kw;
-            ky = (int)(cost * y + sint * x + 2 * w * h) % kh;
+            kx = (int)((cost * x - sint * y + 2 * w * h) / scale) % kw;
+            ky = (int)((cost * y + sint * x + 2 * w * h) / scale) % kh;
 
             p = dstdata[y * w + x];
             q = p > kerneldata[ky * kw + kx] ? 1. : 0.;
diff --git a/pipi/pipi.h b/pipi/pipi.h
index 6114a2e..35abd57 100644
--- a/pipi/pipi.h
+++ b/pipi/pipi.h
@@ -183,7 +183,7 @@ extern pipi_image_t *pipi_dither_ediff(pipi_image_t *, 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_ordered_ext(pipi_image_t *, pipi_image_t *,
-                                             double);
+                                             double, double);
 extern pipi_image_t *pipi_dither_halftone(pipi_image_t *, double, double);
 extern pipi_image_t *pipi_dither_random(pipi_image_t *);
 extern pipi_image_t *pipi_dither_ostromoukhov(pipi_image_t *, pipi_scan_t);