VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/DisplayResampleImage.cpp@ 36067

Last change on this file since 36067 was 35579, checked in by vboxsync, 14 years ago

scm: cleanups and adjustments

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/** @file
2 * Image resampling code, used for snapshot thumbnails.
3 */
4
5/*
6 * Copyright (C) 2009 Oracle Corporation
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
17/*
18 * Based on gdImageCopyResampled from libgd.
19 * Original copyright notice follows:
20
21 Portions copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
22 Pierre-Alain Joye (pierre@libgd.org).
23
24 Permission has been granted to copy, distribute and modify gd in
25 any context without fee, including a commercial application,
26 provided that this notice is present in user-accessible supporting
27 documentation.
28
29 This does not affect your ownership of the derived work itself, and
30 the intent is to assure proper credit for the authors of gd, not to
31 interfere with your productive use of gd. If you have questions,
32 ask. "Derived works" includes all programs that utilize the
33 library. Credit must be given in user-accessible documentation.
34
35 This software is provided "AS IS." The copyright holders disclaim
36 all warranties, either express or implied, including but not
37 limited to implied warranties of merchantability and fitness for a
38 particular purpose, with respect to this code and accompanying
39 documentation.
40 */
41
42/*
43 *
44 * @todo Simplify: Offsets of images are 0,0 => no dstX, dstY, srcX, srcY;
45 * Screenshot has no alpha channel => no processing of alpha byte.
46 */
47
48#include <iprt/types.h>
49
50/* 2.0.10: cast instead of floor() yields 35% performance improvement.
51 Thanks to John Buckman. */
52
53#define floor2(exp) ((long) exp)
54/*#define floor2(exp) floor(exp)*/
55
56typedef uint8_t *gdImagePtr;
57
58DECLINLINE(int) gdImageGetTrueColorPixel (gdImagePtr im, int x, int y, int w)
59{
60 return *(int32_t *)(im + y * w * 4 + x * 4);
61}
62
63DECLINLINE(void) gdImageSetPixel (gdImagePtr im, int x, int y, int color, int w)
64{
65 *(int32_t *)(im + y * w * 4 + x * 4) = color;
66}
67
68#define gdAlphaMax 127
69#define gdAlphaOpaque 0
70#define gdAlphaTransparent 127
71#define gdRedMax 255
72#define gdGreenMax 255
73#define gdBlueMax 255
74#define gdTrueColorGetAlpha(c) (((c) & 0x7F000000) >> 24)
75#define gdTrueColorGetRed(c) (((c) & 0xFF0000) >> 16)
76#define gdTrueColorGetGreen(c) (((c) & 0x00FF00) >> 8)
77#define gdTrueColorGetBlue(c) ((c) & 0x0000FF)
78#define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + \
79 ((r) << 16) + \
80 ((g) << 8) + \
81 (b))
82
83void gdImageCopyResampled (uint8_t *dst,
84 uint8_t *src,
85 int dstX, int dstY,
86 int srcX, int srcY,
87 int dstW, int dstH, int srcW, int srcH)
88{
89 int x, y;
90 double sy1, sy2, sx1, sx2;
91 for (y = dstY; (y < dstY + dstH); y++)
92 {
93 sy1 = ((double) y - (double) dstY) * (double) srcH / (double) dstH;
94 sy2 = ((double) (y + 1) - (double) dstY) * (double) srcH /
95 (double) dstH;
96 for (x = dstX; (x < dstX + dstW); x++)
97 {
98 double sx, sy;
99 double spixels = 0;
100 double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
101 sx1 = ((double) x - (double) dstX) * (double) srcW / dstW;
102 sx2 = ((double) (x + 1) - (double) dstX) * (double) srcW / dstW;
103 sy = sy1;
104 do
105 {
106 double yportion;
107 if (floor2 (sy) == floor2 (sy1))
108 {
109 yportion = 1.0 - (sy - floor2 (sy));
110 if (yportion > sy2 - sy1)
111 {
112 yportion = sy2 - sy1;
113 }
114 sy = floor2 (sy);
115 }
116 else if (sy == floor2 (sy2))
117 {
118 yportion = sy2 - floor2 (sy2);
119 }
120 else
121 {
122 yportion = 1.0;
123 }
124 sx = sx1;
125 do
126 {
127 double xportion;
128 double pcontribution;
129 int p;
130 if (floor2 (sx) == floor2 (sx1))
131 {
132 xportion = 1.0 - (sx - floor2 (sx));
133 if (xportion > sx2 - sx1)
134 {
135 xportion = sx2 - sx1;
136 }
137 sx = floor2 (sx);
138 }
139 else if (sx == floor2 (sx2))
140 {
141 xportion = sx2 - floor2 (sx2);
142 }
143 else
144 {
145 xportion = 1.0;
146 }
147 pcontribution = xportion * yportion;
148 /* 2.08: previously srcX and srcY were ignored.
149 Andrew Pattison */
150 p = gdImageGetTrueColorPixel (src,
151 (int) sx + srcX,
152 (int) sy + srcY, srcW);
153 red += gdTrueColorGetRed (p) * pcontribution;
154 green += gdTrueColorGetGreen (p) * pcontribution;
155 blue += gdTrueColorGetBlue (p) * pcontribution;
156 alpha += gdTrueColorGetAlpha (p) * pcontribution;
157 spixels += xportion * yportion;
158 sx += 1.0;
159 }
160 while (sx < sx2);
161 sy += 1.0;
162 }
163 while (sy < sy2);
164 if (spixels != 0.0)
165 {
166 red /= spixels;
167 green /= spixels;
168 blue /= spixels;
169 alpha /= spixels;
170 }
171 /* Clamping to allow for rounding errors above */
172 if (red > 255.0)
173 {
174 red = 255.0;
175 }
176 if (green > 255.0)
177 {
178 green = 255.0;
179 }
180 if (blue > 255.0)
181 {
182 blue = 255.0;
183 }
184 if (alpha > gdAlphaMax)
185 {
186 alpha = gdAlphaMax;
187 }
188 gdImageSetPixel (dst,
189 x, y,
190 gdTrueColorAlpha ((int) red,
191 (int) green,
192 (int) blue, (int) alpha), dstW);
193 }
194 }
195}
196
197/* Fast integer implementation for 32 bpp bitmap scaling.
198 * Use fixed point values * 16.
199 */
200typedef int32_t FIXEDPOINT;
201#define INT_TO_FIXEDPOINT(i) (FIXEDPOINT)((i) << 4)
202#define FIXEDPOINT_TO_INT(v) (int)((v) >> 4)
203#define FIXEDPOINT_FLOOR(v) ((v) & ~0xF)
204#define FIXEDPOINT_FRACTION(v) ((v) & 0xF)
205
206/* For 32 bit source only. */
207void BitmapScale32 (uint8_t *dst,
208 int dstW, int dstH,
209 const uint8_t *src,
210 int iDeltaLine,
211 int srcW, int srcH)
212{
213 int x, y;
214
215 for (y = 0; y < dstH; y++)
216 {
217 FIXEDPOINT sy1 = INT_TO_FIXEDPOINT(y * srcH) / dstH;
218 FIXEDPOINT sy2 = INT_TO_FIXEDPOINT((y + 1) * srcH) / dstH;
219
220 for (x = 0; x < dstW; x++)
221 {
222 FIXEDPOINT red = 0, green = 0, blue = 0;
223
224 FIXEDPOINT sx1 = INT_TO_FIXEDPOINT(x * srcW) / dstW;
225 FIXEDPOINT sx2 = INT_TO_FIXEDPOINT((x + 1) * srcW) / dstW;
226
227 FIXEDPOINT spixels = (sx2 - sx1) * (sy2 - sy1);
228
229 FIXEDPOINT sy = sy1;
230
231 do
232 {
233 FIXEDPOINT yportion;
234 if (FIXEDPOINT_FLOOR (sy) == FIXEDPOINT_FLOOR (sy1))
235 {
236 yportion = INT_TO_FIXEDPOINT(1) - FIXEDPOINT_FRACTION(sy);
237 if (yportion > sy2 - sy1)
238 {
239 yportion = sy2 - sy1;
240 }
241 sy = FIXEDPOINT_FLOOR (sy);
242 }
243 else if (sy == FIXEDPOINT_FLOOR (sy2))
244 {
245 yportion = FIXEDPOINT_FRACTION(sy2);
246 }
247 else
248 {
249 yportion = INT_TO_FIXEDPOINT(1);
250 }
251
252 const uint8_t *pu8SrcLine = src + iDeltaLine * FIXEDPOINT_TO_INT(sy);
253 FIXEDPOINT sx = sx1;
254 do
255 {
256 FIXEDPOINT xportion;
257 FIXEDPOINT pcontribution;
258 int p;
259 if (FIXEDPOINT_FLOOR (sx) == FIXEDPOINT_FLOOR (sx1))
260 {
261 xportion = INT_TO_FIXEDPOINT(1) - FIXEDPOINT_FRACTION(sx);
262 if (xportion > sx2 - sx1)
263 {
264 xportion = sx2 - sx1;
265 }
266 pcontribution = xportion * yportion;
267 sx = FIXEDPOINT_FLOOR (sx);
268 }
269 else if (sx == FIXEDPOINT_FLOOR (sx2))
270 {
271 xportion = FIXEDPOINT_FRACTION(sx2);
272 pcontribution = xportion * yportion;
273 }
274 else
275 {
276 xportion = INT_TO_FIXEDPOINT(1);
277 pcontribution = xportion * yportion;
278 }
279 /* Color depth specific code begin */
280 p = *(uint32_t *)(pu8SrcLine + FIXEDPOINT_TO_INT(sx) * 4);
281 /* Color depth specific code end */
282 red += gdTrueColorGetRed (p) * pcontribution;
283 green += gdTrueColorGetGreen (p) * pcontribution;
284 blue += gdTrueColorGetBlue (p) * pcontribution;
285
286 sx += INT_TO_FIXEDPOINT(1);
287 } while (sx < sx2);
288
289 sy += INT_TO_FIXEDPOINT(1);
290 } while (sy < sy2);
291
292 if (spixels != 0)
293 {
294 red /= spixels;
295 green /= spixels;
296 blue /= spixels;
297 }
298 /* Clamping to allow for rounding errors above */
299 if (red > 255)
300 {
301 red = 255;
302 }
303 if (green > 255)
304 {
305 green = 255;
306 }
307 if (blue > 255)
308 {
309 blue = 255;
310 }
311 gdImageSetPixel (dst,
312 x, y,
313 ( ((int) red) << 16) + (((int) green) << 8) + ((int) blue),
314 dstW);
315 }
316 }
317}
318
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