~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, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

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
}