blob: 962a54be2a2d122c456a500880ae669d70de9eb9 [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)
31 : src_data(src_data_), src_width(src_width_), src_height(src_height_),
george8271273472006-02-12 06:44:18 +000032 bpp(32), scaled_data(0) {
george82f891ccd2005-12-03 07:30:51 +000033
34 scale_ratio = double(scale) / 100;
35
george8201117ec2006-02-12 11:03:48 +000036 scaled_width = (int)ceil(src_width * scale_ratio);
37 scaled_height = (int)ceil(src_height * scale_ratio);
george82f891ccd2005-12-03 07:30:51 +000038
george8297e5c232006-02-12 11:28:53 +000039 recreateScaledBuffer();
george82f891ccd2005-12-03 07:30:51 +000040}
41
george8204a91b92006-02-12 05:59:58 +000042ScaledPixelBuffer::ScaledPixelBuffer()
george8271273472006-02-12 06:44:18 +000043 : src_data(0), src_width(0), src_height(0), scale_ratio(1), bpp(32),
44 scaled_data(0) {
george8204a91b92006-02-12 05:59:58 +000045}
46
george82f891ccd2005-12-03 07:30:51 +000047ScaledPixelBuffer::~ScaledPixelBuffer() {
george8271273472006-02-12 06:44:18 +000048 if (scaled_data) delete [] scaled_data;
george82f891ccd2005-12-03 07:30:51 +000049}
50
51const U8* ScaledPixelBuffer::getPixelsR(const Rect& r, int* stride) {
52 *stride = getStride();
george8271273472006-02-12 06:44:18 +000053 return &scaled_data[(r.tl.x + (r.tl.y * *stride)) * bpp/8];
george82f891ccd2005-12-03 07:30:51 +000054}
55
56void ScaledPixelBuffer::getImage(void* imageBuf, const Rect& r, int outStride) {
57 int inStride;
58 const U8* pixels_data = getPixelsR(r, &inStride);
59 // We assume that the specified rectangle is pre-clipped to the buffer
60 int bytesPerPixel = bpp/8;
61 int inBytesPerRow = inStride * bytesPerPixel;
62 if (!outStride) outStride = r.width();
63 int outBytesPerRow = outStride * bytesPerPixel;
64 int bytesPerMemCpy = r.width() * bytesPerPixel;
65 U8* imageBufPos = (U8*)imageBuf;
66 const U8* end = pixels_data + (inBytesPerRow * r.height());
67 while (pixels_data < end) {
68 memcpy(imageBufPos, pixels_data, bytesPerMemCpy);
69 imageBufPos += outBytesPerRow;
70 pixels_data += inBytesPerRow;
71 }
72}
73
74void ScaledPixelBuffer::setScale(int scale) {
75 if (scale != scale_ratio * 100) {
76 scale_ratio = double(scale) / 100;
77
george8201117ec2006-02-12 11:03:48 +000078 scaled_width = (int)ceil(src_width * scale_ratio);
79 scaled_height = (int)ceil(src_height * scale_ratio);
george82f891ccd2005-12-03 07:30:51 +000080
george8297e5c232006-02-12 11:28:53 +000081 recreateScaledBuffer();
george82f891ccd2005-12-03 07:30:51 +000082
george8201117ec2006-02-12 11:03:48 +000083 scaleRect(Rect(0, 0, scaled_width, scaled_height));
george82f891ccd2005-12-03 07:30:51 +000084 }
85}
86
87void ScaledPixelBuffer::scaleRect(const Rect& r) {
88 static U8 *src_ptr, *ptr;
89 static U8 r0, r1, r2, r3;
90 static U8 g0, g1, g2, g3;
91 static U8 b0, b1, b2, b3;
92 static double c1_sub_dx, c1_sub_dy;
93 static double x_start, x_end, y_start, y_end;
94 static double dx, dy;
95 static int i, j;
96
97 // Calculate the scale boundaries
98 x_start = vncmax(0, (r.tl.x-1) * scale_ratio);
99 (x_start==int(x_start)) ? true : x_start=(int)(x_start+1);
george8201117ec2006-02-12 11:03:48 +0000100 x_end = vncmin(scaled_width - 1, r.br.x * scale_ratio);
101 ((x_end==int(x_end))&&(x_end!=scaled_width-1)&&(x_end>0)) ? x_end-=1:x_end=(int)(x_end);
george82f891ccd2005-12-03 07:30:51 +0000102 y_start = vncmax(0, (r.tl.y-1) * scale_ratio);
103 (y_start==int(y_start)) ? true : y_start=(int)(y_start+1);
george8201117ec2006-02-12 11:03:48 +0000104 y_end = vncmin(scaled_height - 1, r.br.y * scale_ratio);
105 ((y_end==int(y_end))&&(y_end!=scaled_height-1)&&(y_end>0)) ? y_end-=1:y_end=(int)(y_end);
george82f891ccd2005-12-03 07:30:51 +0000106
107 // Scale the source rect to the destination image buffer using
108 // bilinear interplation
109 for (int y = (int)y_start; y <= y_end; y++) {
110 j = (int)(dy = y / scale_ratio);
111 dy -= j;
112 c1_sub_dy = 1 - dy;
113
114 for (int x = (int)x_start; x <= x_end; x++) {
george8201117ec2006-02-12 11:03:48 +0000115 ptr = &scaled_data[(x + y*scaled_width) * 4];
george82f891ccd2005-12-03 07:30:51 +0000116
117 i = (int)(dx = x / scale_ratio);
118 dx -= i;
119 c1_sub_dx = 1 - dx;
120
george82461e4b32006-02-12 07:58:02 +0000121 src_ptr = &(*src_data)[(i + (j*src_width))*4];
george82f891ccd2005-12-03 07:30:51 +0000122 b0 = *src_ptr; g0 = *(src_ptr+1); r0 = *(src_ptr+2);
123 if (i+1 < src_width) {
124 b1 = *(src_ptr+4); g1 = *(src_ptr+5); r1 = *(src_ptr+6);
125 } else {
126 b1 = b0; r1 = r0; g1 = g0;
127 }
128 if (j+1 < src_height) {
129 src_ptr += src_width * 4;
130 b3 = *src_ptr; g3 = *(src_ptr+1); r3 = *(src_ptr+2);
131 } else {
132 b3 = b0; r3 = r0; g3 = g0;
133 }
134 if ((i+1 < src_width) && (j+1 < src_height)) {
135 b2 = *(src_ptr+4); g2 = *(src_ptr+5); r2 = *(src_ptr+6);
136 } else if (i+1 >= src_width) {
137 b2 = b3; r2 = r3; g2 = g3;
138 } else {
139 b2 = b1; r2 = r1; g2 = g1;
140 }
141 *ptr++ = (U8)((b0*c1_sub_dx+b1*dx)*c1_sub_dy + (b3*c1_sub_dx+b2*dx)*dy);
142 *ptr++ = (U8)((g0*c1_sub_dx+g1*dx)*c1_sub_dy + (g3*c1_sub_dx+g2*dx)*dy);
143 *ptr = (U8)((r0*c1_sub_dx+r1*dx)*c1_sub_dy + (r3*c1_sub_dx+r2*dx)*dy);
144 }
145 }
146}
george8297e5c232006-02-12 11:28:53 +0000147
148void ScaledPixelBuffer::recreateScaledBuffer() {
149 if (scaled_data) delete [] scaled_data;
150 scaled_data = new U8[scaled_width * scaled_height * (bpp / 8)];
151}