comparison src/shape_image.c @ 356:3e84458968ec

Move mb_img_data_t out from argument list of rdman_shape_image_new(). - Application should specify content of sh_image_t by fill the shape with a paint_image_t.
author Thinker K.F. Li <thinker@branda.to>
date Mon, 09 Mar 2009 23:05:57 +0800
parents 04d22dc38bc0
children 16116d84bc5e
comparison
equal deleted inserted replaced
355:958b67d911db 356:3e84458968ec
8 8
9 /*! \page sh_image_n_image_ldr Image and Image Loader 9 /*! \page sh_image_n_image_ldr Image and Image Loader
10 * 10 *
11 * Image (\ref sh_image_t) is a shape to show an image on the output 11 * Image (\ref sh_image_t) is a shape to show an image on the output
12 * device. Programmers manipulate object of an image shape to show it 12 * device. Programmers manipulate object of an image shape to show it
13 * at specified position with specified size. To create a new instance 13 * at specified position with specified size. For a sh_image_t, an
14 * of sh_iamge_t, an image should be specified. Programmers must have 14 * image should be specified to fill the shape. Programmers must have
15 * a way to load image from files. The solution proposed by MadButterfly 15 * a way to load image from files. The solution proposed by MadButterfly
16 * is image loader (\ref mb_img_ldr_t). 16 * is image loader (\ref mb_img_ldr_t).
17 * 17 *
18 * Image loader is a repository of image files, programmers give him an 18 * Image loader is a repository of image files, programmers give him an
19 * ID and get an image returned the loader. Image loader decodes image 19 * ID and get an image returned the loader. Image loader decodes image
56 56
57 co_aix x, y; 57 co_aix x, y;
58 co_aix w, h; 58 co_aix w, h;
59 co_aix poses[4][2]; 59 co_aix poses[4][2];
60 60
61 mb_img_data_t *img_data;
62 paint_t *paint;
63 redraw_man_t *rdman; 61 redraw_man_t *rdman;
64 } sh_image_t; 62 } sh_image_t;
65 63
66 static void sh_image_free(shape_t *shape); 64 static void sh_image_free(shape_t *shape);
67 65
68 int _sh_image_set_img_data(shape_t *shape, mb_img_data_t *img_data,
69 co_aix x, co_aix y, co_aix w, co_aix h) {
70 sh_image_t *img = (sh_image_t *)shape;
71 paint_t *paint;
72
73 ASSERT(img_data != NULL);
74 ASSERT(shape->obj.obj_type == MBO_IMAGE);
75
76 paint = rdman_paint_image_new(img->rdman, img_data);
77 if(paint == NULL)
78 return ERR;
79
80 if(img->paint)
81 rdman_paint_free(img->rdman, img->paint);
82
83 img->img_data = img_data;
84 img->x = x;
85 img->y = y;
86 img->w = w;
87 img->h = h;
88 img->paint = paint;
89 rdman_paint_fill(img->rdman, paint, (shape_t *)img);
90
91 return OK;
92 }
93
94 /*! \brief Creae a new image shape. 66 /*! \brief Creae a new image shape.
95 * 67 *
96 * \param img_data is image data whose owner-ship is transfered. 68 * \param img_data is image data whose owner-ship is transfered.
97 */ 69 */
98 shape_t *rdman_shape_image_new(redraw_man_t *rdman, mb_img_data_t *img_data, 70 shape_t *rdman_shape_image_new(redraw_man_t *rdman,
99 co_aix x, co_aix y, co_aix w, co_aix h) { 71 co_aix x, co_aix y, co_aix w, co_aix h) {
100 sh_image_t *img; 72 sh_image_t *img;
101 cairo_format_t fmt; 73 cairo_format_t fmt;
102 paint_t *paint;
103 int r; 74 int r;
104 75
105 img = O_ALLOC(sh_image_t); 76 img = O_ALLOC(sh_image_t);
106 if(img == NULL) 77 if(img == NULL)
107 return NULL; 78 return NULL;
108 79
109 memset(img, 0, sizeof(sh_image_t)); 80 memset(img, 0, sizeof(sh_image_t));
110 mb_obj_init((mb_obj_t *)img, MBO_IMAGE); 81 mb_obj_init((mb_obj_t *)img, MBO_IMAGE);
111 img->rdman = rdman; 82 img->rdman = rdman;
112 img->shape.free = sh_image_free; 83 img->shape.free = sh_image_free;
84
85 img->x = x;
86 img->y = y;
87 img->w = w;
88 img->h = h;
113 89
114 r = _sh_image_set_img_data((shape_t *)img, img_data, x, y, w, h);
115 if(r != OK) {
116 mb_obj_destroy((shape_t *)img);
117 free(img);
118 return NULL;
119 }
120
121 return (shape_t *)img; 90 return (shape_t *)img;
122 } 91 }
123 92
124 void sh_image_free(shape_t *shape) { 93 void sh_image_free(shape_t *shape) {
125 sh_image_t *img = (sh_image_t *)shape; 94 sh_image_t *img = (sh_image_t *)shape;
126 95
127 rdman_paint_free(img->rdman, img->paint);
128 mb_obj_destroy(shape); 96 mb_obj_destroy(shape);
129 free(img); 97 free(img);
130 } 98 }
131 99
132 void sh_image_transform(shape_t *shape) { 100 void sh_image_transform(shape_t *shape) {
133 sh_image_t *img = (sh_image_t *)shape; 101 sh_image_t *img = (sh_image_t *)shape;
134 mb_img_data_t *img_data; 102 paint_t *paint;
135 co_aix (*poses)[2]; 103 co_aix (*poses)[2];
136 co_aix img_matrix[6]; 104 co_aix img_matrix[6];
137 co_aix x_factor, y_factor; 105 co_aix x_factor, y_factor;
106 int img_w, img_h;
138 cairo_matrix_t cmatrix; 107 cairo_matrix_t cmatrix;
139 int i; 108 int i;
140
141 img_data = img->img_data;
142 109
143 poses = img->poses; 110 poses = img->poses;
144 poses[0][0] = img->x; 111 poses[0][0] = img->x;
145 poses[0][1] = img->y; 112 poses[0][1] = img->y;
146 poses[1][0] = img->x + img->w; 113 poses[1][0] = img->x + img->w;
149 poses[2][1] = img->y + img->h; 116 poses[2][1] = img->y + img->h;
150 poses[3][0] = img->x; 117 poses[3][0] = img->x;
151 poses[3][1] = img->y + img->h; 118 poses[3][1] = img->y + img->h;
152 for(i = 0; i < 4; i++) 119 for(i = 0; i < 4; i++)
153 coord_trans_pos(img->shape.coord, &poses[i][0], &poses[i][1]); 120 coord_trans_pos(img->shape.coord, &poses[i][0], &poses[i][1]);
121
122 geo_from_positions(sh_get_geo(shape), 4, poses);
154 123
124 paint = sh_get_fill(shape);
125 if(paint == NULL)
126 return;
127
128 ASSERT(paint.pnt_type == MBP_IMAGE);
129
130 paint_image_get_size(paint, &img_w, &img_h);
131
155 /* Transformation from user space to image space */ 132 /* Transformation from user space to image space */
156 img_matrix[0] = (poses[1][0] - poses[0][0]) / img->w; 133 img_matrix[0] = (poses[1][0] - poses[0][0]) / img->w;
157 img_matrix[1] = (poses[1][1] - poses[0][1]) / img->w; 134 img_matrix[1] = (poses[1][1] - poses[0][1]) / img->w;
158 img_matrix[2] = -poses[0][0]; 135 img_matrix[2] = -poses[0][0];
159 img_matrix[3] = (poses[3][0] - poses[0][0]) / img->h; 136 img_matrix[3] = (poses[3][0] - poses[0][0]) / img->h;
160 img_matrix[4] = (poses[3][1] - poses[0][1]) / img->h; 137 img_matrix[4] = (poses[3][1] - poses[0][1]) / img->h;
161 img_matrix[5] = -poses[0][1]; 138 img_matrix[5] = -poses[0][1];
162 if(img->w != img_data->w || 139 if(img->w != img_w ||
163 img->h != img_data->h) { 140 img->h != img_h) {
164 /* Resize image */ 141 /* Resize image */
165 x_factor = img_data->w / img->w; 142 x_factor = img_w / img->w;
166 img_matrix[0] *= x_factor; 143 img_matrix[0] *= x_factor;
167 img_matrix[1] *= x_factor; 144 img_matrix[1] *= x_factor;
168 img_matrix[2] *= x_factor; 145 img_matrix[2] *= x_factor;
169 y_factor = img_data->h / img->h; 146 y_factor = img_h / img->h;
170 img_matrix[3] *= y_factor; 147 img_matrix[3] *= y_factor;
171 img_matrix[4] *= y_factor; 148 img_matrix[4] *= y_factor;
172 img_matrix[5] *= y_factor; 149 img_matrix[5] *= y_factor;
173 } 150 }
174 paint_image_set_matrix(sh_get_fill(shape), img_matrix); 151 paint_image_set_matrix(sh_get_fill(shape), img_matrix);
175
176 geo_from_positions(sh_get_geo(shape), 4, poses);
177 } 152 }
178 153
179 /*! \brief Draw image for an image shape. 154 /*! \brief Draw image for an image shape.
180 * 155 *
181 * \note Image is not rescaled for size of the shape. 156 * \note Image is not rescaled for size of the shape.
204 img->x = x; 179 img->x = x;
205 img->y = y; 180 img->y = y;
206 img->w = w; 181 img->w = w;
207 img->h = h; 182 img->h = h;
208 } 183 }
209
210 int sh_image_set_img_data(shape_t *shape, mb_img_data_t *img_data) {
211 int r;
212 sh_image_t *img = (sh_image_t *)shape;
213
214 r = _sh_image_set_img_data(shape, img_data,
215 img->x, img->y, img->w, img->h);
216 return r;
217 }
218
219 mb_img_data_t *sh_image_get_img_data(shape_t *shape) {
220 sh_image_t *img = (sh_image_t *)shape;
221
222 ASSERT(shape->obj.obj_type == MBO_IMAGE);
223
224 return img->img_data;
225 }