Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Stucki.cxx

Go to the documentation of this file.
00001 /*
00002  * @(#)src/Stucki.cxx  1.0  2002-08-27 16:24
00003  *
00004  * Copyright (C)  2002  Daniel Léonard
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  */
00020 
00026 #include "Stucki.h"
00027 
00028 namespace halftoner
00029 {
00030 
00031 //---------------------------
00032 // Constructor
00033 //---------------------------
00034 
00035    Stucki::Stucki()
00036    {
00037    // nothing
00038    }
00039 
00040 
00041 //---------------------------
00042 // Destructor
00043 //---------------------------
00044 
00045    Stucki::~Stucki()
00046    {
00047    // nothing
00048    }
00049 
00050 
00051 //---------------------------
00052 // Implemented methods from ErrorDiffusionHalftoner
00053 //---------------------------
00054 
00055    int Stucki::halftoneToRight(float* source, int i, int j, int offset, int width, int height, int threshold) throw()
00056    {
00057       float grey = source[offset];
00058       int bw = (grey < threshold) ? 0 : 1;
00059       float diff = grey - 255.0f * bw;
00060 
00061    // first diffusion ( (x+1,y) -> 7)
00062       bool one_on_right = (i + 1) < width;
00063       if (one_on_right)
00064       {
00065          float* pixel = source + offset + 1;
00066          *pixel += diff * (8.0f / 42.0f);
00067       }
00068    // second diffusion ( (x+2,y) -> 5)
00069       bool two_on_right = (i + 2) < width;
00070       if (two_on_right)
00071       {
00072          float* pixel = source + offset + 2;
00073          *pixel += diff * (4.0f / 42.0f);
00074       }
00075       if (j + 1 < height)
00076       {
00077          float* pixel = source + offset + width - 2;
00078       // third diffusion ( (x-2,y+1) -> 3)
00079          if ((i - 2) >= 0)
00080          {
00081             *pixel += diff * (2.0f / 42.0f);
00082          }
00083          pixel++;
00084 
00085       // fourth diffusion ( (x-1,y+1) -> 5)
00086          if (i - 1 >= 0)
00087          {
00088             *pixel += diff * (4.0f / 42.0f);
00089          }
00090          pixel++;
00091 
00092       // fifth diffusion ( (x,y+1) -> 7)
00093          *pixel += diff * (8.0f / 42.0f);
00094 
00095       // sixth diffusion ( (x+1,y+1) -> 5)
00096          if (one_on_right)
00097          {
00098             pixel++;
00099             *pixel += diff * (4.0f / 42.0f);
00100          }
00101 
00102       // seventh diffusion ( (x+2,y+1) -> 3)
00103          if (two_on_right)
00104          {
00105             pixel++;
00106             *pixel += diff * (2.0f / 42.0f);
00107          }
00108       }
00109       if (j + 2 < height)
00110       {
00111          float* pixel = source + offset + width + width - 2;
00112       // eightth diffusion ( (x-2,y+2) -> 1)
00113          if (i - 2 >= 0)
00114          {
00115             *pixel += diff * (1.0f / 42.0f);
00116          }
00117          pixel++;
00118 
00119       // ninth diffusion ( (x-1,y+2) -> 3)
00120          if (i - 1 >= 0)
00121          {
00122             *pixel += diff * (2.0f / 42.0f);
00123          }
00124          pixel++;
00125 
00126       // tenth diffusion ( (x,y+2) -> 5)
00127          *pixel += diff * (4.0f / 42.0f);
00128 
00129       // eleventh diffusion ( (x+1,y+2) -> 3)
00130          if (one_on_right)
00131          {
00132             pixel++;
00133             *pixel += diff * (2.0f / 42.0f);
00134          }
00135       // twelfth diffusion ( (x+2,y+2) -> 1)
00136          if (two_on_right)
00137          {
00138             pixel++;
00139             *pixel += diff * (1.0f / 42.0f);
00140          }
00141       }
00142 
00143       return bw;
00144    }
00145 
00146    int Stucki::halftoneToLeft(float* source, int i, int j, int offset, int width, int height, int threshold) throw()
00147    {
00148       float grey = source[offset];
00149       int bw = (grey < threshold) ? 0 : 1;
00150       float diff = grey - 255.0f * bw;
00151 
00152    // first diffusion ( (x+1,y) -> 7)
00153       bool one_on_left = (i - 1) >= 0;
00154       if (one_on_left)
00155       {
00156          float* pixel = source + offset - 1;
00157          *pixel += diff * (8.0f / 42.0f);
00158       }
00159    // second diffusion ( (x+2,y) -> 5)
00160       bool two_on_left = (i - 2) >= 0;
00161       if (two_on_left)
00162       {
00163          float* pixel = source + offset - 2;
00164          *pixel += diff * (4.0f / 42.0f);
00165       }
00166       if (j + 1 < height)
00167       {
00168          float* pixel = source + offset + width + 2;
00169       // third diffusion ( (x-2,y+1) -> 3)
00170          if ((i + 2) < width)
00171          {
00172             *pixel += diff * (2.0f / 42.0f);
00173          }
00174          pixel--;
00175 
00176       // fourth diffusion ( (x-1,y+1) -> 5)
00177          if ((i + 1) < width)
00178          {
00179             *pixel += diff * (4.0f / 42.0f);
00180          }
00181          pixel--;
00182 
00183       // fifth diffusion ( (x,y+1) -> 7)
00184          *pixel += diff * (8.0f / 42.0f);
00185 
00186       // sixth diffusion ( (x+1,y+1) -> 5)
00187          if (one_on_left)
00188          {
00189             pixel--;
00190             *pixel += diff * (4.0f / 42.0f);
00191          }
00192 
00193       // seventh diffusion ( (x+2,y+1) -> 3)
00194          if (two_on_left)
00195          {
00196             pixel--;
00197             *pixel += diff * (2.0f / 42.0f);
00198          }
00199       }
00200       if (j + 2 < height)
00201       {
00202          float* pixel = source + offset + width + width + 2;
00203       // eightth diffusion ( (x-2,y+2) -> 1)
00204          if ((i + 2) < width)
00205          {
00206             *pixel += diff * (1.0f / 42.0f);
00207          }
00208          pixel--;
00209 
00210       // ninth diffusion ( (x-1,y+2) -> 3)
00211          if ((i + 1) < width)
00212          {
00213             *pixel += diff * (2.0f / 42.0f);
00214          }
00215          pixel--;
00216 
00217       // tenth diffusion ( (x,y+2) -> 5)
00218          *pixel += diff * (4.0f / 42.0f);
00219 
00220       // eleventh diffusion ( (x+1,y+2) -> 3)
00221          if (one_on_left)
00222          {
00223             pixel--;
00224             *pixel += diff * (2.0f / 42.0f);
00225          }
00226       // twelfth diffusion ( (x+2,y+2) -> 1)
00227          if (two_on_left)
00228          {
00229             pixel--;
00230             *pixel += diff * (1.0f / 42.0f);
00231          }
00232       }
00233 
00234       return bw;
00235    }
00236 
00237 }

Generated on Sat Sep 7 16:31:40 2002 for Halftoning Library by doxygen1.2.17