54 template<
typename T>
class safevector :
public std::vector<T>
58 safevector(
size_t n,
const T& val = T()) : std::vector<T>(n, val) {}
59 const T& operator[] (
size_t n)
const {
60 assert(n < std::vector<T>::size());
61 return std::vector<T>::operator[](n);
63 T& operator[] (
size_t n) {
64 assert(n < std::vector<T>::size());
65 return std::vector<T>::operator[](n);
69 #define safevector std::vector
79 virtual void release();
80 virtual const char*
path() {
return _path.c_str(); }
87 virtual int numFaces() {
return _header.nfaces; }
89 virtual bool hasMipMaps() {
return _header.nlevels > 1; }
93 virtual void getData(
int faceid,
void* buffer,
int stride);
94 virtual void getData(
int faceid,
void* buffer,
int stride,
Res res);
97 virtual void getPixel(
int faceid,
int u,
int v,
98 float* result,
int firstchan,
int nchannels);
99 virtual void getPixel(
int faceid,
int u,
int v,
100 float* result,
int firstchan,
int nchannels,
106 const Header&
header()
const {
return _header; }
113 :
PtexCachedData((void**)parent, cache, (int)sizeof(*this) + size),
118 for (
size_t i = 0, n = _lmdRefs.size(); i < n; i++)
119 _lmdRefs[i]->unref();
126 virtual int numKeys() {
return int(_entries.size()); }
129 Entry* e = _entries[n];
134 virtual void getValue(
const char* key,
const char*& value)
136 Entry* e = getEntry(key);
137 if (e) value = (
const char*) e->
data;
141 virtual void getValue(
const char* key,
const int8_t*& value,
int& count)
143 Entry* e = getEntry(key);
144 if (e) { value = (
const int8_t*) e->
data; count = e->
datasize; }
145 else { value = 0; count = 0; }
148 virtual void getValue(
const char* key,
const int16_t*& value,
int& count)
150 Entry* e = getEntry(key);
152 value = (
const int16_t*) e->
data;
153 count =
int(e->
datasize/
sizeof(int16_t));
155 else { value = 0; count = 0; }
158 virtual void getValue(
const char* key,
const int32_t*& value,
int& count)
160 Entry* e = getEntry(key);
162 value = (
const int32_t*) e->
data;
163 count =
int(e->
datasize/
sizeof(int32_t));
165 else { value = 0; count = 0; }
168 virtual void getValue(
const char* key,
const float*& value,
int& count)
170 Entry* e = getEntry(key);
172 value = (
const float*) e->
data;
173 count =
int(e->
datasize/
sizeof(
float));
175 else { value = 0; count = 0; }
178 virtual void getValue(
const char* key,
const double*& value,
int& count)
180 Entry* e = getEntry(key);
182 value = (
const double*) e->
data;
183 count =
int(e->
datasize/
sizeof(
double));
185 else { value = 0; count = 0; }
188 void addEntry(uint8_t keysize,
const char* key, uint8_t datatype,
189 uint32_t datasize,
void* data)
191 Entry* e = newEntry(keysize, key, datatype, datasize);
192 e->
data = malloc(datasize);
193 memcpy(e->
data, data, datasize);
196 void addLmdEntry(uint8_t keysize,
const char* key, uint8_t datatype,
197 uint32_t datasize,
FilePos filepos, uint32_t zipsize)
199 Entry* e = newEntry(keysize, key, datatype, datasize);
230 isLmd(0), lmdData(0), lmdPos(0), lmdZipSize(0) {}
235 if (lmdData) { lmdData->
orphan(); lmdData = 0; }
246 Entry*
newEntry(uint8_t keysize,
const char* key, uint8_t datatype, uint32_t datasize)
248 std::pair<MetaMap::iterator,bool> result =
249 _map.insert(std::make_pair(std::string(key, keysize),
Entry()));
250 Entry* e = &result.first->second;
251 bool newEntry = result.second;
252 if (newEntry) _entries.push_back(e);
254 e->
key = result.first->first.c_str();
260 Entry* getEntry(
const char* key);
273 : _data(data), _pixelsize(pixelsize) {}
278 { memcpy(result, _data, _pixelsize); }
305 :
FaceData(parent, cache, res, (int)sizeof(*this)+size),
306 _pixelsize(pixelsize), _data(malloc(size)) {}
311 memcpy(result, (
char*)_data + (v*_res.u() + u) * _pixelsize, _pixelsize);
330 :
PackedFace(parent, cache, 0, pixelsize, pixelsize) {}
332 virtual void getPixel(
int,
int,
void* result) { memcpy(result, _data, _pixelsize); }
342 :
FaceData(parent, cache, res, sizeof(*this)),
348 _ntilesu = _res.ntilesu(tileres);
349 _ntilesv = _res.ntilesv(tileres);
350 _ntiles = _ntilesu*_ntilesv;
351 _tiles.resize(_ntiles);
352 incSize((
int)
sizeof(
FaceData*)*_ntiles);
366 virtual void getPixel(
int u,
int v,
void* result);
396 reader->datatype(), reader->nchannels()),
400 _fdh.resize(_ntiles),
401 _offsets.resize(_ntiles);
408 if (!f) readTile(tile, f);
412 void readTile(
int tile,
FaceData*& data);
418 safevector<FaceDataHeader>
_fdh;
429 _parentface(parentface),
437 _parentface->unref();
449 safevector<FaceDataHeader>
fdh;
472 _error = error; _error +=
" PtexFile: "; _error += _path;
486 bool readBlock(
void* data,
int size,
bool reportError=
true);
487 bool readZipBlock(
void* data,
int zipsize,
int unzipsize);
490 Level*& level = _levels[levelid];
491 if (!level) readLevel(levelid, level);
496 uint8_t*
getConstData() {
if (!_constdata) readConstData();
return _constdata; }
500 if (!face) readFace(levelid, level, faceid);
507 if (levelid == 0)
return _faceinfo[faceid].res;
510 Res res = _res_r[faceid];
512 return Res((uint8_t)(res.
ulog2 - levelid), (uint8_t)(res.
vlog2 - levelid));
520 return levelid == 0 ? 0 : _pixelsize;
522 return getRes(levelid, faceid).size() * _pixelsize;
526 void readLevelInfo();
527 void readConstData();
528 void readLevel(
int levelid, Level*& level);
529 void readFace(
int levelid, Level* level,
int faceid);
530 void readFaceData(
FilePos pos, FaceDataHeader fdh,
Res res,
int levelid, FaceData*& face);
532 void readMetaDataBlock(MetaData* metadata,
FilePos pos,
int zipsize,
int memsize);
533 void readLargeMetaDataHeaders(MetaData* metadata,
FilePos pos,
int zipsize,
int memsize);
535 void readEditFaceData();
536 void readEditMetaData();
540 FilePos* end = offsets + noffsets;
541 while (offsets != end) { *offsets++ = pos; pos += fdh->
blocksize(); fdh++; }
543 void blendFaces(FaceData*& face,
int faceid,
Res res,
bool blendu);
550 FILE* fp = fopen(path,
"rb");
552 buffer = (
char*) malloc(IBuffSize);
553 setvbuf(fp, buffer, _IOFBF, IBuffSize);
558 virtual void seek(
Handle handle, int64_t pos) { fseeko((FILE*)handle, pos, SEEK_SET); }
559 virtual size_t read(
void* buffer,
size_t size,
Handle handle) {
560 return fread(buffer, size, 1, (FILE*)handle) == 1 ? size : 0;
563 bool ok = handle && (fclose((FILE*)handle) == 0);
564 if (buffer) free(buffer);
568 virtual const char*
lastError() {
return strerror(errno); }
624 {
return k.
faceid == faceid && k.
res == res; }
629 static uint32_t M = 1664525,
C = 1013904223;
DefaultInputHandler _defaultIo
void handlePendingDelete()
FaceData * getFace(int levelid, Level *level, int faceid)
virtual int numFaces()
Number of faces stored in file.
virtual void release()
Release resources held by this pointer (pointer becomes invalid).
virtual bool hasMipMaps()
True if the file has mipmaps.
virtual PtexFaceData * getTile(int)
Access a tile from the data block.
const ExtHeader & extheader() const
safevector< FaceData * > _tiles
safevector< LevelInfo > _levelinfo
virtual void * getData()
Access the data from this data block.
virtual void release()
Release resources held by this pointer (pointer becomes invalid).
virtual bool hasEdits()
True if the file has edit blocks.
virtual Ptex::Res tileRes()
Resolution of each tile in this data block.
MeshType
Type of base mesh for which the textures are defined.
ReductionKey(uint32_t faceid, Res res)
FaceData(void **parent, PtexCacheImpl *cache, Res res, int size)
virtual PtexFaceData * getTile(int)
Access a tile from the data block.
safevector< FilePos > _levelpos
safevector< FilePos > _offsets
DataType
Type of data stored in texture file.
safevector< MetaEdit > _metaedits
safevector< Level * > _levels
virtual int alphaChannel()
Index of alpha channel (if any).
virtual bool isConstant()
True if this data block is constant.
virtual const char * path()
Path that file was opened with.
virtual int numChannels()
Number of channels stored in file.
Pixel resolution of a given texture.
MetaDataType
Type of meta data entry.
virtual Ptex::Res res()
Resolution of the texture held by this data block.
int8_t vlog2
log base 2 of v resolution, in texels
virtual void getPixel(int, int, void *result)
Read a single texel from the data block.
PackedFace(void **parent, PtexCacheImpl *cache, Res res, int pixelsize, int size)
virtual Ptex::DataType dataType()
Type of data stored in file.
BorderMode
How to handle mesh border when filtering.
safevector< FaceEdit > _faceedits
virtual void release()
Release resources held by this pointer (pointer becomes invalid).
safevector< uint32_t > _rfaceids
static int DataSize(DataType dt)
Look up size of given data type (in bytes).
Per-face texture data accessor.
Res getRes(int levelid, int faceid)
int8_t vlog2
log base 2 of v resolution, in texels
virtual Ptex::BorderMode vBorderMode()
Mode for filtering texture access beyond mesh border.
const LevelInfo & levelinfo(int level) const
virtual void getPixel(int u, int v, void *result)
Read a single texel from the data block.
virtual bool isTiled()
True if this data block is tiled.
virtual Ptex::BorderMode uBorderMode()
Mode for filtering texture access beyond mesh border.
virtual bool isConstant()
True if this data block is constant.
int8_t ulog2
log base 2 of u resolution, in texels
virtual void * getData()
Access the data from this data block.
Cache entry for allocated memory block.
DataType
Type of data stored in texture file.
virtual bool isConstant()
True if this data block is constant.
safevector< FilePos > offsets
BorderMode
How to handle mesh border when filtering.
virtual Ptex::Res tileRes()
Resolution of each tile in this data block.
void computeOffsets(FilePos pos, int noffsets, const FaceDataHeader *fdh, FilePos *offsets)
void ReduceFn(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
safevector< FaceDataHeader > fdh
void setError(const char *error)
virtual Ptex::Res res()
Resolution of the texture held by this data block.
virtual PtexFaceData * getTile(int tile)
Access a tile from the data block.
DataType datatype() const
virtual bool isConstant()
True if this data block is constant.
MeshType
Type of base mesh for which the textures are defined.
MetaDataType
Type of meta data entry.
virtual bool isTiled()
True if this data block is tiled.
Level * getLevel(int levelid)
virtual bool isTiled()
True if this data block is tiled.
PtexInputHandler::Handle _fp
const Header & header() const
virtual void getPixel(int, int, void *result)
Read a single texel from the data block.
PtexUtils::ReduceFn * _reducefn
bool operator==(const ReductionKey &k) const
virtual void * getData()
Access the data from this data block.
int8_t ulog2
log base 2 of u resolution, in texels
Interface for reading data from a ptex file.
PtexHashMap< ReductionKey, FaceData *, ReductionKey::Hasher > ReductionMap
safevector< FaceData * > faces
Pixel resolution of a given texture.
safevector< FaceDataHeader > _fdh
Cache entry for open file handle.
safevector< FaceInfo > _faceinfo
virtual Ptex::MeshType meshType()
Type of mesh for which texture data is defined.
Contains PtexHashMap, a lightweight hash table.
ConstDataPtr(void *data, int pixelsize)
Ptex cache implementation.
virtual Ptex::Res tileRes()
Resolution of each tile in this data block.
Automatically acquire and release lock within enclosing scope.
Information about a face, as stored in the Ptex file header.
TiledFaceBase(void **parent, PtexCacheImpl *cache, Res res, Res tileres, DataType dt, int nchan)
TiledFace(void **parent, PtexCacheImpl *cache, Res res, Res tileres, int levelid, PtexReader *reader)
Level(void **parent, PtexCacheImpl *cache, int nfaces)
Public API classes for reading, writing, caching, and filtering Ptex files.
int unpackedSize(FaceDataHeader fdh, int levelid, int faceid)
ConstantFace(void **parent, PtexCacheImpl *cache, int pixelsize)
TiledReducedFace(void **parent, PtexCacheImpl *cache, Res res, Res tileres, DataType dt, int nchan, TiledFaceBase *parentface, PtexUtils::ReduceFn reducefn)
TiledFaceBase * _parentface