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

Image.cxx

Go to the documentation of this file.
00001 /*
00002  * @(#)src/Image.cxx  1.0  2002-08-27 13:47
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 <algorithm>
00027 #include <cstring>
00028 #include <string>
00029 #include "Image.h"
00030 
00031 namespace halftoner
00032 {
00033 
00034 //---------------------------
00035 // Constructor
00036 //---------------------------
00037 
00038    Image::Image(int width, int height) throw(std::invalid_argument) :
00039       pixels(0),
00040       width(width),
00041       height(height)
00042    {
00043    // check parameters
00044       if ((0 >= width) || (0 >= height))
00045       {
00046          std::string message("Cannot create image with a negative width or height");
00047          throw std::invalid_argument(message);
00048       }
00049 
00050    // create the raw data
00051       int size = width * height;
00052       this->pixels = new unsigned char[size];
00053       memset(this->pixels, 0, size * sizeof(unsigned char));
00054    }
00055 
00056 
00057 //---------------------------
00058 // Destructor
00059 //---------------------------
00060 
00061    Image::~Image() throw()
00062    {
00063       delete [] this->pixels;
00064    }
00065 
00066 
00067 //---------------------------
00068 // Instance methods
00069 //---------------------------
00070 
00071    unsigned char* Image::getPixels() const throw()
00072    {
00073       return this->pixels;
00074    }
00075 
00076    int Image::getWidth() const throw()
00077    {
00078       return this->width;
00079    }
00080 
00081    int Image::getHeight() const throw()
00082    {
00083       return this->height;
00084    }
00085 
00086    int Image::getColorAt(int x, int y) const throw (std::out_of_range)
00087    {
00088       unsigned char* pixel = this->getPixel(x, y);
00089       int color = *pixel;
00090       return color;
00091    }
00092 
00093    void Image::setColorAt(int x, int y, int color) throw (std::out_of_range)
00094    {
00095       unsigned char* pixel = this->getPixel(x, y);
00096       *pixel = color;
00097    }
00098 
00099    void Image::setLine(int line, int* colors, int colors_size) throw (std::out_of_range)
00100    {
00101       int width = this->getWidth();
00102       if (colors_size != width)
00103       {
00104          std::string message("Given size is not equal to image width");
00105          throw std::out_of_range(message);
00106       }
00107 
00108       int x = 0;
00109       int y = line;
00110       unsigned char* pixels = this->getPixel(x, y);
00111 
00112       std::copy_n(colors, colors_size, pixels);
00113    }
00114 
00115    unsigned char* Image::getPixel(int x, int y) const throw (std::out_of_range)
00116    {
00117       int w = this->getWidth();
00118       int h = this->getHeight();
00119 
00120       if ((0 > x) || (0 > y) || (x > w) || (y > h))
00121       {
00122          std::string message("Given coordinates are out of bounds");
00123          throw std::out_of_range(message);
00124       }
00125 
00126       unsigned char* data = this->getPixels();
00127       int position = Image::calculateOffset(x, y, w);
00128       unsigned char* pixel = &data[position];
00129 
00130       return pixel;
00131    }
00132 
00133    unsigned char* Image::getCopy() const throw()
00134    {
00135       int width = this->getWidth();
00136       int height = this->getHeight();
00137       int size = width * height;
00138 
00139       unsigned char* pixels = this->getPixels();
00140       unsigned char* copy = new unsigned char[size];
00141 
00142       memcpy(copy, pixels, size);
00143 
00144       return copy;
00145    }
00146 
00147 //---------------------------
00148 // Class methods
00149 //---------------------------
00150 
00151    int Image::calculateOffset(int x, int y, int line_width) throw()
00152    {
00153       int offset = x + (y * line_width);
00154       return offset;
00155    }
00156 
00157 }

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