cmd_bmp.c 6.0 KB

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