blob: bf4612deb18ed0155b40bd530f237e62d4ee392d [file] [log] [blame]
george82f891ccd2005-12-03 07:30:51 +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
21#include <rfb/ScaledPixelBuffer.h>
22
23#include <math.h>
24#include <memory.h>
25
26using namespace rdr;
27using namespace rfb;
28
george82461e4b32006-02-12 07:58:02 +000029ScaledPixelBuffer::ScaledPixelBuffer(U8 **src_data_, int src_width_,
george82f891ccd2005-12-03 07:30:51 +000030 int src_height_, int scale)
george82c5b39732006-04-05 15:25:14 +000031 : bpp(32), scaled_data(0), scale_ratio(1), scale(100) {
george82f891ccd2005-12-03 07:30:51 +000032
george82c5b39732006-04-05 15:25:14 +000033 setSourceBuffer(src_data_, src_width_, src_height_);
george82f891ccd2005-12-03 07:30:51 +000034}
35
george8204a91b92006-02-12 05:59:58 +000036ScaledPixelBuffer::ScaledPixelBuffer()
george826b974db2006-04-05 15:02:36 +000037 : src_data(0), src_width(0), src_height(0), scale_ratio(1), scale(100),
38 bpp(32), scaled_data(0) {
george8204a91b92006-02-12 05:59:58 +000039}
40
george82f891ccd2005-12-03 07:30:51 +000041ScaledPixelBuffer::~ScaledPixelBuffer() {
george82f891ccd2005-12-03 07:30:51 +000042}
43
george82c5b39732006-04-05 15:25:14 +000044void ScaledPixelBuffer::setSourceBuffer(U8 **src_data_, int w, int h) {
45 src_data = src_data_;
46 src_width = w;
47 src_height = h;
george82fea770d2006-04-05 15:33:50 +000048 calculateScaledBufferSize();
george82c5b39732006-04-05 15:25:14 +000049 recreateScaledBuffer();
50}
51
george826b974db2006-04-05 15:02:36 +000052void ScaledPixelBuffer::setScale(int scale_) {
53 if (scale != scale_) {
54 scale = scale_;
george82f891ccd2005-12-03 07:30:51 +000055 scale_ratio = double(scale) / 100;
george82fea770d2006-04-05 15:33:50 +000056 calculateScaledBufferSize();
george8297e5c232006-02-12 11:28:53 +000057 recreateScaledBuffer();
george82f891ccd2005-12-03 07:30:51 +000058 }
59}
60
61void ScaledPixelBuffer::scaleRect(const Rect& r) {
62 static U8 *src_ptr, *ptr;
63 static U8 r0, r1, r2, r3;
64 static U8 g0, g1, g2, g3;
65 static U8 b0, b1, b2, b3;
66 static double c1_sub_dx, c1_sub_dy;
george82f891ccd2005-12-03 07:30:51 +000067 static double dx, dy;
68 static int i, j;
george82ba4be912006-03-27 15:26:11 +000069 static Rect changed_rect;
george82f891ccd2005-12-03 07:30:51 +000070
george82ba4be912006-03-27 15:26:11 +000071 // Calculate the changed pixel rect in the scaled image
72 changed_rect = calculateScaleBoundary(r);
george82f891ccd2005-12-03 07:30:51 +000073
74 // Scale the source rect to the destination image buffer using
75 // bilinear interplation
george82ba4be912006-03-27 15:26:11 +000076 for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) {
george82f891ccd2005-12-03 07:30:51 +000077 j = (int)(dy = y / scale_ratio);
78 dy -= j;
79 c1_sub_dy = 1 - dy;
80
george82ba4be912006-03-27 15:26:11 +000081 for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) {
george8201117ec2006-02-12 11:03:48 +000082 ptr = &scaled_data[(x + y*scaled_width) * 4];
george82f891ccd2005-12-03 07:30:51 +000083
84 i = (int)(dx = x / scale_ratio);
85 dx -= i;
86 c1_sub_dx = 1 - dx;
87
george82461e4b32006-02-12 07:58:02 +000088 src_ptr = &(*src_data)[(i + (j*src_width))*4];
george82f891ccd2005-12-03 07:30:51 +000089 b0 = *src_ptr; g0 = *(src_ptr+1); r0 = *(src_ptr+2);
90 if (i+1 < src_width) {
91 b1 = *(src_ptr+4); g1 = *(src_ptr+5); r1 = *(src_ptr+6);
92 } else {
93 b1 = b0; r1 = r0; g1 = g0;
94 }
95 if (j+1 < src_height) {
96 src_ptr += src_width * 4;
97 b3 = *src_ptr; g3 = *(src_ptr+1); r3 = *(src_ptr+2);
98 } else {
99 b3 = b0; r3 = r0; g3 = g0;
100 }
101 if ((i+1 < src_width) && (j+1 < src_height)) {
102 b2 = *(src_ptr+4); g2 = *(src_ptr+5); r2 = *(src_ptr+6);
103 } else if (i+1 >= src_width) {
104 b2 = b3; r2 = r3; g2 = g3;
105 } else {
106 b2 = b1; r2 = r1; g2 = g1;
107 }
108 *ptr++ = (U8)((b0*c1_sub_dx+b1*dx)*c1_sub_dy + (b3*c1_sub_dx+b2*dx)*dy);
109 *ptr++ = (U8)((g0*c1_sub_dx+g1*dx)*c1_sub_dy + (g3*c1_sub_dx+g2*dx)*dy);
110 *ptr = (U8)((r0*c1_sub_dx+r1*dx)*c1_sub_dy + (r3*c1_sub_dx+r2*dx)*dy);
111 }
112 }
113}
george8297e5c232006-02-12 11:28:53 +0000114
george821d78bd32006-03-27 15:10:43 +0000115Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) {
116 static int x_start, y_start, x_end, y_end;
117 x_start = r.tl.x == 0 ? 0 : ceil((r.tl.x-1) * scale_ratio);
118 y_start = r.tl.y == 0 ? 0 : ceil((r.tl.y-1) * scale_ratio);
119 x_end = ceil(r.br.x * scale_ratio - 1);
120 x_end = x_end < scaled_width ? x_end + 1 : scaled_width;
121 y_end = ceil(r.br.y * scale_ratio - 1);
122 y_end = y_end < scaled_height ? y_end + 1 : scaled_height;
123 return Rect(x_start, y_start, x_end, y_end);
124}
125
george82fea770d2006-04-05 15:33:50 +0000126void ScaledPixelBuffer::calculateScaledBufferSize() {
127 scaled_width = (int)ceil(src_width * scale_ratio);
128 scaled_height = (int)ceil(src_height * scale_ratio);
129}
130
george8297e5c232006-02-12 11:28:53 +0000131void ScaledPixelBuffer::recreateScaledBuffer() {
george8297e5c232006-02-12 11:28:53 +0000132}