Ptex: PtexWriter.h Source File

Ptex

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

PtexWriter.h
Go to the documentation of this file.
1 #ifndef PtexWriter_h
2 #define PtexWriter_h
3
4 /*
5 PTEX SOFTWARE
6 Copyright 2009 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
49 class PtexWriterBase : public PtexWriter, public PtexIO {
50 public:
51 virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
52 {
53 _extheader.ubordermode = uBorderMode;
54 _extheader.vbordermode = vBorderMode;
55 }
56 virtual void writeMeta(const char* key, const char* value);
57 virtual void writeMeta(const char* key, const int8_t* value, int count);
58 virtual void writeMeta(const char* key, const int16_t* value, int count);
59 virtual void writeMeta(const char* key, const int32_t* value, int count);
60 virtual void writeMeta(const char* key, const float* value, int count);
61 virtual void writeMeta(const char* key, const double* value, int count);
62 virtual void writeMeta(PtexMetaData* data);
63 virtual bool close(Ptex::String& error);
64 virtual void release();
65
66 bool ok(Ptex::String& error) {
67 if (!_ok) getError(error);
68 return _ok;
69 }
70 void getError(Ptex::String& error) {
71 error = (_error + "\nPtex file: " + _path).c_str();
72 }
73
74 protected:
75 struct MetaEntry {
76 std::string key;
77 MetaDataType datatype;
78 std::vector<uint8_t> data;
79 MetaEntry() : datatype(MetaDataType(0)), data() {}
80 };
81
82 virtual void finish() = 0;
83 PtexWriterBase(const char* path,
84 Ptex::MeshType mt, Ptex::DataType dt,
85 int nchannels, int alphachan, int nfaces,
86 bool compress);
87 virtual ~PtexWriterBase();
88
89 int writeBlank(FILE* fp, int size);
90 int writeBlock(FILE* fp, const void* data, int size);
91 int writeZipBlock(FILE* fp, const void* data, int size, bool finish=true);
92 int readBlock(FILE* fp, void* data, int size);
93 int copyBlock(FILE* dst, FILE* src, FilePos pos, int size);
94 Res calcTileRes(Res faceres);
95 virtual void addMetaData(const char* key, MetaDataType t, const void* value, int size);
96 void writeConstFaceBlock(FILE* fp, const void* data, FaceDataHeader& fdh);
97 void writeFaceBlock(FILE* fp, const void* data, int stride, Res res,
98 FaceDataHeader& fdh);
99 void writeFaceData(FILE* fp, const void* data, int stride, Res res,
100 FaceDataHeader& fdh);
101 void writeReduction(FILE* fp, const void* data, int stride, Res res);
102 int writeMetaDataBlock(FILE* fp, MetaEntry& val);
103 void setError(const std::string& error) { _error = error; _ok = false; }
104 bool storeFaceInfo(int faceid, FaceInfo& dest, const FaceInfo& src, int flags=0);
105
106 bool _ok; // true if no error has occurred
107 std::string _error; // the error text (if any)
108 std::string _path; // file path
109 std::string _tilepath; // temp tile file path ("<path>.tiles.tmp")
110 FILE* _tilefp; // temp tile file handle
111 Header _header; // the file header
112 ExtHeader _extheader; // extended header
113 int _pixelSize; // size of a pixel in bytes
114 std::vector<MetaEntry> _metadata; // meta data waiting to be written
115 std::map<std::string,int> _metamap; // for preventing duplicate keys
116 z_stream_s _zstream; // libzip compression stream
117
118 PtexUtils::ReduceFn* _reduceFn;
119 };
120
121
122 class PtexMainWriter : public PtexWriterBase {
123 public:
124 PtexMainWriter(const char* path, PtexTexture* tex,
125 Ptex::MeshType mt, Ptex::DataType dt,
126 int nchannels, int alphachan, int nfaces, bool genmipmaps);
127
128 virtual bool close(Ptex::String& error);
129 virtual bool writeFace(int faceid, const FaceInfo& f, const void* data, int stride);
130 virtual bool writeConstantFace(int faceid, const FaceInfo& f, const void* data);
131
132 protected:
133 virtual ~PtexMainWriter();
134 virtual void addMetaData(const char* key, MetaDataType t, const void* value, int size)
135 {
136 PtexWriterBase::addMetaData(key, t, value, size);
137 _hasNewData = true;
138 }
139
140 private:
141 virtual void finish();
142 void generateReductions();
143 void flagConstantNeighorhoods();
144 void storeConstValue(int faceid, const void* data, int stride, Res res);
145 void writeMetaData(FILE* fp);
146
147 std::string _newpath; // path to ".new" file
148 std::string _tmppath; // temp file path ("<path>.tmp")
149 FILE* _tmpfp; // temp file handle
150 bool _hasNewData; // true if data has been written
151 bool _genmipmaps; // true if mipmaps should be generated
152 std::vector<FaceInfo> _faceinfo; // info about each face
153 std::vector<uint8_t> _constdata; // constant data for each face
154 std::vector<uint32_t> _rfaceids; // faceid reordering for reduction levels
155 std::vector<uint32_t> _faceids_r; // faceid indexed by rfaceid
156
157 static const int MinReductionLog2 =2; // log2(minimum reduction size) - can tune
158 struct LevelRec {
159 // note: level 0 is ordered by faceid
160 // levels 1+ are reduction levels (half res in both u and v) and
161 // are ordered by rfaceid[faceid]. Also, faces with a minimum
162 // dimension (the smaller of u or v) smaller than MinReductionLog2
163 // are omitted from subsequent levels.
164 std::vector<FilePos> pos; // position of data blocks within _tmp file
165 std::vector<FaceDataHeader> fdh; // face data headers
166 };
167 std::vector<LevelRec> _levels; // info about each level
168 std::vector<FilePos> _rpos; // reduction file positions
169
170 PtexReader* _reader; // reader for accessing existing data in file
171 };
172
173
174 class PtexIncrWriter : public PtexWriterBase {
175 public:
176 PtexIncrWriter(const char* path, FILE* fp,
177 Ptex::MeshType mt, Ptex::DataType dt,
178 int nchannels, int alphachan, int nfaces);
179
180 virtual bool close(Ptex::String& error);
181 virtual bool writeFace(int faceid, const FaceInfo& f, const void* data, int stride);
182 virtual bool writeConstantFace(int faceid, const FaceInfo& f, const void* data);
183
184 protected:
185 void writeMetaDataEdit();
186 virtual void finish();
187 virtual ~PtexIncrWriter();
188
189 private:
190 FILE* _fp; // the file being edited
191 };
192
193 #endif
PtexWriterBase::_tilepath
std::string _tilepath
Definition: PtexWriter.h:109
PtexWriter
Interface for writing data to a ptex file.
Definition: Ptexture.h:737
PtexWriterBase::getError
void getError(Ptex::String &error)
Definition: PtexWriter.h:70
PtexWriterBase::storeFaceInfo
bool storeFaceInfo(int faceid, FaceInfo &dest, const FaceInfo &src, int flags=0)
Definition: PtexWriter.cpp:331
PtexWriterBase::~PtexWriterBase
virtual ~PtexWriterBase()
Definition: PtexWriter.cpp:312
PtexIncrWriter::close
virtual bool close(Ptex::String &error)
Close the file.
Definition: PtexWriter.cpp:1375
PtexWriterBase::MetaEntry
Definition: PtexWriter.h:75
PtexWriterBase::release
virtual void release()
Release resources held by this pointer (pointer becomes invalid).
Definition: PtexWriter.cpp:303
PtexWriterBase::writeReduction
void writeReduction(FILE *fp, const void *data, int stride, Res res)
Definition: PtexWriter.cpp:692
PtexWriterBase::_extheader
ExtHeader _extheader
Definition: PtexWriter.h:112
Ptex::String
Memory-managed string.
Definition: Ptexture.h:303
PtexWriterBase
Definition: PtexWriter.h:49
PtexMainWriter::LevelRec::fdh
std::vector< FaceDataHeader > fdh
Definition: PtexWriter.h:165
PtexWriterBase::_reduceFn
PtexUtils::ReduceFn * _reduceFn
Definition: PtexWriter.h:118
PtexWriterBase::writeFaceData
void writeFaceData(FILE *fp, const void *data, int stride, Res res, FaceDataHeader &fdh)
Definition: PtexWriter.cpp:631
Ptex::DataType
DataType
Type of data stored in texture file.
Definition: Ptexture.h:83
PtexMetaData
Meta data accessor.
Definition: Ptexture.h:337
PtexIncrWriter::finish
virtual void finish()
Definition: PtexWriter.cpp:1387
PtexWriterBase::_ok
bool _ok
Definition: PtexWriter.h:106
PtexMainWriter::LevelRec
Definition: PtexWriter.h:158
PtexMainWriter::writeMetaData
void writeMetaData(FILE *fp)
Definition: PtexWriter.cpp:1130
PtexWriterBase::ok
bool ok(Ptex::String &error)
Definition: PtexWriter.h:66
PtexMainWriter::_rfaceids
std::vector< uint32_t > _rfaceids
Definition: PtexWriter.h:154
PtexIO::ExtHeader::ubordermode
BorderMode ubordermode
Definition: PtexIO.h:63
PtexWriterBase::setBorderModes
virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
Set border modes.
Definition: PtexWriter.h:51
PtexMainWriter::writeConstantFace
virtual bool writeConstantFace(int faceid, const FaceInfo &f, const void *data)
Write constant texture data for a face.
Definition: PtexWriter.cpp:862
PtexWriterBase::_pixelSize
int _pixelSize
Definition: PtexWriter.h:113
PtexMainWriter::storeConstValue
void storeConstValue(int faceid, const void *data, int stride, Res res)
Definition: PtexWriter.cpp:877
Res
Pixel resolution of a given texture.
Definition: Ptexture.h:274
PtexWriterBase::_path
std::string _path
Definition: PtexWriter.h:108
PtexMainWriter::_faceinfo
std::vector< FaceInfo > _faceinfo
Definition: PtexWriter.h:152
PtexPlatform.h
Platform-specific classes, functions, and includes.
PtexMainWriter::_newpath
std::string _newpath
Definition: PtexWriter.h:147
PtexMainWriter::generateReductions
void generateReductions()
Definition: PtexWriter.cpp:1059
PtexMainWriter::close
virtual bool close(Ptex::String &error)
Close the file.
Definition: PtexWriter.cpp:779
Ptex::MetaDataType
MetaDataType
Type of meta data entry.
Definition: Ptexture.h:107
PtexMainWriter::MinReductionLog2
static const int MinReductionLog2
Definition: PtexWriter.h:157
PtexWriterBase::writeMetaDataBlock
int writeMetaDataBlock(FILE *fp, MetaEntry &val)
Definition: PtexWriter.cpp:709
PtexIncrWriter::_fp
FILE * _fp
Definition: PtexWriter.h:190
PtexMainWriter::finish
virtual void finish()
Definition: PtexWriter.cpp:889
PtexIO.h
FilePos
off_t FilePos
Definition: PtexPlatform.h:85
PtexWriterBase::_zstream
z_stream_s _zstream
Definition: PtexWriter.h:116
PtexMainWriter::_reader
PtexReader * _reader
Definition: PtexWriter.h:170
Ptex::BorderMode
BorderMode
How to handle mesh border when filtering.
Definition: Ptexture.h:91
PtexWriterBase::setError
void setError(const std::string &error)
Definition: PtexWriter.h:103
PtexWriterBase::writeZipBlock
int writeZipBlock(FILE *fp, const void *data, int size, bool finish=true)
Definition: PtexWriter.cpp:512
PtexIncrWriter::writeConstantFace
virtual bool writeConstantFace(int faceid, const FaceInfo &f, const void *data)
Write constant texture data for a face.
Definition: PtexWriter.cpp:1318
PtexIO::ExtHeader
Definition: PtexIO.h:62
PtexWriterBase::_tilefp
FILE * _tilefp
Definition: PtexWriter.h:110
FaceInfo
Information about a face, as stored in the Ptex file header.
Definition: Ptexture.h:350
PtexIncrWriter
Definition: PtexWriter.h:174
PtexWriterBase::copyBlock
int copyBlock(FILE *dst, FILE *src, FilePos pos, int size)
Definition: PtexWriter.cpp:553
PtexUtils::ReduceFn
void ReduceFn(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.h:132
PtexIO
Definition: PtexIO.h:41
PtexReader
Definition: PtexReader.h:72
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:725
PtexWriterBase::writeBlock
int writeBlock(FILE *fp, const void *data, int size)
Definition: PtexWriter.cpp:501
PtexMainWriter::~PtexMainWriter
virtual ~PtexMainWriter()
Definition: PtexWriter.cpp:773
PtexWriterBase::readBlock
int readBlock(FILE *fp, void *data, int size)
Definition: PtexWriter.cpp:543
PtexWriterBase::addMetaData
virtual void addMetaData(const char *key, MetaDataType t, const void *value, int size)
Definition: PtexWriter.cpp:455
PtexMainWriter::_hasNewData
bool _hasNewData
Definition: PtexWriter.h:150
PtexWriterBase::_error
std::string _error
Definition: PtexWriter.h:107
PtexWriterBase::close
virtual bool close(Ptex::String &error)
Close the file.
Definition: PtexWriter.cpp:318
Ptex::MeshType
MeshType
Type of base mesh for which the textures are defined.
Definition: Ptexture.h:77
MetaDataType
MetaDataType
Type of meta data entry.
Definition: Ptexture.h:220
PtexIncrWriter::writeMetaDataEdit
void writeMetaDataEdit()
Definition: PtexWriter.cpp:1342
PtexWriterBase::PtexWriterBase
PtexWriterBase(const char *path, Ptex::MeshType mt, Ptex::DataType dt, int nchannels, int alphachan, int nfaces, bool compress)
Definition: PtexWriter.cpp:262
PtexIO::ExtHeader::vbordermode
BorderMode vbordermode
Definition: PtexIO.h:64
PtexMainWriter::_rpos
std::vector< FilePos > _rpos
Definition: PtexWriter.h:168
PtexMainWriter::LevelRec::pos
std::vector< FilePos > pos
Definition: PtexWriter.h:164
PtexMainWriter::writeFace
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
Write texture data for a face.
Definition: PtexWriter.cpp:805
PtexMainWriter::_levels
std::vector< LevelRec > _levels
Definition: PtexWriter.h:167
PtexMainWriter::flagConstantNeighorhoods
void flagConstantNeighorhoods()
Definition: PtexWriter.cpp:995
PtexIncrWriter::~PtexIncrWriter
virtual ~PtexIncrWriter()
Definition: PtexWriter.cpp:1245
PtexWriterBase::calcTileRes
Res calcTileRes(Res faceres)
Definition: PtexWriter.cpp:572
PtexTexture
Interface for reading data from a ptex file.
Definition: Ptexture.h:439
PtexWriterBase::_header
Header _header
Definition: PtexWriter.h:111
PtexMainWriter::_genmipmaps
bool _genmipmaps
Definition: PtexWriter.h:151
PtexMainWriter::_faceids_r
std::vector< uint32_t > _faceids_r
Definition: PtexWriter.h:155
PtexWriterBase::MetaEntry::MetaEntry
MetaEntry()
Definition: PtexWriter.h:79
PtexWriterBase::writeBlank
int writeBlank(FILE *fp, int size)
Definition: PtexWriter.cpp:489
PtexWriterBase::finish
virtual void finish()=0
PtexMainWriter
Definition: PtexWriter.h:122
Ptex::FaceInfo
Information about a face, as stored in the Ptex file header.
Definition: Ptexture.h:237
PtexWriterBase::_metadata
std::vector< MetaEntry > _metadata
Definition: PtexWriter.h:114
PtexWriterBase::writeMeta
virtual void writeMeta(const char *key, const char *value)
Write a string as meta data.
Definition: PtexWriter.cpp:363
PtexIncrWriter::PtexIncrWriter
PtexIncrWriter(const char *path, FILE *fp, Ptex::MeshType mt, Ptex::DataType dt, int nchannels, int alphachan, int nfaces)
Definition: PtexWriter.cpp:1197
PtexWriterBase::_metamap
std::map< std::string, int > _metamap
Definition: PtexWriter.h:115
PtexWriterBase::writeConstFaceBlock
void writeConstFaceBlock(FILE *fp, const void *data, FaceDataHeader &fdh)
Definition: PtexWriter.cpp:594
PtexIncrWriter::writeFace
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
Write texture data for a face.
Definition: PtexWriter.cpp:1250
PtexMainWriter::_tmppath
std::string _tmppath
Definition: PtexWriter.h:148
PtexMainWriter::_tmpfp
FILE * _tmpfp
Definition: PtexWriter.h:149
PtexWriterBase::MetaEntry::key
std::string key
Definition: PtexWriter.h:76
PtexWriterBase::writeFaceBlock
void writeFaceBlock(FILE *fp, const void *data, int stride, Res res, FaceDataHeader &fdh)
Definition: PtexWriter.cpp:604
PtexWriterBase::MetaEntry::data
std::vector< uint8_t > data
Definition: PtexWriter.h:78
PtexReader.h
Ptexture.h
Public API classes for reading, writing, caching, and filtering Ptex files.
PtexWriterBase::MetaEntry::datatype
MetaDataType datatype
Definition: PtexWriter.h:77
PtexMainWriter::addMetaData
virtual void addMetaData(const char *key, MetaDataType t, const void *value, int size)
Definition: PtexWriter.h:134
PtexMainWriter::_constdata
std::vector< uint8_t > _constdata
Definition: PtexWriter.h:153