From 64d5ac0d20b4dc7bb2213bfc33e1631bbd3d3e4d Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Mon, 4 Nov 2019 14:00:26 +0100 Subject: [PATCH] ENABLE_THUMBNAIL_GENERATOR -> Export thumbnails to gcode: max length of gcode lines set to 80 characters --- src/libslic3r/GCode.cpp | 22 ++++++++++++++++++++-- src/slic3r/GUI/GUI_App.cpp | 18 +++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 481e82da1..df3225f81 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -957,6 +957,8 @@ void GCode::_do_export(Print &print, FILE *file) // Write thumbnails using base64 encoding if (thumbnail_data != nullptr) { + const unsigned int max_row_length = 78; + for (const ThumbnailData& data : *thumbnail_data) { if (data.is_valid()) @@ -966,10 +968,26 @@ void GCode::_do_export(Print &print, FILE *file) size_t row_size = 4 * data.width; for (int r = (int)data.height - 1; r >= 0; --r) { - _write_format(file, "; %s\n", boost::beast::detail::base64_encode((const std::uint8_t*)(data.pixels.data() + r * row_size), row_size).c_str()); + std::string encoded = boost::beast::detail::base64_encode((const std::uint8_t*)(data.pixels.data() + r * row_size), row_size); + unsigned int row_count = 0; + while (encoded.length() > max_row_length) + { + if (row_count == 0) + _write_format(file, "; %s\n", encoded.substr(0, max_row_length).c_str()); + else + _write_format(file, ";>%s\n", encoded.substr(0, max_row_length).c_str()); + + encoded = encoded.substr(max_row_length); + ++row_count; + } + + if (row_count == 0) + _write_format(file, "; %s\n", encoded.c_str()); + else + _write_format(file, ";>%s\n", encoded.c_str()); } - _write(file, "; thumbnail end\n;\n\n"); + _write(file, "; thumbnail end\n;\n"); } print.throw_if_canceled(); } diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index c35985f11..8f0014a05 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -1106,6 +1106,7 @@ void GUI_App::gcode_thumbnails_debug() boost::nowide::ifstream file(in_filename.c_str()); std::vector rows; + std::string row; if (file.good()) { while (std::getline(file, gcode_line)) @@ -1121,9 +1122,16 @@ void GUI_App::gcode_thumbnails_debug() width = (unsigned int)::atoi(width_str.c_str()); std::string height_str = gcode_line.substr(x_pos + 1); height = (unsigned int)::atoi(height_str.c_str()); + row.clear(); } else if (reading_image && boost::starts_with(gcode_line, END_MASK)) { + if (!row.empty()) + { + rows.push_back(row); + row.clear(); + } + if ((unsigned int)rows.size() == height) { std::vector thumbnail(4 * width * height, 0); @@ -1160,7 +1168,15 @@ void GUI_App::gcode_thumbnails_debug() rows.clear(); } else if (reading_image) - rows.push_back(gcode_line.substr(2)); + { + if (!row.empty() && (gcode_line[1] == ' ')) + { + rows.push_back(row); + row.clear(); + } + + row += gcode_line.substr(2); + } } }