egami
C++ Image Processing program
|
00001 00006 #ifndef IMAGE_HPP_INCLUDED 00007 #define IMAGE_HPP_INCLUDED 00008 00009 #include <memory> 00010 #include <string> 00011 00012 #include <gdkmm/pixbuf.h> 00013 #include <glibmm/refptr.h> 00014 00015 class Image; 00016 class const_Pixel_proxy; 00017 00021 struct Pixel_proxy{ 00029 Pixel_proxy(Image *img, unsigned y, unsigned x) noexcept; 00030 00040 Pixel_proxy& operator=(std::initializer_list<int> values); 00041 Pixel_proxy& operator=(const const_Pixel_proxy &rhs); 00042 00043 unsigned char &r, 00044 &g, 00045 &b; 00046 }; 00047 00048 struct Pixel{ 00049 Pixel(); 00050 Pixel(unsigned char r, unsigned char g, unsigned char b); 00051 unsigned char r, g, b; 00052 }; 00053 00057 class Row_proxy{ 00058 public: 00065 Row_proxy(Image *img, unsigned y) noexcept; 00066 00072 Pixel_proxy operator[](unsigned x); 00073 00074 private: 00075 Image *img; 00076 unsigned y; 00077 }; 00078 00079 00080 struct const_Pixel_proxy{ 00081 const_Pixel_proxy(const Image *img, unsigned y, unsigned x) noexcept; 00082 00083 const unsigned char &r, 00084 &g, 00085 &b; 00086 }; 00087 00088 class const_Row_proxy{ 00089 public: 00090 const_Row_proxy(const Image *img, unsigned y) noexcept; 00091 const_Pixel_proxy operator[](unsigned x) const; 00092 00093 private: 00094 const Image *img; 00095 unsigned y; 00096 }; 00097 00101 class Image{ 00102 public: 00103 class iterator; 00104 class const_iterator; 00105 00111 Image(const std::string &filepath); 00112 00113 Image(unsigned height, unsigned width); 00114 00115 Image() = default; 00116 Image(const Image&); 00117 Image(Image&&) noexcept; 00118 Image& operator=(Image) noexcept; 00119 friend void swap(Image &lhs, Image &rhs) noexcept; 00120 void save_img(const std::string &fp) const; 00121 00127 Row_proxy operator[](unsigned y); 00128 00134 const_Row_proxy operator[](unsigned y) const; 00135 00136 Pixel_proxy at(unsigned y, unsigned x); 00137 const_Pixel_proxy at(unsigned y, unsigned x) const; 00138 00139 iterator begin() noexcept; 00140 iterator end() noexcept; 00141 const_iterator begin() const noexcept; 00142 const_iterator end() const noexcept; 00143 00144 unsigned char red(unsigned y, unsigned x) const; 00145 unsigned char green(unsigned y, unsigned x) const; 00146 unsigned char blue(unsigned y, unsigned x) const; 00147 unsigned char m_red(int y, int x) const; 00148 unsigned char m_green(int y, int x) const; 00149 unsigned char m_blue(int y, int x) const; 00150 void red(unsigned y, unsigned x, unsigned char v); 00151 void green(unsigned y, unsigned x, unsigned char v); 00152 void blue(unsigned y, unsigned x, unsigned char v); 00153 00154 unsigned width() const noexcept; 00155 unsigned height() const noexcept; 00156 00157 bool has_roi() const; 00158 unsigned true_width() const; 00159 unsigned true_height() const; 00160 00161 unsigned x_win() const; 00162 unsigned y_win() const; 00163 void clip(unsigned y, unsigned x, unsigned dy, unsigned dx); 00164 void unclip(); 00165 00166 protected: 00167 Glib::RefPtr<Gdk::Pixbuf> data; 00168 bool regionalised; 00169 unsigned clip_x, clip_y, clip_dx, clip_dy; 00171 unsigned rowstride() const noexcept; 00172 unsigned n_channels() const noexcept; 00173 00174 friend class Pixel_proxy; 00175 friend class const_Pixel_proxy; 00176 }; 00177 00178 class Image::iterator{ 00179 public: 00180 iterator(Image *img, unsigned y, unsigned x); 00181 iterator(const iterator &other); 00182 iterator(iterator &&other) noexcept; 00183 friend void (::swap)(iterator &lhs, iterator &rhs) noexcept; 00184 iterator& operator=(iterator other) noexcept; 00185 00186 bool valid() const; 00187 00188 operator Image::const_iterator() const noexcept; 00189 00190 Pixel get_copy() const; 00191 00192 Pixel_proxy* operator->() noexcept; 00193 Pixel_proxy& operator*() noexcept; 00194 Pixel_proxy* operator->() const noexcept; 00195 Pixel_proxy& operator*() const noexcept; 00196 00197 unsigned char red() const; 00198 unsigned char green() const; 00199 unsigned char blue() const; 00200 void red(unsigned char v); 00201 void green(unsigned char v); 00202 void blue(unsigned char v); 00203 00204 Image::iterator& operator++(); 00205 Image::iterator operator++(int); 00206 Image::iterator& operator--(); 00207 Image::iterator operator--(int); 00208 Image::iterator& operator+=(int); 00209 Image::iterator& operator-=(int); 00210 bool operator==(const Image::iterator &o) const noexcept; 00211 bool operator!=(const Image::iterator &o) const noexcept; 00212 00213 friend Image::iterator operator+(Image::iterator lhs, int rhs); 00214 friend Image::iterator operator+(int lhs, Image::iterator rhs); 00215 friend Image::iterator operator-(Image::iterator lhs, int rhs); 00216 friend int operator-(const Image::iterator &lhs, const Image::iterator &rhs); 00217 00218 private: 00219 Image *img; 00220 unsigned y, x; 00221 std::unique_ptr<Pixel_proxy> proxy; 00222 00223 void rebuild(); 00224 }; 00225 00226 class Image::const_iterator{ 00227 public: 00228 const_iterator(const Image *img, unsigned y, unsigned x); 00229 const_iterator(const const_iterator &other); 00230 const_iterator(const_iterator &&other) noexcept; 00231 friend void (::swap)(const_iterator &lhs, const_iterator &rhs) noexcept; 00232 const_iterator& operator=(const_iterator other) noexcept; 00233 00234 bool valid() const; 00235 00236 Pixel get_copy() const; 00237 00238 const_Pixel_proxy* operator->() noexcept; 00239 const_Pixel_proxy& operator*() noexcept; 00240 const_Pixel_proxy* operator->() const noexcept; 00241 const_Pixel_proxy& operator*() const noexcept; 00242 00243 unsigned char red() const; 00244 unsigned char green() const; 00245 unsigned char blue() const; 00246 00247 Image::const_iterator& operator++(); 00248 Image::const_iterator operator++(int); 00249 Image::const_iterator& operator--(); 00250 Image::const_iterator operator--(int); 00251 Image::const_iterator& operator+=(int); 00252 Image::const_iterator& operator-=(int); 00253 bool operator==(const Image::const_iterator &o) const noexcept; 00254 bool operator!=(const Image::const_iterator &o) const noexcept; 00255 00256 friend Image::const_iterator operator+(Image::const_iterator lhs, int rhs); 00257 friend Image::const_iterator operator+(int lhs, Image::const_iterator rhs); 00258 friend Image::const_iterator operator-(Image::const_iterator lhs, int rhs); 00259 friend int operator-(const Image::const_iterator &lhs, const Image::const_iterator &rhs); 00260 00261 private: 00262 const Image *img; 00263 unsigned y, x; 00264 std::unique_ptr<const_Pixel_proxy> proxy; 00265 00266 void rebuild(); 00267 }; 00268 #endif