2
0

sdl_zoom.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "sdl_zoom.h"
  14. #include "qemu/osdep.h"
  15. #include <glib.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. static void sdl_zoom_rgb16(SDL_Surface *src, SDL_Surface *dst, int smooth,
  19. SDL_Rect *dst_rect);
  20. static void sdl_zoom_rgb32(SDL_Surface *src, SDL_Surface *dst, int smooth,
  21. SDL_Rect *dst_rect);
  22. #define BPP 32
  23. #include "sdl_zoom_template.h"
  24. #undef BPP
  25. #define BPP 16
  26. #include "sdl_zoom_template.h"
  27. #undef BPP
  28. int sdl_zoom_blit(SDL_Surface *src_sfc, SDL_Surface *dst_sfc, int smooth,
  29. SDL_Rect *in_rect)
  30. {
  31. SDL_Rect zoom, src_rect;
  32. int extra;
  33. /* Grow the size of the modified rectangle to avoid edge artefacts */
  34. src_rect.x = (in_rect->x > 0) ? (in_rect->x - 1) : 0;
  35. src_rect.y = (in_rect->y > 0) ? (in_rect->y - 1) : 0;
  36. src_rect.w = in_rect->w + 1;
  37. if (src_rect.x + src_rect.w > src_sfc->w)
  38. src_rect.w = src_sfc->w - src_rect.x;
  39. src_rect.h = in_rect->h + 1;
  40. if (src_rect.y + src_rect.h > src_sfc->h)
  41. src_rect.h = src_sfc->h - src_rect.y;
  42. /* (x,y) : round down */
  43. zoom.x = (int)(((float)(src_rect.x * dst_sfc->w)) / (float)(src_sfc->w));
  44. zoom.y = (int)(((float)(src_rect.y * dst_sfc->h)) / (float)(src_sfc->h));
  45. /* (w,h) : round up */
  46. zoom.w = (int)( ((double)((src_rect.w * dst_sfc->w) + (src_sfc->w - 1))) /
  47. (double)(src_sfc->w));
  48. zoom.h = (int)( ((double)((src_rect.h * dst_sfc->h) + (src_sfc->h - 1))) /
  49. (double)(src_sfc->h));
  50. /* Account for any (x,y) rounding by adding one-source-pixel's worth
  51. * of destination pixels and then edge checking.
  52. */
  53. extra = ((dst_sfc->w-1) / src_sfc->w) + 1;
  54. if ((zoom.x + zoom.w) < (dst_sfc->w - extra))
  55. zoom.w += extra;
  56. else
  57. zoom.w = dst_sfc->w - zoom.x;
  58. extra = ((dst_sfc->h-1) / src_sfc->h) + 1;
  59. if ((zoom.y + zoom.h) < (dst_sfc->h - extra))
  60. zoom.h += extra;
  61. else
  62. zoom.h = dst_sfc->h - zoom.y;
  63. /* The rectangle (zoom.x, zoom.y, zoom.w, zoom.h) is the area on the
  64. * destination surface that needs to be updated.
  65. */
  66. if (src_sfc->format->BitsPerPixel == 32)
  67. sdl_zoom_rgb32(src_sfc, dst_sfc, smooth, &zoom);
  68. else if (src_sfc->format->BitsPerPixel == 16)
  69. sdl_zoom_rgb16(src_sfc, dst_sfc, smooth, &zoom);
  70. else {
  71. fprintf(stderr, "pixel format not supported\n");
  72. return -1;
  73. }
  74. /* Return the rectangle of the update to the caller */
  75. *in_rect = zoom;
  76. return 0;
  77. }