bitmap.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/video.h>
  19. #include <grub/bitmap.h>
  20. #include <grub/types.h>
  21. #include <grub/dl.h>
  22. #include <grub/mm.h>
  23. #include <grub/misc.h>
  24. #include <grub/i18n.h>
  25. #include <grub/safemath.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. /* List of bitmap readers registered to system. */
  28. static grub_video_bitmap_reader_t bitmap_readers_list;
  29. /* Register bitmap reader. */
  30. void
  31. grub_video_bitmap_reader_register (grub_video_bitmap_reader_t reader)
  32. {
  33. reader->next = bitmap_readers_list;
  34. bitmap_readers_list = reader;
  35. }
  36. /* Unregister bitmap reader. */
  37. void
  38. grub_video_bitmap_reader_unregister (grub_video_bitmap_reader_t reader)
  39. {
  40. grub_video_bitmap_reader_t *p, q;
  41. for (p = &bitmap_readers_list, q = *p; q; p = &(q->next), q = q->next)
  42. if (q == reader)
  43. {
  44. *p = q->next;
  45. break;
  46. }
  47. }
  48. /* Creates new bitmap, saves created bitmap on success to *bitmap. */
  49. grub_err_t
  50. grub_video_bitmap_create (struct grub_video_bitmap **bitmap,
  51. unsigned int width, unsigned int height,
  52. enum grub_video_blit_format blit_format)
  53. {
  54. struct grub_video_mode_info *mode_info;
  55. grub_size_t size;
  56. if (!bitmap)
  57. return grub_error (GRUB_ERR_BUG, "invalid argument");
  58. *bitmap = 0;
  59. if (width == 0 || height == 0)
  60. return grub_error (GRUB_ERR_BUG, "invalid argument");
  61. *bitmap = (struct grub_video_bitmap *)grub_malloc (sizeof (struct grub_video_bitmap));
  62. if (! *bitmap)
  63. return grub_errno;
  64. mode_info = &((*bitmap)->mode_info);
  65. /* Populate mode_info. */
  66. mode_info->width = width;
  67. mode_info->height = height;
  68. mode_info->blit_format = blit_format;
  69. switch (blit_format)
  70. {
  71. case GRUB_VIDEO_BLIT_FORMAT_RGBA_8888:
  72. mode_info->mode_type = GRUB_VIDEO_MODE_TYPE_RGB
  73. | GRUB_VIDEO_MODE_TYPE_ALPHA;
  74. mode_info->bpp = 32;
  75. mode_info->bytes_per_pixel = 4;
  76. mode_info->number_of_colors = 256;
  77. mode_info->red_mask_size = 8;
  78. mode_info->red_field_pos = 0;
  79. mode_info->green_mask_size = 8;
  80. mode_info->green_field_pos = 8;
  81. mode_info->blue_mask_size = 8;
  82. mode_info->blue_field_pos = 16;
  83. mode_info->reserved_mask_size = 8;
  84. mode_info->reserved_field_pos = 24;
  85. break;
  86. case GRUB_VIDEO_BLIT_FORMAT_RGB_888:
  87. mode_info->mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
  88. mode_info->bpp = 24;
  89. mode_info->bytes_per_pixel = 3;
  90. mode_info->number_of_colors = 256;
  91. mode_info->red_mask_size = 8;
  92. mode_info->red_field_pos = 0;
  93. mode_info->green_mask_size = 8;
  94. mode_info->green_field_pos = 8;
  95. mode_info->blue_mask_size = 8;
  96. mode_info->blue_field_pos = 16;
  97. mode_info->reserved_mask_size = 0;
  98. mode_info->reserved_field_pos = 0;
  99. break;
  100. case GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR:
  101. mode_info->mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
  102. mode_info->bpp = 8;
  103. mode_info->bytes_per_pixel = 1;
  104. mode_info->number_of_colors = 256;
  105. mode_info->red_mask_size = 0;
  106. mode_info->red_field_pos = 0;
  107. mode_info->green_mask_size = 0;
  108. mode_info->green_field_pos = 0;
  109. mode_info->blue_mask_size = 0;
  110. mode_info->blue_field_pos = 0;
  111. mode_info->reserved_mask_size = 0;
  112. mode_info->reserved_field_pos = 0;
  113. break;
  114. default:
  115. grub_free (*bitmap);
  116. *bitmap = 0;
  117. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  118. "unsupported bitmap format");
  119. }
  120. mode_info->pitch = width * mode_info->bytes_per_pixel;
  121. /* Calculate size needed for the data. */
  122. if (grub_mul (width, mode_info->bytes_per_pixel, &size) ||
  123. grub_mul (size, height, &size))
  124. {
  125. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
  126. goto fail;
  127. }
  128. (*bitmap)->data = grub_zalloc (size);
  129. if (! (*bitmap)->data)
  130. goto fail;
  131. return GRUB_ERR_NONE;
  132. fail:
  133. grub_free (*bitmap);
  134. *bitmap = NULL;
  135. return grub_errno;
  136. }
  137. /* Frees all resources allocated by bitmap. */
  138. grub_err_t
  139. grub_video_bitmap_destroy (struct grub_video_bitmap *bitmap)
  140. {
  141. if (! bitmap)
  142. return GRUB_ERR_NONE;
  143. grub_free (bitmap->data);
  144. grub_free (bitmap);
  145. return GRUB_ERR_NONE;
  146. }
  147. /* Match extension to filename. */
  148. static int
  149. match_extension (const char *filename, const char *ext)
  150. {
  151. int pos;
  152. int ext_len;
  153. pos = grub_strlen (filename);
  154. ext_len = grub_strlen (ext);
  155. if (! pos || ! ext_len || ext_len > pos)
  156. return 0;
  157. pos -= ext_len;
  158. return grub_strcasecmp (filename + pos, ext) == 0;
  159. }
  160. /* Loads bitmap using registered bitmap readers. */
  161. grub_err_t
  162. grub_video_bitmap_load (struct grub_video_bitmap **bitmap,
  163. const char *filename)
  164. {
  165. grub_video_bitmap_reader_t reader = bitmap_readers_list;
  166. if (!bitmap)
  167. return grub_error (GRUB_ERR_BUG, "invalid argument");
  168. *bitmap = 0;
  169. while (reader)
  170. {
  171. if (match_extension (filename, reader->extension))
  172. return reader->reader (bitmap, filename);
  173. reader = reader->next;
  174. }
  175. return grub_error (GRUB_ERR_BAD_FILE_TYPE,
  176. /* TRANSLATORS: We're speaking about bitmap images like
  177. JPEG or PNG. */
  178. N_("bitmap file `%s' is of"
  179. " unsupported format"), filename);
  180. }
  181. /* Return mode info for bitmap. */
  182. void grub_video_bitmap_get_mode_info (struct grub_video_bitmap *bitmap,
  183. struct grub_video_mode_info *mode_info)
  184. {
  185. if (!bitmap)
  186. return;
  187. *mode_info = bitmap->mode_info;
  188. }
  189. /* Return pointer to bitmap's raw data. */
  190. void *grub_video_bitmap_get_data (struct grub_video_bitmap *bitmap)
  191. {
  192. if (!bitmap)
  193. return 0;
  194. return bitmap->data;
  195. }