2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/video.h>
20 #include <grub/bitmap.h>
21 #include <grub/types.h>
24 #include <grub/misc.h>
25 #include <grub/i18n.h>
27 GRUB_MOD_LICENSE ("GPLv3+");
29 /* List of bitmap readers registered to system. */
30 static grub_video_bitmap_reader_t bitmap_readers_list
;
32 /* Register bitmap reader. */
34 grub_video_bitmap_reader_register (grub_video_bitmap_reader_t reader
)
36 reader
->next
= bitmap_readers_list
;
37 bitmap_readers_list
= reader
;
40 /* Unregister bitmap reader. */
42 grub_video_bitmap_reader_unregister (grub_video_bitmap_reader_t reader
)
44 grub_video_bitmap_reader_t
*p
, q
;
46 for (p
= &bitmap_readers_list
, q
= *p
; q
; p
= &(q
->next
), q
= q
->next
)
54 /* Creates new bitmap, saves created bitmap on success to *bitmap. */
56 grub_video_bitmap_create (struct grub_video_bitmap
**bitmap
,
57 unsigned int width
, unsigned int height
,
58 enum grub_video_blit_format blit_format
)
60 struct grub_video_mode_info
*mode_info
;
64 return grub_error (GRUB_ERR_BUG
, "invalid argument");
68 if (width
== 0 || height
== 0)
69 return grub_error (GRUB_ERR_BUG
, "invalid argument");
71 *bitmap
= (struct grub_video_bitmap
*)grub_malloc (sizeof (struct grub_video_bitmap
));
75 mode_info
= &((*bitmap
)->mode_info
);
77 /* Populate mode_info. */
78 mode_info
->width
= width
;
79 mode_info
->height
= height
;
80 mode_info
->blit_format
= blit_format
;
84 case GRUB_VIDEO_BLIT_FORMAT_RGBA_8888
:
85 mode_info
->mode_type
= GRUB_VIDEO_MODE_TYPE_RGB
86 | GRUB_VIDEO_MODE_TYPE_ALPHA
;
88 mode_info
->bytes_per_pixel
= 4;
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
= 8;
97 mode_info
->reserved_field_pos
= 24;
100 case GRUB_VIDEO_BLIT_FORMAT_RGB_888
:
101 mode_info
->mode_type
= GRUB_VIDEO_MODE_TYPE_RGB
;
103 mode_info
->bytes_per_pixel
= 3;
104 mode_info
->number_of_colors
= 256;
105 mode_info
->red_mask_size
= 8;
106 mode_info
->red_field_pos
= 0;
107 mode_info
->green_mask_size
= 8;
108 mode_info
->green_field_pos
= 8;
109 mode_info
->blue_mask_size
= 8;
110 mode_info
->blue_field_pos
= 16;
111 mode_info
->reserved_mask_size
= 0;
112 mode_info
->reserved_field_pos
= 0;
115 case GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR
:
116 mode_info
->mode_type
= GRUB_VIDEO_MODE_TYPE_INDEX_COLOR
;
118 mode_info
->bytes_per_pixel
= 1;
119 mode_info
->number_of_colors
= 256;
120 mode_info
->red_mask_size
= 0;
121 mode_info
->red_field_pos
= 0;
122 mode_info
->green_mask_size
= 0;
123 mode_info
->green_field_pos
= 0;
124 mode_info
->blue_mask_size
= 0;
125 mode_info
->blue_field_pos
= 0;
126 mode_info
->reserved_mask_size
= 0;
127 mode_info
->reserved_field_pos
= 0;
134 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
135 "unsupported bitmap format");
138 mode_info
->pitch
= width
* mode_info
->bytes_per_pixel
;
140 /* Calculate size needed for the data. */
141 size
= (width
* mode_info
->bytes_per_pixel
) * height
;
143 (*bitmap
)->data
= grub_zalloc (size
);
144 if (! (*bitmap
)->data
)
152 return GRUB_ERR_NONE
;
155 /* Frees all resources allocated by bitmap. */
157 grub_video_bitmap_destroy (struct grub_video_bitmap
*bitmap
)
160 return GRUB_ERR_NONE
;
162 grub_free (bitmap
->data
);
165 return GRUB_ERR_NONE
;
168 /* Match extension to filename. */
170 match_extension (const char *filename
, const char *ext
)
175 pos
= grub_strlen (filename
);
176 ext_len
= grub_strlen (ext
);
178 if (! pos
|| ! ext_len
|| ext_len
> pos
)
183 return grub_strcasecmp (filename
+ pos
, ext
) == 0;
186 /* Loads bitmap using registered bitmap readers. */
188 grub_video_bitmap_load (struct grub_video_bitmap
**bitmap
,
189 const char *filename
)
191 grub_video_bitmap_reader_t reader
= bitmap_readers_list
;
194 return grub_error (GRUB_ERR_BUG
, "invalid argument");
200 if (match_extension (filename
, reader
->extension
))
201 return reader
->reader (bitmap
, filename
);
203 reader
= reader
->next
;
206 return grub_error (GRUB_ERR_BAD_FILE_TYPE
,
207 /* TRANSLATORS: We're speaking about bitmap images like
209 N_("bitmap file `%s' is of"
210 " unsupported format"), filename
);
213 /* Return mode info for bitmap. */
214 void grub_video_bitmap_get_mode_info (struct grub_video_bitmap
*bitmap
,
215 struct grub_video_mode_info
*mode_info
)
220 *mode_info
= bitmap
->mode_info
;
223 /* Return pointer to bitmap's raw data. */
224 void *grub_video_bitmap_get_data (struct grub_video_bitmap
*bitmap
)