gif viewer: remove max frames count constraint
[maemo-rb.git] / apps / plugins / imageviewer / gif / gif.c
blobc3cad71e10c033414045149f5b1b66a0a541c176
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (c) 2012 Marcin Bukat
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include "plugin.h"
22 #include "lcd.h"
23 #include <lib/pluginlib_bmp.h>
24 #include "../imageviewer.h"
25 #include "bmp.h"
26 #include "gif_decoder.h"
27 #include "gif_lib.h"
29 /* decoder context struct */
30 static struct gif_decoder decoder;
32 static char print[32]; /* use a common snprintf() buffer */
34 /* pointers to decompressed frame in the possible sizes ds = (1,2,4,8)
35 * basicaly equivalent to *disp[n][4] where n is the number of frames
36 * in gif file. The matrix is allocated after decoded frames.
38 static unsigned char **disp;
39 static unsigned char *disp_buf;
41 #if defined(HAVE_LCD_COLOR)
42 #define resize_bitmap smooth_resize_bitmap
43 #else
44 #define resize_bitmap grey_resize_bitmap
45 #endif
47 #if defined(USEGSLIB) && (CONFIG_PLATFORM & PLATFORM_HOSTED)
48 /* hack: fix error "undefined reference to `_grey_info'". */
49 GREY_INFO_STRUCT
50 #endif /* USEGSLIB */
52 static void draw_image_rect(struct image_info *info,
53 int x, int y, int width, int height)
55 unsigned char **pdisp = (unsigned char **)info->data;
57 #ifdef HAVE_LCD_COLOR
58 rb->lcd_bitmap_part((fb_data *)*pdisp, info->x + x, info->y + y,
59 STRIDE(SCREEN_MAIN, info->width, info->height),
60 x + MAX(0, (LCD_WIDTH-info->width)/2),
61 y + MAX(0, (LCD_HEIGHT-info->height)/2),
62 width, height);
63 #else
64 mylcd_ub_gray_bitmap_part(*pdisp,
65 info->x + x, info->y + y, info->width,
66 x + MAX(0, (LCD_WIDTH-info->width)/2),
67 y + MAX(0, (LCD_HEIGHT-info->height)/2),
68 width, height);
69 #endif
72 static int img_mem(int ds)
74 struct gif_decoder *p_decoder = &decoder;
75 return p_decoder->native_img_size/ds;
78 static int load_image(char *filename, struct image_info *info,
79 unsigned char *buf, ssize_t *buf_size)
81 int w, h;
82 long time = 0; /* measured ticks */
83 struct gif_decoder *p_decoder = &decoder;
85 unsigned char *memory, *memory_max;
86 size_t memory_size, img_size, disp_size;
88 /* align buffer */
89 memory = (unsigned char *)((intptr_t)(buf + 3) & ~3);
90 memory_max = (unsigned char *)((intptr_t)(memory + *buf_size) & ~3);
91 memory_size = memory_max - memory;
93 #ifdef DISK_SPINDOWN
94 if (iv->running_slideshow && iv->immediate_ata_off) {
95 /* running slideshow and time is long enough: power down disk */
96 rb->storage_sleep();
98 #endif
100 /* initialize decoder context struct, set buffer decoder is free
101 * to use.
103 gif_decoder_init(p_decoder, memory, memory_size);
105 /* populate internal data from gif file control structs */
106 gif_open(filename, p_decoder);
108 if (!p_decoder->error)
111 if (!iv->running_slideshow)
113 rb->lcd_putsf(0, 2, "image %dx%d",
114 p_decoder->width,
115 p_decoder->height);
116 rb->lcd_putsf(0, 3, "decoding %d*%d",
117 p_decoder->width,
118 p_decoder->height);
119 rb->lcd_update();
122 /* the actual decoding */
123 time = *rb->current_tick;
125 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
126 rb->cpu_boost(true);
127 #endif
128 gif_decode(p_decoder, iv->cb_progress);
130 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
131 rb->cpu_boost(false);
132 #endif
133 time = *rb->current_tick - time;
136 if (!iv->running_slideshow && !p_decoder->error)
138 rb->snprintf(print, sizeof(print), " %ld.%02ld sec ", time/HZ, time%HZ);
139 rb->lcd_getstringsize(print, &w, &h); /* centered in progress bar */
140 rb->lcd_putsxy((LCD_WIDTH - w)/2, LCD_HEIGHT - h, print);
141 rb->lcd_update();
144 if (p_decoder->error)
146 rb->splashf(HZ, "%s", GifErrorString(p_decoder->error));
147 return PLUGIN_ERROR;
150 info->x_size = p_decoder->width;
151 info->y_size = p_decoder->height;
152 info->frames_count = p_decoder->frames_count;
153 info->delay = p_decoder->delay;
155 /* check mem constraints
156 * each frame can have 4 scaled versions with ds = (1,2,4,8)
158 img_size = (p_decoder->native_img_size*p_decoder->frames_count + 3) & ~3;
159 disp_size = (sizeof(unsigned char *)*p_decoder->frames_count*4 + 3) & ~3;
161 if (memory_size < img_size + disp_size)
163 /* No memory to allocate disp matrix */
164 rb->splashf(HZ, "%s", GifErrorString(D_GIF_ERR_NOT_ENOUGH_MEM));
165 return PLUGIN_ERROR;
168 disp = (unsigned char **)(p_decoder->mem + img_size);
169 disp_buf = (unsigned char *)disp + disp_size;
171 *buf_size = memory_max - disp_buf;
173 /* set all pointers to NULL initially */
174 memset(disp, 0, sizeof(unsigned char *)*p_decoder->frames_count*4);
176 return PLUGIN_OK;
179 /* small helper to convert scalling factor ds
180 * into disp[frame][] array index
182 static int ds2index(int ds)
184 int index = 0;
186 ds >>= 1;
187 while (ds)
189 index++;
190 ds >>=1;
193 return index;
196 static int get_image(struct image_info *info, int frame, int ds)
198 unsigned char **p_disp = disp + frame*4 + ds2index(ds);
199 struct gif_decoder *p_decoder = &decoder;
201 info->width = p_decoder->width / ds;
202 info->height = p_decoder->height / ds;
203 info->data = p_disp;
205 if (*p_disp != NULL)
207 /* we still have it */
208 return PLUGIN_OK;
211 /* assign image buffer */
212 if (ds > 1)
214 if (!iv->running_slideshow)
216 rb->lcd_putsf(0, 3, "resizing %d*%d", info->width, info->height);
217 rb->lcd_update();
219 struct bitmap bmp_src, bmp_dst;
221 /* size of the scalled image */
222 int size = img_mem(ds);
224 if (disp_buf + size >= p_decoder->mem + p_decoder->mem_size)
226 /* have to discard scaled versions */
227 for (int i=0; i<p_decoder->frames_count; i++)
229 /* leave unscaled pointer allone,
230 * set rest to NULL
232 p_disp = disp + i*4 + 1;
233 memset(p_disp, 0, 3*sizeof(unsigned char *));
236 /* start again from the beginning of the buffer */
237 disp_buf = p_decoder->mem +
238 p_decoder->native_img_size*p_decoder->frames_count +
239 sizeof(unsigned char *)*p_decoder->frames_count*4;
242 *p_disp = disp_buf;
243 disp_buf += size;
245 bmp_src.width = p_decoder->width;
246 bmp_src.height = p_decoder->height;
247 bmp_src.data = p_decoder->mem + p_decoder->native_img_size*frame;
249 bmp_dst.width = info->width;
250 bmp_dst.height = info->height;
251 bmp_dst.data = *p_disp;
253 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
254 rb->cpu_boost(true);
255 #endif
256 resize_bitmap(&bmp_src, &bmp_dst);
258 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
259 rb->cpu_boost(false);
260 #endif
262 else
264 *p_disp = p_decoder->mem + p_decoder->native_img_size*frame;
267 return PLUGIN_OK;
270 const struct image_decoder image_decoder = {
271 true,
272 img_mem,
273 load_image,
274 get_image,
275 draw_image_rect,
278 IMGDEC_HEADER