blob: 12658f7b22755423b0189e3b1af9c80e15251347 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2005 TightVNC Team. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- ScaledPixelBuffer.cxx
20
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000021#include <rfb/Exception.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000022#include <rfb/ScaledPixelBuffer.h>
23
24#include <math.h>
25#include <memory.h>
george824ff66752006-11-20 15:55:05 +000026#include <stdlib.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027
28using namespace rdr;
29using namespace rfb;
30
31ScaledPixelBuffer::ScaledPixelBuffer(U8 **src_data_, int src_width_,
george82204162c2007-10-28 08:35:08 +000032 int src_height_, int scale_, PixelFormat pf_)
33 : scale(scale_), scale_ratio_x(1), scale_ratio_y(1), scaleFilterID(scaleFilterBicubic),
george823a1982e2007-11-04 07:35:51 +000034 xWeightTabs(0), yWeightTabs(0), scaled_data(0), raccum(0), gaccum(0), baccum(0) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035
36 setSourceBuffer(src_data_, src_width_, src_height_);
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000037 setPF(pf_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000038}
39
40ScaledPixelBuffer::ScaledPixelBuffer()
george822446ed02007-03-10 08:55:35 +000041 : src_width(0), src_height(0), scaled_width(0), scaled_height(0), scale(100),
42 scale_ratio_x(1), scale_ratio_y(1), scaleFilterID(scaleFilterBicubic),
george823a1982e2007-11-04 07:35:51 +000043 xWeightTabs(0), yWeightTabs(0), src_data(0), scaled_data(0), raccum(0),
44 gaccum(0), baccum(0) {
george824ff66752006-11-20 15:55:05 +000045 memset(&pf, 0, sizeof(pf));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046}
47
48ScaledPixelBuffer::~ScaledPixelBuffer() {
george824ff66752006-11-20 15:55:05 +000049 freeWeightTabs();
george823a1982e2007-11-04 07:35:51 +000050 if (raccum) delete [] raccum;
51 if (gaccum) delete [] gaccum;
52 if (baccum) delete [] baccum;
george824ff66752006-11-20 15:55:05 +000053}
54
55void ScaledPixelBuffer::freeWeightTabs() {
56 if (xWeightTabs) {
57 for (int i = 0; i < scaled_width; i++) delete [] xWeightTabs[i].weight;
58 delete [] xWeightTabs;
59 xWeightTabs = 0;
60 }
61 if (yWeightTabs) {
62 for (int i = 0; i < scaled_height; i++) delete [] yWeightTabs[i].weight;
63 delete [] yWeightTabs;
64 yWeightTabs = 0;
65 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066}
67
george823a1982e2007-11-04 07:35:51 +000068void ScaledPixelBuffer::recreateRowAccum() {
69 if (raccum) delete [] raccum;
70 if (gaccum) delete [] gaccum;
71 if (baccum) delete [] baccum;
72 raccum = new int[src_width];
73 gaccum = new int[src_width];
74 baccum = new int[src_width];
75}
76
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000077void ScaledPixelBuffer::setSourceBuffer(U8 **src_data_, int w, int h) {
george823a1982e2007-11-04 07:35:51 +000078 if (w > 0 && h > 0 && src_data != NULL) {
79 freeWeightTabs();
80 src_data = src_data_;
81 src_width = w;
82 src_height = h;
83 recreateRowAccum();
84 calculateScaledBufferSize();
85 scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
86 scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
87 }
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000088}
89
90void ScaledPixelBuffer::setPF(const PixelFormat &pf_) {
george824ff66752006-11-20 15:55:05 +000091 ///if (pf_.depth != 24) throw rfb::UnsupportedPixelFormatException();
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000092 pf = pf_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093}
94
george822446ed02007-03-10 08:55:35 +000095void ScaledPixelBuffer::setScale(int scale_) {
96 if (scale != scale_ && scale_ > 0) {
george822446ed02007-03-10 08:55:35 +000097 scale = scale_;
george82c79c7bb2007-11-05 11:22:14 +000098 freeWeightTabs();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099 calculateScaledBufferSize();
george822446ed02007-03-10 08:55:35 +0000100 scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
101 scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 }
103}
104
george82c4eb6262007-03-20 10:54:38 +0000105void ScaledPixelBuffer::setScaleFilter(unsigned int scaleFilterID_) {
106 if (scaleFilterID == scaleFilterID_ || scaleFilterID_ > scaleFilterMaxNumber) return;
107
108 scaleFilterID = scaleFilterID_;
109
george82bfd8ecd2007-04-30 13:28:54 +0000110 if (src_width && src_height && scaled_width && scaled_height) {
george82c4eb6262007-03-20 10:54:38 +0000111 freeWeightTabs();
112 scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
113 scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
george82bfd8ecd2007-04-30 13:28:54 +0000114 if (scale != 100 && pf.depth > 0) scaleRect(Rect(0, 0, src_width, src_height));
george82c4eb6262007-03-20 10:54:38 +0000115 }
116}
117
george824ff66752006-11-20 15:55:05 +0000118inline void ScaledPixelBuffer::rgbFromPixel(U32 p, int &r, int &g, int &b) {
119 r = (((p >> pf.redShift ) & pf.redMax ) * 255 + pf.redMax /2) / pf.redMax;
120 g = (((p >> pf.greenShift) & pf.greenMax) * 255 + pf.greenMax/2) / pf.greenMax;
121 b = (((p >> pf.blueShift ) & pf.blueMax ) * 255 + pf.blueMax /2) / pf.blueMax;
122}
123
124inline U32 ScaledPixelBuffer::getSourcePixel(int x, int y) {
125 int bytes_per_pixel = pf.bpp / 8;
126 U8 *ptr = &(*src_data)[(x + y*src_width)*bytes_per_pixel];
127 if (bytes_per_pixel == 1) {
128 return *ptr;
129 } else if (bytes_per_pixel == 2) {
130 int b0 = *ptr++; int b1 = *ptr;
131 return b1 << 8 | b0;
132 } else if (bytes_per_pixel == 4) {
133 int b0 = *ptr++; int b1 = *ptr++;
134 int b2 = *ptr++; int b3 = *ptr;
135 return b3 << 24 | b2 << 16 | b1 << 8 | b0;
136 } else {
137 return 0;
138 }
139}
140
141void ScaledPixelBuffer::scaleRect(const Rect& rect) {
george824ff66752006-11-20 15:55:05 +0000142 Rect changed_rect;
george82e7e0ce22006-12-04 16:35:56 +0000143 U8 *ptr, *ptrs, *px, *pxs;
george823a1982e2007-11-04 07:35:51 +0000144 int r, g, b, red, green, blue;
145 short *xweight, *yweight, weight;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146
147 // Calculate the changed pixel rect in the scaled image
george824ff66752006-11-20 15:55:05 +0000148 changed_rect = calculateScaleBoundary(rect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000149
george82e7e0ce22006-12-04 16:35:56 +0000150 int bytesPerSrcPixel = pf.bpp / 8;
151 int bytesPerSrcRow = src_width * bytesPerSrcPixel;
152 int bytesPerScaledRow = scaled_width * 4;
george8243d1fa02006-11-26 11:18:38 +0000153
george823a1982e2007-11-04 07:35:51 +0000154 int bytesPerAccumRow = src_width * sizeof(int);
155
george82e7e0ce22006-12-04 16:35:56 +0000156 ptrs = &(*scaled_data)[(changed_rect.tl.x + changed_rect.tl.y*scaled_width) * 4];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157 for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) {
george82e7e0ce22006-12-04 16:35:56 +0000158 ptr = ptrs;
george82b09756b2006-12-04 15:49:41 +0000159 yweight = yWeightTabs[y].weight;
george82d5dacda2006-11-23 10:12:50 +0000160
george823a1982e2007-11-04 07:35:51 +0000161 // Clear the color accumulators
162 memset(raccum, 0, bytesPerAccumRow);
163 memset(gaccum, 0, bytesPerAccumRow);
164 memset(baccum, 0, bytesPerAccumRow);
165
166 // Make the convolution the source image with scale filter weights
167 // by y axis and save results to the color accumulators.
168 pxs = &(*src_data)[(xWeightTabs[changed_rect.tl.x].i0 + yWeightTabs[y].i0*src_width) * bytesPerSrcPixel];
169 for (int ys = yWeightTabs[y].i0; ys < yWeightTabs[y].i1; ys++) {
170 px = pxs;
171 for (int xs = xWeightTabs[changed_rect.tl.x].i0; xs < xWeightTabs[changed_rect.br.x-1].i1; xs++) {
172 rgbFromPixel(*((U32*)px), r, g, b);
173 weight = *yweight;
174 raccum[xs] += (int)(weight) * r;
175 gaccum[xs] += (int)(weight) * g;
176 baccum[xs] += (int)(weight) * b;
177 px += bytesPerSrcPixel;
178 }
179 yweight++;
180 pxs += bytesPerSrcRow;
181 }
182
183 // Make the convolution the color accumulators with scale filter weights
184 // by x axis and save results to the scaled image.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000185 for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) {
george823a1982e2007-11-04 07:35:51 +0000186 // Init the sum of colors with (1 << (shift-1)) for rounding.
187 red = green = blue = 1 << (FINALSHIFT-1);
george82d5dacda2006-11-23 10:12:50 +0000188 xweight = xWeightTabs[x].weight;
george823a1982e2007-11-04 07:35:51 +0000189 for (int xs = xWeightTabs[x].i0; xs < xWeightTabs[x].i1; xs++) {
190 weight = *xweight;
191 red += (int)(weight) * (raccum[xs] >> BITS_OF_CHANEL);
192 green += (int)(weight) * (gaccum[xs] >> BITS_OF_CHANEL);
193 blue += (int)(weight) * (baccum[xs] >> BITS_OF_CHANEL);
194 xweight++;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195 }
george8278122662007-10-28 10:32:01 +0000196 *ptr++ = U8(blue >> FINALSHIFT);
197 *ptr++ = U8(green >> FINALSHIFT);
198 *ptr++ = U8(red >> FINALSHIFT);
george824ff66752006-11-20 15:55:05 +0000199 ptr++;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000200 }
george82e7e0ce22006-12-04 16:35:56 +0000201 ptrs += bytesPerScaledRow;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 }
203}
204
205Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) {
george824ff66752006-11-20 15:55:05 +0000206 int x_start, y_start, x_end, y_end;
george822446ed02007-03-10 08:55:35 +0000207 double translate_x = 0.5*scale_ratio_x - 0.5;
208 double translate_y = 0.5*scale_ratio_y - 0.5;
george82c79c7bb2007-11-05 11:22:14 +0000209 double sourceXScale = __rfbmax(1.0, 1.0/scale_ratio_x);
210 double sourceYScale = __rfbmax(1.0, 1.0/scale_ratio_y);
211 double sourceXRadius = __rfbmax(0.5, sourceXScale*scaleFilters[scaleFilterID].radius);
212 double sourceYRadius = __rfbmax(0.5, sourceYScale*scaleFilters[scaleFilterID].radius);
213 x_start = (int)ceil(scale_ratio_x*(r.tl.x-sourceXRadius) + translate_x + SCALE_ERROR);
214 y_start = (int)ceil(scale_ratio_y*(r.tl.y-sourceYRadius) + translate_y + SCALE_ERROR);
215 x_end = (int)floor(scale_ratio_x*((r.br.x-1)+sourceXRadius) + translate_x - SCALE_ERROR) + 1;
216 y_end = (int)floor(scale_ratio_y*((r.br.y-1)+sourceXRadius) + translate_y - SCALE_ERROR) + 1;
george825e562842006-12-03 17:31:39 +0000217 if (x_start < 0) x_start = 0;
218 if (y_start < 0) y_start = 0;
219 if (x_end > scaled_width) x_end = scaled_width;
220 if (y_end > scaled_height) y_end = scaled_height;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000221 return Rect(x_start, y_start, x_end, y_end);
222}
223
224void ScaledPixelBuffer::calculateScaledBufferSize() {
george822446ed02007-03-10 08:55:35 +0000225 double scale_ratio = (double)scale / 100;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000226 scaled_width = (int)ceil(src_width * scale_ratio);
227 scaled_height = (int)ceil(src_height * scale_ratio);
george822446ed02007-03-10 08:55:35 +0000228 scale_ratio_x = (double)scaled_width / src_width;
229 scale_ratio_y = (double)scaled_height / src_height;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000230}