Index: apps/recorder/bmp.c =================================================================== --- apps/recorder/bmp.c (revision 18756) +++ apps/recorder/bmp.c (working copy) @@ -165,11 +165,500 @@ int read_bmp_file(const char* filename, return fd * 10 - 1; } +#if (LCD_DEPTH == 16) + /* no resize */ + bm->width = 0; + bm->height = 0; +#endif + ret = read_bmp_fd(fd, bm, maxsize, format); close(fd); return ret; } +#ifdef HAVE_ALBUMART +int get_bmp_size(int fd, struct bitmap *bmp) +{ + struct bmp_header bmph; + + int ret = read(fd, &bmph, sizeof(struct bmp_header)); + + if (ret < 0) + return ret * 10 - 2; + + if (ret != sizeof(struct bmp_header)) { + DEBUGF("get_bmp_size: can't read BMP header."); + return -3; + } + + bmp->width = readlong(&bmph.width); + bmp->height = readlong(&bmph.height); + + /* Top-down BMP file */ + if (bmp->height < 0) + bmp->height = -bmp->height; + + if (-1 == lseek(fd, -ret, SEEK_CUR)) + return 0; + + return get_bmp_file_size(bmp); +} +#endif /* HAVE_ALBUMART */ + +static int read_one_line(int fd, uint32_t *bmpb, int padded_width, + int width, int depth, uint32_t *palette) +{ + unsigned data, mask; + unsigned char *p; + uint16_t *p2; + uint32_t *rp; + int ret; + union rgb_union q0, q1; + + ret = read(fd, bmpb, padded_width); + if (ret != padded_width) { + DEBUGF("read_bmp_fd: error reading image, read returned: %d " + "expected: %d\n", ret, padded_width); + return -9; + } + + /* convert whole line in-place to XRGB8888 (little endian) */ + rp = bmpb + width; + switch (depth) { + case 1: + q0.raw = palette[0]; + q1.raw = palette[1]; + p = (unsigned char*)bmpb + ((width + 7) >> 3); + mask = 0x80 >> ((width + 7) & 7); + while (p > (unsigned char*)bmpb) { + data = *(--p); + for (; mask <= 0x80; mask <<= 1) + *(--rp) = (data & mask) ? q1.raw : q0.raw; + mask = 0x01; + } + break; + + case 4: + if (width & 1) + rp++; + p = (unsigned char*)bmpb + ((width + 1) >> 1); + while (p > (unsigned char*)bmpb) { + data = *(--p); + *(--rp) = palette[data & 0x0f]; + *(--rp) = palette[data >> 4]; + } + break; + + case 8: + p = (unsigned char*)bmpb + width; + while (p > (unsigned char*)bmpb) + *(--rp) = palette[*(--p)]; + break; + + case 15: + case 16: + p2 = (uint16_t *)bmpb + width; + while (p2 > (uint16_t *)bmpb) { + unsigned component, rgb; + + data = letoh16(*(--p2)); + /* blue */ + component = (data << 3) & 0xf8; +#ifdef ROCKBOX_BIG_ENDIAN + rgb = (component | (component >> 5)) << 8; + /* green */ + data >>= 2; + if (depth == 15) { + component = data & 0xf8; + rgb |= component | (component >> 5); + } else { + data >>= 1; + component = data & 0xfc; + rgb |= component | (component >> 6); + } + /* red */ + data >>= 5; + component = data & 0xf8; + rgb = (rgb << 8) | component | (component >> 5); + *(--rp) = rgb << 8; +#else /* little endian */ + rgb = component | (component >> 5); + /* green */ + data >>= 2; + if (depth == 15) { + component = data & 0xf8; + rgb |= (component | (component >> 5)) << 8; + } else { + data >>= 1; + component = data & 0xfc; + rgb |= (component | (component >> 6)) << 8; + } + /* red */ + data >>= 5; + component = data & 0xf8; + rgb |= (component | (component >> 5)) << 16; + *(--rp) = rgb; +#endif + } + break; + + case 24: + p = (unsigned char*)bmpb + 3 * width; + while (p > (unsigned char*)bmpb) { + data = *(--p); + data = (data << 8) | *(--p); + data = (data << 8) | *(--p); + *(--rp) = htole32(data); + } + break; + + case 32: /* already in desired format */ + break; + } + return 0; +} + +#if (LCD_DEPTH == 16) +# define LIMIT_WIDTH (500) +#else +# define LIMIT_WIDTH LCD_WIDTH +#endif + +#if (LCD_WIDTH < LIMIT_WIDTH) +# define MAX_WIDTH LIMIT_WIDTH +#else +# define MAX_WIDTH LCD_WIDTH +#endif + +#if (LCD_DEPTH == 16) +struct cache_line_t { + uint32_t bmpbuf[MAX_WIDTH]; + int y; +}; + +#define CACHE_SIZE 5 +static struct cache_line_t cache_line[CACHE_SIZE]; +static int cache_line_index = 0; +#define ARRAY_SIZE(array) (int)(sizeof(array)/(sizeof(array[0]))) + +#define PACKRED(r, delata) (31 * r + (r >> 3) + delta) >> 8; +#define PACKGREEN(g, delta) (63 * g + (g >> 2) + delta) >> 8; +#define PACKBLUE(b, delta) (31 * b + (b >> 3) + delta) >> 8; + +static union rgb_union *read_cache_line(int fd, int y, int padded_width, + int width, int depth, uint32_t *palette) +{ + union rgb_union *qp = NULL; + int i = 0; + + for (i = 0; i < ARRAY_SIZE(cache_line); i++) { + if (y == cache_line[i].y) { + qp = (union rgb_union *)cache_line[i].bmpbuf; + cache_line_index = i + 1; + + if (cache_line_index == ARRAY_SIZE(cache_line)) + cache_line_index = 0; + + return qp; /* cache found */ + } + } + + /* cache missed, so read one line from disk.. */ + if (0 != read_one_line(fd, (uint32_t *)cache_line[cache_line_index].bmpbuf, + padded_width, width, depth, palette)) + return NULL; + + cache_line[cache_line_index].y = y; + qp = (union rgb_union *)cache_line[cache_line_index].bmpbuf; + cache_line_index++; + if (cache_line_index == ARRAY_SIZE(cache_line)) + cache_line_index = 0; + + return qp; +} + +struct tmp_rgb_t { + int r; + int g; + int b; +}; + +static void downscale_help(union rgb_union *pix, int xap, int Cx, int Cy, + struct tmp_rgb_t *rgb) +{ + int i; + int rx = (pix->red * xap) >> 9; + int gx = (pix->green * xap) >> 9; + int bx = (pix->blue * xap) >> 9; + pix++; + for (i = (1 << 14) - xap; i > Cx; i -= Cx) { + rx += (pix->red * Cx) >> 9; + gx += (pix->green * Cx) >> 9; + bx += (pix->blue * Cx) >> 9; + pix++; + } + if (i > 0) { + rx += (pix->red * i) >> 9; + gx += (pix->green * i) >> 9; + bx += (pix->blue * i) >> 9; + } + + rgb->r = (rx * Cy) >> 14; + rgb->g = (gx * Cy) >> 14; + rgb->b = (bx * Cy) >> 14; +} + +/* + * This algorithm is taken from app/plugins/lib/bmp_smooth_scale.c + * And that is take from Imlib2 + */ +static int resize_on_load(int fd, int padded_width, int depth, bool dither, + uint32_t *palette, unsigned char *bitmap, + int src_width, int src_height, + int dst_width, int dst_height, + int rowstart, int rowstop, int rowstep) +{ + const int sw = src_width; + const int sh = src_height; + const int dw = dst_width; + const int dh = dst_height; + const int inc_x = (sw << 16) / dw; + const int inc_y = (sh << 16) / dh; + const int Cp_x = ((dw << 14) / sw) + 1; + const int Cp_y = ((dh << 14) / sh) + 1; + const int xup_yup = (dw >= sw) + ((dh >= sh) << 1); + int XAP, YAP, INV_YAP, INV_XAP; + int end = dw, x, y, val_y = 0, val_x, xpoint; + union rgb_union *ypoint; + int i = 0; + + if (rowstep == -1) { + rowstart = dh - 1; + rowstop = -1; + } else { + rowstart = 0; + rowstop = dh; + } + + cache_line_index=0; + for (i = 0; i < ARRAY_SIZE(cache_line); i++) + cache_line[i].y = -1; + + for (y = rowstart; y != rowstop; y += rowstep) { + union rgb_union *qp, *qp2; + int delta = 127; + + if (xup_yup == 3) { + /* upscaling both */ + int tmp_y = val_y >> 16; + fb_data *dest = (fb_data *)bitmap + y * dw; + + if ((qp = read_cache_line(fd, tmp_y, padded_width, sw, + depth, palette)) == NULL) + goto out; + + ypoint = qp; + YAP = ((val_y >> 16) >= (sh - 1)) ? 0 : + (val_y >> 8) - ((val_y >> 8) & 0xffffff00); + INV_YAP = 256 - YAP; + val_y += inc_y; + val_x = 0; + if (YAP > 0) { + for (x = 0; x < dw; x++) { + int r = 0, g = 0, b = 0; + int rr = 0, gg = 0, bb = 0; + union rgb_union *pix; + if (dither) + delta = dither_matrix[y & 0xf][x & 0xf]; + xpoint = (val_x >> 16); + XAP = ((val_x >> 16) >= (sw - 1)) ? 0 : + (val_x >> 8) - ((val_x >> 8) & 0xffffff00); + INV_XAP = 256 - XAP; + val_x += inc_x; + + if (XAP > 0) { + pix = qp + xpoint; + r = pix->red * INV_XAP; + g = pix->green * INV_XAP; + b = pix->blue * INV_XAP; + pix++; + r += pix->red * XAP; + g += pix->green * XAP; + b += pix->blue * XAP; + + if ((qp2 = read_cache_line(fd, tmp_y + 1, padded_width, + sw, depth, palette)) == NULL) + goto out; + + + pix = qp2 + xpoint; + rr = pix->red * INV_XAP; + gg = pix->green * INV_XAP; + bb = pix->blue * INV_XAP; + pix++; + rr += pix->red * XAP; + gg += pix->green * XAP; + bb += pix->blue * XAP; + r = ((rr * YAP) + (r * INV_YAP)) >> 16; + g = ((gg * YAP) + (g * INV_YAP)) >> 16; + b = ((bb * YAP) + (b * INV_YAP)) >> 16; + r = PACKRED(r, delta); + g = PACKGREEN(g, delta); + b = PACKBLUE(b, delta); + *dest++ = LCD_RGBPACK_LCD(r, g, b); + } else { + pix = qp + xpoint; + r = pix->red * INV_YAP; + g = pix->green * INV_YAP; + b = pix->blue * INV_YAP; + + if ((qp2 = read_cache_line(fd, tmp_y + 1, padded_width, + sw, depth, palette)) == NULL) + goto out; + + pix = qp2 + xpoint; + r += pix->red * YAP; + g += pix->green * YAP; + b += pix->blue * YAP; + r >>= 8; + g >>= 8; + b >>= 8; + r = PACKRED(r, delta); + g = PACKGREEN(g, delta); + b = PACKBLUE(b, delta); + *dest++ = LCD_RGBPACK_LCD(r, g, b); + } + } + } else { + for (x = 0; x < dw; x++) { + int r = 0, g = 0, b = 0; + union rgb_union *pix; + if (dither) + delta = dither_matrix[y & 0xf][x & 0xf]; + xpoint = (val_x >> 16); + XAP = ((val_x >> 16) >= (sw - 1)) ? 0 : + (val_x >> 8) - ((val_x >> 8) & 0xffffff00); + INV_XAP = 256 - XAP; + val_x += inc_x; + + if (XAP > 0) { + pix = qp + xpoint; + r = pix->red * INV_XAP; + g = pix->green * INV_XAP; + b = pix->blue * INV_XAP; + pix++; + r += pix->red * XAP; + g += pix->green * XAP; + b += pix->blue * XAP; + r >>= 8; + g >>= 8; + b >>= 8; + r = PACKRED(r, delta); + g = PACKGREEN(g, delta); + b = PACKBLUE(b, delta); + *dest++ = LCD_RGBPACK_LCD(r, g, b); + } else { + pix = qp; + r = pix->red; + g = pix->green; + b = pix->blue; + r = PACKRED(r, delta); + g = PACKGREEN(g, delta); + b = PACKBLUE(b, delta); + *dest++ = LCD_RGBPACK_LCD(r, g, b); + } + } + } + } else if (xup_yup == 0) { + /* downscaling both */ + int Cx, Cy, j; + union rgb_union *pix; + int r, g, b; + int xap, yap; + fb_data *dest = (fb_data *)bitmap + y * dw; + + if ((qp = read_cache_line(fd, val_y >> 16, padded_width, sw, depth, + palette)) == NULL) + goto out; + + ypoint = qp; + YAP = (((0x100 - ((val_y >> 8) & 0xff)) * Cp_y) >> 8) | (Cp_y << 16); + INV_YAP = 256 - YAP; + val_y += inc_y; + val_x = 0; + + Cy = YAP >> 16; + yap = YAP & 0xffff; + + for (x = 0; x < end; x++) { + struct tmp_rgb_t tmp_rgb; + int tmp_y = (val_y-inc_y) >> 16; + if (dither) + delta = dither_matrix[y & 0xf][x & 0xf]; + xpoint = (val_x >> 16); + XAP = (((0x100 - ((val_x >> 8) & 0xff)) * Cp_x) >> 8) | (Cp_x << 16); + INV_XAP = 256 - XAP; + val_x += inc_x; + + Cx = XAP >> 16; + xap = XAP & 0xffff; + + pix = qp + xpoint; + tmp_y++; + + downscale_help(pix, xap, Cx, yap, &tmp_rgb); + r = tmp_rgb.r; + g = tmp_rgb.g; + b = tmp_rgb.b; + + for (j = (1 << 14) - yap; j > Cy; j -= Cy) { + if ((qp = read_cache_line(fd, tmp_y, padded_width, sw, + depth, palette)) == NULL) + goto out; + + pix = qp + xpoint; + tmp_y++; + + downscale_help(pix, xap, Cx, Cy, &tmp_rgb); + r += tmp_rgb.r; + g += tmp_rgb.g; + b += tmp_rgb.b; + } + if (j > 0) { + + if ((qp = read_cache_line(fd, tmp_y, padded_width, sw, + depth, palette)) == NULL) + goto out; + + pix = qp + xpoint; + tmp_y++; + + downscale_help(pix, xap, Cx, j, &tmp_rgb); + r += tmp_rgb.r; + g += tmp_rgb.g; + b += tmp_rgb.b; + } + + r = PACKRED(r, delta); + g = PACKGREEN(g, delta); + b = PACKBLUE(b, delta); + *dest = LCD_RGBPACK_LCD(r >> 5, g >> 5, b >> 5); + dest++; + } + } else { + DEBUGF("error: xup_yup = %d is not implemented!!\n", xup_yup); + goto out; + } + } + + /* return the used buffer size. */ + return dst_width * dst_height * sizeof(fb_data); + +out: + return -1; +} +#endif /* (LCD_DEPTH == 16) */ + /****************************************************************************** * read_bmp_fd() * @@ -190,7 +679,11 @@ int read_bmp_fd(int fd, int rowstart, rowstop, rowstep; unsigned char *bitmap = bm->data; +#if (LCD_DEPTH == 16) + bool resize = false; +#else uint32_t bmpbuf[LCD_WIDTH]; /* Buffer for one line */ +#endif uint32_t palette[256]; #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) bool transparent = false; @@ -207,6 +700,12 @@ int read_bmp_fd(int fd, #endif } #endif /* HAVE_REMOTE_LCD */ + +#if (LCD_DEPTH == 16) + if ((bm->height > 0) && (bm->width > 0)) + resize = true; +#endif + if (format & FORMAT_TRANSPARENT) { transparent = true; format &= ~FORMAT_TRANSPARENT; @@ -232,9 +731,9 @@ int read_bmp_fd(int fd, } width = readlong(&bmph.width); - if (width > LCD_WIDTH) { + if (width > MAX_WIDTH) { DEBUGF("read_bmp_fd: Bitmap too wide (%d pixels, max is %d)\n", - width, LCD_WIDTH); + width, MAX_WIDTH); return -4; } @@ -262,9 +761,21 @@ int read_bmp_fd(int fd, } bm->format = format; #endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */ - /* returning image size */ - bm->width = width; - bm->height = height; +#if (LCD_DEPTH == 16) + if (resize) { + if ((bm->width * CACHE_SIZE < width) || + (bm->height * CACHE_SIZE < height)) { + DEBUGF("This bitmap is too large to resize!\n"); + resize = false; + } + } + + if (!resize) { + /* returning image size */ + bm->width = width; + bm->height = height; + } +#endif #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) if (format == FORMAT_NATIVE) { @@ -292,8 +803,11 @@ int read_bmp_fd(int fd, #elif LCD_DEPTH == 16 dst_width = width; dst_height = height; + if (resize) + totalsize = bm->width * bm->height * sizeof(fb_data); + else #endif /* LCD_DEPTH */ - totalsize = dst_width * dst_height * sizeof(fb_data); + totalsize = dst_width * dst_height * sizeof(fb_data); } } else #endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */ @@ -368,119 +882,37 @@ int read_bmp_fd(int fd, memset(bitmap, 0, totalsize); +#if (LCD_DEPTH == 16) + if ((width == bm->width) && (height == bm->height)) + resize = false; + + if (format != FORMAT_NATIVE) + resize = false; + + if (resize) + return resize_on_load(fd, padded_width, depth, dither, palette, + bitmap, width, height, bm->width, bm->height, + rowstart, rowstop, rowstep); + totalsize = dst_width * dst_height * sizeof(fb_data); +#endif + + uint32_t *bmpb; +#if (LCD_DEPTH == 16) + bmpb = cache_line[0].bmpbuf; +#else + bmpb = bmpbuf; +#endif /* loop to read rows and put them to buffer */ for (row = rowstart; row != rowstop; row += rowstep) { - unsigned data, mask; + unsigned mask; unsigned char *p; - uint16_t *p2; - uint32_t *rp; union rgb_union *qp; - union rgb_union q0, q1; - /* read one row */ - ret = read(fd, bmpbuf, padded_width); - if (ret != padded_width) { - DEBUGF("read_bmp_fd: error reading image, read returned: %d " - "expected: %d\n", ret, padded_width); + if (0 != read_one_line(fd, bmpb, padded_width, width, depth, palette)) return -9; - } - - /* convert whole line in-place to XRGB8888 (little endian) */ - rp = bmpbuf + width; - switch (depth) { - case 1: - q0.raw = palette[0]; - q1.raw = palette[1]; - p = (unsigned char*)bmpbuf + ((width + 7) >> 3); - mask = 0x80 >> ((width + 7) & 7); - while (p > (unsigned char*)bmpbuf) { - data = *(--p); - for (; mask <= 0x80; mask <<= 1) - *(--rp) = (data & mask) ? q1.raw : q0.raw; - mask = 0x01; - } - break; - - case 4: - if (width & 1) - rp++; - p = (unsigned char*)bmpbuf + ((width + 1) >> 1); - while (p > (unsigned char*)bmpbuf) { - data = *(--p); - *(--rp) = palette[data & 0x0f]; - *(--rp) = palette[data >> 4]; - } - break; - - case 8: - p = (unsigned char*)bmpbuf + width; - while (p > (unsigned char*)bmpbuf) - *(--rp) = palette[*(--p)]; - break; - - case 15: - case 16: - p2 = (uint16_t *)bmpbuf + width; - while (p2 > (uint16_t *)bmpbuf) { - unsigned component, rgb; - - data = letoh16(*(--p2)); - /* blue */ - component = (data << 3) & 0xf8; -#ifdef ROCKBOX_BIG_ENDIAN - rgb = (component | (component >> 5)) << 8; - /* green */ - data >>= 2; - if (depth == 15) { - component = data & 0xf8; - rgb |= component | (component >> 5); - } else { - data >>= 1; - component = data & 0xfc; - rgb |= component | (component >> 6); - } - /* red */ - data >>= 5; - component = data & 0xf8; - rgb = (rgb << 8) | component | (component >> 5); - *(--rp) = rgb << 8; -#else /* little endian */ - rgb = component | (component >> 5); - /* green */ - data >>= 2; - if (depth == 15) { - component = data & 0xf8; - rgb |= (component | (component >> 5)) << 8; - } else { - data >>= 1; - component = data & 0xfc; - rgb |= (component | (component >> 6)) << 8; - } - /* red */ - data >>= 5; - component = data & 0xf8; - rgb |= (component | (component >> 5)) << 16; - *(--rp) = rgb; -#endif - } - break; - - case 24: - p = (unsigned char*)bmpbuf + 3 * width; - while (p > (unsigned char*)bmpbuf) { - data = *(--p); - data = (data << 8) | *(--p); - data = (data << 8) | *(--p); - *(--rp) = htole32(data); - } - break; - - case 32: /* already in desired format */ - break; - } /* Convert to destination format */ - qp = (union rgb_union *)bmpbuf; + qp = (union rgb_union *)bmpb; #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) if (format == FORMAT_NATIVE) { #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1 @@ -562,6 +994,7 @@ int read_bmp_fd(int fd, fb_data *dest = (fb_data *)bitmap + dst_width * row; int delta = 127; unsigned r, g, b; + union rgb_union q0; for (col = 0; col < width; col++) { if (dither) Index: apps/recorder/bmp.h =================================================================== --- apps/recorder/bmp.h (revision 18756) +++ apps/recorder/bmp.h (working copy) @@ -23,6 +23,7 @@ #include "config.h" #include "lcd.h" +#include "debug.h" /********************************************************************* * read_bmp_file() @@ -41,4 +42,33 @@ int read_bmp_fd(int fd, struct bitmap *bm, int maxsize, int format); + +#ifdef HAVE_ALBUMART +static inline int get_bmp_file_size(struct bitmap *bmp) +{ + int width = bmp->width, height = bmp->height; + int dst_width, dst_height; + +#if LCD_DEPTH == 2 +#if LCD_PIXELFORMAT == HORIZONTAL_PACKING + dst_width = (width + 3) >> 2; + dst_height = height; +#elif LCD_PIXELFORMAT == VERTICAL_PACKING + dst_width = width; + dst_height = (height + 3) >> 2; +#elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED + dst_width = width; + dst_height = (height + 7) >> 3; +#endif /* LCD_PIXELFORMAT */ +#elif LCD_DEPTH == 16 + dst_width = width; + dst_height = height; +#endif /* LCD_DEPTH */ + + return dst_width * dst_height * sizeof(fb_data); +} + +int get_bmp_size(int fd, + struct bitmap *bmp); +#endif /* HAVE_ALBUMART */ #endif Index: apps/recorder/albumart.c =================================================================== --- apps/recorder/albumart.c (revision 18756) +++ apps/recorder/albumart.c (working copy) @@ -30,7 +30,9 @@ #include "debug.h" #include "misc.h" #include "settings.h" +#include "bmp.h" +#define SCALE 1000 /* Strip filename from a full path * @@ -297,3 +299,39 @@ void draw_album_art(struct gui_wps *gwps gwps->display->set_drawmode(DRMODE_SOLID); } } + +int get_albumart_size(struct bitmap *dst_bmp, struct bitmap *src_bmp) +{ + struct wps_data *data = gui_wps[0].data; + + short aa_max_width = data->albumart_max_width; + short aa_max_height = data->albumart_max_height; + const short width = src_bmp->width; + const short height = src_bmp->height; + + if (aa_max_width < 0 || aa_max_height < 0 || width <= 0 || height <= 0) + return 0; + + if (aa_max_width == 0) + aa_max_width = width; + if (aa_max_height == 0) + aa_max_height = height; + + if ((aa_max_width == width) && (aa_max_width == width)) { + dst_bmp->width = width; + dst_bmp->height = height; + } else { + /* + * keep aspect ratio + * because resize fanction doesn't keep aspect rate + */ + const unsigned short factx = aa_max_width * SCALE / width; + const unsigned short facty = aa_max_height * SCALE / height; + const unsigned short fact = MIN(factx, facty); + + dst_bmp->width = (width * fact + SCALE/2) / SCALE; /* round off */ + dst_bmp->height = (height * fact + SCALE/2) / SCALE; /* round off */ + } + + return get_bmp_file_size(dst_bmp); +} Index: apps/recorder/albumart.h =================================================================== --- apps/recorder/albumart.h (revision 18756) +++ apps/recorder/albumart.h (working copy) @@ -40,6 +40,8 @@ void draw_album_art(struct gui_wps *gwps bool search_albumart_files(const struct mp3entry *id3, const char *size_string, char *buf, int buflen); +int get_albumart_size(struct bitmap *dst_bmp, struct bitmap *src_bmp); + #endif /* HAVE_ALBUMART */ #endif /* _ALBUMART_H_ */ Index: apps/gui/gwps.h =================================================================== --- apps/gui/gwps.h (revision 18756) +++ apps/gui/gwps.h (working copy) @@ -47,13 +47,11 @@ #define WPS_ALBUMART_CHECK 1 /* WPS contains AA conditional tag */ #define WPS_ALBUMART_LOAD 2 /* WPS contains AA tag */ -#define WPS_ALBUMART_ALIGN_RIGHT WPS_ALIGN_RIGHT /* x align: right */ -#define WPS_ALBUMART_ALIGN_CENTER WPS_ALIGN_CENTER /* x/y align: center */ -#define WPS_ALBUMART_ALIGN_LEFT WPS_ALIGN_LEFT /* x align: left */ -#define WPS_ALBUMART_ALIGN_TOP WPS_ALIGN_RIGHT /* y align: top */ -#define WPS_ALBUMART_ALIGN_BOTTOM WPS_ALIGN_LEFT /* y align: bottom */ -#define WPS_ALBUMART_INCREASE 8 /* increase if smaller */ -#define WPS_ALBUMART_DECREASE 16 /* decrease if larger */ +#define WPS_ALBUMART_ALIGN_RIGHT 1 /* x align: right */ +#define WPS_ALBUMART_ALIGN_CENTER 2 /* x/y align: center */ +#define WPS_ALBUMART_ALIGN_LEFT 4 /* x align: left */ +#define WPS_ALBUMART_ALIGN_TOP 1 /* y align: top */ +#define WPS_ALBUMART_ALIGN_BOTTOM 4 /* y align: bottom */ #endif /* HAVE_ALBUMART */ @@ -382,10 +380,8 @@ struct wps_data unsigned char wps_uses_albumart; /* WPS_ALBUMART_NONE, _CHECK, _LOAD */ short albumart_x; short albumart_y; - unsigned short albumart_xalign; /* WPS_ALBUMART_ALIGN_LEFT, _CENTER, _RIGHT, - + .._INCREASE, + .._DECREASE */ - unsigned short albumart_yalign; /* WPS_ALBUMART_ALIGN_TOP, _CENTER, _BOTTOM, - + .._INCREASE, + .._DECREASE */ + unsigned char albumart_xalign; /* WPS_ALBUMART_ALIGN_LEFT, _CENTER, _RIGHT */ + unsigned char albumart_yalign; /* WPS_ALBUMART_ALIGN_TOP, _CENTER, _BOTTOM */ short albumart_max_width; short albumart_max_height; Index: apps/gui/wps_parser.c =================================================================== --- apps/gui/wps_parser.c (revision 18756) +++ apps/gui/wps_parser.c (working copy) @@ -898,13 +898,6 @@ static int parse_albumart_load(const cha { const char *_pos, *newline; bool parsing; - const short xalign_mask = WPS_ALBUMART_ALIGN_LEFT | - WPS_ALBUMART_ALIGN_CENTER | - WPS_ALBUMART_ALIGN_RIGHT; - const short yalign_mask = WPS_ALBUMART_ALIGN_TOP | - WPS_ALBUMART_ALIGN_CENTER | - WPS_ALBUMART_ALIGN_BOTTOM; - (void)token; /* silence warning */ /* reset albumart info in wps */ @@ -914,7 +907,7 @@ static int parse_albumart_load(const cha wps_data->albumart_xalign = WPS_ALBUMART_ALIGN_CENTER; /* default */ wps_data->albumart_yalign = WPS_ALBUMART_ALIGN_CENTER; /* default */ - /* format: %Cl|x|y|[[l|c|r][d|i|s]mwidth]|[[t|c|b][d|i|s]mheight]| */ + /* format: %Cl|x|y|[[l|c|r]mwidth]|[[t|c|b]mheight]| */ newline = strchr(wps_bufptr, '\n'); @@ -949,35 +942,24 @@ static int parse_albumart_load(const cha case 'l': case 'L': case '+': - wps_data->albumart_xalign = - (wps_data->albumart_xalign & xalign_mask) | - WPS_ALBUMART_ALIGN_LEFT; + wps_data->albumart_xalign = WPS_ALBUMART_ALIGN_LEFT; break; case 'c': case 'C': - wps_data->albumart_xalign = - (wps_data->albumart_xalign & xalign_mask) | - WPS_ALBUMART_ALIGN_CENTER; + wps_data->albumart_xalign = WPS_ALBUMART_ALIGN_CENTER; break; case 'r': case 'R': case '-': - wps_data->albumart_xalign = - (wps_data->albumart_xalign & xalign_mask) | - WPS_ALBUMART_ALIGN_RIGHT; + wps_data->albumart_xalign = WPS_ALBUMART_ALIGN_RIGHT; break; case 'd': case 'D': - wps_data->albumart_xalign |= WPS_ALBUMART_DECREASE; - break; case 'i': case 'I': - wps_data->albumart_xalign |= WPS_ALBUMART_INCREASE; - break; case 's': case 'S': - wps_data->albumart_xalign |= - (WPS_ALBUMART_DECREASE | WPS_ALBUMART_INCREASE); + /* simply ignored */ break; default: parsing = false; @@ -1009,35 +991,24 @@ static int parse_albumart_load(const cha case 't': case 'T': case '-': - wps_data->albumart_yalign = - (wps_data->albumart_yalign & yalign_mask) | - WPS_ALBUMART_ALIGN_TOP; + wps_data->albumart_yalign = WPS_ALBUMART_ALIGN_TOP; break; case 'c': case 'C': - wps_data->albumart_yalign = - (wps_data->albumart_yalign & yalign_mask) | - WPS_ALBUMART_ALIGN_CENTER; + wps_data->albumart_yalign = WPS_ALBUMART_ALIGN_CENTER; break; case 'b': case 'B': case '+': - wps_data->albumart_yalign = - (wps_data->albumart_yalign & yalign_mask) | - WPS_ALBUMART_ALIGN_BOTTOM; + wps_data->albumart_yalign = WPS_ALBUMART_ALIGN_BOTTOM; break; case 'd': case 'D': - wps_data->albumart_yalign |= WPS_ALBUMART_DECREASE; - break; case 'i': case 'I': - wps_data->albumart_yalign |= WPS_ALBUMART_INCREASE; - break; case 's': case 'S': - wps_data->albumart_yalign |= - (WPS_ALBUMART_DECREASE | WPS_ALBUMART_INCREASE); + /* simply ignored */ break; default: parsing = false; Index: apps/buffering.c =================================================================== --- apps/buffering.c (revision 18756) +++ apps/buffering.c (working copy) @@ -50,6 +50,9 @@ #include "pcmbuf.h" #include "buffer.h" #include "bmp.h" +#ifdef HAVE_ALBUMART +#include "albumart.h" +#endif #include "events.h" #include "metadata.h" @@ -843,18 +846,33 @@ static bool fill_buffer(void) Return value is the total size (struct + data). */ static int load_bitmap(int fd) { - int rc; - struct bitmap *bmp = (struct bitmap *)&buffer[buf_widx]; + struct bitmap *aa_bmp, orig_bmp; + int orig_sz, aa_sz, free; /* bytes */ + + orig_sz = get_bmp_size(fd, &orig_bmp); + + if (orig_sz <= 0) + return orig_sz; + + aa_bmp = (struct bitmap *)&buffer[buf_widx]; + free = (int)MIN(buffer_len - BUF_USED, buffer_len - buf_widx) + - sizeof(struct bitmap); + + aa_sz = get_albumart_size(aa_bmp, &orig_bmp); + + if ((aa_sz > free) || aa_sz <= 0) + return 0; + /* FIXME: alignment may be needed for the data buffer. */ - bmp->data = &buffer[buf_widx + sizeof(struct bitmap)]; + aa_bmp->data = &buffer[buf_widx + sizeof(struct bitmap)]; #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) - bmp->maskdata = NULL; + aa_bmp->maskdata = NULL; #endif - int free = (int)MIN(buffer_len - BUF_USED, buffer_len - buf_widx); - rc = read_bmp_fd(fd, bmp, free, FORMAT_ANY|FORMAT_DITHER); - return rc + (rc > 0 ? sizeof(struct bitmap) : 0); + aa_sz = read_bmp_fd(fd, aa_bmp, free, FORMAT_ANY|FORMAT_DITHER); + + return aa_sz + (aa_sz > 0 ? sizeof(struct bitmap) : 0); } #endif