Browse Source

Use unique_ptr for gltf2 textures

Alexandre Avenel 7 years ago
parent
commit
504e5fd5c5
3 changed files with 13 additions and 13 deletions
  1. 2 2
      code/glTF2Asset.h
  2. 10 10
      code/glTF2Asset.inl
  3. 1 1
      code/glTF2Importer.cpp

+ 2 - 2
code/glTF2Asset.h

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

+ 10 - 10
code/glTF2Asset.inl

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

+ 1 - 1
code/glTF2Importer.cpp

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