VirtualBox

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

Last change on this file since 60422 was 55401, checked in by vboxsync, 10 years ago

added a couple of missing Id headers

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