cmd_bmp.c 6.0 KB

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