blob: a7714764f141ee39dfa63173195133117a6f2374 [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),
Constantin Kaplinsky82800262006-12-05 03:31:03 +000034 xWeightTabs(0), yWeightTabs(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),
42 scale_ratio_x(1), scale_ratio_y(1), scaleFilterID(scaleFilterBicubic),
Constantin Kaplinsky82800262006-12-05 03:31:03 +000043 xWeightTabs(0), yWeightTabs(0), src_data(0), scaled_data(0) {
george824ff66752006-11-20 15:55:05 +000044 memset(&pf, 0, sizeof(pf));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000045}
46
47ScaledPixelBuffer::~ScaledPixelBuffer() {
george824ff66752006-11-20 15:55:05 +000048 freeWeightTabs();
49}
50
51void ScaledPixelBuffer::freeWeightTabs() {
52 if (xWeightTabs) {
53 for (int i = 0; i < scaled_width; i++) delete [] xWeightTabs[i].weight;
54 delete [] xWeightTabs;
55 xWeightTabs = 0;
56 }
57 if (yWeightTabs) {
58 for (int i = 0; i < scaled_height; i++) delete [] yWeightTabs[i].weight;
59 delete [] yWeightTabs;
60 yWeightTabs = 0;
61 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000062}
63
64void ScaledPixelBuffer::setSourceBuffer(U8 **src_data_, int w, int h) {
george824ff66752006-11-20 15:55:05 +000065 freeWeightTabs();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066 src_data = src_data_;
67 src_width = w;
68 src_height = h;
69 calculateScaledBufferSize();
george822446ed02007-03-10 08:55:35 +000070 scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
71 scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000072}
73
74void ScaledPixelBuffer::setPF(const PixelFormat &pf_) {
george824ff66752006-11-20 15:55:05 +000075 ///if (pf_.depth != 24) throw rfb::UnsupportedPixelFormatException();
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000076 pf = pf_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000077}
78
george822446ed02007-03-10 08:55:35 +000079void ScaledPixelBuffer::setScale(int scale_) {
80 if (scale != scale_ && scale_ > 0) {
george824ff66752006-11-20 15:55:05 +000081 freeWeightTabs();
george822446ed02007-03-10 08:55:35 +000082 scale = scale_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083 calculateScaledBufferSize();
george822446ed02007-03-10 08:55:35 +000084 scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
85 scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 }
87}
88
george82c4eb6262007-03-20 10:54:38 +000089void ScaledPixelBuffer::setScaleFilter(unsigned int scaleFilterID_) {
90 if (scaleFilterID == scaleFilterID_ || scaleFilterID_ > scaleFilterMaxNumber) return;
91
92 scaleFilterID = scaleFilterID_;
93
george82bfd8ecd2007-04-30 13:28:54 +000094 if (src_width && src_height && scaled_width && scaled_height) {
george82c4eb6262007-03-20 10:54:38 +000095 freeWeightTabs();
96 scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
97 scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
george82bfd8ecd2007-04-30 13:28:54 +000098 if (scale != 100 && pf.depth > 0) scaleRect(Rect(0, 0, src_width, src_height));
george82c4eb6262007-03-20 10:54:38 +000099 }
100}
101
george824ff66752006-11-20 15:55:05 +0000102inline void ScaledPixelBuffer::rgbFromPixel(U32 p, int &r, int &g, int &b) {
103 r = (((p >> pf.redShift ) & pf.redMax ) * 255 + pf.redMax /2) / pf.redMax;
104 g = (((p >> pf.greenShift) & pf.greenMax) * 255 + pf.greenMax/2) / pf.greenMax;
105 b = (((p >> pf.blueShift ) & pf.blueMax ) * 255 + pf.blueMax /2) / pf.blueMax;
106}
107
108inline U32 ScaledPixelBuffer::getSourcePixel(int x, int y) {
109 int bytes_per_pixel = pf.bpp / 8;
110 U8 *ptr = &(*src_data)[(x + y*src_width)*bytes_per_pixel];
111 if (bytes_per_pixel == 1) {
112 return *ptr;
113 } else if (bytes_per_pixel == 2) {
114 int b0 = *ptr++; int b1 = *ptr;
115 return b1 << 8 | b0;
116 } else if (bytes_per_pixel == 4) {
117 int b0 = *ptr++; int b1 = *ptr++;
118 int b2 = *ptr++; int b3 = *ptr;
119 return b3 << 24 | b2 << 16 | b1 << 8 | b0;
120 } else {
121 return 0;
122 }
123}
124
125void ScaledPixelBuffer::scaleRect(const Rect& rect) {
george824ff66752006-11-20 15:55:05 +0000126 Rect changed_rect;
george82e7e0ce22006-12-04 16:35:56 +0000127 U8 *ptr, *ptrs, *px, *pxs;
george8278122662007-10-28 10:32:01 +0000128 int r, g, b, rx, gx, bx, red, green, blue, xwi, ywi;
129 short *xweight, *yweight, xWeight, yWeight;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130
131 // Calculate the changed pixel rect in the scaled image
george824ff66752006-11-20 15:55:05 +0000132 changed_rect = calculateScaleBoundary(rect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000133
george82e7e0ce22006-12-04 16:35:56 +0000134 int bytesPerSrcPixel = pf.bpp / 8;
135 int bytesPerSrcRow = src_width * bytesPerSrcPixel;
136 int bytesPerScaledRow = scaled_width * 4;
george8243d1fa02006-11-26 11:18:38 +0000137
george82e7e0ce22006-12-04 16:35:56 +0000138 ptrs = &(*scaled_data)[(changed_rect.tl.x + changed_rect.tl.y*scaled_width) * 4];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139 for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) {
george82e7e0ce22006-12-04 16:35:56 +0000140 ptr = ptrs;
george82b09756b2006-12-04 15:49:41 +0000141 yweight = yWeightTabs[y].weight;
george82d5dacda2006-11-23 10:12:50 +0000142
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143 for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) {
george8278122662007-10-28 10:32:01 +0000144 // Init the sum of colors with (1 << (shift-1)) for rounding
145 ywi = 0; red = green = blue = 1 << (FINALSHIFT-1);
george82d5dacda2006-11-23 10:12:50 +0000146 xweight = xWeightTabs[x].weight;
147
148 // Calculate the scaled pixel value at (x, y) coordinates by
149 // convolution the matrix from source image:
150 // [(xWeight.i0,yWeight.i0)......(xWeight.i1-1,yWeight.i0)]
151 // [......................................................]
152 // [(xWeight.i0,yWeight.i1-1)..(xWeight.i1-1,yWeight.i1-1)],
153 // where [i0, i1) is the scaled filter interval.
george82e7e0ce22006-12-04 16:35:56 +0000154 pxs = &(*src_data)[(xWeightTabs[x].i0 + yWeightTabs[y].i0*src_width) * bytesPerSrcPixel];
george824ff66752006-11-20 15:55:05 +0000155 for (int ys = yWeightTabs[y].i0; ys < yWeightTabs[y].i1; ys++) {
george82e7e0ce22006-12-04 16:35:56 +0000156 xwi = 0; rx = 0; gx = 0; bx = 0; px = pxs;
george824ff66752006-11-20 15:55:05 +0000157 for (int xs = xWeightTabs[x].i0; xs < xWeightTabs[x].i1; xs++) {
george82e7e0ce22006-12-04 16:35:56 +0000158 rgbFromPixel(*((U32*)px), r, g, b);
george82d5dacda2006-11-23 10:12:50 +0000159 xWeight = xweight[xwi++];
george8278122662007-10-28 10:32:01 +0000160 rx += (int)xWeight * r;
161 gx += (int)xWeight * g;
162 bx += (int)xWeight * b;
george82e7e0ce22006-12-04 16:35:56 +0000163 px += bytesPerSrcPixel;
george824ff66752006-11-20 15:55:05 +0000164 }
george82d5dacda2006-11-23 10:12:50 +0000165 yWeight = yweight[ywi++];
george8278122662007-10-28 10:32:01 +0000166 red += (int)yWeight * (rx >> BITS_OF_CHANEL);
167 green += (int)yWeight * (gx >> BITS_OF_CHANEL);
168 blue += (int)yWeight * (bx >> BITS_OF_CHANEL);
george82e7e0ce22006-12-04 16:35:56 +0000169 pxs += bytesPerSrcRow;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170 }
george8278122662007-10-28 10:32:01 +0000171 *ptr++ = U8(blue >> FINALSHIFT);
172 *ptr++ = U8(green >> FINALSHIFT);
173 *ptr++ = U8(red >> FINALSHIFT);
george824ff66752006-11-20 15:55:05 +0000174 ptr++;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 }
george82e7e0ce22006-12-04 16:35:56 +0000176 ptrs += bytesPerScaledRow;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177 }
178}
179
180Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) {
george824ff66752006-11-20 15:55:05 +0000181 int x_start, y_start, x_end, y_end;
george825e562842006-12-03 17:31:39 +0000182 double radius = scaleFilters[scaleFilterID].radius;
george822446ed02007-03-10 08:55:35 +0000183 double translate_x = 0.5*scale_ratio_x - 0.5;
184 double translate_y = 0.5*scale_ratio_y - 0.5;
185 x_start = (int)ceil(scale_ratio_x*(r.tl.x-radius) + translate_x);
186 y_start = (int)ceil(scale_ratio_y*(r.tl.y-radius) + translate_y);
187 x_end = (int)ceil(scale_ratio_x*(r.br.x+radius) + translate_x);
188 y_end = (int)ceil(scale_ratio_y*(r.br.y+radius) + translate_y);
george825e562842006-12-03 17:31:39 +0000189 if (x_start < 0) x_start = 0;
190 if (y_start < 0) y_start = 0;
191 if (x_end > scaled_width) x_end = scaled_width;
192 if (y_end > scaled_height) y_end = scaled_height;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000193 return Rect(x_start, y_start, x_end, y_end);
194}
195
196void ScaledPixelBuffer::calculateScaledBufferSize() {
george822446ed02007-03-10 08:55:35 +0000197 double scale_ratio = (double)scale / 100;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000198 scaled_width = (int)ceil(src_width * scale_ratio);
199 scaled_height = (int)ceil(src_height * scale_ratio);
george822446ed02007-03-10 08:55:35 +0000200 scale_ratio_x = (double)scaled_width / src_width;
201 scale_ratio_y = (double)scaled_height / src_height;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202}