diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index df3225f81..6b773f4d3 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -35,6 +35,10 @@ #include +#if ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE +#include "miniz_extension.hpp" +#endif // ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE + #if 0 // Enable debugging and asserts, even in the release build. #define DEBUG @@ -963,6 +967,31 @@ void GCode::_do_export(Print &print, FILE *file) { if (data.is_valid()) { +#if ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE + size_t png_size = 0; + void* png_data = tdefl_write_image_to_png_file_in_memory_ex((const void*)data.pixels.data(), data.width, data.height, 4, &png_size, MZ_DEFAULT_LEVEL, 1); + if (png_data != nullptr) + { + _write_format(file, "\n;\n; thumbnail begin %dx%d\n", data.width, data.height); + + std::string encoded = boost::beast::detail::base64_encode((const std::uint8_t*)png_data, png_size); + + unsigned int row_count = 0; + while (encoded.length() > max_row_length) + { + _write_format(file, "; %s\n", encoded.substr(0, max_row_length).c_str()); + encoded = encoded.substr(max_row_length); + ++row_count; + } + + if (encoded.length() > 0) + _write_format(file, "; %s\n", encoded.c_str()); + + _write(file, "; thumbnail end\n;\n"); + + mz_free(png_data); + } +#else _write_format(file, "\n;\n; thumbnail begin %dx%d\n", data.width, data.height); size_t row_size = 4 * data.width; @@ -981,13 +1010,17 @@ void GCode::_do_export(Print &print, FILE *file) ++row_count; } - if (row_count == 0) - _write_format(file, "; %s\n", encoded.c_str()); - else - _write_format(file, ";>%s\n", encoded.c_str()); + if (encoded.length() > 0) + { + 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"); +#endif // ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE } print.throw_if_canceled(); } diff --git a/src/libslic3r/Technologies.hpp b/src/libslic3r/Technologies.hpp index 4b351adb2..15cbd3827 100644 --- a/src/libslic3r/Technologies.hpp +++ b/src/libslic3r/Technologies.hpp @@ -39,5 +39,6 @@ // Enable thumbnail generator #define ENABLE_THUMBNAIL_GENERATOR (1 && ENABLE_2_2_0_ALPHA1) +#define ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE (1 && ENABLE_THUMBNAIL_GENERATOR) #endif // _technologies_h_ diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 8f0014a05..07deff54e 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -1104,14 +1104,14 @@ void GUI_App::gcode_thumbnails_debug() std::string in_filename = into_u8(dialog.GetPath()); std::string out_path = boost::filesystem::path(in_filename).remove_filename().append(L"thumbnail").string(); - boost::nowide::ifstream file(in_filename.c_str()); + boost::nowide::ifstream in_file(in_filename.c_str()); std::vector rows; std::string row; - if (file.good()) + if (in_file.good()) { - while (std::getline(file, gcode_line)) + while (std::getline(in_file, gcode_line)) { - if (file.good()) + if (in_file.good()) { if (boost::starts_with(gcode_line, BEGIN_MASK)) { @@ -1126,6 +1126,16 @@ void GUI_App::gcode_thumbnails_debug() } else if (reading_image && boost::starts_with(gcode_line, END_MASK)) { +#if ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE + std::string out_filename = out_path + std::to_string(width) + "x" + std::to_string(height) + ".png"; + boost::nowide::ofstream out_file(out_filename.c_str(), std::ios::binary); + if (out_file.good()) + { + std::string decoded = boost::beast::detail::base64_decode(row); + out_file.write(decoded.c_str(), decoded.length()); + out_file.close(); + } +#else if (!row.empty()) { rows.push_back(row); @@ -1161,6 +1171,7 @@ void GUI_App::gcode_thumbnails_debug() image.SaveFile(out_path + std::to_string(width) + "x" + std::to_string(height) + ".png", wxBITMAP_TYPE_PNG); } +#endif // ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE reading_image = false; width = 0; @@ -1169,18 +1180,20 @@ void GUI_App::gcode_thumbnails_debug() } else if (reading_image) { +#if !ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE if (!row.empty() && (gcode_line[1] == ' ')) { rows.push_back(row); row.clear(); } +#endif // !ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE row += gcode_line.substr(2); } } } - file.close(); + in_file.close(); } } #endif // ENABLE_THUMBNAIL_GENERATOR