PtexWriter.h
Go to the documentation of this file.
#ifndef PtexWriter_h
#define PtexWriter_h
/*
PTEX SOFTWARE
Copyright 2014 Disney Enterprises, Inc. All rights reserved
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
Studios" or the names of its contributors may NOT be used to
endorse or promote products derived from this software without
specific prior written permission from Walt Disney Pictures.
Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*/
#include "PtexPlatform.h"
#include <zlib.h>
#include <map>
#include <vector>
#include <stdio.h>
#include "Ptexture.h"
#include "PtexIO.h"
#include "PtexReader.h"
PTEX_NAMESPACE_BEGIN
class PtexWriterBase : public PtexWriter {
public:
virtual void setBorderModes(Ptex::BorderMode uBorderMode, Ptex::BorderMode vBorderMode)
{
_extheader.ubordermode = uBorderMode;
_extheader.vbordermode = vBorderMode;
}
virtual void setEdgeFilterMode(Ptex::EdgeFilterMode edgeFilterMode)
{
_extheader.edgefiltermode = edgeFilterMode;
}
virtual void writeMeta(const char* key, const char* value);
virtual void writeMeta(const char* key, const int8_t* value, int count);
virtual void writeMeta(const char* key, const int16_t* value, int count);
virtual void writeMeta(const char* key, const int32_t* value, int count);
virtual void writeMeta(const char* key, const float* value, int count);
virtual void writeMeta(const char* key, const double* value, int count);
virtual void writeMeta(PtexMetaData* data);
virtual bool close(Ptex::String& error);
virtual void release();
bool ok(Ptex::String& error) {
if (!_ok) getError(error);
return _ok;
}
void getError(Ptex::String& error) {
error = (_error + "\nPtex file: " + _path).c_str();
}
protected:
DataType datatype() const { return DataType(_header.datatype); }
struct MetaEntry {
std::string key;
MetaDataType datatype;
std::vector<uint8_t> data;
MetaEntry() : datatype(MetaDataType(0)), data() {}
};
virtual void finish() = 0;
PtexWriterBase(const char* path,
Ptex::MeshType mt, Ptex::DataType dt,
int nchannels, int alphachan, int nfaces,
bool compress);
virtual ~PtexWriterBase();
int writeBlank(FILE* fp, int size);
int writeBlock(FILE* fp, const void* data, int size);
int writeZipBlock(FILE* fp, const void* data, int size, bool finish=true);
int readBlock(FILE* fp, void* data, int size);
int copyBlock(FILE* dst, FILE* src, FilePos pos, int size);
Res calcTileRes(Res faceres);
virtual void addMetaData(const char* key, MetaDataType t, const void* value, int size);
void writeConstFaceBlock(FILE* fp, const void* data, FaceDataHeader& fdh);
void writeFaceBlock(FILE* fp, const void* data, int stride, Res res,
FaceDataHeader& fdh);
void writeFaceData(FILE* fp, const void* data, int stride, Res res,
FaceDataHeader& fdh);
void writeReduction(FILE* fp, const void* data, int stride, Res res);
int writeMetaDataBlock(FILE* fp, MetaEntry& val);
void setError(const std::string& error) { _error = error; _ok = false; }
bool storeFaceInfo(int faceid, FaceInfo& dest, const FaceInfo& src, int flags=0);
bool _ok; // true if no error has occurred
std::string _error; // the error text (if any)
std::string _path; // file path
std::string _tilepath; // temp tile file path ("<path>.tiles.tmp")
FILE* _tilefp; // temp tile file handle
Header _header; // the file header
ExtHeader _extheader; // extended header
int _pixelSize; // size of a pixel in bytes
std::vector<MetaEntry> _metadata; // meta data waiting to be written
std::map<std::string,int> _metamap; // for preventing duplicate keys
z_stream_s _zstream; // libzip compression stream
PtexUtils::ReduceFn* _reduceFn;
};
class PtexMainWriter : public PtexWriterBase {
public:
PtexMainWriter(const char* path, PtexTexture* tex,
Ptex::MeshType mt, Ptex::DataType dt,
int nchannels, int alphachan, int nfaces, bool genmipmaps);
virtual bool close(Ptex::String& error);
virtual bool writeFace(int faceid, const FaceInfo& f, const void* data, int stride);
virtual bool writeConstantFace(int faceid, const FaceInfo& f, const void* data);
protected:
virtual ~PtexMainWriter();
virtual void addMetaData(const char* key, MetaDataType t, const void* value, int size)
{
PtexWriterBase::addMetaData(key, t, value, size);
_hasNewData = true;
}
private:
virtual void finish();
void generateReductions();
void flagConstantNeighorhoods();
void storeConstValue(int faceid, const void* data, int stride, Res res);
void writeMetaData(FILE* fp);
std::string _newpath; // path to ".new" file
std::string _tmppath; // temp file path ("<path>.tmp")
FILE* _tmpfp; // temp file handle
bool _hasNewData; // true if data has been written
bool _genmipmaps; // true if mipmaps should be generated
std::vector<FaceInfo> _faceinfo; // info about each face
std::vector<uint8_t> _constdata; // constant data for each face
std::vector<uint32_t> _rfaceids; // faceid reordering for reduction levels
std::vector<uint32_t> _faceids_r; // faceid indexed by rfaceid
static const int MinReductionLog2 =2; // log2(minimum reduction size) - can tune
struct LevelRec {
// note: level 0 is ordered by faceid
// levels 1+ are reduction levels (half res in both u and v) and
// are ordered by rfaceid[faceid]. Also, faces with a minimum
// dimension (the smaller of u or v) smaller than MinReductionLog2
// are omitted from subsequent levels.
std::vector<FilePos> pos; // position of data blocks within _tmp file
std::vector<FaceDataHeader> fdh; // face data headers
};
std::vector<LevelRec> _levels; // info about each level
std::vector<FilePos> _rpos; // reduction file positions
PtexReader* _reader; // reader for accessing existing data in file
};
class PtexIncrWriter : public PtexWriterBase {
public:
PtexIncrWriter(const char* path, FILE* fp,
Ptex::MeshType mt, Ptex::DataType dt,
int nchannels, int alphachan, int nfaces);
virtual bool close(Ptex::String& error);
virtual bool writeFace(int faceid, const FaceInfo& f, const void* data, int stride);
virtual bool writeConstantFace(int faceid, const FaceInfo& f, const void* data);
protected:
void writeMetaDataEdit();
virtual void finish();
virtual ~PtexIncrWriter();
private:
FILE* _fp; // the file being edited
};
PTEX_NAMESPACE_END
#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