1 | /** @file
|
---|
2 | * Image resampling code, used for snapshot thumbnails.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2009-2017 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 <cr_bmpscale.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 |
|
---|
56 | typedef uint8_t *gdImagePtr;
|
---|
57 |
|
---|
58 | DECLINLINE(int) gdImageGetTrueColorPixel (gdImagePtr im, int x, int y, int w)
|
---|
59 | {
|
---|
60 | return *(int32_t *)(im + y * w * 4 + x * 4);
|
---|
61 | }
|
---|
62 |
|
---|
63 | DECLINLINE(void) gdImageSetPixel (gdImagePtr im, int x, int y, int color, int cbLine)
|
---|
64 | {
|
---|
65 | *(int32_t *)(im + y * cbLine + 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 |
|
---|
83 | void 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 * 4);
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* Fast integer implementation for 32 bpp bitmap scaling.
|
---|
198 | * Use fixed point values * 16.
|
---|
199 | */
|
---|
200 | typedef 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. */
|
---|
207 | VBOXBMPSCALEDECL(void) CrBmpScale32 (uint8_t *dst,
|
---|
208 | int iDstDeltaLine,
|
---|
209 | int dstW, int dstH,
|
---|
210 | const uint8_t *src,
|
---|
211 | int iSrcDeltaLine,
|
---|
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 + iSrcDeltaLine * 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 | iDstDeltaLine);
|
---|
316 | }
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|