bitmap_scale.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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_fb.h>
  22. #include <grub/bitmap.h>
  23. #include <grub/bitmap_scale.h>
  24. #include <grub/fbutil.h>
  25. #include <grub/fbfill.h>
  26. #include <grub/fbblit.h>
  27. #include <grub/types.h>
  28. GRUB_EXPORT(grub_video_bitmap_create_scaled);
  29. /* Prototypes for module-local functions. */
  30. static grub_err_t scale_nn (struct grub_video_bitmap *dst,
  31. struct grub_video_bitmap *src);
  32. static grub_err_t scale_bilinear (struct grub_video_bitmap *dst,
  33. struct grub_video_bitmap *src);
  34. /* This function creates a new scaled version of the bitmap SRC. The new
  35. bitmap has dimensions DST_WIDTH by DST_HEIGHT. The scaling algorithm
  36. is given by SCALE_METHOD. If an error is encountered, the return code is
  37. not equal to GRUB_ERR_NONE, and the bitmap DST is either not created, or
  38. 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. */
  43. static grub_err_t
  44. grub_video_bitmap_create_scaled_internal (struct grub_video_bitmap **dst,
  45. int dst_width, int dst_height,
  46. struct grub_video_bitmap *src,
  47. int scale_method)
  48. {
  49. /* Create the new bitmap. */
  50. grub_err_t ret;
  51. ret = grub_video_bitmap_create (dst, dst_width, dst_height,
  52. src->mode_info.blit_format);
  53. if (ret != GRUB_ERR_NONE)
  54. return ret; /* Error. */
  55. (*dst)->transparent = src->transparent;
  56. switch (scale_method)
  57. {
  58. case GRUB_VIDEO_BITMAP_SCALE_METHOD_FASTEST:
  59. case GRUB_VIDEO_BITMAP_SCALE_METHOD_NEAREST:
  60. ret = scale_nn (*dst, src);
  61. break;
  62. case GRUB_VIDEO_BITMAP_SCALE_METHOD_BEST:
  63. case GRUB_VIDEO_BITMAP_SCALE_METHOD_BILINEAR:
  64. ret = scale_bilinear (*dst, src);
  65. break;
  66. default:
  67. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid scale method value");
  68. break;
  69. }
  70. if (ret == GRUB_ERR_NONE)
  71. {
  72. /* Success: *dst is now a pointer to the scaled bitmap. */
  73. return GRUB_ERR_NONE;
  74. }
  75. else
  76. {
  77. /* Destroy the bitmap and return the error code. */
  78. grub_video_bitmap_destroy (*dst);
  79. *dst = 0;
  80. return ret;
  81. }
  82. }
  83. static grub_video_color_t
  84. map_color (struct grub_video_mode_info *mode_info, grub_video_color_t color)
  85. {
  86. grub_uint8_t red, green, blue, alpha;
  87. red = green = blue = alpha = 0;
  88. grub_video_unmap_color (color, &red, &green, &blue, &alpha);
  89. return grub_video_fbblit_map_rgba (mode_info, red, green, blue, alpha);
  90. }
  91. grub_err_t
  92. grub_video_bitmap_create_scaled (struct grub_video_bitmap **dst,
  93. int dst_width, int dst_height,
  94. struct grub_video_bitmap *src,
  95. int scale, grub_video_color_t color)
  96. {
  97. int type, method;
  98. int src_width, src_height, width, height;
  99. struct grub_video_fbblit_info dst_info;
  100. struct grub_video_bitmap *tmp = 0;
  101. grub_err_t ret;
  102. *dst = 0;
  103. /* Verify the simplifying assumptions. */
  104. if (src == 0)
  105. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  106. "null src bitmap in grub_video_bitmap_create_scaled");
  107. if (src->mode_info.red_field_pos % 8 != 0
  108. || src->mode_info.green_field_pos % 8 != 0
  109. || src->mode_info.blue_field_pos % 8 != 0
  110. || src->mode_info.reserved_field_pos % 8 != 0)
  111. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  112. "src format not supported for scale");
  113. if (src->mode_info.width == 0 || src->mode_info.height == 0)
  114. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  115. "source bitmap has a zero dimension");
  116. if (dst_width <= 0 || dst_height <= 0)
  117. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  118. "requested to scale to a size w/ a zero dimension");
  119. if (src->mode_info.bytes_per_pixel * 8 != src->mode_info.bpp)
  120. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  121. "bitmap to scale has inconsistent Bpp and bpp");
  122. type = scale & GRUB_VIDEO_BITMAP_SCALE_TYPE_MASK;
  123. method = scale & GRUB_VIDEO_BITMAP_SCALE_METHOD_MASK;
  124. src_width = src->mode_info.width;
  125. src_height = src->mode_info.height;
  126. if ((src_width == dst_width) && (src_height == dst_height))
  127. type = GRUB_VIDEO_BITMAP_SCALE_TYPE_CENTER;
  128. if ((type == GRUB_VIDEO_BITMAP_SCALE_TYPE_NORMAL) ||
  129. (((type == GRUB_VIDEO_BITMAP_SCALE_TYPE_MINFIT) ||
  130. (type == GRUB_VIDEO_BITMAP_SCALE_TYPE_MAXFIT)) &&
  131. (src_width * dst_height == src_height * dst_width)))
  132. return grub_video_bitmap_create_scaled_internal (dst, dst_width,
  133. dst_height, src, method);
  134. ret = grub_video_bitmap_create (dst, dst_width, dst_height,
  135. src->mode_info.blit_format);
  136. if (ret != GRUB_ERR_NONE)
  137. return ret; /* Error. */
  138. (*dst)->transparent = src->transparent;
  139. dst_info.mode_info = &(*dst)->mode_info;
  140. dst_info.data = (*dst)->data;
  141. color = map_color (dst_info.mode_info, color);
  142. switch (type)
  143. {
  144. case GRUB_VIDEO_BITMAP_SCALE_TYPE_MINFIT:
  145. case GRUB_VIDEO_BITMAP_SCALE_TYPE_MAXFIT:
  146. {
  147. if ((src_width * dst_height > src_height * dst_width) ==
  148. (type == GRUB_VIDEO_BITMAP_SCALE_TYPE_MAXFIT))
  149. {
  150. width = src_width * dst_height / src_height;
  151. height = dst_height;
  152. }
  153. else
  154. {
  155. width = dst_width;
  156. height = src_height * dst_width / src_width;
  157. }
  158. ret = grub_video_bitmap_create_scaled_internal (&tmp, width, height,
  159. src, method);
  160. if (ret)
  161. return ret;
  162. src = tmp;
  163. src_width = width;
  164. src_height = height;
  165. }
  166. case GRUB_VIDEO_BITMAP_SCALE_TYPE_CENTER:
  167. {
  168. int src_x, src_y, dst_x, dst_y;
  169. struct grub_video_fbblit_info src_info;
  170. width = (src_width > dst_width) ? dst_width : src_width;
  171. height = (src_height > dst_height) ? dst_height : src_height;
  172. src_x = (src_width - width) >> 1;
  173. src_y = (src_height - height) >> 1;
  174. dst_x = (dst_width - width) >> 1;
  175. dst_y = (dst_height - height) >> 1;
  176. if (dst_y)
  177. {
  178. grub_video_fbfill (&dst_info, color, 0, 0, dst_width, dst_y);
  179. grub_video_fbfill (&dst_info, color, 0, dst_y + height,
  180. dst_width, dst_height - dst_y - height);
  181. }
  182. if (dst_x)
  183. {
  184. grub_video_fbfill (&dst_info, color, 0, dst_y, dst_x, height);
  185. grub_video_fbfill (&dst_info, color, dst_x + width, dst_y,
  186. dst_width - dst_x - width, height);
  187. }
  188. src_info.mode_info = &src->mode_info;
  189. src_info.data = src->data;
  190. grub_video_fbblit (&dst_info, &src_info, GRUB_VIDEO_BLIT_REPLACE,
  191. dst_x, dst_y, width, height, src_x, src_y);
  192. break;
  193. }
  194. case GRUB_VIDEO_BITMAP_SCALE_TYPE_TILING:
  195. {
  196. int x, y;
  197. struct grub_video_fbblit_info src_info;
  198. src_info.mode_info = &src->mode_info;
  199. src_info.data = src->data;
  200. for (y = 0; y < dst_height; y += src_height)
  201. {
  202. for (x = 0; x < dst_width; x += src_width)
  203. {
  204. int w, h;
  205. w = src_width;
  206. if (x + w > dst_width)
  207. w = dst_width - x;
  208. h = src_height;
  209. if (y + h > dst_height)
  210. h = dst_height - y;
  211. grub_video_fbblit (&dst_info, &src_info,
  212. GRUB_VIDEO_BLIT_REPLACE,
  213. x, y, w, h, 0, 0);
  214. }
  215. }
  216. break;
  217. }
  218. default:
  219. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid scale type value");
  220. }
  221. if (tmp)
  222. grub_video_bitmap_destroy (tmp);
  223. return 0;
  224. }
  225. /* Nearest neighbor bitmap scaling algorithm.
  226. Copy the bitmap SRC to the bitmap DST, scaling the bitmap to fit the
  227. dimensions of DST. This function uses the nearest neighbor algorithm to
  228. interpolate the pixels.
  229. Supports only direct color modes which have components separated
  230. into bytes (e.g., RGBA 8:8:8:8 or BGR 8:8:8 true color).
  231. But because of this simplifying assumption, the implementation is
  232. greatly simplified. */
  233. static grub_err_t
  234. scale_nn (struct grub_video_bitmap *dst, struct grub_video_bitmap *src)
  235. {
  236. /* Verify the simplifying assumptions. */
  237. if (dst == 0 || src == 0)
  238. return grub_error (GRUB_ERR_BAD_ARGUMENT, "null bitmap in scale_nn");
  239. if (dst->mode_info.red_field_pos % 8 != 0
  240. || dst->mode_info.green_field_pos % 8 != 0
  241. || dst->mode_info.blue_field_pos % 8 != 0
  242. || dst->mode_info.reserved_field_pos % 8 != 0)
  243. return grub_error (GRUB_ERR_BAD_ARGUMENT, "dst format not supported");
  244. if (src->mode_info.red_field_pos % 8 != 0
  245. || src->mode_info.green_field_pos % 8 != 0
  246. || src->mode_info.blue_field_pos % 8 != 0
  247. || src->mode_info.reserved_field_pos % 8 != 0)
  248. return grub_error (GRUB_ERR_BAD_ARGUMENT, "src format not supported");
  249. if (dst->mode_info.red_field_pos != src->mode_info.red_field_pos
  250. || dst->mode_info.red_mask_size != src->mode_info.red_mask_size
  251. || dst->mode_info.green_field_pos != src->mode_info.green_field_pos
  252. || dst->mode_info.green_mask_size != src->mode_info.green_mask_size
  253. || dst->mode_info.blue_field_pos != src->mode_info.blue_field_pos
  254. || dst->mode_info.blue_mask_size != src->mode_info.blue_mask_size
  255. || dst->mode_info.reserved_field_pos !=
  256. src->mode_info.reserved_field_pos
  257. || dst->mode_info.reserved_mask_size !=
  258. src->mode_info.reserved_mask_size)
  259. return grub_error (GRUB_ERR_BAD_ARGUMENT, "dst and src not compatible");
  260. if (dst->mode_info.bytes_per_pixel != src->mode_info.bytes_per_pixel)
  261. return grub_error (GRUB_ERR_BAD_ARGUMENT, "dst and src not compatible");
  262. if (dst->mode_info.width == 0 || dst->mode_info.height == 0
  263. || src->mode_info.width == 0 || src->mode_info.height == 0)
  264. return grub_error (GRUB_ERR_BAD_ARGUMENT, "bitmap has a zero dimension");
  265. grub_uint8_t *ddata = dst->data;
  266. grub_uint8_t *sdata = src->data;
  267. int dw = dst->mode_info.width;
  268. int dh = dst->mode_info.height;
  269. int sw = src->mode_info.width;
  270. int sh = src->mode_info.height;
  271. int dstride = dst->mode_info.pitch;
  272. int sstride = src->mode_info.pitch;
  273. /* bytes_per_pixel is the same for both src and dst. */
  274. int bytes_per_pixel = dst->mode_info.bytes_per_pixel;
  275. int dy;
  276. for (dy = 0; dy < dh; dy++)
  277. {
  278. int dx;
  279. for (dx = 0; dx < dw; dx++)
  280. {
  281. grub_uint8_t *dptr;
  282. grub_uint8_t *sptr;
  283. int sx;
  284. int sy;
  285. int comp;
  286. /* Compute the source coordinate that the destination coordinate
  287. maps to. Note: sx/sw = dx/dw => sx = sw*dx/dw. */
  288. sx = sw * dx / dw;
  289. sy = sh * dy / dh;
  290. /* Get the address of the pixels in src and dst. */
  291. dptr = ddata + dy * dstride + dx * bytes_per_pixel;
  292. sptr = sdata + sy * sstride + sx * bytes_per_pixel;
  293. /* Copy the pixel color value. */
  294. for (comp = 0; comp < bytes_per_pixel; comp++)
  295. dptr[comp] = sptr[comp];
  296. }
  297. }
  298. return GRUB_ERR_NONE;
  299. }
  300. /* Bilinear interpolation image scaling algorithm.
  301. Copy the bitmap SRC to the bitmap DST, scaling the bitmap to fit the
  302. dimensions of DST. This function uses the bilinear interpolation algorithm
  303. to interpolate the pixels.
  304. Supports only direct color modes which have components separated
  305. into bytes (e.g., RGBA 8:8:8:8 or BGR 8:8:8 true color).
  306. But because of this simplifying assumption, the implementation is
  307. greatly simplified. */
  308. static grub_err_t
  309. scale_bilinear (struct grub_video_bitmap *dst, struct grub_video_bitmap *src)
  310. {
  311. /* Verify the simplifying assumptions. */
  312. if (dst == 0 || src == 0)
  313. return grub_error (GRUB_ERR_BAD_ARGUMENT, "null bitmap in scale func");
  314. if (dst->mode_info.red_field_pos % 8 != 0
  315. || dst->mode_info.green_field_pos % 8 != 0
  316. || dst->mode_info.blue_field_pos % 8 != 0
  317. || dst->mode_info.reserved_field_pos % 8 != 0)
  318. return grub_error (GRUB_ERR_BAD_ARGUMENT, "dst format not supported");
  319. if (src->mode_info.red_field_pos % 8 != 0
  320. || src->mode_info.green_field_pos % 8 != 0
  321. || src->mode_info.blue_field_pos % 8 != 0
  322. || src->mode_info.reserved_field_pos % 8 != 0)
  323. return grub_error (GRUB_ERR_BAD_ARGUMENT, "src format not supported");
  324. if (dst->mode_info.red_field_pos != src->mode_info.red_field_pos
  325. || dst->mode_info.red_mask_size != src->mode_info.red_mask_size
  326. || dst->mode_info.green_field_pos != src->mode_info.green_field_pos
  327. || dst->mode_info.green_mask_size != src->mode_info.green_mask_size
  328. || dst->mode_info.blue_field_pos != src->mode_info.blue_field_pos
  329. || dst->mode_info.blue_mask_size != src->mode_info.blue_mask_size
  330. || dst->mode_info.reserved_field_pos !=
  331. src->mode_info.reserved_field_pos
  332. || dst->mode_info.reserved_mask_size !=
  333. src->mode_info.reserved_mask_size)
  334. return grub_error (GRUB_ERR_BAD_ARGUMENT, "dst and src not compatible");
  335. if (dst->mode_info.bytes_per_pixel != src->mode_info.bytes_per_pixel)
  336. return grub_error (GRUB_ERR_BAD_ARGUMENT, "dst and src not compatible");
  337. if (dst->mode_info.width == 0 || dst->mode_info.height == 0
  338. || src->mode_info.width == 0 || src->mode_info.height == 0)
  339. return grub_error (GRUB_ERR_BAD_ARGUMENT, "bitmap has a zero dimension");
  340. grub_uint8_t *ddata = dst->data;
  341. grub_uint8_t *sdata = src->data;
  342. int dw = dst->mode_info.width;
  343. int dh = dst->mode_info.height;
  344. int sw = src->mode_info.width;
  345. int sh = src->mode_info.height;
  346. int dstride = dst->mode_info.pitch;
  347. int sstride = src->mode_info.pitch;
  348. /* bytes_per_pixel is the same for both src and dst. */
  349. int bytes_per_pixel = dst->mode_info.bytes_per_pixel;
  350. int dy;
  351. for (dy = 0; dy < dh; dy++)
  352. {
  353. int dx;
  354. for (dx = 0; dx < dw; dx++)
  355. {
  356. grub_uint8_t *dptr;
  357. grub_uint8_t *sptr;
  358. int sx;
  359. int sy;
  360. int comp;
  361. /* Compute the source coordinate that the destination coordinate
  362. maps to. Note: sx/sw = dx/dw => sx = sw*dx/dw. */
  363. sx = sw * dx / dw;
  364. sy = sh * dy / dh;
  365. /* Get the address of the pixels in src and dst. */
  366. dptr = ddata + dy * dstride + dx * bytes_per_pixel;
  367. sptr = sdata + sy * sstride + sx * bytes_per_pixel;
  368. /* If we have enough space to do so, use bilinear interpolation.
  369. Otherwise, fall back to nearest neighbor for this pixel. */
  370. if (sx < sw - 1 && sy < sh - 1)
  371. {
  372. /* Do bilinear interpolation. */
  373. /* Fixed-point .8 numbers representing the fraction of the
  374. distance in the x (u) and y (v) direction within the
  375. box of 4 pixels in the source. */
  376. int u = (256 * sw * dx / dw) - (sx * 256);
  377. int v = (256 * sh * dy / dh) - (sy * 256);
  378. for (comp = 0; comp < bytes_per_pixel; comp++)
  379. {
  380. /* Get the component's values for the
  381. four source corner pixels. */
  382. grub_uint8_t f00 = sptr[comp];
  383. grub_uint8_t f10 = sptr[comp + bytes_per_pixel];
  384. grub_uint8_t f01 = sptr[comp + sstride];
  385. grub_uint8_t f11 = sptr[comp + sstride + bytes_per_pixel];
  386. /* Do linear interpolations along the top and bottom
  387. rows of the box. */
  388. grub_uint8_t f0y = (256 - v) * f00 / 256 + v * f01 / 256;
  389. grub_uint8_t f1y = (256 - v) * f10 / 256 + v * f11 / 256;
  390. /* Interpolate vertically. */
  391. grub_uint8_t fxy = (256 - u) * f0y / 256 + u * f1y / 256;
  392. dptr[comp] = fxy;
  393. }
  394. }
  395. else
  396. {
  397. /* Fall back to nearest neighbor interpolation. */
  398. /* Copy the pixel color value. */
  399. for (comp = 0; comp < bytes_per_pixel; comp++)
  400. dptr[comp] = sptr[comp];
  401. }
  402. }
  403. }
  404. return GRUB_ERR_NONE;
  405. }