example:refactor to remove naked malloc and RAII warning
authorsubhransu mohanty <sub.mohanty@samsung.com>
Mon, 20 May 2019 04:10:43 +0000 (13:10 +0900)
committerHermet Park <hermetpark@gmail.com>
Wed, 22 May 2019 04:01:09 +0000 (13:01 +0900)
example/lottie2gif.cpp

index 3e2e91a..ae9c58c 100644 (file)
@@ -4,15 +4,19 @@
 #include<iostream>
 #include<vector>
 #include<array>
-#include<cstdlib>
+#include<libgen.h>
 
 class GifBuilder {
 public:
-    GifBuilder(const std::string &fileName , const uint32_t width,
-               const uint32_t height, const uint32_t delay = 2)
+    explicit GifBuilder(const std::string &fileName , const uint32_t width,
+                        const uint32_t height, const uint32_t delay = 2)
     {
         GifBegin(&handle, fileName.c_str(), width, height, delay);
     }
+    ~GifBuilder()
+    {
+        GifEnd(&handle);
+    }
     void addFrame(rlottie::Surface &s, uint32_t delay = 2)
     {
         argbTorgba(s);
@@ -56,10 +60,7 @@ public:
            }
         }
     }
-    void commit()
-    {
-        GifEnd(&handle);
-    }
+
 private:
     GifWriter      handle;
 };
@@ -71,18 +72,15 @@ public:
         auto player = rlottie::Animation::loadFromFile(fileName);
         if (!player) return help();
 
-        uint32_t* buffer = (uint32_t *) malloc(w * h * 4);
+        auto buffer = std::make_unique<uint32_t[]>(w * h);
         size_t frameCount = player->totalFrame();
 
         GifBuilder builder(baseName.data(), w, h);
         for (size_t i = 0; i < frameCount ; i++) {
-            rlottie::Surface surface(buffer, w, h, w * 4);
+            rlottie::Surface surface(buffer.get(), w, h, w * 4);
             player->renderSync(i, surface);
             builder.addFrame(surface);
         }
-        builder.commit();
-
-        free(buffer);
         return result();
     }