-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNanoVDBToolSupport_Blue.cpp
More file actions
237 lines (191 loc) · 6.77 KB
/
Copy pathNanoVDBToolSupport_Blue.cpp
File metadata and controls
237 lines (191 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright © 2024 CCP ehf.
#include "StdAfx.h"
#pragma warning( push )
#pragma warning( disable : 4996 )
#include <NanoVdbSupport.h>
#pragma warning( pop )
#include "NanoVDBToolSupport.h"
#include "ImageToolsBitmap.h"
#include "FileStream.h"
#include "AllowThreads.h"
#include <VtaHandler.h>
const Be::VarChooser VtaEncoding_Chooser[] = {
{ "NONE", BeCast( ImageIO::Vta::Encoding::None ), "no encoding" },
{ "RLE_7", BeCast( ImageIO::Vta::Encoding::Rle7 ), "7bit RLE encoding" },
{ "RLE_7_5", BeCast( ImageIO::Vta::Encoding::Rle7_5 ), "7bit for the first frame and 5bit for subsequent frame diffs RLE encoding" },
{ "RLE_6", BeCast( ImageIO::Vta::Encoding::Rle6 ), "6bit RLE encoding" },
{ 0 }
};
BLUE_REGISTER_ENUM_EX(
"VtaEncoding",
ImageIO::Vta::Encoding,
VtaEncoding_Chooser,
ENUM_REG_ENUM_OBJECT_ON_MODULE );
BLUE_DEFINE( GridEncodingInfo );
const Be::ClassInfo* GridEncodingInfo::ExposeToBlue()
{
EXPOSURE_BEGIN( GridEncodingInfo, "" )
MAP_INTERFACE( IRoot )
MAP_INTERFACE( GridEncodingInfo )
MAP_ATTRIBUTE( "index", m_info.index, "grid index in the source VDB file", Be::READWRITE );
MAP_ATTRIBUTE_WITH_CHOOSER( "encoding", m_info.encoding, "grid encoding", Be::READWRITE, VtaEncoding_Chooser );
MAP_ATTRIBUTE( "format", m_info.format, "grid pixel format", Be::READWRITE );
EXPOSURE_END()
}
BLUE_DEFINE( NanoVDBGridMetadata );
const Be::ClassInfo* NanoVDBGridMetadata::ExposeToBlue()
{
EXPOSURE_BEGIN( NanoVDBGridMetadata, "" )
MAP_INTERFACE( IRoot )
MAP_INTERFACE( NanoVDBGridMetadata )
MAP_ATTRIBUTE( "name", m_metadata.name, "grid name", Be::READWRITE );
MAP_ATTRIBUTE( "width", m_metadata.width, "grid width", Be::READWRITE );
MAP_ATTRIBUTE( "height", m_metadata.height, "grid height", Be::READWRITE );
MAP_ATTRIBUTE( "depth", m_metadata.depth, "grid depth", Be::READWRITE );
MAP_ATTRIBUTE( "type", m_metadata.type, "grid type", Be::READWRITE );
EXPOSURE_END()
}
BLUE_DEFINE( VtaGridInfo );
const Be::ClassInfo* VtaGridInfo::ExposeToBlue()
{
EXPOSURE_BEGIN( VtaGridInfo, "" )
MAP_INTERFACE( IRoot )
MAP_INTERFACE( VtaGridInfo )
MAP_ATTRIBUTE( "name", m_info.name, "grid name", Be::READWRITE );
MAP_ATTRIBUTE( "width", m_info.width, "grid width", Be::READWRITE );
MAP_ATTRIBUTE( "height", m_info.height, "grid height", Be::READWRITE );
MAP_ATTRIBUTE( "depth", m_info.depth, "grid depth", Be::READWRITE );
MAP_ATTRIBUTE( "format", m_info.format, "grid pixel format", Be::READWRITE );
MAP_ATTRIBUTE( "encoding", m_info.encoding, "grid encoding", Be::READWRITE );
EXPOSURE_END()
}
BLUE_DEFINE( VtaInfo );
const Be::ClassInfo* VtaInfo::ExposeToBlue()
{
EXPOSURE_BEGIN( VtaInfo, "" )
MAP_INTERFACE( IRoot )
MAP_INTERFACE( VtaInfo )
MAP_ATTRIBUTE( "frames", m_frames, "number of animation frames in the file", Be::READ );
MAP_ATTRIBUTE( "version", m_fileVersion, "VTA file version", Be::READ );
MAP_METHOD_AND_WRAP( "grids", GetGrids, "returns grid information" )
MAP_METHOD_AND_WRAP( "metadata", GetMetadata, "returns metadata strings" )
EXPOSURE_END()
}
Be::Result<std::string> RasterizeNanoVDB( const char* vdbPath, uint32_t gridNumber, ImageIO::PixelFormat format, int32_t bpp, ImageToolsBitmapPtr& bitmap )
{
bitmap.CreateInstance();
if( !ImageIO::RasterizeNanoVDB( vdbPath, gridNumber, format, bpp, *bitmap ) )
{
bitmap = nullptr;
return Be::Result<std::string>( "Failed" );
}
return Be::Result<std::string>();
}
MAP_FUNCTION_AND_WRAP(
"rasterize_nano_vdb",
RasterizeNanoVDB,
"Rasterizes a NanoVDB file into a bitmap.\n"
":param path: path to a valid NanoVDB file\n"
":param grid: index of the grid in the NanoVDB file; we only support Float type grids ATM\n"
":param pixel_format: pixel format to save to: can be either R8_UNORM or R32_FLOAT" );
Be::Result<std::string> NanoVDBToVTA( const std::vector<std::string>& vdbPath, const std::vector<GridEncodingInfo*>& grids, const wchar_t* destPath )
{
std::vector<ImageIO::RasterizeGridInfo> gridInfo;
for( auto& grid : grids )
{
gridInfo.push_back( grid->m_info );
}
auto vta = ImageIO::NanoVDBToVTA( vdbPath, gridInfo );
if( vta.empty() )
{
return Be::Result<std::string>( "Failed" );
}
FileStream stream( destPath, FileStream::WRITE );
stream.Write( vta.data(), vta.size() );
return Be::Result<std::string>();
}
MAP_FUNCTION_AND_WRAP(
"nano_vdb_to_vta",
NanoVDBToVTA,
"Rasterizes a NanoVDB file into a bitmap.\n"
":param path: path to a valid NanoVDB file\n"
":param grids: per-grid options"
":param dest_path: path to the destination VTA file" );
Be::Result<std::string> DownsampleVTA( const wchar_t* sourcePath, const wchar_t* destPath )
{
FileStream srcStream( sourcePath, FileStream::READ );
if( !srcStream.IsValid() )
{
return Be::Result<std::string>( "Failed to read srouce file" );
}
std::vector<uint8_t> src;
src.resize( srcStream.GetSize() );
auto r = srcStream.Read( src.data(), src.size() );
if( r != ptrdiff_t( src.size() ) )
{
return Be::Result<std::string>( "Failed to read srouce file" );
}
auto vta = ImageIO::DownsampleVTA( src.data(), src.size() );
FileStream stream( destPath, FileStream::WRITE );
stream.Write( vta.data(), vta.size() );
return Be::Result<std::string>();
}
MAP_FUNCTION_AND_WRAP(
"downsample_vta",
DownsampleVTA,
"Downsamples source VTA file.\n"
":param src_path: path to a valid VTA file\n"
":param dest_path: path to the destination VTA file" );
Be::Result<std::string> GetNanoVDBMetaData( const char* filename, std::vector<NanoVDBGridMetadataPtr>& result )
{
std::vector<ImageIO::NanoVDBGridMetadata> md;
if( !ImageIO::GetNanoVDBMetaData( filename, md ) )
{
return Be::Result<std::string>( "Failed to read from file" );
}
for( auto& m : md )
{
NanoVDBGridMetadataPtr metadata;
metadata.CreateInstance();
metadata->m_metadata = m;
result.push_back( metadata );
}
return {};
}
MAP_FUNCTION_AND_WRAP(
"get_nano_vdb_meta_data",
GetNanoVDBMetaData,
"Reads meta-data from a NanoVDB file. Returns a list of grid meta-data.\n"
":param path: path to a NanoVDB file" );
StdOrImageIOResult LoadVtaInfo( const wchar_t* path, VtaInfoPtr& info )
{
AllowThreads allowThreads;
FileStream stream( path, FileStream::READ );
if( !stream.IsValid() )
{
return BlueStdResult( BLUE_STD_RESULT_IO_ERROR, "failed to open file" );
}
ImageIO::Vta::FileReader reader;
auto result = reader.SetStream( &stream );
if( !result )
{
return StdOrImageIOResult( result );
}
info.CreateInstance();
info->m_fileVersion = reader.GetVersion();
info->m_frames = reader.GetFrameCount();
info->m_metadata = reader.GetMetadata();
for( uint32_t i = 0; i < reader.GetGridCount(); ++i )
{
VtaGridInfoPtr grid;
grid.CreateInstance();
grid->m_info = reader.GetGridInfo( i );
info->m_grids.push_back( grid );
}
return {};
}
MAP_FUNCTION_AND_WRAP(
"load_vta_info",
LoadVtaInfo,
"Reads header information from a VTA file.\n"
":param path: path to a VTA file" );