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

VG91.cxx

Go to the documentation of this file.
00001 /*
00002  * @(#)src/VG91.cxx  1.0  2002-09-07 09:52
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 <utility>
00027 #include "Image.h"
00028 #include "SFCPath.h"
00029 #include "SFCPathFactory.h"
00030 #include "VG91.h"
00031 #include "VGSpec.h"
00032 
00033 namespace halftoner
00034 {
00035 
00036 //---------------------------
00037 // Constructor
00038 //---------------------------
00039 
00040    VG91::VG91() throw()
00041    {
00042    // nothing
00043    }
00044 
00045 
00046 //---------------------------
00047 // Destructor
00048 //---------------------------
00049 
00050    VG91::~VG91()
00051    {
00052    // nothing
00053    }
00054 
00055 
00056 //---------------------------
00057 // Implemented method from Halftoner
00058 //---------------------------
00059 
00060    Image* VG91::halftone(const Image& source, const HalftonerSpec* spec) throw (std::invalid_argument)
00061    {
00062    // dimensions
00063       const int width = source.getWidth();
00064       const int height = source.getHeight();
00065    // square check
00066       if (width != height)
00067       {
00068          string s("image is not square");
00069          throw std::invalid_argument(s);
00070       }
00071 
00072 
00073    // getting the curve (can throw std::invalid_argument)
00074       const VGSpec* vgspec = dynamic_cast<const VGSpec*>(spec);
00075       int cluster = vgspec->getClusterSize();
00076       const SFCPathFactory::SizeMnemonic& mnemonic = vgspec->getMnemonic();
00077       SFCPath* curve = SFCPathFactory::getInstance(mnemonic);
00078 
00079    // destination image
00080       Image* out = new Image(width, height);
00081 
00082    // needed variables
00083       int accumulator = 0;
00084       int len = width * height;
00085       std::pair<int, int> p;
00086    // here we go
00087       for (int i = 0; i < len; i += cluster)
00088       {
00089       // accumulator
00090          for (int n = 0; n < cluster; n++)
00091          {
00092             if (i + n < len)
00093             {
00094                p = (*curve)[i + n];
00095                accumulator += source.getColorAt(p.first, p.second);
00096             }
00097          }
00098       // distributing
00099          for (int n = 0; n < cluster; n++)
00100          {
00101             if (i + n < len)
00102             {
00103                p = (*curve)[i + n];
00104                if (accumulator >= 255)
00105                {
00106                   accumulator -= 255;
00107                   out->setColorAt(p.first, p.second, 1);
00108                }
00109                else
00110                {
00111                   out->setColorAt(p.first, p.second, 0);
00112                }
00113             }
00114          }
00115       }
00116 
00117       delete curve;
00118 
00119       return out;
00120    }
00121 
00122 }

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