VirtualBox

source: vbox/trunk/src/VBox/Main/DisplayResampleImage.cpp@ 26782

Last change on this file since 26782 was 26511, checked in by vboxsync, 15 years ago

Main: tabs -> spaces.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1/** @file
2 * Image resampling code, used for snapshot thumbnails.
3 */
4
5/*
6 * Copyright (C) 2009 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
17 * Clara, CA 95054 USA or visit http://www.sun.com if you need
18 * additional information or have any questions.
19 */
20
21/*
22 * Based on gdImageCopyResampled from libgd.
23 * Original copyright notice follows:
24
25 Portions copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
26 Pierre-Alain Joye ([email protected]).
27
28 Permission has been granted to copy, distribute and modify gd in
29 any context without fee, including a commercial application,
30 provided that this notice is present in user-accessible supporting
31 documentation.
32
33 This does not affect your ownership of the derived work itself, and
34 the intent is to assure proper credit for the authors of gd, not to
35 interfere with your productive use of gd. If you have questions,
36 ask. "Derived works" includes all programs that utilize the
37 library. Credit must be given in user-accessible documentation.
38
39 This software is provided "AS IS." The copyright holders disclaim
40 all warranties, either express or implied, including but not
41 limited to implied warranties of merchantability and fitness for a
42 particular purpose, with respect to this code and accompanying
43 documentation.
44 */
45
46/*
47 *
48 * @todo Simplify: Offsets of images are 0,0 => no dstX, dstY, srcX, srcY;
49 * Screenshot has no alpha channel => no processing of alpha byte.
50 */
51
52#include <iprt/types.h>
53
54/* 2.0.10: cast instead of floor() yields 35% performance improvement.
55 Thanks to John Buckman. */
56
57#define floor2(exp) ((long) exp)
58/*#define floor2(exp) floor(exp)*/
59
60typedef uint8_t *gdImagePtr;
61
62DECLINLINE(int) gdImageGetTrueColorPixel (gdImagePtr im, int x, int y, int w)
63{
64 return *(int32_t *)(im + y * w * 4 + x * 4);
65}
66
67DECLINLINE(void) gdImageSetPixel (gdImagePtr im, int x, int y, int color, int w)
68{
69 *(int32_t *)(im + y * w * 4 + x * 4) = color;
70}
71
72#define gdAlphaMax 127
73#define gdAlphaOpaque 0
74#define gdAlphaTransparent 127
75#define gdRedMax 255
76#define gdGreenMax 255
77#define gdBlueMax 255
78#define gdTrueColorGetAlpha(c) (((c) & 0x7F000000) >> 24)
79#define gdTrueColorGetRed(c) (((c) & 0xFF0000) >> 16)
80#define gdTrueColorGetGreen(c) (((c) & 0x00FF00) >> 8)
81#define gdTrueColorGetBlue(c) ((c) & 0x0000FF)
82#define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + \
83 ((r) << 16) + \
84 ((g) << 8) + \
85 (b))
86
87void gdImageCopyResampled (uint8_t *dst,
88 uint8_t *src,
89 int dstX, int dstY,
90 int srcX, int srcY,
91 int dstW, int dstH, int srcW, int srcH)
92{
93 int x, y;
94 double sy1, sy2, sx1, sx2;
95 for (y = dstY; (y < dstY + dstH); y++)
96 {
97 sy1 = ((double) y - (double) dstY) * (double) srcH / (double) dstH;
98 sy2 = ((double) (y + 1) - (double) dstY) * (double) srcH /
99 (double) dstH;
100 for (x = dstX; (x < dstX + dstW); x++)
101 {
102 double sx, sy;
103 double spixels = 0;
104 double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
105 sx1 = ((double) x - (double) dstX) * (double) srcW / dstW;
106 sx2 = ((double) (x + 1) - (double) dstX) * (double) srcW / dstW;
107 sy = sy1;
108 do
109 {
110 double yportion;
111 if (floor2 (sy) == floor2 (sy1))
112 {
113 yportion = 1.0 - (sy - floor2 (sy));
114 if (yportion > sy2 - sy1)
115 {
116 yportion = sy2 - sy1;
117 }
118 sy = floor2 (sy);
119 }
120 else if (sy == floor2 (sy2))
121 {
122 yportion = sy2 - floor2 (sy2);
123 }
124 else
125 {
126 yportion = 1.0;
127 }
128 sx = sx1;
129 do
130 {
131 double xportion;
132 double pcontribution;
133 int p;
134 if (floor2 (sx) == floor2 (sx1))
135 {
136 xportion = 1.0 - (sx - floor2 (sx));
137 if (xportion > sx2 - sx1)
138 {
139 xportion = sx2 - sx1;
140 }
141 sx = floor2 (sx);
142 }
143 else if (sx == floor2 (sx2))
144 {
145 xportion = sx2 - floor2 (sx2);
146 }
147 else
148 {
149 xportion = 1.0;
150 }
151 pcontribution = xportion * yportion;
152 /* 2.08: previously srcX and srcY were ignored.
153 Andrew Pattison */
154 p = gdImageGetTrueColorPixel (src,
155 (int) sx + srcX,
156 (int) sy + srcY, srcW);
157 red += gdTrueColorGetRed (p) * pcontribution;
158 green += gdTrueColorGetGreen (p) * pcontribution;
159 blue += gdTrueColorGetBlue (p) * pcontribution;
160 alpha += gdTrueColorGetAlpha (p) * pcontribution;
161 spixels += xportion * yportion;
162 sx += 1.0;
163 }
164 while (sx < sx2);
165 sy += 1.0;
166 }
167 while (sy < sy2);
168 if (spixels != 0.0)
169 {
170 red /= spixels;
171 green /= spixels;
172 blue /= spixels;
173 alpha /= spixels;
174 }
175 /* Clamping to allow for rounding errors above */
176 if (red > 255.0)
177 {
178 red = 255.0;
179 }
180 if (green > 255.0)
181 {
182 green = 255.0;
183 }
184 if (blue > 255.0)
185 {
186 blue = 255.0;
187 }
188 if (alpha > gdAlphaMax)
189 {
190 alpha = gdAlphaMax;
191 }
192 gdImageSetPixel (dst,
193 x, y,
194 gdTrueColorAlpha ((int) red,
195 (int) green,
196 (int) blue, (int) alpha), dstW);
197 }
198 }
199}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette