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

JarvisJudiceNinke.cxx

Go to the documentation of this file.
00001 /*
00002  * @(#)src/JarvisJudiceNinke.cxx  1.0  2002-07-21 20:23
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 
00027 #include "JarvisJudiceNinke.h"
00028 
00029 namespace halftoner
00030 {
00031 
00032 //---------------------------
00033 // Constructor
00034 //---------------------------
00035 
00036    JarvisJudiceNinke::JarvisJudiceNinke()
00037    {
00038    // nothing
00039    }
00040 
00041 
00042 //---------------------------
00043 // Destructor
00044 //---------------------------
00045 
00046    JarvisJudiceNinke::~JarvisJudiceNinke()
00047    {
00048    // nothing
00049    }
00050 
00051 
00052 //---------------------------
00053 // Implemented methods from ErrorDiffusionHalftoner
00054 //---------------------------
00055 
00066    int JarvisJudiceNinke::halftoneToRight(float* source, int i, int j, int offset, int width, int height, int threshold) throw()
00067    {
00068       float grey = source[offset];
00069       int bw = (grey < threshold) ? 0 : 1;
00070       float diff = grey - 255.0f * bw;
00071 
00072    // first diffusion ( (x+1,y) -> 7)
00073       bool one_on_right = (i + 1) < width;
00074       if (one_on_right)
00075       {
00076          float* pixel = source + offset + 1;
00077          *pixel += diff * (7.0f / 48.0f);
00078       }
00079    // second diffusion ( (x+2,y) -> 5)
00080       bool two_on_right = (i + 2) < width;
00081       if (two_on_right)
00082       {
00083          float* pixel = source + offset + 2;
00084          *pixel += diff * (5.0f / 48.0f);
00085       }
00086       if (j + 1 < height)
00087       {
00088          float* pixel = source + offset + width - 2;
00089       // third diffusion ( (x-2,y+1) -> 3)
00090          if ((i - 2) >= 0)
00091          {
00092             *pixel += diff * (3.0f / 48.0f);
00093          }
00094          pixel++;
00095 
00096       // fourth diffusion ( (x-1,y+1) -> 5)
00097          if (i - 1 >= 0)
00098          {
00099             *pixel += diff * (5.0f / 48.0f);
00100          }
00101          pixel++;
00102 
00103       // fifth diffusion ( (x,y+1) -> 7)
00104          *pixel += diff * (7.0f / 48.0f);
00105 
00106       // sixth diffusion ( (x+1,y+1) -> 5)
00107          if (one_on_right)
00108          {
00109             pixel++;
00110             *pixel += diff * (5.0f / 48.0f);
00111          }
00112 
00113       // seventh diffusion ( (x+2,y+1) -> 3)
00114          if (two_on_right)
00115          {
00116             pixel++;
00117             *pixel += diff * (3.0f / 48.0f);
00118          }
00119       }
00120       if (j + 2 < height)
00121       {
00122          float* pixel = source + offset + width + width - 2;
00123       // eightth diffusion ( (x-2,y+2) -> 1)
00124          if (i - 2 >= 0)
00125          {
00126             *pixel += diff * (1.0f / 48.0f);
00127          }
00128          pixel++;
00129 
00130       // ninth diffusion ( (x-1,y+2) -> 3)
00131          if (i - 1 >= 0)
00132          {
00133             *pixel += diff * (3.0f / 48.0f);
00134          }
00135          pixel++;
00136 
00137       // tenth diffusion ( (x,y+2) -> 5)
00138          *pixel += diff * (5.0f / 48.0f);
00139 
00140       // eleventh diffusion ( (x+1,y+2) -> 3)
00141          if (one_on_right)
00142          {
00143             pixel++;
00144             *pixel += diff * (3.0f / 48.0f);
00145          }
00146       // twelfth diffusion ( (x+2,y+2) -> 1)
00147          if (two_on_right)
00148          {
00149             pixel++;
00150             *pixel += diff * (1.0f / 48.0f);
00151          }
00152       }
00153 
00154       return bw;
00155    }
00156 
00167    int JarvisJudiceNinke::halftoneToLeft(float* source, int i, int j, int offset, int width, int height, int threshold) throw()
00168    {
00169       float grey = source[offset];
00170       int bw = (grey < threshold) ? 0 : 1;
00171       float diff = grey - 255.0f * bw;
00172 
00173    // first diffusion ( (x+1,y) -> 7)
00174       bool one_on_left = (i - 1) >= 0;
00175       if (one_on_left)
00176       {
00177          float* pixel = source + offset - 1;
00178          *pixel += diff * (7.0f / 48.0f);
00179       }
00180    // second diffusion ( (x+2,y) -> 5)
00181       bool two_on_left = (i - 2) >= 0;
00182       if (two_on_left)
00183       {
00184          float* pixel = source + offset - 2;
00185          *pixel += diff * (5.0f / 48.0f);
00186       }
00187       if (j + 1 < height)
00188       {
00189          float* pixel = source + offset + width + 2;
00190       // third diffusion ( (x-2,y+1) -> 3)
00191          if ((i + 2) < width)
00192          {
00193             *pixel += diff * (3.0f / 48.0f);
00194          }
00195          pixel--;
00196 
00197       // fourth diffusion ( (x-1,y+1) -> 5)
00198          if ((i + 1) < width)
00199          {
00200             *pixel += diff * (5.0f / 48.0f);
00201          }
00202          pixel--;
00203 
00204       // fifth diffusion ( (x,y+1) -> 7)
00205          *pixel += diff * (7.0f / 48.0f);
00206 
00207       // sixth diffusion ( (x+1,y+1) -> 5)
00208          if (one_on_left)
00209          {
00210             pixel--;
00211             *pixel += diff * (5.0f / 48.0f);
00212          }
00213 
00214       // seventh diffusion ( (x+2,y+1) -> 3)
00215          if (two_on_left)
00216          {
00217             pixel--;
00218             *pixel += diff * (3.0f / 48.0f);
00219          }
00220       }
00221       if (j + 2 < height)
00222       {
00223          float* pixel = source + offset + width + width + 2;
00224       // eightth diffusion ( (x-2,y+2) -> 1)
00225          if ((i + 2) < width)
00226          {
00227             *pixel += diff * (1.0f / 48.0f);
00228          }
00229          pixel--;
00230 
00231       // ninth diffusion ( (x-1,y+2) -> 3)
00232          if ((i + 1) < width)
00233          {
00234             *pixel += diff * (3.0f / 48.0f);
00235          }
00236          pixel--;
00237 
00238       // tenth diffusion ( (x,y+2) -> 5)
00239          *pixel += diff * (5.0f / 48.0f);
00240 
00241       // eleventh diffusion ( (x+1,y+2) -> 3)
00242          if (one_on_left)
00243          {
00244             pixel--;
00245             *pixel += diff * (3.0f / 48.0f);
00246          }
00247       // twelfth diffusion ( (x+2,y+2) -> 1)
00248          if (two_on_left)
00249          {
00250             pixel--;
00251             *pixel += diff * (1.0f / 48.0f);
00252          }
00253       }
00254 
00255       return bw;
00256    }
00257 
00258 }

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