tvg_format: optimize data format

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
This commit is contained in:
Hermet Park 2021-07-29 20:25:49 +09:00 committed by Hermet Park
parent a91bd37ea9
commit e59ba67b7c
3 changed files with 26 additions and 10 deletions

Binary file not shown.

View 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);
READ_UI32(&ptsCnt, ptr);
ptr += SIZE(uint32_t);
const PathCommand* cmds = (PathCommand*) ptr;
ptr += SIZE(PathCommand) * cmdCnt;
const Point* pts = (Point*) ptr;
READ_UI32(&cmdCnt, ptr);
ptr += SIZE(cmdCnt);
READ_UI32(&ptsCnt, ptr);
ptr += SIZE(ptsCnt);
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;
}

View 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);