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