summaryrefslogtreecommitdiffstats
path: root/common/rfb/ScaledPixelBuffer.cxx
blob: 35d5d3b161046fc38010385fcfee13c000056609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Copyright (C) 2005 TightVNC Team.  All Rights Reserved.
 *    
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- ScaledPixelBuffer.cxx

#include <rfb/Exception.h>
#include <rfb/ScaledPixelBuffer.h>

#include <math.h>
#include <memory.h>
#include <stdlib.h>

using namespace rdr;
using namespace rfb;

ScaledPixelBuffer::ScaledPixelBuffer(U8 **src_data_, int src_width_,
                                     int src_height_, int scale, PixelFormat pf_)
  : scale(100), scale_ratio_x(1), scale_ratio_y(1), scaleFilterID(scaleFilterBicubic),
    xWeightTabs(0), yWeightTabs(0), scaled_data(0) {

  setSourceBuffer(src_data_, src_width_, src_height_);
  setPF(pf_);
}

ScaledPixelBuffer::ScaledPixelBuffer() 
  : src_width(0), src_height(0), scaled_width(0), scaled_height(0), scale(100), 
    scale_ratio_x(1), scale_ratio_y(1), scaleFilterID(scaleFilterBicubic),
    xWeightTabs(0), yWeightTabs(0), src_data(0), scaled_data(0) {
  memset(&pf, 0, sizeof(pf));
}

ScaledPixelBuffer::~ScaledPixelBuffer() {
  freeWeightTabs();
}

void ScaledPixelBuffer::freeWeightTabs() {
  if (xWeightTabs) {
    for (int i = 0; i < scaled_width; i++) delete [] xWeightTabs[i].weight;
    delete [] xWeightTabs;
    xWeightTabs = 0;
  }
  if (yWeightTabs) {
    for (int i = 0; i < scaled_height; i++) delete [] yWeightTabs[i].weight;
    delete [] yWeightTabs;
    yWeightTabs = 0;
  }
}

void ScaledPixelBuffer::setSourceBuffer(U8 **src_data_, int w, int h) {
  freeWeightTabs();
  src_data = src_data_;
  src_width  = w;
  src_height = h;
  calculateScaledBufferSize();
  scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
  scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
}

void ScaledPixelBuffer::setPF(const PixelFormat &pf_) {
  ///if (pf_.depth != 24) throw rfb::UnsupportedPixelFormatException();
  pf = pf_;
}

void ScaledPixelBuffer::setScale(int scale_) {
  if (scale != scale_ && scale_ > 0) {
    freeWeightTabs();
    scale = scale_;
    calculateScaledBufferSize();
    scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
    scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
  }
}

void ScaledPixelBuffer::setScaleFilter(unsigned int scaleFilterID_) {
  if (scaleFilterID == scaleFilterID_ || scaleFilterID_ > scaleFilterMaxNumber) return;

  scaleFilterID = scaleFilterID_;
  
  if (src_width && src_height && scaled_width && scaled_height && pf.depth > 0) {
    freeWeightTabs();
    scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
    scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
    if (scale != 100) scaleRect(Rect(0, 0, src_width, src_height));
  }
}

inline void ScaledPixelBuffer::rgbFromPixel(U32 p, int &r, int &g, int &b) {
  r = (((p >> pf.redShift  ) & pf.redMax  ) * 255 + pf.redMax  /2) / pf.redMax;
  g = (((p >> pf.greenShift) & pf.greenMax) * 255 + pf.greenMax/2) / pf.greenMax;
  b = (((p >> pf.blueShift ) & pf.blueMax ) * 255 + pf.blueMax /2) / pf.blueMax;
}

inline U32 ScaledPixelBuffer::getSourcePixel(int x, int y) {
  int bytes_per_pixel = pf.bpp / 8;
  U8 *ptr = &(*src_data)[(x + y*src_width)*bytes_per_pixel];
  if (bytes_per_pixel == 1) {
    return *ptr;
  } else if (bytes_per_pixel == 2) {
    int b0 = *ptr++; int b1 = *ptr;
    return b1 << 8 | b0;
  } else if (bytes_per_pixel == 4) {
    int b0 = *ptr++; int b1 = *ptr++;
    int b2 = *ptr++; int b3 = *ptr;
    return b3 << 24 | b2 << 16 | b1 << 8 | b0;
  } else {
    return 0;
  }
}

void ScaledPixelBuffer::scaleRect(const Rect& rect) {
  Rect changed_rect;
  U8 *ptr, *ptrs, *px, *pxs;
  double rx, gx, bx, red, green, blue, *xweight, *yweight, xWeight, yWeight;
  int r, g, b, xwi, ywi;

  // Calculate the changed pixel rect in the scaled image
  changed_rect = calculateScaleBoundary(rect);

  int bytesPerSrcPixel = pf.bpp / 8;
  int bytesPerSrcRow = src_width * bytesPerSrcPixel;
  int bytesPerScaledRow = scaled_width * 4;

  ptrs = &(*scaled_data)[(changed_rect.tl.x + changed_rect.tl.y*scaled_width) * 4];
  for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) {
    ptr = ptrs;
    yweight = yWeightTabs[y].weight;

    for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) {
      ywi = 0; red = 0; green = 0; blue = 0;
      xweight = xWeightTabs[x].weight;
    
      // Calculate the scaled pixel value at (x, y) coordinates by
      // convolution the matrix from source image:
      // [(xWeight.i0,yWeight.i0)......(xWeight.i1-1,yWeight.i0)]
      // [......................................................]
      // [(xWeight.i0,yWeight.i1-1)..(xWeight.i1-1,yWeight.i1-1)],
      // where [i0, i1) is the scaled filter interval.
      pxs = &(*src_data)[(xWeightTabs[x].i0 + yWeightTabs[y].i0*src_width) * bytesPerSrcPixel];
      for (int ys = yWeightTabs[y].i0; ys < yWeightTabs[y].i1; ys++) {
        xwi = 0; rx = 0; gx = 0; bx = 0; px = pxs;
        for (int xs = xWeightTabs[x].i0; xs < xWeightTabs[x].i1; xs++) {
          rgbFromPixel(*((U32*)px), r, g, b);
          xWeight = xweight[xwi++];
          rx += r * xWeight;
          gx += g * xWeight;
          bx += b * xWeight;
          px += bytesPerSrcPixel;
        }
        yWeight = yweight[ywi++];
        red += rx * yWeight;
        green += gx * yWeight;
        blue += bx * yWeight;
        pxs += bytesPerSrcRow;
      }
      *ptr++ = U8(blue);
      *ptr++ = U8(green);
      *ptr++ = U8(red);
      ptr++;
    }
    ptrs += bytesPerScaledRow;
  }
}

Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) {
  int x_start, y_start, x_end, y_end;
  double radius = scaleFilters[scaleFilterID].radius;
  double translate_x = 0.5*scale_ratio_x - 0.5;
  double translate_y = 0.5*scale_ratio_y - 0.5;
  x_start = (int)ceil(scale_ratio_x*(r.tl.x-radius) + translate_x);
  y_start = (int)ceil(scale_ratio_y*(r.tl.y-radius) + translate_y);
  x_end = (int)ceil(scale_ratio_x*(r.br.x+radius) + translate_x);
  y_end = (int)ceil(scale_ratio_y*(r.br.y+radius) + translate_y);
  if (x_start < 0) x_start = 0;
  if (y_start < 0) y_start = 0;
  if (x_end > scaled_width) x_end = scaled_width;
  if (y_end > scaled_height) y_end = scaled_height;
  return Rect(x_start, y_start, x_end, y_end);
}

void ScaledPixelBuffer::calculateScaledBufferSize() {
  double scale_ratio = (double)scale / 100;
  scaled_width  = (int)ceil(src_width  * scale_ratio);
  scaled_height = (int)ceil(src_height * scale_ratio);
  scale_ratio_x = (double)scaled_width / src_width;
  scale_ratio_y = (double)scaled_height / src_height;
}