comparison src/img_ldr_imlib2.c @ 1067:7b4e80ab671a openvg

merge from default branch
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 01 Dec 2010 12:25:56 +0800
parents
children b5145de15ace
comparison
equal deleted inserted replaced
630:bd18951b51d5 1067:7b4e80ab671a
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
2 // vim: sw=4:ts=8:sts=4
3 #include <stdio.h>
4 #include <string.h>
5 #include <Imlib2.h>
6 #include "mb_graph_engine.h"
7 #include "mb_tools.h"
8 #include "mb_paint.h"
9 #include "mb_img_ldr.h"
10
11 /*! \brief Simple image loader.
12 *
13 */
14 struct _simple_mb_img_ldr {
15 mb_img_ldr_t ldr;
16 const char *repo; /*!< \brief The directory of repository. */
17 };
18 typedef struct _simple_mb_img_ldr simple_mb_img_ldr_t;
19
20 struct _simple_mb_img_data {
21 mb_img_data_t img;
22 Imlib_Image img_hdl;
23 };
24 typedef struct _simple_mb_img_data simple_mb_img_data_t;
25
26 static void simple_mb_img_ldr_img_free(mb_img_data_t *img);
27
28 static
29 mb_img_data_t *simple_mb_img_ldr_load(mb_img_ldr_t *ldr, const char *img_id) {
30 simple_mb_img_ldr_t *sldr = (simple_mb_img_ldr_t *)ldr;
31 simple_mb_img_data_t *img;
32 Imlib_Image img_hdl;
33 int w, h;
34 void *data;
35 char *fname;
36 int sz;
37
38 sz = strlen(sldr->repo);
39 sz += strlen(img_id);
40 fname = (char *)malloc(sz + 2);
41 if (img_id[0] != '/')
42 strcpy(fname, sldr->repo);
43 else
44 fname[0] = 0;
45 strcat(fname, img_id);
46
47 img_hdl = imlib_load_image(fname);
48 if(!img_hdl)
49 return NULL;
50 imlib_context_set_image(img_hdl);
51 w = imlib_image_get_width();
52 h = imlib_image_get_height();
53 data = imlib_image_get_data_for_reading_only();
54
55 img = O_ALLOC(simple_mb_img_data_t);
56 if(img == NULL) {
57 imlib_free_image();
58 return NULL;
59 }
60 img->img.content = data;
61 img->img.w = w;
62 img->img.h = h;
63 img->img.stride = w * 4;
64 img->img.fmt = MB_IFMT_ARGB32;
65 img->img.free = simple_mb_img_ldr_img_free;
66 img->img_hdl = img_hdl;
67
68 return (mb_img_data_t *)img;
69 }
70
71 static
72 void simple_mb_img_ldr_img_free(mb_img_data_t *img) {
73 simple_mb_img_data_t *simg = (simple_mb_img_data_t *)img;
74
75 imlib_context_set_image(simg->img_hdl);
76 imlib_free_image();
77 free(img);
78 }
79
80 static
81 void simple_mb_img_ldr_free(mb_img_ldr_t *ldr) {
82 simple_mb_img_ldr_t *defldr = (simple_mb_img_ldr_t *)ldr;
83
84 free((void *)defldr->repo);
85 }
86
87 mb_img_ldr_t *simple_mb_img_ldr_new(const char *img_repository) {
88 simple_mb_img_ldr_t *ldr;
89 int sz;
90
91 if(img_repository == NULL)
92 return NULL;
93
94 ldr = O_ALLOC(simple_mb_img_ldr_t);
95 if(ldr == NULL)
96 return NULL;
97
98 /*
99 * Copy and formalize path of image repository.
100 */
101 sz = strlen(img_repository);
102 ldr->repo = (const char *)malloc(sz + 2);
103 if(ldr->repo == NULL) {
104 free(ldr);
105 return NULL;
106 }
107 strcpy((char *)ldr->repo, img_repository);
108 if(img_repository[sz - 1] != '/' && strlen(img_repository) != 0) {
109 ((char *)ldr->repo)[sz] = '/';
110 ((char *)ldr->repo)[sz + 1] = 0;
111 }
112
113 ldr->ldr.load = simple_mb_img_ldr_load;
114 ldr->ldr.free = simple_mb_img_ldr_free;
115
116 return (mb_img_ldr_t *)ldr;
117 }