Mercurial > MadButterfly
annotate src/img_ldr.c @ 365:6adb28388132
Add capability to add new scene and source into the project
Add Save button to save the project.
author | wycc |
---|---|
date | Sat, 14 Mar 2009 16:42:37 +0800 |
parents | 3e84458968ec |
children | 16116d84bc5e |
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> |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
3 #include <cairo.h> |
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; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
19 cairo_surface_t *surf; |
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; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
29 cairo_surface_t *surf; |
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 cairo_format_t fmt; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
32 int sz; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
33 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
34 sz = strlen(sldr->repo); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
35 sz += strlen(img_id); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
36 fname = (char *)malloc(sz + 2); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
37 strcpy(fname, sldr->repo); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
38 strcat(fname, img_id); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
39 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
40 surf = cairo_image_surface_create_from_png(fname); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
41 if(surf == NULL) |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
42 return NULL; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
43 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
44 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
|
45 if(img == NULL) { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
46 cairo_surface_destroy(surf); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
47 return NULL; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
48 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
49 img->img.content = cairo_image_surface_get_data(surf); |
268
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
50 img->img.w = cairo_image_surface_get_width(surf); |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
51 img->img.h = cairo_image_surface_get_height(surf); |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
52 img->img.stride = cairo_image_surface_get_stride(surf); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
53 fmt = cairo_image_surface_get_format(surf); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
54 switch(fmt) { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
55 case CAIRO_FORMAT_ARGB32: |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
56 img->img.fmt = MB_IFMT_ARGB32; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
57 break; |
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 case CAIRO_FORMAT_RGB24: |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
60 img->img.fmt = MB_IFMT_RGB24; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
61 break; |
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 case CAIRO_FORMAT_A8: |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
64 img->img.fmt = MB_IFMT_A8; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
65 break; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
66 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
67 case CAIRO_FORMAT_A1: |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
68 img->img.fmt = MB_IFMT_A1; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
69 break; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
70 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
71 default: |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
72 cairo_surface_destroy(surf); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
73 free(img); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
74 return NULL; |
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 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
|
77 img->surf = surf; |
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 return (mb_img_data_t *)img; |
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 static |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
83 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
|
84 simple_mb_img_data_t *simg = (simple_mb_img_data_t *)img; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
85 cairo_surface_destroy((cairo_surface_t *)simg->surf); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
86 free(img); |
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 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
89 static |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
90 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
|
91 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
|
92 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
93 free((void *)defldr->repo); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
94 } |
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 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
|
97 simple_mb_img_ldr_t *ldr; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
98 int sz; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
99 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
100 if(img_repository == NULL) |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
101 return NULL; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
102 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
103 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
|
104 if(ldr == NULL) |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
105 return NULL; |
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 * Copy and formalize path of image repository. |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
109 */ |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
110 sz = strlen(img_repository); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
111 ldr->repo = (const char *)malloc(sz + 2); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
112 if(ldr->repo == NULL) { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
113 free(ldr); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
114 return NULL; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
115 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
116 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
|
117 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
|
118 ((char *)ldr->repo)[sz] = '/'; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
119 ((char *)ldr->repo)[sz + 1] = 0; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
120 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
121 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
122 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
|
123 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
|
124 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
125 return (mb_img_ldr_t *)ldr; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
126 } |