aboutsummaryrefslogtreecommitdiffstats
path: root/rfb/ScaledPixelBuffer.cxx
blob: bf4612deb18ed0155b40bd530f237e62d4ee392d (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
/* 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/ScaledPixelBuffer.h>

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

using namespace rdr;
using namespace rfb;

ScaledPixelBuffer::ScaledPixelBuffer(U8 **src_data_, int src_width_,
                                     int src_height_, int scale)
  : bpp(32), scaled_data(0), scale_ratio(1), scale(100) {

  setSourceBuffer(src_data_, src_width_, src_height_);
}

ScaledPixelBuffer::ScaledPixelBuffer() 
  : src_data(0), src_width(0), src_height(0), scale_ratio(1), scale(100),
    bpp(32), scaled_data(0) {
}

ScaledPixelBuffer::~ScaledPixelBuffer() {
}

void ScaledPixelBuffer::setSourceBuffer(U8 **src_data_, int w, int h) {
  src_data = src_data_;
  src_width  = w;
  src_height = h;
  calculateScaledBufferSize();
  recreateScaledBuffer();
}

void ScaledPixelBuffer::setScale(int scale_) {
  if (scale != scale_) {
    scale = scale_;
    scale_ratio = double(scale) / 100;
    calculateScaledBufferSize();
    recreateScaledBuffer();
  }
}

void ScaledPixelBuffer::scaleRect(const Rect& r) {
  static U8 *src_ptr, *ptr;
  static U8 r0, r1, r2, r3;
  static U8 g0, g1, g2, g3;
  static U8 b0, b1, b2, b3;
  static double c1_sub_dx, c1_sub_dy;
  static double dx, dy;
  static int i, j;
  static Rect changed_rect;

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

  // Scale the source rect to the destination image buffer using
  // bilinear interplation
  for (int y = changed_rect.tl.y; y < changed_rect.br.y; y++) {
    j = (int)(dy = y / scale_ratio);
    dy -= j;
    c1_sub_dy = 1 - dy;

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

      i = (int)(dx = x / scale_ratio);
      dx -= i;
      c1_sub_dx = 1 - dx;

      src_ptr = &(*src_data)[(i + (j*src_width))*4];
      b0 = *src_ptr; g0 = *(src_ptr+1); r0 = *(src_ptr+2);
      if (i+1 < src_width) {
        b1 = *(src_ptr+4); g1 = *(src_ptr+5); r1 = *(src_ptr+6);
      } else {
        b1 = b0; r1 = r0; g1 = g0;
      }
      if (j+1 < src_height) {
        src_ptr += src_width * 4;
        b3 = *src_ptr; g3 = *(src_ptr+1); r3 = *(src_ptr+2);
      } else {
        b3 = b0; r3 = r0; g3 = g0;
      }
      if ((i+1 < src_width) && (j+1 < src_height)) {
        b2 = *(src_ptr+4); g2 = *(src_ptr+5); r2 = *(src_ptr+6);
      } else if (i+1 >= src_width) {
        b2 = b3; r2 = r3; g2 = g3;
      } else {
        b2 = b1; r2 = r1; g2 = g1;
      }
      *ptr++ = (U8)((b0*c1_sub_dx+b1*dx)*c1_sub_dy + (b3*c1_sub_dx+b2*dx)*dy);
      *ptr++ = (U8)((g0*c1_sub_dx+g1*dx)*c1_sub_dy + (g3*c1_sub_dx+g2*dx)*dy);
      *ptr   = (U8)((r0*c1_sub_dx+r1*dx)*c1_sub_dy + (r3*c1_sub_dx+r2*dx)*dy);
    }
  }
}

Rect ScaledPixelBuffer::calculateScaleBoundary(const Rect& r) {
  static int x_start, y_start, x_end, y_end;
  x_start = r.tl.x == 0 ? 0 : ceil((r.tl.x-1) * scale_ratio);
  y_start = r.tl.y == 0 ? 0 : ceil((r.tl.y-1) * scale_ratio);
  x_end = ceil(r.br.x * scale_ratio - 1); 
  x_end = x_end < scaled_width ? x_end + 1 : scaled_width;
  y_end = ceil(r.br.y * scale_ratio - 1);
  y_end = y_end < scaled_height ? y_end + 1 : scaled_height;
  return Rect(x_start, y_start, x_end, y_end);
}

void ScaledPixelBuffer::calculateScaledBufferSize() {
  scaled_width  = (int)ceil(src_width  * scale_ratio);
  scaled_height = (int)ceil(src_height * scale_ratio);
}

void ScaledPixelBuffer::recreateScaledBuffer() {
}