Mercurial > MadButterfly
annotate src/shape_image.c @ 346:b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
- sh_image_t should not try to free it.
- call sh_text_P_generate_layout() in sh_text_transform()
- remove calling from other functions.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 08 Mar 2009 14:44:41 +0800 |
parents | bb6e964da1c8 |
children | 04d22dc38bc0 |
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 |
265
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
9 /*! \page sh_image_n_image_ldr Image and Image Loader |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
10 * |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
11 * Image (\ref sh_image_t) is a shape to show an image on the output |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
12 * device. Programmers manipulate object of an image shape to show it |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
13 * at specified position with specified size. To create a new instance |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
14 * of sh_iamge_t, an image should be specified. Programmers must have |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
15 * a way to load image from files. The solution proposed by MadButterfly |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
16 * is image loader (\ref mb_img_ldr_t). |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
17 * |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
18 * Image loader is a repository of image files, programmers give him an |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
19 * ID and get an image returned the loader. Image loader decodes image |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
20 * files specified IDs and return them in an internal representation. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
21 * The internal representation of an array of pixels. Pixels are in |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
22 * order of columns (X-axis) and then row by row (Y-axis). An pixel |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
23 * can be 32bits, for ARGB, 24bits, for RGB, 8bits, for 8bits Alpha or |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
24 * 256-grey-levels, and 1bits, for bitmap. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
25 * |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
26 * Every row is padded to round to byte boundary, a rounded row is a stride. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
27 * Bytes a stride occupied is stride size. The internal rpresentation |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
28 * is a series of strides. The data returned by image loader is |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
29 * \ref mb_img_data_t type. mb_img_data_t::content is data in internal |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
30 * representation. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
31 * |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
32 * \ref simple_mb_img_ldr_t is a simple implementation of image loader. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
33 * It is a repository of image files in a directory and sub-directories. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
34 * ID of an image is mapped to a file in the directory and sub-directories. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
35 * ID it-self is a relative path relate to root directory of the repository. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
36 * \ref simple_mb_img_ldr_t handle PNG files only, now. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
37 * |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
38 * \section get_img_ldr Get an Image Loader for Program |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
39 * redraw_man_t::img_ldr is an image loader assigned by backend. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
40 * X backend, now, create an instance of simple_mb_img_ldr_t and assigns |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
41 * the instance to redraw_man_t::img_ldr. Programmers should get |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
42 * image loader assigned for a rdman by calling rdman_img_ldr(). |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
43 * |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
44 * \image html image_n_ldr.png |
266
af2d3300f8ff
Add image for document of latex format
Thinker K.F. Li <thinker@branda.to>
parents:
265
diff
changeset
|
45 * \image latex image_n_ldr.eps "Relationship of image and loader" width=10cm |
265
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
46 */ |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
47 |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
48 #define ASSERT(x) |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
49 #define OK 0 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
50 #define ERR -1 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
51 |
265
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
52 /*! \brief Image shape. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
53 */ |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
54 typedef struct _sh_image { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
55 shape_t shape; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
56 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
57 co_aix x, y; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
58 co_aix w, h; |
260
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
59 co_aix poses[4][2]; |
257
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 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
|
62 paint_t *paint; |
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
63 redraw_man_t *rdman; |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
64 } sh_image_t; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
65 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
66 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
|
67 |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
68 int _sh_image_set_img_data(shape_t *shape, mb_img_data_t *img_data, |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
69 co_aix x, co_aix y, co_aix w, co_aix h) { |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
70 sh_image_t *img = (sh_image_t *)shape; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
71 paint_t *paint; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
72 |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
73 ASSERT(img_data != NULL); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
74 ASSERT(shape->obj.obj_type == MBO_IMAGE); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
75 |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
76 paint = rdman_paint_image_new(img->rdman, img_data); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
77 if(paint == NULL) |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
78 return ERR; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
79 |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
80 if(img->paint) |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
81 rdman_paint_free(img->rdman, img->paint); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
82 |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
83 img->img_data = img_data; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
84 img->x = x; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
85 img->y = y; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
86 img->w = w; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
87 img->h = h; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
88 img->paint = paint; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
89 rdman_paint_fill(img->rdman, paint, (shape_t *)img); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
90 |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
91 return OK; |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
92 } |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
93 |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
94 /*! \brief Creae a new image shape. |
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 * \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
|
97 */ |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
98 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
|
99 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
|
100 sh_image_t *img; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
101 cairo_format_t fmt; |
260
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
102 paint_t *paint; |
338
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
103 int r; |
257
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 img = O_ALLOC(sh_image_t); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
106 if(img == NULL) |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
107 return NULL; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
108 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
109 memset(img, 0, sizeof(sh_image_t)); |
338
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
110 mb_obj_init((mb_obj_t *)img, MBO_IMAGE); |
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
111 img->rdman = rdman; |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
112 img->shape.free = sh_image_free; |
338
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
113 |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
114 r = _sh_image_set_img_data((shape_t *)img, img_data, x, y, w, h); |
338
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
115 if(r != OK) { |
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
116 mb_obj_destroy((shape_t *)img); |
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
117 free(img); |
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
118 return NULL; |
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
119 } |
257
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 return (shape_t *)img; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
122 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
123 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
124 void sh_image_free(shape_t *shape) { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
125 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
|
126 |
260
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
127 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
|
128 mb_obj_destroy(shape); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
129 free(img); |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
130 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
131 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
132 void sh_image_transform(shape_t *shape) { |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
133 sh_image_t *img = (sh_image_t *)shape; |
268
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
134 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
|
135 co_aix (*poses)[2]; |
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
136 co_aix img_matrix[6]; |
268
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
137 co_aix x_factor, y_factor; |
260
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
138 cairo_matrix_t cmatrix; |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
139 int i; |
268
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
140 |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
141 img_data = img->img_data; |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
142 |
260
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
143 poses = img->poses; |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
144 poses[0][0] = img->x; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
145 poses[0][1] = img->y; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
146 poses[1][0] = img->x + img->w; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
147 poses[1][1] = img->y; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
148 poses[2][0] = img->x + img->w; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
149 poses[2][1] = img->y + img->h; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
150 poses[3][0] = img->x; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
151 poses[3][1] = img->y + img->h; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
152 for(i = 0; i < 4; i++) |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
153 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
|
154 |
265
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
155 /* Transformation from user space to image space */ |
260
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
156 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
|
157 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
|
158 img_matrix[2] = -poses[0][0]; |
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
159 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
|
160 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
|
161 img_matrix[5] = -poses[0][1]; |
268
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
162 if(img->w != img_data->w || |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
163 img->h != img_data->h) { |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
164 /* Resize image */ |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
165 x_factor = img_data->w / img->w; |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
166 img_matrix[0] *= x_factor; |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
167 img_matrix[1] *= x_factor; |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
168 img_matrix[2] *= x_factor; |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
169 y_factor = img_data->h / img->h; |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
170 img_matrix[3] *= y_factor; |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
171 img_matrix[4] *= y_factor; |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
172 img_matrix[5] *= y_factor; |
43900cae1d49
Support resizing for image.
Thinker K.F. Li <thinker@branda.to>
parents:
266
diff
changeset
|
173 } |
260
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
174 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
|
175 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
176 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
|
177 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
178 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
179 /*! \brief Draw image for an image shape. |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
180 * |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
181 * \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
|
182 */ |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
183 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
|
184 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
|
185 cairo_pattern_t *saved_source; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
186 cairo_matrix_t matrix, saved_matrix; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
187 co_aix *aggr; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
188 |
260
29acbd8a0dd0
Integrate sh_image with svg2code.py.
Thinker K.F. Li <thinker@branda.to>
parents:
257
diff
changeset
|
189 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
|
190 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
|
191 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
|
192 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
|
193 cairo_close_path(cr); |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
194 } |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
195 |
265
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
196 /*! \brief Change geometry of an image. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
197 * |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
198 * Set position and size of an image. |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
199 */ |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
200 void sh_image_set_geometry(shape_t *shape, co_aix x, co_aix y, |
b42ee279669e
Change function name and add comments.
Thinker K.F. Li <thinker@branda.to>
parents:
260
diff
changeset
|
201 co_aix w, co_aix h) { |
257
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
202 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
|
203 |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
204 img->x = x; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
205 img->y = y; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
206 img->w = w; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
207 img->h = h; |
50d253d0fcba
Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
208 } |
338
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
209 |
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
210 int sh_image_set_img_data(shape_t *shape, mb_img_data_t *img_data, |
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
211 co_aix x, co_aix y, co_aix w, co_aix h) { |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
212 int r; |
338
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
213 |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
214 r = _sh_image_set_img_data(shape, img_data, x, y, w, h); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
343
diff
changeset
|
215 return r; |
338
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
216 } |
6a1b36738d3d
sh_image_set_img_data() is a function to change content of a sh_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
268
diff
changeset
|
217 |
343
bb6e964da1c8
sh_image_get_img_data()
Thinker K.F. Li <thinker@branda.to>
parents:
338
diff
changeset
|
218 mb_img_data_t *sh_image_get_img_data(shape_t *shape) { |
bb6e964da1c8
sh_image_get_img_data()
Thinker K.F. Li <thinker@branda.to>
parents:
338
diff
changeset
|
219 sh_image_t *img = (sh_image_t *)shape; |
bb6e964da1c8
sh_image_get_img_data()
Thinker K.F. Li <thinker@branda.to>
parents:
338
diff
changeset
|
220 |
bb6e964da1c8
sh_image_get_img_data()
Thinker K.F. Li <thinker@branda.to>
parents:
338
diff
changeset
|
221 ASSERT(shape->obj.obj_type == MBO_IMAGE); |
bb6e964da1c8
sh_image_get_img_data()
Thinker K.F. Li <thinker@branda.to>
parents:
338
diff
changeset
|
222 |
bb6e964da1c8
sh_image_get_img_data()
Thinker K.F. Li <thinker@branda.to>
parents:
338
diff
changeset
|
223 return img->img_data; |
bb6e964da1c8
sh_image_get_img_data()
Thinker K.F. Li <thinker@branda.to>
parents:
338
diff
changeset
|
224 } |