Initial 800x480 cabbiev2 port, based on 480x800x16 one
[kugel-rb.git] / apps / plugins / imageviewer / image_decoder.c
blobb4fa27e4250beae7e86f118c42638f324caec870
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * load image decoder.
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
23 #include "imageviewer.h"
24 #include "image_decoder.h"
26 static const char *decoder_names[MAX_IMAGE_TYPES] = {
27 "bmp",
28 "jpeg",
29 "png",
32 /* check file type by extention */
33 enum image_type get_image_type(const char *name)
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 },
46 const char *ext = rb->strrchr(name, '.');
47 int i;
48 if (!ext)
49 return IMAGE_UNKNOWN;
51 for (i = 0; i < (int)ARRAYLEN(ext_list); i++)
53 if (!rb->strcasecmp(ext, ext_list[i].ext))
54 return ext_list[i].type;
56 return IMAGE_UNKNOWN;
59 static void *decoder_handle = NULL;
60 const struct image_decoder *load_decoder(struct loader_info *loader_info)
62 const char *name;
63 char filename[MAX_PATH];
64 struct imgdec_header *hdr;
65 struct lc_header *lc_hdr;
67 if (loader_info->type < 0 || loader_info->type >= MAX_IMAGE_TYPES)
69 rb->splashf(2*HZ, "Unknown type: %d", loader_info->type);
70 goto error;
73 release_decoder();
75 name = decoder_names[loader_info->type];
76 rb->snprintf(filename, MAX_PATH, VIEWERS_DIR "/%s.ovl", name);
78 /* load decoder to the buffer. */
79 decoder_handle = rb->lc_open(filename, loader_info->buffer, loader_info->size);
80 if (!decoder_handle)
82 rb->splashf(2*HZ, "Can't open %s", filename);
83 goto error;
86 hdr = rb->lc_get_header(decoder_handle);
87 if (!hdr)
89 rb->splash(2*HZ, "Can't get header");
90 goto error_close;
92 lc_hdr = &hdr->lc_hdr;
94 if (lc_hdr->magic != PLUGIN_MAGIC || lc_hdr->target_id != TARGET_ID)
96 rb->splashf(2*HZ, "%s decoder: Incompatible model.", name);
97 goto error_close;
100 if (lc_hdr->api_version != IMGDEC_API_VERSION)
102 rb->splashf(2*HZ, "%s decoder: Incompatible version.", name);
103 goto error_close;
106 *(hdr->api) = rb;
107 *(hdr->img_api) = loader_info->iv;
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
115 return hdr->decoder;
117 error_close:
118 release_decoder();
119 error:
120 return NULL;
123 void release_decoder(void)
125 if (decoder_handle != NULL)
127 rb->lc_close(decoder_handle);
128 decoder_handle = NULL;