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

FloydSteinberg.cxx

Go to the documentation of this file.
00001 /*
00002  * @(#)src/FloydSteinberg.cxx  1.0  2002-08-27 13:27
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 "FloydSteinberg.h"
00027 
00028 namespace halftoner
00029 {
00030 
00031 //---------------------------
00032 // Constructor
00033 //---------------------------
00034 
00035    FloydSteinberg::FloydSteinberg()
00036    {
00037    // nothing
00038    }
00039 
00040 
00041 //---------------------------
00042 // Destructor
00043 //---------------------------
00044 
00045    FloydSteinberg::~FloydSteinberg()
00046    {
00047    // nothing
00048    }
00049 
00050 
00051 //---------------------------
00052 // Implemented methods from ErrorDiffusionHalftoner
00053 //---------------------------
00054 
00055    int FloydSteinberg::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 * (7.0f / 16.0f);
00067       }
00068 
00069    // other diffusion
00070       if ((j + 1) < height)
00071       {
00072          float* pixel = source + offset + width - 1;
00073       // second diffusion ( (x-1,y+1) -> 3)
00074          if (i > 0)
00075          {
00076             *pixel += diff * (3.0f / 16.0f);
00077          }
00078          pixel++;
00079 
00080       // third diffusion ( (x,y+1) -> 5)
00081          *pixel += diff * (5.0f / 16.0f);
00082          pixel++;
00083 
00084       // fourth diffusion ( (x+1,y+1) -> 1)
00085          if (one_on_right)
00086          {
00087             *pixel += diff * (1.0f / 16.0f);
00088          }
00089       }
00090 
00091       return bw;
00092    }
00093 
00094    int FloydSteinberg::halftoneToLeft(float* source, int i, int j, int offset, int width, int height, int threshold) throw()
00095    {
00096       float grey = source[offset];
00097       int bw = (grey < threshold) ? 0 : 1;
00098       float diff = grey - 255.0f * bw;
00099 
00100    // first diffusion ( (x-1,y) -> 7)
00101       bool one_on_left = (i - 1) >= 0;
00102       if (one_on_left)
00103       {
00104          float* pixel = source + offset - 1;
00105          *pixel += diff * (7.0f / 16.0f);
00106       }
00107 
00108    // other diffusion
00109       if ((j + 1) < height)
00110       {
00111          float* pixel = source + offset + width + 1;
00112       // second diffusion ( (x+1,y+1) -> 3)
00113          if (i + 1 < width)
00114          {
00115             *pixel += diff * (3.0f / 16.0f);
00116          }
00117          pixel--;
00118 
00119       // third diffusion ( (x,y+1) -> 5)
00120          *pixel += diff * (5.0f / 16.0f);
00121          pixel--;
00122 
00123       // fourth diffusion ( (x-1,y+1) -> 1)
00124          if (one_on_left)
00125          {
00126             *pixel += diff * (1.0f / 16.0f);
00127          }
00128       }
00129 
00130       return bw;
00131    }
00132 
00133 }

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