diff options
author | Teruaki Kawashima <teru@rockbox.org> | 2010-11-21 13:47:56 +0000 |
---|---|---|
committer | Teruaki Kawashima <teru@rockbox.org> | 2010-11-21 13:47:56 +0000 |
commit | e5b1a7d4237a9006b6c49c9c1c13b292ca4ecf7c (patch) | |
tree | f0b9dd1cba0a4ee70afd8b6da64a05286f76c380 /apps/plugins/imageviewer/image_decoder.c | |
parent | eef21cb18ae4bc7cdf83830554a848e0c733a73d (diff) | |
download | rockbox-e5b1a7d4237a9006b6c49c9c1c13b292ca4ecf7c.tar.gz rockbox-e5b1a7d4237a9006b6c49c9c1c13b292ca4ecf7c.zip |
FS#6321: Universal Image Viewer
This unifies jpeg viewer, png viewer, and bmp viewer to one plugin, image viewer, so that you can navigate through different image formats.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@28626 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/imageviewer/image_decoder.c')
-rw-r--r-- | apps/plugins/imageviewer/image_decoder.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/apps/plugins/imageviewer/image_decoder.c b/apps/plugins/imageviewer/image_decoder.c new file mode 100644 index 0000000000..b4fa27e425 --- /dev/null +++ b/apps/plugins/imageviewer/image_decoder.c | |||
@@ -0,0 +1,130 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * load image decoder. | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "plugin.h" | ||
23 | #include "imageviewer.h" | ||
24 | #include "image_decoder.h" | ||
25 | |||
26 | static const char *decoder_names[MAX_IMAGE_TYPES] = { | ||
27 | "bmp", | ||
28 | "jpeg", | ||
29 | "png", | ||
30 | }; | ||
31 | |||
32 | /* check file type by extention */ | ||
33 | enum image_type get_image_type(const char *name) | ||
34 | { | ||
35 | static const struct { | ||
36 | char *ext; | ||
37 | enum image_type type; | ||
38 | } ext_list[] = { | ||
39 | { ".bmp", IMAGE_BMP }, | ||
40 | { ".jpg", IMAGE_JPEG }, | ||
41 | { ".jpe", IMAGE_JPEG }, | ||
42 | { ".jpeg", IMAGE_JPEG }, | ||
43 | { ".png", IMAGE_PNG }, | ||
44 | }; | ||
45 | |||
46 | const char *ext = rb->strrchr(name, '.'); | ||
47 | int i; | ||
48 | if (!ext) | ||
49 | return IMAGE_UNKNOWN; | ||
50 | |||
51 | for (i = 0; i < (int)ARRAYLEN(ext_list); i++) | ||
52 | { | ||
53 | if (!rb->strcasecmp(ext, ext_list[i].ext)) | ||
54 | return ext_list[i].type; | ||
55 | } | ||
56 | return IMAGE_UNKNOWN; | ||
57 | } | ||
58 | |||
59 | static void *decoder_handle = NULL; | ||
60 | const struct image_decoder *load_decoder(struct loader_info *loader_info) | ||
61 | { | ||
62 | const char *name; | ||
63 | char filename[MAX_PATH]; | ||
64 | struct imgdec_header *hdr; | ||
65 | struct lc_header *lc_hdr; | ||
66 | |||
67 | if (loader_info->type < 0 || loader_info->type >= MAX_IMAGE_TYPES) | ||
68 | { | ||
69 | rb->splashf(2*HZ, "Unknown type: %d", loader_info->type); | ||
70 | goto error; | ||
71 | } | ||
72 | |||
73 | release_decoder(); | ||
74 | |||
75 | name = decoder_names[loader_info->type]; | ||
76 | rb->snprintf(filename, MAX_PATH, VIEWERS_DIR "/%s.ovl", name); | ||
77 | |||
78 | /* load decoder to the buffer. */ | ||
79 | decoder_handle = rb->lc_open(filename, loader_info->buffer, loader_info->size); | ||
80 | if (!decoder_handle) | ||
81 | { | ||
82 | rb->splashf(2*HZ, "Can't open %s", filename); | ||
83 | goto error; | ||
84 | } | ||
85 | |||
86 | hdr = rb->lc_get_header(decoder_handle); | ||
87 | if (!hdr) | ||
88 | { | ||
89 | rb->splash(2*HZ, "Can't get header"); | ||
90 | goto error_close; | ||
91 | } | ||
92 | lc_hdr = &hdr->lc_hdr; | ||
93 | |||
94 | if (lc_hdr->magic != PLUGIN_MAGIC || lc_hdr->target_id != TARGET_ID) | ||
95 | { | ||
96 | rb->splashf(2*HZ, "%s decoder: Incompatible model.", name); | ||
97 | goto error_close; | ||
98 | } | ||
99 | |||
100 | if (lc_hdr->api_version != IMGDEC_API_VERSION) | ||
101 | { | ||
102 | rb->splashf(2*HZ, "%s decoder: Incompatible version.", name); | ||
103 | goto error_close; | ||
104 | } | ||
105 | |||
106 | *(hdr->api) = rb; | ||
107 | *(hdr->img_api) = loader_info->iv; | ||
108 | |||
109 | /* set remaining buffer size to loader_info. decoder will | ||
110 | * be loaded to the end of the buffer, so fix size only. */ | ||
111 | #if (CONFIG_PLATFORM & PLATFORM_NATIVE) | ||
112 | loader_info->size = lc_hdr->load_addr - loader_info->buffer; | ||
113 | #endif | ||
114 | |||
115 | return hdr->decoder; | ||
116 | |||
117 | error_close: | ||
118 | release_decoder(); | ||
119 | error: | ||
120 | return NULL; | ||
121 | } | ||
122 | |||
123 | void release_decoder(void) | ||
124 | { | ||
125 | if (decoder_handle != NULL) | ||
126 | { | ||
127 | rb->lc_close(decoder_handle); | ||
128 | decoder_handle = NULL; | ||
129 | } | ||
130 | } | ||