~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/video/bitmap_scale.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* bitmap_scale.c - Bitmap scaling. */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2006,2007,2008,2009  Free Software Foundation, Inc.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include <grub/mm.h>
 
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>
 
26
 
 
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);
 
32
 
 
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.
 
38
 
 
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.  */
 
43
grub_err_t
 
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
 
48
                                 scale_method)
 
49
{
 
50
  *dst = 0;
 
51
 
 
52
  /* Verify the simplifying assumptions. */
 
53
  if (src == 0)
 
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");
 
71
 
 
72
  /* Create the new bitmap. */
 
73
  grub_err_t ret;
 
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. */
 
78
 
 
79
  switch (scale_method)
 
80
    {
 
81
    case GRUB_VIDEO_BITMAP_SCALE_METHOD_FASTEST:
 
82
    case GRUB_VIDEO_BITMAP_SCALE_METHOD_NEAREST:
 
83
      ret = scale_nn (*dst, src);
 
84
      break;
 
85
    case GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST:
 
86
    case GRUB_VIDEO_BITMAP_SCALE_METHOD_BILINEAR:
 
87
      ret = scale_bilinear (*dst, src);
 
88
      break;
 
89
    default:
 
90
      ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid scale_method value");
 
91
      break;
 
92
    }
 
93
 
 
94
  if (ret == GRUB_ERR_NONE)
 
95
    {
 
96
      /* Success:  *dst is now a pointer to the scaled bitmap. */
 
97
      return GRUB_ERR_NONE;
 
98
    }
 
99
  else
 
100
    {
 
101
      /* Destroy the bitmap and return the error code. */
 
102
      grub_video_bitmap_destroy (*dst);
 
103
      *dst = 0;
 
104
      return ret;
 
105
    }
 
106
}
 
107
 
 
108
/* Nearest neighbor bitmap scaling algorithm.
 
109
 
 
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.
 
113
 
 
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.  */
 
118
static grub_err_t
 
119
scale_nn (struct grub_video_bitmap *dst, struct grub_video_bitmap *src)
 
120
{
 
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");
 
150
 
 
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;
 
161
 
 
162
  int dy;
 
163
  for (dy = 0; dy < dh; dy++)
 
164
    {
 
165
      int dx;
 
166
      for (dx = 0; dx < dw; dx++)
 
167
        {
 
168
          grub_uint8_t *dptr;
 
169
          grub_uint8_t *sptr;
 
170
          int sx;
 
171
          int sy;
 
172
          int comp;
 
173
 
 
174
          /* Compute the source coordinate that the destination coordinate
 
175
             maps to.  Note: sx/sw = dx/dw  =>  sx = sw*dx/dw. */
 
176
          sx = sw * dx / dw;
 
177
          sy = sh * dy / dh;
 
178
 
 
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;
 
182
 
 
183
          /* Copy the pixel color value. */
 
184
          for (comp = 0; comp < bytes_per_pixel; comp++)
 
185
            dptr[comp] = sptr[comp];
 
186
        }
 
187
    }
 
188
  return GRUB_ERR_NONE;
 
189
}
 
190
 
 
191
/* Bilinear interpolation image scaling algorithm.
 
192
 
 
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.
 
196
 
 
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.  */
 
201
static grub_err_t
 
202
scale_bilinear (struct grub_video_bitmap *dst, struct grub_video_bitmap *src)
 
203
{
 
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");
 
233
 
 
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;
 
244
 
 
245
  int dy;
 
246
  for (dy = 0; dy < dh; dy++)
 
247
    {
 
248
      int dx;
 
249
      for (dx = 0; dx < dw; dx++)
 
250
        {
 
251
          grub_uint8_t *dptr;
 
252
          grub_uint8_t *sptr;
 
253
          int sx;
 
254
          int sy;
 
255
          int comp;
 
256
 
 
257
          /* Compute the source coordinate that the destination coordinate
 
258
             maps to.  Note: sx/sw = dx/dw  =>  sx = sw*dx/dw. */
 
259
          sx = sw * dx / dw;
 
260
          sy = sh * dy / dh;
 
261
 
 
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;
 
265
 
 
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)
 
269
            {
 
270
              /* Do bilinear interpolation. */
 
271
 
 
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);
 
277
 
 
278
              for (comp = 0; comp < bytes_per_pixel; comp++)
 
279
                {
 
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];
 
286
 
 
287
                  /* Do linear interpolations along the top and bottom
 
288
                     rows of the box. */
 
289
                  grub_uint8_t f0y = (256 - v) * f00 / 256 + v * f01 / 256;
 
290
                  grub_uint8_t f1y = (256 - v) * f10 / 256 + v * f11 / 256;
 
291
 
 
292
                  /* Interpolate vertically. */
 
293
                  grub_uint8_t fxy = (256 - u) * f0y / 256 + u * f1y / 256;
 
294
 
 
295
                  dptr[comp] = fxy;
 
296
                }
 
297
            }
 
298
          else
 
299
            {
 
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];
 
304
            }
 
305
        }
 
306
    }
 
307
  return GRUB_ERR_NONE;
 
308
}