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 <map>
41#include <vector>
42#include <stdio.h>
43#include "Ptexture.h"
44#include "PtexIO.h"
45#include "PtexReader.h"
46
47struct libdeflate_compressor;
48
50
51class PtexMainWriter : public PtexWriter {
52public:
53 PtexMainWriter(const char* path, PtexTexture* tex,
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
87private:
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();
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;
129
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
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
152
153#endif
Platform-specific classes, functions, and includes.
#define PTEX_NAMESPACE_END
Definition PtexVersion.h:62
Public API classes for reading, writing, caching, and filtering Ptex files.
std::string _error
Definition PtexWriter.h:119
Res calcTileRes(Res faceres)
void releaseCompressor(libdeflate_compressor *compressor)
void getError(Ptex::String &error)
Definition PtexWriter.h:83
void compressFaceDataBlock(libdeflate_compressor *compressor, std::vector< std::byte > &compressedData, FaceDataHeader &fdh, Res res, const void *uncompressedData, int stride)
std::vector< libdeflate_compressor * > _compressors
Definition PtexWriter.h:127
bool storeFaceInfo(int faceid, FaceInfo &dest, const FaceInfo &src, int flags=0)
void addToDataBlock(std::vector< std::byte > &dataBlock, const void *data, size_t size)
std::string _newpath
Definition PtexWriter.h:132
std::vector< uint32_t > _rfaceids
Definition PtexWriter.h:136
size_t writeBlock(FILE *fp, const void *data, size_t size)
std::map< std::string, int > _metamap
Definition PtexWriter.h:125
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:147
bool ok(Ptex::String &error)
Definition PtexWriter.h:79
std::vector< uint32_t > _faceids_r
Definition PtexWriter.h:137
virtual void writeMeta(const char *key, const char *value)
Write a string as meta data.
void compressDataBlock(libdeflate_compressor *compressor, std::vector< std::byte > &compressedData, const void *data, size_t size)
void setError(const std::string &error)
Definition PtexWriter.h:112
void storeConstValue(int faceid, const void *data, int stride, Res res)
std::vector< FaceInfo > _faceinfo
Definition PtexWriter.h:134
ExtHeader _extheader
Definition PtexWriter.h:122
void addMetaData(const char *key, MetaDataType t, const void *value, int size)
void compressFaceData(libdeflate_compressor *compressor, std::vector< std::byte > &compressedData, FaceDataHeader &fdh, Res res, const void *uncompressedData)
virtual ~PtexMainWriter()
virtual void setEdgeFilterMode(Ptex::EdgeFilterMode edgeFilterMode)
Set edge filter mode.
Definition PtexWriter.h:67
std::vector< MetaEntry > _metadata
Definition PtexWriter.h:124
PtexUtils::ReduceFn * _reduceFn
Definition PtexWriter.h:130
static const int MinReductionLog2
Definition PtexWriter.h:139
void addToMetaDataBlock(std::vector< std::byte > &dataBlock, const MetaEntry &val)
std::string _path
Definition PtexWriter.h:120
size_t writeBlock(FILE *fp, const std::vector< std::byte > &dataBlock)
Definition PtexWriter.h:100
DataType datatype() const
Definition PtexWriter.h:90
std::vector< FaceRec > _faces
Definition PtexWriter.h:145
virtual bool writeFace(int faceid, const FaceInfo &f, const void *data, int stride)
Mutex _compressorMutex
Definition PtexWriter.h:128
std::vector< std::byte > _constdata
Definition PtexWriter.h:135
PtexMainWriter(const char *path, PtexTexture *tex, Ptex::MeshType mt, Ptex::DataType dt, int nchannels, int alphachan, int nfaces, bool genmipmaps)
virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
Set border modes.
Definition PtexWriter.h:62
void flagConstantNeighorhoods()
virtual void release()
Release resources held by this pointer (pointer becomes invalid).
libdeflate_compressor * getCompressor()
Meta data accessor.
Definition Ptexture.h:331
Interface for reading data from a ptex file.
Definition Ptexture.h:460
Interface for writing data to a ptex file.
Definition Ptexture.h:819
Memory-managed string.
Definition Ptexture.h:299
void ReduceFn(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition PtexUtils.h:167
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
std::vector< std::vector< std::byte > > faceData
Definition PtexWriter.h:141
std::vector< FaceDataHeader > fdh
Definition PtexWriter.h:142
std::vector< uint8_t > data
Definition PtexWriter.h:95