handle transparent images when importing symbols

This commit is contained in:
GenevensiS
2026-02-06 03:17:46 +01:00
parent 857bfb6c9c
commit d2dce6617d
16 changed files with 57 additions and 5 deletions
+3 -2
View File
@@ -194,10 +194,11 @@ String compute_new_variable_value(const String& str, const String& tag, size_t s
}
while (end_i < str.size() &&
is_formatting_tag(str, end_i)) {
end_i = str.find_first_of(">", end_i) + 1;
end_i = str.find_first_of(">", end_i);
if (end_i != String::npos) end_i++;
}
String prefix(substr(str, 0, start_i));
String suffix(substr(str, end_i));
String suffix = end_i == String::npos ? String() : substr(str, end_i);
String selection(substr(str, start_i, end_i - start_i));
// tally open tags that are variants of this tag
+30 -3
View File
@@ -15,6 +15,7 @@
#include <util/prec.hpp>
#include <data/format/image_to_symbol.hpp>
#include <gfx/gfx.hpp>
#include <gfx/bezier.hpp>
#include <util/error.hpp>
#include <util/platform.hpp>
@@ -205,11 +206,13 @@ SymbolShapeP read_symbol_shape(const ImageData& data) {
SymbolP image_to_symbol(Image& img) {
int w = img.GetWidth(), h = img.GetHeight();
// 1. threshold the image
int w = img.GetWidth(), h = img.GetHeight();
// get rid of alpha channel
fill_alpha(img, w, h);
// threshold the image
greyscale(img);
threshold(img.GetData(), w, h);
// 2. read as many symbol shapes as we can
// read as many symbol shapes as we can
ImageData data = {w,h,img.GetData()};
SymbolP symbol(new Symbol);
while (true) {
@@ -221,6 +224,30 @@ SymbolP image_to_symbol(Image& img) {
return symbol;
}
void fill_alpha(Image& img, int w, int h) {
if (!img.HasAlpha() || w <= 0 || h <= 0) return;
Byte* alpha = img.GetAlpha();
for (int i = 0; i < w*h ; ++i) {
if (alpha[i] < 255) {
Byte* pixels = img.GetData();
Byte first_pixel = pixels[0];
for (int j = 1; j < 3 * w * h; ++j) {
if (pixels[j] != first_pixel) {
wxMessageDialog dial = wxMessageDialog(nullptr, _ERROR_("symbol image has alpha"));
dial.ShowModal();
break;
}
}
Image copy = img.Copy();
memset(copy.GetData(), 255, 3 * w * h);
memset(pixels, 0, 3 * w * h);
memset(alpha, 255, w * h);
img.Paste(copy, 0, 0, wxIMAGE_ALPHA_BLEND_COMPOSE);
return;
}
}
}
SymbolP import_symbol(Image& img) {
SymbolP symbol;
if (is_mse1_symbol(img)) {
+3
View File
@@ -23,6 +23,9 @@ bool is_mse1_symbol(const Image& img);
/// Convert an image to a symbol, destroys the image in the process
SymbolP image_to_symbol(Image& img);
/// If an image has alpha, try to save it if it has only one color
void fill_alpha(Image& img, int w, int h);
// ----------------------------------------------------------------------------- : Simplify symbol