Skip to content

Commit

Permalink
WIP: Thumbnails improvements: Merge "thumbnails" and "thumbnails_form…
Browse files Browse the repository at this point in the history
…at" options into just one "thumbnails" option with coString type
  • Loading branch information
YuSanka committed Aug 29, 2023
1 parent 3cfe2f4 commit a6dea25
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 101 deletions.
39 changes: 31 additions & 8 deletions src/libslic3r/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,14 +886,37 @@ void GCodeGenerator::_do_export(Print& print, GCodeOutputStream &file, Thumbnail
// Write information on the generator.
file.write_format("; %s\n\n", Slic3r::header_slic3r_generated().c_str());

// Unit tests or command line slicing may not define "thumbnails" or "thumbnails_format".
// If "thumbnails_format" is not defined, export to PNG.
if (const auto [thumbnails, thumbnails_format] = std::make_pair(
print.full_print_config().option<ConfigOptionPoints>("thumbnails"),
print.full_print_config().option<ConfigOptionEnum<GCodeThumbnailsFormat>>("thumbnails_format"));
thumbnails)
GCodeThumbnails::export_thumbnails_to_file(
thumbnail_cb, thumbnails->values, thumbnails_format ? thumbnails_format->value : GCodeThumbnailsFormat::PNG,
// ??? Unit tests or command line slicing may not define "thumbnails" or "thumbnails_format".
// ??? If "thumbnails_format" is not defined, export to PNG.

// generate thumbnails data to process it

std::vector<std::pair<GCodeThumbnailsFormat, Vec2d>> thumbnails_list;
if (const auto thumbnails_value = print.full_print_config().option<ConfigOptionString>("thumbnails")) {
std::string str = thumbnails_value->value;
std::istringstream is(str);
std::string point_str;
while (std::getline(is, point_str, ',')) {
Vec2d point(Vec2d::Zero());
GCodeThumbnailsFormat format;
std::istringstream iss(point_str);
std::string coord_str;
if (std::getline(iss, coord_str, 'x')) {
std::istringstream(coord_str) >> point(0);
if (std::getline(iss, coord_str, '/')) {
std::istringstream(coord_str) >> point(1);
std::string ext_str;
if (std::getline(iss, ext_str, '/'))
format = ext_str == "JPG" ? GCodeThumbnailsFormat::JPG :
ext_str == "QOI" ? GCodeThumbnailsFormat::QOI :GCodeThumbnailsFormat::PNG;
}
}
thumbnails_list.emplace_back(std::make_pair(format, point));
}
}

if (!thumbnails_list.empty())
GCodeThumbnails::export_thumbnails_to_file(thumbnail_cb, thumbnails_list,
[&file](const char* sz) { file.write(sz); },
[&print]() { print.throw_if_canceled(); });

Expand Down
48 changes: 25 additions & 23 deletions src/libslic3r/GCode/Thumbnails.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,36 @@ struct CompressedImageBuffer
std::unique_ptr<CompressedImageBuffer> compress_thumbnail(const ThumbnailData &data, GCodeThumbnailsFormat format);

template<typename WriteToOutput, typename ThrowIfCanceledCallback>
inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb, const std::vector<Vec2d> &sizes, GCodeThumbnailsFormat format, WriteToOutput output, ThrowIfCanceledCallback throw_if_canceled)
inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb, const std::vector<std::pair<GCodeThumbnailsFormat, Vec2d>>& thumbnails_list, WriteToOutput output, ThrowIfCanceledCallback throw_if_canceled)
{
// Write thumbnails using base64 encoding
if (thumbnail_cb != nullptr) {
static constexpr const size_t max_row_length = 78;
ThumbnailsList thumbnails = thumbnail_cb(ThumbnailsParams{ sizes, true, true, true, true });
for (const ThumbnailData& data : thumbnails)
if (data.is_valid()) {
auto compressed = compress_thumbnail(data, format);
if (compressed->data && compressed->size) {
std::string encoded;
encoded.resize(boost::beast::detail::base64::encoded_size(compressed->size));
encoded.resize(boost::beast::detail::base64::encode((void*)encoded.data(), (const void*)compressed->data, compressed->size));

output((boost::format("\n;\n; %s begin %dx%d %d\n") % compressed->tag() % data.width % data.height % encoded.size()).str().c_str());

while (encoded.size() > max_row_length) {
output((boost::format("; %s\n") % encoded.substr(0, max_row_length)).str().c_str());
encoded = encoded.substr(max_row_length);
for (const auto& [format, size] : thumbnails_list) {
static constexpr const size_t max_row_length = 78;
ThumbnailsList thumbnails = thumbnail_cb(ThumbnailsParams{ {size}, true, true, true, true });
for (const ThumbnailData& data : thumbnails)
if (data.is_valid()) {
auto compressed = compress_thumbnail(data, format);
if (compressed->data && compressed->size) {
std::string encoded;
encoded.resize(boost::beast::detail::base64::encoded_size(compressed->size));
encoded.resize(boost::beast::detail::base64::encode((void*)encoded.data(), (const void*)compressed->data, compressed->size));

output((boost::format("\n;\n; %s begin %dx%d %d\n") % compressed->tag() % data.width % data.height % encoded.size()).str().c_str());

while (encoded.size() > max_row_length) {
output((boost::format("; %s\n") % encoded.substr(0, max_row_length)).str().c_str());
encoded = encoded.substr(max_row_length);
}

if (encoded.size() > 0)
output((boost::format("; %s\n") % encoded).str().c_str());

output((boost::format("; %s end\n;\n") % compressed->tag()).str().c_str());
}

if (encoded.size() > 0)
output((boost::format("; %s\n") % encoded).str().c_str());

output((boost::format("; %s end\n;\n") % compressed->tag()).str().c_str());
throw_if_canceled();
}
throw_if_canceled();
}
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/libslic3r/PrintConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ void PrintConfigDef::init_common_params()
def->mode = comAdvanced;
def->set_default_value(new ConfigOptionFloat(0.));

def = this->add("thumbnails", coPoints);
def = this->add("thumbnails", coString);
def->label = L("G-code thumbnails");
def->tooltip = L("Picture sizes to be stored into a .gcode and .sl1 / .sl1s files, in the following format: \"XxY, XxY, ...\"");
def->tooltip = L("Picture sizes to be stored into a .gcode and .sl1 / .sl1s files, in the following format: \"XxYxEXT, XxYxEXT, ...\"");
def->mode = comExpert;
def->gui_type = ConfigOptionDef::GUIType::one_string;
def->set_default_value(new ConfigOptionPoints());
def->set_default_value(new ConfigOptionString());

def = this->add("thumbnails_format", coEnum);
def->label = L("Format of G-code thumbnails");
Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/PrintConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ PRINT_CONFIG_CLASS_DERIVED_DEFINE(
((ConfigOptionInt, standby_temperature_delta))
((ConfigOptionInts, temperature))
((ConfigOptionInt, threads))
((ConfigOptionPoints, thumbnails))
((ConfigOptionString, thumbnails))
((ConfigOptionEnum<GCodeThumbnailsFormat>, thumbnails_format))
((ConfigOptionFloat, top_solid_infill_acceleration))
((ConfigOptionFloat, travel_acceleration))
Expand Down
160 changes: 104 additions & 56 deletions src/slic3r/GUI/Field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "format.hpp"

#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/enum_bitmask.hpp"

#include <regex>
#include <wx/numformatter.h>
Expand All @@ -26,7 +27,13 @@
#define wxOSX false
#endif

namespace Slic3r { namespace GUI {
namespace Slic3r {

enum class ThumbnailError : int { InvalidVal, OutOfRange, InvalidExt };
using ThumbnailErrors = enum_bitmask<ThumbnailError>;
ENABLE_ENUM_BITMASK_OPERATORS(ThumbnailError);

namespace GUI {

wxString double_to_string(double const value, const int max_precision /*= 4*/)
{
Expand Down Expand Up @@ -58,14 +65,79 @@ wxString double_to_string(double const value, const int max_precision /*= 4*/)
return s;
}

wxString get_thumbnails_string(const std::vector<Vec2d>& values)
bool is_valid_thumbnails_extention(wxString& ext)
{
wxString ret_str;
for (size_t i = 0; i < values.size(); ++ i) {
const Vec2d& el = values[i];
ret_str += wxString::Format((i == 0) ? "%ix%i" : ", %ix%i", int(el[0]), int(el[1]));
}
return ret_str;
ext.UpperCase();
static const std::vector<wxString> extentions = { "PNG", "JPG", "QOI" };

return std::find(extentions.begin(), extentions.end(), ext) != extentions.end();
}

ThumbnailErrors validate_thumbnails_string(wxString& str, const wxString& def_ext = "PNG")
{
bool invalid_val, out_of_range_val, invalid_ext;
invalid_val = out_of_range_val = invalid_ext = false;
str.Replace(" ", wxEmptyString, true);

if (!str.IsEmpty()) {

std::vector<std::pair<Vec2d, std::string>> out_thumbnails;

wxStringTokenizer thumbnails(str, ",");
while (thumbnails.HasMoreTokens()) {
wxString token = thumbnails.GetNextToken();
double x, y;
wxStringTokenizer thumbnail(token, "x");
if (thumbnail.HasMoreTokens()) {
wxString x_str = thumbnail.GetNextToken();
if (x_str.ToDouble(&x) && thumbnail.HasMoreTokens()) {
wxStringTokenizer y_and_ext(thumbnail.GetNextToken(), "/");

wxString y_str = y_and_ext.GetNextToken();
if (y_str.ToDouble(&y)) {
// thumbnail has no extension
if (0 < x && x < 1000 && 0 < y && y < 1000) {
wxString ext = y_and_ext.HasMoreTokens() ? y_and_ext.GetNextToken() : def_ext;
bool is_valid_ext = is_valid_thumbnails_extention(ext);
invalid_ext |= !is_valid_ext;
out_thumbnails.push_back({ Vec2d(x, y), into_u8(is_valid_ext ? ext : def_ext) });
continue;
}
out_of_range_val |= true;
continue;
}
}
}
invalid_val |= true;
}

str.Clear();
for (const auto& [size, ext] : out_thumbnails)
str += format_wxstr("%1%x%2%/%3%, ", size.x(), size.y(), ext);
str.resize(str.Len()-2);
}

ThumbnailErrors errors = only_if(invalid_val, ThumbnailError::InvalidVal) |
only_if(invalid_ext, ThumbnailError::InvalidExt) |
only_if(out_of_range_val, ThumbnailError::OutOfRange);

return errors;
}

wxString get_valid_thumbnails_string(const DynamicPrintConfig& config)
{
// >>> ysFIXME - temporary code, till "thumbnails_format" options exists in config
wxString format = "PNG";
if (const ConfigOptionDef* opt = config.def()->get("thumbnails_format"))
if (auto label = opt->enum_def->enum_to_label(config.option("thumbnails_format")->getInt());
label.has_value())
format = from_u8(*label);
// <<<

wxString str = from_u8(config.opt_string("thumbnails"));
validate_thumbnails_string(str, format);

return str;
}


Expand Down Expand Up @@ -355,56 +427,35 @@ void Field::get_value_by_opt_type(wxString& str, const bool check_value/* = true
}
}

m_value = into_u8(str);
break; }

case coPoints: {
std::vector<Vec2d> out_values;
str.Replace(" ", wxEmptyString, true);
if (!str.IsEmpty()) {
bool invalid_val = false;
bool out_of_range_val = false;
wxStringTokenizer thumbnails(str, ",");
while (thumbnails.HasMoreTokens()) {
wxString token = thumbnails.GetNextToken();
double x, y;
wxStringTokenizer thumbnail(token, "x");
if (thumbnail.HasMoreTokens()) {
wxString x_str = thumbnail.GetNextToken();
if (x_str.ToDouble(&x) && thumbnail.HasMoreTokens()) {
wxString y_str = thumbnail.GetNextToken();
if (y_str.ToDouble(&y) && !thumbnail.HasMoreTokens()) {
if (0 < x && x < 1000 && 0 < y && y < 1000) {
out_values.push_back(Vec2d(x, y));
continue;
}
out_of_range_val = true;
break;
}
}
if (m_opt.opt_key == "thumbnails") {
wxString str_out = str;
ThumbnailErrors errors = validate_thumbnails_string(str_out);
if (errors != enum_bitmask<ThumbnailError>()) {
set_value(str_out, true);
wxString error_str;
if (errors.has(ThumbnailError::InvalidVal))
error_str += format_wxstr(_L("Invalid input format. Expected vector of dimensions in the following format: \"%1%\""), "XxYxEXT, XxYxEXT, ...");
if (errors.has(ThumbnailError::OutOfRange)) {
if (!error_str.empty())
error_str += "\n\n";
error_str += _L("Input value is out of range");
}
invalid_val = true;
break;
}

if (out_of_range_val) {
wxString text_value;
if (!m_value.empty())
text_value = get_thumbnails_string(boost::any_cast<std::vector<Vec2d>>(m_value));
set_value(text_value, true);
show_error(m_parent, _L("Input value is out of range"));
if (errors.has(ThumbnailError::InvalidExt)) {
if (!error_str.empty())
error_str += "\n\n";
error_str += _L("Some input extention is invalid");
}
show_error(m_parent, error_str);
}
else if (invalid_val) {
wxString text_value;
if (!m_value.empty())
text_value = get_thumbnails_string(boost::any_cast<std::vector<Vec2d>>(m_value));
set_value(text_value, true);
show_error(m_parent, format_wxstr(_L("Invalid input format. Expected vector of dimensions in the following format: \"%1%\""),"XxY, XxY, ..." ));
else if (str_out != str) {
str = str_out;
set_value(str, true);
}
}

m_value = out_values;
break; }
m_value = into_u8(str);
break;
}

default:
break;
Expand Down Expand Up @@ -484,9 +535,6 @@ void TextCtrl::BUILD() {
text_value = vec->get_at(m_opt_idx);
break;
}
case coPoints:
text_value = get_thumbnails_string(m_opt.get_default_value<ConfigOptionPoints>()->values);
break;
default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/slic3r/GUI/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ using t_change = std::function<void(const t_config_option_key&, const boost::any
using t_back_to_init = std::function<void(const std::string&)>;

wxString double_to_string(double const value, const int max_precision = 4);
wxString get_thumbnails_string(const std::vector<Vec2d>& values);
wxString get_valid_thumbnails_string(const DynamicPrintConfig& config);

class UndoValueUIManager
{
Expand Down
12 changes: 7 additions & 5 deletions src/slic3r/GUI/OptionsGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,13 @@ boost::any ConfigOptionsGroup::get_config_value(const DynamicPrintConfig& config
ret = double_to_string(val);
}
break;
case coString:
ret = from_u8(config.opt_string(opt_key));
break;
case coString: {
if (opt_key == "thumbnails")
ret = get_valid_thumbnails_string(config);
else
ret = from_u8(config.opt_string(opt_key));
break;
}
case coStrings:
if (opt_key == "compatible_printers" || opt_key == "compatible_prints" || opt_key == "gcode_substitutions") {
ret = config.option<ConfigOptionStrings>(opt_key)->values;
Expand Down Expand Up @@ -946,8 +950,6 @@ boost::any ConfigOptionsGroup::get_config_value(const DynamicPrintConfig& config
case coPoints:
if (opt_key == "bed_shape")
ret = config.option<ConfigOptionPoints>(opt_key)->values;
else if (opt_key == "thumbnails")
ret = get_thumbnails_string(config.option<ConfigOptionPoints>(opt_key)->values);
else
ret = config.option<ConfigOptionPoints>(opt_key)->get_at(idx);
break;
Expand Down
16 changes: 15 additions & 1 deletion src/slic3r/GUI/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,21 @@ void Tab::update_changed_ui()
if (tab->m_sys_extruders_count != tab->m_extruders_count)
nonsys_options.emplace_back("extruders_count");
}

// "thumbnails" can not containe a extentions in old config but are valid and use PNG extention by default
// So, check if "thumbnails" is really changed
// We will compare full strings for thumnails instead of exactly config values
{
auto check_thumbnails_option = [](std::vector<std::string>& keys, const DynamicPrintConfig& config, const DynamicPrintConfig& config_new) {
if (auto it = std::find(keys.begin(), keys.end(), "thumbnails"); it != keys.end())
if (get_valid_thumbnails_string(config) == get_valid_thumbnails_string(config_new))
// if those strings are actually the same, erase them from the list of dirty oprions
keys.erase(it);
};
check_thumbnails_option(dirty_options, m_presets->get_edited_preset().config, m_presets->get_selected_preset().config);
if (const Preset* parent_preset = m_presets->get_selected_preset_parent())
check_thumbnails_option(nonsys_options, m_presets->get_edited_preset().config, parent_preset->config);
}
}

for (auto& it : m_options_list)
Expand Down Expand Up @@ -2526,7 +2541,6 @@ void TabPrinter::build_fff()
option = optgroup->get_option("thumbnails");
option.opt.full_width = true;
optgroup->append_single_option_line(option);
optgroup->append_single_option_line("thumbnails_format");

optgroup->append_single_option_line("silent_mode");
optgroup->append_single_option_line("remaining_times");
Expand Down
Loading

0 comments on commit a6dea25

Please sign in to comment.