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> |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace rdr; |
| 29 | using namespace rfb; |
| 30 | |
| 31 | ScaledPixelBuffer::ScaledPixelBuffer(U8 **src_data_, int src_width_, |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 32 | int src_height_, int scale, PixelFormat pf_) |
Constantin Kaplinsky | 8280026 | 2006-12-05 03:31:03 +0000 | [diff] [blame^] | 33 | : scale_ratio(1), scaleFilterID(scaleFilterBicubic), |
| 34 | xWeightTabs(0), yWeightTabs(0), scaled_data(0) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 35 | |
| 36 | setSourceBuffer(src_data_, src_width_, src_height_); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 37 | setPF(pf_); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | ScaledPixelBuffer::ScaledPixelBuffer() |
Constantin Kaplinsky | 8280026 | 2006-12-05 03:31:03 +0000 | [diff] [blame^] | 41 | : src_width(0), src_height(0), scaled_width(0), scaled_height(0), |
| 42 | scale_ratio(1), scaleFilterID(scaleFilterBicubic), |
| 43 | xWeightTabs(0), yWeightTabs(0), src_data(0), scaled_data(0) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 44 | memset(&pf, 0, sizeof(pf)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | ScaledPixelBuffer::~ScaledPixelBuffer() { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 48 | freeWeightTabs(); |
| 49 | } |
| 50 | |
| 51 | void 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void ScaledPixelBuffer::setSourceBuffer(U8 **src_data_, int w, int h) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 65 | freeWeightTabs(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 66 | src_data = src_data_; |
| 67 | src_width = w; |
| 68 | src_height = h; |
| 69 | calculateScaledBufferSize(); |
george82 | 1c2e9e6 | 2006-12-03 12:46:54 +0000 | [diff] [blame] | 70 | scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, scale_ratio, &xWeightTabs); |
| 71 | scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, scale_ratio, &yWeightTabs); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void ScaledPixelBuffer::setPF(const PixelFormat &pf_) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 75 | ///if (pf_.depth != 24) throw rfb::UnsupportedPixelFormatException(); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 76 | pf = pf_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
george82 | d4344be | 2006-07-29 10:27:19 +0000 | [diff] [blame] | 79 | void ScaledPixelBuffer::setScaleRatio(double scale_ratio_) { |
| 80 | if (scale_ratio != scale_ratio_) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 81 | freeWeightTabs(); |
george82 | d4344be | 2006-07-29 10:27:19 +0000 | [diff] [blame] | 82 | scale_ratio = scale_ratio_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 83 | calculateScaledBufferSize(); |
george82 | 1c2e9e6 | 2006-12-03 12:46:54 +0000 | [diff] [blame] | 84 | scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, scale_ratio, &xWeightTabs); |
| 85 | scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, scale_ratio, &yWeightTabs); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 89 | inline 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 | |
| 95 | inline 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 | |
| 112 | void ScaledPixelBuffer::scaleRect(const Rect& rect) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 113 | Rect changed_rect; |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 114 | U8 *ptr, *ptrs, *px, *pxs; |
george82 | 1c2e9e6 | 2006-12-03 12:46:54 +0000 | [diff] [blame] | 115 | double rx, gx, bx, red, green, blue, *xweight, *yweight, xWeight, yWeight; |
george82 | d5dacda | 2006-11-23 10:12:50 +0000 | [diff] [blame] | 116 | int r, g, b, xwi, ywi; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 117 | |
| 118 | // Calculate the changed pixel rect in the scaled image |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 119 | changed_rect = calculateScaleBoundary(rect); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 120 | |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 121 | int bytesPerSrcPixel = pf.bpp / 8; |
| 122 | int bytesPerSrcRow = src_width * bytesPerSrcPixel; |
| 123 | int bytesPerScaledRow = scaled_width * 4; |
george82 | 43d1fa0 | 2006-11-26 11:18:38 +0000 | [diff] [blame] | 124 | |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 125 | ptrs = &(*scaled_data)[(changed_rect.tl.x + changed_rect.tl.y*scaled_width) * 4]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 126 | for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) { |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 127 | ptr = ptrs; |
george82 | b09756b | 2006-12-04 15:49:41 +0000 | [diff] [blame] | 128 | yweight = yWeightTabs[y].weight; |
george82 | d5dacda | 2006-11-23 10:12:50 +0000 | [diff] [blame] | 129 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 130 | for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) { |
george82 | d5dacda | 2006-11-23 10:12:50 +0000 | [diff] [blame] | 131 | ywi = 0; red = 0; green = 0; blue = 0; |
george82 | d5dacda | 2006-11-23 10:12:50 +0000 | [diff] [blame] | 132 | xweight = xWeightTabs[x].weight; |
| 133 | |
| 134 | // Calculate the scaled pixel value at (x, y) coordinates by |
| 135 | // convolution the matrix from source image: |
| 136 | // [(xWeight.i0,yWeight.i0)......(xWeight.i1-1,yWeight.i0)] |
| 137 | // [......................................................] |
| 138 | // [(xWeight.i0,yWeight.i1-1)..(xWeight.i1-1,yWeight.i1-1)], |
| 139 | // where [i0, i1) is the scaled filter interval. |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 140 | pxs = &(*src_data)[(xWeightTabs[x].i0 + yWeightTabs[y].i0*src_width) * bytesPerSrcPixel]; |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 141 | for (int ys = yWeightTabs[y].i0; ys < yWeightTabs[y].i1; ys++) { |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 142 | xwi = 0; rx = 0; gx = 0; bx = 0; px = pxs; |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 143 | for (int xs = xWeightTabs[x].i0; xs < xWeightTabs[x].i1; xs++) { |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 144 | rgbFromPixel(*((U32*)px), r, g, b); |
george82 | d5dacda | 2006-11-23 10:12:50 +0000 | [diff] [blame] | 145 | xWeight = xweight[xwi++]; |
| 146 | rx += r * xWeight; |
| 147 | gx += g * xWeight; |
| 148 | bx += b * xWeight; |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 149 | px += bytesPerSrcPixel; |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 150 | } |
george82 | d5dacda | 2006-11-23 10:12:50 +0000 | [diff] [blame] | 151 | yWeight = yweight[ywi++]; |
| 152 | red += rx * yWeight; |
| 153 | green += gx * yWeight; |
| 154 | blue += bx * yWeight; |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 155 | pxs += bytesPerSrcRow; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 156 | } |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 157 | *ptr++ = U8(blue); |
| 158 | *ptr++ = U8(green); |
| 159 | *ptr++ = U8(red); |
| 160 | ptr++; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 161 | } |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 162 | ptrs += bytesPerScaledRow; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 167 | int x_start, y_start, x_end, y_end; |
george82 | 5e56284 | 2006-12-03 17:31:39 +0000 | [diff] [blame] | 168 | double radius = scaleFilters[scaleFilterID].radius; |
| 169 | double translate = 0.5*scale_ratio - 0.5; |
| 170 | x_start = (int)ceil(scale_ratio*(r.tl.x-radius) + translate); |
| 171 | y_start = (int)ceil(scale_ratio*(r.tl.y-radius) + translate); |
| 172 | x_end = (int)ceil(scale_ratio*(r.br.x+radius) + translate); |
| 173 | y_end = (int)ceil(scale_ratio*(r.br.y+radius) + translate); |
| 174 | if (x_start < 0) x_start = 0; |
| 175 | if (y_start < 0) y_start = 0; |
| 176 | if (x_end > scaled_width) x_end = scaled_width; |
| 177 | if (y_end > scaled_height) y_end = scaled_height; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 178 | return Rect(x_start, y_start, x_end, y_end); |
| 179 | } |
| 180 | |
| 181 | void ScaledPixelBuffer::calculateScaledBufferSize() { |
| 182 | scaled_width = (int)ceil(src_width * scale_ratio); |
| 183 | scaled_height = (int)ceil(src_height * scale_ratio); |
| 184 | } |