Ptex: PtexWriter.h Source File

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 <zlib.h>
41 #include <map>
42 #include <vector>
43 #include <stdio.h>
44 #include "Ptexture.h"
45 #include "PtexIO.h"
46 #include "PtexReader.h"
47
48 PTEX_NAMESPACE_BEGIN
49
50 class PtexWriterBase : public PtexWriter {
51 public:
52 virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
53 {
54 _extheader.ubordermode = uBorderMode;
55 _extheader.vbordermode = vBorderMode;
56 }
57 virtual void setEdgeFilterMode(Ptex::EdgeFilterMode edgeFilterMode)
58 {
59 _extheader.edgefiltermode = edgeFilterMode;
60 }
61 virtual void writeMeta(const char* key, const char* value);
62 virtual void writeMeta(const char* key, const int8_t* value, int count);
63 virtual void writeMeta(const char* key, const int16_t* value, int count);
64 virtual void writeMeta(const char* key, const int32_t* value, int count);
65 virtual void writeMeta(const char* key, const float* value, int count);
66 virtual void writeMeta(const char* key, const double* value, int count);
67 virtual void writeMeta(PtexMetaData* data);
68 virtual bool close(Ptex::String& error);
69 virtual void release();
70
71 bool ok(Ptex::String& error) {
72 if (!_ok) getError(error);
73 return _ok;
74 }
75 void getError(Ptex::String& error) {
76 error = (_error + "\nPtex file: " + _path).c_str();
77 }
78
79 protected:
80 DataType datatype() const { return DataType(_header.datatype); }
81
82 struct MetaEntry {
83 std::string key;
84 MetaDataType datatype;
85 std::vector<uint8_t> data;
86 MetaEntry() : datatype(MetaDataType(0)), data() {}
87 };
88
89 virtual void finish() = 0;
90 PtexWriterBase(const char* path,
91 Ptex::MeshType mt, Ptex::DataType dt,
92 int nchannels, int alphachan, int nfaces,
93 bool compress);
94 virtual ~PtexWriterBase();
95
96 int writeBlank(FILE* fp, int size);
97 int writeBlock(FILE* fp, const void* data, int size);
98 int writeZipBlock(FILE* fp, const void* data, int size, bool finish=true);
99 int readBlock(FILE* fp, void* data, int size);
100 int copyBlock(FILE* dst, FILE* src, FilePos pos, int size);
101 Res calcTileRes(Res faceres);
102 virtual void addMetaData(const char* key, MetaDataType t, const void* value, int size);
103 void writeConstFaceBlock(FILE* fp, const void* data, FaceDataHeader& fdh);
104 void writeFaceBlock(FILE* fp, const void* data, int stride, Res res,
105 FaceDataHeader& fdh);
106 void writeFaceData(FILE* fp, const void* data, int stride, Res res,
107 FaceDataHeader& fdh);
108 void writeReduction(FILE* fp, const void* data, int stride, Res res);
109 int writeMetaDataBlock(FILE* fp, MetaEntry& val);
110 void setError(const std::string& error) { _error = error; _ok = false; }
111 bool storeFaceInfo(int faceid, FaceInfo& dest, const FaceInfo& src, int flags=0);
112
113 bool _ok; // true if no error has occurred
114 std::string _error; // the error text (if any)
115 std::string _path; // file path
116 std::string _tilepath; // temp tile file path ("<path>.tiles.tmp")
117 FILE* _tilefp; // temp tile file handle
118 Header _header; // the file header
119 ExtHeader _extheader; // extended header
120 int _pixelSize; // size of a pixel in bytes
121 std::vector<MetaEntry> _metadata; // meta data waiting to be written
122 std::map<std::string,int> _metamap; // for preventing duplicate keys
123 z_stream_s _zstream; // libzip compression stream
124
125 PtexUtils::ReduceFn* _reduceFn;
126 };
127
128
129 class PtexMainWriter : public PtexWriterBase {
130 public:
131 PtexMainWriter(const char* path, PtexTexture* tex,
132 Ptex::MeshType mt, Ptex::DataType dt,
133 int nchannels, int alphachan, int nfaces, bool genmipmaps);
134
135 virtual bool close(Ptex::String& error);
136 virtual bool writeFace(int faceid, const FaceInfo& f, const void* data, int stride);
137 virtual bool writeConstantFace(int faceid, const FaceInfo& f, const void* data);
138
139 protected:
140 virtual ~PtexMainWriter();
141 virtual void addMetaData(const char* key, MetaDataType t, const void* value, int size)
142 {
143 PtexWriterBase::addMetaData(key, t, value, size);
144 _hasNewData = true;
145 }
146
147 private:
148 virtual void finish();
149 void generateReductions();
150 void flagConstantNeighorhoods();
151 void storeConstValue(int faceid, const void* data, int stride, Res res);
152 void writeMetaData(FILE* fp);
153
154 std::string _newpath; // path to ".new" file
155 std::string _tmppath; // temp file path ("<path>.tmp")
156 FILE* _tmpfp; // temp file handle
157 bool _hasNewData; // true if data has been written
158 bool _genmipmaps; // true if mipmaps should be generated
159 std::vector<FaceInfo> _faceinfo; // info about each face
160 std::vector<uint8_t> _constdata; // constant data for each face
161 std::vector<uint32_t> _rfaceids; // faceid reordering for reduction levels
162 std::vector<uint32_t> _faceids_r; // faceid indexed by rfaceid
163
164 static const int MinReductionLog2 =2; // log2(minimum reduction size) - can tune
165 struct LevelRec {
166 // note: level 0 is ordered by faceid
167 // levels 1+ are reduction levels (half res in both u and v) and
168 // are ordered by rfaceid[faceid]. Also, faces with a minimum
169 // dimension (the smaller of u or v) smaller than MinReductionLog2
170 // are omitted from subsequent levels.
171 std::vector<FilePos> pos; // position of data blocks within _tmp file
172 std::vector<FaceDataHeader> fdh; // face data headers
173 };
174 std::vector<LevelRec> _levels; // info about each level
175 std::vector<FilePos> _rpos; // reduction file positions
176
177 PtexReader* _reader; // reader for accessing existing data in file
178 };
179
180
181 class PtexIncrWriter : public PtexWriterBase {
182 public:
183 PtexIncrWriter(const char* path, FILE* fp,
184 Ptex::MeshType mt, Ptex::DataType dt,
185 int nchannels, int alphachan, int nfaces);
186
187 virtual bool close(Ptex::String& error);
188 virtual bool writeFace(int faceid, const FaceInfo& f, const void* data, int stride);
189 virtual bool writeConstantFace(int faceid, const FaceInfo& f, const void* data);
190
191 protected:
192 void writeMetaDataEdit();
193 virtual void finish();
194 virtual ~PtexIncrWriter();
195
196 private:
197 FILE* _fp; // the file being edited
198 };
199
200 PTEX_NAMESPACE_END
201
202 #endif
PtexWriterBase::storeFaceInfo
bool storeFaceInfo(int faceid, FaceInfo &dest, const FaceInfo &src, int flags=0)
Definition: PtexWriter.cpp:329
PtexWriterBase::calcTileRes
Res calcTileRes(Res faceres)
Definition: PtexWriter.cpp:570
PtexMainWriter::writeFace
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
Definition: PtexWriter.cpp:806
PtexWriterBase::writeMeta
virtual void writeMeta(const char *key, const char *value)
Write a string as meta data.
Definition: PtexWriter.cpp:361
PtexWriterBase::_metamap
std::map< std::string, int > _metamap
Definition: PtexWriter.h:122
PtexMainWriter::_levels
std::vector< LevelRec > _levels
Definition: PtexWriter.h:174
PtexMainWriter::addMetaData
virtual void addMetaData(const char *key, MetaDataType t, const void *value, int size)
Definition: PtexWriter.h:141
PtexMainWriter::_hasNewData
bool _hasNewData
Definition: PtexWriter.h:157
PtexWriterBase::writeReduction
void writeReduction(FILE *fp, const void *data, int stride, Res res)
Definition: PtexWriter.cpp:690
PtexIncrWriter::writeMetaDataEdit
void writeMetaDataEdit()
Definition: PtexWriter.cpp:1362
Header
Definition: PtexIO.h:44
PtexWriterBase::writeConstFaceBlock
void writeConstFaceBlock(FILE *fp, const void *data, FaceDataHeader &fdh)
Definition: PtexWriter.cpp:592
PtexMainWriter::_tmpfp
FILE * _tmpfp
Definition: PtexWriter.h:156
PTEX_NAMESPACE_END
#define PTEX_NAMESPACE_END
Definition: PtexVersion.h:62
PtexWriterBase::getError
void getError(Ptex::String &error)
Definition: PtexWriter.h:75
PtexIO.h
PtexWriterBase::_path
std::string _path
Definition: PtexWriter.h:115
PtexWriterBase::_ok
bool _ok
Definition: PtexWriter.h:113
PtexMainWriter::_faceids_r
std::vector< uint32_t > _faceids_r
Definition: PtexWriter.h:162
PtexUtils::ReduceFn
void ReduceFn(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.h:174
PtexWriterBase::~PtexWriterBase
virtual ~PtexWriterBase()
Definition: PtexWriter.cpp:310
PtexWriterBase::ok
bool ok(Ptex::String &error)
Definition: PtexWriter.h:71
PtexWriterBase::writeZipBlock
int writeZipBlock(FILE *fp, const void *data, int size, bool finish=true)
Definition: PtexWriter.cpp:510
ExtHeader::ubordermode
uint16_t ubordermode
Definition: PtexIO.h:65
ExtHeader::edgefiltermode
uint16_t edgefiltermode
Definition: PtexIO.h:68
PtexWriterBase::writeMetaDataBlock
int writeMetaDataBlock(FILE *fp, MetaEntry &val)
Definition: PtexWriter.cpp:707
PtexWriterBase::MetaEntry::MetaEntry
MetaEntry()
Definition: PtexWriter.h:86
ExtHeader::vbordermode
uint16_t vbordermode
Definition: PtexIO.h:67
PtexWriterBase::_error
std::string _error
Definition: PtexWriter.h:114
PtexIncrWriter::close
virtual bool close(Ptex::String &error)
Close the file.
Definition: PtexWriter.cpp:1395
PtexWriterBase::setBorderModes
virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
Set border modes.
Definition: PtexWriter.h:52
PtexMainWriter::MinReductionLog2
static const int MinReductionLog2
Definition: PtexWriter.h:164
PtexMetaData
Meta data accessor.
Definition: Ptexture.h:341
PtexWriterBase::writeFaceData
void writeFaceData(FILE *fp, const void *data, int stride, Res res, FaceDataHeader &fdh)
Definition: PtexWriter.cpp:629
PtexMainWriter::generateReductions
void generateReductions()
Definition: PtexWriter.cpp:1079
PtexMainWriter::_rpos
std::vector< FilePos > _rpos
Definition: PtexWriter.h:175
Ptex::DataType
DataType
Type of data stored in texture file.
Definition: Ptexture.h:85
PtexMainWriter::LevelRec
Definition: PtexWriter.h:165
PtexMainWriter::storeConstValue
void storeConstValue(int faceid, const void *data, int stride, Res res)
Definition: PtexWriter.cpp:878
PtexWriterBase::MetaEntry::datatype
MetaDataType datatype
Definition: PtexWriter.h:84
PtexMainWriter::LevelRec::fdh
std::vector< FaceDataHeader > fdh
Definition: PtexWriter.h:172
PtexMainWriter::_reader
PtexReader * _reader
Definition: PtexWriter.h:177
PtexWriterBase::copyBlock
int copyBlock(FILE *dst, FILE *src, FilePos pos, int size)
Definition: PtexWriter.cpp:551
PTEX_NAMESPACE_BEGIN
Definition: PtexSeparableKernel.cpp:42
PtexMainWriter::_genmipmaps
bool _genmipmaps
Definition: PtexWriter.h:158
PtexMainWriter::_constdata
std::vector< uint8_t > _constdata
Definition: PtexWriter.h:160
PtexIncrWriter::writeFace
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
Definition: PtexWriter.cpp:1270
PtexWriterBase::_pixelSize
int _pixelSize
Definition: PtexWriter.h:120
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:723
Header::datatype
uint32_t datatype
Definition: PtexIO.h:48
PtexMainWriter::writeConstantFace
virtual bool writeConstantFace(int faceid, const FaceInfo &f, const void *data)
Definition: PtexWriter.cpp:863
ExtHeader
Definition: PtexIO.h:64
PtexWriterBase::_tilefp
FILE * _tilefp
Definition: PtexWriter.h:117
PtexIncrWriter::~PtexIncrWriter
virtual ~PtexIncrWriter()
Definition: PtexWriter.cpp:1265
PtexIncrWriter::PtexIncrWriter
PtexIncrWriter(const char *path, FILE *fp, Ptex::MeshType mt, Ptex::DataType dt, int nchannels, int alphachan, int nfaces)
Definition: PtexWriter.cpp:1217
PtexMainWriter::flagConstantNeighorhoods
void flagConstantNeighorhoods()
Definition: PtexWriter.cpp:996
FaceDataHeader
Definition: PtexIO.h:82
PtexMainWriter::~PtexMainWriter
virtual ~PtexMainWriter()
Definition: PtexWriter.cpp:774
PtexWriter
Interface for writing data to a ptex file.
Definition: Ptexture.h:823
PtexWriterBase::finish
virtual void finish()=0
Ptex::String
Memory-managed string.
Definition: Ptexture.h:308
Ptex::MetaDataType
MetaDataType
Type of meta data entry.
Definition: Ptexture.h:115
PtexReader.h
PtexWriterBase::writeBlank
int writeBlank(FILE *fp, int size)
Definition: PtexWriter.cpp:487
PtexWriterBase::datatype
DataType datatype() const
Definition: PtexWriter.h:80
PtexWriterBase::setError
void setError(const std::string &error)
Definition: PtexWriter.h:110
PtexWriterBase::_header
Header _header
Definition: PtexWriter.h:118
PtexTexture
Interface for reading data from a ptex file.
Definition: Ptexture.h:470
PtexWriterBase::setEdgeFilterMode
virtual void setEdgeFilterMode(Ptex::EdgeFilterMode edgeFilterMode)
Set edge filter mode.
Definition: PtexWriter.h:57
PtexWriterBase::_metadata
std::vector< MetaEntry > _metadata
Definition: PtexWriter.h:121
PtexWriterBase
Definition: PtexWriter.h:50
Ptex::EdgeFilterMode
EdgeFilterMode
How to handle transformation across edges when filtering.
Definition: Ptexture.h:93
PtexWriterBase::addMetaData
virtual void addMetaData(const char *key, MetaDataType t, const void *value, int size)
Definition: PtexWriter.cpp:453
PtexMainWriter::writeMetaData
void writeMetaData(FILE *fp)
Definition: PtexWriter.cpp:1150
PtexWriterBase::_zstream
z_stream_s _zstream
Definition: PtexWriter.h:123
PtexIncrWriter::_fp
FILE * _fp
Definition: PtexWriter.h:197
PtexWriterBase::_extheader
ExtHeader _extheader
Definition: PtexWriter.h:119
PtexMainWriter::finish
virtual void finish()
Definition: PtexWriter.cpp:890
PtexMainWriter::LevelRec::pos
std::vector< FilePos > pos
Definition: PtexWriter.h:171
PtexMainWriter::close
virtual bool close(Ptex::String &error)
Close the file.
Definition: PtexWriter.cpp:780
PtexWriterBase::MetaEntry::key
std::string key
Definition: PtexWriter.h:83
PtexIncrWriter::finish
virtual void finish()
Definition: PtexWriter.cpp:1407
PtexReader
Definition: PtexReader.h:52
PtexMainWriter
Definition: PtexWriter.h:129
PtexMainWriter::_newpath
std::string _newpath
Definition: PtexWriter.h:154
PtexMainWriter::_rfaceids
std::vector< uint32_t > _rfaceids
Definition: PtexWriter.h:161
PtexMainWriter::_tmppath
std::string _tmppath
Definition: PtexWriter.h:155
PtexWriterBase::writeBlock
int writeBlock(FILE *fp, const void *data, int size)
Definition: PtexWriter.cpp:499
PtexWriterBase::readBlock
int readBlock(FILE *fp, void *data, int size)
Definition: PtexWriter.cpp:541
PtexWriterBase::MetaEntry::data
std::vector< uint8_t > data
Definition: PtexWriter.h:85
PtexWriterBase::writeFaceBlock
void writeFaceBlock(FILE *fp, const void *data, int stride, Res res, FaceDataHeader &fdh)
Definition: PtexWriter.cpp:602
PtexWriterBase::_reduceFn
PtexUtils::ReduceFn * _reduceFn
Definition: PtexWriter.h:125
Ptexture.h
Public API classes for reading, writing, caching, and filtering Ptex files.
Ptex::BorderMode
BorderMode
How to handle mesh border when filtering.
Definition: Ptexture.h:99
PtexWriterBase::PtexWriterBase
PtexWriterBase(const char *path, Ptex::MeshType mt, Ptex::DataType dt, int nchannels, int alphachan, int nfaces, bool compress)
Definition: PtexWriter.cpp:261
PtexIncrWriter::writeConstantFace
virtual bool writeConstantFace(int faceid, const FaceInfo &f, const void *data)
Definition: PtexWriter.cpp:1338
PtexWriterBase::MetaEntry
Definition: PtexWriter.h:82
PtexWriterBase::_tilepath
std::string _tilepath
Definition: PtexWriter.h:116
Ptex::MeshType
MeshType
Type of base mesh for which the textures are defined.
Definition: Ptexture.h:79
PtexIncrWriter
Definition: PtexWriter.h:181
PtexMainWriter::_faceinfo
std::vector< FaceInfo > _faceinfo
Definition: PtexWriter.h:159
PtexPlatform.h
Platform-specific classes, functions, and includes.
FilePos
off_t FilePos
Definition: PtexPlatform.h:87
PtexWriterBase::release
virtual void release()
Release resources held by this pointer (pointer becomes invalid).
Definition: PtexWriter.cpp:301
PtexWriterBase::close
virtual bool close(Ptex::String &error)
Close the file.
Definition: PtexWriter.cpp:316