george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 1 | /* 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 | |
| 26 | using namespace rdr; |
| 27 | using namespace rfb; |
| 28 | |
george82 | 461e4b3 | 2006-02-12 07:58:02 +0000 | [diff] [blame] | 29 | ScaledPixelBuffer::ScaledPixelBuffer(U8 **src_data_, int src_width_, |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 30 | int src_height_, int scale) |
| 31 | : src_data(src_data_), src_width(src_width_), src_height(src_height_), |
george82 | a758843 | 2006-02-24 08:34:01 +0000 | [diff] [blame] | 32 | bpp(32), scaled_data(0), scale_ratio(0) { |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 33 | |
george82 | a758843 | 2006-02-24 08:34:01 +0000 | [diff] [blame] | 34 | setScale(scale); |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 35 | } |
| 36 | |
george82 | 04a91b9 | 2006-02-12 05:59:58 +0000 | [diff] [blame] | 37 | ScaledPixelBuffer::ScaledPixelBuffer() |
george82 | 7127347 | 2006-02-12 06:44:18 +0000 | [diff] [blame] | 38 | : src_data(0), src_width(0), src_height(0), scale_ratio(1), bpp(32), |
| 39 | scaled_data(0) { |
george82 | 04a91b9 | 2006-02-12 05:59:58 +0000 | [diff] [blame] | 40 | } |
| 41 | |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 42 | ScaledPixelBuffer::~ScaledPixelBuffer() { |
george82 | 7127347 | 2006-02-12 06:44:18 +0000 | [diff] [blame] | 43 | if (scaled_data) delete [] scaled_data; |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | const U8* ScaledPixelBuffer::getPixelsR(const Rect& r, int* stride) { |
| 47 | *stride = getStride(); |
george82 | 7127347 | 2006-02-12 06:44:18 +0000 | [diff] [blame] | 48 | return &scaled_data[(r.tl.x + (r.tl.y * *stride)) * bpp/8]; |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void ScaledPixelBuffer::getImage(void* imageBuf, const Rect& r, int outStride) { |
| 52 | int inStride; |
| 53 | const U8* pixels_data = getPixelsR(r, &inStride); |
| 54 | // We assume that the specified rectangle is pre-clipped to the buffer |
| 55 | int bytesPerPixel = bpp/8; |
| 56 | int inBytesPerRow = inStride * bytesPerPixel; |
| 57 | if (!outStride) outStride = r.width(); |
| 58 | int outBytesPerRow = outStride * bytesPerPixel; |
| 59 | int bytesPerMemCpy = r.width() * bytesPerPixel; |
| 60 | U8* imageBufPos = (U8*)imageBuf; |
| 61 | const U8* end = pixels_data + (inBytesPerRow * r.height()); |
| 62 | while (pixels_data < end) { |
| 63 | memcpy(imageBufPos, pixels_data, bytesPerMemCpy); |
| 64 | imageBufPos += outBytesPerRow; |
| 65 | pixels_data += inBytesPerRow; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void ScaledPixelBuffer::setScale(int scale) { |
| 70 | if (scale != scale_ratio * 100) { |
| 71 | scale_ratio = double(scale) / 100; |
| 72 | |
george82 | 01117ec | 2006-02-12 11:03:48 +0000 | [diff] [blame] | 73 | scaled_width = (int)ceil(src_width * scale_ratio); |
| 74 | scaled_height = (int)ceil(src_height * scale_ratio); |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 75 | |
george82 | 97e5c23 | 2006-02-12 11:28:53 +0000 | [diff] [blame] | 76 | recreateScaledBuffer(); |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 77 | |
george82 | 01117ec | 2006-02-12 11:03:48 +0000 | [diff] [blame] | 78 | scaleRect(Rect(0, 0, scaled_width, scaled_height)); |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | void ScaledPixelBuffer::scaleRect(const Rect& r) { |
| 83 | static U8 *src_ptr, *ptr; |
| 84 | static U8 r0, r1, r2, r3; |
| 85 | static U8 g0, g1, g2, g3; |
| 86 | static U8 b0, b1, b2, b3; |
| 87 | static double c1_sub_dx, c1_sub_dy; |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 88 | static double dx, dy; |
| 89 | static int i, j; |
george82 | ba4be91 | 2006-03-27 15:26:11 +0000 | [diff] [blame^] | 90 | static Rect changed_rect; |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 91 | |
george82 | ba4be91 | 2006-03-27 15:26:11 +0000 | [diff] [blame^] | 92 | // Calculate the changed pixel rect in the scaled image |
| 93 | changed_rect = calculateScaleBoundary(r); |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 94 | |
| 95 | // Scale the source rect to the destination image buffer using |
| 96 | // bilinear interplation |
george82 | ba4be91 | 2006-03-27 15:26:11 +0000 | [diff] [blame^] | 97 | for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) { |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 98 | j = (int)(dy = y / scale_ratio); |
| 99 | dy -= j; |
| 100 | c1_sub_dy = 1 - dy; |
| 101 | |
george82 | ba4be91 | 2006-03-27 15:26:11 +0000 | [diff] [blame^] | 102 | for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) { |
george82 | 01117ec | 2006-02-12 11:03:48 +0000 | [diff] [blame] | 103 | ptr = &scaled_data[(x + y*scaled_width) * 4]; |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 104 | |
| 105 | i = (int)(dx = x / scale_ratio); |
| 106 | dx -= i; |
| 107 | c1_sub_dx = 1 - dx; |
| 108 | |
george82 | 461e4b3 | 2006-02-12 07:58:02 +0000 | [diff] [blame] | 109 | src_ptr = &(*src_data)[(i + (j*src_width))*4]; |
george82 | f891ccd | 2005-12-03 07:30:51 +0000 | [diff] [blame] | 110 | b0 = *src_ptr; g0 = *(src_ptr+1); r0 = *(src_ptr+2); |
| 111 | if (i+1 < src_width) { |
| 112 | b1 = *(src_ptr+4); g1 = *(src_ptr+5); r1 = *(src_ptr+6); |
| 113 | } else { |
| 114 | b1 = b0; r1 = r0; g1 = g0; |
| 115 | } |
| 116 | if (j+1 < src_height) { |
| 117 | src_ptr += src_width * 4; |
| 118 | b3 = *src_ptr; g3 = *(src_ptr+1); r3 = *(src_ptr+2); |
| 119 | } else { |
| 120 | b3 = b0; r3 = r0; g3 = g0; |
| 121 | } |
| 122 | if ((i+1 < src_width) && (j+1 < src_height)) { |
| 123 | b2 = *(src_ptr+4); g2 = *(src_ptr+5); r2 = *(src_ptr+6); |
| 124 | } else if (i+1 >= src_width) { |
| 125 | b2 = b3; r2 = r3; g2 = g3; |
| 126 | } else { |
| 127 | b2 = b1; r2 = r1; g2 = g1; |
| 128 | } |
| 129 | *ptr++ = (U8)((b0*c1_sub_dx+b1*dx)*c1_sub_dy + (b3*c1_sub_dx+b2*dx)*dy); |
| 130 | *ptr++ = (U8)((g0*c1_sub_dx+g1*dx)*c1_sub_dy + (g3*c1_sub_dx+g2*dx)*dy); |
| 131 | *ptr = (U8)((r0*c1_sub_dx+r1*dx)*c1_sub_dy + (r3*c1_sub_dx+r2*dx)*dy); |
| 132 | } |
| 133 | } |
| 134 | } |
george82 | 97e5c23 | 2006-02-12 11:28:53 +0000 | [diff] [blame] | 135 | |
george82 | 1d78bd3 | 2006-03-27 15:10:43 +0000 | [diff] [blame] | 136 | Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) { |
| 137 | static int x_start, y_start, x_end, y_end; |
| 138 | x_start = r.tl.x == 0 ? 0 : ceil((r.tl.x-1) * scale_ratio); |
| 139 | y_start = r.tl.y == 0 ? 0 : ceil((r.tl.y-1) * scale_ratio); |
| 140 | x_end = ceil(r.br.x * scale_ratio - 1); |
| 141 | x_end = x_end < scaled_width ? x_end + 1 : scaled_width; |
| 142 | y_end = ceil(r.br.y * scale_ratio - 1); |
| 143 | y_end = y_end < scaled_height ? y_end + 1 : scaled_height; |
| 144 | return Rect(x_start, y_start, x_end, y_end); |
| 145 | } |
| 146 | |
george82 | 97e5c23 | 2006-02-12 11:28:53 +0000 | [diff] [blame] | 147 | void ScaledPixelBuffer::recreateScaledBuffer() { |
| 148 | if (scaled_data) delete [] scaled_data; |
| 149 | scaled_data = new U8[scaled_width * scaled_height * (bpp / 8)]; |
| 150 | } |