1 /* bitmap_scale.c - Bitmap scaling. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/misc.h>
22 #include <grub/video.h>
23 #include <grub/bitmap.h>
24 #include <grub/bitmap_scale.h>
25 #include <grub/types.h>
27 /* Prototypes for module-local functions. */
28 static grub_err_t
scale_nn (struct grub_video_bitmap
*dst
,
29 struct grub_video_bitmap
*src
);
30 static grub_err_t
scale_bilinear (struct grub_video_bitmap
*dst
,
31 struct grub_video_bitmap
*src
);
33 /* This function creates a new scaled version of the bitmap SRC. The new
34 bitmap has dimensions DST_WIDTH by DST_HEIGHT. The scaling algorithm
35 is given by SCALE_METHOD. If an error is encountered, the return code is
36 not equal to GRUB_ERR_NONE, and the bitmap DST is either not created, or
37 it is destroyed before this function returns.
39 Supports only direct color modes which have components separated
40 into bytes (e.g., RGBA 8:8:8:8 or BGR 8:8:8 true color).
41 But because of this simplifying assumption, the implementation is
42 greatly simplified. */
44 grub_video_bitmap_create_scaled (struct grub_video_bitmap
**dst
,
45 int dst_width
, int dst_height
,
46 struct grub_video_bitmap
*src
,
47 enum grub_video_bitmap_scale_method
52 /* Verify the simplifying assumptions. */
54 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
55 "null src bitmap in grub_video_bitmap_create_scaled");
56 if (src
->mode_info
.red_field_pos
% 8 != 0
57 || src
->mode_info
.green_field_pos
% 8 != 0
58 || src
->mode_info
.blue_field_pos
% 8 != 0
59 || src
->mode_info
.reserved_field_pos
% 8 != 0)
60 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
61 "src format not supported for scale");
62 if (src
->mode_info
.width
== 0 || src
->mode_info
.height
== 0)
63 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
64 "source bitmap has a zero dimension");
65 if (dst_width
<= 0 || dst_height
<= 0)
66 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
67 "requested to scale to a size w/ a zero dimension");
68 if (src
->mode_info
.bytes_per_pixel
* 8 != src
->mode_info
.bpp
)
69 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
70 "bitmap to scale has inconsistent Bpp and bpp");
72 /* Create the new bitmap. */
74 ret
= grub_video_bitmap_create (dst
, dst_width
, dst_height
,
75 src
->mode_info
.blit_format
);
76 if (ret
!= GRUB_ERR_NONE
)
77 return ret
; /* Error. */
81 case GRUB_VIDEO_BITMAP_SCALE_METHOD_FASTEST
:
82 case GRUB_VIDEO_BITMAP_SCALE_METHOD_NEAREST
:
83 ret
= scale_nn (*dst
, src
);
85 case GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST
:
86 case GRUB_VIDEO_BITMAP_SCALE_METHOD_BILINEAR
:
87 ret
= scale_bilinear (*dst
, src
);
90 ret
= grub_error (GRUB_ERR_BAD_ARGUMENT
, "Invalid scale_method value");
94 if (ret
== GRUB_ERR_NONE
)
96 /* Success: *dst is now a pointer to the scaled bitmap. */
101 /* Destroy the bitmap and return the error code. */
102 grub_video_bitmap_destroy (*dst
);
108 /* Nearest neighbor bitmap scaling algorithm.
110 Copy the bitmap SRC to the bitmap DST, scaling the bitmap to fit the
111 dimensions of DST. This function uses the nearest neighbor algorithm to
112 interpolate the pixels.
114 Supports only direct color modes which have components separated
115 into bytes (e.g., RGBA 8:8:8:8 or BGR 8:8:8 true color).
116 But because of this simplifying assumption, the implementation is
117 greatly simplified. */
119 scale_nn (struct grub_video_bitmap
*dst
, struct grub_video_bitmap
*src
)
121 /* Verify the simplifying assumptions. */
122 if (dst
== 0 || src
== 0)
123 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "null bitmap in scale_nn");
124 if (dst
->mode_info
.red_field_pos
% 8 != 0
125 || dst
->mode_info
.green_field_pos
% 8 != 0
126 || dst
->mode_info
.blue_field_pos
% 8 != 0
127 || dst
->mode_info
.reserved_field_pos
% 8 != 0)
128 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "dst format not supported");
129 if (src
->mode_info
.red_field_pos
% 8 != 0
130 || src
->mode_info
.green_field_pos
% 8 != 0
131 || src
->mode_info
.blue_field_pos
% 8 != 0
132 || src
->mode_info
.reserved_field_pos
% 8 != 0)
133 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "src format not supported");
134 if (dst
->mode_info
.red_field_pos
!= src
->mode_info
.red_field_pos
135 || dst
->mode_info
.red_mask_size
!= src
->mode_info
.red_mask_size
136 || dst
->mode_info
.green_field_pos
!= src
->mode_info
.green_field_pos
137 || dst
->mode_info
.green_mask_size
!= src
->mode_info
.green_mask_size
138 || dst
->mode_info
.blue_field_pos
!= src
->mode_info
.blue_field_pos
139 || dst
->mode_info
.blue_mask_size
!= src
->mode_info
.blue_mask_size
140 || dst
->mode_info
.reserved_field_pos
!=
141 src
->mode_info
.reserved_field_pos
142 || dst
->mode_info
.reserved_mask_size
!=
143 src
->mode_info
.reserved_mask_size
)
144 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "dst and src not compatible");
145 if (dst
->mode_info
.bytes_per_pixel
!= src
->mode_info
.bytes_per_pixel
)
146 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "dst and src not compatible");
147 if (dst
->mode_info
.width
== 0 || dst
->mode_info
.height
== 0
148 || src
->mode_info
.width
== 0 || src
->mode_info
.height
== 0)
149 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "bitmap has a zero dimension");
151 grub_uint8_t
*ddata
= dst
->data
;
152 grub_uint8_t
*sdata
= src
->data
;
153 int dw
= dst
->mode_info
.width
;
154 int dh
= dst
->mode_info
.height
;
155 int sw
= src
->mode_info
.width
;
156 int sh
= src
->mode_info
.height
;
157 int dstride
= dst
->mode_info
.pitch
;
158 int sstride
= src
->mode_info
.pitch
;
159 /* bytes_per_pixel is the same for both src and dst. */
160 int bytes_per_pixel
= dst
->mode_info
.bytes_per_pixel
;
163 for (dy
= 0; dy
< dh
; dy
++)
166 for (dx
= 0; dx
< dw
; dx
++)
174 /* Compute the source coordinate that the destination coordinate
175 maps to. Note: sx/sw = dx/dw => sx = sw*dx/dw. */
179 /* Get the address of the pixels in src and dst. */
180 dptr
= ddata
+ dy
* dstride
+ dx
* bytes_per_pixel
;
181 sptr
= sdata
+ sy
* sstride
+ sx
* bytes_per_pixel
;
183 /* Copy the pixel color value. */
184 for (comp
= 0; comp
< bytes_per_pixel
; comp
++)
185 dptr
[comp
] = sptr
[comp
];
188 return GRUB_ERR_NONE
;
191 /* Bilinear interpolation image scaling algorithm.
193 Copy the bitmap SRC to the bitmap DST, scaling the bitmap to fit the
194 dimensions of DST. This function uses the bilinear interpolation algorithm
195 to interpolate the pixels.
197 Supports only direct color modes which have components separated
198 into bytes (e.g., RGBA 8:8:8:8 or BGR 8:8:8 true color).
199 But because of this simplifying assumption, the implementation is
200 greatly simplified. */
202 scale_bilinear (struct grub_video_bitmap
*dst
, struct grub_video_bitmap
*src
)
204 /* Verify the simplifying assumptions. */
205 if (dst
== 0 || src
== 0)
206 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "null bitmap in scale func");
207 if (dst
->mode_info
.red_field_pos
% 8 != 0
208 || dst
->mode_info
.green_field_pos
% 8 != 0
209 || dst
->mode_info
.blue_field_pos
% 8 != 0
210 || dst
->mode_info
.reserved_field_pos
% 8 != 0)
211 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "dst format not supported");
212 if (src
->mode_info
.red_field_pos
% 8 != 0
213 || src
->mode_info
.green_field_pos
% 8 != 0
214 || src
->mode_info
.blue_field_pos
% 8 != 0
215 || src
->mode_info
.reserved_field_pos
% 8 != 0)
216 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "src format not supported");
217 if (dst
->mode_info
.red_field_pos
!= src
->mode_info
.red_field_pos
218 || dst
->mode_info
.red_mask_size
!= src
->mode_info
.red_mask_size
219 || dst
->mode_info
.green_field_pos
!= src
->mode_info
.green_field_pos
220 || dst
->mode_info
.green_mask_size
!= src
->mode_info
.green_mask_size
221 || dst
->mode_info
.blue_field_pos
!= src
->mode_info
.blue_field_pos
222 || dst
->mode_info
.blue_mask_size
!= src
->mode_info
.blue_mask_size
223 || dst
->mode_info
.reserved_field_pos
!=
224 src
->mode_info
.reserved_field_pos
225 || dst
->mode_info
.reserved_mask_size
!=
226 src
->mode_info
.reserved_mask_size
)
227 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "dst and src not compatible");
228 if (dst
->mode_info
.bytes_per_pixel
!= src
->mode_info
.bytes_per_pixel
)
229 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "dst and src not compatible");
230 if (dst
->mode_info
.width
== 0 || dst
->mode_info
.height
== 0
231 || src
->mode_info
.width
== 0 || src
->mode_info
.height
== 0)
232 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "bitmap has a zero dimension");
234 grub_uint8_t
*ddata
= dst
->data
;
235 grub_uint8_t
*sdata
= src
->data
;
236 int dw
= dst
->mode_info
.width
;
237 int dh
= dst
->mode_info
.height
;
238 int sw
= src
->mode_info
.width
;
239 int sh
= src
->mode_info
.height
;
240 int dstride
= dst
->mode_info
.pitch
;
241 int sstride
= src
->mode_info
.pitch
;
242 /* bytes_per_pixel is the same for both src and dst. */
243 int bytes_per_pixel
= dst
->mode_info
.bytes_per_pixel
;
246 for (dy
= 0; dy
< dh
; dy
++)
249 for (dx
= 0; dx
< dw
; dx
++)
257 /* Compute the source coordinate that the destination coordinate
258 maps to. Note: sx/sw = dx/dw => sx = sw*dx/dw. */
262 /* Get the address of the pixels in src and dst. */
263 dptr
= ddata
+ dy
* dstride
+ dx
* bytes_per_pixel
;
264 sptr
= sdata
+ sy
* sstride
+ sx
* bytes_per_pixel
;
266 /* If we have enough space to do so, use bilinear interpolation.
267 Otherwise, fall back to nearest neighbor for this pixel. */
268 if (sx
< sw
- 1 && sy
< sh
- 1)
270 /* Do bilinear interpolation. */
272 /* Fixed-point .8 numbers representing the fraction of the
273 distance in the x (u) and y (v) direction within the
274 box of 4 pixels in the source. */
275 int u
= (256 * sw
* dx
/ dw
) - (sx
* 256);
276 int v
= (256 * sh
* dy
/ dh
) - (sy
* 256);
278 for (comp
= 0; comp
< bytes_per_pixel
; comp
++)
280 /* Get the component's values for the
281 four source corner pixels. */
282 grub_uint8_t f00
= sptr
[comp
];
283 grub_uint8_t f10
= sptr
[comp
+ bytes_per_pixel
];
284 grub_uint8_t f01
= sptr
[comp
+ sstride
];
285 grub_uint8_t f11
= sptr
[comp
+ sstride
+ bytes_per_pixel
];
287 /* Do linear interpolations along the top and bottom
289 grub_uint8_t f0y
= (256 - v
) * f00
/ 256 + v
* f01
/ 256;
290 grub_uint8_t f1y
= (256 - v
) * f10
/ 256 + v
* f11
/ 256;
292 /* Interpolate vertically. */
293 grub_uint8_t fxy
= (256 - u
) * f0y
/ 256 + u
* f1y
/ 256;
300 /* Fall back to nearest neighbor interpolation. */
301 /* Copy the pixel color value. */
302 for (comp
= 0; comp
< bytes_per_pixel
; comp
++)
303 dptr
[comp
] = sptr
[comp
];
307 return GRUB_ERR_NONE
;