From eb8cb63d8875daea30f2800891b4b6451d9b5712 Mon Sep 17 00:00:00 2001 From: Stone Li Date: Thu, 31 Aug 2023 11:12:56 +0800 Subject: [PATCH] NEW: generate small thumbnail size in 3mf JIRA: STUDIO-4282 Change-Id: I2f6e35bd643e950d15dcfacb9f6ecae8f8bd80fa Signed-off-by: Stone Li --- src/libslic3r/Format/bbs_3mf.cpp | 33 +++++++++++++++++++++++++++++--- src/libslic3r/Format/bbs_3mf.hpp | 4 ++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/libslic3r/Format/bbs_3mf.cpp b/src/libslic3r/Format/bbs_3mf.cpp index a3f055fd8..b329a94d2 100644 --- a/src/libslic3r/Format/bbs_3mf.cpp +++ b/src/libslic3r/Format/bbs_3mf.cpp @@ -5229,7 +5229,7 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result) bool _add_content_types_file_to_archive(mz_zip_archive& archive); - bool _add_thumbnail_file_to_archive(mz_zip_archive& archive, const ThumbnailData& thumbnail_data, const char* local_path, int index); + bool _add_thumbnail_file_to_archive(mz_zip_archive& archive, const ThumbnailData& thumbnail_data, const char* local_path, int index, bool generate_small_thumbnail = false); bool _add_calibration_file_to_archive(mz_zip_archive& archive, const ThumbnailData& thumbnail_data, int index); bool _add_bbox_file_to_archive(mz_zip_archive& archive, const PlateBBoxData& id_bboxes, int index); bool _add_relationships_file_to_archive(mz_zip_archive & archive, @@ -5477,9 +5477,10 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result) { if (thumbnail_data[index]->is_valid()) { - if (!_add_thumbnail_file_to_archive(archive, *thumbnail_data[index], "Metadata/plate", index)) { + if (!_add_thumbnail_file_to_archive(archive, *thumbnail_data[index], "Metadata/plate", index, true)) { return false; } + BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ":" <<__LINE__ << boost::format(",add thumbnail %1%'s data into 3mf")%(index+1); thumbnail_status[index] = true; } @@ -5852,7 +5853,7 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result) return true; } - bool _BBS_3MF_Exporter::_add_thumbnail_file_to_archive(mz_zip_archive& archive, const ThumbnailData& thumbnail_data, const char* local_path, int index) + bool _BBS_3MF_Exporter::_add_thumbnail_file_to_archive(mz_zip_archive& archive, const ThumbnailData& thumbnail_data, const char* local_path, int index, bool generate_small_thumbnail) { bool res = false; @@ -5869,6 +5870,32 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result) BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ":" << __LINE__ << boost::format(", Unable to add thumbnail file to archive\n"); } + if (generate_small_thumbnail && thumbnail_data.is_valid()) { + //generate small size of thumbnail + std::vector small_pixels; + small_pixels.resize(PLATE_THUMBNAIL_SMALL_WIDTH * PLATE_THUMBNAIL_SMALL_HEIGHT * 4); + /* step width and step height */ + int sw = thumbnail_data.width / PLATE_THUMBNAIL_SMALL_WIDTH; + int sh = thumbnail_data.height / PLATE_THUMBNAIL_SMALL_HEIGHT; + for (int i = 0; i < thumbnail_data.width; i += sw) { + for (int j = 0; j < thumbnail_data.height; j += sh) { + memcpy((void*)&small_pixels[4*(i / sw * PLATE_THUMBNAIL_SMALL_WIDTH + j / sh)], thumbnail_data.pixels.data() + 4*(i * thumbnail_data.width + j), 4); + } + } + size_t small_png_size = 0; + void* small_png_data = tdefl_write_image_to_png_file_in_memory_ex((const void*)small_pixels.data(), PLATE_THUMBNAIL_SMALL_WIDTH, PLATE_THUMBNAIL_SMALL_HEIGHT, 4, &small_png_size, MZ_DEFAULT_COMPRESSION, 1); + if (png_data != nullptr) { + std::string thumbnail_name = (boost::format("%1%_%2%_small.png") % local_path % (index + 1)).str(); + res = mz_zip_writer_add_mem(&archive, thumbnail_name.c_str(), (const void*)small_png_data, small_png_size, MZ_NO_COMPRESSION); + mz_free(small_png_data); + } + + if (!res) { + add_error("Unable to add small thumbnail file to archive"); + BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ":" << __LINE__ << boost::format(", Unable to add small thumbnail file to archive\n"); + } + } + return res; } diff --git a/src/libslic3r/Format/bbs_3mf.hpp b/src/libslic3r/Format/bbs_3mf.hpp index 8b8a53a71..607e301ba 100644 --- a/src/libslic3r/Format/bbs_3mf.hpp +++ b/src/libslic3r/Format/bbs_3mf.hpp @@ -14,6 +14,10 @@ class Preset; struct FilamentInfo; struct ThumbnailData; + +#define PLATE_THUMBNAIL_SMALL_WIDTH 128 +#define PLATE_THUMBNAIL_SMALL_HEIGHT 128 + #define GCODE_FILE_FORMAT "Metadata/plate_%1%.gcode" #define THUMBNAIL_FILE_FORMAT "Metadata/plate_%1%.png" #define TOP_FILE_FORMAT "Metadata/top_%1%.png"