blob: 056e5d6d446f1d7cc3641f48e17819620c0b4983 [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_)
george823e111532007-11-05 14:28:40 +000033 : scale(scale_), scale_ratio_x(1), scale_ratio_y(1), scaleFilterID(scaleFilterBilinear),
Adam Tkacfee32e32008-10-10 15:48:22 +000034 xWeightTabs(0), yWeightTabs(0), raccum(0), gaccum(0), baccum(0), scaled_data(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),
george823e111532007-11-05 14:28:40 +000042 scale_ratio_x(1), scale_ratio_y(1), scaleFilterID(scaleFilterBilinear),
Adam Tkacfee32e32008-10-10 15:48:22 +000043 xWeightTabs(0), yWeightTabs(0), raccum(0), gaccum(0), baccum(0),
44 src_data(0), scaled_data(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);
george8264379b02007-11-05 16:33:09 +0000114 if (scale != 100 && pf.depth > 0 && scaled_data) scaleRect(Rect(0, 0, src_width, src_height));
george82c4eb6262007-03-20 10:54:38 +0000115 }
116}
117
george824ff66752006-11-20 15:55:05 +0000118inline U32 ScaledPixelBuffer::getSourcePixel(int x, int y) {
119 int bytes_per_pixel = pf.bpp / 8;
120 U8 *ptr = &(*src_data)[(x + y*src_width)*bytes_per_pixel];
121 if (bytes_per_pixel == 1) {
122 return *ptr;
123 } else if (bytes_per_pixel == 2) {
124 int b0 = *ptr++; int b1 = *ptr;
125 return b1 << 8 | b0;
126 } else if (bytes_per_pixel == 4) {
127 int b0 = *ptr++; int b1 = *ptr++;
128 int b2 = *ptr++; int b3 = *ptr;
129 return b3 << 24 | b2 << 16 | b1 << 8 | b0;
130 } else {
131 return 0;
132 }
133}
134
135void ScaledPixelBuffer::scaleRect(const Rect& rect) {
george824ff66752006-11-20 15:55:05 +0000136 Rect changed_rect;
george82e7e0ce22006-12-04 16:35:56 +0000137 U8 *ptr, *ptrs, *px, *pxs;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000138 U16 r, g, b;
139 int red, green, blue;
george823a1982e2007-11-04 07:35:51 +0000140 short *xweight, *yweight, weight;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000141
142 // Calculate the changed pixel rect in the scaled image
george824ff66752006-11-20 15:55:05 +0000143 changed_rect = calculateScaleBoundary(rect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000144
george82e7e0ce22006-12-04 16:35:56 +0000145 int bytesPerSrcPixel = pf.bpp / 8;
146 int bytesPerSrcRow = src_width * bytesPerSrcPixel;
147 int bytesPerScaledRow = scaled_width * 4;
george8243d1fa02006-11-26 11:18:38 +0000148
george823a1982e2007-11-04 07:35:51 +0000149 int bytesPerAccumRow = src_width * sizeof(int);
150
george82e7e0ce22006-12-04 16:35:56 +0000151 ptrs = &(*scaled_data)[(changed_rect.tl.x + changed_rect.tl.y*scaled_width) * 4];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000152 for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) {
george82e7e0ce22006-12-04 16:35:56 +0000153 ptr = ptrs;
george82b09756b2006-12-04 15:49:41 +0000154 yweight = yWeightTabs[y].weight;
george82d5dacda2006-11-23 10:12:50 +0000155
george823a1982e2007-11-04 07:35:51 +0000156 // Clear the color accumulators
157 memset(raccum, 0, bytesPerAccumRow);
158 memset(gaccum, 0, bytesPerAccumRow);
159 memset(baccum, 0, bytesPerAccumRow);
160
161 // Make the convolution the source image with scale filter weights
162 // by y axis and save results to the color accumulators.
163 pxs = &(*src_data)[(xWeightTabs[changed_rect.tl.x].i0 + yWeightTabs[y].i0*src_width) * bytesPerSrcPixel];
164 for (int ys = yWeightTabs[y].i0; ys < yWeightTabs[y].i1; ys++) {
165 px = pxs;
166 for (int xs = xWeightTabs[changed_rect.tl.x].i0; xs < xWeightTabs[changed_rect.br.x-1].i1; xs++) {
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000167 pf.rgbFromPixel(*((U32*)px), NULL, &r, &g, &b);
george823a1982e2007-11-04 07:35:51 +0000168 weight = *yweight;
169 raccum[xs] += (int)(weight) * r;
170 gaccum[xs] += (int)(weight) * g;
171 baccum[xs] += (int)(weight) * b;
172 px += bytesPerSrcPixel;
173 }
174 yweight++;
175 pxs += bytesPerSrcRow;
176 }
177
178 // Make the convolution the color accumulators with scale filter weights
179 // by x axis and save results to the scaled image.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180 for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) {
george823a1982e2007-11-04 07:35:51 +0000181 // Init the sum of colors with (1 << (shift-1)) for rounding.
182 red = green = blue = 1 << (FINALSHIFT-1);
george82d5dacda2006-11-23 10:12:50 +0000183 xweight = xWeightTabs[x].weight;
george823a1982e2007-11-04 07:35:51 +0000184 for (int xs = xWeightTabs[x].i0; xs < xWeightTabs[x].i1; xs++) {
185 weight = *xweight;
186 red += (int)(weight) * (raccum[xs] >> BITS_OF_CHANEL);
187 green += (int)(weight) * (gaccum[xs] >> BITS_OF_CHANEL);
188 blue += (int)(weight) * (baccum[xs] >> BITS_OF_CHANEL);
189 xweight++;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000190 }
george8278122662007-10-28 10:32:01 +0000191 *ptr++ = U8(blue >> FINALSHIFT);
192 *ptr++ = U8(green >> FINALSHIFT);
193 *ptr++ = U8(red >> FINALSHIFT);
george824ff66752006-11-20 15:55:05 +0000194 ptr++;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195 }
george82e7e0ce22006-12-04 16:35:56 +0000196 ptrs += bytesPerScaledRow;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000197 }
198}
199
200Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) {
george824ff66752006-11-20 15:55:05 +0000201 int x_start, y_start, x_end, y_end;
george822446ed02007-03-10 08:55:35 +0000202 double translate_x = 0.5*scale_ratio_x - 0.5;
203 double translate_y = 0.5*scale_ratio_y - 0.5;
george82c79c7bb2007-11-05 11:22:14 +0000204 double sourceXScale = __rfbmax(1.0, 1.0/scale_ratio_x);
205 double sourceYScale = __rfbmax(1.0, 1.0/scale_ratio_y);
206 double sourceXRadius = __rfbmax(0.5, sourceXScale*scaleFilters[scaleFilterID].radius);
207 double sourceYRadius = __rfbmax(0.5, sourceYScale*scaleFilters[scaleFilterID].radius);
208 x_start = (int)ceil(scale_ratio_x*(r.tl.x-sourceXRadius) + translate_x + SCALE_ERROR);
209 y_start = (int)ceil(scale_ratio_y*(r.tl.y-sourceYRadius) + translate_y + SCALE_ERROR);
210 x_end = (int)floor(scale_ratio_x*((r.br.x-1)+sourceXRadius) + translate_x - SCALE_ERROR) + 1;
211 y_end = (int)floor(scale_ratio_y*((r.br.y-1)+sourceXRadius) + translate_y - SCALE_ERROR) + 1;
george825e562842006-12-03 17:31:39 +0000212 if (x_start < 0) x_start = 0;
213 if (y_start < 0) y_start = 0;
214 if (x_end > scaled_width) x_end = scaled_width;
215 if (y_end > scaled_height) y_end = scaled_height;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216 return Rect(x_start, y_start, x_end, y_end);
217}
218
219void ScaledPixelBuffer::calculateScaledBufferSize() {
george822446ed02007-03-10 08:55:35 +0000220 double scale_ratio = (double)scale / 100;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000221 scaled_width = (int)ceil(src_width * scale_ratio);
222 scaled_height = (int)ceil(src_height * scale_ratio);
george822446ed02007-03-10 08:55:35 +0000223 scale_ratio_x = (double)scaled_width / src_width;
224 scale_ratio_y = (double)scaled_height / src_height;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000225}