blob: 3e55630d4f5403d9f74d73f54d695922fa102022 [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_,
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000032 int src_height_, int scale, PixelFormat pf_)
george824ff66752006-11-20 15:55:05 +000033 : xWeightTabs(0), yWeightTabs(0), scaled_data(0), scale_ratio(1),
george8210135272006-12-03 17:36:47 +000034 scaleFilterID(scaleFilterBilinear) {
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()
george82f80971b2006-08-17 09:57:43 +000041 : src_data(0), src_width(0), src_height(0), scale_ratio(1), scaled_width(0),
george824ff66752006-11-20 15:55:05 +000042 xWeightTabs(0), yWeightTabs(0), scaled_height(0), scaled_data(0),
george8210135272006-12-03 17:36:47 +000043 scaleFilterID(scaleFilterBilinear) {
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();
george821c2e9e62006-12-03 12:46:54 +000070 scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, scale_ratio, &xWeightTabs);
71 scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, scale_ratio, &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
george82d4344be2006-07-29 10:27:19 +000079void ScaledPixelBuffer::setScaleRatio(double scale_ratio_) {
80 if (scale_ratio != scale_ratio_) {
george824ff66752006-11-20 15:55:05 +000081 freeWeightTabs();
george82d4344be2006-07-29 10:27:19 +000082 scale_ratio = scale_ratio_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083 calculateScaledBufferSize();
george821c2e9e62006-12-03 12:46:54 +000084 scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, scale_ratio, &xWeightTabs);
85 scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, scale_ratio, &yWeightTabs);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 }
87}
88
george824ff66752006-11-20 15:55:05 +000089inline void ScaledPixelBuffer::rgbFromPixel(U32 p, int &r, int &g, int &b) {
90 r = (((p >> pf.redShift ) & pf.redMax ) * 255 + pf.redMax /2) / pf.redMax;
91 g = (((p >> pf.greenShift) & pf.greenMax) * 255 + pf.greenMax/2) / pf.greenMax;
92 b = (((p >> pf.blueShift ) & pf.blueMax ) * 255 + pf.blueMax /2) / pf.blueMax;
93}
94
95inline U32 ScaledPixelBuffer::getSourcePixel(int x, int y) {
96 int bytes_per_pixel = pf.bpp / 8;
97 U8 *ptr = &(*src_data)[(x + y*src_width)*bytes_per_pixel];
98 if (bytes_per_pixel == 1) {
99 return *ptr;
100 } else if (bytes_per_pixel == 2) {
101 int b0 = *ptr++; int b1 = *ptr;
102 return b1 << 8 | b0;
103 } else if (bytes_per_pixel == 4) {
104 int b0 = *ptr++; int b1 = *ptr++;
105 int b2 = *ptr++; int b3 = *ptr;
106 return b3 << 24 | b2 << 16 | b1 << 8 | b0;
107 } else {
108 return 0;
109 }
110}
111
112void ScaledPixelBuffer::scaleRect(const Rect& rect) {
george824ff66752006-11-20 15:55:05 +0000113 Rect changed_rect;
george821c2e9e62006-12-03 12:46:54 +0000114 U8 *ptr;
115 double rx, gx, bx, red, green, blue, *xweight, *yweight, xWeight, yWeight;
george82d5dacda2006-11-23 10:12:50 +0000116 int r, g, b, xwi, ywi;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117
118 // Calculate the changed pixel rect in the scaled image
george824ff66752006-11-20 15:55:05 +0000119 changed_rect = calculateScaleBoundary(rect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000120
george8243d1fa02006-11-26 11:18:38 +0000121 int bytesPerPixel = pf.bpp / 8;
122
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000123 for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) {
george82d5dacda2006-11-23 10:12:50 +0000124 ptr = &(*scaled_data)[(changed_rect.tl.x + y*scaled_width) * 4];
125 yweight = xWeightTabs[y].weight;
126
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127 for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) {
george82d5dacda2006-11-23 10:12:50 +0000128 ywi = 0; red = 0; green = 0; blue = 0;
george82d5dacda2006-11-23 10:12:50 +0000129 xweight = xWeightTabs[x].weight;
130
131 // Calculate the scaled pixel value at (x, y) coordinates by
132 // convolution the matrix from source image:
133 // [(xWeight.i0,yWeight.i0)......(xWeight.i1-1,yWeight.i0)]
134 // [......................................................]
135 // [(xWeight.i0,yWeight.i1-1)..(xWeight.i1-1,yWeight.i1-1)],
136 // where [i0, i1) is the scaled filter interval.
george824ff66752006-11-20 15:55:05 +0000137 for (int ys = yWeightTabs[y].i0; ys < yWeightTabs[y].i1; ys++) {
george821c2e9e62006-12-03 12:46:54 +0000138 xwi = 0; rx = 0; gx = 0; bx = 0;
george824ff66752006-11-20 15:55:05 +0000139 for (int xs = xWeightTabs[x].i0; xs < xWeightTabs[x].i1; xs++) {
george821c2e9e62006-12-03 12:46:54 +0000140 rgbFromPixel(getSourcePixel(xs, ys), r, g, b);
george82d5dacda2006-11-23 10:12:50 +0000141 xWeight = xweight[xwi++];
142 rx += r * xWeight;
143 gx += g * xWeight;
144 bx += b * xWeight;
george824ff66752006-11-20 15:55:05 +0000145 }
george82d5dacda2006-11-23 10:12:50 +0000146 yWeight = yweight[ywi++];
147 red += rx * yWeight;
148 green += gx * yWeight;
149 blue += bx * yWeight;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000150 }
george824ff66752006-11-20 15:55:05 +0000151 *ptr++ = U8(blue);
152 *ptr++ = U8(green);
153 *ptr++ = U8(red);
154 ptr++;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155 }
156 }
157}
158
159Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) {
george824ff66752006-11-20 15:55:05 +0000160 int x_start, y_start, x_end, y_end;
george825e562842006-12-03 17:31:39 +0000161 double radius = scaleFilters[scaleFilterID].radius;
162 double translate = 0.5*scale_ratio - 0.5;
163 x_start = (int)ceil(scale_ratio*(r.tl.x-radius) + translate);
164 y_start = (int)ceil(scale_ratio*(r.tl.y-radius) + translate);
165 x_end = (int)ceil(scale_ratio*(r.br.x+radius) + translate);
166 y_end = (int)ceil(scale_ratio*(r.br.y+radius) + translate);
167 if (x_start < 0) x_start = 0;
168 if (y_start < 0) y_start = 0;
169 if (x_end > scaled_width) x_end = scaled_width;
170 if (y_end > scaled_height) y_end = scaled_height;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 return Rect(x_start, y_start, x_end, y_end);
172}
173
174void ScaledPixelBuffer::calculateScaledBufferSize() {
175 scaled_width = (int)ceil(src_width * scale_ratio);
176 scaled_height = (int)ceil(src_height * scale_ratio);
177}