From a5ceceaf778dffa10a7d0e5c94c81791308f8c22 Mon Sep 17 00:00:00 2001
From: sam <sam@92316355-f0b4-4df1-b90c-862c8a59935f>
Date: Tue, 12 Aug 2008 17:18:34 +0000
Subject: [PATCH]   * stock.c: allow arbitrary sizes for the bayer stock image,
 even if they     give shitty results when used for ordered dithering.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2707 92316355-f0b4-4df1-b90c-862c8a59935f
---
 pipi/stock.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/pipi/stock.c b/pipi/stock.c
index 623a5c1..2ae4863 100644
--- a/pipi/stock.c
+++ b/pipi/stock.c
@@ -35,17 +35,25 @@ pipi_image_t *pipi_load_stock(char const *name)
     /* Generate a Bayer dithering pattern. */
     if(!strncmp(name, "bayer", 5))
     {
-        int i, j, n = atoi(name + 5);
+        int i, j, w, h, n;
 
-        if(n <= 0 || (n & (n - 1))) /* is n a power of two? */
+        w = atoi(name + 5);
+        name = strchr(name + 5, 'x');
+        if(!name)
+            return NULL;
+        h = atoi(name + 1);
+        if(w <= 0 || h <= 0)
             return NULL;
 
-        ret = pipi_new(n, n);
+        for(n = 1; n < w || n < h; n *= 2)
+            ;
+
+        ret = pipi_new(w, h);
         pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
         data = (float *)pix->pixels;
 
-        for(j = 0; j < n; j++)
-            for(i = 0; i < n; i++)
+        for(j = 0; j < h; j++)
+            for(i = 0; i < w; i++)
             {
                 int k, l, x = 0;
 
@@ -59,7 +67,7 @@ pipi_image_t *pipi_load_stock(char const *name)
                         x += 2 * l;
                 }
 
-                data[j * n + i] = (double)(x + 1) / (n * n + 1);
+                data[j * w + i] = (double)(x + 1) / (n * n + 1);
             }
 
         return ret;