Blender V5.3
spz_read.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 <bit>
12
13#include "BLI_fileops.hh"
14
15#include "BKE_report.hh"
16
17#include "CLG_log.h"
18
19#include "spz_header.hh"
20
21namespace blender::io::spz {
22
23static CLG_LogRef LOG = {"io.spz"};
24
25template<class T> static bool read_spz_scalar(FILE *file, T &data)
26{
27 if (fread(&data, sizeof(T), 1, file) != 1) {
28 return false;
29 }
30 return true;
31}
32
34{
35 if constexpr (std::endian::native != std::endian::little) {
36 BKE_report(reports, RPT_ERROR, "SPZ Read: Big endian machines are not supported");
37 return nullptr;
38 }
39
40 FILE *file = BLI_fopen(filepath.c_str(), "rb");
41 if (!file) {
42 BKE_report(reports, RPT_ERROR, "SPZ Read: Error opening file for read");
43 return nullptr;
44 }
45
46 uint32_t magic = 0;
47 if (!read_spz_scalar(file, magic)) {
48 BKE_report(reports, RPT_ERROR, "SPZ Read: Error reading magic value from file");
49 fclose(file);
50 return nullptr;
51 }
52 CLOG_DEBUG(&LOG, "Got file header magic: 0x%X", magic);
53
54 /* Rewind the file stream so that specialized version reading can start from its beginning. */
55 if (fseek(file, 0, SEEK_SET) == -1) {
56 fclose(file);
57 return nullptr;
58 }
59
60 PointCloud *point_cloud = nullptr;
61
62 if (magic == SPZ_HEADER_MAGIC) {
63 CLOG_DEBUG(&LOG, "Detected uncompressed NGSP magic");
64 point_cloud = read_spz_ngsp_file(file, reports);
65 }
66 else if ((magic & 0xffff) == 0x8b1f) {
67 /* Format prior to v4: single GZip stream. */
68 CLOG_DEBUG(&LOG, "Detected Gzip compressed stream");
69 point_cloud = read_spz_gzip_compressed_file(file, reports);
70 }
71 else {
72 BKE_reportf(reports, RPT_ERROR, "SPZ Read: Unrecognized file format");
73 }
74
75 fclose(file);
76
77 return point_cloud;
78}
79
80} // namespace blender::io::spz
File and directory operations.
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
constexpr const char * c_str() const
#define T
PointCloud * read_spz_file(const StringRefNull filepath, ReportList *reports)
Definition spz_read.cc:33
static bool read_spz_scalar(FILE *file, T &data)
Definition spz_read.cc:25
PointCloud * read_spz_gzip_compressed_file(FILE *file, ReportList *reports)
static CLG_LogRef LOG
Definition spz_read.cc:23
constexpr uint32_t SPZ_HEADER_MAGIC
Definition spz_header.hh:14
PointCloud * read_spz_ngsp_file(FILE *file, ReportList *reports)
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
unsigned int uint32_t
Definition BKE_text.h:81
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191