2002-05-17 12:22:24 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
|
|
|
*
|
2008-06-28 18:10:04 +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.
|
2002-05-17 12:22:24 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2005-04-25 07:42:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
2005-04-16 Tomas Salfischberger:
|
|
|
|
- New BMP loader function, based on the old one (borrowed a lot of
|
|
|
|
calculations and checks there.)
|
|
|
|
- Conversion part needs some optimization, doing unneeded calulations now.
|
2006-11-18 14:29:51 +00:00
|
|
|
2006-11-18 Jens Arnold: complete rework
|
|
|
|
- All canonical formats supported now (1, 4, 8, 15/16, 24 and 32 bit)
|
|
|
|
- better protection against malformed / non-standard BMPs
|
|
|
|
- code heavily optimised for both size and speed
|
|
|
|
- dithering for 2 bit targets
|
2008-12-09 23:07:59 +00:00
|
|
|
2008-11-02 Akio Idehara: refactor for scaler frontend
|
|
|
|
2008-12-08 Andrew Mahone: partial-line reading, scaler frontend
|
|
|
|
- read_part_line does the actual source BMP reading, return columns read
|
|
|
|
and updates fields in a struct bmp_args with the new data and current
|
|
|
|
reader state
|
|
|
|
- skip_lines_bmp and store_part_bmp implement the scaler callbacks to skip
|
|
|
|
ahead by whole lines, or read the next chunk of the current line
|
2005-04-25 07:42:10 +00:00
|
|
|
*/
|
2002-05-17 12:22:24 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2006-01-28 12:12:42 +00:00
|
|
|
#include <string.h>
|
2006-11-11 11:26:42 +00:00
|
|
|
#include "inttypes.h"
|
2009-10-06 22:02:06 +00:00
|
|
|
#include "system.h"
|
2009-01-04 21:22:05 +00:00
|
|
|
#ifndef PLUGIN
|
2005-04-25 07:42:10 +00:00
|
|
|
#include "debug.h"
|
2009-01-04 21:22:05 +00:00
|
|
|
#endif
|
2005-04-25 07:42:10 +00:00
|
|
|
#include "lcd.h"
|
2008-03-26 23:35:34 +00:00
|
|
|
#include "file.h"
|
2009-01-29 20:49:43 +00:00
|
|
|
#include "bmp.h"
|
2006-11-20 00:20:09 +00:00
|
|
|
#ifdef HAVE_REMOTE_LCD
|
|
|
|
#include "lcd-remote.h"
|
|
|
|
#endif
|
2008-12-09 23:07:59 +00:00
|
|
|
#ifdef ROCKBOX_DEBUG_BMP_LOADER
|
2014-01-05 18:37:48 +00:00
|
|
|
#define BDEBUGF DEBUGF
|
2008-12-09 23:07:59 +00:00
|
|
|
#else
|
|
|
|
#define BDEBUGF(...)
|
|
|
|
#endif
|
2008-03-26 18:18:22 +00:00
|
|
|
#ifndef __PCTOOL__
|
2006-01-28 12:12:42 +00:00
|
|
|
#include "config.h"
|
2008-12-09 23:07:59 +00:00
|
|
|
#include "resize.h"
|
2008-03-26 18:18:22 +00:00
|
|
|
#else
|
|
|
|
#undef DEBUGF
|
|
|
|
#define DEBUGF(...)
|
|
|
|
#endif
|
2002-05-17 12:22:24 +00:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define STRUCT_PACKED __attribute__((packed))
|
2002-05-29 14:36:09 +00:00
|
|
|
#else
|
|
|
|
#define STRUCT_PACKED
|
|
|
|
#pragma pack (push, 2)
|
2002-05-17 12:22:24 +00:00
|
|
|
#endif
|
|
|
|
|
2006-11-18 14:29:51 +00:00
|
|
|
/* BMP header structure */
|
|
|
|
struct bmp_header {
|
|
|
|
uint16_t type; /* signature - 'BM' */
|
|
|
|
uint32_t size; /* file size in bytes */
|
|
|
|
uint16_t reserved1; /* 0 */
|
|
|
|
uint16_t reserved2; /* 0 */
|
|
|
|
uint32_t off_bits; /* offset to bitmap */
|
|
|
|
uint32_t struct_size; /* size of this struct (40) */
|
2007-11-30 08:51:18 +00:00
|
|
|
int32_t width; /* bmap width in pixels */
|
|
|
|
int32_t height; /* bmap height in pixels */
|
2006-11-18 14:29:51 +00:00
|
|
|
uint16_t planes; /* num planes - always 1 */
|
|
|
|
uint16_t bit_count; /* bits per pixel */
|
|
|
|
uint32_t compression; /* compression flag */
|
|
|
|
uint32_t size_image; /* image size in bytes */
|
|
|
|
int32_t x_pels_per_meter; /* horz resolution */
|
|
|
|
int32_t y_pels_per_meter; /* vert resolution */
|
|
|
|
uint32_t clr_used; /* 0 -> color table size */
|
|
|
|
uint32_t clr_important; /* important color count */
|
2002-05-17 12:22:24 +00:00
|
|
|
} STRUCT_PACKED;
|
|
|
|
|
2008-12-09 23:07:59 +00:00
|
|
|
/* masks for supported BI_BITFIELDS encodings (16/32 bit) */
|
2013-12-21 16:12:21 +00:00
|
|
|
static const struct uint8_rgb bitfields[][4] = {
|
2008-12-09 23:07:59 +00:00
|
|
|
/* 15bit */
|
|
|
|
{
|
2013-12-21 16:12:21 +00:00
|
|
|
{ .blue = 0x00, .green = 0x7c, .red = 0x00, .alpha = 0x00 },
|
|
|
|
{ .blue = 0xe0, .green = 0x03, .red = 0x00, .alpha = 0x00 },
|
|
|
|
{ .blue = 0x1f, .green = 0x00, .red = 0x00, .alpha = 0x00 },
|
|
|
|
{ .blue = 0x00, .green = 0x00, .red = 0x00, .alpha = 0x00 },
|
2008-12-09 23:07:59 +00:00
|
|
|
},
|
|
|
|
/* 16bit */
|
|
|
|
{
|
2013-12-21 16:12:21 +00:00
|
|
|
{ .blue = 0x00, .green = 0xf8, .red = 0x00, .alpha = 0x00 },
|
|
|
|
{ .blue = 0xe0, .green = 0x07, .red = 0x00, .alpha = 0x00 },
|
|
|
|
{ .blue = 0x1f, .green = 0x00, .red = 0x00, .alpha = 0x00 },
|
|
|
|
{ .blue = 0x00, .green = 0x00, .red = 0x00, .alpha = 0x00 },
|
2008-12-09 23:07:59 +00:00
|
|
|
},
|
2013-12-21 16:12:21 +00:00
|
|
|
/* 32bit BGRA */
|
2008-12-09 23:07:59 +00:00
|
|
|
{
|
2013-12-21 16:12:21 +00:00
|
|
|
{ .blue = 0x00, .green = 0x00, .red = 0xff, .alpha = 0x00 },
|
|
|
|
{ .blue = 0x00, .green = 0xff, .red = 0x00, .alpha = 0x00 },
|
|
|
|
{ .blue = 0xff, .green = 0x00, .red = 0x00, .alpha = 0x00 },
|
|
|
|
{ .blue = 0x00, .green = 0x00, .red = 0x00, .alpha = 0xff },
|
|
|
|
},
|
|
|
|
/* 32bit ABGR */
|
|
|
|
{
|
|
|
|
{ .blue = 0x00, .green = 0x00, .red = 0x00, .alpha = 0xff },
|
|
|
|
{ .blue = 0x00, .green = 0x00, .red = 0xff, .alpha = 0x00 },
|
|
|
|
{ .blue = 0x00, .green = 0xff, .red = 0x00, .alpha = 0x00 },
|
|
|
|
{ .blue = 0xff, .green = 0x00, .red = 0x00, .alpha = 0x00 },
|
2008-12-09 23:07:59 +00:00
|
|
|
},
|
2006-11-18 14:29:51 +00:00
|
|
|
};
|
2005-04-25 07:42:10 +00:00
|
|
|
|
2009-01-08 02:49:23 +00:00
|
|
|
#if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
|
2008-12-26 07:03:22 +00:00
|
|
|
/* the full 16x16 Bayer dither matrix may be calculated quickly with this table
|
|
|
|
*/
|
|
|
|
const unsigned char dither_table[16] =
|
|
|
|
{ 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 };
|
2006-11-18 14:29:51 +00:00
|
|
|
#endif
|
2005-04-25 07:42:10 +00:00
|
|
|
|
2008-03-20 17:40:58 +00:00
|
|
|
#if ((LCD_DEPTH == 2) && (LCD_PIXELFORMAT == VERTICAL_INTERLEAVED)) \
|
|
|
|
|| (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH == 2) \
|
|
|
|
&& (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED))
|
2008-12-09 23:07:59 +00:00
|
|
|
const unsigned short vi_pattern[4] = {
|
2006-11-20 00:20:09 +00:00
|
|
|
0x0101, 0x0100, 0x0001, 0x0000
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2005-04-25 07:42:10 +00:00
|
|
|
/******************************************************************************
|
2002-05-17 12:22:24 +00:00
|
|
|
* read_bmp_file()
|
|
|
|
*
|
2006-11-18 14:29:51 +00:00
|
|
|
* Reads a BMP file and puts the data in rockbox format in *bitmap.
|
2002-05-17 12:22:24 +00:00
|
|
|
*
|
2005-04-25 07:42:10 +00:00
|
|
|
*****************************************************************************/
|
2007-11-10 13:26:11 +00:00
|
|
|
int read_bmp_file(const char* filename,
|
2006-01-28 12:12:42 +00:00
|
|
|
struct bitmap *bm,
|
|
|
|
int maxsize,
|
2008-12-26 07:05:13 +00:00
|
|
|
int format,
|
|
|
|
const struct custom_format *cformat)
|
2007-11-10 13:26:11 +00:00
|
|
|
{
|
|
|
|
int fd, ret;
|
2009-01-08 02:49:23 +00:00
|
|
|
fd = open(filename, O_RDONLY);
|
2007-11-10 13:26:11 +00:00
|
|
|
|
|
|
|
/* Exit if file opening failed */
|
|
|
|
if (fd < 0) {
|
|
|
|
DEBUGF("read_bmp_file: can't open '%s', rc: %d\n", filename, fd);
|
|
|
|
return fd * 10 - 1;
|
|
|
|
}
|
|
|
|
|
2008-12-09 23:07:59 +00:00
|
|
|
BDEBUGF("read_bmp_file: '%s' remote: %d resize: %d keep_aspect: %d\n",
|
|
|
|
filename, !!(format & FORMAT_REMOTE), !!(format & FORMAT_RESIZE),
|
|
|
|
!!(format & FORMAT_KEEP_ASPECT));
|
2008-12-26 07:05:13 +00:00
|
|
|
ret = read_bmp_fd(fd, bm, maxsize, format, cformat);
|
2009-01-08 02:49:23 +00:00
|
|
|
close(fd);
|
2007-11-10 13:26:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-21 16:12:21 +00:00
|
|
|
enum color_order {
|
|
|
|
/* only used for different types of 32bpp images */
|
|
|
|
BGRA, /* should be most common */
|
|
|
|
ABGR /* generated by some GIMP versions */
|
|
|
|
};
|
2008-12-09 23:07:59 +00:00
|
|
|
|
|
|
|
struct bmp_args {
|
2014-01-05 00:14:14 +00:00
|
|
|
/* needs to be at least 2byte aligned for faster 16bit reads.
|
|
|
|
* but aligning to cache should be even faster */
|
|
|
|
unsigned char buf[BM_MAX_WIDTH * 4] CACHEALIGN_AT_LEAST_ATTR(2);
|
2008-12-09 23:07:59 +00:00
|
|
|
int fd;
|
|
|
|
short padded_width;
|
|
|
|
short read_width;
|
|
|
|
short width;
|
|
|
|
short depth;
|
2013-12-21 16:12:21 +00:00
|
|
|
enum color_order order;
|
2008-12-09 23:07:59 +00:00
|
|
|
struct uint8_rgb *palette;
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-09 23:07:59 +00:00
|
|
|
int cur_row;
|
|
|
|
int cur_col;
|
|
|
|
struct img_part part;
|
|
|
|
#endif
|
2011-11-08 21:36:49 +00:00
|
|
|
/* as read_part_line() goes through the rows it'll set this to true
|
2011-11-11 22:03:29 +00:00
|
|
|
* if it finds transparency. Initialize to 0 before calling */
|
|
|
|
int alpha_detected;
|
|
|
|
/* for checking transparency it checks the against the very first byte
|
|
|
|
* of the bitmap. Initalize to 0x80 before calling */
|
|
|
|
unsigned char first_alpha_byte;
|
2008-12-09 23:07:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned int read_part_line(struct bmp_args *ba)
|
|
|
|
{
|
|
|
|
const int padded_width = ba->padded_width;
|
|
|
|
const int read_width = ba->read_width;
|
|
|
|
const int width = ba->width;
|
2013-12-21 16:12:21 +00:00
|
|
|
int depth = ba->depth;
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-09 23:07:59 +00:00
|
|
|
int cur_row = ba->cur_row;
|
|
|
|
int cur_col = ba->cur_col;
|
|
|
|
#endif
|
|
|
|
const int fd = ba->fd;
|
|
|
|
uint8_t *ibuf;
|
|
|
|
struct uint8_rgb *buf = (struct uint8_rgb *)(ba->buf);
|
|
|
|
const struct uint8_rgb *palette = ba->palette;
|
2011-06-12 22:17:45 +00:00
|
|
|
uint32_t component, data;
|
2008-12-09 23:07:59 +00:00
|
|
|
int ret;
|
|
|
|
int i, cols, len;
|
|
|
|
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-26 07:03:22 +00:00
|
|
|
cols = MIN(width - cur_col,(int)BM_MAX_WIDTH);
|
|
|
|
BDEBUGF("reading %d cols (width: %d, max: %d)\n",cols,width,BM_MAX_WIDTH);
|
2008-12-09 23:07:59 +00:00
|
|
|
len = (cols * (depth == 15 ? 16 : depth) + 7) >> 3;
|
|
|
|
#else
|
|
|
|
cols = width;
|
|
|
|
len = read_width;
|
|
|
|
#endif
|
2008-12-26 07:03:22 +00:00
|
|
|
ibuf = ((unsigned char *)buf) + (BM_MAX_WIDTH << 2) - len;
|
2008-12-09 23:07:59 +00:00
|
|
|
BDEBUGF("read_part_line: cols=%d len=%d\n",cols,len);
|
2009-01-08 02:49:23 +00:00
|
|
|
ret = read(fd, ibuf, len);
|
2008-12-09 23:07:59 +00:00
|
|
|
if (ret != len)
|
|
|
|
{
|
|
|
|
DEBUGF("read_part_line: error reading image, read returned %d "
|
|
|
|
"expected %d\n", ret, len);
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-09 23:07:59 +00:00
|
|
|
BDEBUGF("cur_row: %d cur_col: %d cols: %d len: %d\n", cur_row, cur_col,
|
|
|
|
cols, len);
|
2009-01-04 21:22:05 +00:00
|
|
|
#endif
|
2008-12-09 23:07:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-11-08 21:36:49 +00:00
|
|
|
|
2011-11-11 22:03:29 +00:00
|
|
|
/* detect if the image has useful alpha information.
|
|
|
|
* if all alpha bits are 0xff or 0x00 discard the information.
|
|
|
|
* if it has other bits, or is mixed with 0x00 and 0xff then interpret
|
|
|
|
* as alpha. assume no alpha until the opposite is proven. as mixed
|
|
|
|
* is alpha, compare to the first byte instead of 0xff and 0x00 separately
|
|
|
|
*/
|
|
|
|
if (depth == 32 && ba->first_alpha_byte == 0x80)
|
|
|
|
ba->first_alpha_byte = ibuf[3] ? 0xff : 0x0;
|
|
|
|
|
2013-12-21 16:12:21 +00:00
|
|
|
/* select different color orders within the switch-case to avoid
|
|
|
|
* nested if/switch */
|
|
|
|
if (depth == 32)
|
|
|
|
depth += ba->order;
|
|
|
|
|
2008-12-26 07:03:22 +00:00
|
|
|
while (ibuf < ba->buf + (BM_MAX_WIDTH << 2))
|
2008-12-09 23:07:59 +00:00
|
|
|
{
|
|
|
|
switch (depth)
|
|
|
|
{
|
|
|
|
case 1:
|
2008-12-26 07:03:22 +00:00
|
|
|
data = *ibuf++;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
*buf++ = palette[data & 0x80 ? 1 : 0];
|
|
|
|
data <<= 1;
|
|
|
|
}
|
2008-12-09 23:07:59 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2008-12-26 07:03:22 +00:00
|
|
|
data = *ibuf++;
|
|
|
|
*buf++ = palette[data >> 4];
|
|
|
|
*buf++ = palette[data & 0xf];
|
2008-12-09 23:07:59 +00:00
|
|
|
break;
|
|
|
|
case 8:
|
2008-12-26 07:03:22 +00:00
|
|
|
*buf++ = palette[*ibuf++];
|
2008-12-09 23:07:59 +00:00
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
case 16:
|
2014-01-05 00:14:14 +00:00
|
|
|
data = letoh16(*(uint16_t*)ibuf);
|
2008-12-09 23:07:59 +00:00
|
|
|
component = (data << 3) & 0xf8;
|
|
|
|
component |= component >> 5;
|
|
|
|
buf->blue = component;
|
|
|
|
if (depth == 15)
|
|
|
|
{
|
|
|
|
data >>= 2;
|
|
|
|
component = data & 0xf8;
|
|
|
|
component |= component >> 5;
|
|
|
|
} else {
|
|
|
|
data >>= 3;
|
|
|
|
component = data & 0xfc;
|
|
|
|
component |= component >> 6;
|
|
|
|
}
|
|
|
|
buf->green = component;
|
|
|
|
data >>= 5;
|
|
|
|
component = data & 0xf8;
|
|
|
|
component |= component >> 5;
|
|
|
|
buf->red = component;
|
2011-11-08 21:36:49 +00:00
|
|
|
buf->alpha = 0xff;
|
2008-12-26 07:03:22 +00:00
|
|
|
buf++;
|
2008-12-09 23:07:59 +00:00
|
|
|
ibuf += 2;
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
buf->blue = *ibuf++;
|
|
|
|
buf->green = *ibuf++;
|
|
|
|
buf->red = *ibuf++;
|
2011-11-11 22:03:29 +00:00
|
|
|
buf->alpha = 0xff;
|
|
|
|
buf++;
|
|
|
|
break;
|
2013-12-21 16:12:21 +00:00
|
|
|
case 32 + BGRA:
|
2011-11-11 22:03:29 +00:00
|
|
|
buf->blue = *ibuf++;
|
|
|
|
buf->green = *ibuf++;
|
|
|
|
buf->red = *ibuf++;
|
|
|
|
buf->alpha = *ibuf++;
|
|
|
|
ba->alpha_detected |= (buf->alpha != ba->first_alpha_byte);
|
2008-12-26 07:03:22 +00:00
|
|
|
buf++;
|
2008-12-09 23:07:59 +00:00
|
|
|
break;
|
2013-12-21 16:12:21 +00:00
|
|
|
case 32 + ABGR:
|
|
|
|
buf->alpha = *ibuf++;
|
|
|
|
buf->blue = *ibuf++;
|
|
|
|
buf->green = *ibuf++;
|
|
|
|
buf->red = *ibuf++;
|
|
|
|
ba->alpha_detected |= (buf->alpha != ba->first_alpha_byte);
|
|
|
|
buf++;
|
|
|
|
break;
|
2008-12-09 23:07:59 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-04 15:46:41 +00:00
|
|
|
#if !defined(HAVE_LCD_COLOR) && \
|
|
|
|
((LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) || \
|
|
|
|
defined(PLUGIN))
|
2008-12-26 07:03:22 +00:00
|
|
|
ibuf = ba->buf;
|
|
|
|
buf = (struct uint8_rgb*)ba->buf;
|
|
|
|
while (ibuf < ba->buf + cols)
|
|
|
|
*ibuf++ = brightness(*buf++);
|
|
|
|
#endif
|
|
|
|
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-09 23:07:59 +00:00
|
|
|
cur_col += cols;
|
|
|
|
if (cur_col == width)
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
int pad = padded_width - read_width;
|
|
|
|
if (pad > 0)
|
|
|
|
{
|
|
|
|
BDEBUGF("seeking %d bytes to next line\n",pad);
|
2009-01-08 02:49:23 +00:00
|
|
|
lseek(fd, pad, SEEK_CUR);
|
2008-12-09 23:07:59 +00:00
|
|
|
}
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-09 23:07:59 +00:00
|
|
|
cur_col = 0;
|
|
|
|
BDEBUGF("read_part_line: completed row %d\n", cur_row);
|
|
|
|
cur_row += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ba->cur_row = cur_row;
|
|
|
|
ba->cur_col = cur_col;
|
|
|
|
#endif
|
|
|
|
return cols;
|
|
|
|
}
|
|
|
|
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-09 23:07:59 +00:00
|
|
|
static struct img_part *store_part_bmp(void *args)
|
|
|
|
{
|
|
|
|
struct bmp_args *ba = (struct bmp_args *)args;
|
|
|
|
|
|
|
|
ba->part.len = read_part_line(ba);
|
2008-12-26 07:03:22 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2008-12-09 23:07:59 +00:00
|
|
|
ba->part.buf = (struct uint8_rgb *)ba->buf;
|
2008-12-26 07:03:22 +00:00
|
|
|
#else
|
|
|
|
ba->part.buf = (uint8_t *)ba->buf;
|
|
|
|
#endif
|
2008-12-09 23:07:59 +00:00
|
|
|
if (ba->part.len)
|
|
|
|
return &(ba->part);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-21 16:12:21 +00:00
|
|
|
static inline int rgbcmp(const struct uint8_rgb *rgb1, const struct uint8_rgb *rgb2)
|
2008-12-09 23:07:59 +00:00
|
|
|
{
|
2013-12-21 16:12:21 +00:00
|
|
|
return memcmp(rgb1, rgb2, sizeof(struct uint8_rgb));
|
2008-12-09 23:07:59 +00:00
|
|
|
}
|
2013-12-21 16:12:21 +00:00
|
|
|
|
2009-05-09 07:31:27 +00:00
|
|
|
#if LCD_DEPTH > 1
|
2009-05-09 10:32:07 +00:00
|
|
|
#if !defined(PLUGIN) && !defined(HAVE_JPEG) && !defined(HAVE_BMP_SCALING)
|
|
|
|
static inline
|
|
|
|
#endif
|
2009-05-09 07:31:27 +00:00
|
|
|
void output_row_8_native(uint32_t row, void * row_in,
|
|
|
|
struct scaler_context *ctx)
|
|
|
|
{
|
|
|
|
int col;
|
|
|
|
int fb_width = BM_WIDTH(ctx->bm->width,FORMAT_NATIVE,0);
|
|
|
|
uint8_t dy = DITHERY(row);
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
struct uint8_rgb *qp = (struct uint8_rgb*)row_in;
|
|
|
|
#else
|
|
|
|
uint8_t *qp = (uint8_t*)row_in;
|
|
|
|
#endif
|
|
|
|
BDEBUGF("output_row: y: %lu in: %p\n",row, row_in);
|
|
|
|
#if LCD_DEPTH == 2
|
|
|
|
#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
|
|
|
|
/* greyscale iPods */
|
|
|
|
fb_data *dest = (fb_data *)ctx->bm->data + fb_width * row;
|
|
|
|
int shift = 6;
|
|
|
|
int delta = 127;
|
|
|
|
unsigned bright;
|
|
|
|
unsigned data = 0;
|
|
|
|
|
|
|
|
for (col = 0; col < ctx->bm->width; col++) {
|
|
|
|
if (ctx->dither)
|
|
|
|
delta = DITHERXDY(col,dy);
|
|
|
|
bright = *qp++;
|
|
|
|
bright = (3 * bright + (bright >> 6) + delta) >> 8;
|
|
|
|
data |= (~bright & 3) << shift;
|
|
|
|
shift -= 2;
|
|
|
|
if (shift < 0) {
|
|
|
|
*dest++ = data;
|
|
|
|
data = 0;
|
|
|
|
shift = 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shift < 6)
|
|
|
|
*dest++ = data;
|
|
|
|
#elif LCD_PIXELFORMAT == VERTICAL_PACKING
|
|
|
|
/* iriver H1x0 */
|
|
|
|
fb_data *dest = (fb_data *)ctx->bm->data + fb_width *
|
|
|
|
(row >> 2);
|
|
|
|
int shift = 2 * (row & 3);
|
|
|
|
int delta = 127;
|
|
|
|
unsigned bright;
|
|
|
|
|
|
|
|
for (col = 0; col < ctx->bm->width; col++) {
|
|
|
|
if (ctx->dither)
|
|
|
|
delta = DITHERXDY(col,dy);
|
|
|
|
bright = *qp++;
|
|
|
|
bright = (3 * bright + (bright >> 6) + delta) >> 8;
|
|
|
|
*dest++ |= (~bright & 3) << shift;
|
|
|
|
}
|
|
|
|
#elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
|
|
|
|
/* iAudio M3 */
|
|
|
|
fb_data *dest = (fb_data *)ctx->bm->data + fb_width *
|
|
|
|
(row >> 3);
|
|
|
|
int shift = row & 7;
|
|
|
|
int delta = 127;
|
|
|
|
unsigned bright;
|
|
|
|
|
|
|
|
for (col = 0; col < ctx->bm->width; col++) {
|
|
|
|
if (ctx->dither)
|
|
|
|
delta = DITHERXDY(col,dy);
|
|
|
|
bright = *qp++;
|
|
|
|
bright = (3 * bright + (bright >> 6) + delta) >> 8;
|
|
|
|
*dest++ |= vi_pattern[bright] << shift;
|
|
|
|
}
|
|
|
|
#endif /* LCD_PIXELFORMAT */
|
2014-06-18 05:15:00 +00:00
|
|
|
#elif LCD_DEPTH >= 16
|
2009-05-09 07:31:27 +00:00
|
|
|
/* iriver h300, colour iPods, X5 */
|
2011-11-08 21:36:49 +00:00
|
|
|
(void)fb_width;
|
|
|
|
fb_data *dest = STRIDE_MAIN((fb_data *)ctx->bm->data + fb_width * row,
|
|
|
|
(fb_data *)ctx->bm->data + row);
|
2009-05-09 07:31:27 +00:00
|
|
|
int delta = 127;
|
2011-11-08 21:36:49 +00:00
|
|
|
unsigned r, g, b;
|
|
|
|
/* setup alpha channel buffer */
|
|
|
|
unsigned char *bm_alpha = NULL;
|
|
|
|
if (ctx->bm->alpha_offset > 0)
|
|
|
|
bm_alpha = ctx->bm->data + ctx->bm->alpha_offset;
|
|
|
|
if (bm_alpha)
|
2011-11-11 19:05:11 +00:00
|
|
|
bm_alpha += ALIGN_UP(ctx->bm->width, 2) * row/2;
|
2011-11-08 21:36:49 +00:00
|
|
|
|
2009-05-09 07:31:27 +00:00
|
|
|
for (col = 0; col < ctx->bm->width; col++) {
|
2014-06-18 05:15:00 +00:00
|
|
|
(void) delta;
|
2009-05-09 07:31:27 +00:00
|
|
|
if (ctx->dither)
|
|
|
|
delta = DITHERXDY(col,dy);
|
|
|
|
r = qp->red;
|
|
|
|
g = qp->green;
|
2011-11-08 21:36:49 +00:00
|
|
|
b = qp->blue;
|
2014-06-18 05:15:00 +00:00
|
|
|
#if LCD_DEPTH < 24
|
2009-05-09 07:31:27 +00:00
|
|
|
r = (31 * r + (r >> 3) + delta) >> 8;
|
|
|
|
g = (63 * g + (g >> 2) + delta) >> 8;
|
|
|
|
b = (31 * b + (b >> 3) + delta) >> 8;
|
2014-06-18 05:15:00 +00:00
|
|
|
#endif
|
|
|
|
*dest = FB_RGBPACK_LCD(r, g, b);
|
2011-11-08 21:36:49 +00:00
|
|
|
dest += STRIDE_MAIN(1, ctx->bm->height);
|
|
|
|
if (bm_alpha) {
|
2013-02-12 09:26:44 +00:00
|
|
|
/* pack alpha channel for 2 pixels into 1 byte and negate
|
|
|
|
* according to the interal alpha channel format */
|
|
|
|
uint8_t alpha = ~qp->alpha;
|
2011-11-08 21:36:49 +00:00
|
|
|
if (col%2)
|
|
|
|
*bm_alpha++ |= alpha&0xf0;
|
|
|
|
else
|
|
|
|
*bm_alpha = alpha>>4;
|
|
|
|
}
|
|
|
|
qp++;
|
2009-05-09 07:31:27 +00:00
|
|
|
}
|
|
|
|
#endif /* LCD_DEPTH */
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-09 23:07:59 +00:00
|
|
|
|
2007-11-10 13:26:11 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* read_bmp_fd()
|
|
|
|
*
|
|
|
|
* Reads a BMP file in an open file descriptor and puts the data in rockbox
|
|
|
|
* format in *bitmap.
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
int read_bmp_fd(int fd,
|
|
|
|
struct bitmap *bm,
|
|
|
|
int maxsize,
|
2008-12-26 07:05:13 +00:00
|
|
|
int format,
|
|
|
|
const struct custom_format *cformat)
|
2002-05-17 12:22:24 +00:00
|
|
|
{
|
2006-11-18 14:29:51 +00:00
|
|
|
struct bmp_header bmph;
|
2008-12-09 23:07:59 +00:00
|
|
|
int padded_width;
|
|
|
|
int read_width;
|
2006-11-18 14:29:51 +00:00
|
|
|
int depth, numcolors, compression, totalsize;
|
2014-01-11 13:14:46 +00:00
|
|
|
int ret, hdr_size;
|
2009-05-25 11:12:27 +00:00
|
|
|
bool return_size = format & FORMAT_RETURN_SIZE;
|
2011-11-08 21:36:49 +00:00
|
|
|
bool read_alpha = format & FORMAT_TRANSPARENT;
|
2013-12-21 16:12:21 +00:00
|
|
|
enum color_order order = BGRA;
|
2006-11-18 14:29:51 +00:00
|
|
|
|
|
|
|
unsigned char *bitmap = bm->data;
|
2008-12-09 23:07:59 +00:00
|
|
|
struct uint8_rgb palette[256];
|
|
|
|
struct rowset rset;
|
|
|
|
struct dim src_dim;
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) || \
|
|
|
|
defined(PLUGIN)
|
2006-10-11 01:26:09 +00:00
|
|
|
bool dither = false;
|
2009-05-04 15:46:41 +00:00
|
|
|
#endif
|
2008-12-26 07:03:22 +00:00
|
|
|
|
2011-09-03 11:04:41 +00:00
|
|
|
bool remote = false;
|
2014-06-18 05:15:00 +00:00
|
|
|
#ifdef HAVE_REMOTE_LCD
|
2006-11-20 00:20:09 +00:00
|
|
|
if (format & FORMAT_REMOTE) {
|
2011-09-03 11:04:41 +00:00
|
|
|
remote = true;
|
2006-11-20 00:20:09 +00:00
|
|
|
#if LCD_REMOTE_DEPTH == 1
|
|
|
|
format = FORMAT_MONO;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* HAVE_REMOTE_LCD */
|
2008-12-09 23:07:59 +00:00
|
|
|
|
2011-11-08 22:34:35 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
|
|
|
unsigned int resize = IMG_NORESIZE;
|
|
|
|
|
2008-12-09 23:07:59 +00:00
|
|
|
if (format & FORMAT_RESIZE) {
|
|
|
|
resize = IMG_RESIZE;
|
|
|
|
}
|
|
|
|
|
2006-10-11 01:26:09 +00:00
|
|
|
#else
|
2006-11-18 14:29:51 +00:00
|
|
|
|
|
|
|
(void)format;
|
2008-12-09 23:07:59 +00:00
|
|
|
#endif /*(LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)*/
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) || \
|
|
|
|
defined(PLUGIN)
|
|
|
|
if (format & FORMAT_DITHER) {
|
|
|
|
dither = true;
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-25 07:42:10 +00:00
|
|
|
/* read fileheader */
|
2009-01-08 02:49:23 +00:00
|
|
|
ret = read(fd, &bmph, sizeof(struct bmp_header));
|
2006-11-18 14:29:51 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret * 10 - 2;
|
2005-04-25 07:42:10 +00:00
|
|
|
}
|
2006-10-15 17:51:00 +00:00
|
|
|
|
2006-11-18 14:29:51 +00:00
|
|
|
if (ret != sizeof(struct bmp_header)) {
|
2007-11-10 13:26:11 +00:00
|
|
|
DEBUGF("read_bmp_fd: can't read BMP header.");
|
2005-04-25 07:42:10 +00:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
2008-12-09 23:07:59 +00:00
|
|
|
src_dim.width = letoh32(bmph.width);
|
|
|
|
src_dim.height = letoh32(bmph.height);
|
|
|
|
if (src_dim.height < 0) { /* Top-down BMP file */
|
|
|
|
src_dim.height = -src_dim.height;
|
|
|
|
rset.rowstep = 1;
|
2007-11-30 08:51:18 +00:00
|
|
|
} else { /* normal BMP */
|
2008-12-09 23:07:59 +00:00
|
|
|
rset.rowstep = -1;
|
2007-11-30 08:51:18 +00:00
|
|
|
}
|
|
|
|
|
2008-12-09 23:07:59 +00:00
|
|
|
depth = letoh16(bmph.bit_count);
|
|
|
|
/* 4-byte boundary aligned */
|
|
|
|
read_width = ((src_dim.width * (depth == 15 ? 16 : depth) + 7) >> 3);
|
|
|
|
padded_width = (read_width + 3) & ~3;
|
2006-01-28 12:12:42 +00:00
|
|
|
|
2008-12-09 23:07:59 +00:00
|
|
|
BDEBUGF("width: %d height: %d depth: %d padded_width: %d\n", src_dim.width,
|
|
|
|
src_dim.height, depth, padded_width);
|
2008-12-26 07:03:22 +00:00
|
|
|
|
|
|
|
#if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
|
2008-12-09 23:07:59 +00:00
|
|
|
if ((format & 3) == FORMAT_ANY) {
|
2006-11-18 14:29:51 +00:00
|
|
|
if (depth == 1)
|
2008-12-09 23:07:59 +00:00
|
|
|
format = (format & ~3);
|
2006-11-18 14:29:51 +00:00
|
|
|
else
|
2008-12-09 23:07:59 +00:00
|
|
|
format = (format & ~3) | FORMAT_NATIVE;
|
2006-11-18 14:29:51 +00:00
|
|
|
}
|
2008-12-26 07:03:22 +00:00
|
|
|
bm->format = format & 1;
|
|
|
|
if ((format & 1) == FORMAT_MONO)
|
2006-11-18 14:29:51 +00:00
|
|
|
{
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-09 23:07:59 +00:00
|
|
|
resize &= ~IMG_RESIZE;
|
|
|
|
resize |= IMG_NORESIZE;
|
2009-05-04 15:46:41 +00:00
|
|
|
#endif
|
2011-08-01 20:07:05 +00:00
|
|
|
remote = false;
|
2008-12-09 23:07:59 +00:00
|
|
|
}
|
2009-01-04 21:22:05 +00:00
|
|
|
#elif !defined(PLUGIN)
|
2008-12-26 07:03:22 +00:00
|
|
|
if (src_dim.width > BM_MAX_WIDTH)
|
2008-12-09 23:07:59 +00:00
|
|
|
return -6;
|
|
|
|
#endif /*(LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)*/
|
|
|
|
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-09 23:07:59 +00:00
|
|
|
if (resize & IMG_RESIZE) {
|
|
|
|
if(format & FORMAT_KEEP_ASPECT) {
|
|
|
|
/* keep aspect ratio.. */
|
|
|
|
struct dim resize_dim = {
|
|
|
|
.width = bm->width,
|
|
|
|
.height = bm->height,
|
|
|
|
};
|
|
|
|
if (recalc_dimension(&resize_dim, &src_dim))
|
|
|
|
resize = IMG_NORESIZE;
|
|
|
|
bm->width = resize_dim.width;
|
|
|
|
bm->height = resize_dim.height;
|
|
|
|
}
|
2006-01-28 12:12:42 +00:00
|
|
|
}
|
2005-04-25 07:42:10 +00:00
|
|
|
|
2008-12-09 23:07:59 +00:00
|
|
|
if (!(resize & IMG_RESIZE)) {
|
|
|
|
#endif
|
|
|
|
/* returning image size */
|
|
|
|
bm->width = src_dim.width;
|
|
|
|
bm->height = src_dim.height;
|
|
|
|
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2008-12-09 23:07:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-05-04 15:46:41 +00:00
|
|
|
#if LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
|
|
|
|
format &= 1;
|
|
|
|
#endif
|
2008-12-09 23:07:59 +00:00
|
|
|
if (rset.rowstep > 0) { /* Top-down BMP file */
|
|
|
|
rset.rowstart = 0;
|
|
|
|
rset.rowstop = bm->height;
|
|
|
|
} else { /* normal BMP */
|
|
|
|
rset.rowstart = bm->height - 1;
|
|
|
|
rset.rowstop = -1;
|
|
|
|
}
|
|
|
|
|
2011-11-11 19:05:11 +00:00
|
|
|
/* need even rows (see lcd-16bit-common.c for details) */
|
|
|
|
int alphasize = ALIGN_UP(bm->width, 2) * bm->height / 2;
|
2008-12-26 07:05:13 +00:00
|
|
|
if (cformat)
|
|
|
|
totalsize = cformat->get_size(bm);
|
2011-11-08 21:36:49 +00:00
|
|
|
else {
|
2008-12-26 07:05:13 +00:00
|
|
|
totalsize = BM_SIZE(bm->width,bm->height,format,remote);
|
2011-11-08 21:36:49 +00:00
|
|
|
if (!remote)
|
|
|
|
if (depth == 32 && read_alpha) /* account for possible 4bit alpha per pixel */
|
2011-11-11 19:05:11 +00:00
|
|
|
totalsize += alphasize;
|
2011-11-08 21:36:49 +00:00
|
|
|
}
|
2008-12-09 23:07:59 +00:00
|
|
|
|
2009-05-25 11:12:27 +00:00
|
|
|
if(return_size)
|
2009-05-25 15:19:34 +00:00
|
|
|
{
|
2009-05-25 20:15:02 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2009-05-25 15:19:34 +00:00
|
|
|
if(resize)
|
|
|
|
totalsize += BM_SCALED_SIZE(bm->width, 0, 0, 0);
|
2010-02-18 15:10:31 +00:00
|
|
|
else if (bm->width > BM_MAX_WIDTH)
|
|
|
|
totalsize += bm->width*4;
|
2009-05-25 20:15:02 +00:00
|
|
|
#endif
|
2009-05-25 11:12:27 +00:00
|
|
|
return totalsize;
|
2009-05-25 15:19:34 +00:00
|
|
|
}
|
2009-05-25 11:12:27 +00:00
|
|
|
|
2005-04-25 07:42:10 +00:00
|
|
|
/* Check if this fits the buffer */
|
2006-01-28 12:12:42 +00:00
|
|
|
if (totalsize > maxsize) {
|
2007-11-10 13:26:11 +00:00
|
|
|
DEBUGF("read_bmp_fd: Bitmap too large for buffer: "
|
2017-04-27 09:36:40 +00:00
|
|
|
"%d bytes (%d max).\n", totalsize, maxsize);
|
2006-11-18 14:29:51 +00:00
|
|
|
return -6;
|
2005-04-25 07:42:10 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 13:14:46 +00:00
|
|
|
hdr_size = letoh32(bmph.struct_size);
|
2008-12-09 23:07:59 +00:00
|
|
|
compression = letoh32(bmph.compression);
|
2006-11-18 14:29:51 +00:00
|
|
|
if (depth <= 8) {
|
2008-12-09 23:07:59 +00:00
|
|
|
numcolors = letoh32(bmph.clr_used);
|
2006-01-28 12:12:42 +00:00
|
|
|
if (numcolors == 0)
|
2009-06-07 21:27:05 +00:00
|
|
|
numcolors = BIT_N(depth);
|
2014-01-11 13:14:46 +00:00
|
|
|
/* forward to the color table */
|
|
|
|
lseek(fd, 14+hdr_size, SEEK_SET);
|
2013-12-21 16:12:21 +00:00
|
|
|
} else {
|
|
|
|
numcolors = 0;
|
|
|
|
if (compression == 3) {
|
|
|
|
if (hdr_size >= 56)
|
|
|
|
numcolors = 4;
|
|
|
|
else /* hdr_size == 52 */
|
|
|
|
numcolors = 3;
|
|
|
|
}
|
|
|
|
}
|
2008-12-09 23:07:59 +00:00
|
|
|
|
2013-12-21 16:12:21 +00:00
|
|
|
/* read color tables. for BI_BITFIELDS this actually
|
|
|
|
* reads the color masks */
|
2006-11-18 14:29:51 +00:00
|
|
|
if (numcolors > 0 && numcolors <= 256) {
|
2008-12-09 23:07:59 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < numcolors; i++) {
|
2013-12-21 16:12:21 +00:00
|
|
|
if (read(fd, &palette[i], sizeof(struct uint8_rgb))
|
|
|
|
!= (int)sizeof(struct uint8_rgb)) {
|
2008-12-09 23:07:59 +00:00
|
|
|
DEBUGF("read_bmp_fd: Can't read color palette\n");
|
|
|
|
return -7;
|
|
|
|
}
|
2006-01-28 12:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-18 14:29:51 +00:00
|
|
|
switch (depth) {
|
|
|
|
case 16:
|
|
|
|
#if LCD_DEPTH >= 16
|
|
|
|
/* don't dither 16 bit BMP to LCD with same or larger depth */
|
2006-11-20 00:20:09 +00:00
|
|
|
if (!remote)
|
|
|
|
dither = false;
|
2006-11-18 14:29:51 +00:00
|
|
|
#endif
|
|
|
|
if (compression == 0) { /* BI_RGB, i.e. 15 bit */
|
|
|
|
depth = 15;
|
|
|
|
break;
|
|
|
|
} /* else fall through */
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
if (compression == 3) { /* BI_BITFIELDS */
|
2013-12-22 19:15:03 +00:00
|
|
|
bool found = false;
|
2008-12-09 23:07:59 +00:00
|
|
|
int i, j;
|
|
|
|
|
2013-12-22 20:04:11 +00:00
|
|
|
/* (i == 0) is 15bit, (i == 1) is 16bit, (i == {2,3}) is 32bit */
|
2013-12-22 19:15:03 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(bitfields) && !found; i++) {
|
2013-12-21 16:12:21 +00:00
|
|
|
/* for 15bpp and higher numcolors has the number of color masks */
|
|
|
|
for (j = 0; j < numcolors; j++) {
|
|
|
|
if (!rgbcmp(&palette[j], &bitfields[i][j])) {
|
2008-12-09 23:07:59 +00:00
|
|
|
found = true;
|
|
|
|
} else {
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-11-18 14:29:51 +00:00
|
|
|
}
|
2013-12-22 19:15:03 +00:00
|
|
|
if (found) {
|
2013-12-22 20:04:11 +00:00
|
|
|
if (i == 1) /* 15bit */
|
2013-12-22 19:15:03 +00:00
|
|
|
depth = 15;
|
2013-12-22 20:04:11 +00:00
|
|
|
else if (i == 4) /* 32bit, ABGR bitmap */
|
2013-12-22 19:15:03 +00:00
|
|
|
order = ABGR;
|
2013-12-22 20:04:11 +00:00
|
|
|
break;
|
2013-12-22 19:15:03 +00:00
|
|
|
}
|
2006-11-18 14:29:51 +00:00
|
|
|
} /* else fall through */
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (compression != 0) { /* not BI_RGB */
|
2007-11-10 13:26:11 +00:00
|
|
|
DEBUGF("read_bmp_fd: Unsupported compression (type %d)\n",
|
2006-11-18 14:29:51 +00:00
|
|
|
compression);
|
|
|
|
return -8;
|
|
|
|
}
|
|
|
|
break;
|
2006-01-28 12:12:42 +00:00
|
|
|
}
|
2006-10-15 17:51:00 +00:00
|
|
|
|
2014-06-18 05:15:00 +00:00
|
|
|
#if LCD_DEPTH >= 24
|
|
|
|
/* Never dither 24/32 bit BMP to 24 bit LCDs */
|
|
|
|
if (depth >= 24 && !remote)
|
|
|
|
dither = false;
|
|
|
|
#endif
|
|
|
|
|
2005-04-25 07:42:10 +00:00
|
|
|
/* Search to the beginning of the image data */
|
2009-01-08 02:49:23 +00:00
|
|
|
lseek(fd, (off_t)letoh32(bmph.off_bits), SEEK_SET);
|
2006-10-15 17:51:00 +00:00
|
|
|
|
2009-01-08 02:49:23 +00:00
|
|
|
memset(bitmap, 0, totalsize);
|
2006-10-15 17:51:00 +00:00
|
|
|
|
2011-11-08 21:36:49 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
if (read_alpha && depth == 32)
|
2011-11-11 19:05:11 +00:00
|
|
|
bm->alpha_offset = totalsize - alphasize;
|
2011-11-08 21:36:49 +00:00
|
|
|
else
|
|
|
|
bm->alpha_offset = 0;
|
|
|
|
#endif
|
|
|
|
|
2008-12-09 23:07:59 +00:00
|
|
|
struct bmp_args ba = {
|
|
|
|
.fd = fd, .padded_width = padded_width, .read_width = read_width,
|
|
|
|
.width = src_dim.width, .depth = depth, .palette = palette,
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2011-11-08 21:36:49 +00:00
|
|
|
.cur_row = 0, .cur_col = 0, .part = {0,0},
|
2008-12-09 23:07:59 +00:00
|
|
|
#endif
|
2011-11-11 22:03:29 +00:00
|
|
|
.alpha_detected = false, .first_alpha_byte = 0x80,
|
2013-12-21 16:12:21 +00:00
|
|
|
.order = order,
|
2008-12-09 23:07:59 +00:00
|
|
|
};
|
2005-04-25 07:42:10 +00:00
|
|
|
|
2009-05-04 15:46:41 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
2009-05-09 07:31:27 +00:00
|
|
|
if (resize)
|
2008-12-26 07:05:13 +00:00
|
|
|
{
|
|
|
|
if (resize_on_load(bm, dither, &src_dim, &rset,
|
|
|
|
bitmap + totalsize, maxsize - totalsize,
|
2009-05-06 04:53:56 +00:00
|
|
|
cformat, IF_PIX_FMT(0,) store_part_bmp, &ba))
|
2008-12-26 07:05:13 +00:00
|
|
|
return totalsize;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2008-12-09 23:07:59 +00:00
|
|
|
#endif /* LCD_DEPTH */
|
2006-01-28 12:12:42 +00:00
|
|
|
|
2009-05-09 07:31:27 +00:00
|
|
|
#if LCD_DEPTH > 1 || defined(PLUGIN)
|
|
|
|
struct scaler_context ctx = {
|
|
|
|
.bm = bm,
|
|
|
|
.dither = dither,
|
|
|
|
};
|
|
|
|
#endif
|
2009-05-09 10:32:07 +00:00
|
|
|
#if defined(PLUGIN) || defined(HAVE_JPEG) || defined(HAVE_BMP_SCALING)
|
2009-05-09 07:31:27 +00:00
|
|
|
#if LCD_DEPTH > 1
|
|
|
|
void (*output_row_8)(uint32_t, void*, struct scaler_context*) =
|
|
|
|
output_row_8_native;
|
|
|
|
#elif defined(PLUGIN)
|
|
|
|
void (*output_row_8)(uint32_t, void*, struct scaler_context*) = NULL;
|
|
|
|
#endif
|
|
|
|
#if LCD_DEPTH > 1 || defined(PLUGIN)
|
|
|
|
if (cformat)
|
|
|
|
output_row_8 = cformat->output_row_8;
|
2009-05-09 10:32:07 +00:00
|
|
|
#endif
|
2009-05-04 15:46:41 +00:00
|
|
|
#endif
|
2006-01-31 23:34:02 +00:00
|
|
|
|
2010-02-18 15:10:31 +00:00
|
|
|
unsigned char *buf = ba.buf;
|
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) || \
|
|
|
|
defined(PLUGIN)
|
|
|
|
if (bm->width > BM_MAX_WIDTH)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
|
|
|
unsigned int len = maxsize - totalsize;
|
|
|
|
buf = bitmap + totalsize;
|
|
|
|
ALIGN_BUFFER(buf, len, sizeof(uint32_t));
|
|
|
|
if (bm->width*4 > (int)len)
|
|
|
|
#endif
|
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
#endif
|
2009-05-09 07:31:27 +00:00
|
|
|
int row;
|
2008-12-09 23:07:59 +00:00
|
|
|
/* loop to read rows and put them to buffer */
|
|
|
|
for (row = rset.rowstart; row != rset.rowstop; row += rset.rowstep) {
|
2010-02-18 15:10:31 +00:00
|
|
|
#if (LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
defined(HAVE_BMP_SCALING) || defined(PLUGIN)
|
|
|
|
if (bm->width > BM_MAX_WIDTH)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_LCD_COLOR)
|
|
|
|
struct uint8_rgb *p = (struct uint8_rgb *)buf;
|
|
|
|
#else
|
|
|
|
uint8_t* p = buf;
|
|
|
|
#endif
|
|
|
|
do {
|
|
|
|
int len = read_part_line(&ba);
|
|
|
|
if (!len)
|
|
|
|
return -9;
|
|
|
|
memcpy(p, ba.buf, len*sizeof(*p));
|
|
|
|
p += len;
|
|
|
|
} while (ba.cur_col);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2009-05-09 07:31:27 +00:00
|
|
|
if (!read_part_line(&ba))
|
|
|
|
return -9;
|
|
|
|
#ifndef PLUGIN
|
2008-12-26 07:03:22 +00:00
|
|
|
#if !defined(HAVE_LCD_COLOR) && \
|
|
|
|
(LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1))
|
2010-02-18 15:10:31 +00:00
|
|
|
uint8_t* qp = buf;
|
2008-12-26 07:03:22 +00:00
|
|
|
#else
|
2010-02-18 15:10:31 +00:00
|
|
|
struct uint8_rgb *qp = (struct uint8_rgb *)buf;
|
2008-12-26 07:03:22 +00:00
|
|
|
#endif
|
2009-05-09 07:31:27 +00:00
|
|
|
#endif
|
2006-11-18 14:29:51 +00:00
|
|
|
/* Convert to destination format */
|
2009-05-09 07:31:27 +00:00
|
|
|
#if ((LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
!defined(PLUGIN)
|
2008-12-26 07:03:22 +00:00
|
|
|
if (format == FORMAT_NATIVE) {
|
|
|
|
#if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
|
|
|
|
if (remote) {
|
2009-05-09 07:31:27 +00:00
|
|
|
unsigned char dy = DITHERY(row);
|
2008-12-26 07:03:22 +00:00
|
|
|
#if (LCD_REMOTE_DEPTH == 2) && (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED)
|
|
|
|
/* iAudio X5/M5 remote */
|
|
|
|
fb_remote_data *dest = (fb_remote_data *)bitmap
|
|
|
|
+ bm->width * (row >> 3);
|
|
|
|
int shift = row & 7;
|
|
|
|
int delta = 127;
|
|
|
|
unsigned bright;
|
|
|
|
|
2009-05-09 07:31:27 +00:00
|
|
|
int col;
|
2008-12-26 07:03:22 +00:00
|
|
|
for (col = 0; col < bm->width; col++) {
|
|
|
|
if (dither)
|
|
|
|
delta = DITHERXDY(col,dy);
|
|
|
|
#if !defined(HAVE_LCD_COLOR) && \
|
|
|
|
(LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1))
|
|
|
|
bright = *qp++;
|
|
|
|
#else
|
|
|
|
bright = brightness(*qp++);
|
|
|
|
#endif
|
|
|
|
bright = (3 * bright + (bright >> 6) + delta) >> 8;
|
|
|
|
*dest++ |= vi_pattern[bright] << shift;
|
2006-11-20 00:20:09 +00:00
|
|
|
}
|
2008-12-26 07:03:22 +00:00
|
|
|
#endif /* LCD_REMOTE_DEPTH / LCD_REMOTE_PIXELFORMAT */
|
|
|
|
} else
|
|
|
|
#endif /* defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1 */
|
2009-05-09 07:31:27 +00:00
|
|
|
#endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) &&
|
|
|
|
(LCD_REMOTE_DEPTH > 1) */
|
|
|
|
#if LCD_DEPTH > 1 || defined(PLUGIN)
|
2008-12-26 07:03:22 +00:00
|
|
|
{
|
2009-05-09 10:32:07 +00:00
|
|
|
#if !defined(PLUGIN) && !defined(HAVE_JPEG) && !defined(HAVE_BMP_SCALING)
|
2010-02-18 15:10:31 +00:00
|
|
|
output_row_8_native(row, buf, &ctx);
|
2009-05-09 10:32:07 +00:00
|
|
|
#else
|
2010-02-18 15:10:31 +00:00
|
|
|
output_row_8(row, buf, &ctx);
|
2009-05-09 10:32:07 +00:00
|
|
|
#endif
|
2006-11-20 00:20:09 +00:00
|
|
|
}
|
2009-05-09 07:31:27 +00:00
|
|
|
#endif
|
|
|
|
#if ((LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)) && \
|
|
|
|
!defined(PLUGIN)
|
|
|
|
}
|
|
|
|
#ifndef PLUGIN
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifndef PLUGIN
|
2006-11-18 14:29:51 +00:00
|
|
|
{
|
2009-05-09 07:31:27 +00:00
|
|
|
unsigned char *p = bitmap + bm->width * (row >> 3);
|
2009-06-07 21:27:05 +00:00
|
|
|
unsigned char mask = BIT_N(row & 7);
|
2009-05-09 07:31:27 +00:00
|
|
|
int col;
|
2008-12-26 07:03:22 +00:00
|
|
|
for (col = 0; col < bm->width; col++, p++)
|
|
|
|
#if !defined(HAVE_LCD_COLOR) && \
|
|
|
|
(LCD_DEPTH > 1 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1))
|
|
|
|
if (*qp++ < 128)
|
|
|
|
*p |= mask;
|
|
|
|
#else
|
2006-11-18 14:29:51 +00:00
|
|
|
if (brightness(*qp++) < 128)
|
|
|
|
*p |= mask;
|
2008-12-26 07:03:22 +00:00
|
|
|
#endif
|
2005-04-25 07:42:10 +00:00
|
|
|
}
|
2009-05-09 07:31:27 +00:00
|
|
|
#endif
|
2005-04-25 07:42:10 +00:00
|
|
|
}
|
2011-11-08 21:36:49 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
if (!ba.alpha_detected)
|
|
|
|
{ /* if this has an alpha channel, totalsize accounts for it as well
|
|
|
|
* subtract if no actual alpha information was found */
|
|
|
|
if (bm->alpha_offset > 0)
|
2011-11-11 19:05:11 +00:00
|
|
|
totalsize -= alphasize;
|
2011-11-08 21:36:49 +00:00
|
|
|
bm->alpha_offset = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2006-01-28 12:12:42 +00:00
|
|
|
return totalsize; /* return the used buffer size. */
|
2005-04-25 07:42:10 +00:00
|
|
|
}
|