annotate src/img_ldr.c @ 593:ac942664fe86 openvg

stroke and fill for VG
author Thinker K.F. Li <thinker@branda.to>
date Wed, 30 Jun 2010 22:57:23 +0800
parents a417fd980228
children 8863d23cea4b 818e870e1eb5
rev   line source
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <string.h>
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
3 #include "mb_graph_engine.h"
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include "mb_tools.h"
356
3e84458968ec Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents: 346
diff changeset
5 #include "mb_paint.h"
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 #include "mb_img_ldr.h"
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 /*! \brief Simple image loader.
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 *
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 */
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 struct _simple_mb_img_ldr {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 mb_img_ldr_t ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 const char *repo; /*!< \brief The directory of repository. */
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 };
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 typedef struct _simple_mb_img_ldr simple_mb_img_ldr_t;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 struct _simple_mb_img_data {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 mb_img_data_t img;
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
19 mbe_surface_t *surf;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 };
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 typedef struct _simple_mb_img_data simple_mb_img_data_t;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 static void simple_mb_img_ldr_img_free(mb_img_data_t *img);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 mb_img_data_t *simple_mb_img_ldr_load(mb_img_ldr_t *ldr, const char *img_id) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 simple_mb_img_ldr_t *sldr = (simple_mb_img_ldr_t *)ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 simple_mb_img_data_t *img;
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
29 mbe_surface_t *surf;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 char *fname;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 int sz;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 sz = strlen(sldr->repo);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 sz += strlen(img_id);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 fname = (char *)malloc(sz + 2);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 strcpy(fname, sldr->repo);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 strcat(fname, img_id);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
39 surf = mbe_image_surface_create_from_png(fname);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 if(surf == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 img = O_ALLOC(simple_mb_img_data_t);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 if(img == NULL) {
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
45 mbe_surface_destroy(surf);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 }
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
48 img->img.content = mbe_image_surface_get_data(surf);
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
49 img->img.w = mbe_image_surface_get_width(surf);
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
50 img->img.h = mbe_image_surface_get_height(surf);
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
51 img->img.stride = mbe_image_surface_get_stride(surf);
450
a417fd980228 Replace cairo_format_t with mb_img_fmt_t.
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
52 img->img.fmt = mbe_image_surface_get_format(surf);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 img->img.free = simple_mb_img_ldr_img_free;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 img->surf = surf;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 return (mb_img_data_t *)img;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 void simple_mb_img_ldr_img_free(mb_img_data_t *img) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 simple_mb_img_data_t *simg = (simple_mb_img_data_t *)img;
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
62 mbe_surface_destroy((mbe_surface_t *)simg->surf);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 free(img);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 void simple_mb_img_ldr_free(mb_img_ldr_t *ldr) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 simple_mb_img_ldr_t *defldr = (simple_mb_img_ldr_t *)ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 free((void *)defldr->repo);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 mb_img_ldr_t *simple_mb_img_ldr_new(const char *img_repository) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 simple_mb_img_ldr_t *ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 int sz;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 if(img_repository == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 ldr = O_ALLOC(simple_mb_img_ldr_t);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 if(ldr == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 /*
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 * Copy and formalize path of image repository.
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 */
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 sz = strlen(img_repository);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 ldr->repo = (const char *)malloc(sz + 2);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 if(ldr->repo == NULL) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 free(ldr);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 strcpy((char *)ldr->repo, img_repository);
346
b391722bf20e sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents: 268
diff changeset
94 if(img_repository[sz - 1] != '/' && strlen(img_repository) != 0) {
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 ((char *)ldr->repo)[sz] = '/';
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 ((char *)ldr->repo)[sz + 1] = 0;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 ldr->ldr.load = simple_mb_img_ldr_load;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 ldr->ldr.free = simple_mb_img_ldr_free;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 return (mb_img_ldr_t *)ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 }