Merge commit 'd918f23efaf486293b96418fe5deaff8a5583304' into upstream-merge
[qemu-kvm/fedora.git] / sdl_zoom.c
blob56d3604fc3d79e4cc4622be8437c78bf70075da3
1 /*
2 * SDL_zoom - surface scaling
3 *
4 * Copyright (c) 2009 Citrix Systems, Inc.
6 * Derived from: SDL_rotozoom, LGPL (c) A. Schiffler from the SDL_gfx library.
7 * Modifications by Stefano Stabellini.
9 * This work is licensed under the terms of the GNU GPL version 2.
10 * See the COPYING file in the top-level directory.
14 #include "sdl_zoom.h"
15 #include "osdep.h"
16 #include <stdint.h>
18 static int sdl_zoom_rgb16(SDL_Surface *src, SDL_Surface *dst, int smooth,
19 SDL_Rect *dst_rect);
20 static int sdl_zoom_rgb32(SDL_Surface *src, SDL_Surface *dst, int smooth,
21 SDL_Rect *dst_rect);
23 #define BPP 32
24 #include "sdl_zoom_template.h"
25 #undef BPP
26 #define BPP 16
27 #include "sdl_zoom_template.h"
28 #undef BPP
30 int sdl_zoom_blit(SDL_Surface *src_sfc, SDL_Surface *dst_sfc, int smooth,
31 SDL_Rect *in_rect)
33 SDL_Rect zoom, src_rect;
34 int extra;
36 /* Grow the size of the modified rectangle to avoid edge artefacts */
37 src_rect.x = (in_rect->x > 0) ? (in_rect->x - 1) : 0;
38 src_rect.y = (in_rect->y > 0) ? (in_rect->y - 1) : 0;
40 src_rect.w = in_rect->w + 1;
41 if (src_rect.x + src_rect.w > src_sfc->w)
42 src_rect.w = src_sfc->w - src_rect.x;
44 src_rect.h = in_rect->h + 1;
45 if (src_rect.y + src_rect.h > src_sfc->h)
46 src_rect.h = src_sfc->h - src_rect.y;
48 /* (x,y) : round down */
49 zoom.x = (int)(((float)(src_rect.x * dst_sfc->w)) / (float)(src_sfc->w));
50 zoom.y = (int)(((float)(src_rect.y * dst_sfc->h)) / (float)(src_sfc->h));
52 /* (w,h) : round up */
53 zoom.w = (int)( ((double)((src_rect.w * dst_sfc->w) + (src_sfc->w - 1))) /
54 (double)(src_sfc->w));
56 zoom.h = (int)( ((double)((src_rect.h * dst_sfc->h) + (src_sfc->h - 1))) /
57 (double)(src_sfc->h));
59 /* Account for any (x,y) rounding by adding one-source-pixel's worth
60 * of destination pixels and then edge checking.
63 extra = ((dst_sfc->w-1) / src_sfc->w) + 1;
65 if ((zoom.x + zoom.w) < (dst_sfc->w - extra))
66 zoom.w += extra;
67 else
68 zoom.w = dst_sfc->w - zoom.x;
70 extra = ((dst_sfc->h-1) / src_sfc->h) + 1;
72 if ((zoom.y + zoom.h) < (dst_sfc->h - extra))
73 zoom.h += extra;
74 else
75 zoom.h = dst_sfc->h - zoom.y;
77 /* The rectangle (zoom.x, zoom.y, zoom.w, zoom.h) is the area on the
78 * destination surface that needs to be updated.
80 if (src_sfc->format->BitsPerPixel == 32)
81 sdl_zoom_rgb32(src_sfc, dst_sfc, smooth, &zoom);
82 else if (src_sfc->format->BitsPerPixel == 16)
83 sdl_zoom_rgb16(src_sfc, dst_sfc, smooth, &zoom);
84 else {
85 fprintf(stderr, "pixel format not supported\n");
86 return -1;
89 /* Return the rectangle of the update to the caller */
90 *in_rect = zoom;
92 return 0;