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