comparison src/shape_image.c @ 260:29acbd8a0dd0

Integrate sh_image with svg2code.py. diff -r e8a784a306d0 examples/svg2code_ex/dsc_3241.png Binary file examples/svg2code_ex/dsc_3241.png has changed diff -r e8a784a306d0 examples/svg2code_ex/dsc_3241.png Binary file examples/svg2code_ex/dsc_3241.png has changed
author Thinker K.F. Li <thinker@branda.to>
date Fri, 23 Jan 2009 23:00:23 +0800
parents 50d253d0fcba
children b42ee279669e
comparison
equal deleted inserted replaced
259:e8a784a306d0 260:29acbd8a0dd0
13 typedef struct _sh_image { 13 typedef struct _sh_image {
14 shape_t shape; 14 shape_t shape;
15 15
16 co_aix x, y; 16 co_aix x, y;
17 co_aix w, h; 17 co_aix w, h;
18 co_aix poses[4][2];
18 19
19 mb_img_data_t *img_data; 20 mb_img_data_t *img_data;
20 cairo_surface_t *surf; 21 paint_t *paint;
22 redraw_man_t *rdman;
21 } sh_image_t; 23 } sh_image_t;
22 24
23 static void sh_image_free(shape_t *shape); 25 static void sh_image_free(shape_t *shape);
24 26
25 /*! \brief Creae a new image shape. 27 /*! \brief Creae a new image shape.
28 */ 30 */
29 shape_t *rdman_shape_image_new(redraw_man_t *rdman, mb_img_data_t *img_data, 31 shape_t *rdman_shape_image_new(redraw_man_t *rdman, mb_img_data_t *img_data,
30 co_aix x, co_aix y, co_aix w, co_aix h) { 32 co_aix x, co_aix y, co_aix w, co_aix h) {
31 sh_image_t *img; 33 sh_image_t *img;
32 cairo_format_t fmt; 34 cairo_format_t fmt;
35 paint_t *paint;
33 36
34 img = O_ALLOC(sh_image_t); 37 img = O_ALLOC(sh_image_t);
35 if(img == NULL) 38 if(img == NULL)
36 return NULL; 39 return NULL;
37 40
43 img->y = y; 46 img->y = y;
44 img->w = w; 47 img->w = w;
45 img->h = h; 48 img->h = h;
46 img->img_data = img_data; 49 img->img_data = img_data;
47 50
48 switch(img_data->fmt) { 51 paint = rdman_paint_image_new(rdman, img_data);
49 case MB_IFMT_ARGB32: 52 rdman_paint_fill(rdman, paint, (shape_t *)img);
50 fmt = CAIRO_FORMAT_ARGB32; 53 img->paint = paint;
51 break; 54 img->rdman = rdman;
52 55
53 case MB_IFMT_RGB24:
54 fmt = CAIRO_FORMAT_RGB24;
55 break;
56
57 case MB_IFMT_A8:
58 fmt = CAIRO_FORMAT_A8;
59 break;
60
61 case MB_IFMT_A1:
62 fmt = CAIRO_FORMAT_A1;
63 break;
64
65 case MB_IFMT_RGB16_565:
66 fmt = CAIRO_FORMAT_RGB16_565;
67 break;
68
69 default:
70 mb_obj_destroy(img);
71 free(img);
72 return NULL;
73 }
74
75 img->surf = cairo_image_surface_create_for_data(img_data->content,
76 fmt,
77 img_data->width,
78 img_data->height,
79 img_data->stride);
80 if(img->surf == NULL) {
81 mb_obj_destroy(img);
82 free(img);
83 return NULL;
84 }
85
86 return (shape_t *)img; 56 return (shape_t *)img;
87 } 57 }
88 58
89 void sh_image_free(shape_t *shape) { 59 void sh_image_free(shape_t *shape) {
90 sh_image_t *img = (sh_image_t *)shape; 60 sh_image_t *img = (sh_image_t *)shape;
91 61
62 rdman_paint_free(img->rdman, img->paint);
92 mb_obj_destroy(shape); 63 mb_obj_destroy(shape);
93 MB_IMG_DATA_FREE(img->img_data); 64 MB_IMG_DATA_FREE(img->img_data);
94 cairo_surface_destroy(img->surf);
95 free(img); 65 free(img);
96 } 66 }
97 67
98 void sh_image_transform(shape_t *shape) { 68 void sh_image_transform(shape_t *shape) {
99 sh_image_t *img = (sh_image_t *)shape; 69 sh_image_t *img = (sh_image_t *)shape;
100 co_aix poses[4][2]; 70 co_aix (*poses)[2];
71 co_aix img_matrix[6];
72 cairo_matrix_t cmatrix;
101 int i; 73 int i;
102 74
75 poses = img->poses;
103 poses[0][0] = img->x; 76 poses[0][0] = img->x;
104 poses[0][1] = img->y; 77 poses[0][1] = img->y;
105 poses[1][0] = img->x + img->w; 78 poses[1][0] = img->x + img->w;
106 poses[1][1] = img->y; 79 poses[1][1] = img->y;
107 poses[2][0] = img->x + img->w; 80 poses[2][0] = img->x + img->w;
108 poses[2][1] = img->y + img->h; 81 poses[2][1] = img->y + img->h;
109 poses[3][0] = img->x; 82 poses[3][0] = img->x;
110 poses[3][1] = img->y + img->h; 83 poses[3][1] = img->y + img->h;
111 for(i = 0; i < 4; i++) 84 for(i = 0; i < 4; i++)
112 coord_trans_pos(img->shape.coord, &poses[i][0], &poses[i][1]); 85 coord_trans_pos(img->shape.coord, &poses[i][0], &poses[i][1]);
86
87 img_matrix[0] = (poses[1][0] - poses[0][0]) / img->w;
88 img_matrix[1] = (poses[1][1] - poses[0][1]) / img->w;
89 img_matrix[2] = -poses[0][0];
90 img_matrix[3] = (poses[3][0] - poses[0][0]) / img->h;
91 img_matrix[4] = (poses[3][1] - poses[0][1]) / img->h;
92 img_matrix[5] = -poses[0][1];
93 paint_image_set_matrix(sh_get_fill(shape), img_matrix);
113 94
114 geo_from_positions(sh_get_geo(shape), 4, poses); 95 geo_from_positions(sh_get_geo(shape), 4, poses);
115 } 96 }
116 97
117 /*! \brief Draw image for an image shape. 98 /*! \brief Draw image for an image shape.
122 sh_image_t *img = (sh_image_t *)shape; 103 sh_image_t *img = (sh_image_t *)shape;
123 cairo_pattern_t *saved_source; 104 cairo_pattern_t *saved_source;
124 cairo_matrix_t matrix, saved_matrix; 105 cairo_matrix_t matrix, saved_matrix;
125 co_aix *aggr; 106 co_aix *aggr;
126 107
127 aggr = coord_get_aggr_matrix(sh_get_coord(shape)); 108 cairo_move_to(cr, img->poses[0][0], img->poses[0][1]);
128 cairo_matrix_init(&matrix, 109 cairo_line_to(cr, img->poses[1][0], img->poses[1][1]);
129 aggr[0], aggr[3], 110 cairo_line_to(cr, img->poses[2][0], img->poses[2][1]);
130 aggr[1], aggr[4], 111 cairo_line_to(cr, img->poses[3][0], img->poses[3][1]);
131 aggr[2], aggr[5]); 112 cairo_close_path(cr);
132
133 /* set matrix */
134 cairo_get_matrix(cr, &saved_matrix);
135 cairo_set_matrix(cr, &matrix);
136
137 /* set source */
138 saved_source = cairo_get_source(cr);
139 cairo_pattern_reference(saved_source);
140
141 /* draw image */
142 cairo_set_source_surface(cr, img->surf, 0, 0);
143 cairo_paint(cr);
144
145 /* restore source */
146 cairo_set_source(cr, saved_source);
147 cairo_pattern_destroy(saved_source);
148
149 /* restore matrix */
150 cairo_set_matrix(cr, &saved_matrix);
151 } 113 }
152 114
153 void sh_image_set(shape_t *shape, co_aix x, co_aix y, 115 void sh_image_set(shape_t *shape, co_aix x, co_aix y,
154 co_aix w, co_aix h) { 116 co_aix w, co_aix h) {
155 sh_image_t *img = (sh_image_t *)shape; 117 sh_image_t *img = (sh_image_t *)shape;