Thumbnails feature revamp. (#5555)

* Thumbnails feature revamp.
Support generating different size/format combinations
* misc fix

Co-authored-by: Lukas Matena <lukasmatena@seznam.cz>
This commit is contained in:
SoftFever 2024-06-03 21:30:38 +08:00 committed by GitHub
parent 50d00a1d54
commit c083541e0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 1145 additions and 854 deletions

View file

@ -1014,6 +1014,10 @@ int ConfigBase::load_from_json(const std::string &file, ConfigSubstitutionContex
}
}
}
// Do legacy conversion on a completely loaded dictionary.
// Perform composite conversions, for example merging multiple keys into one key.
this->handle_legacy_composite();
return 0;
}
catch (const std::ifstream::failure &err) {
@ -1103,6 +1107,9 @@ ConfigSubstitutions ConfigBase::load(const boost::property_tree::ptree &tree, Fo
// ignore
}
}
// Do legacy conversion on a completely loaded dictionary.
// Perform composite conversions, for example merging multiple keys into one key.
this->handle_legacy_composite();
return std::move(substitutions_ctxt.substitutions);
}
@ -1189,7 +1196,10 @@ size_t ConfigBase::load_from_gcode_string_legacy(ConfigBase& config, const char*
end = start;
}
// BBS
// Do legacy conversion on a completely loaded dictionary.
// Perform composite conversions, for example merging multiple keys into one key.
config.handle_legacy_composite();
free(result);
return num_key_value_pairs;
}
@ -1368,6 +1378,10 @@ ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &file, Fo
throw Slic3r::RuntimeError(format("Suspiciously low number of configuration values extracted from %1%: %2%", file, key_value_pairs));
}
// Do legacy conversion on a completely loaded dictionary.
// Perform composite conversions, for example merging multiple keys into one key.
this->handle_legacy_composite();
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": finished to parse_file %1%") % file.c_str();
return std::move(substitutions_ctxt.substitutions);
}

View file

@ -2057,6 +2057,10 @@ protected:
// If the opt_key is no more valid in this version of Slic3r, opt_key is cleared by handle_legacy().
// handle_legacy() is called internally by set_deserialize().
virtual void handle_legacy(t_config_option_key &/*opt_key*/, std::string &/*value*/) const {}
// Called after a config is loaded as a whole.
// Perform composite conversions, for example merging multiple keys into one key.
// For conversion of single options, the handle_legacy() method above is called.
virtual void handle_legacy_composite() {}
public:
using ConfigOptionResolver::option;

View file

@ -1978,19 +1978,6 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
if (!print.config().small_area_infill_flow_compensation_model.empty())
m_small_area_infill_flow_compensator = make_unique<SmallAreaInfillFlowCompensator>(print.config());
// if thumbnail type of BTT_TFT, insert above header
// if not, it is inserted under the header in its normal spot
GCodeThumbnailsFormat m_gcode_thumbnail_format = GCodeThumbnailsFormat::PNG;
if (thumbnail_cb != nullptr) {
m_gcode_thumbnail_format = print.full_print_config().opt_enum<GCodeThumbnailsFormat>("thumbnails_format");
if (m_gcode_thumbnail_format == GCodeThumbnailsFormat::BTT_TFT)
GCodeThumbnails::export_thumbnails_to_file(
thumbnail_cb, print.get_plate_index(), print.full_print_config().option<ConfigOptionPoints>("thumbnails")->values,
m_gcode_thumbnail_format,
[&file](const char *sz) { file.write(sz); },
[&print]() { print.throw_if_canceled(); });
}
file.write_format("; HEADER_BLOCK_START\n");
// Write information on the generator.
file.write_format("; generated by %s on %s\n", Slic3r::header_slic3r_generated().c_str(), Slic3r::Utils::local_timestamp().c_str());
@ -2066,15 +2053,18 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
print.config().nozzle_temperature_initial_layer.get_at(0));
file.write("; CONFIG_BLOCK_END\n\n");
} else if (thumbnail_cb != nullptr) {
if (m_gcode_thumbnail_format != GCodeThumbnailsFormat::BTT_TFT) {
auto thumbnaim_fmt = m_gcode_thumbnail_format;
// Orca: if the thumbnail format is ColPic, we write PNG in the beginning of gcode file and ColPic in the end of gcode file.
if (m_gcode_thumbnail_format == GCodeThumbnailsFormat::ColPic)
thumbnaim_fmt = GCodeThumbnailsFormat::PNG;
GCodeThumbnails::export_thumbnails_to_file(
thumbnail_cb, print.get_plate_index(), print.full_print_config().option<ConfigOptionPoints>("thumbnails")->values,
thumbnaim_fmt, [&file](const char* sz) { file.write(sz); }, [&print]() { print.throw_if_canceled(); });
// generate the thumbnails
auto [thumbnails, errors] = GCodeThumbnails::make_and_check_thumbnail_list(print.full_print_config());
if (errors != enum_bitmask<ThumbnailError>()) {
std::string error_str = format("Invalid thumbnails value:");
error_str += GCodeThumbnails::get_error_string(errors);
throw Slic3r::ExportError(error_str);
}
if (!thumbnails.empty())
GCodeThumbnails::export_thumbnails_to_file(
thumbnail_cb, print.get_plate_index(), thumbnails, [&file](const char* sz) { file.write(sz); }, [&print]() { print.throw_if_canceled(); });
}
}
@ -2781,11 +2771,6 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
GCodeProcessor::ETags::Estimated_Printing_Time_Placeholder)
.c_str());
file.write("\n");
if (m_gcode_thumbnail_format == GCodeThumbnailsFormat::ColPic)
GCodeThumbnails::export_thumbnails_to_file(
thumbnail_cb, print.get_plate_index(), print.full_print_config().option<ConfigOptionPoints>("thumbnails")->values,
m_gcode_thumbnail_format, [&file](const char* sz) { file.write(sz); }, [&print]() { print.throw_if_canceled(); });
file.write("; CONFIG_BLOCK_START\n");
std::string full_config;
append_full_config(print, full_config);

View file

@ -528,5 +528,81 @@ int ColPic_EncodeStr(unsigned short* fromcolor16, int picw, int pich, unsigned c
outputdata[qty] = 0;
return qty;
}
std::pair<GCodeThumbnailDefinitionsList, ThumbnailErrors> make_and_check_thumbnail_list(const std::string& thumbnails_string, const std::string_view def_ext /*= "PNG"sv*/)
{
if (thumbnails_string.empty())
return {};
std::istringstream is(thumbnails_string);
std::string point_str;
ThumbnailErrors errors;
// generate thumbnails data to process it
GCodeThumbnailDefinitionsList thumbnails_list;
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') && !coord_str.empty()) {
std::istringstream(coord_str) >> point(0);
if (std::getline(iss, coord_str, '/') && !coord_str.empty()) {
std::istringstream(coord_str) >> point(1);
if (0 < point(0) && point(0) < 1000 && 0 < point(1) && point(1) < 1000) {
std::string ext_str;
std::getline(iss, ext_str, '/');
if (ext_str.empty())
ext_str = def_ext.empty() ? "PNG"sv : def_ext;
// check validity of extention
boost::to_upper(ext_str);
if (!ConfigOptionEnum<GCodeThumbnailsFormat>::from_string(ext_str, format)) {
format = GCodeThumbnailsFormat::PNG;
errors = enum_bitmask(errors | ThumbnailError::InvalidExt);
}
thumbnails_list.emplace_back(std::make_pair(format, point));
}
else
errors = enum_bitmask(errors | ThumbnailError::OutOfRange);
continue;
}
}
errors = enum_bitmask(errors | ThumbnailError::InvalidVal);
}
return std::make_pair(std::move(thumbnails_list), errors);
}
std::pair<GCodeThumbnailDefinitionsList, ThumbnailErrors> make_and_check_thumbnail_list(const ConfigBase& config)
{
// ??? 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
if (const auto thumbnails_value = config.option<ConfigOptionString>("thumbnails"))
return make_and_check_thumbnail_list(thumbnails_value->value);
return {};
}
std::string get_error_string(const ThumbnailErrors& errors)
{
std::string error_str;
if (errors.has(ThumbnailError::InvalidVal))
error_str += "\n - " + format("Invalid input format. Expected vector of dimensions in the following format: \"%1%\"", "XxY/EXT, XxY/EXT, ...");
if (errors.has(ThumbnailError::OutOfRange))
error_str += "\n - Input value is out of range";
if (errors.has(ThumbnailError::InvalidExt))
error_str += "\n - Some extension in the input is invalid";
return error_str;
}
} // namespace Slic3r::GCodeThumbnails

View file

@ -7,7 +7,6 @@
#include "../Point.hpp"
#include "../PrintConfig.hpp"
#include "PrintConfig.hpp"
#include "ThumbnailData.hpp"
#include <vector>
@ -16,6 +15,12 @@
#include <boost/beast/core/detail/base64.hpp>
namespace Slic3r {
enum class ThumbnailError : int { InvalidVal, OutOfRange, InvalidExt };
using ThumbnailErrors = enum_bitmask<ThumbnailError>;
ENABLE_ENUM_BITMASK_OPERATORS(ThumbnailError);
}
namespace Slic3r::GCodeThumbnails {
struct CompressedImageBuffer
@ -29,20 +34,30 @@ struct CompressedImageBuffer
std::string get_hex(const unsigned int input);
std::string rjust(std::string input, unsigned int width, char fill_char);
std::unique_ptr<CompressedImageBuffer> compress_thumbnail(const ThumbnailData &data, GCodeThumbnailsFormat format);
std::string get_error_string(const ThumbnailErrors& errors);
typedef std::vector<std::pair<GCodeThumbnailsFormat, Vec2d>> GCodeThumbnailDefinitionsList;
using namespace std::literals;
std::pair<GCodeThumbnailDefinitionsList, ThumbnailErrors> make_and_check_thumbnail_list(const std::string& thumbnails_string, const std::string_view def_ext = "PNG"sv);
std::pair<GCodeThumbnailDefinitionsList, ThumbnailErrors> make_and_check_thumbnail_list(const ConfigBase &config);
template<typename WriteToOutput, typename ThrowIfCanceledCallback>
inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb,
int plate_id,
const std::vector<Vec2d> &sizes,
GCodeThumbnailsFormat format,
WriteToOutput output,
ThrowIfCanceledCallback throw_if_canceled)
inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback& thumbnail_cb,
int plate_id,
const std::vector<std::pair<GCodeThumbnailsFormat, Vec2d>>& thumbnails_list,
WriteToOutput output,
ThrowIfCanceledCallback throw_if_canceled)
{
// Write thumbnails using base64 encoding
if (thumbnail_cb != nullptr) {
if (thumbnail_cb == nullptr)
return;
short i = 0;
bool first_ColPic = true;
for (const auto& [format, size] : thumbnails_list) {
static constexpr const size_t max_row_length = 78;
ThumbnailsList thumbnails = thumbnail_cb(ThumbnailsParams{sizes, true, true, true, true, plate_id});
short i = 0;
ThumbnailsList thumbnails = thumbnail_cb(ThumbnailsParams{{size}, true, true, true, true, plate_id});
for (const ThumbnailData &data : thumbnails) {
if (data.is_valid()) {
auto compressed = compress_thumbnail(data, format);
@ -51,15 +66,16 @@ inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb,
// write BTT_TFT header
output((";" + rjust(get_hex(data.width), 4, '0') + rjust(get_hex(data.height), 4, '0') + "\r\n").c_str());
output((char *) compressed->data);
if (i == (thumbnails.size() - 1))
if (i == (thumbnails_list.size() - 1))
output("; bigtree thumbnail end\r\n\r\n");
}
else if (format == GCodeThumbnailsFormat::ColPic) {
if (i == 0) {
if (first_ColPic) {
output((boost::format("\n\n;gimage:%s\n\n") % reinterpret_cast<char*>(compressed->data)).str().c_str());
} else {
output((boost::format("\n\n;simage:%s\n\n") % reinterpret_cast<char*>(compressed->data)).str().c_str());
}
first_ColPic = false;
}
else {
output("; THUMBNAIL_BLOCK_START\n");
@ -84,10 +100,9 @@ inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb,
}
throw_if_canceled();
}
i++;
}
}
i++;
}
}

View file

@ -55,6 +55,7 @@
#include "Utils.hpp"
#include "Time.hpp"
#include "PlaceholderParser.hpp"
#include "libslic3r/GCode/Thumbnails.hpp"
using boost::property_tree::ptree;
@ -2637,6 +2638,16 @@ inline t_config_option_keys deep_diff(const ConfigBase &config_this, const Confi
} else if (opt_key == "default_filament_profile") {
// Ignore this field, it is not presented to the user, therefore showing a "modified" flag for this parameter does not help.
// Also the length of this field may differ, which may lead to a crash if the block below is used.
}
else if (opt_key == "thumbnails") {
// "thumbnails" can not contain extensions in old config but they are valid and use PNG extension by default
// So, check if "thumbnails" is really changed
// We will compare full thumbnails instead of exactly config values
auto [thumbnails, er] = GCodeThumbnails::make_and_check_thumbnail_list(config_this);
auto [thumbnails_new, er_new] = GCodeThumbnails::make_and_check_thumbnail_list(config_other);
if (thumbnails != thumbnails_new || er != er_new)
// if those strings are actually the same, erase them from the list of dirty oprions
diff.emplace_back(opt_key);
} else {
switch (other_opt->type()) {
case coInts: add_correct_opts_to_diff<ConfigOptionInts >(opt_key, diff, config_other, config_this); break;

View file

@ -24,6 +24,7 @@
#include "Config.hpp"
#include "I18N.hpp"
#include "GCode/Thumbnails.hpp"
#include <set>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/case_conv.hpp>
@ -422,7 +423,7 @@ static const t_config_enum_values s_keys_map_GCodeThumbnailsFormat = {
{ "JPG", int(GCodeThumbnailsFormat::JPG) },
{ "QOI", int(GCodeThumbnailsFormat::QOI) },
{ "BTT_TFT", int(GCodeThumbnailsFormat::BTT_TFT) },
{ "ColPic", int(GCodeThumbnailsFormat::ColPic) }
{ "COLPIC", int(GCodeThumbnailsFormat::ColPic) }
};
CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(GCodeThumbnailsFormat)
@ -4907,12 +4908,12 @@ def = this->add("filament_loading_speed", coFloats);
def->mode = comAdvanced;
def->set_default_value(new ConfigOptionBool(true));
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->mode = comAdvanced;
def->gui_type = ConfigOptionDef::GUIType::one_string;
def->set_default_value(new ConfigOptionPoints{Vec2d(300, 300)});
def->set_default_value(new ConfigOptionString("48x48/PNG,300x300/PNG"));
def = this->add("thumbnails_format", coEnum);
def->label = L("Format of G-code thumbnails");
@ -4923,7 +4924,7 @@ def = this->add("filament_loading_speed", coFloats);
def->enum_values.push_back("JPG");
def->enum_values.push_back("QOI");
def->enum_values.push_back("BTT_TFT");
def->enum_values.push_back("ColPic");
def->enum_values.push_back("COLPIC");
def->enum_labels.push_back("PNG");
def->enum_labels.push_back("JPG");
def->enum_labels.push_back("QOI");
@ -5950,6 +5951,60 @@ void PrintConfigDef::handle_legacy(t_config_option_key &opt_key, std::string &va
}
}
// Called after a config is loaded as a whole.
// Perform composite conversions, for example merging multiple keys into one key.
// Don't convert single options here, implement such conversion in PrintConfigDef::handle_legacy() instead.
void PrintConfigDef::handle_legacy_composite(DynamicPrintConfig &config)
{
if (config.has("thumbnails")) {
std::string extention;
if (config.has("thumbnails_format")) {
if (const ConfigOptionDef* opt = config.def()->get("thumbnails_format")) {
extention = opt->enum_values.at(config.option("thumbnails_format")->getInt());
}
}
std::string thumbnails_str = config.opt_string("thumbnails");
auto [thumbnails_list, errors] = GCodeThumbnails::make_and_check_thumbnail_list(thumbnails_str, extention);
if (errors != enum_bitmask<ThumbnailError>()) {
std::string error_str = "\n" + format("Invalid value provided for parameter %1%: %2%", "thumbnails", thumbnails_str);
error_str += GCodeThumbnails::get_error_string(errors);
throw BadOptionValueException(error_str);
}
if (!thumbnails_list.empty()) {
const auto& extentions = ConfigOptionEnum<GCodeThumbnailsFormat>::get_enum_names();
thumbnails_str.clear();
for (const auto& [ext, size] : thumbnails_list)
thumbnails_str += format("%1%x%2%/%3%, ", size.x(), size.y(), extentions[int(ext)]);
thumbnails_str.resize(thumbnails_str.length() - 2);
config.set_key_value("thumbnails", new ConfigOptionString(thumbnails_str));
}
}
if (config.has("wiping_volumes_matrix") && !config.has("wiping_volumes_use_custom_matrix")) {
// This is apparently some pre-2.7.3 config, where the wiping_volumes_matrix was always used.
// The 2.7.3 introduced an option to use defaults derived from config. In case the matrix
// contains only default values, switch it to default behaviour. The default values
// were zeros on the diagonal and 140 otherwise.
std::vector<double> matrix = config.opt<ConfigOptionFloats>("wiping_volumes_matrix")->values;
int num_of_extruders = int(std::sqrt(matrix.size()) + 0.5);
int i = -1;
bool custom = false;
for (int j = 0; j < int(matrix.size()); ++j) {
if (j % num_of_extruders == 0)
++i;
if (i != j % num_of_extruders && !is_approx(matrix[j], 140.)) {
custom = true;
break;
}
}
config.set_key_value("wiping_volumes_use_custom_matrix", new ConfigOptionBool(custom));
}
}
const PrintConfigDef print_config_def;
DynamicPrintConfig DynamicPrintConfig::full_print_config()

View file

@ -419,6 +419,8 @@ CONFIG_OPTION_ENUM_DECLARE_STATIC_MAPS(AuthorizationType)
CONFIG_OPTION_ENUM_DECLARE_STATIC_MAPS(PerimeterGeneratorType)
#undef CONFIG_OPTION_ENUM_DECLARE_STATIC_MAPS
class DynamicPrintConfig;
// Defines each and every confiuration option of Slic3r, including the properties of the GUI dialogs.
// Does not store the actual values, but defines default values.
class PrintConfigDef : public ConfigDef
@ -427,6 +429,7 @@ public:
PrintConfigDef();
static void handle_legacy(t_config_option_key &opt_key, std::string &value);
static void handle_legacy_composite(DynamicPrintConfig &config);
// Array options growing with the number of extruders
const std::vector<std::string>& extruder_option_keys() const { return m_extruder_option_keys; }
@ -509,6 +512,12 @@ public:
void handle_legacy(t_config_option_key &opt_key, std::string &value) const override
{ PrintConfigDef::handle_legacy(opt_key, value); }
// Called after a config is loaded as a whole.
// Perform composite conversions, for example merging multiple keys into one key.
// For conversion of single options, the handle_legacy() method above is called.
void handle_legacy_composite() override
{ PrintConfigDef::handle_legacy_composite(*this); }
//BBS special case Support G/ Support W
std::string get_filament_type(std::string &displayed_filament_type, int id = 0);
@ -1256,7 +1265,7 @@ PRINT_CONFIG_CLASS_DERIVED_DEFINE(
((ConfigOptionFloat, nozzle_volume))
((ConfigOptionPoints, start_end_points))
((ConfigOptionEnum<TimelapseType>, timelapse_type))
((ConfigOptionPoints, thumbnails))
((ConfigOptionString, thumbnails))
// BBS: move from PrintObjectConfig
((ConfigOptionBool, independent_support_layer_height))
// SoftFever