1
0
Fork 0
forked from len0rd/rockbox

Add read_bmp_fd and make the first parameter of read_bmp_file ("filename") const.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15553 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Nicolas Pennequin 2007-11-10 13:26:11 +00:00
parent d3ba403f60
commit ec6569ed22
3 changed files with 42 additions and 28 deletions

View file

@ -603,7 +603,7 @@ struct plugin_api {
bool (*peak_meter_get_use_dbfs)(void); bool (*peak_meter_get_use_dbfs)(void);
#endif #endif
#ifdef HAVE_LCD_BITMAP #ifdef HAVE_LCD_BITMAP
int (*read_bmp_file)(char* filename, struct bitmap *bm, int maxsize, int (*read_bmp_file)(const char* filename, struct bitmap *bm, int maxsize,
int format); int format);
void (*screen_dump_set_hook)(void (*hook)(int fh)); void (*screen_dump_set_hook)(void (*hook)(int fh));
#endif #endif

View file

@ -143,15 +143,41 @@ static inline unsigned brightness(union rgb_union color)
* Reads a BMP file and puts the data in rockbox format in *bitmap. * Reads a BMP file and puts the data in rockbox format in *bitmap.
* *
*****************************************************************************/ *****************************************************************************/
int read_bmp_file(char* filename, int read_bmp_file(const char* filename,
struct bitmap *bm, struct bitmap *bm,
int maxsize, int maxsize,
int format) int format)
{
int fd, ret;
fd = open(filename, O_RDONLY);
/* 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;
}
ret = read_bmp_fd(fd, bm, maxsize, format);
close(fd);
return ret;
}
/******************************************************************************
* 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,
int format)
{ {
struct bmp_header bmph; struct bmp_header bmph;
int width, height, padded_width; int width, height, padded_width;
int dst_height, dst_width; int dst_height, dst_width;
int fd, row, col, ret; int row, col, ret;
int depth, numcolors, compression, totalsize; int depth, numcolors, compression, totalsize;
unsigned char *bitmap = bm->data; unsigned char *bitmap = bm->data;
@ -185,32 +211,21 @@ int read_bmp_file(char* filename,
(void)format; (void)format;
#endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */ #endif /* (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1) */
fd = open(filename, O_RDONLY);
/* 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;
}
/* read fileheader */ /* read fileheader */
ret = read(fd, &bmph, sizeof(struct bmp_header)); ret = read(fd, &bmph, sizeof(struct bmp_header));
if (ret < 0) { if (ret < 0) {
close(fd);
return ret * 10 - 2; return ret * 10 - 2;
} }
if (ret != sizeof(struct bmp_header)) { if (ret != sizeof(struct bmp_header)) {
DEBUGF("read_bmp_file: can't read BMP header."); DEBUGF("read_bmp_fd: can't read BMP header.");
close(fd);
return -3; return -3;
} }
width = readlong(&bmph.width); width = readlong(&bmph.width);
if (width > LCD_WIDTH) { if (width > LCD_WIDTH) {
DEBUGF("read_bmp_file: Bitmap too wide (%d pixels, max is %d)\n", DEBUGF("read_bmp_fd: Bitmap too wide (%d pixels, max is %d)\n",
width, LCD_WIDTH); width, LCD_WIDTH);
close(fd);
return -4; return -4;
} }
@ -267,9 +282,8 @@ int read_bmp_file(char* filename,
/* Check if this fits the buffer */ /* Check if this fits the buffer */
if (totalsize > maxsize) { if (totalsize > maxsize) {
DEBUGF("read_bmp_file: Bitmap too large for buffer: " DEBUGF("read_bmp_fd: Bitmap too large for buffer: "
"%d bytes.\n", totalsize); "%d bytes.\n", totalsize);
close(fd);
return -6; return -6;
} }
@ -285,8 +299,7 @@ int read_bmp_file(char* filename,
if (read(fd, palette, numcolors * sizeof(uint32_t)) if (read(fd, palette, numcolors * sizeof(uint32_t))
!= numcolors * (int)sizeof(uint32_t)) != numcolors * (int)sizeof(uint32_t))
{ {
DEBUGF("read_bmp_file: Can't read color palette\n"); DEBUGF("read_bmp_fd: Can't read color palette\n");
close(fd);
return -7; return -7;
} }
} }
@ -320,9 +333,8 @@ int read_bmp_file(char* filename,
default: default:
if (compression != 0) { /* not BI_RGB */ if (compression != 0) { /* not BI_RGB */
DEBUGF("read_bmp_file: Unsupported compression (type %d)\n", DEBUGF("read_bmp_fd: Unsupported compression (type %d)\n",
compression); compression);
close(fd);
return -8; return -8;
} }
break; break;
@ -345,9 +357,8 @@ int read_bmp_file(char* filename,
/* read one row */ /* read one row */
ret = read(fd, bmpbuf, padded_width); ret = read(fd, bmpbuf, padded_width);
if (ret != padded_width) { if (ret != padded_width) {
DEBUGF("read_bmp_file: error reading image, read returned: %d " DEBUGF("read_bmp_fd: error reading image, read returned: %d "
"expected: %d\n", ret, padded_width); "expected: %d\n", ret, padded_width);
close(fd);
return -9; return -9;
} }
@ -537,8 +548,6 @@ int read_bmp_file(char* filename,
} }
} }
close(fd);
DEBUGF("totalsize: %d\n", totalsize); DEBUGF("totalsize: %d\n", totalsize);
return totalsize; /* return the used buffer size. */ return totalsize; /* return the used buffer size. */
} }

View file

@ -26,12 +26,17 @@
* read_bmp_file() * read_bmp_file()
* *
* Reads a 8bit BMP file and puts the data in a 1-pixel-per-byte * Reads a 8bit BMP file and puts the data in a 1-pixel-per-byte
* array. * array.
* Returns < 0 for error, or number of bytes used from the bitmap buffer * Returns < 0 for error, or number of bytes used from the bitmap buffer
* *
**********************************************/ **********************************************/
int read_bmp_file(char* filename, int read_bmp_file(const char* filename,
struct bitmap *bm, struct bitmap *bm,
int maxsize, int maxsize,
int format); int format);
int read_bmp_fd(int fd,
struct bitmap *bm,
int maxsize,
int format);
#endif #endif