wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
wdenk | b9bbd24 | 2003-06-30 16:24:52 +0000 | [diff] [blame] | 3 | * Detlev Zundel, DENX Software Engineering, dzu@denx.de. |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * BMP handling routines |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <bmp_layout.h> |
| 30 | #include <command.h> |
wdenk | 9ca7bbc | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 31 | #include <asm/byteorder.h> |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 32 | #include <malloc.h> |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 33 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 34 | static int bmp_info (ulong addr); |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 35 | static int bmp_display (ulong addr, int x, int y); |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 36 | |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 37 | int gunzip(void *, int, unsigned char *, unsigned long *); |
| 38 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 39 | /* |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 40 | * Allocate and decompress a BMP image using gunzip(). |
| 41 | * |
| 42 | * Returns a pointer to the decompressed image data. Must be freed by |
| 43 | * the caller after use. |
| 44 | * |
| 45 | * Returns NULL if decompression failed, or if the decompressed data |
| 46 | * didn't contain a valid BMP signature. |
| 47 | */ |
| 48 | #ifdef CONFIG_VIDEO_BMP_GZIP |
| 49 | static bmp_image_t *gunzip_bmp(unsigned long addr, unsigned long *lenp) |
| 50 | { |
| 51 | void *dst; |
| 52 | unsigned long len; |
| 53 | bmp_image_t *bmp; |
| 54 | |
| 55 | /* |
| 56 | * Decompress bmp image |
| 57 | */ |
| 58 | len = CFG_VIDEO_LOGO_MAX_SIZE; |
| 59 | dst = malloc(CFG_VIDEO_LOGO_MAX_SIZE); |
| 60 | if (dst == NULL) { |
| 61 | puts("Error: malloc in gunzip failed!\n"); |
| 62 | return NULL; |
| 63 | } |
| 64 | if (gunzip(dst, CFG_VIDEO_LOGO_MAX_SIZE, (uchar *)addr, &len) != 0) { |
| 65 | free(dst); |
| 66 | return NULL; |
| 67 | } |
| 68 | if (len == CFG_VIDEO_LOGO_MAX_SIZE) |
| 69 | puts("Image could be truncated" |
| 70 | " (increase CFG_VIDEO_LOGO_MAX_SIZE)!\n"); |
| 71 | |
| 72 | bmp = dst; |
| 73 | |
| 74 | /* |
| 75 | * Check for bmp mark 'BM' |
| 76 | */ |
| 77 | if (!((bmp->header.signature[0] == 'B') && |
| 78 | (bmp->header.signature[1] == 'M'))) { |
| 79 | free(dst); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | puts("Gzipped BMP image detected!\n"); |
| 84 | |
| 85 | return bmp; |
| 86 | } |
| 87 | #else |
Anatolij Gustschin | ea00044 | 2008-01-11 02:15:02 +0100 | [diff] [blame] | 88 | static bmp_image_t *gunzip_bmp(unsigned long addr, unsigned long *lenp) |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 89 | { |
| 90 | return NULL; |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | |
| 95 | /* |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 96 | * Subroutine: do_bmp |
| 97 | * |
| 98 | * Description: Handler for 'bmp' command.. |
| 99 | * |
| 100 | * Inputs: argv[1] contains the subcommand |
| 101 | * |
| 102 | * Return: None |
| 103 | * |
| 104 | */ |
| 105 | int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 106 | { |
| 107 | ulong addr; |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 108 | int x = 0, y = 0; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 109 | |
| 110 | switch (argc) { |
| 111 | case 2: /* use load_addr as default address */ |
| 112 | addr = load_addr; |
| 113 | break; |
| 114 | case 3: /* use argument */ |
| 115 | addr = simple_strtoul(argv[2], NULL, 16); |
| 116 | break; |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 117 | case 5: |
| 118 | addr = simple_strtoul(argv[2], NULL, 16); |
| 119 | x = simple_strtoul(argv[3], NULL, 10); |
| 120 | y = simple_strtoul(argv[4], NULL, 10); |
| 121 | break; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 122 | default: |
| 123 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | /* Allow for short names |
| 128 | * Adjust length if more sub-commands get added |
| 129 | */ |
| 130 | if (strncmp(argv[1],"info",1) == 0) { |
| 131 | return (bmp_info(addr)); |
| 132 | } else if (strncmp(argv[1],"display",1) == 0) { |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 133 | return (bmp_display(addr, x, y)); |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 134 | } else { |
| 135 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 136 | return 1; |
| 137 | } |
| 138 | } |
| 139 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 140 | U_BOOT_CMD( |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 141 | bmp, 5, 1, do_bmp, |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 142 | "bmp - manipulate BMP image data\n", |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 143 | "info <imageAddr> - display image info\n" |
| 144 | "bmp display <imageAddr> [x y] - display image at x,y\n" |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 145 | ); |
| 146 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 147 | /* |
| 148 | * Subroutine: bmp_info |
| 149 | * |
| 150 | * Description: Show information about bmp file in memory |
| 151 | * |
| 152 | * Inputs: addr address of the bmp file |
| 153 | * |
| 154 | * Return: None |
| 155 | * |
| 156 | */ |
| 157 | static int bmp_info(ulong addr) |
| 158 | { |
| 159 | bmp_image_t *bmp=(bmp_image_t *)addr; |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 160 | unsigned long len; |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 161 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 162 | if (!((bmp->header.signature[0]=='B') && |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 163 | (bmp->header.signature[1]=='M'))) |
| 164 | bmp = gunzip_bmp(addr, &len); |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 165 | |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 166 | if (bmp == NULL) { |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 167 | printf("There is no valid bmp file at the given address\n"); |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 168 | return 1; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 169 | } |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 170 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 171 | printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width), |
| 172 | le32_to_cpu(bmp->header.height)); |
| 173 | printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count)); |
| 174 | printf("Compression : %d\n", le32_to_cpu(bmp->header.compression)); |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 175 | |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 176 | if ((unsigned long)bmp != addr) |
| 177 | free(bmp); |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 178 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 179 | return(0); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Subroutine: bmp_display |
| 184 | * |
| 185 | * Description: Display bmp file located in memory |
| 186 | * |
| 187 | * Inputs: addr address of the bmp file |
| 188 | * |
| 189 | * Return: None |
| 190 | * |
| 191 | */ |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 192 | static int bmp_display(ulong addr, int x, int y) |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 193 | { |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 194 | int ret; |
| 195 | bmp_image_t *bmp = (bmp_image_t *)addr; |
| 196 | unsigned long len; |
| 197 | |
| 198 | if (!((bmp->header.signature[0]=='B') && |
| 199 | (bmp->header.signature[1]=='M'))) |
| 200 | bmp = gunzip_bmp(addr, &len); |
| 201 | |
| 202 | if (!bmp) { |
| 203 | printf("There is no valid bmp file at the given address\n"); |
| 204 | return 1; |
| 205 | } |
| 206 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 207 | #if defined(CONFIG_LCD) |
| 208 | extern int lcd_display_bitmap (ulong, int, int); |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 209 | |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 210 | ret = lcd_display_bitmap ((unsigned long)bmp, x, y); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 211 | #elif defined(CONFIG_VIDEO) |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 212 | extern int video_display_bitmap (ulong, int, int); |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 213 | |
| 214 | ret = video_display_bitmap ((unsigned long)bmp, x, y); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 215 | #else |
| 216 | # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 217 | #endif |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 218 | |
| 219 | if ((unsigned long)bmp != addr) |
| 220 | free(bmp); |
| 221 | |
| 222 | return ret; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 223 | } |