diff --git a/src/slic3r/GUI/BitmapCache.cpp b/src/slic3r/GUI/BitmapCache.cpp index db116a5dd..efa35a511 100644 --- a/src/slic3r/GUI/BitmapCache.cpp +++ b/src/slic3r/GUI/BitmapCache.cpp @@ -174,7 +174,7 @@ wxBitmap* BitmapCache::insert(const std::string &bitmap_key, const wxBitmap *beg #endif } -wxBitmap* BitmapCache::insert_raw_rgba(const std::string &bitmap_key, unsigned width, unsigned height, const unsigned char *raw_data, float scale /* = 1.0f */) +wxBitmap* BitmapCache::insert_raw_rgba(const std::string &bitmap_key, unsigned width, unsigned height, const unsigned char *raw_data, float scale /* = 1.0f */, const bool grayscale/* = false*/) { wxImage image(width, height); image.InitAlpha(); @@ -187,14 +187,20 @@ wxBitmap* BitmapCache::insert_raw_rgba(const std::string &bitmap_key, unsigned w *rgb ++ = *raw_data ++; *alpha ++ = *raw_data ++; } + + if (grayscale) + image.ConvertToGreyscale(m_gs, m_gs, m_gs); + return this->insert(bitmap_key, wxImage_to_wxBitmap_with_alpha(std::move(image), scale)); } -wxBitmap* BitmapCache::load_png(const std::string &bitmap_name, unsigned int width, unsigned int height) +wxBitmap* BitmapCache::load_png(const std::string &bitmap_name, unsigned int width, unsigned int height, + const bool grayscale/* = false*/) { std::string bitmap_key = bitmap_name + ( height !=0 ? "-h" + std::to_string(height) : - "-w" + std::to_string(width)); + "-w" + std::to_string(width)) + + (grayscale ? "-gs" : ""); auto it = m_map.find(bitmap_key); if (it != m_map.end()) @@ -213,15 +219,20 @@ wxBitmap* BitmapCache::load_png(const std::string &bitmap_name, unsigned int wid if (height != 0 && width != 0) image.Rescale(width, height, wxIMAGE_QUALITY_BILINEAR); + if (grayscale) + image.ConvertToGreyscale(m_gs, m_gs, m_gs); + return this->insert(bitmap_key, wxImage_to_wxBitmap_with_alpha(std::move(image))); } -wxBitmap* BitmapCache::load_svg(const std::string &bitmap_name, unsigned target_width, unsigned target_height, float scale /* = 1.0f */) +wxBitmap* BitmapCache::load_svg(const std::string &bitmap_name, unsigned target_width, unsigned target_height, + float scale /* = 1.0f */, const bool grayscale/* = false*/) { std::string bitmap_key = bitmap_name + ( target_height !=0 ? "-h" + std::to_string(target_height) : "-w" + std::to_string(target_width)) - + (scale != 1.0f ? "-s" + std::to_string(scale) : ""); + + (scale != 1.0f ? "-s" + std::to_string(scale) : "") + + (grayscale ? "-gs" : ""); target_height != 0 ? target_height *= scale : target_width *= scale; @@ -256,7 +267,7 @@ wxBitmap* BitmapCache::load_svg(const std::string &bitmap_name, unsigned target_ ::nsvgDeleteRasterizer(rast); ::nsvgDelete(image); - return this->insert_raw_rgba(bitmap_key, width, height, data.data(), scale); + return this->insert_raw_rgba(bitmap_key, width, height, data.data(), scale, grayscale); } wxBitmap BitmapCache::mksolid(size_t width, size_t height, unsigned char r, unsigned char g, unsigned char b, unsigned char transparency) diff --git a/src/slic3r/GUI/BitmapCache.hpp b/src/slic3r/GUI/BitmapCache.hpp index f032d8082..10ce3299e 100644 --- a/src/slic3r/GUI/BitmapCache.hpp +++ b/src/slic3r/GUI/BitmapCache.hpp @@ -29,12 +29,12 @@ public: wxBitmap* insert(const std::string &name, const wxBitmap &bmp, const wxBitmap &bmp2, const wxBitmap &bmp3); wxBitmap* insert(const std::string &name, const std::vector &bmps) { return this->insert(name, &bmps.front(), &bmps.front() + bmps.size()); } wxBitmap* insert(const std::string &name, const wxBitmap *begin, const wxBitmap *end); - wxBitmap* insert_raw_rgba(const std::string &bitmap_key, unsigned width, unsigned height, const unsigned char *raw_data, float scale = 1.0f); + wxBitmap* insert_raw_rgba(const std::string &bitmap_key, unsigned width, unsigned height, const unsigned char *raw_data, float scale = 1.0f, const bool grayscale = false); // Load png from resources/icons. bitmap_key is given without the .png suffix. Bitmap will be rescaled to provided height/width if nonzero. - wxBitmap* load_png(const std::string &bitmap_key, unsigned int width = 0, unsigned int height = 0); + wxBitmap* load_png(const std::string &bitmap_key, unsigned int width = 0, unsigned int height = 0, const bool grayscale = false); // Load svg from resources/icons. bitmap_key is given without the .svg suffix. SVG will be rasterized to provided height/width. - wxBitmap* load_svg(const std::string &bitmap_key, unsigned int width = 0, unsigned int height = 0, float scale = 1.0f); + wxBitmap* load_svg(const std::string &bitmap_key, unsigned int width = 0, unsigned int height = 0, float scale = 1.0f, const bool grayscale = false); static wxBitmap mksolid(size_t width, size_t height, unsigned char r, unsigned char g, unsigned char b, unsigned char transparency); static wxBitmap mksolid(size_t width, size_t height, const unsigned char rgb[3]) { return mksolid(width, height, rgb[0], rgb[1], rgb[2], wxALPHA_OPAQUE); } @@ -42,6 +42,7 @@ public: private: std::map m_map; + double m_gs = 0.2; // value, used for image.ConvertToGreyscale(m_gs, m_gs, m_gs) }; } // GUI diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index f410cab6d..fa06cf915 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -59,11 +59,9 @@ void enable_menu_item(wxUpdateUIEvent& evt, std::function const cb_condi const auto it = msw_menuitem_bitmaps.find(item->GetId()); if (it != msw_menuitem_bitmaps.end()) { - const wxBitmap& item_icon = create_scaled_bitmap(nullptr, it->second); - if (item_icon.IsOk()) { - double g = 0.6; - item->SetBitmap(enable ? item_icon : item_icon.ConvertToImage().ConvertToGreyscale(g, g, g)); - } + const wxBitmap& item_icon = create_scaled_bitmap(nullptr, it->second, 16, false, !enable); + if (item_icon.IsOk()) + item->SetBitmap(item_icon); } #endif // __WXOSX__ } @@ -385,7 +383,8 @@ int em_unit(wxWindow* win) } // If an icon has horizontal orientation (width > height) call this function with is_horizontal = true -wxBitmap create_scaled_bitmap(wxWindow *win, const std::string& bmp_name_in, const int px_cnt/* = 16*/, const bool is_horizontal /* = false*/) +wxBitmap create_scaled_bitmap(wxWindow *win, const std::string& bmp_name_in, + const int px_cnt/* = 16*/, const bool is_horizontal /* = false*/, const bool grayscale/* = false*/) { static Slic3r::GUI::BitmapCache cache; @@ -405,9 +404,9 @@ wxBitmap create_scaled_bitmap(wxWindow *win, const std::string& bmp_name_in, con boost::replace_last(bmp_name, ".png", ""); // Try loading an SVG first, then PNG if SVG is not found: - wxBitmap *bmp = cache.load_svg(bmp_name, width, height, scale_factor); + wxBitmap *bmp = cache.load_svg(bmp_name, width, height, scale_factor, grayscale); if (bmp == nullptr) { - bmp = cache.load_png(bmp_name, width, height); + bmp = cache.load_png(bmp_name, width, height, grayscale); } if (bmp == nullptr) { diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index 2eb1d1832..78fb7be55 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -45,7 +45,8 @@ void edit_tooltip(wxString& tooltip); void msw_buttons_rescale(wxDialog* dlg, const int em_unit, const std::vector& btn_ids); int em_unit(wxWindow* win); -wxBitmap create_scaled_bitmap(wxWindow *win, const std::string& bmp_name, const int px_cnt = 16, const bool is_horizontal = false); +wxBitmap create_scaled_bitmap(wxWindow *win, const std::string& bmp_name, + const int px_cnt = 16, const bool is_horizontal = false, const bool grayscale = false); class wxCheckListBoxComboPopup : public wxCheckListBox, public wxComboPopup {