ENABLE_THUMBNAIL_GENERATOR -> Export thumbnails to gcode as png data in lines with max 80 characters length

This commit is contained in:
Enrico Turri 2019-11-04 15:38:15 +01:00
parent 065cdd878e
commit 76377ee0fe
3 changed files with 56 additions and 9 deletions

View file

@ -35,6 +35,10 @@
#include <Shiny/Shiny.h> #include <Shiny/Shiny.h>
#if ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE
#include "miniz_extension.hpp"
#endif // ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE
#if 0 #if 0
// Enable debugging and asserts, even in the release build. // Enable debugging and asserts, even in the release build.
#define DEBUG #define DEBUG
@ -963,6 +967,31 @@ void GCode::_do_export(Print &print, FILE *file)
{ {
if (data.is_valid()) 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); _write_format(file, "\n;\n; thumbnail begin %dx%d\n", data.width, data.height);
size_t row_size = 4 * data.width; size_t row_size = 4 * data.width;
@ -981,13 +1010,17 @@ void GCode::_do_export(Print &print, FILE *file)
++row_count; ++row_count;
} }
if (row_count == 0) if (encoded.length() > 0)
_write_format(file, "; %s\n", encoded.c_str()); {
else if (row_count == 0)
_write_format(file, ";>%s\n", encoded.c_str()); _write_format(file, "; %s\n", encoded.c_str());
else
_write_format(file, ";>%s\n", encoded.c_str());
}
} }
_write(file, "; thumbnail end\n;\n"); _write(file, "; thumbnail end\n;\n");
#endif // ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE
} }
print.throw_if_canceled(); print.throw_if_canceled();
} }

View file

@ -39,5 +39,6 @@
// Enable thumbnail generator // Enable thumbnail generator
#define ENABLE_THUMBNAIL_GENERATOR (1 && ENABLE_2_2_0_ALPHA1) #define ENABLE_THUMBNAIL_GENERATOR (1 && ENABLE_2_2_0_ALPHA1)
#define ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE (1 && ENABLE_THUMBNAIL_GENERATOR)
#endif // _technologies_h_ #endif // _technologies_h_

View file

@ -1104,14 +1104,14 @@ void GUI_App::gcode_thumbnails_debug()
std::string in_filename = into_u8(dialog.GetPath()); std::string in_filename = into_u8(dialog.GetPath());
std::string out_path = boost::filesystem::path(in_filename).remove_filename().append(L"thumbnail").string(); 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<std::string> rows; std::vector<std::string> rows;
std::string row; 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)) 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)) 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()) if (!row.empty())
{ {
rows.push_back(row); 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); 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; reading_image = false;
width = 0; width = 0;
@ -1169,18 +1180,20 @@ void GUI_App::gcode_thumbnails_debug()
} }
else if (reading_image) else if (reading_image)
{ {
#if !ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE
if (!row.empty() && (gcode_line[1] == ' ')) if (!row.empty() && (gcode_line[1] == ' '))
{ {
rows.push_back(row); rows.push_back(row);
row.clear(); row.clear();
} }
#endif // !ENABLE_THUMBNAIL_GENERATOR_PNG_TO_GCODE
row += gcode_line.substr(2); row += gcode_line.substr(2);
} }
} }
} }
file.close(); in_file.close();
} }
} }
#endif // ENABLE_THUMBNAIL_GENERATOR #endif // ENABLE_THUMBNAIL_GENERATOR