VirtualBox

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

Last change on this file since 29940 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.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 ([email protected]).
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}
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