Mercurial > MadButterfly
annotate src/img_ldr.c @ 615:0f0abc48e10a openvg
mbe_new_path() for OpenVG
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Thu, 08 Jul 2010 11:29:58 +0800 |
parents | 8863d23cea4b |
children | 7b4e80ab671a |
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> |
601
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
3 #include <Imlib2.h> |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
356
diff
changeset
|
4 #include "mb_graph_engine.h" |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
5 #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
|
6 #include "mb_paint.h" |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
7 #include "mb_img_ldr.h" |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
8 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
9 /*! \brief Simple image loader. |
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 */ |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
12 struct _simple_mb_img_ldr { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 mb_img_ldr_t ldr; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
14 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
|
15 }; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
16 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
|
17 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
18 struct _simple_mb_img_data { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
19 mb_img_data_t img; |
601
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
20 Imlib_Image img_hdl; |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
21 }; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
22 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
|
23 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
24 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
|
25 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
26 static |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
27 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
|
28 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
|
29 simple_mb_img_data_t *img; |
601
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
30 Imlib_Image img_hdl; |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
31 int w, h; |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
32 void *data; |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
33 char *fname; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
34 int sz; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
35 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
36 sz = strlen(sldr->repo); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
37 sz += strlen(img_id); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
38 fname = (char *)malloc(sz + 2); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
39 strcpy(fname, sldr->repo); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
40 strcat(fname, img_id); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
41 |
601
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
42 img_hdl = imlib_load_image(fname); |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
43 if(!img_hdl) |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
44 return NULL; |
601
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
45 imlib_context_set_image(img_hdl); |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
46 w = imlib_image_get_width(); |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
47 h = imlib_image_get_height(); |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
48 data = imlib_image_get_data_for_reading_only(); |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
49 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
50 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
|
51 if(img == NULL) { |
601
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
52 imlib_free_image(); |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
53 return NULL; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
54 } |
601
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
55 img->img.content = data; |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
56 img->img.w = w; |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
57 img->img.h = h; |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
58 img->img.stride = w * 4; |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
59 img->img.fmt = MB_IFMT_ARGB32; |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
60 img->img.free = simple_mb_img_ldr_img_free; |
601
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
61 img->img_hdl = img_hdl; |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
62 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
63 return (mb_img_data_t *)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_img_free(mb_img_data_t *img) { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
68 simple_mb_img_data_t *simg = (simple_mb_img_data_t *)img; |
601
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
69 |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
70 imlib_context_set_image(simg->img_hdl); |
8863d23cea4b
Load images with Imlib2.
Thinker K.F. Li <thinker@branda.to>
parents:
450
diff
changeset
|
71 imlib_free_image(); |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
72 free(img); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
73 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
74 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
75 static |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
76 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
|
77 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
|
78 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
79 free((void *)defldr->repo); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
80 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
81 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
82 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
|
83 simple_mb_img_ldr_t *ldr; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
84 int sz; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
85 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
86 if(img_repository == NULL) |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
87 return NULL; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
88 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
89 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
|
90 if(ldr == NULL) |
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 /* |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
94 * Copy and formalize path of image repository. |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
95 */ |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
96 sz = strlen(img_repository); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
97 ldr->repo = (const char *)malloc(sz + 2); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
98 if(ldr->repo == NULL) { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
99 free(ldr); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
100 return NULL; |
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 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
|
103 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
|
104 ((char *)ldr->repo)[sz] = '/'; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
105 ((char *)ldr->repo)[sz + 1] = 0; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
106 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
107 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
108 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
|
109 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
|
110 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
111 return (mb_img_ldr_t *)ldr; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
112 } |