Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2006 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 | * TightVNC distribution homepage on the Web: http://www.tightvnc.com/ |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | // -=- ScaledDIBSectionBuffer.cxx |
| 23 | |
| 24 | #include <math.h> |
| 25 | |
| 26 | #include <rfb_win32/ScaledDIBSectionBuffer.h> |
| 27 | |
| 28 | using namespace rfb; |
| 29 | using namespace win32; |
| 30 | |
| 31 | ScaledDIBSectionBuffer::ScaledDIBSectionBuffer(HWND window) |
| 32 | : src_buffer(0), scaling(false), DIBSectionBuffer(window) { |
| 33 | scaled_data = data; |
| 34 | } |
| 35 | |
| 36 | ScaledDIBSectionBuffer::~ScaledDIBSectionBuffer() { |
| 37 | if (src_buffer) delete src_buffer; |
| 38 | } |
| 39 | |
george82 | bf0adb3 | 2006-07-29 10:29:41 +0000 | [diff] [blame] | 40 | void ScaledDIBSectionBuffer::setScaleRatio(double scale_ratio_) { |
| 41 | if (scale_ratio == scale_ratio_) return; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 42 | |
george82 | 2ac935e | 2006-05-29 13:57:39 +0000 | [diff] [blame] | 43 | if (format.depth != 24) throw rfb::UnsupportedPixelFormatException(); |
| 44 | |
george82 | bf0adb3 | 2006-07-29 10:29:41 +0000 | [diff] [blame] | 45 | if (scale_ratio_ != 1) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 46 | scaling = true; |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 47 | if (!src_buffer) { |
| 48 | src_buffer = new ManagedPixelBuffer(format, src_width, src_height); |
| 49 | src_data = &(src_buffer->data); |
george82 | e89d47f | 2006-05-27 12:31:08 +0000 | [diff] [blame] | 50 | memcpy(src_buffer->data, data, area() * (getPF().bpp/8)); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 51 | } |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 52 | } else { |
| 53 | scaling = false; |
| 54 | } |
george82 | bf0adb3 | 2006-07-29 10:29:41 +0000 | [diff] [blame] | 55 | ScaledPixelBuffer::setScaleRatio(scale_ratio_); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 56 | recreateScaledBuffer(); |
george82 | e89d47f | 2006-05-27 12:31:08 +0000 | [diff] [blame] | 57 | if (scaling) { |
| 58 | scaleRect(Rect(0, 0, src_width, src_height)); |
| 59 | } else { |
| 60 | memcpy(data, src_buffer->data, src_buffer->area() * (src_buffer->getPF().bpp/8)); |
| 61 | if (src_buffer) { |
| 62 | delete src_buffer; |
| 63 | src_buffer = 0; |
| 64 | src_data = 0; |
| 65 | } |
| 66 | } |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 69 | void ScaledDIBSectionBuffer::setPF(const PixelFormat &pf_) { |
george82 | e3341e7 | 2006-08-02 15:23:39 +0000 | [diff] [blame^] | 70 | if (memcmp(&getPF(), &pf_, sizeof(pf_)) == 0) return; |
| 71 | |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 72 | if (scaling) { |
| 73 | ScaledPixelBuffer::setPF(pf_); |
| 74 | src_buffer->setPF(pf_); |
| 75 | } |
| 76 | DIBSectionBuffer::setPF(pf_); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 77 | scaled_data = data; |
| 78 | } |
| 79 | |
| 80 | void ScaledDIBSectionBuffer::setSize(int src_width_, int src_height_) { |
george82 | e3341e7 | 2006-08-02 15:23:39 +0000 | [diff] [blame^] | 81 | if (src_width == src_width_ && src_height == src_height_) return; |
| 82 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 83 | src_width = src_width_; |
| 84 | src_height = src_height_; |
| 85 | if (scaling) { |
| 86 | src_buffer->setSize(src_width, src_height); |
| 87 | } |
| 88 | calculateScaledBufferSize(); |
| 89 | recreateScaledBuffer(); |
| 90 | scaled_data = data; |
| 91 | } |
| 92 | |
| 93 | void ScaledDIBSectionBuffer::recreateScaledBuffer() { |
| 94 | width_ = scaled_width; |
| 95 | height_ = scaled_height; |
george82 | e3341e7 | 2006-08-02 15:23:39 +0000 | [diff] [blame^] | 96 | if (width_ && height_ && (format.depth != 0)) { |
| 97 | DIBSectionBuffer::recreateBuffer(); |
| 98 | scaled_data = data; |
| 99 | } |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void ScaledDIBSectionBuffer::fillRect(const Rect &dest, Pixel pix) { |
| 103 | if (scaling) { |
| 104 | src_buffer->fillRect(dest, pix); |
| 105 | scaleRect(dest); |
| 106 | } else { |
| 107 | DIBSectionBuffer::fillRect(dest, pix); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void ScaledDIBSectionBuffer::imageRect(const Rect &dest, const void* pixels, int stride) { |
| 112 | if (scaling) { |
| 113 | src_buffer->imageRect(dest, pixels, stride); |
| 114 | scaleRect(dest); |
| 115 | } else { |
| 116 | DIBSectionBuffer::imageRect(dest, pixels, stride); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void ScaledDIBSectionBuffer::copyRect(const Rect &dest, const Point &move_by_delta) { |
| 121 | if (scaling) { |
| 122 | src_buffer->copyRect(dest, move_by_delta); |
| 123 | scaleRect(dest); |
| 124 | } else { |
| 125 | DIBSectionBuffer::copyRect(dest, move_by_delta); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void ScaledDIBSectionBuffer::maskRect(const Rect& r, const void* pixels, const void* mask_) { |
| 130 | if (scaling) { |
| 131 | src_buffer->maskRect(r, pixels, mask_); |
| 132 | scaleRect(r); |
| 133 | } else { |
| 134 | DIBSectionBuffer::maskRect(r, pixels, mask_); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void ScaledDIBSectionBuffer::maskRect(const Rect& r, Pixel pixel, const void* mask_) { |
| 139 | if (scaling) { |
| 140 | src_buffer->maskRect(r, pixel, mask_); |
| 141 | scaleRect(r); |
| 142 | } else { |
| 143 | DIBSectionBuffer::maskRect(r, pixel, mask_); |
| 144 | } |
| 145 | } |