1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include <lib/pluginlib_bmp.h>
24 #include "../imageviewer.h"
26 #include "gif_decoder.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
44 #define resize_bitmap grey_resize_bitmap
47 #if defined(USEGSLIB) && (CONFIG_PLATFORM & PLATFORM_HOSTED)
48 /* hack: fix error "undefined reference to `_grey_info'". */
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
;
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),
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),
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
)
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
;
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
;
94 if (iv
->running_slideshow
&& iv
->immediate_ata_off
) {
95 /* running slideshow and time is long enough: power down disk */
100 /* initialize decoder context struct, set buffer decoder is free
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",
116 rb
->lcd_putsf(0, 3, "decoding %d*%d",
122 /* the actual decoding */
123 time
= *rb
->current_tick
;
125 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
128 gif_decode(p_decoder
, iv
->cb_progress
);
130 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
131 rb
->cpu_boost(false);
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
);
144 if (p_decoder
->error
)
146 rb
->splashf(HZ
, "%s", GifErrorString(p_decoder
->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
));
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);
179 /* small helper to convert scalling factor ds
180 * into disp[frame][] array index
182 static int ds2index(int ds
)
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
;
207 /* we still have it */
211 /* assign image buffer */
214 if (!iv
->running_slideshow
)
216 rb
->lcd_putsf(0, 3, "resizing %d*%d", info
->width
, info
->height
);
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,
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;
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
256 resize_bitmap(&bmp_src
, &bmp_dst
);
258 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
259 rb
->cpu_boost(false);
264 *p_disp
= p_decoder
->mem
+ p_decoder
->native_img_size
*frame
;
270 const struct image_decoder image_decoder
= {