From 2fc2c19493f1667526a64138a639ac0a9680887d Mon Sep 17 00:00:00 2001
From: Thomas Oster <thomas.oster@rwth-aachen.de>
Date: Mon, 5 Nov 2012 12:36:38 +0100
Subject: [PATCH] Dithering algorithms don't have internal variables for source
 and target, but they are provided as method arguments

---
 .../liblasercut/dithering/Average.java        | 19 +++++-----
 .../dithering/DitheringAlgorithm.java         | 36 ++++++++-----------
 .../liblasercut/dithering/FloydSteinberg.java | 15 ++++----
 .../t_oster/liblasercut/dithering/Grid.java   | 19 +++++-----
 .../liblasercut/dithering/Ordered.java        | 19 +++++-----
 .../t_oster/liblasercut/dithering/Random.java | 15 ++++----
 6 files changed, 65 insertions(+), 58 deletions(-)

diff --git a/src/com/t_oster/liblasercut/dithering/Average.java b/src/com/t_oster/liblasercut/dithering/Average.java
index 3242d3b..1a9adc2 100644
--- a/src/com/t_oster/liblasercut/dithering/Average.java
+++ b/src/com/t_oster/liblasercut/dithering/Average.java
@@ -2,31 +2,34 @@
  * This file is part of VisiCut.
  * Copyright (C) 2012 Thomas Oster <thomas.oster@rwth-aachen.de>
  * RWTH Aachen University - 52062 Aachen, Germany
- * 
+ *
  *     VisiCut is free software: you can redistribute it and/or modify
  *     it under the terms of the GNU Lesser General Public License as published by
  *     the Free Software Foundation, either version 3 of the License, or
  *     (at your option) any later version.
- * 
+ *
  *    VisiCut is distributed in the hope that it will be useful,
  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *     GNU Lesser General Public License for more details.
- * 
+ *
  *     You should have received a copy of the GNU Lesser General Public License
  *     along with VisiCut.  If not, see <http://www.gnu.org/licenses/>.
  **/
 package com.t_oster.liblasercut.dithering;
 
+import com.t_oster.liblasercut.BlackWhiteRaster;
+import com.t_oster.liblasercut.GreyscaleRaster;
+
 /**
  *
  * @author Thomas Oster <thomas.oster@rwth-aachen.de>
  */
 public class Average extends  DitheringAlgorithm
 {
-  
-    @Override
-  protected void doDithering()
+
+  @Override
+  protected void doDithering(GreyscaleRaster src, BlackWhiteRaster target)
   {
     long lumTotal = 0;
     int pixelcount = 0;
@@ -47,7 +50,7 @@ public class Average extends  DitheringAlgorithm
     {
       for (int x = 0; x < width; x++)
       {
-        this.setBlack(x, y, src.getGreyScale(x, y) < thresh);
+        this.setBlack(src, target, x, y, src.getGreyScale(x, y) < thresh);
       }
       setProgress((100 * pixelcount++) / (2 * height));
     }
@@ -57,7 +60,7 @@ public class Average extends  DitheringAlgorithm
   public DitheringAlgorithm clone() {
     return new Average();
   }
-  
+
   @Override
   public String toString()
   {
diff --git a/src/com/t_oster/liblasercut/dithering/DitheringAlgorithm.java b/src/com/t_oster/liblasercut/dithering/DitheringAlgorithm.java
index 54c7d21..c266f96 100644
--- a/src/com/t_oster/liblasercut/dithering/DitheringAlgorithm.java
+++ b/src/com/t_oster/liblasercut/dithering/DitheringAlgorithm.java
@@ -2,17 +2,17 @@
  * This file is part of VisiCut.
  * Copyright (C) 2012 Thomas Oster <thomas.oster@rwth-aachen.de>
  * RWTH Aachen University - 52062 Aachen, Germany
- * 
+ *
  *     VisiCut is free software: you can redistribute it and/or modify
  *     it under the terms of the GNU Lesser General Public License as published by
  *     the Free Software Foundation, either version 3 of the License, or
  *     (at your option) any later version.
- * 
+ *
  *    VisiCut is distributed in the hope that it will be useful,
  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *     GNU Lesser General Public License for more details.
- * 
+ *
  *     You should have received a copy of the GNU Lesser General Public License
  *     along with VisiCut.  If not, see <http://www.gnu.org/licenses/>.
  **/
@@ -29,11 +29,8 @@ import com.t_oster.liblasercut.TimeIntensiveOperation;
  */
 public abstract class DitheringAlgorithm extends TimeIntensiveOperation implements Customizable, Cloneable
 {
-  
-  protected GreyscaleRaster src;
-  protected BlackWhiteRaster target;
 
-  protected void setBlack(int x, int y, boolean black)
+  protected void setBlack(GreyscaleRaster src, BlackWhiteRaster target, int x, int y, boolean black)
   {
     if (target != null)
     {
@@ -47,28 +44,23 @@ public abstract class DitheringAlgorithm extends TimeIntensiveOperation implemen
 
   public BlackWhiteRaster dither(GreyscaleRaster input)
   {
-    src = input;
-    target = new BlackWhiteRaster(input.getWidth(), input.getHeight());
-    doDithering();
+    BlackWhiteRaster target = new BlackWhiteRaster(input.getWidth(), input.getHeight());
+    doDithering(input, target);
     return target;
   }
 
   public void ditherDirect(GreyscaleRaster input)
   {
-    src = input;
-    target = null;
-    doDithering();
+    doDithering(input, null);
   }
-  
+
   public void ditherDirect(GreyscaleRaster input, BlackWhiteRaster output)
   {
-    src = input;
-    target = output;
-    doDithering();
+    doDithering(input, output);
   }
-  
-  protected abstract void doDithering();
-  
+
+  protected abstract void doDithering(GreyscaleRaster src, BlackWhiteRaster target);
+
   @Override
   public String[] getPropertyKeys() {
     return new String[0];
@@ -82,10 +74,10 @@ public abstract class DitheringAlgorithm extends TimeIntensiveOperation implemen
   public Object getProperty(String key) {
     return null;
   }
-  
+
   @Override
   public abstract DitheringAlgorithm clone();
-  
+
   @Override
   public abstract String toString();
 }
diff --git a/src/com/t_oster/liblasercut/dithering/FloydSteinberg.java b/src/com/t_oster/liblasercut/dithering/FloydSteinberg.java
index cc742fe..538553b 100644
--- a/src/com/t_oster/liblasercut/dithering/FloydSteinberg.java
+++ b/src/com/t_oster/liblasercut/dithering/FloydSteinberg.java
@@ -2,22 +2,25 @@
  * This file is part of VisiCut.
  * Copyright (C) 2012 Thomas Oster <thomas.oster@rwth-aachen.de>
  * RWTH Aachen University - 52062 Aachen, Germany
- * 
+ *
  *     VisiCut is free software: you can redistribute it and/or modify
  *     it under the terms of the GNU Lesser General Public License as published by
  *     the Free Software Foundation, either version 3 of the License, or
  *     (at your option) any later version.
- * 
+ *
  *    VisiCut is distributed in the hope that it will be useful,
  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *     GNU Lesser General Public License for more details.
- * 
+ *
  *     You should have received a copy of the GNU Lesser General Public License
  *     along with VisiCut.  If not, see <http://www.gnu.org/licenses/>.
  **/
 package com.t_oster.liblasercut.dithering;
 
+import com.t_oster.liblasercut.BlackWhiteRaster;
+import com.t_oster.liblasercut.GreyscaleRaster;
+
 /**
  *
  * @author Thomas Oster <thomas.oster@rwth-aachen.de>
@@ -26,7 +29,7 @@ public class FloydSteinberg extends DitheringAlgorithm
 {
 
   @Override
-  protected void doDithering()
+  protected void doDithering(GreyscaleRaster src, BlackWhiteRaster target)
   {
     int pixelcount = 0;
     /**
@@ -54,7 +57,7 @@ public class FloydSteinberg extends DitheringAlgorithm
 
       for (int x = 0; x < input.length; x++)
       {
-        this.setBlack(x, y, input[x][0] <= 127);
+        this.setBlack(src, target, x, y, input[x][0] <= 127);
         int error = input[x][0] - ((input[x][0] <= 127) ? 0 : 255);
         if (x + 1 < input.length)
         {
@@ -81,7 +84,7 @@ public class FloydSteinberg extends DitheringAlgorithm
   public DitheringAlgorithm clone() {
     return new FloydSteinberg();
   }
-  
+
   @Override
   public String toString()
   {
diff --git a/src/com/t_oster/liblasercut/dithering/Grid.java b/src/com/t_oster/liblasercut/dithering/Grid.java
index 87bee48..b6d6968 100644
--- a/src/com/t_oster/liblasercut/dithering/Grid.java
+++ b/src/com/t_oster/liblasercut/dithering/Grid.java
@@ -18,6 +18,9 @@
  */
 package com.t_oster.liblasercut.dithering;
 
+import com.t_oster.liblasercut.BlackWhiteRaster;
+import com.t_oster.liblasercut.GreyscaleRaster;
+
 /**
  *
  * @author Thomas Oster <thomas.oster@rwth-aachen.de>
@@ -26,13 +29,13 @@ public class Grid extends DitheringAlgorithm
 {
 
   private static String[] properties = new String[]{"Blocksize", "Blockdistance"};
-  
+
   @Override
   public String[] getPropertyKeys()
   {
     return properties;
   }
-  
+
   @Override
   public void setProperty(String key, Object value)
   {
@@ -49,7 +52,7 @@ public class Grid extends DitheringAlgorithm
       throw new IllegalArgumentException("No such key "+key);
     }
   }
-  
+
   @Override
   public Object getProperty(String key)
   {
@@ -63,12 +66,12 @@ public class Grid extends DitheringAlgorithm
     }
     throw new IllegalArgumentException("No such key "+key);
   }
-  
+
   protected int blocksize = 10;
   protected int blockdistance = 5;
 
   @Override
-  protected void doDithering()
+  protected void doDithering(GreyscaleRaster src, BlackWhiteRaster target)
   {
     long lumTotal = 0;
     int pixelcount = 0;
@@ -93,11 +96,11 @@ public class Grid extends DitheringAlgorithm
           && x % (blocksize + blockdistance) <= blocksize
           && src.getGreyScale(x, y) < thresh)
         {
-          this.setBlack(x, y, true);
+          this.setBlack(src, target, x, y, true);
         }
         else
         {
-          this.setBlack(x, y, false);
+          this.setBlack(src, target, x, y, false);
         }
       }
       setProgress((100 * pixelcount++) / (2 * height));
@@ -111,7 +114,7 @@ public class Grid extends DitheringAlgorithm
     clone.blocksize = blocksize;
     return clone;
   }
-  
+
   @Override
   public String toString()
   {
diff --git a/src/com/t_oster/liblasercut/dithering/Ordered.java b/src/com/t_oster/liblasercut/dithering/Ordered.java
index 6ebd641..9c6b7c1 100644
--- a/src/com/t_oster/liblasercut/dithering/Ordered.java
+++ b/src/com/t_oster/liblasercut/dithering/Ordered.java
@@ -2,22 +2,25 @@
  * This file is part of VisiCut.
  * Copyright (C) 2012 Thomas Oster <thomas.oster@rwth-aachen.de>
  * RWTH Aachen University - 52062 Aachen, Germany
- * 
+ *
  *     VisiCut is free software: you can redistribute it and/or modify
  *     it under the terms of the GNU Lesser General Public License as published by
  *     the Free Software Foundation, either version 3 of the License, or
  *     (at your option) any later version.
- * 
+ *
  *    VisiCut is distributed in the hope that it will be useful,
  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *     GNU Lesser General Public License for more details.
- * 
+ *
  *     You should have received a copy of the GNU Lesser General Public License
  *     along with VisiCut.  If not, see <http://www.gnu.org/licenses/>.
  **/
 package com.t_oster.liblasercut.dithering;
 
+import com.t_oster.liblasercut.BlackWhiteRaster;
+import com.t_oster.liblasercut.GreyscaleRaster;
+
 /**
  *
  * @author Thomas Oster <thomas.oster@rwth-aachen.de>
@@ -26,7 +29,7 @@ public class Ordered extends DitheringAlgorithm
 {
 
   @Override
-  protected void doDithering()
+  protected void doDithering(GreyscaleRaster src, BlackWhiteRaster target)
   {
     int width = src.getWidth();
     int height = src.getHeight();
@@ -61,7 +64,7 @@ public class Ordered extends DitheringAlgorithm
         {
           for (int ydelta = 0; ydelta < nPatWid; ydelta++)
           {
-            this.setBlack(x + xdelta, y + ydelta, src.getGreyScale(x + xdelta, y + ydelta) < filter[xdelta][ydelta]);
+            this.setBlack(src, target, x + xdelta, y + ydelta, src.getGreyScale(x + xdelta, y + ydelta) < filter[xdelta][ydelta]);
           }
         }
       }
@@ -72,7 +75,7 @@ public class Ordered extends DitheringAlgorithm
 
           if (((x + xdelta) < width) && ((y + ydelta) < height))
           {
-            this.setBlack(x + xdelta, y + ydelta, src.getGreyScale(x + xdelta, y + ydelta) < filter[xdelta][ydelta]);
+            this.setBlack(src, target, x + xdelta, y + ydelta, src.getGreyScale(x + xdelta, y + ydelta) < filter[xdelta][ydelta]);
           }
         }
       }
@@ -89,7 +92,7 @@ public class Ordered extends DitheringAlgorithm
 
           if (((x + xdelta) < width) && ((y + ydelta) < height))
           {
-            this.setBlack(x + xdelta, y + ydelta, src.getGreyScale(x + xdelta, y + ydelta) < filter[xdelta][ydelta]);
+            this.setBlack(src, target, x + xdelta, y + ydelta, src.getGreyScale(x + xdelta, y + ydelta) < filter[xdelta][ydelta]);
           }
         }
       }
@@ -100,7 +103,7 @@ public class Ordered extends DitheringAlgorithm
   public DitheringAlgorithm clone() {
     return new Ordered();
   }
-  
+
   @Override
   public String toString()
   {
diff --git a/src/com/t_oster/liblasercut/dithering/Random.java b/src/com/t_oster/liblasercut/dithering/Random.java
index 5601919..b7fa034 100644
--- a/src/com/t_oster/liblasercut/dithering/Random.java
+++ b/src/com/t_oster/liblasercut/dithering/Random.java
@@ -2,22 +2,25 @@
  * This file is part of VisiCut.
  * Copyright (C) 2012 Thomas Oster <thomas.oster@rwth-aachen.de>
  * RWTH Aachen University - 52062 Aachen, Germany
- * 
+ *
  *     VisiCut is free software: you can redistribute it and/or modify
  *     it under the terms of the GNU Lesser General Public License as published by
  *     the Free Software Foundation, either version 3 of the License, or
  *     (at your option) any later version.
- * 
+ *
  *    VisiCut is distributed in the hope that it will be useful,
  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *     GNU Lesser General Public License for more details.
- * 
+ *
  *     You should have received a copy of the GNU Lesser General Public License
  *     along with VisiCut.  If not, see <http://www.gnu.org/licenses/>.
  **/
 package com.t_oster.liblasercut.dithering;
 
+import com.t_oster.liblasercut.BlackWhiteRaster;
+import com.t_oster.liblasercut.GreyscaleRaster;
+
 /**
  *
  * @author Thomas Oster <thomas.oster@rwth-aachen.de>
@@ -26,7 +29,7 @@ public class Random extends DitheringAlgorithm
 {
 
   @Override
-  protected void doDithering()
+  protected void doDithering(GreyscaleRaster src, BlackWhiteRaster target)
   {
     int width = src.getWidth();
     int height = src.getHeight();
@@ -37,7 +40,7 @@ public class Random extends DitheringAlgorithm
     {
       for (int x = 0; x < width; x++)
       {
-        this.setBlack(x, y, src.getGreyScale(x, y) < r.nextInt(256));
+        this.setBlack(src, target, x, y, src.getGreyScale(x, y) < r.nextInt(256));
       }
       setProgress((100 * pixelcount++) / (height));
     }
@@ -47,7 +50,7 @@ public class Random extends DitheringAlgorithm
   public DitheringAlgorithm clone() {
     return new Random();
   }
-  
+
   @Override
   public String toString()
   {
-- 
GitLab