2009-07-25 21:36:11 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$id $
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 by Christophe Gouiran <bechris13250 -at- gmail -dot- com>
|
|
|
|
*
|
|
|
|
* Based on lodepng, a lightweight png decoder/encoder
|
|
|
|
* (c) 2005-2008 Lode Vandevenne
|
|
|
|
*
|
2010-10-31 12:40:49 +00:00
|
|
|
* Copyright (c) 2010 Marcin Bukat
|
|
|
|
* - pixel format conversion & transparency handling
|
|
|
|
* - adaptation of tinf (tiny inflate library)
|
|
|
|
* - code refactoring & cleanups
|
|
|
|
*
|
2009-07-25 21:36:11 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "lcd.h"
|
|
|
|
#include <lib/pluginlib_bmp.h>
|
2010-10-31 12:40:49 +00:00
|
|
|
#include "tinf.h"
|
2010-11-01 12:13:49 +00:00
|
|
|
#include "../imageviewer.h"
|
2010-10-31 12:40:49 +00:00
|
|
|
#include "png_decoder.h"
|
|
|
|
#include "bmp.h"
|
2009-07-25 21:36:11 +00:00
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
/* decoder context struct */
|
|
|
|
static LodePNG_Decoder decoder;
|
2009-07-25 21:36:11 +00:00
|
|
|
|
2010-11-01 12:13:49 +00:00
|
|
|
static char print[32]; /* use a common snprintf() buffer */
|
2009-07-25 21:36:11 +00:00
|
|
|
|
|
|
|
/* decompressed image in the possible sizes (1,2,4,8), wasting the other */
|
2010-11-01 12:13:49 +00:00
|
|
|
static unsigned char *disp[9];
|
|
|
|
static unsigned char *disp_buf;
|
2009-07-25 21:36:11 +00:00
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
#if defined(HAVE_LCD_COLOR)
|
|
|
|
#define resize_bitmap smooth_resize_bitmap
|
|
|
|
#else
|
|
|
|
#define resize_bitmap grey_resize_bitmap
|
|
|
|
#endif
|
2009-12-18 13:06:21 +00:00
|
|
|
|
2010-11-21 13:47:56 +00:00
|
|
|
#if defined(USEGSLIB) && (CONFIG_PLATFORM & PLATFORM_HOSTED)
|
|
|
|
/* hack: fix error "undefined reference to `_grey_info'". */
|
|
|
|
GREY_INFO_STRUCT
|
|
|
|
#endif /* USEGSLIB */
|
2009-07-25 21:36:11 +00:00
|
|
|
|
2010-11-21 13:47:56 +00:00
|
|
|
static void draw_image_rect(struct image_info *info,
|
|
|
|
int x, int y, int width, int height)
|
2009-07-25 21:36:11 +00:00
|
|
|
{
|
2010-11-01 12:13:49 +00:00
|
|
|
unsigned char **pdisp = (unsigned char **)info->data;
|
2010-10-31 12:40:49 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
2010-11-01 12:13:49 +00:00
|
|
|
rb->lcd_bitmap_part((fb_data *)*pdisp, info->x + x, info->y + y,
|
2010-10-31 12:40:49 +00:00
|
|
|
STRIDE(SCREEN_MAIN, info->width, info->height),
|
2010-01-18 12:46:19 +00:00
|
|
|
x + MAX(0, (LCD_WIDTH-info->width)/2),
|
|
|
|
y + MAX(0, (LCD_HEIGHT-info->height)/2),
|
|
|
|
width, height);
|
2010-10-31 12:40:49 +00:00
|
|
|
#else
|
2010-11-01 12:13:49 +00:00
|
|
|
mylcd_ub_gray_bitmap_part(*pdisp,
|
2010-10-31 12:40:49 +00:00
|
|
|
info->x + x, info->y + y, info->width,
|
|
|
|
x + MAX(0, (LCD_WIDTH-info->width)/2),
|
|
|
|
y + MAX(0, (LCD_HEIGHT-info->height)/2),
|
|
|
|
width, height);
|
|
|
|
#endif
|
2009-07-25 21:36:11 +00:00
|
|
|
}
|
|
|
|
|
2010-11-21 13:47:56 +00:00
|
|
|
static int img_mem(int ds)
|
2009-07-25 21:36:11 +00:00
|
|
|
{
|
2010-10-31 12:40:49 +00:00
|
|
|
LodePNG_Decoder *p_decoder = &decoder;
|
|
|
|
|
|
|
|
#ifdef USEGSLIB
|
|
|
|
return (p_decoder->infoPng.width/ds) * (p_decoder->infoPng.height/ds);
|
|
|
|
#else
|
|
|
|
return (p_decoder->infoPng.width/ds) *
|
|
|
|
(p_decoder->infoPng.height/ds) *
|
|
|
|
FB_DATA_SZ;
|
|
|
|
#endif
|
2009-07-25 21:36:11 +00:00
|
|
|
}
|
|
|
|
|
2010-11-21 13:47:56 +00:00
|
|
|
static int load_image(char *filename, struct image_info *info,
|
|
|
|
unsigned char *buf, ssize_t *buf_size)
|
2009-12-18 13:06:21 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
long time = 0; /* measured ticks */
|
|
|
|
int w, h; /* used to center output */
|
2010-10-31 12:40:49 +00:00
|
|
|
LodePNG_Decoder *p_decoder = &decoder;
|
|
|
|
|
2010-11-01 12:13:49 +00:00
|
|
|
unsigned char *memory, *memory_max, *image;
|
|
|
|
size_t memory_size, file_size;
|
2010-01-18 12:46:19 +00:00
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
/* cleanup */
|
2010-01-18 12:46:19 +00:00
|
|
|
memset(&disp, 0, sizeof(disp));
|
|
|
|
|
2010-02-12 14:41:52 +00:00
|
|
|
/* align buffer */
|
|
|
|
memory = (unsigned char *)((intptr_t)(buf + 3) & ~3);
|
|
|
|
memory_max = (unsigned char *)((intptr_t)(memory + *buf_size) & ~3);
|
|
|
|
memory_size = memory_max - memory;
|
2009-12-18 13:06:21 +00:00
|
|
|
|
|
|
|
fd = rb->open(filename, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2010-02-18 15:10:31 +00:00
|
|
|
rb->splashf(HZ, "err opening %s: %d", filename, fd);
|
2009-12-18 13:06:21 +00:00
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
2010-10-31 12:40:49 +00:00
|
|
|
file_size = rb->filesize(fd);
|
2009-12-18 13:06:21 +00:00
|
|
|
|
|
|
|
DEBUGF("reading file '%s'\n", filename);
|
|
|
|
|
2010-11-21 13:47:56 +00:00
|
|
|
if (!iv->running_slideshow) {
|
2010-02-12 14:41:52 +00:00
|
|
|
rb->lcd_puts(0, 0, rb->strrchr(filename,'/')+1);
|
2009-12-18 13:06:21 +00:00
|
|
|
rb->lcd_update();
|
|
|
|
}
|
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
if (file_size > memory_size) {
|
|
|
|
p_decoder->error = FILE_TOO_LARGE;
|
2009-12-18 13:06:21 +00:00
|
|
|
rb->close(fd);
|
|
|
|
|
|
|
|
} else {
|
2010-11-21 13:47:56 +00:00
|
|
|
if (!iv->running_slideshow) {
|
2010-10-31 12:40:49 +00:00
|
|
|
rb->lcd_putsf(0, 1, "loading %zu bytes", file_size);
|
2009-12-18 13:06:21 +00:00
|
|
|
rb->lcd_update();
|
|
|
|
}
|
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
/* load file to the end of the buffer */
|
|
|
|
image = memory_max - file_size;
|
|
|
|
rb->read(fd, image, file_size);
|
2009-12-18 13:06:21 +00:00
|
|
|
rb->close(fd);
|
|
|
|
|
2010-11-21 13:47:56 +00:00
|
|
|
if (!iv->running_slideshow) {
|
2010-02-12 14:41:52 +00:00
|
|
|
rb->lcd_puts(0, 2, "decoding image");
|
2009-12-18 13:06:21 +00:00
|
|
|
rb->lcd_update();
|
|
|
|
}
|
2010-01-18 12:46:19 +00:00
|
|
|
#ifdef DISK_SPINDOWN
|
2010-11-21 13:47:56 +00:00
|
|
|
else if (iv->immediate_ata_off) {
|
2009-12-18 13:06:21 +00:00
|
|
|
/* running slideshow and time is long enough: power down disk */
|
|
|
|
rb->storage_sleep();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
/* Initialize decoder context struct, set buffer decoder is free
|
|
|
|
* to use.
|
|
|
|
* Decoder assumes that raw image file is loaded at the very
|
|
|
|
* end of the allocated buffer
|
|
|
|
*/
|
|
|
|
LodePNG_Decoder_init(p_decoder, memory, memory_size);
|
2009-12-18 13:06:21 +00:00
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
/* read file header; file is loaded at the end
|
|
|
|
* of the allocated buffer
|
|
|
|
*/
|
|
|
|
LodePNG_inspect(p_decoder, image, file_size);
|
2009-12-18 13:06:21 +00:00
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
if (!p_decoder->error) {
|
2009-12-18 13:06:21 +00:00
|
|
|
|
2010-11-21 13:47:56 +00:00
|
|
|
if (!iv->running_slideshow) {
|
2010-08-28 21:46:45 +00:00
|
|
|
rb->lcd_putsf(0, 2, "image %dx%d",
|
2010-10-31 12:40:49 +00:00
|
|
|
p_decoder->infoPng.width,
|
|
|
|
p_decoder->infoPng.height);
|
2010-08-28 21:46:45 +00:00
|
|
|
rb->lcd_putsf(0, 3, "decoding %d*%d",
|
2010-10-31 12:40:49 +00:00
|
|
|
p_decoder->infoPng.width,
|
|
|
|
p_decoder->infoPng.height);
|
2009-12-18 13:06:21 +00:00
|
|
|
rb->lcd_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the actual decoding */
|
|
|
|
time = *rb->current_tick;
|
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(true);
|
2010-11-21 13:47:56 +00:00
|
|
|
LodePNG_decode(p_decoder, image, file_size, iv->cb_progress);
|
2009-12-18 13:06:21 +00:00
|
|
|
rb->cpu_boost(false);
|
|
|
|
#else
|
2010-11-21 13:47:56 +00:00
|
|
|
LodePNG_decode(p_decoder, image, file_size, iv->cb_progress);
|
2009-12-18 13:06:21 +00:00
|
|
|
#endif /*HAVE_ADJUSTABLE_CPU_FREQ*/
|
2010-02-12 14:41:52 +00:00
|
|
|
time = *rb->current_tick - time;
|
2009-12-18 13:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-21 13:47:56 +00:00
|
|
|
if (!iv->running_slideshow && !p_decoder->error)
|
2009-12-18 13:06:21 +00:00
|
|
|
{
|
|
|
|
rb->snprintf(print, sizeof(print), " %ld.%02ld sec ", time/HZ, time%HZ);
|
|
|
|
rb->lcd_getstringsize(print, &w, &h); /* centered in progress bar */
|
|
|
|
rb->lcd_putsxy((LCD_WIDTH - w)/2, LCD_HEIGHT - h, print);
|
|
|
|
rb->lcd_update();
|
|
|
|
}
|
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
if (p_decoder->error) {
|
2011-01-19 13:20:00 +00:00
|
|
|
if (p_decoder->error == FILE_TOO_LARGE ||
|
|
|
|
p_decoder->error == OUT_OF_MEMORY)
|
|
|
|
{
|
2009-12-18 13:06:21 +00:00
|
|
|
return PLUGIN_OUTOFMEM;
|
2011-01-19 13:20:00 +00:00
|
|
|
}
|
2009-12-18 13:06:21 +00:00
|
|
|
|
2011-01-19 13:20:00 +00:00
|
|
|
if (LodePNG_perror(p_decoder) != NULL)
|
2009-12-18 13:06:21 +00:00
|
|
|
{
|
2010-10-31 12:40:49 +00:00
|
|
|
rb->splash(HZ, LodePNG_perror(p_decoder));
|
2009-12-18 13:06:21 +00:00
|
|
|
}
|
2011-01-19 13:20:00 +00:00
|
|
|
else if (p_decoder->error == TINF_DATA_ERROR)
|
|
|
|
{
|
|
|
|
rb->splash(HZ, "Zlib decompressor error");
|
|
|
|
}
|
2009-12-18 13:06:21 +00:00
|
|
|
else
|
|
|
|
{
|
2011-01-19 13:20:00 +00:00
|
|
|
rb->splashf(HZ, "other error : %ld", p_decoder->error);
|
2009-12-18 13:06:21 +00:00
|
|
|
}
|
|
|
|
|
2011-01-19 13:20:00 +00:00
|
|
|
return PLUGIN_ERROR;
|
2009-12-18 13:06:21 +00:00
|
|
|
}
|
2010-01-18 12:46:19 +00:00
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
info->x_size = p_decoder->infoPng.width;
|
|
|
|
info->y_size = p_decoder->infoPng.height;
|
|
|
|
|
2010-11-01 12:13:49 +00:00
|
|
|
p_decoder->native_img_size = (p_decoder->native_img_size + 3) & ~3;
|
|
|
|
disp_buf = p_decoder->buf + p_decoder->native_img_size;
|
|
|
|
*buf_size = memory_max - disp_buf;
|
2010-10-31 12:40:49 +00:00
|
|
|
|
2009-12-18 13:06:21 +00:00
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-02 12:03:58 +00:00
|
|
|
static int get_image(struct image_info *info, int frame, int ds)
|
2009-07-25 21:36:11 +00:00
|
|
|
{
|
2012-11-02 12:03:58 +00:00
|
|
|
(void)frame;
|
2010-11-01 12:13:49 +00:00
|
|
|
unsigned char **p_disp = &disp[ds]; /* short cut */
|
2010-10-31 12:40:49 +00:00
|
|
|
LodePNG_Decoder *p_decoder = &decoder;
|
2009-07-25 21:36:11 +00:00
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
info->width = p_decoder->infoPng.width / ds;
|
|
|
|
info->height = p_decoder->infoPng.height / ds;
|
2010-01-18 12:46:19 +00:00
|
|
|
info->data = p_disp;
|
2009-12-04 13:38:51 +00:00
|
|
|
|
2010-01-18 12:46:19 +00:00
|
|
|
if (*p_disp != NULL)
|
2009-07-25 21:36:11 +00:00
|
|
|
{
|
2010-01-18 12:46:19 +00:00
|
|
|
/* we still have it */
|
|
|
|
return PLUGIN_OK;
|
2009-07-25 21:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* assign image buffer */
|
|
|
|
if (ds > 1) {
|
2010-11-21 13:47:56 +00:00
|
|
|
if (!iv->running_slideshow)
|
2009-07-25 21:36:11 +00:00
|
|
|
{
|
2010-08-28 21:46:45 +00:00
|
|
|
rb->lcd_putsf(0, 3, "resizing %d*%d", info->width, info->height);
|
2009-07-25 21:36:11 +00:00
|
|
|
rb->lcd_update();
|
|
|
|
}
|
2009-12-18 13:06:21 +00:00
|
|
|
struct bitmap bmp_src, bmp_dst;
|
2009-07-25 21:36:11 +00:00
|
|
|
|
2010-11-01 12:13:49 +00:00
|
|
|
int size = img_mem(ds);
|
2009-12-04 13:38:51 +00:00
|
|
|
|
2010-11-01 12:13:49 +00:00
|
|
|
if (disp_buf + size >= p_decoder->buf + p_decoder->buf_size) {
|
2009-12-04 13:38:51 +00:00
|
|
|
/* have to discard the current */
|
|
|
|
int i;
|
|
|
|
for (i=1; i<=8; i++)
|
|
|
|
disp[i] = NULL; /* invalidate all bitmaps */
|
2010-10-31 12:40:49 +00:00
|
|
|
|
2009-12-04 13:38:51 +00:00
|
|
|
/* start again from the beginning of the buffer */
|
2010-11-01 12:13:49 +00:00
|
|
|
disp_buf = p_decoder->buf + p_decoder->native_img_size;
|
2009-12-04 13:38:51 +00:00
|
|
|
}
|
|
|
|
|
2010-01-18 12:46:19 +00:00
|
|
|
*p_disp = disp_buf;
|
2010-02-12 14:41:52 +00:00
|
|
|
disp_buf += size;
|
2009-07-25 21:36:11 +00:00
|
|
|
|
2010-10-31 12:40:49 +00:00
|
|
|
bmp_src.width = p_decoder->infoPng.width;
|
|
|
|
bmp_src.height = p_decoder->infoPng.height;
|
2010-11-01 12:13:49 +00:00
|
|
|
bmp_src.data = p_decoder->buf;
|
2009-07-25 21:36:11 +00:00
|
|
|
|
2010-01-18 12:46:19 +00:00
|
|
|
bmp_dst.width = info->width;
|
|
|
|
bmp_dst.height = info->height;
|
2010-11-01 12:13:49 +00:00
|
|
|
bmp_dst.data = *p_disp;
|
2009-07-25 21:36:11 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2009-12-04 13:38:51 +00:00
|
|
|
rb->cpu_boost(true);
|
2010-10-31 12:40:49 +00:00
|
|
|
resize_bitmap(&bmp_src, &bmp_dst);
|
2009-12-04 13:38:51 +00:00
|
|
|
rb->cpu_boost(false);
|
2009-07-25 21:36:11 +00:00
|
|
|
#else
|
2010-10-31 12:40:49 +00:00
|
|
|
resize_bitmap(&bmp_src, &bmp_dst);
|
2009-07-25 21:36:11 +00:00
|
|
|
#endif /*HAVE_ADJUSTABLE_CPU_FREQ*/
|
|
|
|
} else {
|
2010-11-01 12:13:49 +00:00
|
|
|
*p_disp = p_decoder->buf;
|
2009-12-18 13:06:21 +00:00
|
|
|
}
|
2009-07-25 21:36:11 +00:00
|
|
|
|
2010-01-18 12:46:19 +00:00
|
|
|
return PLUGIN_OK;
|
2009-07-25 21:36:11 +00:00
|
|
|
}
|
2010-11-21 13:47:56 +00:00
|
|
|
|
|
|
|
const struct image_decoder image_decoder = {
|
|
|
|
true,
|
|
|
|
img_mem,
|
|
|
|
load_image,
|
|
|
|
get_image,
|
|
|
|
draw_image_rect,
|
|
|
|
};
|
|
|
|
|
|
|
|
IMGDEC_HEADER
|