changeset 231:a53b3fe97412

Clean up the quantization code slightly.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 06 Sep 2018 01:15:29 +0300
parents 2d2e313882b4
children ea6eb6541607
files exporters.pde
diffstat 1 files changed, 41 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/exporters.pde	Wed Sep 05 22:36:20 2018 +0300
+++ b/exporters.pde	Thu Sep 06 01:15:29 2018 +0300
@@ -1113,6 +1113,26 @@
 }
 
 
+color mpFoobarBaz(PImage image, int xc, int yc, int bwidth, int bheight)
+{
+    int xoffs = int(image.width / X) * xc,
+        yoffs = int(image.height / Y) * yc,
+        rr = 0, gg = 0, bb = 0, avg = 0;
+
+    for (int dy = 0; dy < bheight; dy++)
+    for (int dx = 0; dx < bwidth; dx++)
+    {
+        color c = image.get(xoffs + dx, yoffs + dy);
+        rr += int(red(c));
+        gg += int(green(c));
+        bb += int(blue(c));
+        avg++;
+    }
+
+    return color(int(rr / avg), int(gg / avg), int(bb / avg));
+}
+
+
 boolean mpImportFromImage(PImage image)
 {
     if (image == null)
@@ -1174,28 +1194,12 @@
         for (cy = 0; cy < MY * erkki; cy++)
         for (cx = 0; cx < MX; cx++)
         for (yy = cy * vertti; yy <= cy * vertti + vertti - 1; yy++)
-        for (xx = cx * 8; xx <= cx * 8 + 7; xx = xx + g_pixelw)
+        for (xx = cx * 8; xx <= cx * 8 + 7; xx += g_pixelw)
         {
-            x2 = int(image.width / X) * xx;
-            y2 = int(image.height / Y) * yy;
-            rr = 0;
-            gg = 0;
-            bb = 0;
-            avg = 0;
-
-            for (avy = 0; avy < baly; avy++)
-            for (avx = 0; avx < balx; avx++)
-            {
-                c = image.get(x2 + avx, y2 + avy);
-                rr = rr + int(red(c));
-                gg = gg + int(green(c));
-                bb = bb + int(blue(c));
-                avg++;
-            }
-
-            rh = int(int(rr / avg) / palls);
-            gh = int(int(gg / avg) / palls);
-            bh = int(int(bb / avg) / palls);
+            color hvals = mpFoobarBaz(image, xx, yy, balx, baly);
+            rh = int(red(hvals) / palls);
+            gh = int(green(hvals) / palls);
+            bh = int(blue(hvals) / palls);
 
             int hindex = int(rh * (g_palsteps * g_palsteps) + gh * (g_palsteps) + bh);
             if (++histog[hindex] > maxhis)
@@ -1208,14 +1212,10 @@
         {
             if (histog[i] == j)
             {
-                rh = i / int(g_palsteps * g_palsteps);
-                gh = i - int(rh * (g_palsteps * g_palsteps));
-                gh = gh / int(g_palsteps);
-                bh = i - int(rh * (g_palsteps * g_palsteps));
-                bh = bh - int(gh * g_palsteps);
-                rh = rh * int(palls);
-                gh = gh * int(palls);
-                bh = bh * int(palls);
+                rh = int(i / (g_palsteps * g_palsteps)) * palls;
+                gh = int((i - (rh * g_palsteps * g_palsteps)) / g_palsteps) * palls;
+                bh = ((i - (rh * g_palsteps * g_palsteps)) - int(gh * g_palsteps)) * palls;
+
                 if (step < g_maxcolors)
                 {
                     makecolor(step, rh, gh, bh);
@@ -1234,28 +1234,12 @@
             idx[i] = i;
         }
         for (yy = cy * vertti; yy <= cy * vertti + vertti - 1; yy++)
-        for (xx = cx * 8; xx <= cx * 8 + 7; xx = xx + g_pixelw)
+        for (xx = cx * 8; xx <= cx * 8 + 7; xx += g_pixelw)
         {
-            x2 = int(image.width / X) * xx;
-            y2 = int(image.height / Y) * yy;
-            rr = 0;
-            gg = 0;
-            bb = 0;
-            avg = 0;
-
-            for (avy = 0; avy < baly; avy++)
-            for (avx = 0; avx < balx; avx++)
-            {
-                c = image.get(x2 + avx, y2 + avy);
-                rr += int(red(c));
-                gg += int(green(c));
-                bb += int(blue(c));
-                avg++;
-            }
-
-            rr = int(rr / avg);
-            gg = int(gg / avg);
-            bb = int(bb / avg);
+            color hvals = mpFoobarBaz(image, xx, yy, balx, baly);
+            rr = int(red(hvals));
+            gg = int(green(hvals));
+            bb = int(blue(hvals));
 
             int target = -1;
             float compa = 9999;
@@ -1292,28 +1276,12 @@
             i = idx[idefix];
 
             for (yy = cy * vertti; yy <= cy * vertti + vertti - 1; yy++)
-            for (xx = cx * 8; xx <= cx * 8 + 7; xx = xx + g_pixelw)
+            for (xx = cx * 8; xx <= cx * 8 + 7; xx += g_pixelw)
             {
-                x2 = int(image.width / X) * xx;
-                y2 = int(image.height / Y) * yy;
-
-                rr = 0;
-                gg = 0;
-                bb = 0;
-                avg = 0;
-                for (avy = 0; avy < baly; avy++)
-                for (avx = 0; avx < balx; avx++)
-                {
-                    c = image.get(x2 + avx, y2 + avy);
-                    rr += int(red(c));
-                    gg += int(green(c));
-                    bb += int(blue(c));
-                    avg++;
-                }
-
-                rr = int(rr / avg);
-                gg = int(gg / avg);
-                bb = int(bb / avg);
+                color hvals = mpFoobarBaz(image, xx, yy, balx, baly);
+                rr = int(red(hvals));
+                gg = int(green(hvals));
+                bb = int(blue(hvals));
 
                 int target = -1;
                 float compa = 9999;
@@ -1338,7 +1306,7 @@
             if (idefix == 0)
             {
                 for (yy = cy * vertti; yy <= cy * vertti + vertti - 1; yy++)
-                for (xx = cx * 8; xx <= cx * 8 + 7; xx = xx + g_pixelw)
+                for (xx = cx * 8; xx <= cx * 8 + 7; xx += g_pixelw)
                 {
                     g_farge = idx[0];
                     makepoint(xx, yy);
@@ -1346,6 +1314,7 @@
             }
         }
     }
+
     return true;
 }