1 // Copyright (C) 2011  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 #ifndef DLIB_METADATA_EdITOR_H__
4 #define DLIB_METADATA_EdITOR_H__
5 
6 #include <dlib/gui_widgets.h>
7 #include <dlib/data_io.h>
8 #include <dlib/pixel.h>
9 #include <map>
10 
11 // ----------------------------------------------------------------------------------------
12 
13 class color_mapper
14 {
15 public:
16 
operator()17     dlib::rgb_alpha_pixel operator() (
18         const std::string& str
19     )
20     {
21         auto i = colors.find(str);
22         if (i != colors.end())
23         {
24             return i->second;
25         }
26         else
27         {
28             using namespace dlib;
29             hsi_pixel pix;
30             pix.h = reverse(colors.size());
31             pix.s = 255;
32             pix.i = 150;
33             rgb_alpha_pixel result;
34             assign_pixel(result, pix);
35             colors[str] = result;
36             return result;
37         }
38     }
39 
40 private:
41 
42     // We use a bit reverse here because it causes us to evenly spread the colors as we
43     // allocated them. First the colors are maximally different, then become interleaved
44     // and progressively more similar as they are allocated.
reverse(unsigned char b)45     unsigned char reverse(unsigned char b)
46     {
47         // reverse the order of the bits in b.
48         b = ((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
49         return b;
50     }
51 
52     std::map<std::string, dlib::rgb_alpha_pixel> colors;
53 };
54 
55 // ----------------------------------------------------------------------------------------
56 
57 class metadata_editor : public dlib::drawable_window
58 {
59 public:
60     metadata_editor(
61         const std::string& filename_
62     );
63 
64     ~metadata_editor();
65 
66     void add_labelable_part_name (
67         const std::string& name
68     );
69 
70 private:
71 
72     void file_save();
73     void file_save_as();
74     void remove_selected_images();
75 
76     virtual void on_window_resized();
77     virtual void on_keydown (
78         unsigned long key,
79         bool is_printable,
80         unsigned long state
81     );
82 
83     void on_lb_images_clicked(unsigned long idx);
84     void select_image(unsigned long idx);
85     void save_metadata_to_file (const std::string& file);
86     void load_image(unsigned long idx);
87     void load_image_and_set_size(unsigned long idx);
88     void on_image_clicked(const dlib::point& p, bool is_double_click, unsigned long btn);
89     void on_overlay_rects_changed();
90     void on_overlay_label_changed();
91     void on_overlay_rect_selected(const dlib::image_display::overlay_rect& orect);
92 
93     void display_about();
94 
95     std::string filename;
96     dlib::image_dataset_metadata::dataset metadata;
97 
98     dlib::menu_bar mbar;
99     dlib::list_box lb_images;
100     unsigned long image_pos;
101 
102     dlib::image_display display;
103     dlib::label overlay_label_name;
104     dlib::text_field overlay_label;
105 
106     unsigned long keyboard_jump_pos;
107     time_t last_keyboard_jump_pos_update;
108     bool display_equialized_image = false;
109     color_mapper string_to_color;
110 };
111 
112 // ----------------------------------------------------------------------------------------
113 
114 
115 #endif // DLIB_METADATA_EdITOR_H__
116 
117