From ee5dc05a09e50351e93026107abcdb94ef1216a2 Mon Sep 17 00:00:00 2001 From: toger5 Date: Fri, 8 Sep 2017 04:31:12 +0200 Subject: [PATCH] optimized color conversion for svg generation --- editor/editor_themes.cpp | 25 +++++++++------ modules/svg/image_loader_svg.cpp | 55 ++++++++++++++++++-------------- modules/svg/image_loader_svg.h | 12 ++++--- 3 files changed, 55 insertions(+), 37 deletions(-) diff --git a/editor/editor_themes.cpp b/editor/editor_themes.cpp index 1903514ea64..cbb6c098686 100644 --- a/editor/editor_themes.cpp +++ b/editor/editor_themes.cpp @@ -86,7 +86,7 @@ static Ref change_border_color(Ref p_style, Color p_ return style; } -Ref editor_generate_icon(int p_index, bool dark_theme = true, Dictionary *p_colors = NULL, float p_scale = EDSCALE, bool force_filter = false) { +Ref editor_generate_icon(int p_index, bool convert_color, float p_scale = EDSCALE, bool force_filter = false) { Ref icon = memnew(ImageTexture); Ref img = memnew(Image); @@ -94,7 +94,7 @@ Ref editor_generate_icon(int p_index, bool dark_theme = true, Dict // dumb gizmo check bool is_gizmo = String(editor_icons_names[p_index]).begins_with("Gizmo"); - ImageLoaderSVG::create_image_from_string(img, editor_icons_sources[p_index], p_scale, true, dark_theme ? NULL : p_colors); + ImageLoaderSVG::create_image_from_string(img, editor_icons_sources[p_index], p_scale, true, convert_color); if ((p_scale - (float)((int)p_scale)) > 0.0 || is_gizmo || force_filter) icon->create_from_image(img); // in this case filter really helps @@ -160,34 +160,41 @@ void editor_register_and_generate_icons(Ref p_theme, bool dark_theme = tr clock_t begin_time = clock(); + ImageLoaderSVG::set_convert_colors(dark_theme ? NULL : &dark_icon_color_dictionary); + + // generate icons if (!only_thumbs) for (int i = 0; i < editor_icons_count; i++) { List::Element *is_exception = exceptions.find(editor_icons_names[i]); - if (is_exception) { - exceptions.erase(is_exception); - } - Ref icon = editor_generate_icon(i, dark_theme, is_exception ? NULL : &dark_icon_color_dictionary); + if (is_exception) exceptions.erase(is_exception); + Ref icon = editor_generate_icon(i, !dark_theme && !is_exception); p_theme->set_icon(editor_icons_names[i], "EditorIcons", icon); } - bool force_filter = !(p_thumb_size == 64 && p_thumb_size == 32); // we dont need filter with original resolution // generate thumb files with the given thumb size + bool force_filter = !(p_thumb_size == 64 && p_thumb_size == 32); // we dont need filter with original resolution if (p_thumb_size >= 64) { float scale = (float)p_thumb_size / 64.0 * EDSCALE; for (int i = 0; i < editor_bg_thumbs_count; i++) { int index = editor_bg_thumbs_indices[i]; - Ref icon = editor_generate_icon(index, dark_theme, &dark_icon_color_dictionary, scale, force_filter); + List::Element *is_exception = exceptions.find(editor_icons_names[index]); + if (is_exception) exceptions.erase(is_exception); + Ref icon = editor_generate_icon(index, !dark_theme && !is_exception, scale, force_filter); p_theme->set_icon(editor_icons_names[index], "EditorIcons", icon); } } else { float scale = (float)p_thumb_size / 32.0 * EDSCALE; for (int i = 0; i < editor_md_thumbs_count; i++) { int index = editor_md_thumbs_indices[i]; - Ref icon = editor_generate_icon(index, dark_theme, &dark_icon_color_dictionary, scale, force_filter); + List::Element *is_exception = exceptions.find(editor_icons_names[index]); + if (is_exception) exceptions.erase(is_exception); + Ref icon = editor_generate_icon(index, !dark_theme && !is_exception, scale, force_filter); p_theme->set_icon(editor_icons_names[index], "EditorIcons", icon); } } + ImageLoaderSVG::set_convert_colors(NULL); + clock_t end_time = clock(); double time_d = (double)(end_time - begin_time) / CLOCKS_PER_SEC; diff --git a/modules/svg/image_loader_svg.cpp b/modules/svg/image_loader_svg.cpp index 935a412ed11..c575edbba3a 100644 --- a/modules/svg/image_loader_svg.cpp +++ b/modules/svg/image_loader_svg.cpp @@ -63,33 +63,39 @@ inline void change_nsvg_paint_color(NSVGpaint *p_paint, const uint32_t p_old, co } } } -void ImageLoaderSVG::_convert_colors(NSVGimage *p_svg_image, const Dictionary p_colors) { - List replace_colors_i; - List new_colors_i; - List replace_colors; - List new_colors; - for (int i = 0; i < p_colors.keys().size(); i++) { - Variant r_c = p_colors.keys()[i]; - Variant n_c = p_colors[p_colors.keys()[i]]; - if (r_c.get_type() == Variant::COLOR && n_c.get_type() == Variant::COLOR) { - Color replace_color = r_c; - Color new_color = n_c; - replace_colors_i.push_back(replace_color.to_abgr32()); - new_colors_i.push_back(new_color.to_abgr32()); - } - } +void ImageLoaderSVG::_convert_colors(NSVGimage *p_svg_image) { for (NSVGshape *shape = p_svg_image->shapes; shape != NULL; shape = shape->next) { - for (int i = 0; i < replace_colors_i.size(); i++) { - change_nsvg_paint_color(&(shape->stroke), replace_colors_i[i], new_colors_i[i]); - change_nsvg_paint_color(&(shape->fill), replace_colors_i[i], new_colors_i[i]); + for (int i = 0; i < replace_colors.old_colors.size(); i++) { + change_nsvg_paint_color(&(shape->stroke), replace_colors.old_colors[i], replace_colors.new_colors[i]); + change_nsvg_paint_color(&(shape->fill), replace_colors.old_colors[i], replace_colors.new_colors[i]); } } } -Error ImageLoaderSVG::_create_image(Ref p_image, const PoolVector *p_data, float p_scale, bool upsample, const Dictionary *p_replace_colors) { +void ImageLoaderSVG::set_convert_colors(Dictionary *p_replace_color) { + + if (p_replace_color) { + Dictionary replace_color = *p_replace_color; + for (int i = 0; i < replace_color.keys().size(); i++) { + Variant o_c = replace_color.keys()[i]; + Variant n_c = replace_color[replace_color.keys()[i]]; + if (o_c.get_type() == Variant::COLOR && n_c.get_type() == Variant::COLOR) { + Color old_color = o_c; + Color new_color = n_c; + replace_colors.old_colors.push_back(old_color.to_abgr32()); + replace_colors.new_colors.push_back(new_color.to_abgr32()); + } + } + } else { + replace_colors.old_colors.clear(); + replace_colors.new_colors.clear(); + } +} + +Error ImageLoaderSVG::_create_image(Ref p_image, const PoolVector *p_data, float p_scale, bool upsample, bool convert_colors) { NSVGimage *svg_image; PoolVector::Read src_r = p_data->read(); svg_image = nsvgParse((char *)src_r.ptr(), "px", 96); @@ -97,10 +103,9 @@ Error ImageLoaderSVG::_create_image(Ref p_image, const PoolVectorwidth * p_scale * upscale); @@ -123,7 +128,7 @@ Error ImageLoaderSVG::_create_image(Ref p_image, const PoolVector p_image, const char *svg_str, float p_scale, bool upsample, const Dictionary *p_replace_colors) { +Error ImageLoaderSVG::create_image_from_string(Ref p_image, const char *svg_str, float p_scale, bool upsample, bool convert_colors) { size_t str_len = strlen(svg_str); PoolVector src_data; @@ -131,7 +136,7 @@ Error ImageLoaderSVG::create_image_from_string(Ref p_image, const char *s PoolVector::Write src_w = src_data.write(); memcpy(src_w.ptr(), svg_str, str_len + 1); - return _create_image(p_image, &src_data, p_scale, upsample, p_replace_colors); + return _create_image(p_image, &src_data, p_scale, upsample, convert_colors); } Error ImageLoaderSVG::load_image(Ref p_image, FileAccess *f, bool p_force_linear, float p_scale) { @@ -154,3 +159,5 @@ void ImageLoaderSVG::get_recognized_extensions(List *p_extensions) const ImageLoaderSVG::ImageLoaderSVG() { } + +ImageLoaderSVG::ReplaceColors ImageLoaderSVG::replace_colors; \ No newline at end of file diff --git a/modules/svg/image_loader_svg.h b/modules/svg/image_loader_svg.h index f692b1b28cf..332ac214a52 100644 --- a/modules/svg/image_loader_svg.h +++ b/modules/svg/image_loader_svg.h @@ -52,13 +52,17 @@ public: }; class ImageLoaderSVG : public ImageFormatLoader { - + static struct ReplaceColors { + List old_colors; + List new_colors; + } replace_colors; static SVGRasterizer rasterizer; - static void _convert_colors(NSVGimage *p_svg_imge, const Dictionary p_colors); - static Error _create_image(Ref p_image, const PoolVector *p_data, float p_scale, bool upsample, const Dictionary *p_replace_color = NULL); + static void _convert_colors(NSVGimage *p_svg_imge); + static Error _create_image(Ref p_image, const PoolVector *p_data, float p_scale, bool upsample, bool convert_colors = false); public: - static Error create_image_from_string(Ref p_image, const char *p_svg_str, float p_scale, bool upsample, const Dictionary *p_replace_color = NULL); + static void set_convert_colors(Dictionary *p_replace_color = NULL); + static Error create_image_from_string(Ref p_image, const char *p_svg_str, float p_scale, bool upsample, bool convert_colors = false); virtual Error load_image(Ref p_image, FileAccess *f, bool p_force_linear, float p_scale); virtual void get_recognized_extensions(List *p_extensions) const;