tvg_format: optimize data format
authorHermet Park <chuneon.park@samsung.com>
Thu, 29 Jul 2021 11:25:49 +0000 (20:25 +0900)
committerJunsuChoi <jsuya.choi@samsung.com>
Mon, 2 Aug 2021 03:25:14 +0000 (12:25 +0900)
This reduces tvg binary format size by converting PathCommand to more compact size.

This optimization increase +12% compress rate with our example:

195,668 => 174,071 (sum of all converted tvgs from svgs)

@Issues: https://github.com/Samsung/thorvg/issues/639

src/examples/images/test.tvg
src/loaders/tvg/tvgTvgLoadParser.cpp
src/savers/tvg/tvgTvgSaver.cpp

index 80e5576..5a61851 100644 (file)
Binary files a/src/examples/images/test.tvg and b/src/examples/images/test.tvg differ
index ee2540d..7979fb2 100644 (file)
@@ -176,21 +176,30 @@ static bool _parseScene(TvgBinBlock block, Paint *paint)
 
 static bool _parseShapePath(const char *ptr, const char *end, Shape *shape)
 {
-    //Shape Path
     uint32_t cmdCnt, ptsCnt;
+
     READ_UI32(&cmdCnt, ptr);
-    ptr += SIZE(uint32_t);
+    ptr += SIZE(cmdCnt);
+
     READ_UI32(&ptsCnt, ptr);
-    ptr += SIZE(uint32_t);
+    ptr += SIZE(ptsCnt);
 
-    const PathCommand* cmds = (PathCommand*) ptr;
-    ptr += SIZE(PathCommand) * cmdCnt;
-    const Point* pts = (Point*) ptr;
+    auto cmds = (TvgBinFlag*) ptr;
+    ptr += SIZE(TvgBinFlag) * cmdCnt;
+
+    auto pts = (Point*) ptr;
     ptr += SIZE(Point) * ptsCnt;
 
     if (ptr > end) return false;
 
-    shape->appendPath(cmds, cmdCnt, pts, ptsCnt);
+    /* Recover to PathCommand(4 bytes) from TvgBinFlag(1 byte) */
+    PathCommand inCmds[cmdCnt];
+    for (uint32_t i = 0; i < cmdCnt; ++i) {
+        inCmds[i] = static_cast<PathCommand>(cmds[i]);
+    }
+
+    shape->appendPath(inCmds, cmdCnt, pts, ptsCnt);
+
     return true;
 }
 
index 5a8ed42..131d9b5 100644 (file)
@@ -273,9 +273,16 @@ TvgBinCounter TvgSaver::serializePath(const Shape* shape)
     writeTag(TVG_TAG_SHAPE_PATH);
     reserveCount();
 
+    /* Reduce the binary size.
+       Convert PathCommand(4 bytes) to TvgBinFlag(1 byte) */
+    TvgBinFlag outCmds[cmdCnt];
+    for (uint32_t i = 0; i < cmdCnt; ++i) {
+        outCmds[i] = static_cast<TvgBinFlag>(cmds[i]);
+    }
+
     auto cnt = writeData(&cmdCnt, SIZE(cmdCnt));
     cnt += writeData(&ptsCnt, SIZE(ptsCnt));
-    cnt += writeData(cmds, cmdCnt * SIZE(cmds[0]));
+    cnt += writeData(outCmds, SIZE(outCmds));
     cnt += writeData(pts, ptsCnt * SIZE(pts[0]));
 
     writeReservedCount(cnt);