cmd_bmp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. /*
  84. * Subroutine: do_bmp
  85. *
  86. * Description: Handler for 'bmp' command..
  87. *
  88. * Inputs: argv[1] contains the subcommand
  89. *
  90. * Return: None
  91. *
  92. */
  93. int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  94. {
  95. ulong addr;
  96. int x = 0, y = 0;
  97. switch (argc) {
  98. case 2: /* use load_addr as default address */
  99. addr = load_addr;
  100. break;
  101. case 3: /* use argument */
  102. addr = simple_strtoul(argv[2], NULL, 16);
  103. break;
  104. case 5:
  105. addr = simple_strtoul(argv[2], NULL, 16);
  106. x = simple_strtoul(argv[3], NULL, 10);
  107. y = simple_strtoul(argv[4], NULL, 10);
  108. break;
  109. default:
  110. cmd_usage(cmdtp);
  111. return 1;
  112. }
  113. /* Allow for short names
  114. * Adjust length if more sub-commands get added
  115. */
  116. if (strncmp(argv[1],"info",1) == 0) {
  117. return (bmp_info(addr));
  118. } else if (strncmp(argv[1],"display",1) == 0) {
  119. return (bmp_display(addr, x, y));
  120. } else {
  121. cmd_usage(cmdtp);
  122. return 1;
  123. }
  124. }
  125. U_BOOT_CMD(
  126. bmp, 5, 1, do_bmp,
  127. "manipulate BMP image data",
  128. "info <imageAddr> - display image info\n"
  129. "bmp display <imageAddr> [x y] - display image at x,y"
  130. );
  131. /*
  132. * Subroutine: bmp_info
  133. *
  134. * Description: Show information about bmp file in memory
  135. *
  136. * Inputs: addr address of the bmp file
  137. *
  138. * Return: None
  139. *
  140. */
  141. static int bmp_info(ulong addr)
  142. {
  143. bmp_image_t *bmp=(bmp_image_t *)addr;
  144. unsigned long len;
  145. if (!((bmp->header.signature[0]=='B') &&
  146. (bmp->header.signature[1]=='M')))
  147. bmp = gunzip_bmp(addr, &len);
  148. if (bmp == NULL) {
  149. printf("There is no valid bmp file at the given address\n");
  150. return 1;
  151. }
  152. printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
  153. le32_to_cpu(bmp->header.height));
  154. printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
  155. printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
  156. if ((unsigned long)bmp != addr)
  157. free(bmp);
  158. return(0);
  159. }
  160. /*
  161. * Subroutine: bmp_display
  162. *
  163. * Description: Display bmp file located in memory
  164. *
  165. * Inputs: addr address of the bmp file
  166. *
  167. * Return: None
  168. *
  169. */
  170. static int bmp_display(ulong addr, int x, int y)
  171. {
  172. int ret;
  173. bmp_image_t *bmp = (bmp_image_t *)addr;
  174. unsigned long len;
  175. if (!((bmp->header.signature[0]=='B') &&
  176. (bmp->header.signature[1]=='M')))
  177. bmp = gunzip_bmp(addr, &len);
  178. if (!bmp) {
  179. printf("There is no valid bmp file at the given address\n");
  180. return 1;
  181. }
  182. #if defined(CONFIG_LCD)
  183. extern int lcd_display_bitmap (ulong, int, int);
  184. ret = lcd_display_bitmap ((unsigned long)bmp, x, y);
  185. #elif defined(CONFIG_VIDEO)
  186. extern int video_display_bitmap (ulong, int, int);
  187. ret = video_display_bitmap ((unsigned long)bmp, x, y);
  188. #else
  189. # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
  190. #endif
  191. if ((unsigned long)bmp != addr)
  192. free(bmp);
  193. return ret;
  194. }