2009-11-02 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2/jjazz.git] / video / bitmap.c
blob7b135a5dc459476a052550832e5694ca3d0abf6f
1 /*
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>
22 #include <grub/dl.h>
23 #include <grub/mm.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. */
30 void
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. */
38 void
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)
44 if (q == reader)
46 *p = q->next;
47 break;
51 /* Creates new bitmap, saves created bitmap on success to *bitmap. */
52 grub_err_t
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;
58 unsigned int size;
60 if (!bitmap)
61 return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument.");
63 *bitmap = 0;
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));
69 if (! *bitmap)
70 return grub_errno;
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;
79 switch (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;
84 mode_info->bpp = 32;
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;
95 break;
97 case GRUB_VIDEO_BLIT_FORMAT_RGB_888:
98 mode_info->mode_type = GRUB_VIDEO_MODE_TYPE_RGB;
99 mode_info->bpp = 24;
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;
110 break;
112 case GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR:
113 mode_info->mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
114 mode_info->bpp = 8;
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;
125 break;
127 default:
128 grub_free (*bitmap);
129 *bitmap = 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)
143 grub_free (*bitmap);
144 *bitmap = 0;
146 return grub_errno;
149 return GRUB_ERR_NONE;
152 /* Frees all resources allocated by bitmap. */
153 grub_err_t
154 grub_video_bitmap_destroy (struct grub_video_bitmap *bitmap)
156 if (! bitmap)
157 return GRUB_ERR_NONE;
159 grub_free (bitmap->data);
160 grub_free (bitmap);
162 return GRUB_ERR_NONE;
165 /* Match extension to filename. */
166 static int
167 match_extension (const char *filename, const char *ext)
169 int pos;
170 int ext_len;
172 pos = grub_strlen (filename);
173 ext_len = grub_strlen (ext);
175 if (! pos || ! ext_len || ext_len > pos)
176 return 0;
178 pos -= ext_len;
180 return grub_strcmp (filename + pos, ext) == 0;
183 /* Loads bitmap using registered bitmap readers. */
184 grub_err_t
185 grub_video_bitmap_load (struct grub_video_bitmap **bitmap,
186 const char *filename)
188 grub_video_bitmap_reader_t reader = bitmap_readers_list;
190 if (!bitmap)
191 return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument.");
193 *bitmap = 0;
195 while (reader)
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. */
207 unsigned int
208 grub_video_bitmap_get_width (struct grub_video_bitmap *bitmap)
210 if (!bitmap)
211 return 0;
213 return bitmap->mode_info.width;
216 /* Return bitmap height. */
217 unsigned int
218 grub_video_bitmap_get_height (struct grub_video_bitmap *bitmap)
220 if (!bitmap)
221 return 0;
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)
230 if (!bitmap)
231 return;
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)
239 if (!bitmap)
240 return 0;
242 return bitmap->data;
245 /* Initialize bitmap module. */
246 GRUB_MOD_INIT(video_bitmap)
250 /* Finalize bitmap module. */
251 GRUB_MOD_FINI(video_bitmap)