Ptex

PtexWriter.h
Go to the documentation of this file.
1#ifndef PtexWriter_h
2#define PtexWriter_h
3
4/*
5 PTEX SOFTWARE
6 Copyright 2014 Disney Enterprises, Inc. All rights reserved
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
10 met:
11
12 * Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14
15 * Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in
17 the documentation and/or other materials provided with the
18 distribution.
19
20 * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
21 Studios" or the names of its contributors may NOT be used to
22 endorse or promote products derived from this software without
23 specific prior written permission from Walt Disney Pictures.
24
25 Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
26 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
27 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
28 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
29 IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
30 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37*/
38
39#include "PtexPlatform.h"
40#include <map>
41#include <vector>
42#include <stdio.h>
43#include "Ptexture.h"
44#include "PtexIO.h"
45#include "PtexReader.h"
46
47 struct libdeflate_compressor;
48
49 PTEX_NAMESPACE_BEGIN
50
51 class PtexMainWriter : public PtexWriter {
52 public:
53 PtexMainWriter(const char* path, PtexTexture* tex,
54 Ptex::MeshType mt, Ptex::DataType dt,
55 int nchannels, int alphachan, int nfaces, bool genmipmaps);
56
57 virtual void release();
58 virtual bool close(Ptex::String& error);
59 virtual bool writeFace(int faceid, const FaceInfo& f, const void* data, int stride);
60 virtual bool writeConstantFace(int faceid, const FaceInfo& f, const void* data);
61
62 virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
63 {
64 _extheader.ubordermode = uBorderMode;
65 _extheader.vbordermode = vBorderMode;
66 }
67 virtual void setEdgeFilterMode(Ptex::EdgeFilterMode edgeFilterMode)
68 {
69 _extheader.edgefiltermode = edgeFilterMode;
70 }
71 virtual void writeMeta(const char* key, const char* value);
72 virtual void writeMeta(const char* key, const int8_t* value, int count);
73 virtual void writeMeta(const char* key, const int16_t* value, int count);
74 virtual void writeMeta(const char* key, const int32_t* value, int count);
75 virtual void writeMeta(const char* key, const float* value, int count);
76 virtual void writeMeta(const char* key, const double* value, int count);
77 virtual void writeMeta(PtexMetaData* data);
78
79 bool ok(Ptex::String& error) {
80 if (!_ok) getError(error);
81 return _ok;
82 }
83 void getError(Ptex::String& error) {
84 error = (_error + "\nPtex file: " + _path).c_str();
85 }
86
87 private:
88 virtual ~PtexMainWriter();
89
90 DataType datatype() const { return DataType(_header.datatype); }
91
92 struct MetaEntry {
93 std::string key;
94 MetaDataType datatype;
95 std::vector<uint8_t> data;
96 MetaEntry() : datatype(MetaDataType(0)), data() {}
97 };
98
99 size_t writeBlock(FILE* fp, const void* data, size_t size);
100 size_t writeBlock(FILE* fp, const std::vector<std::byte>& dataBlock) { return writeBlock(fp, dataBlock.data(), dataBlock.size()); }
101 void addToDataBlock(std::vector<std::byte>& dataBlock, const void* data, size_t size);
102 libdeflate_compressor* getCompressor();
103 void releaseCompressor(libdeflate_compressor* compressor);
104 void compressDataBlock(libdeflate_compressor* compressor, std::vector<std::byte>& compressedData, const void* data, size_t size);
105 Res calcTileRes(Res faceres);
106 void addMetaData(const char* key, MetaDataType t, const void* value, int size);
107 void compressFaceDataBlock(libdeflate_compressor* compressor, std::vector<std::byte>& compressedData, FaceDataHeader& fdh,
108 Res res, const void* uncompressedData, int stride);
109 void compressFaceData(libdeflate_compressor* compressor, std::vector<std::byte>& compressedData, FaceDataHeader& fdh,
110 Res res, const void* uncompressedData);
111 void addToMetaDataBlock(std::vector<std::byte>& dataBlock, const MetaEntry& val);
112 void setError(const std::string& error) { _error = error; _ok = false; }
113 bool storeFaceInfo(int faceid, FaceInfo& dest, const FaceInfo& src, int flags=0);
114 void finish();
115 void flagConstantNeighorhoods();
116 void storeConstValue(int faceid, const void* data, int stride, Res res);
117
118 bool _ok; // true if no error has occurred
119 std::string _error; // the error text (if any)
120 std::string _path; // file path
121 Header _header; // the file header
122 ExtHeader _extheader; // extended header
123 int _pixelSize; // size of a pixel in bytes
124 std::vector<MetaEntry> _metadata; // meta data waiting to be written
125 std::map<std::string,int> _metamap; // for preventing duplicate keys
126
127 std::vector<libdeflate_compressor*> _compressors;
128 Mutex _compressorMutex;
129
130 PtexUtils::ReduceFn* _reduceFn;
131
132 std::string _newpath; // path to ".new" file
133 bool _genmipmaps; // true if mipmaps should be generated
134 std::vector<FaceInfo> _faceinfo; // info about each face
135 std::vector<std::byte> _constdata; // constant data for each face
136 std::vector<uint32_t> _rfaceids; // faceid reordering for reduction levels
137 std::vector<uint32_t> _faceids_r; // faceid indexed by rfaceid
138
139 static const int MinReductionLog2 =2; // log2(minimum reduction size) - can tune
140 struct FaceRec {
141 std::vector<std::vector<std::byte>> faceData; // compressed face data, including reductions
142 std::vector<FaceDataHeader> fdh; // face data headers
143 Mutex mutex;
144 };
145 std::vector<FaceRec> _faces; // compressed data for each face
146
147 PtexReader* _reader; // reader for accessing existing data in file
148};
149
150
151 PTEX_NAMESPACE_END
152
153#endif
PtexIO.h
PtexPlatform.h
Platform-specific classes, functions, and includes.
PtexReader.h
PTEX_NAMESPACE_END
#define PTEX_NAMESPACE_END
Definition PtexVersion.h:62
Ptexture.h
Public API classes for reading, writing, caching, and filtering Ptex files.
Mutex
Definition PtexPlatform.h:139
PtexMainWriter::_error
std::string _error
Definition PtexWriter.h:119
PtexMainWriter::calcTileRes
Res calcTileRes(Res faceres)
Definition PtexWriter.cpp:402
PtexMainWriter::releaseCompressor
void releaseCompressor(libdeflate_compressor *compressor)
Definition PtexWriter.cpp:384
PtexMainWriter::_pixelSize
int _pixelSize
Definition PtexWriter.h:123
PtexMainWriter::getError
void getError(Ptex::String &error)
Definition PtexWriter.h:83
PtexMainWriter::compressFaceDataBlock
void compressFaceDataBlock(libdeflate_compressor *compressor, std::vector< std::byte > &compressedData, FaceDataHeader &fdh, Res res, const void *uncompressedData, int stride)
Definition PtexWriter.cpp:424
PtexMainWriter::_compressors
std::vector< libdeflate_compressor * > _compressors
Definition PtexWriter.h:127
PtexMainWriter::storeFaceInfo
bool storeFaceInfo(int faceid, FaceInfo &dest, const FaceInfo &src, int flags=0)
Definition PtexWriter.cpp:192
PtexMainWriter::addToDataBlock
void addToDataBlock(std::vector< std::byte > &dataBlock, const void *data, size_t size)
Definition PtexWriter.cpp:361
PtexMainWriter::_newpath
std::string _newpath
Definition PtexWriter.h:132
PtexMainWriter::_rfaceids
std::vector< uint32_t > _rfaceids
Definition PtexWriter.h:136
PtexMainWriter::writeBlock
size_t writeBlock(FILE *fp, const void *data, size_t size)
Definition PtexWriter.cpp:350
PtexMainWriter::_metamap
std::map< std::string, int > _metamap
Definition PtexWriter.h:125
PtexMainWriter::close
virtual bool close(Ptex::String &error)
Close the file.
Definition PtexWriter.cpp:591
PtexMainWriter::_genmipmaps
bool _genmipmaps
Definition PtexWriter.h:133
PtexMainWriter::writeConstantFace
virtual bool writeConstantFace(int faceid, const FaceInfo &f, const void *data)
Definition PtexWriter.cpp:695
PtexMainWriter::_reader
PtexReader * _reader
Definition PtexWriter.h:147
PtexMainWriter::finish
void finish()
Definition PtexWriter.cpp:721
PtexMainWriter::ok
bool ok(Ptex::String &error)
Definition PtexWriter.h:79
PtexMainWriter::_faceids_r
std::vector< uint32_t > _faceids_r
Definition PtexWriter.h:137
PtexMainWriter::writeMeta
virtual void writeMeta(const char *key, const char *value)
Write a string as meta data.
Definition PtexWriter.cpp:224
PtexMainWriter::compressDataBlock
void compressDataBlock(libdeflate_compressor *compressor, std::vector< std::byte > &compressedData, const void *data, size_t size)
Definition PtexWriter.cpp:391
PtexMainWriter::setError
void setError(const std::string &error)
Definition PtexWriter.h:112
PtexMainWriter::storeConstValue
void storeConstValue(int faceid, const void *data, int stride, Res res)
Definition PtexWriter.cpp:709
PtexMainWriter::_faceinfo
std::vector< FaceInfo > _faceinfo
Definition PtexWriter.h:134
PtexMainWriter::_extheader
ExtHeader _extheader
Definition PtexWriter.h:122
PtexMainWriter::addMetaData
void addMetaData(const char *key, MetaDataType t, const void *value, int size)
Definition PtexWriter.cpp:316
PtexMainWriter::compressFaceData
void compressFaceData(libdeflate_compressor *compressor, std::vector< std::byte > &compressedData, FaceDataHeader &fdh, Res res, const void *uncompressedData)
Definition PtexWriter.cpp:450
PtexMainWriter::~PtexMainWriter
virtual ~PtexMainWriter()
Definition PtexWriter.cpp:582
PtexMainWriter::setEdgeFilterMode
virtual void setEdgeFilterMode(Ptex::EdgeFilterMode edgeFilterMode)
Set edge filter mode.
Definition PtexWriter.h:67
PtexMainWriter::_metadata
std::vector< MetaEntry > _metadata
Definition PtexWriter.h:124
PtexMainWriter::_reduceFn
PtexUtils::ReduceFn * _reduceFn
Definition PtexWriter.h:130
PtexMainWriter::MinReductionLog2
static const int MinReductionLog2
Definition PtexWriter.h:139
PtexMainWriter::addToMetaDataBlock
void addToMetaDataBlock(std::vector< std::byte > &dataBlock, const MetaEntry &val)
Definition PtexWriter.cpp:515
PtexMainWriter::_path
std::string _path
Definition PtexWriter.h:120
PtexMainWriter::_header
Header _header
Definition PtexWriter.h:121
PtexMainWriter::writeBlock
size_t writeBlock(FILE *fp, const std::vector< std::byte > &dataBlock)
Definition PtexWriter.h:100
PtexMainWriter::datatype
DataType datatype() const
Definition PtexWriter.h:90
PtexMainWriter::_faces
std::vector< FaceRec > _faces
Definition PtexWriter.h:145
PtexMainWriter::writeFace
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
Definition PtexWriter.cpp:613
PtexMainWriter::_compressorMutex
Mutex _compressorMutex
Definition PtexWriter.h:128
PtexMainWriter::_constdata
std::vector< std::byte > _constdata
Definition PtexWriter.h:135
PtexMainWriter::PtexMainWriter
PtexMainWriter(const char *path, PtexTexture *tex, Ptex::MeshType mt, Ptex::DataType dt, int nchannels, int alphachan, int nfaces, bool genmipmaps)
Definition PtexWriter.cpp:528
PtexMainWriter::_ok
bool _ok
Definition PtexWriter.h:118
PtexMainWriter::setBorderModes
virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
Set border modes.
Definition PtexWriter.h:62
PtexMainWriter::flagConstantNeighorhoods
void flagConstantNeighorhoods()
Definition PtexWriter.cpp:939
PtexMainWriter::release
virtual void release()
Release resources held by this pointer (pointer becomes invalid).
Definition PtexWriter.cpp:182
PtexMainWriter::getCompressor
libdeflate_compressor * getCompressor()
Definition PtexWriter.cpp:369
PtexMetaData
Meta data accessor.
Definition Ptexture.h:331
PtexReader
Definition PtexReader.h:53
PtexTexture
Interface for reading data from a ptex file.
Definition Ptexture.h:460
PtexWriter
Interface for writing data to a ptex file.
Definition Ptexture.h:819
Ptex::String
Memory-managed string.
Definition Ptexture.h:299
PTEX_NAMESPACE_BEGIN
Definition PtexSeparableKernel.cpp:42
PtexUtils::ReduceFn
void ReduceFn(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition PtexUtils.h:167
Ptex::DataType
DataType
Type of data stored in texture file.
Definition Ptexture.h:72
Ptex::MeshType
MeshType
Type of base mesh for which the textures are defined.
Definition Ptexture.h:66
Ptex::BorderMode
BorderMode
How to handle mesh border when filtering.
Definition Ptexture.h:86
Ptex::EdgeFilterMode
EdgeFilterMode
How to handle transformation across edges when filtering.
Definition Ptexture.h:80
ExtHeader
Definition PtexIO.h:64
FaceDataHeader
Definition PtexIO.h:82
Header
Definition PtexIO.h:44
PtexMainWriter::FaceRec
Definition PtexWriter.h:140
PtexMainWriter::FaceRec::faceData
std::vector< std::vector< std::byte > > faceData
Definition PtexWriter.h:141
PtexMainWriter::FaceRec::fdh
std::vector< FaceDataHeader > fdh
Definition PtexWriter.h:142
PtexMainWriter::FaceRec::mutex
Mutex mutex
Definition PtexWriter.h:143
PtexMainWriter::MetaEntry::key
std::string key
Definition PtexWriter.h:93
PtexMainWriter::MetaEntry::MetaEntry
MetaEntry()
Definition PtexWriter.h:96
PtexMainWriter::MetaEntry::datatype
MetaDataType datatype
Definition PtexWriter.h:94
PtexMainWriter::MetaEntry::data
std::vector< uint8_t > data
Definition PtexWriter.h:95