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
49
50class 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,
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
126};
127
128
130public:
131 PtexMainWriter(const char* path, PtexTexture* tex,
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();
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
182 public:
183 PtexIncrWriter(const char* path, FILE* fp,
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
201
202#endif
Platform-specific classes, functions, and includes.
off_t FilePos
#define PTEX_NAMESPACE_END
Definition PtexVersion.h:62
Public API classes for reading, writing, caching, and filtering Ptex files.
virtual bool close(Ptex::String &error)
Close the file.
virtual void finish()
virtual bool writeConstantFace(int faceid, const FaceInfo &f, const void *data)
virtual ~PtexIncrWriter()
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
void writeMetaDataEdit()
void generateReductions()
std::string _tmppath
Definition PtexWriter.h:155
std::vector< uint8_t > _constdata
Definition PtexWriter.h:160
std::string _newpath
Definition PtexWriter.h:154
std::vector< uint32_t > _rfaceids
Definition PtexWriter.h:161
virtual bool close(Ptex::String &error)
Close the file.
virtual bool writeConstantFace(int faceid, const FaceInfo &f, const void *data)
PtexReader * _reader
Definition PtexWriter.h:177
virtual void finish()
std::vector< uint32_t > _faceids_r
Definition PtexWriter.h:162
void storeConstValue(int faceid, const void *data, int stride, Res res)
std::vector< FaceInfo > _faceinfo
Definition PtexWriter.h:159
virtual void addMetaData(const char *key, MetaDataType t, const void *value, int size)
Definition PtexWriter.h:141
void writeMetaData(FILE *fp)
virtual ~PtexMainWriter()
static const int MinReductionLog2
Definition PtexWriter.h:164
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
std::vector< FilePos > _rpos
Definition PtexWriter.h:175
std::vector< LevelRec > _levels
Definition PtexWriter.h:174
void flagConstantNeighorhoods()
Meta data accessor.
Definition Ptexture.h:328
Interface for reading data from a ptex file.
Definition Ptexture.h:457
std::map< std::string, int > _metamap
Definition PtexWriter.h:122
DataType datatype() const
Definition PtexWriter.h:80
int writeBlank(FILE *fp, int size)
virtual void setEdgeFilterMode(Ptex::EdgeFilterMode edgeFilterMode)
Set edge filter mode.
Definition PtexWriter.h:57
std::string _path
Definition PtexWriter.h:115
int copyBlock(FILE *dst, FILE *src, FilePos pos, int size)
void setError(const std::string &error)
Definition PtexWriter.h:110
int writeZipBlock(FILE *fp, const void *data, int size, bool finish=true)
virtual void writeMeta(const char *key, const char *value)
Write a string as meta data.
z_stream_s _zstream
Definition PtexWriter.h:123
virtual void finish()=0
virtual void addMetaData(const char *key, MetaDataType t, const void *value, int size)
virtual void release()
Release resources held by this pointer (pointer becomes invalid).
int writeBlock(FILE *fp, const void *data, int size)
std::string _tilepath
Definition PtexWriter.h:116
virtual bool close(Ptex::String &error)
Close the file.
void writeFaceData(FILE *fp, const void *data, int stride, Res res, FaceDataHeader &fdh)
int readBlock(FILE *fp, void *data, int size)
std::vector< MetaEntry > _metadata
Definition PtexWriter.h:121
void writeConstFaceBlock(FILE *fp, const void *data, FaceDataHeader &fdh)
int writeMetaDataBlock(FILE *fp, MetaEntry &val)
ExtHeader _extheader
Definition PtexWriter.h:119
PtexUtils::ReduceFn * _reduceFn
Definition PtexWriter.h:125
void writeReduction(FILE *fp, const void *data, int stride, Res res)
bool storeFaceInfo(int faceid, FaceInfo &dest, const FaceInfo &src, int flags=0)
virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
Set border modes.
Definition PtexWriter.h:52
virtual ~PtexWriterBase()
void getError(Ptex::String &error)
Definition PtexWriter.h:75
std::string _error
Definition PtexWriter.h:114
bool ok(Ptex::String &error)
Definition PtexWriter.h:71
void writeFaceBlock(FILE *fp, const void *data, int stride, Res res, FaceDataHeader &fdh)
Res calcTileRes(Res faceres)
Interface for writing data to a ptex file.
Definition Ptexture.h:810
Memory-managed string.
Definition Ptexture.h:296
void ReduceFn(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition PtexUtils.h:185
DataType
Type of data stored in texture file.
Definition Ptexture.h:72
MeshType
Type of base mesh for which the textures are defined.
Definition Ptexture.h:66
BorderMode
How to handle mesh border when filtering.
Definition Ptexture.h:86
EdgeFilterMode
How to handle transformation across edges when filtering.
Definition Ptexture.h:80
uint16_t edgefiltermode
Definition PtexIO.h:68
uint16_t vbordermode
Definition PtexIO.h:67
uint16_t ubordermode
Definition PtexIO.h:65
uint32_t datatype
Definition PtexIO.h:48
std::vector< FilePos > pos
Definition PtexWriter.h:171
std::vector< FaceDataHeader > fdh
Definition PtexWriter.h:172
std::vector< uint8_t > data
Definition PtexWriter.h:85