removed accidental commit
[grub2/phcoder.git] / video / fb / fbutil.c
blob09cbb12f96e3013ec6883a4985cec42315050f6d
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2009 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/fbutil.h>
20 #include <grub/types.h>
21 #include <grub/video.h>
23 grub_uint8_t *
24 get_data_ptr (struct grub_video_fbblit_info *source,
25 unsigned int x, unsigned int y)
27 grub_uint8_t *ptr = 0;
29 switch (source->mode_info->bpp)
31 case 32:
32 ptr = (grub_uint8_t *)source->data
33 + y * source->mode_info->pitch
34 + x * 4;
35 break;
37 case 24:
38 ptr = (grub_uint8_t *)source->data
39 + y * source->mode_info->pitch
40 + x * 3;
41 break;
43 case 16:
44 case 15:
45 ptr = (grub_uint8_t *)source->data
46 + y * source->mode_info->pitch
47 + x * 2;
48 break;
50 case 8:
51 ptr = (grub_uint8_t *)source->data
52 + y * source->mode_info->pitch
53 + x;
54 break;
56 case 1:
57 /* For 1-bit bitmaps, addressing needs to be done at the bit level
58 and it doesn't make sense, in general, to ask for a pointer
59 to a particular pixel's data. */
60 break;
63 return ptr;
66 grub_video_color_t
67 get_pixel (struct grub_video_fbblit_info *source,
68 unsigned int x, unsigned int y)
70 grub_video_color_t color = 0;
72 switch (source->mode_info->bpp)
74 case 32:
75 color = *(grub_uint32_t *)get_data_ptr (source, x, y);
76 break;
78 case 24:
80 grub_uint8_t *ptr;
81 ptr = get_data_ptr (source, x, y);
82 color = ptr[0] | (ptr[1] << 8) | (ptr[2] << 16);
84 break;
86 case 16:
87 case 15:
88 color = *(grub_uint16_t *)get_data_ptr (source, x, y);
89 break;
91 case 8:
92 color = *(grub_uint8_t *)get_data_ptr (source, x, y);
93 break;
95 case 1:
96 if (source->mode_info->blit_format == GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED)
98 int bit_index = y * source->mode_info->width + x;
99 grub_uint8_t *ptr = (grub_uint8_t *)source->data
100 + bit_index / 8;
101 int bit_pos = 7 - bit_index % 8;
102 color = (*ptr >> bit_pos) & 0x01;
104 break;
106 default:
107 break;
110 return color;
113 void
114 set_pixel (struct grub_video_fbblit_info *source,
115 unsigned int x, unsigned int y, grub_video_color_t color)
117 switch (source->mode_info->bpp)
119 case 32:
121 grub_uint32_t *ptr;
123 ptr = (grub_uint32_t *)get_data_ptr (source, x, y);
125 *ptr = color;
127 break;
129 case 24:
131 grub_uint8_t *ptr;
132 grub_uint8_t *colorptr = (grub_uint8_t *)&color;
134 ptr = get_data_ptr (source, x, y);
136 ptr[0] = colorptr[0];
137 ptr[1] = colorptr[1];
138 ptr[2] = colorptr[2];
140 break;
142 case 16:
143 case 15:
145 grub_uint16_t *ptr;
147 ptr = (grub_uint16_t *)get_data_ptr (source, x, y);
149 *ptr = (grub_uint16_t) (color & 0xFFFF);
151 break;
153 case 8:
155 grub_uint8_t *ptr;
157 ptr = (grub_uint8_t *)get_data_ptr (source, x, y);
159 *ptr = (grub_uint8_t) (color & 0xFF);
161 break;
163 case 1:
164 if (source->mode_info->blit_format == GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED)
166 int bit_index = y * source->mode_info->width + x;
167 grub_uint8_t *ptr = (grub_uint8_t *)source->data
168 + bit_index / 8;
169 int bit_pos = 7 - bit_index % 8;
170 *ptr = (*ptr & ~(1 << bit_pos)) | ((color & 0x01) << bit_pos);
172 break;
174 default:
175 break;