annotate src/img_ldr.c @ 676:f264b50c469c

Bind subject of mouse_event to JS object of coord
author Thinker K.F. Li <thinker@branda.to>
date Fri, 06 Aug 2010 13:33:47 +0800
parents 818e870e1eb5
children 586e50f82c1f
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);
645
818e870e1eb5 Check if the path is absolute path. We use the default prefix only for relative path.
wycc
parents: 450
diff changeset
36 if (img_id[0] != '/')
818e870e1eb5 Check if the path is absolute path. We use the default prefix only for relative path.
wycc
parents: 450
diff changeset
37 strcpy(fname, sldr->repo);
818e870e1eb5 Check if the path is absolute path. We use the default prefix only for relative path.
wycc
parents: 450
diff changeset
38 else
818e870e1eb5 Check if the path is absolute path. We use the default prefix only for relative path.
wycc
parents: 450
diff changeset
39 fname[0] = 0;
257
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
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
42 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
43 if(surf == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 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
47 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
48 mbe_surface_destroy(surf);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 }
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
51 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
52 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
53 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
54 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
55 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
56 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
57 img->surf = surf;
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 return (mb_img_data_t *)img;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 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
64 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
65 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
66 free(img);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 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
71 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
72
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 free((void *)defldr->repo);
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
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 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
77 simple_mb_img_ldr_t *ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 int sz;
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 if(img_repository == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 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
84 if(ldr == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 return NULL;
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 /*
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 * Copy and formalize path of image repository.
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 */
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 sz = strlen(img_repository);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 ldr->repo = (const char *)malloc(sz + 2);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 if(ldr->repo == NULL) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 free(ldr);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 return NULL;
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 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
97 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
98 ((char *)ldr->repo)[sz] = '/';
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 ((char *)ldr->repo)[sz + 1] = 0;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 }
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 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
103 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
104
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 return (mb_img_ldr_t *)ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 }