blob: 4c37752115034fcf94b4e7bb61952ef144c8d87b [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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.h
20//
21// The ScaledPixelBuffer class allows to scale the image data
22// from the source buffer to destination buffer using bilinear
23// interpolation.
24
Peter Åstrandb49cd1b2008-12-09 13:39:24 +000025#ifndef __RFB_SCALEDPIXELBUFFER_H__
26#define __RFB_SCALEDPIXELBUFFER_H__
27
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028#include <rdr/types.h>
george82f98083a2006-05-29 13:52:55 +000029#include <rdr/Exception.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030#include <rfb/Rect.h>
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000031#include <rfb/PixelFormat.h>
george824ff66752006-11-20 15:55:05 +000032#include <rfb/ScaleFilters.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033
34using namespace rdr;
35
36namespace rfb {
37
george82f98083a2006-05-29 13:52:55 +000038 struct UnsupportedPixelFormatException : public Exception {
39 UnsupportedPixelFormatException(const char* s="Now supported only true colour pixel data in the scaling mode.")
40 : Exception(s) {}
41 };
42
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043 class ScaledPixelBuffer {
44 public:
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000045 ScaledPixelBuffer(U8 **data, int width, int height, int scale, PixelFormat pf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046 ScaledPixelBuffer();
47 virtual ~ScaledPixelBuffer();
48
49 // Get width, height, number of pixels and scale
50 int width() const { return scaled_width; }
51 int height() const { return scaled_height; }
george82b85a0d62006-07-23 07:09:35 +000052 int getSrcWidth() const { return src_width; }
53 int getSrcHeight() const { return src_height; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054 int area() const { return scaled_width * scaled_height; }
george822446ed02007-03-10 08:55:35 +000055 int getScale() const { return scale; }
56 double getScaleRatioX() const { return scale_ratio_x; }
57 double getScaleRatioY() const { return scale_ratio_y; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058
george824ff66752006-11-20 15:55:05 +000059 // Pixel manipulation routines
60 inline U32 getSourcePixel(int x, int y);
61 inline void rgbFromPixel(U32 p, int &r, int &g, int &b);
62
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000063 // Get rectangle encompassing this buffer
64 // Top-left of rectangle is either at (0,0), or the specified point.
65 Rect getRect() const { return Rect(0, 0, scaled_width, scaled_height); }
66 Rect getRect(const Point& pos) const {
67 return Rect(pos, pos.translate(Point(scaled_width, scaled_height)));
68 }
69
70 // Set the new source buffer and its parameters
71 void setSourceBuffer(U8 **src_data, int w, int h);
72
george824ff66752006-11-20 15:55:05 +000073 void setScaledBuffer(U8 **scaled_data_) {
74 scaled_data = scaled_data_;
75 };
76
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000077 // Set the new pixel format
78 void setPF(const PixelFormat &pf);
79
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080 // Set the new scale, in percent
george822446ed02007-03-10 08:55:35 +000081 virtual void setScale(int scale);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082
george82c4eb6262007-03-20 10:54:38 +000083 // Set/get the scale method
84 virtual void setScaleFilter(unsigned int scaleFilterID);
85 unsigned int getScaleFilterID() const { return scaleFilterID; }
86
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 // Scale rect from the source image buffer to the destination buffer
george82ea974d72006-11-26 11:21:02 +000088 // using the current interpolation method
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 virtual void scaleRect(const Rect& r);
90
91 // Calculate the scaled image rectangle which depend on the source
92 // image rectangle.
Peter Åstrandb49cd1b2008-12-09 13:39:24 +000093 Rect calculateScaleBoundary(const Rect& r);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094
95 protected:
96
97 // Calculate the scaled buffer size depending on the source buffer
98 // parameters (width, height, pixel format)
george824ff66752006-11-20 15:55:05 +000099 virtual void calculateScaledBufferSize();
100
101 // Free the weight tabs for x and y
102 virtual void freeWeightTabs();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000103
george823a1982e2007-11-04 07:35:51 +0000104 // Recreates the row accumulators.
105 virtual void recreateRowAccum();
106
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107
108 int src_width;
109 int src_height;
110 int scaled_width;
111 int scaled_height;
george822446ed02007-03-10 08:55:35 +0000112 int scale;
113 double scale_ratio_x;
114 double scale_ratio_y;
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +0000115 PixelFormat pf;
george824ff66752006-11-20 15:55:05 +0000116 unsigned int scaleFilterID;
117 ScaleFilters scaleFilters;
118 SFilterWeightTab *xWeightTabs;
119 SFilterWeightTab *yWeightTabs;
george823a1982e2007-11-04 07:35:51 +0000120 int *raccum;
121 int *gaccum;
122 int *baccum;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000123 U8 **src_data;
george82df9f31c2006-09-16 11:06:07 +0000124 U8 **scaled_data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000125 };
126
127};
Peter Åstrandb49cd1b2008-12-09 13:39:24 +0000128
129#endif