bitmap_scale.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. #include <grub/mm.h>
  20. #include <grub/misc.h>
  21. #include <grub/video.h>
  22. #include <grub/bitmap.h>
  23. #include <grub/bitmap_scale.h>
  24. #include <grub/types.h>
  25. #include <grub/dl.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  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. static grub_err_t
  33. verify_source_bitmap (struct grub_video_bitmap *src)
  34. {
  35. /* Verify the simplifying assumptions. */
  36. if (src == 0)
  37. return grub_error (GRUB_ERR_BUG,
  38. "null src bitmap in grub_video_bitmap_create_scaled");
  39. if (src->mode_info.red_field_pos % 8 != 0
  40. || src->mode_info.green_field_pos % 8 != 0
  41. || src->mode_info.blue_field_pos % 8 != 0
  42. || src->mode_info.reserved_field_pos % 8 != 0)
  43. return grub_error (GRUB_ERR_BUG,
  44. "src format not supported for scale");
  45. if (src->mode_info.width == 0 || src->mode_info.height == 0)
  46. return grub_error (GRUB_ERR_BUG,
  47. "source bitmap has a zero dimension");
  48. if (src->mode_info.bytes_per_pixel * 8 != src->mode_info.bpp)
  49. return grub_error (GRUB_ERR_BUG,
  50. "bitmap to scale has inconsistent Bpp and bpp");
  51. return GRUB_ERR_NONE;
  52. }
  53. static grub_err_t
  54. grub_video_bitmap_scale (struct grub_video_bitmap *dst,
  55. struct grub_video_bitmap *src,
  56. enum grub_video_bitmap_scale_method scale_method)
  57. {
  58. switch (scale_method)
  59. {
  60. case GRUB_VIDEO_BITMAP_SCALE_METHOD_FASTEST:
  61. case GRUB_VIDEO_BITMAP_SCALE_METHOD_NEAREST:
  62. return scale_nn (dst, src);
  63. case GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST:
  64. case GRUB_VIDEO_BITMAP_SCALE_METHOD_BILINEAR:
  65. return scale_bilinear (dst, src);
  66. default:
  67. return grub_error (GRUB_ERR_BUG, "Invalid scale_method value");
  68. }
  69. }
  70. /* This function creates a new scaled version of the bitmap SRC. The new
  71. bitmap has dimensions DST_WIDTH by DST_HEIGHT. The scaling algorithm
  72. is given by SCALE_METHOD. If an error is encountered, the return code is
  73. not equal to GRUB_ERR_NONE, and the bitmap DST is either not created, or
  74. it is destroyed before this function returns.
  75. Supports only direct color modes which have components separated
  76. into bytes (e.g., RGBA 8:8:8:8 or BGR 8:8:8 true color).
  77. But because of this simplifying assumption, the implementation is
  78. greatly simplified. */
  79. grub_err_t
  80. grub_video_bitmap_create_scaled (struct grub_video_bitmap **dst,
  81. int dst_width, int dst_height,
  82. struct grub_video_bitmap *src,
  83. enum grub_video_bitmap_scale_method
  84. scale_method)
  85. {
  86. *dst = 0;
  87. grub_err_t err = verify_source_bitmap(src);
  88. if (err != GRUB_ERR_NONE)
  89. return err;
  90. if (dst_width <= 0 || dst_height <= 0)
  91. return grub_error (GRUB_ERR_BUG,
  92. "requested to scale to a size w/ a zero dimension");
  93. /* Create the new bitmap. */
  94. grub_err_t ret;
  95. ret = grub_video_bitmap_create (dst, dst_width, dst_height,
  96. src->mode_info.blit_format);
  97. if (ret != GRUB_ERR_NONE)
  98. return ret; /* Error. */
  99. ret = grub_video_bitmap_scale (*dst, src, scale_method);
  100. if (ret == GRUB_ERR_NONE)
  101. {
  102. /* Success: *dst is now a pointer to the scaled bitmap. */
  103. return GRUB_ERR_NONE;
  104. }
  105. else
  106. {
  107. /* Destroy the bitmap and return the error code. */
  108. grub_video_bitmap_destroy (*dst);
  109. *dst = 0;
  110. return ret;
  111. }
  112. }
  113. static grub_err_t
  114. make_h_align (unsigned *x, unsigned *w, unsigned new_w,
  115. grub_video_bitmap_h_align_t h_align)
  116. {
  117. grub_err_t ret = GRUB_ERR_NONE;
  118. if (new_w >= *w)
  119. {
  120. *x = 0;
  121. *w = new_w;
  122. return GRUB_ERR_NONE;
  123. }
  124. switch (h_align)
  125. {
  126. case GRUB_VIDEO_BITMAP_H_ALIGN_LEFT:
  127. *x = 0;
  128. break;
  129. case GRUB_VIDEO_BITMAP_H_ALIGN_CENTER:
  130. *x = (*w - new_w) / 2;
  131. break;
  132. case GRUB_VIDEO_BITMAP_H_ALIGN_RIGHT:
  133. *x = *w - new_w;
  134. break;
  135. default:
  136. ret = grub_error (GRUB_ERR_BUG, "Invalid h_align value");
  137. break;
  138. }
  139. *w = new_w;
  140. return ret;
  141. }
  142. static grub_err_t
  143. make_v_align (unsigned *y, unsigned *h, unsigned new_h,
  144. grub_video_bitmap_v_align_t v_align)
  145. {
  146. grub_err_t ret = GRUB_ERR_NONE;
  147. if (new_h >= *h)
  148. {
  149. *y = 0;
  150. *h = new_h;
  151. return GRUB_ERR_NONE;
  152. }
  153. switch (v_align)
  154. {
  155. case GRUB_VIDEO_BITMAP_V_ALIGN_TOP:
  156. *y = 0;
  157. break;
  158. case GRUB_VIDEO_BITMAP_V_ALIGN_CENTER:
  159. *y = (*h - new_h) / 2;
  160. break;
  161. case GRUB_VIDEO_BITMAP_V_ALIGN_BOTTOM:
  162. *y = *h - new_h;
  163. break;
  164. default:
  165. ret = grub_error (GRUB_ERR_BUG, "Invalid v_align value");
  166. break;
  167. }
  168. *h = new_h;
  169. return ret;
  170. }
  171. grub_err_t
  172. grub_video_bitmap_scale_proportional (struct grub_video_bitmap **dst,
  173. int dst_width, int dst_height,
  174. struct grub_video_bitmap *src,
  175. enum grub_video_bitmap_scale_method
  176. scale_method,
  177. grub_video_bitmap_selection_method_t
  178. selection_method,
  179. grub_video_bitmap_v_align_t v_align,
  180. grub_video_bitmap_h_align_t h_align)
  181. {
  182. *dst = 0;
  183. grub_err_t ret = verify_source_bitmap(src);
  184. if (ret != GRUB_ERR_NONE)
  185. return ret;
  186. if (dst_width <= 0 || dst_height <= 0)
  187. return grub_error (GRUB_ERR_BUG,
  188. "requested to scale to a size w/ a zero dimension");
  189. ret = grub_video_bitmap_create (dst, dst_width, dst_height,
  190. src->mode_info.blit_format);
  191. if (ret != GRUB_ERR_NONE)
  192. return ret; /* Error. */
  193. unsigned dx0 = 0;
  194. unsigned dy0 = 0;
  195. unsigned dw = dst_width;
  196. unsigned dh = dst_height;
  197. unsigned sx0 = 0;
  198. unsigned sy0 = 0;
  199. unsigned sw = src->mode_info.width;
  200. unsigned sh = src->mode_info.height;
  201. switch (selection_method)
  202. {
  203. case GRUB_VIDEO_BITMAP_SELECTION_METHOD_CROP:
  204. /* Comparing sw/sh VS dw/dh. */
  205. if (sw * dh < dw * sh)
  206. ret = make_v_align (&sy0, &sh, sw * dh / dw, v_align);
  207. else
  208. ret = make_h_align (&sx0, &sw, sh * dw / dh, h_align);
  209. break;
  210. case GRUB_VIDEO_BITMAP_SELECTION_METHOD_PADDING:
  211. if (sw * dh < dw * sh)
  212. ret = make_h_align (&dx0, &dw, sw * dh / sh, h_align);
  213. else
  214. ret = make_v_align (&dy0, &dh, sh * dw / sw, v_align);
  215. break;
  216. case GRUB_VIDEO_BITMAP_SELECTION_METHOD_FITWIDTH:
  217. if (sw * dh < dw * sh)
  218. ret = make_v_align (&sy0, &sh, sw * dh / dw, v_align);
  219. else
  220. ret = make_v_align (&dy0, &dh, sh * dw / sw, v_align);
  221. break;
  222. case GRUB_VIDEO_BITMAP_SELECTION_METHOD_FITHEIGHT:
  223. if (sw * dh < dw * sh)
  224. ret = make_h_align (&dx0, &dw, sw * dh / sh, h_align);
  225. else
  226. ret = make_h_align (&sx0, &sw, sh * dw / dh, h_align);
  227. break;
  228. default:
  229. ret = grub_error (GRUB_ERR_BUG, "Invalid selection_method value");
  230. break;
  231. }
  232. if (ret == GRUB_ERR_NONE)
  233. {
  234. /* Backup original data. */
  235. int src_width_orig = src->mode_info.width;
  236. int src_height_orig = src->mode_info.height;
  237. grub_uint8_t *src_data_orig = src->data;
  238. int dst_width_orig = (*dst)->mode_info.width;
  239. int dst_height_orig = (*dst)->mode_info.height;
  240. grub_uint8_t *dst_data_orig = (*dst)->data;
  241. int dstride = (*dst)->mode_info.pitch;
  242. int sstride = src->mode_info.pitch;
  243. /* bytes_per_pixel is the same for both src and dst. */
  244. int bytes_per_pixel = src->mode_info.bytes_per_pixel;
  245. /* Crop src and dst. */
  246. src->mode_info.width = sw;
  247. src->mode_info.height = sh;
  248. src->data = (grub_uint8_t *) src->data + sx0 * bytes_per_pixel
  249. + sy0 * sstride;
  250. (*dst)->mode_info.width = dw;
  251. (*dst)->mode_info.height = dh;
  252. (*dst)->data = (grub_uint8_t *) (*dst)->data + dx0 * bytes_per_pixel
  253. + dy0 * dstride;
  254. /* Scale our image. */
  255. ret = grub_video_bitmap_scale (*dst, src, scale_method);
  256. /* Restore original data. */
  257. src->mode_info.width = src_width_orig;
  258. src->mode_info.height = src_height_orig;
  259. src->data = src_data_orig;
  260. (*dst)->mode_info.width = dst_width_orig;
  261. (*dst)->mode_info.height = dst_height_orig;
  262. (*dst)->data = dst_data_orig;
  263. }
  264. if (ret == GRUB_ERR_NONE)
  265. {
  266. /* Success: *dst is now a pointer to the scaled bitmap. */
  267. return GRUB_ERR_NONE;
  268. }
  269. else
  270. {
  271. /* Destroy the bitmap and return the error code. */
  272. grub_video_bitmap_destroy (*dst);
  273. *dst = 0;
  274. return ret;
  275. }
  276. }
  277. static grub_err_t
  278. verify_bitmaps (struct grub_video_bitmap *dst, struct grub_video_bitmap *src)
  279. {
  280. /* Verify the simplifying assumptions. */
  281. if (dst == 0 || src == 0)
  282. return grub_error (GRUB_ERR_BUG, "null bitmap in scale function");
  283. if (dst->mode_info.red_field_pos % 8 != 0
  284. || dst->mode_info.green_field_pos % 8 != 0
  285. || dst->mode_info.blue_field_pos % 8 != 0
  286. || dst->mode_info.reserved_field_pos % 8 != 0)
  287. return grub_error (GRUB_ERR_BUG,
  288. "dst format not supported");
  289. if (src->mode_info.red_field_pos % 8 != 0
  290. || src->mode_info.green_field_pos % 8 != 0
  291. || src->mode_info.blue_field_pos % 8 != 0
  292. || src->mode_info.reserved_field_pos % 8 != 0)
  293. return grub_error (GRUB_ERR_BUG,
  294. "src format not supported");
  295. if (dst->mode_info.red_field_pos != src->mode_info.red_field_pos
  296. || dst->mode_info.red_mask_size != src->mode_info.red_mask_size
  297. || dst->mode_info.green_field_pos != src->mode_info.green_field_pos
  298. || dst->mode_info.green_mask_size != src->mode_info.green_mask_size
  299. || dst->mode_info.blue_field_pos != src->mode_info.blue_field_pos
  300. || dst->mode_info.blue_mask_size != src->mode_info.blue_mask_size
  301. || dst->mode_info.reserved_field_pos !=
  302. src->mode_info.reserved_field_pos
  303. || dst->mode_info.reserved_mask_size !=
  304. src->mode_info.reserved_mask_size)
  305. return grub_error (GRUB_ERR_BUG,
  306. "dst and src not compatible");
  307. if (dst->mode_info.bytes_per_pixel != src->mode_info.bytes_per_pixel)
  308. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  309. "dst and src not compatible");
  310. if (dst->mode_info.width == 0 || dst->mode_info.height == 0
  311. || src->mode_info.width == 0 || src->mode_info.height == 0)
  312. return grub_error (GRUB_ERR_BUG, "bitmap has a zero dimension");
  313. return GRUB_ERR_NONE;
  314. }
  315. /* Nearest neighbor bitmap scaling algorithm.
  316. Copy the bitmap SRC to the bitmap DST, scaling the bitmap to fit the
  317. dimensions of DST. This function uses the nearest neighbor algorithm to
  318. interpolate the pixels.
  319. Supports only direct color modes which have components separated
  320. into bytes (e.g., RGBA 8:8:8:8 or BGR 8:8:8 true color).
  321. But because of this simplifying assumption, the implementation is
  322. greatly simplified. */
  323. static grub_err_t
  324. scale_nn (struct grub_video_bitmap *dst, struct grub_video_bitmap *src)
  325. {
  326. grub_err_t err = verify_bitmaps(dst, src);
  327. if (err != GRUB_ERR_NONE)
  328. return err;
  329. grub_uint8_t *ddata = dst->data;
  330. grub_uint8_t *sdata = src->data;
  331. unsigned dw = dst->mode_info.width;
  332. unsigned dh = dst->mode_info.height;
  333. unsigned sw = src->mode_info.width;
  334. unsigned sh = src->mode_info.height;
  335. int dstride = dst->mode_info.pitch;
  336. int sstride = src->mode_info.pitch;
  337. /* bytes_per_pixel is the same for both src and dst. */
  338. int bytes_per_pixel = dst->mode_info.bytes_per_pixel;
  339. unsigned dy, sy, ystep, yfrac, yover;
  340. unsigned sx, xstep, xfrac, xover;
  341. grub_uint8_t *dptr, *dline_end, *sline;
  342. xstep = sw / dw;
  343. xover = sw % dw;
  344. ystep = sh / dh;
  345. yover = sh % dh;
  346. for (dy = 0, sy = 0, yfrac = 0; dy < dh; dy++, sy += ystep, yfrac += yover)
  347. {
  348. if (yfrac >= dh)
  349. {
  350. yfrac -= dh;
  351. sy++;
  352. }
  353. dptr = ddata + dy * dstride;
  354. dline_end = dptr + dw * bytes_per_pixel;
  355. sline = sdata + sy * sstride;
  356. for (sx = 0, xfrac = 0; dptr < dline_end; sx += xstep, xfrac += xover, dptr += bytes_per_pixel)
  357. {
  358. grub_uint8_t *sptr;
  359. int comp;
  360. if (xfrac >= dw)
  361. {
  362. xfrac -= dw;
  363. sx++;
  364. }
  365. /* Get the address of the pixels in src and dst. */
  366. sptr = sline + sx * bytes_per_pixel;
  367. /* Copy the pixel color value. */
  368. for (comp = 0; comp < bytes_per_pixel; comp++)
  369. dptr[comp] = sptr[comp];
  370. }
  371. }
  372. return GRUB_ERR_NONE;
  373. }
  374. /* Bilinear interpolation image scaling algorithm.
  375. Copy the bitmap SRC to the bitmap DST, scaling the bitmap to fit the
  376. dimensions of DST. This function uses the bilinear interpolation algorithm
  377. to interpolate the pixels.
  378. Supports only direct color modes which have components separated
  379. into bytes (e.g., RGBA 8:8:8:8 or BGR 8:8:8 true color).
  380. But because of this simplifying assumption, the implementation is
  381. greatly simplified. */
  382. static grub_err_t
  383. scale_bilinear (struct grub_video_bitmap *dst, struct grub_video_bitmap *src)
  384. {
  385. grub_err_t err = verify_bitmaps(dst, src);
  386. if (err != GRUB_ERR_NONE)
  387. return err;
  388. grub_uint8_t *ddata = dst->data;
  389. grub_uint8_t *sdata = src->data;
  390. unsigned dw = dst->mode_info.width;
  391. unsigned dh = dst->mode_info.height;
  392. unsigned sw = src->mode_info.width;
  393. unsigned sh = src->mode_info.height;
  394. int dstride = dst->mode_info.pitch;
  395. int sstride = src->mode_info.pitch;
  396. /* bytes_per_pixel is the same for both src and dst. */
  397. int bytes_per_pixel = dst->mode_info.bytes_per_pixel;
  398. unsigned dy, syf, sy, ystep, yfrac, yover;
  399. unsigned sxf, sx, xstep, xfrac, xover;
  400. grub_uint8_t *dptr, *dline_end, *sline;
  401. xstep = (sw << 8) / dw;
  402. xover = (sw << 8) % dw;
  403. ystep = (sh << 8) / dh;
  404. yover = (sh << 8) % dh;
  405. for (dy = 0, syf = 0, yfrac = 0; dy < dh; dy++, syf += ystep, yfrac += yover)
  406. {
  407. if (yfrac >= dh)
  408. {
  409. yfrac -= dh;
  410. syf++;
  411. }
  412. sy = syf >> 8;
  413. dptr = ddata + dy * dstride;
  414. dline_end = dptr + dw * bytes_per_pixel;
  415. sline = sdata + sy * sstride;
  416. for (sxf = 0, xfrac = 0; dptr < dline_end; sxf += xstep, xfrac += xover, dptr += bytes_per_pixel)
  417. {
  418. grub_uint8_t *sptr;
  419. int comp;
  420. if (xfrac >= dw)
  421. {
  422. xfrac -= dw;
  423. sxf++;
  424. }
  425. /* Get the address of the pixels in src and dst. */
  426. sx = sxf >> 8;
  427. sptr = sline + sx * bytes_per_pixel;
  428. /* If we have enough space to do so, use bilinear interpolation.
  429. Otherwise, fall back to nearest neighbor for this pixel. */
  430. if (sx < sw - 1 && sy < sh - 1)
  431. {
  432. /* Do bilinear interpolation. */
  433. /* Fixed-point .8 numbers representing the fraction of the
  434. distance in the x (u) and y (v) direction within the
  435. box of 4 pixels in the source. */
  436. unsigned u = sxf & 0xff;
  437. unsigned v = syf & 0xff;
  438. for (comp = 0; comp < bytes_per_pixel; comp++)
  439. {
  440. /* Get the component's values for the
  441. four source corner pixels. */
  442. unsigned f00 = sptr[comp];
  443. unsigned f10 = sptr[comp + bytes_per_pixel];
  444. unsigned f01 = sptr[comp + sstride];
  445. unsigned f11 = sptr[comp + sstride + bytes_per_pixel];
  446. /* Count coeffecients. */
  447. unsigned c00 = (256 - u) * (256 - v);
  448. unsigned c10 = u * (256 - v);
  449. unsigned c01 = (256 - u) * v;
  450. unsigned c11 = u * v;
  451. /* Interpolate. */
  452. unsigned fxy = c00 * f00 + c01 * f01 + c10 * f10 + c11 * f11;
  453. fxy = fxy >> 16;
  454. dptr[comp] = fxy;
  455. }
  456. }
  457. else
  458. {
  459. /* Fall back to nearest neighbor interpolation. */
  460. /* Copy the pixel color value. */
  461. for (comp = 0; comp < bytes_per_pixel; comp++)
  462. dptr[comp] = sptr[comp];
  463. }
  464. }
  465. }
  466. return GRUB_ERR_NONE;
  467. }