From 18894644aa7326b1efa0b5936f04cb6a0bd5fa17 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Wed, 19 Feb 2025 22:59:31 +0100 Subject: [PATCH 1/2] SPE-2691: Write 'prepared by' info from an env var, updated libbgcode --- deps/+LibBGCode/LibBGCode.cmake | 4 ++-- src/libslic3r/GCode.cpp | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/deps/+LibBGCode/LibBGCode.cmake b/deps/+LibBGCode/LibBGCode.cmake index 56f5525b7e..b4cf62f12f 100644 --- a/deps/+LibBGCode/LibBGCode.cmake +++ b/deps/+LibBGCode/LibBGCode.cmake @@ -1,8 +1,8 @@ set(LibBGCode_SOURCE_DIR "" CACHE PATH "Optionally specify local LibBGCode source directory") set(_source_dir_line - URL https://github.com/prusa3d/libbgcode/archive/d33a277a3ce2c0a7f9ba325caac6d730e0f7a412.zip - URL_HASH SHA256=0db3b0852df8d3ae32a4283884bc4ad6f1dd4d3c748ed679491f70b1e1d1acd5) + URL https://github.com/prusa3d/libbgcode/archive/5041c093b33e2748e76d6b326f2251310823f3df.zip + URL_HASH SHA256=c323aa196a82d75f08a5b114c95f2d1a019e84b555a196e55d8ea52e5787284c) if (LibBGCode_SOURCE_DIR) set(_source_dir_line "SOURCE_DIR;${LibBGCode_SOURCE_DIR};BUILD_ALWAYS;ON") diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 3906a05304..493b8fed6d 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -930,6 +930,18 @@ static inline std::optional find_M84(const std::string &gcode) { void GCodeGenerator::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGeneratorCallback thumbnail_cb) { const bool export_to_binary_gcode = print.full_print_config().option("binary_gcode")->value; + + std::string prepared_by_info; + if (const char* extras = boost::nowide::getenv("SLIC3R_PREPARED_BY_INFO"); extras) { + std::string str(extras); + if (str.size() < 50 && std::all_of(str.begin(), str.end(), [](char c) { return c < 127 && c != '\n' && c != '\r'; })) + prepared_by_info = extras; + else { + BOOST_LOG_TRIVIAL(error) << "Value in SLIC3R_PREPARED_BY_INFO env variable is invalid. Closing."; + std::terminate(); + } + } + // if exporting gcode in binary format: // we generate here the data to be passed to the post-processor, who is responsible to export them to file // 1) generate the thumbnails @@ -955,6 +967,8 @@ void GCodeGenerator::_do_export(Print& print, GCodeOutputStream &file, Thumbnail // file data binary_data.file_metadata.raw_data.emplace_back("Producer", std::string(SLIC3R_APP_NAME) + " " + std::string(SLIC3R_VERSION)); binary_data.file_metadata.raw_data.emplace_back("Produced on", Utils::utc_timestamp()); + if (! prepared_by_info.empty()) + binary_data.file_metadata.raw_data.emplace_back("Prepared by", prepared_by_info); // config data encode_full_config(*m_print, binary_data.slicer_metadata.raw_data); @@ -1024,9 +1038,13 @@ void GCodeGenerator::_do_export(Print& print, GCodeOutputStream &file, Thumbnail this->m_avoid_crossing_curled_overhangs.init_bed_shape(get_bed_shape(print.config())); } - if (!export_to_binary_gcode) + if (!export_to_binary_gcode) { // Write information on the generator. - file.write_format("; %s\n\n", Slic3r::header_slic3r_generated().c_str()); + file.write_format("; %s\n", Slic3r::header_slic3r_generated().c_str()); + if (! prepared_by_info.empty()) + file.write_format("; prepared by %s\n", prepared_by_info.c_str()); + file.write_format("\n"); + } if (! export_to_binary_gcode) { // if exporting gcode in ascii format, generate the thumbnails here From 8350e395a7d4526fd56a4ac7d33565cfafb58717 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Thu, 20 Feb 2025 14:18:11 +0100 Subject: [PATCH 2/2] SPE-2691: Fixed resizing of thumbnails to maintain aspect ratio of the image --- src/CLI/ProcessActions.cpp | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/CLI/ProcessActions.cpp b/src/CLI/ProcessActions.cpp index 3c36dd5f96..14dd7ec89a 100644 --- a/src/CLI/ProcessActions.cpp +++ b/src/CLI/ProcessActions.cpp @@ -171,6 +171,34 @@ static bool export_models(std::vector& models, IO::ExportFormat format, c return true; } + +static ThumbnailData resize_and_crop(const std::vector& data, int width, int height, int width_new, int height_new) { + ThumbnailData th; + + float scale_x = float(width_new) / width; + float scale_y = float(height_new) / height; + float scale = std::max(scale_x, scale_y); // Choose the larger scale to fill the box + int resized_width = int(width * scale); + int resized_height = int(height * scale); + + std::vector resized_rgba(resized_width * resized_height * 4); + stbir_resize_uint8_linear(data.data(), width, height, 4 * width, + resized_rgba.data(), resized_width, resized_height, 4 * resized_width, + STBIR_RGBA); + + th.set(width_new, height_new); + int crop_x = (resized_width - width_new) / 2; + int crop_y = (resized_height - height_new) / 2; + + for (int y = 0; y < height_new; ++y) { + std::memcpy(th.pixels.data() + y * width_new * 4, + resized_rgba.data() + ((y + crop_y) * resized_width + crop_x) * 4, + width_new * 4); + } + return th; +} + + static std::function get_thumbnail_generator_cli(const std::string& filename) { if (boost::iends_with(filename, ".3mf")) { @@ -214,15 +242,8 @@ static std::function get_thumbnail_gene } for (const Vec2d& size : params.sizes) { - ThumbnailData th; Point isize(size); - th.set(isize.x(), isize.y()); - std::vector resized_rgba(th.width * th.height * 4); - stbir_resize_uint8_linear(data.data(), width, height, 4 * width, - resized_rgba.data(), th.width, th.height, 4 * th.width, - STBIR_RGBA); - th.pixels = resized_rgba; - list_out.push_back(th); + list_out.push_back(resize_and_crop(data, width, height, isize.x(), isize.y())); } return list_out; };