sdl_zoom.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * SDL_zoom - surface scaling
  3. *
  4. * Copyright (c) 2009 Citrix Systems, Inc.
  5. *
  6. * Derived from: SDL_rotozoom, LGPL (c) A. Schiffler from the SDL_gfx library.
  7. * Modifications by Stefano Stabellini.
  8. *
  9. * This work is licensed under the terms of the GNU GPL version 2.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "sdl_zoom.h"
  15. static void sdl_zoom_rgb16(SDL_Surface *src, SDL_Surface *dst, int smooth,
  16. SDL_Rect *dst_rect);
  17. static void sdl_zoom_rgb32(SDL_Surface *src, SDL_Surface *dst, int smooth,
  18. SDL_Rect *dst_rect);
  19. #define BPP 32
  20. #include "sdl_zoom_template.h"
  21. #undef BPP
  22. #define BPP 16
  23. #include "sdl_zoom_template.h"
  24. #undef BPP
  25. int sdl_zoom_blit(SDL_Surface *src_sfc, SDL_Surface *dst_sfc, int smooth,
  26. SDL_Rect *in_rect)
  27. {
  28. SDL_Rect zoom, src_rect;
  29. int extra;
  30. /* Grow the size of the modified rectangle to avoid edge artefacts */
  31. src_rect.x = (in_rect->x > 0) ? (in_rect->x - 1) : 0;
  32. src_rect.y = (in_rect->y > 0) ? (in_rect->y - 1) : 0;
  33. src_rect.w = in_rect->w + 1;
  34. if (src_rect.x + src_rect.w > src_sfc->w)
  35. src_rect.w = src_sfc->w - src_rect.x;
  36. src_rect.h = in_rect->h + 1;
  37. if (src_rect.y + src_rect.h > src_sfc->h)
  38. src_rect.h = src_sfc->h - src_rect.y;
  39. /* (x,y) : round down */
  40. zoom.x = (int)(((float)(src_rect.x * dst_sfc->w)) / (float)(src_sfc->w));
  41. zoom.y = (int)(((float)(src_rect.y * dst_sfc->h)) / (float)(src_sfc->h));
  42. /* (w,h) : round up */
  43. zoom.w = (int)( ((double)((src_rect.w * dst_sfc->w) + (src_sfc->w - 1))) /
  44. (double)(src_sfc->w));
  45. zoom.h = (int)( ((double)((src_rect.h * dst_sfc->h) + (src_sfc->h - 1))) /
  46. (double)(src_sfc->h));
  47. /* Account for any (x,y) rounding by adding one-source-pixel's worth
  48. * of destination pixels and then edge checking.
  49. */
  50. extra = ((dst_sfc->w-1) / src_sfc->w) + 1;
  51. if ((zoom.x + zoom.w) < (dst_sfc->w - extra))
  52. zoom.w += extra;
  53. else
  54. zoom.w = dst_sfc->w - zoom.x;
  55. extra = ((dst_sfc->h-1) / src_sfc->h) + 1;
  56. if ((zoom.y + zoom.h) < (dst_sfc->h - extra))
  57. zoom.h += extra;
  58. else
  59. zoom.h = dst_sfc->h - zoom.y;
  60. /* The rectangle (zoom.x, zoom.y, zoom.w, zoom.h) is the area on the
  61. * destination surface that needs to be updated.
  62. */
  63. if (src_sfc->format->BitsPerPixel == 32)
  64. sdl_zoom_rgb32(src_sfc, dst_sfc, smooth, &zoom);
  65. else if (src_sfc->format->BitsPerPixel == 16)
  66. sdl_zoom_rgb16(src_sfc, dst_sfc, smooth, &zoom);
  67. else {
  68. fprintf(stderr, "pixel format not supported\n");
  69. return -1;
  70. }
  71. /* Return the rectangle of the update to the caller */
  72. *in_rect = zoom;
  73. return 0;
  74. }