Index: apps/recorder/albumart.c =================================================================== --- apps/recorder/albumart.c (revision 16133) +++ apps/recorder/albumart.c (working copy) @@ -224,6 +224,41 @@ bool find_albumart(const struct mp3entry return search_albumart_files(id3, size_string, buf, buflen); } +/** + * Very simple image scale from src to dst (nearest neighbour). + * Source and destination dimensions are read from the struct bitmap. + * */ +static void simple_resize_bitmap(struct bitmap *src, struct bitmap *dst) +{ + const int srcw = src->width; + const int srch = src->height; + const int dstw = dst->width; + const int dsth = dst->height; + const fb_data *srcd = (fb_data*)(src->data); + const fb_data *dstd = (fb_data*)(dst->data); + + const long xrstep = ((srcw-1) << 8) / (dstw-1); + const long yrstep = ((srch-1) << 8) / (dsth-1); + fb_data *src_row, *dst_row; + long xr, yr = 0; + int src_x, src_y, dst_x, dst_y; + for (dst_y=0; dst_y < dsth; dst_y++) + { + src_y = (yr >> 8); + src_row = (fb_data*)&srcd[src_y * srcw]; + dst_row = (fb_data*)&dstd[dst_y * dstw]; + for (xr=0,dst_x=0; dst_x < dstw; dst_x++) + { + src_x = (xr >> 8); + dst_row[dst_x] = src_row[src_x]; + xr += xrstep; + } + yr += yrstep; + } +} + +#define SCALE 1000 + /* Draw the album art bitmap from the given handle ID onto the given WPS. Call with clear = true to clear the bitmap instead of drawing it. */ void draw_album_art(struct gui_wps *gwps, int handle_id, bool clear) @@ -239,7 +274,8 @@ void draw_album_art(struct gui_wps *gwps return; #endif - struct bitmap *bmp; + struct bitmap *bmp, output_bmp; + int output_size = 0; if (bufgetdata(handle_id, 0, (void *)&bmp) <= 0) return; @@ -247,29 +283,77 @@ void draw_album_art(struct gui_wps *gwps short y = data->albumart_y; short width = bmp->width; short height = bmp->height; + short aa_max_width = data->albumart_max_width; + short aa_max_height = data->albumart_max_height; + unsigned short aa_xalign = data->albumart_xalign; + unsigned short aa_yalign = data->albumart_yalign; + + if (((aa_xalign & WPS_ALBUMART_DECREASE) && (aa_max_width < width)) || + ((aa_xalign & WPS_ALBUMART_INCREASE) && (aa_max_width > width)) || + ((aa_yalign & WPS_ALBUMART_DECREASE) && (aa_max_height < height)) || + ((aa_yalign & WPS_ALBUMART_INCREASE) && (aa_max_height > height))) + { + unsigned long factx = aa_max_width * SCALE / width; + unsigned long facty = aa_max_height * SCALE / height; + unsigned long fact = MIN(factx, facty); + + /* resize image */ + short tmp_width = (width * fact + SCALE/2) / SCALE; + short tmp_height = (height * fact + SCALE/2) / SCALE; + + if (tmp_width != width || tmp_height != height) + { + output_bmp.width = tmp_width; + output_bmp.height = tmp_height; + output_bmp.format = bmp->format; + output_bmp.maskdata = NULL; + + if (!clear) + { + output_bmp.data = data->img_buf_ptr; + + output_size = tmp_width * tmp_height * sizeof(fb_data); + if (output_size > data->img_buf_free) + { + /* Can't allocate the resized album art */ + output_size = 0; + goto out; + } + data->img_buf_ptr += output_size; + data->img_buf_free -= output_size; + + simple_resize_bitmap(bmp, &output_bmp); + } + + /* Resized bitmap is now in the output buffer, + copy it back to the bmp buffer */ + bmp = &output_bmp; + } + } +out: - if (data->albumart_max_width > 0) + if (aa_max_width > 0) { /* Crop if the bitmap is too wide */ - width = MIN(bmp->width, data->albumart_max_width); + width = MIN(bmp->width, aa_max_width); /* Align */ - if (data->albumart_xalign & WPS_ALBUMART_ALIGN_RIGHT) - x += data->albumart_max_width - width; - else if (data->albumart_xalign & WPS_ALBUMART_ALIGN_CENTER) - x += (data->albumart_max_width - width) / 2; + if (aa_xalign & WPS_ALBUMART_ALIGN_RIGHT) + x += aa_max_width - width; + else if (aa_xalign & WPS_ALBUMART_ALIGN_CENTER) + x += (aa_max_width - width) / 2; } - if (data->albumart_max_height > 0) + if (aa_max_height > 0) { /* Crop if the bitmap is too high */ - height = MIN(bmp->height, data->albumart_max_height); + height = MIN(bmp->height, aa_max_height); /* Align */ - if (data->albumart_yalign & WPS_ALBUMART_ALIGN_BOTTOM) - y += data->albumart_max_height - height; - else if (data->albumart_yalign & WPS_ALBUMART_ALIGN_CENTER) - y += (data->albumart_max_height - height) / 2; + if (aa_yalign & WPS_ALBUMART_ALIGN_BOTTOM) + y += aa_max_height - height; + else if (aa_yalign & WPS_ALBUMART_ALIGN_CENTER) + y += (aa_max_height - height) / 2; } if (!clear) @@ -279,6 +363,9 @@ void draw_album_art(struct gui_wps *gwps gwps->display->bitmap_part((fb_data*)bmp->data, 0, 0, bmp->width, x, y, width, height); gwps->display->set_drawmode(DRMODE_SOLID); + if (output_size) + data->img_buf_ptr -= output_size; + data->img_buf_free += output_size; } else {