Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +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 | |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 21 | #include <rfb/Exception.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 22 | #include <rfb/ScaledPixelBuffer.h> |
| 23 | |
| 24 | #include <math.h> |
| 25 | #include <memory.h> |
| 26 | |
| 27 | using namespace rdr; |
| 28 | using namespace rfb; |
| 29 | |
| 30 | ScaledPixelBuffer::ScaledPixelBuffer(U8 **src_data_, int src_width_, |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 31 | int src_height_, int scale, PixelFormat pf_) |
george82 | d4344be | 2006-07-29 10:27:19 +0000 | [diff] [blame] | 32 | : scaled_data(0), scale_ratio(1) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 33 | |
| 34 | setSourceBuffer(src_data_, src_width_, src_height_); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 35 | setPF(pf_); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | ScaledPixelBuffer::ScaledPixelBuffer() |
george82 | f80971b | 2006-08-17 09:57:43 +0000 | [diff] [blame] | 39 | : src_data(0), src_width(0), src_height(0), scale_ratio(1), scaled_width(0), |
| 40 | scaled_height(0), pf(PixelFormat(32,24,0,1,255,255,255,0,8,16)), |
| 41 | scaled_data(0) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | ScaledPixelBuffer::~ScaledPixelBuffer() { |
| 45 | } |
| 46 | |
| 47 | void ScaledPixelBuffer::setSourceBuffer(U8 **src_data_, int w, int h) { |
| 48 | src_data = src_data_; |
| 49 | src_width = w; |
| 50 | src_height = h; |
| 51 | calculateScaledBufferSize(); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void ScaledPixelBuffer::setPF(const PixelFormat &pf_) { |
george82 | f98083a | 2006-05-29 13:52:55 +0000 | [diff] [blame] | 55 | if (pf_.depth != 24) throw rfb::UnsupportedPixelFormatException(); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 56 | pf = pf_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 57 | } |
| 58 | |
george82 | d4344be | 2006-07-29 10:27:19 +0000 | [diff] [blame] | 59 | void ScaledPixelBuffer::setScaleRatio(double scale_ratio_) { |
| 60 | if (scale_ratio != scale_ratio_) { |
| 61 | scale_ratio = scale_ratio_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 62 | calculateScaledBufferSize(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | void ScaledPixelBuffer::scaleRect(const Rect& r) { |
| 67 | static U8 *src_ptr, *ptr; |
| 68 | static U8 r0, r1, r2, r3; |
| 69 | static U8 g0, g1, g2, g3; |
| 70 | static U8 b0, b1, b2, b3; |
| 71 | static double c1_sub_dx, c1_sub_dy; |
| 72 | static double dx, dy; |
| 73 | static int i, j; |
| 74 | static Rect changed_rect; |
| 75 | |
| 76 | // Calculate the changed pixel rect in the scaled image |
| 77 | changed_rect = calculateScaleBoundary(r); |
| 78 | |
| 79 | // Scale the source rect to the destination image buffer using |
| 80 | // bilinear interplation |
| 81 | for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) { |
| 82 | j = (int)(dy = y / scale_ratio); |
| 83 | dy -= j; |
| 84 | c1_sub_dy = 1 - dy; |
| 85 | |
| 86 | for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) { |
george82 | df9f31c | 2006-09-16 11:06:07 +0000 | [diff] [blame^] | 87 | ptr = &(*scaled_data)[(x + y*scaled_width) * 4]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 88 | |
| 89 | i = (int)(dx = x / scale_ratio); |
| 90 | dx -= i; |
| 91 | c1_sub_dx = 1 - dx; |
| 92 | |
| 93 | src_ptr = &(*src_data)[(i + (j*src_width))*4]; |
| 94 | b0 = *src_ptr; g0 = *(src_ptr+1); r0 = *(src_ptr+2); |
| 95 | if (i+1 < src_width) { |
| 96 | b1 = *(src_ptr+4); g1 = *(src_ptr+5); r1 = *(src_ptr+6); |
| 97 | } else { |
| 98 | b1 = b0; r1 = r0; g1 = g0; |
| 99 | } |
| 100 | if (j+1 < src_height) { |
| 101 | src_ptr += src_width * 4; |
| 102 | b3 = *src_ptr; g3 = *(src_ptr+1); r3 = *(src_ptr+2); |
| 103 | } else { |
| 104 | b3 = b0; r3 = r0; g3 = g0; |
| 105 | } |
| 106 | if ((i+1 < src_width) && (j+1 < src_height)) { |
| 107 | b2 = *(src_ptr+4); g2 = *(src_ptr+5); r2 = *(src_ptr+6); |
| 108 | } else if (i+1 >= src_width) { |
| 109 | b2 = b3; r2 = r3; g2 = g3; |
| 110 | } else { |
| 111 | b2 = b1; r2 = r1; g2 = g1; |
| 112 | } |
| 113 | *ptr++ = (U8)((b0*c1_sub_dx+b1*dx)*c1_sub_dy + (b3*c1_sub_dx+b2*dx)*dy); |
| 114 | *ptr++ = (U8)((g0*c1_sub_dx+g1*dx)*c1_sub_dy + (g3*c1_sub_dx+g2*dx)*dy); |
| 115 | *ptr = (U8)((r0*c1_sub_dx+r1*dx)*c1_sub_dy + (r3*c1_sub_dx+r2*dx)*dy); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) { |
| 121 | static int x_start, y_start, x_end, y_end; |
Constantin Kaplinsky | 660fa18 | 2006-09-08 13:52:23 +0000 | [diff] [blame] | 122 | x_start = r.tl.x == 0 ? 0 : int(ceil((r.tl.x-1) * scale_ratio)); |
| 123 | y_start = r.tl.y == 0 ? 0 : int(ceil((r.tl.y-1) * scale_ratio)); |
| 124 | x_end = int(ceil(r.br.x * scale_ratio - 1)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 125 | x_end = x_end < scaled_width ? x_end + 1 : scaled_width; |
Constantin Kaplinsky | 660fa18 | 2006-09-08 13:52:23 +0000 | [diff] [blame] | 126 | y_end = int(ceil(r.br.y * scale_ratio - 1)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 127 | y_end = y_end < scaled_height ? y_end + 1 : scaled_height; |
| 128 | return Rect(x_start, y_start, x_end, y_end); |
| 129 | } |
| 130 | |
| 131 | void ScaledPixelBuffer::calculateScaledBufferSize() { |
| 132 | scaled_width = (int)ceil(src_width * scale_ratio); |
| 133 | scaled_height = (int)ceil(src_height * scale_ratio); |
| 134 | } |