Jelajahi Sumber

glTFAsset: Use std:unique_ptr for Image data

Turo Lamminen 7 tahun lalu
induk
melakukan
7ebd8e7543
3 mengubah file dengan 10 tambahan dan 11 penghapusan
  1. 2 2
      code/glTFAsset.h
  2. 7 8
      code/glTFAsset.inl
  3. 1 1
      code/glTFImporter.cpp

+ 2 - 2
code/glTFAsset.h

@@ -649,7 +649,7 @@ namespace glTF
         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:
@@ -664,7 +664,7 @@ namespace glTF
             { 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();
 
 

+ 7 - 8
code/glTFAsset.inl

@@ -601,7 +601,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)
 {
 {
 
 
@@ -624,8 +623,8 @@ inline void Image::Read(Value& obj, Asset& r)
                     Ref<BufferView> bv = r.bufferViews.Get(bufferViewId);
                     Ref<BufferView> bv = r.bufferViews.Get(bufferViewId);
                     if (bv) {
                     if (bv) {
                         mDataLength = bv->byteLength;
                         mDataLength = bv->byteLength;
-                        mData = new uint8_t[mDataLength];
-                        memcpy(mData, bv->buffer->GetPointer() + bv->byteOffset, mDataLength);
+                        mData.reset(new uint8_t[mDataLength]);
+                        memcpy(mData.get(), bv->buffer->GetPointer() + bv->byteOffset, mDataLength);
                     }
                     }
                 }
                 }
             }
             }
@@ -640,7 +639,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 {
@@ -652,10 +653,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;
     mDataLength = 0;
-    mData = 0;
-    return data;
+    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)
@@ -670,7 +669,7 @@ 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->mData.reset(data);
         this->mDataLength = length;
         this->mDataLength = length;
     }
     }
 }
 }

+ 1 - 1
code/glTFImporter.cpp

@@ -616,7 +616,7 @@ void glTFImporter::ImportEmbeddedTextures(glTF::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++;