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_, |
george82 | 204162c | 2007-10-28 08:35:08 +0000 | [diff] [blame] | 32 | int src_height_, int scale_, PixelFormat pf_) |
| 33 | : scale(scale_), scale_ratio_x(1), scale_ratio_y(1), scaleFilterID(scaleFilterBicubic), |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 34 | xWeightTabs(0), yWeightTabs(0), scaled_data(0), raccum(0), gaccum(0), baccum(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() |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 41 | : src_width(0), src_height(0), scaled_width(0), scaled_height(0), scale(100), |
| 42 | scale_ratio_x(1), scale_ratio_y(1), scaleFilterID(scaleFilterBicubic), |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 43 | xWeightTabs(0), yWeightTabs(0), src_data(0), scaled_data(0), raccum(0), |
| 44 | gaccum(0), baccum(0) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 45 | memset(&pf, 0, sizeof(pf)); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ScaledPixelBuffer::~ScaledPixelBuffer() { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 49 | freeWeightTabs(); |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 50 | if (raccum) delete [] raccum; |
| 51 | if (gaccum) delete [] gaccum; |
| 52 | if (baccum) delete [] baccum; |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void ScaledPixelBuffer::freeWeightTabs() { |
| 56 | if (xWeightTabs) { |
| 57 | for (int i = 0; i < scaled_width; i++) delete [] xWeightTabs[i].weight; |
| 58 | delete [] xWeightTabs; |
| 59 | xWeightTabs = 0; |
| 60 | } |
| 61 | if (yWeightTabs) { |
| 62 | for (int i = 0; i < scaled_height; i++) delete [] yWeightTabs[i].weight; |
| 63 | delete [] yWeightTabs; |
| 64 | yWeightTabs = 0; |
| 65 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 66 | } |
| 67 | |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 68 | void ScaledPixelBuffer::recreateRowAccum() { |
| 69 | if (raccum) delete [] raccum; |
| 70 | if (gaccum) delete [] gaccum; |
| 71 | if (baccum) delete [] baccum; |
| 72 | raccum = new int[src_width]; |
| 73 | gaccum = new int[src_width]; |
| 74 | baccum = new int[src_width]; |
| 75 | } |
| 76 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 77 | void ScaledPixelBuffer::setSourceBuffer(U8 **src_data_, int w, int h) { |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 78 | if (w > 0 && h > 0 && src_data != NULL) { |
| 79 | freeWeightTabs(); |
| 80 | src_data = src_data_; |
| 81 | src_width = w; |
| 82 | src_height = h; |
| 83 | recreateRowAccum(); |
| 84 | calculateScaledBufferSize(); |
| 85 | scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs); |
| 86 | scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs); |
| 87 | } |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void ScaledPixelBuffer::setPF(const PixelFormat &pf_) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 91 | ///if (pf_.depth != 24) throw rfb::UnsupportedPixelFormatException(); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 92 | pf = pf_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 95 | void ScaledPixelBuffer::setScale(int scale_) { |
| 96 | if (scale != scale_ && scale_ > 0) { |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 97 | scale = scale_; |
george82 | c79c7bb | 2007-11-05 11:22:14 +0000 | [diff] [blame^] | 98 | freeWeightTabs(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 99 | calculateScaledBufferSize(); |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 100 | scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs); |
| 101 | scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
george82 | c4eb626 | 2007-03-20 10:54:38 +0000 | [diff] [blame] | 105 | void ScaledPixelBuffer::setScaleFilter(unsigned int scaleFilterID_) { |
| 106 | if (scaleFilterID == scaleFilterID_ || scaleFilterID_ > scaleFilterMaxNumber) return; |
| 107 | |
| 108 | scaleFilterID = scaleFilterID_; |
| 109 | |
george82 | bfd8ecd | 2007-04-30 13:28:54 +0000 | [diff] [blame] | 110 | if (src_width && src_height && scaled_width && scaled_height) { |
george82 | c4eb626 | 2007-03-20 10:54:38 +0000 | [diff] [blame] | 111 | freeWeightTabs(); |
| 112 | scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs); |
| 113 | scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs); |
george82 | bfd8ecd | 2007-04-30 13:28:54 +0000 | [diff] [blame] | 114 | if (scale != 100 && pf.depth > 0) scaleRect(Rect(0, 0, src_width, src_height)); |
george82 | c4eb626 | 2007-03-20 10:54:38 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 118 | inline void ScaledPixelBuffer::rgbFromPixel(U32 p, int &r, int &g, int &b) { |
| 119 | r = (((p >> pf.redShift ) & pf.redMax ) * 255 + pf.redMax /2) / pf.redMax; |
| 120 | g = (((p >> pf.greenShift) & pf.greenMax) * 255 + pf.greenMax/2) / pf.greenMax; |
| 121 | b = (((p >> pf.blueShift ) & pf.blueMax ) * 255 + pf.blueMax /2) / pf.blueMax; |
| 122 | } |
| 123 | |
| 124 | inline U32 ScaledPixelBuffer::getSourcePixel(int x, int y) { |
| 125 | int bytes_per_pixel = pf.bpp / 8; |
| 126 | U8 *ptr = &(*src_data)[(x + y*src_width)*bytes_per_pixel]; |
| 127 | if (bytes_per_pixel == 1) { |
| 128 | return *ptr; |
| 129 | } else if (bytes_per_pixel == 2) { |
| 130 | int b0 = *ptr++; int b1 = *ptr; |
| 131 | return b1 << 8 | b0; |
| 132 | } else if (bytes_per_pixel == 4) { |
| 133 | int b0 = *ptr++; int b1 = *ptr++; |
| 134 | int b2 = *ptr++; int b3 = *ptr; |
| 135 | return b3 << 24 | b2 << 16 | b1 << 8 | b0; |
| 136 | } else { |
| 137 | return 0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void ScaledPixelBuffer::scaleRect(const Rect& rect) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 142 | Rect changed_rect; |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 143 | U8 *ptr, *ptrs, *px, *pxs; |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 144 | int r, g, b, red, green, blue; |
| 145 | short *xweight, *yweight, weight; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 146 | |
| 147 | // Calculate the changed pixel rect in the scaled image |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 148 | changed_rect = calculateScaleBoundary(rect); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 149 | |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 150 | int bytesPerSrcPixel = pf.bpp / 8; |
| 151 | int bytesPerSrcRow = src_width * bytesPerSrcPixel; |
| 152 | int bytesPerScaledRow = scaled_width * 4; |
george82 | 43d1fa0 | 2006-11-26 11:18:38 +0000 | [diff] [blame] | 153 | |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 154 | int bytesPerAccumRow = src_width * sizeof(int); |
| 155 | |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 156 | 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] | 157 | for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) { |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 158 | ptr = ptrs; |
george82 | b09756b | 2006-12-04 15:49:41 +0000 | [diff] [blame] | 159 | yweight = yWeightTabs[y].weight; |
george82 | d5dacda | 2006-11-23 10:12:50 +0000 | [diff] [blame] | 160 | |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 161 | // Clear the color accumulators |
| 162 | memset(raccum, 0, bytesPerAccumRow); |
| 163 | memset(gaccum, 0, bytesPerAccumRow); |
| 164 | memset(baccum, 0, bytesPerAccumRow); |
| 165 | |
| 166 | // Make the convolution the source image with scale filter weights |
| 167 | // by y axis and save results to the color accumulators. |
| 168 | pxs = &(*src_data)[(xWeightTabs[changed_rect.tl.x].i0 + yWeightTabs[y].i0*src_width) * bytesPerSrcPixel]; |
| 169 | for (int ys = yWeightTabs[y].i0; ys < yWeightTabs[y].i1; ys++) { |
| 170 | px = pxs; |
| 171 | for (int xs = xWeightTabs[changed_rect.tl.x].i0; xs < xWeightTabs[changed_rect.br.x-1].i1; xs++) { |
| 172 | rgbFromPixel(*((U32*)px), r, g, b); |
| 173 | weight = *yweight; |
| 174 | raccum[xs] += (int)(weight) * r; |
| 175 | gaccum[xs] += (int)(weight) * g; |
| 176 | baccum[xs] += (int)(weight) * b; |
| 177 | px += bytesPerSrcPixel; |
| 178 | } |
| 179 | yweight++; |
| 180 | pxs += bytesPerSrcRow; |
| 181 | } |
| 182 | |
| 183 | // Make the convolution the color accumulators with scale filter weights |
| 184 | // by x axis and save results to the scaled image. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 185 | for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) { |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 186 | // Init the sum of colors with (1 << (shift-1)) for rounding. |
| 187 | red = green = blue = 1 << (FINALSHIFT-1); |
george82 | d5dacda | 2006-11-23 10:12:50 +0000 | [diff] [blame] | 188 | xweight = xWeightTabs[x].weight; |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 189 | for (int xs = xWeightTabs[x].i0; xs < xWeightTabs[x].i1; xs++) { |
| 190 | weight = *xweight; |
| 191 | red += (int)(weight) * (raccum[xs] >> BITS_OF_CHANEL); |
| 192 | green += (int)(weight) * (gaccum[xs] >> BITS_OF_CHANEL); |
| 193 | blue += (int)(weight) * (baccum[xs] >> BITS_OF_CHANEL); |
| 194 | xweight++; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 195 | } |
george82 | 7812266 | 2007-10-28 10:32:01 +0000 | [diff] [blame] | 196 | *ptr++ = U8(blue >> FINALSHIFT); |
| 197 | *ptr++ = U8(green >> FINALSHIFT); |
| 198 | *ptr++ = U8(red >> FINALSHIFT); |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 199 | ptr++; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 200 | } |
george82 | e7e0ce2 | 2006-12-04 16:35:56 +0000 | [diff] [blame] | 201 | ptrs += bytesPerScaledRow; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) { |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 206 | int x_start, y_start, x_end, y_end; |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 207 | double translate_x = 0.5*scale_ratio_x - 0.5; |
| 208 | double translate_y = 0.5*scale_ratio_y - 0.5; |
george82 | c79c7bb | 2007-11-05 11:22:14 +0000 | [diff] [blame^] | 209 | double sourceXScale = __rfbmax(1.0, 1.0/scale_ratio_x); |
| 210 | double sourceYScale = __rfbmax(1.0, 1.0/scale_ratio_y); |
| 211 | double sourceXRadius = __rfbmax(0.5, sourceXScale*scaleFilters[scaleFilterID].radius); |
| 212 | double sourceYRadius = __rfbmax(0.5, sourceYScale*scaleFilters[scaleFilterID].radius); |
| 213 | x_start = (int)ceil(scale_ratio_x*(r.tl.x-sourceXRadius) + translate_x + SCALE_ERROR); |
| 214 | y_start = (int)ceil(scale_ratio_y*(r.tl.y-sourceYRadius) + translate_y + SCALE_ERROR); |
| 215 | x_end = (int)floor(scale_ratio_x*((r.br.x-1)+sourceXRadius) + translate_x - SCALE_ERROR) + 1; |
| 216 | y_end = (int)floor(scale_ratio_y*((r.br.y-1)+sourceXRadius) + translate_y - SCALE_ERROR) + 1; |
george82 | 5e56284 | 2006-12-03 17:31:39 +0000 | [diff] [blame] | 217 | if (x_start < 0) x_start = 0; |
| 218 | if (y_start < 0) y_start = 0; |
| 219 | if (x_end > scaled_width) x_end = scaled_width; |
| 220 | if (y_end > scaled_height) y_end = scaled_height; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 221 | return Rect(x_start, y_start, x_end, y_end); |
| 222 | } |
| 223 | |
| 224 | void ScaledPixelBuffer::calculateScaledBufferSize() { |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 225 | double scale_ratio = (double)scale / 100; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 226 | scaled_width = (int)ceil(src_width * scale_ratio); |
| 227 | scaled_height = (int)ceil(src_height * scale_ratio); |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 228 | scale_ratio_x = (double)scaled_width / src_width; |
| 229 | scale_ratio_y = (double)scaled_height / src_height; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 230 | } |