Blender V5.3
spz_read_gzip.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "spz_read.hh"
10
11#include <zlib.h>
12
13#include <array>
14#include <cstdint>
15#include <optional>
16
17#include "BLI_array.hh"
18#include "BLI_assert.hh"
19#include "BLI_span.hh"
20
21#include "BKE_lib_id.hh"
22#include "BKE_pointcloud.hh"
23#include "BKE_report.hh"
24
25#include "CLG_log.h"
26
27#include "IO_gsplat.hh"
28
29#include "spz_header.hh"
30#include "spz_read_common.hh"
31
32namespace blender::io::spz {
33
34static CLG_LogRef LOG = {"io.spz"};
35
36namespace {
37
38/* Helper class that takes care of reading data from FILE in chunks. */
39class BufferedFileReader {
40 static constexpr int BUFFER_SIZE = 65536;
41
42 FILE *file_ = nullptr;
43 Array<uint8_t> buffer_;
44
45 public:
46 explicit BufferedFileReader(FILE *file) : file_(file), buffer_(BUFFER_SIZE) {}
47
48 std::optional<Span<uint8_t>> read()
49 {
50 const size_t num_read_bytes = fread(buffer_.data(), 1, buffer_.size(), file_);
51 if (ferror(file_)) {
52 return std::nullopt;
53 }
54 return buffer_.as_span().slice(0, num_read_bytes);
55 }
56};
57
58/* Helper class that allows reading a Gzip-compressed file from disk with the minimum amount of
59 * extra memory usage, but allowing to easily access data of specific size. */
60class StreamedGzipReader {
61 BufferedFileReader buffered_reader_;
62 z_stream stream_{};
63
64 public:
65 explicit StreamedGzipReader(FILE *file) : buffered_reader_(file) {}
66
67 ~StreamedGzipReader()
68 {
69 inflateEnd(&stream_);
70 }
71
72 bool initialize()
73 {
74 /* The window size matches the SPZ library: here 16 means enable automatic gzip header
75 * detection; consider switching this to 32 to enable both automated gzip and zlib header
76 * detection. */
77 return inflateInit2(&stream_, 16 | MAX_WBITS) == Z_OK;
78 }
79
80 bool read_buffer(const MutableSpan<uint8_t> buffer)
81 {
82 stream_.avail_out = buffer.size();
83 stream_.next_out = buffer.data();
84
85 do {
86 /* There might be left-over data in the input since previous read.
87 * If there is, process that data first. Otherwise, load the next chunk from the input file.
88 */
89 if (stream_.avail_in == 0) {
90 std::optional<Span<uint8_t>> read_result = buffered_reader_.read();
91 if (!read_result.has_value() || read_result->size() == 0) {
92 return false;
93 }
94 stream_.avail_in = read_result->size();
95 stream_.next_in = const_cast<Bytef *>(read_result->data());
96 }
97
98 const int ret = inflate(&stream_, Z_NO_FLUSH);
99 BLI_assert(ret != Z_STREAM_ERROR);
100 if (ret == Z_STREAM_END) {
101 if (stream_.avail_out != 0) {
102 return false;
103 }
104 break;
105 }
106 if (ret != Z_OK) {
107 /* TODO(sergey): Report error. */
108 return false;
109 }
110 } while (stream_.avail_out != 0);
111
112 /* TODO(sergey): Investigate whether decompressing more data ahead of time helps performance.
113 */
114
115 return true;
116 }
117
118 template<class T> bool read(T &data)
119 {
120 return read_buffer(MutableSpan<uint8_t>(reinterpret_cast<uint8_t *>(&data), sizeof(T)));
121 }
122};
123
124} // namespace
125
127{
128 StreamedGzipReader reader(file);
129
130 if (!reader.initialize()) {
131 BKE_report(reports, RPT_ERROR, "SPZ Read: Error initializing Zlib decompression");
132 return nullptr;
133 }
134
136 if (!reader.read(header)) {
137 BKE_report(reports, RPT_ERROR, "SPZ Read: Error reading header from GZip stream");
138 return nullptr;
139 }
140
141 CLOG_DEBUG(&LOG, "SPZ header magic: 0x%X", header.magic);
142 CLOG_DEBUG(&LOG, "SPZ header version: %u", header.version);
143
144 if (const std::optional<std::string> error = check_header_for_errors(header)) {
145 BKE_reportf(reports, RPT_ERROR, "SPZ Read: %s", error->c_str());
146 return nullptr;
147 }
148
149 CLOG_DEBUG(&LOG, "SPZ header num_points: %u", header.num_points);
150 CLOG_DEBUG(&LOG, "SPZ header sh_degree: %d", int(header.sh_degree));
151 CLOG_DEBUG(&LOG, "SPZ header fractional_bits: %d", int(header.fractional_bits));
152 CLOG_DEBUG(&LOG, "SPZ header flags: %d", header.flags);
153
154 if (header.flags & SPZ_HEADER_ANTIALIASED) {
155 /* TODO(sergey): Support anti-aliased data. */
156 CLOG_WARN(&LOG, "SPZ data was trained with anti-aliasing which is not fully supported");
157 }
158 if (header.flags & SPZ_HEADER_HAS_EXTENSIONS) {
159 /* TODO(sergey): Support extensions. */
160 CLOG_WARN(&LOG, "SPZ file contains extensions that are not yet supported");
161 }
162
164 gsplat::GsplatMutableAttributeAccessor accessor(*point_cloud, header.sh_degree);
165
166 const MutableSpan<float3> positions = accessor.positions_for_write();
167 const MutableSpan<math::Quaternion> rotations = accessor.rotations_for_write();
168 const Span<MutableSpan<float3>> sh_attrs = accessor.sh_for_write();
169
170 if (!read_positions(reader, header.fractional_bits, positions) ||
171 !read_alphas(reader, accessor.radiance_base_for_write()) ||
172 !read_colors(reader, accessor.radiance_base_for_write()) ||
173 !read_scales(reader, accessor.scales_for_write()) ||
174 !read_rotations(reader, header.version, rotations) || !read_sh(reader, sh_attrs))
175 {
176 accessor.finish();
177 BKE_id_free(nullptr, &point_cloud->id);
178 return nullptr;
179 }
180
181 /* TODO(sergey): Handle extensions. */
182
183 convert_axis_to_blender(positions, rotations, sh_attrs);
184
185 accessor.finish();
186
187 return point_cloud;
188}
189
190} // namespace blender::io::spz
General operations for point clouds.
#define BUFFER_SIZE
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define BLI_assert(a)
void initialize()
int64_t size() const
Definition BLI_array.hh:267
Span< T > as_span() const
Definition BLI_array.hh:254
const T * data() const
Definition BLI_array.hh:323
MutableSpan< math::Quaternion > rotations_for_write()
Definition gsplat.cc:48
Span< MutableSpan< float3 > > sh_for_write()
Definition gsplat.cc:53
#define T
bool read_sh(ReaderType &reader, const Span< MutableSpan< float3 > > sh)
PointCloud * read_spz_gzip_compressed_file(FILE *file, ReportList *reports)
bool read_positions(ReaderType &reader, const int fractional_bits, const MutableSpan< float3 > positions)
void convert_axis_to_blender(const MutableSpan< float3 > positions, const MutableSpan< math::Quaternion > rotations, const Span< MutableSpan< float3 > > sh_attrs)
std::optional< std::string > check_header_for_errors(const PackedGaussiansHeader &header)
Definition spz_header.cc:41
bool read_colors(ReaderType &reader, const MutableSpan< float4 > radiance_base)
bool read_alphas(ReaderType &reader, const MutableSpan< float4 > radiance_base)
static CLG_LogRef LOG
Definition spz_read.cc:23
bool read_scales(ReaderType &reader, const MutableSpan< float3 > scales)
bool read_rotations(ReaderType &reader, const int version, const MutableSpan< math::Quaternion > rotations)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
void BKE_id_free(Main *bmain, void *idv)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
T read(T depth_buffer_value)