Skip to content

Commit

Permalink
Support TinyVG format decoding and rendering
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ndsl7109256 committed Dec 5, 2024
1 parent 16f7f9e commit 8d61959
Show file tree
Hide file tree
Showing 9 changed files with 1,514 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ ifeq ($(CONFIG_LOADER_GIF), y)
libtwin.a_files-y += src/image-gif.c
endif

ifeq ($(CONFIG_LOADER_TVG), y)
libtwin.a_files-y += src/image-tvg.c
endif

# Applications

libapps.a_files-y := apps/dummy.c
Expand All @@ -96,6 +100,7 @@ libapps.a_files-$(CONFIG_DEMO_CALCULATOR) += apps/calc.c
libapps.a_files-$(CONFIG_DEMO_LINE) += apps/line.c
libapps.a_files-$(CONFIG_DEMO_SPLINE) += apps/spline.c
libapps.a_files-$(CONFIG_DEMO_ANIMATION) += apps/animation.c
libapps.a_files-$(CONFIG_DEMO_IMAGE) += apps/image.c

libapps.a_includes-y := include

Expand Down
18 changes: 18 additions & 0 deletions apps/apps_image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Twin - A Tiny Window System
* Copyright (c) 2024 National Cheng Kung University
* All rights reserved.
*/

#ifndef _APPS_IMAGE_H_
#define _APPS_IMAGE_H_

#include <twin.h>

void apps_image_start(twin_screen_t *screen,
const char *name,
const char *path,
int x,
int y);

#endif /* _APPS_ANIMATION_H_ */
80 changes: 80 additions & 0 deletions apps/image.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Twin - A Tiny Window System
* Copyright (c) 2024 National Cheng Kung University
* All rights reserved.
*/

#include <stdlib.h>

#include "twin_private.h"

#include "apps_image.h"

#define _apps_image_pixmap(image) ((image)->widget.window->pixmap)

typedef struct {
twin_widget_t widget;
twin_pixmap_t *pix;
twin_timeout_t *timeout;
} apps_image_t;

static void _apps_image_paint(apps_image_t *img)
{
twin_operand_t srcop = {
.source_kind = TWIN_PIXMAP,
.u.pixmap = img->pix,
};
twin_composite(_apps_image_pixmap(img), 0, 0, &srcop, 0, 0, NULL, 0, 0,
TWIN_SOURCE, img->pix->width, img->pix->height);
}

static twin_dispatch_result_t _apps_image_dispatch(twin_widget_t *widget,
twin_event_t *event)
{
apps_image_t *img = (apps_image_t *) widget;
if (_twin_widget_dispatch(widget, event) == TwinDispatchDone)
return TwinDispatchDone;
switch (event->kind) {
case TwinEventPaint:
_apps_image_paint(img);
break;
default:
break;
}
return TwinDispatchContinue;
}

static void _apps_image_init(apps_image_t *anim,
twin_box_t *parent,
twin_dispatch_proc_t dispatch)
{
static const twin_widget_layout_t preferred = {0, 0, 1, 1};
_twin_widget_init(&anim->widget, parent, 0, preferred, dispatch);

anim->timeout = NULL;
}

static apps_image_t *apps_image_create(twin_box_t *parent, twin_pixmap_t *pix)
{
apps_image_t *img = malloc(sizeof(apps_image_t));
img->pix = pix;
_apps_image_init(img, parent, _apps_image_dispatch);
return img;
}

void apps_image_start(twin_screen_t *screen,
const char *name,
const char *path,
int x,
int y)
{
twin_pixmap_t *pix = twin_pixmap_from_file(path, TWIN_ARGB32);
if (!pix)
return;
twin_toplevel_t *toplevel =
twin_toplevel_create(screen, TWIN_ARGB32, TwinWindowApplication, x, y,
pix->width, pix->height, name);
apps_image_t *img = apps_image_create(&toplevel->box, pix);
(void) img;
twin_toplevel_show(toplevel);
}
4 changes: 4 additions & 0 deletions apps/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "apps_calc.h"
#include "apps_clock.h"
#include "apps_hello.h"
#include "apps_image.h"
#include "apps_line.h"
#include "apps_multi.h"
#include "apps_spline.h"
Expand Down Expand Up @@ -127,6 +128,9 @@ int main(void)
apps_animation_start(tx->screen, "Viewer", ASSET_PATH "nyancat.gif", 20,
20);
#endif
#if defined(CONFIG_DEMO_IMAGE)
apps_image_start(tx->screen, "Viewer", ASSET_PATH "chart.tvg", 20, 20);
#endif

twin_dispatch(tx);

Expand Down
Binary file added assets/chart.tvg
Binary file not shown.
9 changes: 9 additions & 0 deletions configs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ config LOADER_GIF
bool "Enable GIF loader"
default y

config LOADER_TVG
bool "Enable TVG loader"
default y

endmenu

menu "Demo Applications"
Expand Down Expand Up @@ -113,4 +117,9 @@ config DEMO_ANIMATION
bool "Build animation demo"
default y
depends on DEMO_APPLICATIONS

config DEMO_IMAGE
bool "Build image demo"
default y
depends on DEMO_APPLICATIONS
endmenu
Loading

0 comments on commit 8d61959

Please sign in to comment.