Skip to content

Commit 0c18345

Browse files
committed
Support TinyVG format decoding and rendering
Implemented support for decoding TinyVG format by parsing commands step-by-step. Each decoded command is executed using twin's path functions to render graphics. This enhances twin's capability to handle compact vector graphics . Ref: https://tinyvg.tech/download/specification.pdf Close #71
1 parent 16f7f9e commit 0c18345

File tree

9 files changed

+1515
-0
lines changed

9 files changed

+1515
-0
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ ifeq ($(CONFIG_LOADER_GIF), y)
8686
libtwin.a_files-y += src/image-gif.c
8787
endif
8888

89+
ifeq ($(CONFIG_LOADER_GIF), y)
90+
libtwin.a_files-y += src/image-tvg.c
91+
endif
92+
8993
# Applications
9094

9195
libapps.a_files-y := apps/dummy.c
@@ -96,6 +100,7 @@ libapps.a_files-$(CONFIG_DEMO_CALCULATOR) += apps/calc.c
96100
libapps.a_files-$(CONFIG_DEMO_LINE) += apps/line.c
97101
libapps.a_files-$(CONFIG_DEMO_SPLINE) += apps/spline.c
98102
libapps.a_files-$(CONFIG_DEMO_ANIMATION) += apps/animation.c
103+
libapps.a_files-$(CONFIG_DEMO_IMAGE) += apps/image.c
99104

100105
libapps.a_includes-y := include
101106

apps/apps_image.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Twin - A Tiny Window System
3+
* Copyright (c) 2024 National Cheng Kung University
4+
* All rights reserved.
5+
*/
6+
7+
#ifndef _APPS_IMAGE_H_
8+
#define _APPS_IMAGE_H_
9+
10+
#include <twin.h>
11+
12+
void apps_image_start(twin_screen_t *screen,
13+
const char *name,
14+
const char *path,
15+
int x,
16+
int y);
17+
18+
#endif /* _APPS_ANIMATION_H_ */

apps/image.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Twin - A Tiny Window System
3+
* Copyright (c) 2024 National Cheng Kung University
4+
* All rights reserved.
5+
*/
6+
7+
#include <stdlib.h>
8+
9+
#include "twin_private.h"
10+
11+
#include "apps_image.h"
12+
13+
#define _apps_image_pixmap(image) ((image)->widget.window->pixmap)
14+
15+
typedef struct {
16+
twin_widget_t widget;
17+
twin_pixmap_t *pix;
18+
twin_timeout_t *timeout;
19+
} apps_image_t;
20+
21+
static void _apps_image_paint(apps_image_t *img)
22+
{
23+
twin_operand_t srcop = {
24+
.source_kind = TWIN_PIXMAP,
25+
.u.pixmap = img->pix,
26+
};
27+
twin_composite(_apps_image_pixmap(img), 0, 0, &srcop, 0, 0, NULL, 0, 0,
28+
TWIN_SOURCE, img->pix->width, img->pix->height);
29+
}
30+
31+
static twin_dispatch_result_t _apps_image_dispatch(twin_widget_t *widget,
32+
twin_event_t *event)
33+
{
34+
apps_image_t *img = (apps_image_t *) widget;
35+
if (_twin_widget_dispatch(widget, event) == TwinDispatchDone)
36+
return TwinDispatchDone;
37+
switch (event->kind) {
38+
case TwinEventPaint:
39+
_apps_image_paint(img);
40+
break;
41+
default:
42+
break;
43+
}
44+
return TwinDispatchContinue;
45+
}
46+
47+
static void _apps_image_init(apps_image_t *anim,
48+
twin_box_t *parent,
49+
twin_dispatch_proc_t dispatch)
50+
{
51+
static const twin_widget_layout_t preferred = {0, 0, 1, 1};
52+
_twin_widget_init(&anim->widget, parent, 0, preferred, dispatch);
53+
54+
anim->timeout = NULL;
55+
}
56+
57+
static apps_image_t *apps_image_create(twin_box_t *parent, twin_pixmap_t *pix)
58+
{
59+
apps_image_t *img = malloc(sizeof(apps_image_t));
60+
img->pix = pix;
61+
_apps_image_init(img, parent, _apps_image_dispatch);
62+
return img;
63+
}
64+
65+
void apps_image_start(twin_screen_t *screen,
66+
const char *name,
67+
const char *path,
68+
int x,
69+
int y)
70+
{
71+
twin_pixmap_t *pix = twin_pixmap_from_file(path, TWIN_ARGB32);
72+
if (!pix)
73+
return;
74+
twin_toplevel_t *toplevel =
75+
twin_toplevel_create(screen, TWIN_ARGB32, TwinWindowApplication, x, y,
76+
pix->width, pix->height, name);
77+
apps_image_t *img = apps_image_create(&toplevel->box, pix);
78+
(void) img;
79+
twin_toplevel_show(toplevel);
80+
}
81+

apps/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "apps_calc.h"
1818
#include "apps_clock.h"
1919
#include "apps_hello.h"
20+
#include "apps_image.h"
2021
#include "apps_line.h"
2122
#include "apps_multi.h"
2223
#include "apps_spline.h"
@@ -127,6 +128,9 @@ int main(void)
127128
apps_animation_start(tx->screen, "Viewer", ASSET_PATH "nyancat.gif", 20,
128129
20);
129130
#endif
131+
#if defined(CONFIG_DEMO_IMAGE)
132+
apps_image_start(tx->screen, "Viewer", ASSET_PATH "chart.tvg", 20, 20);
133+
#endif
130134

131135
twin_dispatch(tx);
132136

assets/chart.tvg

6.39 KB
Binary file not shown.

configs/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ config LOADER_GIF
7171
bool "Enable GIF loader"
7272
default y
7373

74+
config LOADER_TVG
75+
bool "Enable TVG loader"
76+
default y
77+
7478
endmenu
7579

7680
menu "Demo Applications"
@@ -113,4 +117,9 @@ config DEMO_ANIMATION
113117
bool "Build animation demo"
114118
default y
115119
depends on DEMO_APPLICATIONS
120+
121+
config DEMO_IMAGE
122+
bool "Build image demo"
123+
default y
124+
depends on DEMO_APPLICATIONS
116125
endmenu

0 commit comments

Comments
 (0)