VirtualBox

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

Last change on this file since 25414 was 24942, checked in by vboxsync, 14 years ago

Main: fix header for image resampling code

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

© 2023 Oracle
ContactPrivacy policyTerms of Use