cmd_bmp.c 5.7 KB

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