Skip to content

Commit

Permalink
creality thumbnail compatibility (jpg without tag)
Browse files Browse the repository at this point in the history
  • Loading branch information
supermerill committed Dec 27, 2023
1 parent b6c3262 commit 23c861f
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions resources/ui_layout/default/printer_fff.ui
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ group:Thumbnails
line:Thumbnail options
setting:thumbnails_format
setting:thumbnails_with_bed
setting:thumbnails_tag_format
setting:thumbnails_end_file
end_line
group:Advanced
Expand Down
1 change: 1 addition & 0 deletions resources/ui_layout/default/printer_sla.ui
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ group:Thumbnails
line:Options
setting:thumbnails_with_bed
setting:thumbnails_with_support
setting:thumbnails_tag_format
end_line
group:Print Host upload
build_printhost
Expand Down
1 change: 1 addition & 0 deletions resources/ui_layout/example/printer_fff.ui
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ group:Thumbnails
line:Thumbnail options
setting:thumbnails_format
setting:thumbnails_with_bed
setting:thumbnails_tag_format
setting:thumbnails_end_file
end_line
group:Advanced
Expand Down
1 change: 1 addition & 0 deletions resources/ui_layout/example/printer_sla.ui
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ group:Thumbnails
line:Options
setting:thumbnails_with_bed
setting:thumbnails_with_support
setting:thumbnails_tag_format
end_line
group:Print Host upload
build_printhost
Expand Down
8 changes: 6 additions & 2 deletions src/libslic3r/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,13 +1377,15 @@ void GCode::_do_export(Print& print_mod, GCodeOutputStream &file, ThumbnailsGene
const ConfigOptionBool* thumbnails_end_file = print.full_print_config().option<ConfigOptionBool>("thumbnails_end_file");
if(!thumbnails_end_file || !thumbnails_end_file->value) {
const ConfigOptionBool* thumbnails_with_bed = print.full_print_config().option<ConfigOptionBool>("thumbnails_with_bed");
const ConfigOptionBool* thumbnails_tag_with_format = print.full_print_config().option<ConfigOptionBool>("thumbnails_tag_format");
const ConfigOptionEnum<GCodeThumbnailsFormat>* thumbnails_format = print.full_print_config().option<ConfigOptionEnum<GCodeThumbnailsFormat>>("thumbnails_format");
// Unit tests or command line slicing may not define "thumbnails" or "thumbnails_format".
// If "thumbnails_format" is not defined, export to PNG.
GCodeThumbnails::export_thumbnails_to_file(thumbnail_cb,
print.full_print_config().option<ConfigOptionPoints>("thumbnails")->values,
thumbnails_with_bed == nullptr ? false : thumbnails_with_bed->value,
thumbnails_with_bed ? thumbnails_with_bed->value : false,
thumbnails_format ? thumbnails_format->value : GCodeThumbnailsFormat::PNG,
thumbnails_tag_with_format ? thumbnails_tag_with_format->value : false,
[&file](const char* sz) { file.write(sz); },
[&print]() { print.throw_if_canceled(); });
}
Expand Down Expand Up @@ -2085,13 +2087,15 @@ void GCode::_do_export(Print& print_mod, GCodeOutputStream &file, ThumbnailsGene
//print thumbnails at the end instead of the start if requested
if (thumbnails_end_file && thumbnails_end_file->value) {
const ConfigOptionBool* thumbnails_with_bed = print.full_print_config().option<ConfigOptionBool>("thumbnails_with_bed");
const ConfigOptionBool* thumbnails_tag_with_format = print.full_print_config().option<ConfigOptionBool>("thumbnails_tag_format");
const ConfigOptionEnum<GCodeThumbnailsFormat>* thumbnails_format = print.full_print_config().option<ConfigOptionEnum<GCodeThumbnailsFormat>>("thumbnails_format");
// Unit tests or command line slicing may not define "thumbnails" or "thumbnails_format".
// If "thumbnails_format" is not defined, export to PNG.
GCodeThumbnails::export_thumbnails_to_file(thumbnail_cb,
print.full_print_config().option<ConfigOptionPoints>("thumbnails")->values,
thumbnails_with_bed==nullptr? false:thumbnails_with_bed->value,
thumbnails_with_bed ? thumbnails_with_bed->value : false,
thumbnails_format ? thumbnails_format->value : GCodeThumbnailsFormat::PNG,
thumbnails_tag_with_format ? thumbnails_tag_with_format->value: false,
[&file](const char* sz) { file.write(sz); },
[&print]() { print.throw_if_canceled(); });
}
Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/GCode/Thumbnails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using namespace std::literals;
struct CompressedPNG : CompressedImageBuffer
{
~CompressedPNG() override { if (data) mz_free(data); }
std::string_view tag() const override { return "thumbnail"sv; }
std::string_view tag() const override { return "thumbnail_PNG"sv; }
};

struct CompressedJPG : CompressedImageBuffer
Expand Down
20 changes: 16 additions & 4 deletions src/libslic3r/GCode/Thumbnails.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Slic3r::GCodeThumbnails {

constexpr std::string_view EMPTY_TAG = "thumbnail";

struct CompressedImageBuffer
{
void *data { nullptr };
Expand All @@ -24,7 +26,13 @@ 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, bool with_bed, GCodeThumbnailsFormat format, WriteToOutput output, ThrowIfCanceledCallback throw_if_canceled)
inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb,
const std::vector<Vec2d> & sizes,
bool with_bed,
GCodeThumbnailsFormat format,
bool with_tag_format,
WriteToOutput output,
ThrowIfCanceledCallback throw_if_canceled)
{
// Write thumbnails using base64 encoding
if (thumbnail_cb != nullptr) {
Expand All @@ -43,7 +51,9 @@ inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb,
auto compressed = compress_thumbnail(data, format);
if (compressed->data && compressed->size) {
if (format == GCodeThumbnailsFormat::BIQU) {
output((boost::format("\n;\n; %s begin %dx%d %d\n") % compressed->tag() % data.width % data.height % (compressed->size - 1)).str().c_str());
output((boost::format("\n;\n; %s begin %dx%d %d\n")
% (with_tag_format ? compressed->tag() : EMPTY_TAG)
% data.width % data.height % (compressed->size - 1)).str().c_str());
//print size in hex
std::stringstream ss;
ss << std::setfill('0') << std::hex;
Expand All @@ -60,7 +70,9 @@ inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb,
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());
output((boost::format("\n;\n; %s begin %dx%d %d\n")
% (with_tag_format ? compressed->tag() : EMPTY_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);
Expand All @@ -69,7 +81,7 @@ inline void export_thumbnails_to_file(ThumbnailsGeneratorCallback &thumbnail_cb,
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());
output((boost::format("; %s end\n;\n") % (with_tag_format ? compressed->tag() : EMPTY_TAG)).str().c_str());
}
throw_if_canceled();
}
Expand Down
2 changes: 2 additions & 0 deletions src/libslic3r/Preset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ static std::vector<std::string> s_Preset_printer_options {
"thumbnails_custom_color",
"thumbnails_end_file",
"thumbnails_format",
"thumbnails_tag_format",
"thumbnails_with_bed",
"wipe_advanced",
"wipe_advanced_nozzle_melted_volume",
Expand Down Expand Up @@ -960,6 +961,7 @@ static std::vector<std::string> s_Preset_sla_printer_options {
"thumbnails_color",
"thumbnails_custom_color",
"thumbnails_with_bed",
"thumbnails_tag_format",
"thumbnails_with_support",
};

Expand Down
1 change: 1 addition & 0 deletions src/libslic3r/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ bool Print::invalidate_state_by_config_options(const ConfigOptionResolver& /* ne
"thumbnails_custom_color",
"thumbnails_end_file",
"thumbnails_format",
"thumbnails_tag_format",
"thumbnails_with_bed",
"time_estimation_compensation",
"time_cost",
Expand Down
15 changes: 14 additions & 1 deletion src/libslic3r/PrintConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ static const t_config_enum_values s_keys_map_GCodeThumbnailsFormat = {
{ "PNG", int(GCodeThumbnailsFormat::PNG) },
{ "JPG", int(GCodeThumbnailsFormat::JPG) },
{ "QOI", int(GCodeThumbnailsFormat::QOI) },
{ "BIQU", int(GCodeThumbnailsFormat::BIQU) }
{ "BIQU",int(GCodeThumbnailsFormat::BIQU) },
};
CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(GCodeThumbnailsFormat)

Expand Down Expand Up @@ -422,6 +422,13 @@ void PrintConfigDef::init_common_params()
def->enum_labels.push_back("Biqu");
def->set_default_value(new ConfigOptionEnum<GCodeThumbnailsFormat>(GCodeThumbnailsFormat::PNG));

def = this->add("thumbnails_tag_format", coBool);
def->label = L("Write the thumbnail type in gcode.");
def->tooltip = L("instead of writing 'thumbnails' as tag in the gcode, it will write 'thumbnails_PNG', thumbnails_JPG', 'thumbnail_QOI', etc.."
"\n Some firmware needs it to know how to decode the thumbnail, some others don't support it.");
def->mode = comExpert | comSuSi;
def->set_default_value(new ConfigOptionBool(false));

def = this->add("thumbnails_with_support", coBool);
def->label = L("Support on thumbnail");
def->tooltip = L("Show the supports (and pads) on the thumbnail picture.");
Expand Down Expand Up @@ -7756,6 +7763,11 @@ std::map<std::string,std::string> PrintConfigDef::from_prusa(t_config_option_key
if (value != "1")
output["default_fan_speed"] = "0";
}
if ("thumbnails_format" == opt_key) {
// by default, no thumbnails_tag_format for png output
if (value == "PNG")
output["thumbnails_tag_format"] = "0";
}

return output;
}
Expand Down Expand Up @@ -7989,6 +8001,7 @@ std::unordered_set<std::string> prusa_export_to_remove_keys = {
"thumbnails_color",
"thumbnails_custom_color",
"thumbnails_end_file",
"thumbnails_tag_format",
"thumbnails_with_bed",
"thumbnails_with_support",
"time_cost",
Expand Down
2 changes: 2 additions & 0 deletions src/libslic3r/PrintConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ PRINT_CONFIG_CLASS_DERIVED_DEFINE(
((ConfigOptionBool, thumbnails_custom_color))
((ConfigOptionBool, thumbnails_end_file))
((ConfigOptionEnum<GCodeThumbnailsFormat>, thumbnails_format))
((ConfigOptionBool, thumbnails_tag_format))
((ConfigOptionBool, thumbnails_with_bed))
((ConfigOptionPercent, time_estimation_compensation))
((ConfigOptionFloat, time_cost))
Expand Down Expand Up @@ -1483,6 +1484,7 @@ PRINT_CONFIG_CLASS_DEFINE(
((ConfigOptionPoints, thumbnails))
((ConfigOptionString, thumbnails_color))
((ConfigOptionBool, thumbnails_custom_color))
((ConfigOptionBool, thumbnails_tag_format))
((ConfigOptionBool, thumbnails_with_bed))
((ConfigOptionBool, thumbnails_with_support))
((ConfigOptionFloat, z_rotate))
Expand Down
1 change: 1 addition & 0 deletions src/libslic3r/SLAPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ bool SLAPrint::invalidate_state_by_config_options(const std::vector<t_config_opt
"thumbnails_color",
"thumbnails_custom_color",
"thumbnails_end_file",
"thumbnails_tag_format",
"thumbnails_with_bed",
"thumbnails_with_support"
};
Expand Down

0 comments on commit 23c861f

Please sign in to comment.