annotate src/shape_image.c @ 272:01439f28d0bd

Check-in primitive integrated madbuilder code.
author wycc
date Mon, 26 Jan 2009 01:36:04 +0800
parents 29acbd8a0dd0
children b42ee279669e
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_types.h"
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include "mb_shapes.h"
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 #include "mb_tools.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 #define ASSERT(x)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 #define OK 0
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 #define ERR -1
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 typedef struct _sh_image {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 shape_t shape;
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 co_aix x, y;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 co_aix w, h;
260
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
18 co_aix poses[4][2];
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 mb_img_data_t *img_data;
260
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
21 paint_t *paint;
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
22 redraw_man_t *rdman;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 } sh_image_t;
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 void sh_image_free(shape_t *shape);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 /*! \brief Creae a new image shape.
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 *
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 * \param img_data is image data whose owner-ship is transfered.
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 */
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 shape_t *rdman_shape_image_new(redraw_man_t *rdman, mb_img_data_t *img_data,
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 co_aix x, co_aix y, co_aix w, co_aix h) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 sh_image_t *img;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 cairo_format_t fmt;
260
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
35 paint_t *paint;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 img = O_ALLOC(sh_image_t);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 if(img == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 memset(img, 0, sizeof(sh_image_t));
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 img->shape.free = sh_image_free;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 mb_obj_init((mb_obj_t *)img, MBO_IMAGE);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 img->x = x;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 img->y = y;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 img->w = w;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 img->h = h;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 img->img_data = img_data;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50
260
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
51 paint = rdman_paint_image_new(rdman, img_data);
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
52 rdman_paint_fill(rdman, paint, (shape_t *)img);
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
53 img->paint = paint;
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
54 img->rdman = rdman;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 return (shape_t *)img;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 }
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 void sh_image_free(shape_t *shape) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 sh_image_t *img = (sh_image_t *)shape;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61
260
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
62 rdman_paint_free(img->rdman, img->paint);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 mb_obj_destroy(shape);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 MB_IMG_DATA_FREE(img->img_data);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 free(img);
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
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 void sh_image_transform(shape_t *shape) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 sh_image_t *img = (sh_image_t *)shape;
260
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
70 co_aix (*poses)[2];
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
71 co_aix img_matrix[6];
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
72 cairo_matrix_t cmatrix;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 int i;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74
260
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
75 poses = img->poses;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 poses[0][0] = img->x;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 poses[0][1] = img->y;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 poses[1][0] = img->x + img->w;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 poses[1][1] = img->y;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 poses[2][0] = img->x + img->w;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 poses[2][1] = img->y + img->h;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 poses[3][0] = img->x;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 poses[3][1] = img->y + img->h;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 for(i = 0; i < 4; i++)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 coord_trans_pos(img->shape.coord, &poses[i][0], &poses[i][1]);
260
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
86
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
87 img_matrix[0] = (poses[1][0] - poses[0][0]) / img->w;
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
88 img_matrix[1] = (poses[1][1] - poses[0][1]) / img->w;
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
89 img_matrix[2] = -poses[0][0];
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
90 img_matrix[3] = (poses[3][0] - poses[0][0]) / img->h;
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
91 img_matrix[4] = (poses[3][1] - poses[0][1]) / img->h;
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
92 img_matrix[5] = -poses[0][1];
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
93 paint_image_set_matrix(sh_get_fill(shape), img_matrix);
257
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 geo_from_positions(sh_get_geo(shape), 4, poses);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 /*! \brief Draw image for an image shape.
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 * \note Image is not rescaled for size of the shape.
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 void sh_image_draw(shape_t *shape, cairo_t *cr) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 sh_image_t *img = (sh_image_t *)shape;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 cairo_pattern_t *saved_source;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 cairo_matrix_t matrix, saved_matrix;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 co_aix *aggr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107
260
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
108 cairo_move_to(cr, img->poses[0][0], img->poses[0][1]);
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
109 cairo_line_to(cr, img->poses[1][0], img->poses[1][1]);
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
110 cairo_line_to(cr, img->poses[2][0], img->poses[2][1]);
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
111 cairo_line_to(cr, img->poses[3][0], img->poses[3][1]);
29acbd8a0dd0 Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents: 257
diff changeset
112 cairo_close_path(cr);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 void sh_image_set(shape_t *shape, co_aix x, co_aix y,
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 co_aix w, co_aix h) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 sh_image_t *img = (sh_image_t *)shape;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119 img->x = x;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 img->y = y;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 img->w = w;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122 img->h = h;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123 }