mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-13 01:37:53 -06:00
ENH: 3mf: refine the rels to correct link
JIRA: STUDIO-4231 Change-Id: I02ff6343124ef2ea3642e5799469249538a4b97f (cherry picked from commit 781793a4c84b3e8a01f38d990d34bee9add8850a)
This commit is contained in:
parent
bf8afa9889
commit
e76be77cfc
5 changed files with 208 additions and 44 deletions
|
@ -100,6 +100,74 @@ bool decode_png(IStream &in_buf, ImageGreyscale &out_img)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool decode_colored_png(IStream &in_buf, ImageColorscale &out_img)
|
||||
{
|
||||
static const constexpr int PNG_SIG_BYTES = 8;
|
||||
|
||||
std::vector<png_byte> sig(PNG_SIG_BYTES, 0);
|
||||
in_buf.read(sig.data(), PNG_SIG_BYTES);
|
||||
if (!png_check_sig(sig.data(), PNG_SIG_BYTES)) {
|
||||
BOOST_LOG_TRIVIAL(error) << boost::format("decode_colored_png: png_check_sig failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
PNGDescr dsc;
|
||||
dsc.png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr,
|
||||
nullptr);
|
||||
|
||||
if(!dsc.png) {
|
||||
BOOST_LOG_TRIVIAL(error) << boost::format("decode_colored_png: png_create_read_struct failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
dsc.info = png_create_info_struct(dsc.png);
|
||||
if(!dsc.info) {
|
||||
BOOST_LOG_TRIVIAL(error) << boost::format("decode_colored_png: png_create_info_struct failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
png_set_read_fn(dsc.png, static_cast<void *>(&in_buf), png_read_callback);
|
||||
|
||||
// Tell that we have already read the first bytes to check the signature
|
||||
png_set_sig_bytes(dsc.png, PNG_SIG_BYTES);
|
||||
|
||||
png_read_info(dsc.png, dsc.info);
|
||||
|
||||
out_img.cols = png_get_image_width(dsc.png, dsc.info);
|
||||
out_img.rows = png_get_image_height(dsc.png, dsc.info);
|
||||
size_t color_type = png_get_color_type(dsc.png, dsc.info);
|
||||
size_t bit_depth = png_get_bit_depth(dsc.png, dsc.info);
|
||||
|
||||
switch(color_type)
|
||||
{
|
||||
case PNG_COLOR_TYPE_RGB:
|
||||
out_img.bytes_per_pixel = 3;
|
||||
break;
|
||||
case PNG_COLOR_TYPE_RGB_ALPHA:
|
||||
out_img.bytes_per_pixel = 4;
|
||||
break;
|
||||
default: //not supported currently
|
||||
return false;
|
||||
}
|
||||
|
||||
BOOST_LOG_TRIVIAL(info) << boost::format("png's cols %1%, rows %2%, color_type %3%, bit_depth %4%, bytes_per_pixel %5%")%out_img.cols %out_img.rows %color_type %bit_depth %out_img.bytes_per_pixel;
|
||||
out_img.buf.resize(out_img.rows * out_img.cols * out_img.bytes_per_pixel);
|
||||
|
||||
auto readbuf = static_cast<png_bytep>(out_img.buf.data());
|
||||
for (size_t r = 0; r < out_img.rows; ++r)
|
||||
png_read_row(dsc.png, readbuf + r * out_img.cols * out_img.bytes_per_pixel, nullptr);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool decode_colored_png(const ReadBuf &in_buf, ImageColorscale &out_img)
|
||||
{
|
||||
struct ReadBufStream stream{in_buf};
|
||||
|
||||
return decode_colored_png(stream, out_img);
|
||||
}
|
||||
|
||||
|
||||
// Down to earth function to store a packed RGB image to file. Mostly useful for debugging purposes.
|
||||
// Based on https://www.lemoda.net/c/write-png/
|
||||
// png_color_type is PNG_COLOR_TYPE_RGB or PNG_COLOR_TYPE_GRAY
|
||||
|
@ -112,7 +180,7 @@ static bool write_rgb_or_gray_to_file(const char *file_name_utf8, size_t width,
|
|||
png_structp png_ptr = nullptr;
|
||||
png_infop info_ptr = nullptr;
|
||||
png_byte **row_pointers = nullptr;
|
||||
|
||||
|
||||
FILE *fp = boost::nowide::fopen(file_name_utf8, "wb");
|
||||
if (! fp) {
|
||||
BOOST_LOG_TRIVIAL(error) << "write_png_file: File could not be opened for writing: " << file_name_utf8;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue