cmd_bmp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * (C) Copyright 2002
  3. * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
  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. * BMP handling routines
  25. */
  26. #include <common.h>
  27. #include <lcd.h>
  28. #include <bmp_layout.h>
  29. #include <command.h>
  30. #include <asm/byteorder.h>
  31. #include <malloc.h>
  32. static int bmp_info (ulong addr);
  33. /*
  34. * Allocate and decompress a BMP image using gunzip().
  35. *
  36. * Returns a pointer to the decompressed image data. Must be freed by
  37. * the caller after use.
  38. *
  39. * Returns NULL if decompression failed, or if the decompressed data
  40. * didn't contain a valid BMP signature.
  41. */
  42. #ifdef CONFIG_VIDEO_BMP_GZIP
  43. bmp_image_t *gunzip_bmp(unsigned long addr, unsigned long *lenp)
  44. {
  45. void *dst;
  46. unsigned long len;
  47. bmp_image_t *bmp;
  48. /*
  49. * Decompress bmp image
  50. */
  51. len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
  52. dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE);
  53. if (dst == NULL) {
  54. puts("Error: malloc in gunzip failed!\n");
  55. return NULL;
  56. }
  57. if (gunzip(dst, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, (uchar *)addr, &len) != 0) {
  58. free(dst);
  59. return NULL;
  60. }
  61. if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
  62. puts("Image could be truncated"
  63. " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
  64. bmp = dst;
  65. /*
  66. * Check for bmp mark 'BM'
  67. */
  68. if (!((bmp->header.signature[0] == 'B') &&
  69. (bmp->header.signature[1] == 'M'))) {
  70. free(dst);
  71. return NULL;
  72. }
  73. debug("Gzipped BMP image detected!\n");
  74. return bmp;
  75. }
  76. #else
  77. bmp_image_t *gunzip_bmp(unsigned long addr, unsigned long *lenp)
  78. {
  79. return NULL;
  80. }
  81. #endif
  82. static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  83. {
  84. ulong addr;
  85. switch (argc) {
  86. case 1: /* use load_addr as default address */
  87. addr = load_addr;
  88. break;
  89. case 2: /* use argument */
  90. addr = simple_strtoul(argv[1], NULL, 16);
  91. break;
  92. default:
  93. return CMD_RET_USAGE;
  94. }
  95. return (bmp_info(addr));
  96. }
  97. static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  98. {
  99. ulong addr;
  100. int x = 0, y = 0;
  101. switch (argc) {
  102. case 1: /* use load_addr as default address */
  103. addr = load_addr;
  104. break;
  105. case 2: /* use argument */
  106. addr = simple_strtoul(argv[1], NULL, 16);
  107. break;
  108. case 4:
  109. addr = simple_strtoul(argv[1], NULL, 16);
  110. x = simple_strtoul(argv[2], NULL, 10);
  111. y = simple_strtoul(argv[3], NULL, 10);
  112. break;
  113. default:
  114. return CMD_RET_USAGE;
  115. }
  116. return (bmp_display(addr, x, y));
  117. }
  118. static cmd_tbl_t cmd_bmp_sub[] = {
  119. U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
  120. U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
  121. };
  122. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  123. void bmp_reloc(void) {
  124. fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
  125. }
  126. #endif
  127. /*
  128. * Subroutine: do_bmp
  129. *
  130. * Description: Handler for 'bmp' command..
  131. *
  132. * Inputs: argv[1] contains the subcommand
  133. *
  134. * Return: None
  135. *
  136. */
  137. static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  138. {
  139. cmd_tbl_t *c;
  140. /* Strip off leading 'bmp' command argument */
  141. argc--;
  142. argv++;
  143. c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
  144. if (c)
  145. return c->cmd(cmdtp, flag, argc, argv);
  146. else
  147. return CMD_RET_USAGE;
  148. }
  149. U_BOOT_CMD(
  150. bmp, 5, 1, do_bmp,
  151. "manipulate BMP image data",
  152. "info <imageAddr> - display image info\n"
  153. "bmp display <imageAddr> [x y] - display image at x,y"
  154. );
  155. /*
  156. * Subroutine: bmp_info
  157. *
  158. * Description: Show information about bmp file in memory
  159. *
  160. * Inputs: addr address of the bmp file
  161. *
  162. * Return: None
  163. *
  164. */
  165. static int bmp_info(ulong addr)
  166. {
  167. bmp_image_t *bmp=(bmp_image_t *)addr;
  168. unsigned long len;
  169. if (!((bmp->header.signature[0]=='B') &&
  170. (bmp->header.signature[1]=='M')))
  171. bmp = gunzip_bmp(addr, &len);
  172. if (bmp == NULL) {
  173. printf("There is no valid bmp file at the given address\n");
  174. return 1;
  175. }
  176. printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
  177. le32_to_cpu(bmp->header.height));
  178. printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
  179. printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
  180. if ((unsigned long)bmp != addr)
  181. free(bmp);
  182. return(0);
  183. }
  184. /*
  185. * Subroutine: bmp_display
  186. *
  187. * Description: Display bmp file located in memory
  188. *
  189. * Inputs: addr address of the bmp file
  190. *
  191. * Return: None
  192. *
  193. */
  194. int bmp_display(ulong addr, int x, int y)
  195. {
  196. int ret;
  197. bmp_image_t *bmp = (bmp_image_t *)addr;
  198. unsigned long len;
  199. if (!((bmp->header.signature[0]=='B') &&
  200. (bmp->header.signature[1]=='M')))
  201. bmp = gunzip_bmp(addr, &len);
  202. if (!bmp) {
  203. printf("There is no valid bmp file at the given address\n");
  204. return 1;
  205. }
  206. #if defined(CONFIG_LCD)
  207. ret = lcd_display_bitmap((ulong)bmp, x, y);
  208. #elif defined(CONFIG_VIDEO)
  209. extern int video_display_bitmap (ulong, int, int);
  210. ret = video_display_bitmap ((unsigned long)bmp, x, y);
  211. #else
  212. # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
  213. #endif
  214. if ((unsigned long)bmp != addr)
  215. free(bmp);
  216. return ret;
  217. }