cmd_bmp.c 5.0 KB

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