00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include <algorithm>
00027 #include <cstring>
00028 #include <string>
00029 #include "Image.h"
00030
00031 namespace halftoner
00032 {
00033
00034
00035
00036
00037
00038 Image::Image(int width, int height) throw(std::invalid_argument) :
00039 pixels(0),
00040 width(width),
00041 height(height)
00042 {
00043
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
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
00059
00060
00061 Image::~Image() throw()
00062 {
00063 delete [] this->pixels;
00064 }
00065
00066
00067
00068
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
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 }