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/*
5PTEX SOFTWARE
6Copyright 2014 Disney Enterprises, Inc. All rights reserved
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are
10met:
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
25Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
26CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
27BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
28FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
29IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
30CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
34THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36OF 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 {
51public:
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
79protected:
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 {
130public:
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
139protected:
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
147private:
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
PtexIO.h
PtexPlatform.h
Platform-specific classes, functions, and includes.
FilePos
off_t FilePos
Definition: PtexPlatform.h:99
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.
PtexIncrWriter
Definition: PtexWriter.h:181
PtexIncrWriter::close
virtual bool close(Ptex::String &error)
Close the file.
Definition: PtexWriter.cpp:1395
PtexIncrWriter::finish
virtual void finish()
Definition: PtexWriter.cpp:1407
PtexIncrWriter::writeConstantFace
virtual bool writeConstantFace(int faceid, const FaceInfo &f, const void *data)
Definition: PtexWriter.cpp:1338
PtexIncrWriter::_fp
FILE * _fp
Definition: PtexWriter.h:197
PtexIncrWriter::~PtexIncrWriter
virtual ~PtexIncrWriter()
Definition: PtexWriter.cpp:1265
PtexIncrWriter::writeFace
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
Definition: PtexWriter.cpp:1270
PtexIncrWriter::writeMetaDataEdit
void writeMetaDataEdit()
Definition: PtexWriter.cpp:1362
PtexMainWriter
Definition: PtexWriter.h:129
PtexMainWriter::generateReductions
void generateReductions()
Definition: PtexWriter.cpp:1079
PtexMainWriter::_tmppath
std::string _tmppath
Definition: PtexWriter.h:155
PtexMainWriter::_constdata
std::vector< uint8_t > _constdata
Definition: PtexWriter.h:160
PtexMainWriter::_newpath
std::string _newpath
Definition: PtexWriter.h:154
PtexMainWriter::_rfaceids
std::vector< uint32_t > _rfaceids
Definition: PtexWriter.h:161
PtexMainWriter::close
virtual bool close(Ptex::String &error)
Close the file.
Definition: PtexWriter.cpp:780
PtexMainWriter::_genmipmaps
bool _genmipmaps
Definition: PtexWriter.h:158
PtexMainWriter::writeConstantFace
virtual bool writeConstantFace(int faceid, const FaceInfo &f, const void *data)
Definition: PtexWriter.cpp:863
PtexMainWriter::_reader
PtexReader * _reader
Definition: PtexWriter.h:177
PtexMainWriter::_tmpfp
FILE * _tmpfp
Definition: PtexWriter.h:156
PtexMainWriter::finish
virtual void finish()
Definition: PtexWriter.cpp:890
PtexMainWriter::_faceids_r
std::vector< uint32_t > _faceids_r
Definition: PtexWriter.h:162
PtexMainWriter::storeConstValue
void storeConstValue(int faceid, const void *data, int stride, Res res)
Definition: PtexWriter.cpp:878
PtexMainWriter::_faceinfo
std::vector< FaceInfo > _faceinfo
Definition: PtexWriter.h:159
PtexMainWriter::addMetaData
virtual void addMetaData(const char *key, MetaDataType t, const void *value, int size)
Definition: PtexWriter.h:141
PtexMainWriter::writeMetaData
void writeMetaData(FILE *fp)
Definition: PtexWriter.cpp:1150
PtexMainWriter::~PtexMainWriter
virtual ~PtexMainWriter()
Definition: PtexWriter.cpp:774
PtexMainWriter::_hasNewData
bool _hasNewData
Definition: PtexWriter.h:157
PtexMainWriter::MinReductionLog2
static const int MinReductionLog2
Definition: PtexWriter.h:164
PtexMainWriter::writeFace
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
Definition: PtexWriter.cpp:806
PtexMainWriter::_rpos
std::vector< FilePos > _rpos
Definition: PtexWriter.h:175
PtexMainWriter::_levels
std::vector< LevelRec > _levels
Definition: PtexWriter.h:174
PtexMainWriter::flagConstantNeighorhoods
void flagConstantNeighorhoods()
Definition: PtexWriter.cpp:996
PtexMetaData
Meta data accessor.
Definition: Ptexture.h:328
PtexReader
Definition: PtexReader.h:52
PtexTexture
Interface for reading data from a ptex file.
Definition: Ptexture.h:457
PtexWriterBase
Definition: PtexWriter.h:50
PtexWriterBase::_metamap
std::map< std::string, int > _metamap
Definition: PtexWriter.h:122
PtexWriterBase::datatype
DataType datatype() const
Definition: PtexWriter.h:80
PtexWriterBase::writeBlank
int writeBlank(FILE *fp, int size)
Definition: PtexWriter.cpp:487
PtexWriterBase::setEdgeFilterMode
virtual void setEdgeFilterMode(Ptex::EdgeFilterMode edgeFilterMode)
Set edge filter mode.
Definition: PtexWriter.h:57
PtexWriterBase::_path
std::string _path
Definition: PtexWriter.h:115
PtexWriterBase::copyBlock
int copyBlock(FILE *dst, FILE *src, FilePos pos, int size)
Definition: PtexWriter.cpp:551
PtexWriterBase::setError
void setError(const std::string &error)
Definition: PtexWriter.h:110
PtexWriterBase::writeZipBlock
int writeZipBlock(FILE *fp, const void *data, int size, bool finish=true)
Definition: PtexWriter.cpp:510
PtexWriterBase::writeMeta
virtual void writeMeta(const char *key, const char *value)
Write a string as meta data.
Definition: PtexWriter.cpp:361
PtexWriterBase::_zstream
z_stream_s _zstream
Definition: PtexWriter.h:123
PtexWriterBase::_ok
bool _ok
Definition: PtexWriter.h:113
PtexWriterBase::finish
virtual void finish()=0
PtexWriterBase::addMetaData
virtual void addMetaData(const char *key, MetaDataType t, const void *value, int size)
Definition: PtexWriter.cpp:453
PtexWriterBase::release
virtual void release()
Release resources held by this pointer (pointer becomes invalid).
Definition: PtexWriter.cpp:301
PtexWriterBase::writeBlock
int writeBlock(FILE *fp, const void *data, int size)
Definition: PtexWriter.cpp:499
PtexWriterBase::_tilefp
FILE * _tilefp
Definition: PtexWriter.h:117
PtexWriterBase::_tilepath
std::string _tilepath
Definition: PtexWriter.h:116
PtexWriterBase::close
virtual bool close(Ptex::String &error)
Close the file.
Definition: PtexWriter.cpp:316
PtexWriterBase::writeFaceData
void writeFaceData(FILE *fp, const void *data, int stride, Res res, FaceDataHeader &fdh)
Definition: PtexWriter.cpp:629
PtexWriterBase::readBlock
int readBlock(FILE *fp, void *data, int size)
Definition: PtexWriter.cpp:541
PtexWriterBase::_metadata
std::vector< MetaEntry > _metadata
Definition: PtexWriter.h:121
PtexWriterBase::writeConstFaceBlock
void writeConstFaceBlock(FILE *fp, const void *data, FaceDataHeader &fdh)
Definition: PtexWriter.cpp:592
PtexWriterBase::writeMetaDataBlock
int writeMetaDataBlock(FILE *fp, MetaEntry &val)
Definition: PtexWriter.cpp:707
PtexWriterBase::_extheader
ExtHeader _extheader
Definition: PtexWriter.h:119
PtexWriterBase::_reduceFn
PtexUtils::ReduceFn * _reduceFn
Definition: PtexWriter.h:125
PtexWriterBase::writeReduction
void writeReduction(FILE *fp, const void *data, int stride, Res res)
Definition: PtexWriter.cpp:690
PtexWriterBase::storeFaceInfo
bool storeFaceInfo(int faceid, FaceInfo &dest, const FaceInfo &src, int flags=0)
Definition: PtexWriter.cpp:329
PtexWriterBase::setBorderModes
virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
Set border modes.
Definition: PtexWriter.h:52
PtexWriterBase::~PtexWriterBase
virtual ~PtexWriterBase()
Definition: PtexWriter.cpp:310
PtexWriterBase::getError
void getError(Ptex::String &error)
Definition: PtexWriter.h:75
PtexWriterBase::_error
std::string _error
Definition: PtexWriter.h:114
PtexWriterBase::_header
Header _header
Definition: PtexWriter.h:118
PtexWriterBase::_pixelSize
int _pixelSize
Definition: PtexWriter.h:120
PtexWriterBase::ok
bool ok(Ptex::String &error)
Definition: PtexWriter.h:71
PtexWriterBase::writeFaceBlock
void writeFaceBlock(FILE *fp, const void *data, int stride, Res res, FaceDataHeader &fdh)
Definition: PtexWriter.cpp:602
PtexWriterBase::calcTileRes
Res calcTileRes(Res faceres)
Definition: PtexWriter.cpp:570
PtexWriter
Interface for writing data to a ptex file.
Definition: Ptexture.h:810
Ptex::String
Memory-managed string.
Definition: Ptexture.h:296
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:185
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
ExtHeader::edgefiltermode
uint16_t edgefiltermode
Definition: PtexIO.h:68
ExtHeader::vbordermode
uint16_t vbordermode
Definition: PtexIO.h:67
ExtHeader::ubordermode
uint16_t ubordermode
Definition: PtexIO.h:65
FaceDataHeader
Definition: PtexIO.h:82
Header
Definition: PtexIO.h:44
Header::datatype
uint32_t datatype
Definition: PtexIO.h:48
PtexMainWriter::LevelRec
Definition: PtexWriter.h:165
PtexMainWriter::LevelRec::pos
std::vector< FilePos > pos
Definition: PtexWriter.h:171
PtexMainWriter::LevelRec::fdh
std::vector< FaceDataHeader > fdh
Definition: PtexWriter.h:172
PtexWriterBase::MetaEntry
Definition: PtexWriter.h:82
PtexWriterBase::MetaEntry::MetaEntry
MetaEntry()
Definition: PtexWriter.h:86
PtexWriterBase::MetaEntry::key
std::string key
Definition: PtexWriter.h:83
PtexWriterBase::MetaEntry::datatype
MetaDataType datatype
Definition: PtexWriter.h:84
PtexWriterBase::MetaEntry::data
std::vector< uint8_t > data
Definition: PtexWriter.h:85