00001
00004 #ifndef Image_h
00005 #define Image_h
00006
00007 #include "Color.h"
00008 #include "Math.h"
00009 #include <string>
00010
00011 struct Pixel {
00012 unsigned char r, g, b;
00013 };
00014
00015 class Image {
00016 public:
00017 Image(const std::string& name);
00018 Image(int xres, int yres);
00019 ~Image();
00020 void set(int x, int y, const Color& c) {
00021 Pixel p;
00022 p.r = c.r() < 0.f? 0: c.r() >= 1.f? 255 : (int)(c.r() * 255.f);
00023 p.g = c.g() < 0.f? 0: c.g() >= 1.f? 255 : (int)(c.g() * 255.f);
00024 p.b = c.b() < 0.f? 0: c.b() >= 1.f? 255 : (int)(c.b() * 255.f);
00025 data[y][x] = p;
00026 }
00027 void write(const std::string& filename) const;
00028 double aspect_ratio() const {
00029 return double(xres)/double(yres);
00030 }
00031 int getXresolution() {
00032 return xres;
00033 }
00034 int getYresolution() {
00035 return yres;
00036 }
00037 Color interpolate(float x, float y) const {
00038 x *= xres; y *= yres;
00039 int ix = Floor(x)%xres;
00040 if(ix<0)
00041 ix += xres;
00042 int ix1 = (ix+1)%xres;
00043 int iy = Floor(y)%yres;
00044 if(iy<0)
00045 iy += yres;
00046 int iy1 = (iy+1)%yres;
00047 float fx = x-ix;
00048 float fy = y-iy;
00049
00050 Color c00 = Color(data[iy][ix].r, data[iy][ix].g, data[iy][ix].b);
00051 Color c01 = Color(data[iy][ix1].r, data[iy][ix1].g, data[iy][ix1].b);
00052 Color c10 = Color(data[iy1][ix].r, data[iy1][ix].g, data[iy1][ix].b);
00053 Color c11 = Color(data[iy1][ix1].r, data[iy1][ix1].g, data[iy1][ix1].b);
00054 Color c = c00*(1-fx)*(1-fy) + c01*fx*(1-fy) + c10*(1-fx)*fy + c11*fx*fy;
00055 return c*(1./255);
00056 }
00057 protected:
00058 Pixel** data;
00059 int xres, yres;
00060 Image(const Image&);
00061 Image& operator=(const Image&);
00062 private:
00063 void read_png_file(const char* file_name, int &width, int &height);
00064 void write_png_file(const char* file_name, int width, int height) const;
00065 };
00066
00067 #endif