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.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 | |
| 25 | #include <rdr/types.h> |
george82 | f98083a | 2006-05-29 13:52:55 +0000 | [diff] [blame] | 26 | #include <rdr/Exception.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 27 | #include <rfb/Rect.h> |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 28 | #include <rfb/PixelFormat.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace rdr; |
| 31 | |
| 32 | namespace rfb { |
| 33 | |
george82 | f98083a | 2006-05-29 13:52:55 +0000 | [diff] [blame] | 34 | struct UnsupportedPixelFormatException : public Exception { |
| 35 | UnsupportedPixelFormatException(const char* s="Now supported only true colour pixel data in the scaling mode.") |
| 36 | : Exception(s) {} |
| 37 | }; |
| 38 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 39 | class ScaledPixelBuffer { |
| 40 | public: |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 41 | ScaledPixelBuffer(U8 **data, int width, int height, int scale, PixelFormat pf); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | ScaledPixelBuffer(); |
| 43 | virtual ~ScaledPixelBuffer(); |
| 44 | |
| 45 | // Get width, height, number of pixels and scale |
| 46 | int width() const { return scaled_width; } |
| 47 | int height() const { return scaled_height; } |
george82 | b85a0d6 | 2006-07-23 07:09:35 +0000 | [diff] [blame] | 48 | int getSrcWidth() const { return src_width; } |
| 49 | int getSrcHeight() const { return src_height; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 50 | int area() const { return scaled_width * scaled_height; } |
george82 | e297baf | 2006-09-11 06:37:50 +0000 | [diff] [blame] | 51 | int getScale() const { return int(scale_ratio * 100 + 0.5); } |
george82 | d4344be | 2006-07-29 10:27:19 +0000 | [diff] [blame] | 52 | double getScaleRatio() const { return scale_ratio; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 53 | |
| 54 | // Get rectangle encompassing this buffer |
| 55 | // Top-left of rectangle is either at (0,0), or the specified point. |
| 56 | Rect getRect() const { return Rect(0, 0, scaled_width, scaled_height); } |
| 57 | Rect getRect(const Point& pos) const { |
| 58 | return Rect(pos, pos.translate(Point(scaled_width, scaled_height))); |
| 59 | } |
| 60 | |
| 61 | // Set the new source buffer and its parameters |
| 62 | void setSourceBuffer(U8 **src_data, int w, int h); |
| 63 | |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 64 | // Set the new pixel format |
| 65 | void setPF(const PixelFormat &pf); |
| 66 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 67 | // Set the new scale, in percent |
george82 | d4344be | 2006-07-29 10:27:19 +0000 | [diff] [blame] | 68 | virtual void setScale(int scale) { setScaleRatio(double(scale)/100.0); } |
| 69 | virtual void setScaleRatio(double scale_ratio); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 70 | |
| 71 | // Scale rect from the source image buffer to the destination buffer |
| 72 | // using bilinear interpolation |
| 73 | virtual void scaleRect(const Rect& r); |
| 74 | |
| 75 | // Calculate the scaled image rectangle which depend on the source |
| 76 | // image rectangle. |
| 77 | inline Rect calculateScaleBoundary(const Rect& r); |
| 78 | |
| 79 | protected: |
| 80 | |
| 81 | // Calculate the scaled buffer size depending on the source buffer |
| 82 | // parameters (width, height, pixel format) |
| 83 | void calculateScaledBufferSize(); |
| 84 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 85 | |
| 86 | int src_width; |
| 87 | int src_height; |
| 88 | int scaled_width; |
| 89 | int scaled_height; |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 90 | PixelFormat pf; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 91 | double scale_ratio; |
| 92 | U8 **src_data; |
george82 | df9f31c | 2006-09-16 11:06:07 +0000 | [diff] [blame^] | 93 | U8 **scaled_data; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | }; |