Use unique_ptr for gltf2 textures

pull/2155/head
Alexandre Avenel 2018-09-21 21:24:53 +02:00
parent 673885a6d3
commit 504e5fd5c5
3 changed files with 13 additions and 13 deletions

View File

@ -659,7 +659,7 @@ namespace glTF2
int width, height;
private:
uint8_t* mData;
std::unique_ptr<uint8_t[]> mData;
size_t mDataLength;
public:
@ -674,7 +674,7 @@ namespace glTF2
{ return mDataLength; }
inline const uint8_t* GetData() const
{ return mData; }
{ return mData.get(); }
inline uint8_t* StealData();

View File

@ -688,7 +688,6 @@ T Accessor::Indexer::GetValue(int i)
inline Image::Image()
: width(0)
, height(0)
, mData(0)
, mDataLength(0)
{
@ -704,7 +703,9 @@ inline void Image::Read(Value& obj, Asset& r)
if (ParseDataURI(uristr, uri->GetStringLength(), dataURI)) {
mimeType = dataURI.mediaType;
if (dataURI.base64) {
mDataLength = Util::DecodeBase64(dataURI.data, dataURI.dataLength, mData);
uint8_t *ptr = nullptr;
mDataLength = Util::DecodeBase64(dataURI.data, dataURI.dataLength, ptr);
mData.reset(ptr);
}
}
else {
@ -717,8 +718,9 @@ inline void Image::Read(Value& obj, Asset& r)
this->mDataLength = this->bufferView->byteLength;
// maybe this memcpy could be avoided if aiTexture does not delete[] pcData at destruction.
this->mData = new uint8_t [this->mDataLength];
memcpy(this->mData, buffer->GetPointer() + this->bufferView->byteOffset, this->mDataLength);
this->mData.reset(new uint8_t[this->mDataLength]);
memcpy(this->mData.get(), buffer->GetPointer() + this->bufferView->byteOffset, this->mDataLength);
if (Value* mtype = FindString(obj, "mimeType")) {
this->mimeType = mtype->GetString();
@ -729,10 +731,8 @@ inline void Image::Read(Value& obj, Asset& r)
inline uint8_t* Image::StealData()
{
uint8_t* data = mData;
mDataLength = 0;
mData = 0;
return data;
mDataLength = 0;
return mData.release();
}
inline void Image::SetData(uint8_t* data, size_t length, Asset& r)
@ -747,8 +747,8 @@ inline void Image::SetData(uint8_t* data, size_t length, Asset& r)
bufferView->byteOffset = b->AppendData(data, length);
}
else { // text file: will be stored as a data uri
this->mData = data;
this->mDataLength = length;
this->mData.reset(data);
this->mDataLength = length;
}
}

View File

@ -818,7 +818,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r)
// Add the embedded textures
for (size_t i = 0; i < r.images.Size(); ++i) {
Image img = r.images[i];
Image &img = r.images[i];
if (!img.HasData()) continue;
int idx = mScene->mNumTextures++;