bitmap.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. GRUB_MOD_LICENSE ("GPLv3+");
  26. /* List of bitmap readers registered to system. */
  27. static grub_video_bitmap_reader_t bitmap_readers_list;
  28. /* Register bitmap reader. */
  29. void
  30. grub_video_bitmap_reader_register (grub_video_bitmap_reader_t reader)
  31. {
  32. reader->next = bitmap_readers_list;
  33. bitmap_readers_list = reader;
  34. }
  35. /* Unregister bitmap reader. */
  36. void
  37. grub_video_bitmap_reader_unregister (grub_video_bitmap_reader_t reader)
  38. {
  39. grub_video_bitmap_reader_t *p, q;
  40. for (p = &bitmap_readers_list, q = *p; q; p = &(q->next), q = q->next)
  41. if (q == reader)
  42. {
  43. *p = q->next;
  44. break;
  45. }
  46. }
  47. /* Creates new bitmap, saves created bitmap on success to *bitmap. */
  48. grub_err_t
  49. grub_video_bitmap_create (struct grub_video_bitmap **bitmap,
  50. unsigned int width, unsigned int height,
  51. enum grub_video_blit_format blit_format)
  52. {
  53. struct grub_video_mode_info *mode_info;
  54. unsigned int size;
  55. if (!bitmap)
  56. return grub_error (GRUB_ERR_BUG, "invalid argument");
  57. *bitmap = 0;
  58. if (width == 0 || height == 0)
  59. return grub_error (GRUB_ERR_BUG, "invalid argument");
  60. *bitmap = (struct grub_video_bitmap *)grub_malloc (sizeof (struct grub_video_bitmap));
  61. if (! *bitmap)
  62. return grub_errno;
  63. mode_info = &((*bitmap)->mode_info);
  64. /* Populate mode_info. */
  65. mode_info->width = width;
  66. mode_info->height = height;
  67. mode_info->blit_format = blit_format;
  68. switch (blit_format)
  69. {
  70. case GRUB_VIDEO_BLIT_FORMAT_RGBA_8888:
  71. mode_info->mode_type = GRUB_VIDEO_MODE_TYPE_RGB
  72. | GRUB_VIDEO_MODE_TYPE_ALPHA;
  73. mode_info->bpp = 32;
  74. mode_info->bytes_per_pixel = 4;
  75. mode_info->number_of_colors = 256;
  76. mode_info->red_mask_size = 8;
  77. mode_info->red_field_pos = 0;
  78. mode_info->green_mask_size = 8;
  79. mode_info->green_field_pos = 8;
  80. mode_info->blue_mask_size = 8;
  81. mode_info->blue_field_pos = 16;
  82. mode_info->reserved_mask_size = 8;
  83. mode_info->reserved_field_pos = 24;
  84. break;
  85. case GRUB_VIDEO_BLIT_FORMAT_RGB_888:
  86. mode_info->mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
  87. mode_info->bpp = 24;
  88. mode_info->bytes_per_pixel = 3;
  89. mode_info->number_of_colors = 256;
  90. mode_info->red_mask_size = 8;
  91. mode_info->red_field_pos = 0;
  92. mode_info->green_mask_size = 8;
  93. mode_info->green_field_pos = 8;
  94. mode_info->blue_mask_size = 8;
  95. mode_info->blue_field_pos = 16;
  96. mode_info->reserved_mask_size = 0;
  97. mode_info->reserved_field_pos = 0;
  98. break;
  99. case GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR:
  100. mode_info->mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
  101. mode_info->bpp = 8;
  102. mode_info->bytes_per_pixel = 1;
  103. mode_info->number_of_colors = 256;
  104. mode_info->red_mask_size = 0;
  105. mode_info->red_field_pos = 0;
  106. mode_info->green_mask_size = 0;
  107. mode_info->green_field_pos = 0;
  108. mode_info->blue_mask_size = 0;
  109. mode_info->blue_field_pos = 0;
  110. mode_info->reserved_mask_size = 0;
  111. mode_info->reserved_field_pos = 0;
  112. break;
  113. default:
  114. grub_free (*bitmap);
  115. *bitmap = 0;
  116. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  117. "unsupported bitmap format");
  118. }
  119. mode_info->pitch = width * mode_info->bytes_per_pixel;
  120. /* Calculate size needed for the data. */
  121. size = (width * mode_info->bytes_per_pixel) * height;
  122. (*bitmap)->data = grub_zalloc (size);
  123. if (! (*bitmap)->data)
  124. {
  125. grub_free (*bitmap);
  126. *bitmap = 0;
  127. return grub_errno;
  128. }
  129. return GRUB_ERR_NONE;
  130. }
  131. /* Frees all resources allocated by bitmap. */
  132. grub_err_t
  133. grub_video_bitmap_destroy (struct grub_video_bitmap *bitmap)
  134. {
  135. if (! bitmap)
  136. return GRUB_ERR_NONE;
  137. grub_free (bitmap->data);
  138. grub_free (bitmap);
  139. return GRUB_ERR_NONE;
  140. }
  141. /* Match extension to filename. */
  142. static int
  143. match_extension (const char *filename, const char *ext)
  144. {
  145. int pos;
  146. int ext_len;
  147. pos = grub_strlen (filename);
  148. ext_len = grub_strlen (ext);
  149. if (! pos || ! ext_len || ext_len > pos)
  150. return 0;
  151. pos -= ext_len;
  152. return grub_strcasecmp (filename + pos, ext) == 0;
  153. }
  154. /* Loads bitmap using registered bitmap readers. */
  155. grub_err_t
  156. grub_video_bitmap_load (struct grub_video_bitmap **bitmap,
  157. const char *filename)
  158. {
  159. grub_video_bitmap_reader_t reader = bitmap_readers_list;
  160. if (!bitmap)
  161. return grub_error (GRUB_ERR_BUG, "invalid argument");
  162. *bitmap = 0;
  163. while (reader)
  164. {
  165. if (match_extension (filename, reader->extension))
  166. return reader->reader (bitmap, filename);
  167. reader = reader->next;
  168. }
  169. return grub_error (GRUB_ERR_BAD_FILE_TYPE,
  170. /* TRANSLATORS: We're speaking about bitmap images like
  171. JPEG or PNG. */
  172. N_("bitmap file `%s' is of"
  173. " unsupported format"), filename);
  174. }
  175. /* Return mode info for bitmap. */
  176. void grub_video_bitmap_get_mode_info (struct grub_video_bitmap *bitmap,
  177. struct grub_video_mode_info *mode_info)
  178. {
  179. if (!bitmap)
  180. return;
  181. *mode_info = bitmap->mode_info;
  182. }
  183. /* Return pointer to bitmap's raw data. */
  184. void *grub_video_bitmap_get_data (struct grub_video_bitmap *bitmap)
  185. {
  186. if (!bitmap)
  187. return 0;
  188. return bitmap->data;
  189. }