annotate src/img_ldr.c @ 314:6c350fc92ae3

Cache rednering result is now workable. - Know issues - For unknow issue, CAIRO_OPERATOR_CLEAR will not be limited by clipping. Image will be cleaned in a strange way. Maybe, it is a bug of Cairo.
author Thinker K.F. Li <thinker@branda.to>
date Thu, 05 Mar 2009 00:54:42 +0800
parents 43900cae1d49
children b391722bf20e
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"
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include "mb_img_ldr.h"
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 /*! \brief Simple image loader.
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 */
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 struct _simple_mb_img_ldr {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 mb_img_ldr_t ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 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
13 };
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 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
15
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 struct _simple_mb_img_data {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 mb_img_data_t img;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 cairo_surface_t *surf;
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 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
21
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 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
23
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 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
26 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
27 simple_mb_img_data_t *img;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 cairo_surface_t *surf;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 char *fname;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 cairo_format_t fmt;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 int sz;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 sz = strlen(sldr->repo);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 sz += strlen(img_id);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 fname = (char *)malloc(sz + 2);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 strcpy(fname, sldr->repo);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 strcat(fname, img_id);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 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
40 if(surf == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 return NULL;
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 = O_ALLOC(simple_mb_img_data_t);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 if(img == NULL) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 cairo_surface_destroy(surf);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 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
49 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
50 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
51 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
52 fmt = cairo_image_surface_get_format(surf);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 switch(fmt) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 case CAIRO_FORMAT_ARGB32:
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 img->img.fmt = MB_IFMT_ARGB32;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 break;
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 case CAIRO_FORMAT_RGB24:
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 img->img.fmt = MB_IFMT_RGB24;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 break;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 case CAIRO_FORMAT_A8:
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 img->img.fmt = MB_IFMT_A8;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 break;
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 case CAIRO_FORMAT_A1:
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 img->img.fmt = MB_IFMT_A1;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 break;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 default:
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 cairo_surface_destroy(surf);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 free(img);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 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
76 img->surf = surf;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 return (mb_img_data_t *)img;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 }
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 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 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
83 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
84 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
85 free(img);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 }
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 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 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
90 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
91
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 free((void *)defldr->repo);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 }
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 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
96 simple_mb_img_ldr_t *ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 int sz;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 if(img_repository == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 return NULL;
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 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
103 if(ldr == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105
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 * Copy and formalize path of image repository.
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 sz = strlen(img_repository);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 ldr->repo = (const char *)malloc(sz + 2);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 if(ldr->repo == NULL) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 free(ldr);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 return NULL;
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 strcpy((char *)ldr->repo, img_repository);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 if(img_repository[sz - 1] != '/') {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 ((char *)ldr->repo)[sz] = '/';
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 ((char *)ldr->repo)[sz + 1] = 0;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119 }
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 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
122 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
123
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 return (mb_img_ldr_t *)ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 }