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>
26 /* List of bitmap readers registered to system. */
27 static grub_video_bitmap_reader_t bitmap_readers_list
;
29 /* Register bitmap reader. */
31 grub_video_bitmap_reader_register (grub_video_bitmap_reader_t reader
)
33 reader
->next
= bitmap_readers_list
;
34 bitmap_readers_list
= reader
;
37 /* Unregister bitmap reader. */
39 grub_video_bitmap_reader_unregister (grub_video_bitmap_reader_t reader
)
41 grub_video_bitmap_reader_t
*p
, q
;
43 for (p
= &bitmap_readers_list
, q
= *p
; q
; p
= &(q
->next
), q
= q
->next
)
51 /* Creates new bitmap, saves created bitmap on success to *bitmap. */
53 grub_video_bitmap_create (struct grub_video_bitmap
**bitmap
,
54 unsigned int width
, unsigned int height
,
55 enum grub_video_blit_format blit_format
)
57 struct grub_video_mode_info
*mode_info
;
61 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "Invalid argument.");
65 if (width
== 0 || height
== 0)
66 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "Invalid argument.");
68 *bitmap
= (struct grub_video_bitmap
*)grub_malloc (sizeof (struct grub_video_bitmap
));
72 mode_info
= &((*bitmap
)->mode_info
);
74 /* Populate mode_info. */
75 mode_info
->width
= width
;
76 mode_info
->height
= height
;
77 mode_info
->blit_format
= blit_format
;
81 case GRUB_VIDEO_BLIT_FORMAT_RGBA_8888
:
82 mode_info
->mode_type
= GRUB_VIDEO_MODE_TYPE_RGB
83 | GRUB_VIDEO_MODE_TYPE_ALPHA
;
85 mode_info
->bytes_per_pixel
= 4;
86 mode_info
->number_of_colors
= 256;
87 mode_info
->red_mask_size
= 8;
88 mode_info
->red_field_pos
= 0;
89 mode_info
->green_mask_size
= 8;
90 mode_info
->green_field_pos
= 8;
91 mode_info
->blue_mask_size
= 8;
92 mode_info
->blue_field_pos
= 16;
93 mode_info
->reserved_mask_size
= 8;
94 mode_info
->reserved_field_pos
= 24;
97 case GRUB_VIDEO_BLIT_FORMAT_RGB_888
:
98 mode_info
->mode_type
= GRUB_VIDEO_MODE_TYPE_RGB
;
100 mode_info
->bytes_per_pixel
= 3;
101 mode_info
->number_of_colors
= 256;
102 mode_info
->red_mask_size
= 8;
103 mode_info
->red_field_pos
= 0;
104 mode_info
->green_mask_size
= 8;
105 mode_info
->green_field_pos
= 8;
106 mode_info
->blue_mask_size
= 8;
107 mode_info
->blue_field_pos
= 16;
108 mode_info
->reserved_mask_size
= 0;
109 mode_info
->reserved_field_pos
= 0;
112 case GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR
:
113 mode_info
->mode_type
= GRUB_VIDEO_MODE_TYPE_INDEX_COLOR
;
115 mode_info
->bytes_per_pixel
= 1;
116 mode_info
->number_of_colors
= 256;
117 mode_info
->red_mask_size
= 0;
118 mode_info
->red_field_pos
= 0;
119 mode_info
->green_mask_size
= 0;
120 mode_info
->green_field_pos
= 0;
121 mode_info
->blue_mask_size
= 0;
122 mode_info
->blue_field_pos
= 0;
123 mode_info
->reserved_mask_size
= 0;
124 mode_info
->reserved_field_pos
= 0;
131 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
132 "Unsupported bitmap format");
135 mode_info
->pitch
= width
* mode_info
->bytes_per_pixel
;
137 /* Calculate size needed for the data. */
138 size
= (width
* mode_info
->bytes_per_pixel
) * height
;
140 (*bitmap
)->data
= grub_zalloc (size
);
141 if (! (*bitmap
)->data
)
149 return GRUB_ERR_NONE
;
152 /* Frees all resources allocated by bitmap. */
154 grub_video_bitmap_destroy (struct grub_video_bitmap
*bitmap
)
157 return GRUB_ERR_NONE
;
159 grub_free (bitmap
->data
);
162 return GRUB_ERR_NONE
;
165 /* Match extension to filename. */
167 match_extension (const char *filename
, const char *ext
)
172 pos
= grub_strlen (filename
);
173 ext_len
= grub_strlen (ext
);
175 if (! pos
|| ! ext_len
|| ext_len
> pos
)
180 return grub_strcmp (filename
+ pos
, ext
) == 0;
183 /* Loads bitmap using registered bitmap readers. */
185 grub_video_bitmap_load (struct grub_video_bitmap
**bitmap
,
186 const char *filename
)
188 grub_video_bitmap_reader_t reader
= bitmap_readers_list
;
191 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "Invalid argument.");
197 if (match_extension (filename
, reader
->extension
))
198 return reader
->reader (bitmap
, filename
);
200 reader
= reader
->next
;
203 return grub_error(GRUB_ERR_BAD_FILE_TYPE
, "unsupported bitmap format");
206 /* Return bitmap width. */
208 grub_video_bitmap_get_width (struct grub_video_bitmap
*bitmap
)
213 return bitmap
->mode_info
.width
;
216 /* Return bitmap height. */
218 grub_video_bitmap_get_height (struct grub_video_bitmap
*bitmap
)
223 return bitmap
->mode_info
.height
;
226 /* Return mode info for bitmap. */
227 void grub_video_bitmap_get_mode_info (struct grub_video_bitmap
*bitmap
,
228 struct grub_video_mode_info
*mode_info
)
233 *mode_info
= bitmap
->mode_info
;
236 /* Return pointer to bitmap's raw data. */
237 void *grub_video_bitmap_get_data (struct grub_video_bitmap
*bitmap
)
245 /* Initialize bitmap module. */
246 GRUB_MOD_INIT(video_bitmap
)
250 /* Finalize bitmap module. */
251 GRUB_MOD_FINI(video_bitmap
)