20 png_image &pngimage) {
21 pngimage.width =
image.GetCols();
22 pngimage.height =
image.GetRows();
23 pngimage.format = pngimage.flags = 0;
26 pngimage.format |= PNG_FORMAT_FLAG_LINEAR;
28 if (
image.GetChannels() >= 3) {
29 pngimage.format |= PNG_FORMAT_FLAG_COLOR;
31 if (
image.GetChannels() == 4) {
32 pngimage.format |= PNG_FORMAT_FLAG_ALPHA;
35 pngimage.flags |= PNG_IMAGE_FLAG_FAST;
41 memset(&pngimage, 0,
sizeof(pngimage));
42 pngimage.version = PNG_IMAGE_VERSION;
43 if (png_image_begin_read_from_file(&pngimage,
filename.c_str()) == 0) {
51 if (pngimage.format & PNG_FORMAT_FLAG_COLORMAP) {
52 pngimage.format &= ~PNG_FORMAT_FLAG_COLORMAP;
54 if (pngimage.format & PNG_FORMAT_FLAG_LINEAR) {
55 image.Reset(pngimage.height, pngimage.width,
56 PNG_IMAGE_SAMPLE_CHANNELS(pngimage.format),
core::UInt16,
59 image.Reset(pngimage.height, pngimage.width,
60 PNG_IMAGE_SAMPLE_CHANNELS(pngimage.format),
core::UInt8,
64 if (png_image_finish_read(&pngimage,
NULL,
image.GetDataPtr(), 0,
NULL) ==
78 if (
image.IsEmpty()) {
91 if (quality < 0 || quality > 9) {
93 "Write PNG failed: quality ({}) must be in the range [0,9]",
98 memset(&pngimage, 0,
sizeof(pngimage));
99 pngimage.version = PNG_IMAGE_VERSION;
101 if (png_image_write_to_file(&pngimage,
filename.c_str(), 0,
113 if (
image.IsEmpty()) {
125 if (quality < 0 || quality > 9) {
127 "Write PNG failed: quality ({}) must be in the range [0,9]",
132 memset(&pngimage, 0,
sizeof(pngimage));
133 pngimage.version = PNG_IMAGE_VERSION;
137 size_t mem_bytes = 0;
138 if (png_image_write_to_memory(&pngimage,
nullptr, &mem_bytes, 0,
139 image.GetDataPtr(), 0,
nullptr) == 0) {
141 "Could not compute bytes needed for encoding to PNG in "
145 buffer.resize(mem_bytes);
146 if (png_image_write_to_memory(&pngimage, &buffer[0], &mem_bytes, 0,
147 image.GetDataPtr(), 0,
nullptr) == 0) {
std::shared_ptr< core::Tensor > image
The Image class stores image with customizable rows, cols, channels, dtype and device.
bool WriteImageToPNGInMemory(std::vector< uint8_t > &buffer, const t::geometry::Image &image, int quality)
bool ReadImageFromPNG(const std::string &filename, geometry::Image &image)
constexpr int kCloudViewerImageIODefaultQuality
bool WriteImageToPNG(const std::string &filename, const geometry::Image &image, int quality)
static void SetPNGImageFromImage(const geometry::Image &image, int quality, png_image &pngimage)
Generic file read and write utility for python interface.