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