annotate src/img_ldr.c @ 988:bc8cfcd40d0e refine_backend_if

Rename _x_mb_io_man* to _x_supp_io_man*
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 22 Nov 2010 00:42:29 +0800
parents 586e50f82c1f
children 7b4e80ab671a
rev   line source
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 645
diff changeset
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 645
diff changeset
2 // vim: sw=4:ts=8:sts=4
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include <stdio.h>
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include <string.h>
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
5 #include "mb_graph_engine.h"
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 #include "mb_tools.h"
356
3e84458968ec Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents: 346
diff changeset
7 #include "mb_paint.h"
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 #include "mb_img_ldr.h"
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 /*! \brief Simple image loader.
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 *
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 */
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 struct _simple_mb_img_ldr {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 mb_img_ldr_t ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 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
16 };
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 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
18
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 struct _simple_mb_img_data {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 mb_img_data_t img;
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
21 mbe_surface_t *surf;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 };
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 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
24
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 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
26
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 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
29 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
30 simple_mb_img_data_t *img;
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
31 mbe_surface_t *surf;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 char *fname;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 int sz;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 sz = strlen(sldr->repo);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 sz += strlen(img_id);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 fname = (char *)malloc(sz + 2);
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 645
diff changeset
38 if (img_id[0] != '/')
645
818e870e1eb5 Check if the path is absolute path. We use the default prefix only for relative path.
wycc
parents: 450
diff changeset
39 strcpy(fname, sldr->repo);
818e870e1eb5 Check if the path is absolute path. We use the default prefix only for relative path.
wycc
parents: 450
diff changeset
40 else
818e870e1eb5 Check if the path is absolute path. We use the default prefix only for relative path.
wycc
parents: 450
diff changeset
41 fname[0] = 0;
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 strcat(fname, img_id);
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 645
diff changeset
43
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
44 surf = mbe_image_surface_create_from_png(fname);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 if(surf == NULL)
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 = O_ALLOC(simple_mb_img_data_t);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 if(img == NULL) {
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
50 mbe_surface_destroy(surf);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 }
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
53 img->img.content = mbe_image_surface_get_data(surf);
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
54 img->img.w = mbe_image_surface_get_width(surf);
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
55 img->img.h = mbe_image_surface_get_height(surf);
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
56 img->img.stride = mbe_image_surface_get_stride(surf);
450
a417fd980228 Replace cairo_format_t with mb_img_fmt_t.
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
57 img->img.fmt = mbe_image_surface_get_format(surf);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 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
59 img->surf = surf;
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 return (mb_img_data_t *)img;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 }
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 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
66 simple_mb_img_data_t *simg = (simple_mb_img_data_t *)img;
448
16116d84bc5e Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents: 356
diff changeset
67 mbe_surface_destroy((mbe_surface_t *)simg->surf);
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 free(img);
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
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 static
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 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
73 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
74
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 free((void *)defldr->repo);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 }
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 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
79 simple_mb_img_ldr_t *ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 int sz;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 if(img_repository == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 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
86 if(ldr == NULL)
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 return NULL;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 /*
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 * Copy and formalize path of image repository.
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 sz = strlen(img_repository);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 ldr->repo = (const char *)malloc(sz + 2);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 if(ldr->repo == NULL) {
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 free(ldr);
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 return NULL;
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 strcpy((char *)ldr->repo, img_repository);
346
b391722bf20e sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents: 268
diff changeset
99 if(img_repository[sz - 1] != '/' && strlen(img_repository) != 0) {
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 ((char *)ldr->repo)[sz] = '/';
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 ((char *)ldr->repo)[sz + 1] = 0;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 }
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 645
diff changeset
103
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 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
105 ldr->ldr.free = simple_mb_img_ldr_free;
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 645
diff changeset
106
257
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 return (mb_img_ldr_t *)ldr;
50d253d0fcba Simple image loader and image shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 }