sdl_zoom.c 2.8 KB

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