comparison src/img_ldr.c @ 257:50d253d0fcba

Simple image loader and image shape. - img_ldr.c is a simple image loader that rooted on a directory specified when a loader instance been created. - sh_image_t is corresponding shape of image tag in SVG. - This changeset is still buggy. It need more testing. - svg2code.py is not ready for image tag.
author Thinker K.F. Li <thinker@branda.to>
date Thu, 15 Jan 2009 16:46:47 +0800
parents
children 43900cae1d49
comparison
equal deleted inserted replaced
256:cac9ad3df633 257:50d253d0fcba
1 #include <stdio.h>
2 #include <string.h>
3 #include <cairo.h>
4 #include "mb_tools.h"
5 #include "mb_img_ldr.h"
6
7 /*! \brief Simple image loader.
8 *
9 */
10 struct _simple_mb_img_ldr {
11 mb_img_ldr_t ldr;
12 const char *repo; /*!< \brief The directory of repository. */
13 };
14 typedef struct _simple_mb_img_ldr simple_mb_img_ldr_t;
15
16 struct _simple_mb_img_data {
17 mb_img_data_t img;
18 cairo_surface_t *surf;
19 };
20 typedef struct _simple_mb_img_data simple_mb_img_data_t;
21
22 static void simple_mb_img_ldr_img_free(mb_img_data_t *img);
23
24 static
25 mb_img_data_t *simple_mb_img_ldr_load(mb_img_ldr_t *ldr, const char *img_id) {
26 simple_mb_img_ldr_t *sldr = (simple_mb_img_ldr_t *)ldr;
27 simple_mb_img_data_t *img;
28 cairo_surface_t *surf;
29 char *fname;
30 cairo_format_t fmt;
31 int sz;
32
33 sz = strlen(sldr->repo);
34 sz += strlen(img_id);
35 fname = (char *)malloc(sz + 2);
36 strcpy(fname, sldr->repo);
37 strcat(fname, img_id);
38
39 surf = cairo_image_surface_create_from_png(fname);
40 if(surf == NULL)
41 return NULL;
42
43 img = O_ALLOC(simple_mb_img_data_t);
44 if(img == NULL) {
45 cairo_surface_destroy(surf);
46 return NULL;
47 }
48 img->img.content = cairo_image_surface_get_data(surf);
49 img->img.width = cairo_image_surface_get_width(surf);
50 img->img.height = cairo_image_surface_get_height(surf);
51 img->img.stride = cairo_image_surface_get_stride(surf);
52 fmt = cairo_image_surface_get_format(surf);
53 switch(fmt) {
54 case CAIRO_FORMAT_ARGB32:
55 img->img.fmt = MB_IFMT_ARGB32;
56 break;
57
58 case CAIRO_FORMAT_RGB24:
59 img->img.fmt = MB_IFMT_RGB24;
60 break;
61
62 case CAIRO_FORMAT_A8:
63 img->img.fmt = MB_IFMT_A8;
64 break;
65
66 case CAIRO_FORMAT_A1:
67 img->img.fmt = MB_IFMT_A1;
68 break;
69
70 default:
71 cairo_surface_destroy(surf);
72 free(img);
73 return NULL;
74 }
75 img->img.free = simple_mb_img_ldr_img_free;
76 img->surf = surf;
77
78 return (mb_img_data_t *)img;
79 }
80
81 static
82 void simple_mb_img_ldr_img_free(mb_img_data_t *img) {
83 simple_mb_img_data_t *simg = (simple_mb_img_data_t *)img;
84 cairo_surface_destroy((cairo_surface_t *)simg->surf);
85 free(img);
86 }
87
88 static
89 void simple_mb_img_ldr_free(mb_img_ldr_t *ldr) {
90 simple_mb_img_ldr_t *defldr = (simple_mb_img_ldr_t *)ldr;
91
92 free((void *)defldr->repo);
93 }
94
95 mb_img_ldr_t *simple_mb_img_ldr_new(const char *img_repository) {
96 simple_mb_img_ldr_t *ldr;
97 int sz;
98
99 if(img_repository == NULL)
100 return NULL;
101
102 ldr = O_ALLOC(simple_mb_img_ldr_t);
103 if(ldr == NULL)
104 return NULL;
105
106 /*
107 * Copy and formalize path of image repository.
108 */
109 sz = strlen(img_repository);
110 ldr->repo = (const char *)malloc(sz + 2);
111 if(ldr->repo == NULL) {
112 free(ldr);
113 return NULL;
114 }
115 strcpy((char *)ldr->repo, img_repository);
116 if(img_repository[sz - 1] != '/') {
117 ((char *)ldr->repo)[sz] = '/';
118 ((char *)ldr->repo)[sz + 1] = 0;
119 }
120
121 ldr->ldr.load = simple_mb_img_ldr_load;
122 ldr->ldr.free = simple_mb_img_ldr_free;
123
124 return (mb_img_ldr_t *)ldr;
125 }