egami
C++ Image Processing program
|
00001 #include <algorithm> 00002 #include <memory> 00003 #include <string> 00004 00005 #include <gtkmm/image.h> 00006 #include <gtkmm/enums.h> 00007 #include <gtkmm/dialog.h> 00008 #include <gtkmm/stock.h> 00009 #include <gtkmm/label.h> 00010 #include <gtkmm/table.h> 00011 #include <gtkmm/checkbutton.h> 00012 #include <gtkmm/scale.h> 00013 00014 #include "../display_unit.hpp" 00015 #include "../processing_page.hpp" 00016 #include "../processing_unit.hpp" 00017 #include "../utils.hpp" 00018 00019 namespace{ 00020 class Config: public Gtk::Dialog { 00022 public: 00023 Config(): gray_adjustment(Gtk::Adjustment::create(1, 0, 2, 0.01, 0.1)), color_red_adjustment(Gtk::Adjustment::create(1, 0, 2, 0.01, 0.1)), 00024 color_green_adjustment(Gtk::Adjustment::create(1, 0, 2, 0.01, 0.1)), color_blue_adjustment(Gtk::Adjustment::create(1, 0, 2, 0.01, 0.1)), 00025 gray_widget(gray_adjustment), color_red_widget(color_red_adjustment), 00026 color_green_widget(color_green_adjustment), color_blue_widget(color_blue_adjustment), 00027 gray_label("Gray"), color_red_label("Red"), 00028 color_green_label("Green"), color_blue_label("Blue"), 00029 color_box(3, 2), color_widget("Color") { 00030 00031 set_resizable(false); 00032 00033 color_widget.set_active(); 00034 color_widget.signal_clicked().connect(sigc::mem_fun(*this, &Config::on_set_color)); 00035 get_vbox()->pack_start(color_widget); 00036 00037 gray_widget.set_digits(2); 00038 gray_widget.set_value_pos(Gtk::POS_RIGHT); 00039 gray_widget.set_draw_value(); 00040 gray_widget.set_size_request(100, 30); 00041 gray_box.pack_start(gray_label); 00042 gray_box.pack_end(gray_widget); 00043 get_vbox()->pack_start(gray_box); 00044 00045 color_red_widget.set_digits(2); 00046 color_red_widget.set_value_pos(Gtk::POS_RIGHT); 00047 color_red_widget.set_draw_value(); 00048 color_red_widget.set_size_request(100, 30); 00049 color_box.attach(color_red_label, 0, 1, 0, 1); 00050 color_box.attach(color_red_widget, 1, 2, 0, 1); 00051 00052 color_green_widget.set_digits(2); 00053 color_green_widget.set_value_pos(Gtk::POS_RIGHT); 00054 color_green_widget.set_draw_value(); 00055 color_green_widget.set_size_request(100, 30); 00056 color_box.attach(color_green_label, 0, 1, 1, 2); 00057 color_box.attach(color_green_widget, 1, 2, 1, 2); 00058 00059 color_blue_widget.set_digits(2); 00060 color_blue_widget.set_value_pos(Gtk::POS_RIGHT); 00061 color_blue_widget.set_draw_value(); 00062 color_blue_widget.set_size_request(100, 30); 00063 color_box.attach(color_blue_label, 0, 1, 2, 3); 00064 color_box.attach(color_blue_widget, 1, 2, 2, 3); 00065 00066 get_vbox()->pack_start(color_box); 00067 00068 add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK); 00069 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); 00070 00071 show_all_children(); 00072 00073 on_set_color(); 00074 } 00075 00076 void on_set_color(){ 00077 if(color_widget.get_active()){ 00078 gray_box.hide(); 00079 color_box.show(); 00080 resize(1, 1); 00081 } else { 00082 gray_box.show(); 00083 color_box.hide(); 00084 resize(1, 1); 00085 } 00086 } 00087 00088 double red() const { 00089 return color_widget.get_active()?color_red_widget.get_value():gray_widget.get_value(); 00090 } 00091 double green() const { 00092 return color_widget.get_active()?color_green_widget.get_value():gray_widget.get_value(); 00093 } 00094 double blue() const { 00095 return color_widget.get_active()?color_blue_widget.get_value():gray_widget.get_value(); 00096 } 00097 00098 private: 00099 Glib::RefPtr<Gtk::Adjustment> gray_adjustment, color_red_adjustment, color_green_adjustment, color_blue_adjustment; 00100 Gtk::HScale gray_widget, color_red_widget, color_green_widget, color_blue_widget; 00101 Gtk::Label gray_label, color_red_label, color_green_label, color_blue_label; 00102 Gtk::HBox gray_box; 00103 Gtk::Table color_box; 00104 00105 Gtk::CheckButton color_widget; 00106 }; 00107 00108 Display_unit* impl(const Image_unit *in){ 00109 Config c; 00110 if(c.run()!=Gtk::RESPONSE_OK) 00111 return nullptr; 00112 c.hide(); 00113 00114 Image_unit *out(new Image_unit(*in)); 00115 00116 for(Image_unit::iterator it=out->begin(); it!=out->end(); ++it){ 00117 int red=it->r*c.red(), green=it->g*c.green(), blue=it->b*c.blue(); 00118 red=red<0?0:red>255?255:red; 00119 green=green<0?0:green>255?255:green; 00120 blue=blue<0?0:blue>255?255:blue; 00121 *it={ red, green, blue }; 00122 } 00123 00124 return out; 00125 } 00126 } 00127 00128 namespace Processes{ 00129 void rescale(){ 00130 Gtk::Image icon(cmake_install_prefix+std::string("/share/egami/icons/rescale.png")); 00131 std::shared_ptr<Processing_unit> pu(new Processing_unit(Processing(&impl), "Rescale", icon, false)); 00132 Processing_page::add_unit_to_page("Colours", pu); 00133 } 00134 }