1 | /* $Id: DisplayResampleImage.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Image resampling code, used for snapshot thumbnails.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2019 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 | #include <iprt/types.h>
|
---|
19 |
|
---|
20 | DECLINLINE(void) imageSetPixel (uint8_t *im, int x, int y, int color, int w)
|
---|
21 | {
|
---|
22 | *(int32_t *)(im + y * w * 4 + x * 4) = color;
|
---|
23 | }
|
---|
24 |
|
---|
25 | #define trueColorGetAlpha(c) (((c) & 0x7F000000) >> 24)
|
---|
26 | #define trueColorGetRed(c) (((c) & 0xFF0000) >> 16)
|
---|
27 | #define trueColorGetGreen(c) (((c) & 0x00FF00) >> 8)
|
---|
28 | #define trueColorGetBlue(c) ((c) & 0x0000FF)
|
---|
29 |
|
---|
30 | /* Fast integer implementation for 32 bpp bitmap scaling.
|
---|
31 | * Using fixed point values * 16.
|
---|
32 | */
|
---|
33 | typedef int32_t FIXEDPOINT;
|
---|
34 | #define INT_TO_FIXEDPOINT(i) (FIXEDPOINT)((i) << 4)
|
---|
35 | #define FIXEDPOINT_TO_INT(v) (int)((v) >> 4)
|
---|
36 | #define FIXEDPOINT_FLOOR(v) ((v) & ~0xF)
|
---|
37 | #define FIXEDPOINT_FRACTION(v) ((v) & 0xF)
|
---|
38 |
|
---|
39 | /* For 32 bit source only. */
|
---|
40 | void BitmapScale32 (uint8_t *dst,
|
---|
41 | int dstW, int dstH,
|
---|
42 | const uint8_t *src,
|
---|
43 | int iDeltaLine,
|
---|
44 | int srcW, int srcH)
|
---|
45 | {
|
---|
46 | int x, y;
|
---|
47 |
|
---|
48 | for (y = 0; y < dstH; y++)
|
---|
49 | {
|
---|
50 | FIXEDPOINT sy1 = INT_TO_FIXEDPOINT(y * srcH) / dstH;
|
---|
51 | FIXEDPOINT sy2 = INT_TO_FIXEDPOINT((y + 1) * srcH) / dstH;
|
---|
52 |
|
---|
53 | for (x = 0; x < dstW; x++)
|
---|
54 | {
|
---|
55 | FIXEDPOINT red = 0, green = 0, blue = 0;
|
---|
56 |
|
---|
57 | FIXEDPOINT sx1 = INT_TO_FIXEDPOINT(x * srcW) / dstW;
|
---|
58 | FIXEDPOINT sx2 = INT_TO_FIXEDPOINT((x + 1) * srcW) / dstW;
|
---|
59 |
|
---|
60 | FIXEDPOINT spixels = (sx2 - sx1) * (sy2 - sy1);
|
---|
61 |
|
---|
62 | FIXEDPOINT sy = sy1;
|
---|
63 |
|
---|
64 | do
|
---|
65 | {
|
---|
66 | FIXEDPOINT yportion;
|
---|
67 | if (FIXEDPOINT_FLOOR (sy) == FIXEDPOINT_FLOOR (sy1))
|
---|
68 | {
|
---|
69 | yportion = INT_TO_FIXEDPOINT(1) - FIXEDPOINT_FRACTION(sy);
|
---|
70 | if (yportion > sy2 - sy1)
|
---|
71 | {
|
---|
72 | yportion = sy2 - sy1;
|
---|
73 | }
|
---|
74 | sy = FIXEDPOINT_FLOOR (sy);
|
---|
75 | }
|
---|
76 | else if (sy == FIXEDPOINT_FLOOR (sy2))
|
---|
77 | {
|
---|
78 | yportion = FIXEDPOINT_FRACTION(sy2);
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | yportion = INT_TO_FIXEDPOINT(1);
|
---|
83 | }
|
---|
84 |
|
---|
85 | const uint8_t *pu8SrcLine = src + iDeltaLine * FIXEDPOINT_TO_INT(sy);
|
---|
86 | FIXEDPOINT sx = sx1;
|
---|
87 | do
|
---|
88 | {
|
---|
89 | FIXEDPOINT xportion;
|
---|
90 | FIXEDPOINT pcontribution;
|
---|
91 | int p;
|
---|
92 | if (FIXEDPOINT_FLOOR (sx) == FIXEDPOINT_FLOOR (sx1))
|
---|
93 | {
|
---|
94 | xportion = INT_TO_FIXEDPOINT(1) - FIXEDPOINT_FRACTION(sx);
|
---|
95 | if (xportion > sx2 - sx1)
|
---|
96 | {
|
---|
97 | xportion = sx2 - sx1;
|
---|
98 | }
|
---|
99 | pcontribution = xportion * yportion;
|
---|
100 | sx = FIXEDPOINT_FLOOR (sx);
|
---|
101 | }
|
---|
102 | else if (sx == FIXEDPOINT_FLOOR (sx2))
|
---|
103 | {
|
---|
104 | xportion = FIXEDPOINT_FRACTION(sx2);
|
---|
105 | pcontribution = xportion * yportion;
|
---|
106 | }
|
---|
107 | else
|
---|
108 | {
|
---|
109 | xportion = INT_TO_FIXEDPOINT(1);
|
---|
110 | pcontribution = xportion * yportion;
|
---|
111 | }
|
---|
112 | /* Color depth specific code begin */
|
---|
113 | p = *(uint32_t *)(pu8SrcLine + FIXEDPOINT_TO_INT(sx) * 4);
|
---|
114 | /* Color depth specific code end */
|
---|
115 | red += trueColorGetRed (p) * pcontribution;
|
---|
116 | green += trueColorGetGreen (p) * pcontribution;
|
---|
117 | blue += trueColorGetBlue (p) * pcontribution;
|
---|
118 |
|
---|
119 | sx += INT_TO_FIXEDPOINT(1);
|
---|
120 | } while (sx < sx2);
|
---|
121 |
|
---|
122 | sy += INT_TO_FIXEDPOINT(1);
|
---|
123 | } while (sy < sy2);
|
---|
124 |
|
---|
125 | if (spixels != 0)
|
---|
126 | {
|
---|
127 | red /= spixels;
|
---|
128 | green /= spixels;
|
---|
129 | blue /= spixels;
|
---|
130 | }
|
---|
131 | /* Clamping to allow for rounding errors above */
|
---|
132 | if (red > 255)
|
---|
133 | {
|
---|
134 | red = 255;
|
---|
135 | }
|
---|
136 | if (green > 255)
|
---|
137 | {
|
---|
138 | green = 255;
|
---|
139 | }
|
---|
140 | if (blue > 255)
|
---|
141 | {
|
---|
142 | blue = 255;
|
---|
143 | }
|
---|
144 | imageSetPixel (dst,
|
---|
145 | x, y,
|
---|
146 | ( ((int) red) << 16) + (((int) green) << 8) + ((int) blue),
|
---|
147 | dstW);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|