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 | * |
Peter Åstrand | 7877cd6 | 2009-02-25 16:15:48 +0000 | [diff] [blame^] | 18 | * |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 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 | |
george82 | b2f8ae5 | 2006-09-16 14:41:12 +0000 | [diff] [blame] | 31 | const PixelFormat RGB24(32, 24, 0, 1, 255, 255, 255, 16, 8, 0); |
| 32 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 33 | ScaledDIBSectionBuffer::ScaledDIBSectionBuffer(HWND window) |
Peter Åstrand | 5327f41 | 2008-12-10 12:07:33 +0000 | [diff] [blame] | 34 | : DIBSectionBuffer(window), src_buffer(0), scaling(false) { |
george82 | df9f31c | 2006-09-16 11:06:07 +0000 | [diff] [blame] | 35 | scaled_data = &data; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | ScaledDIBSectionBuffer::~ScaledDIBSectionBuffer() { |
| 39 | if (src_buffer) delete src_buffer; |
| 40 | } |
| 41 | |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 42 | void ScaledDIBSectionBuffer::setScale(int scale_) { |
| 43 | if (scale == scale_ || scale_ <= 0) return; |
george82 | 3e43ed5 | 2007-11-05 17:13:10 +0000 | [diff] [blame] | 44 | if (!(getPixelFormat().trueColour) && scale_ != 100) throw rfb::UnsupportedPixelFormatException(); |
george82 | c79c7bb | 2007-11-05 11:22:14 +0000 | [diff] [blame] | 45 | ScaledPixelBuffer::setScale(scale_); |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 46 | if (scale == 100) scaling = false; |
george82 | b2f8ae5 | 2006-09-16 14:41:12 +0000 | [diff] [blame] | 47 | else scaling = true; |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 48 | recreateBuffers(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 51 | void ScaledDIBSectionBuffer::setPF(const PixelFormat &pf_) { |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 52 | if (memcmp(&(ScaledPixelBuffer::pf), &pf_, sizeof(pf_)) == 0) return; |
george82 | e3341e7 | 2006-08-02 15:23:39 +0000 | [diff] [blame] | 53 | |
george82 | 3e43ed5 | 2007-11-05 17:13:10 +0000 | [diff] [blame] | 54 | if (!pf_.trueColour && isScaling()) throw rfb::UnsupportedPixelFormatException(); |
george82 | 0ba8cb7 | 2006-09-23 07:07:17 +0000 | [diff] [blame] | 55 | |
george82 | b2f8ae5 | 2006-09-16 14:41:12 +0000 | [diff] [blame] | 56 | pf = pf_; |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 57 | if (scaling) { |
george82 | b2f8ae5 | 2006-09-16 14:41:12 +0000 | [diff] [blame] | 58 | if (src_buffer) { |
| 59 | src_buffer->setPF(pf); |
| 60 | } else { |
| 61 | src_buffer = new ManagedPixelBuffer(pf, src_width, src_height); |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 62 | src_data = &(src_buffer->data); |
| 63 | } |
george82 | b2f8ae5 | 2006-09-16 14:41:12 +0000 | [diff] [blame] | 64 | if (memcmp(&(DIBSectionBuffer::getPF()), &RGB24, sizeof(PixelFormat)) != 0) { |
| 65 | DIBSectionBuffer::setPF(RGB24); |
| 66 | } |
| 67 | } else { |
| 68 | DIBSectionBuffer::setPF(pf); |
Constantin Kaplinsky | 1ae2eb0 | 2006-05-26 05:24:24 +0000 | [diff] [blame] | 69 | } |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void ScaledDIBSectionBuffer::setSize(int src_width_, int src_height_) { |
george82 | e3341e7 | 2006-08-02 15:23:39 +0000 | [diff] [blame] | 73 | if (src_width == src_width_ && src_height == src_height_) return; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 74 | src_width = src_width_; |
| 75 | src_height = src_height_; |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 76 | |
| 77 | // FIXME: |
| 78 | // Calculate the scale weight tabs must be in the ScalePixelBuffer class |
george82 | 3a1982e | 2007-11-04 07:35:51 +0000 | [diff] [blame] | 79 | recreateRowAccum(); |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 80 | freeWeightTabs(); |
| 81 | calculateScaledBufferSize(); |
george82 | 2446ed0 | 2007-03-10 08:55:35 +0000 | [diff] [blame] | 82 | scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs); |
| 83 | scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs); |
george82 | 4ff6675 | 2006-11-20 15:55:05 +0000 | [diff] [blame] | 84 | |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 85 | recreateBuffers(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
george82 | 770bbbc | 2007-03-12 10:48:09 +0000 | [diff] [blame] | 88 | void ScaledDIBSectionBuffer::setScaleWindowSize(int width, int height) { |
| 89 | if (scaled_width == width && scaled_height == height) return; |
| 90 | |
| 91 | freeWeightTabs(); |
| 92 | |
| 93 | scaled_width = width_ = width; |
| 94 | scaled_height = height_ = height; |
| 95 | |
| 96 | if (scaled_width == src_width && scaled_height == src_height) scaling = false; |
| 97 | else scaling = true; |
| 98 | scale_ratio_x = (double)scaled_width / src_width; |
| 99 | scale_ratio_y = (double)scaled_height / src_height; |
| 100 | scale = (int)(scale_ratio_x * 100); |
| 101 | |
| 102 | // FIXME: |
| 103 | // Calculate the scale weight tabs must be in the ScalePixelBuffer class |
| 104 | scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs); |
| 105 | scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs); |
| 106 | |
| 107 | recreateBuffers(); |
| 108 | } |
| 109 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 110 | void ScaledDIBSectionBuffer::recreateScaledBuffer() { |
george82 | b2f8ae5 | 2006-09-16 14:41:12 +0000 | [diff] [blame] | 111 | if (scaling && memcmp(&(DIBSectionBuffer::getPF()), &RGB24, sizeof(PixelFormat)) != 0) { |
| 112 | DIBSectionBuffer::setPF(RGB24); |
george82 | 0ba8cb7 | 2006-09-23 07:07:17 +0000 | [diff] [blame] | 113 | } else if (!scaling && (memcmp(&(DIBSectionBuffer::getPF()), &pf, sizeof(PixelFormat)) != 0)){ |
| 114 | DIBSectionBuffer::setPF(pf); |
george82 | b2f8ae5 | 2006-09-16 14:41:12 +0000 | [diff] [blame] | 115 | } else { |
george82 | e3341e7 | 2006-08-02 15:23:39 +0000 | [diff] [blame] | 116 | DIBSectionBuffer::recreateBuffer(); |
george82 | e3341e7 | 2006-08-02 15:23:39 +0000 | [diff] [blame] | 117 | } |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 118 | } |
| 119 | |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 120 | void ScaledDIBSectionBuffer::recreateBuffers() { |
george82 | 0ba8cb7 | 2006-09-23 07:07:17 +0000 | [diff] [blame] | 121 | // Recreate the source pixel buffer |
| 122 | if (src_width && src_height && pf.depth > 0) { |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 123 | if (scaling) { |
george82 | b2f8ae5 | 2006-09-16 14:41:12 +0000 | [diff] [blame] | 124 | if (src_buffer) { |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 125 | if (src_buffer->width() != src_width || src_buffer->width() != src_height) |
| 126 | src_buffer->setSize(src_width, src_height); |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 127 | } else { |
george82 | 0ba8cb7 | 2006-09-23 07:07:17 +0000 | [diff] [blame] | 128 | src_buffer = new ManagedPixelBuffer(pf, src_width, src_height); |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 129 | src_data = &(src_buffer->data); |
george82 | 0ba8cb7 | 2006-09-23 07:07:17 +0000 | [diff] [blame] | 130 | if (data) memcpy(src_buffer->data, data, src_width * src_height * (getPF().bpp/8)); |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
george82 | 0ba8cb7 | 2006-09-23 07:07:17 +0000 | [diff] [blame] | 133 | } |
| 134 | // Recreate the scaled pixel buffer |
| 135 | recreateScaledBuffer(); |
| 136 | if (scaling && src_buffer && data) scaleRect(Rect(0, 0, src_width, src_height)); |
| 137 | else if (!scaling && src_buffer) { |
| 138 | if (src_buffer->data && data) memcpy(data, src_buffer->data, src_buffer->area() * (getPF().bpp/8)); |
| 139 | delete src_buffer; |
| 140 | src_buffer = 0; |
| 141 | src_data = 0; |
george82 | e4b3c2c | 2006-09-05 06:43:28 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
george82 | 96b17b9 | 2006-09-05 15:44:35 +0000 | [diff] [blame] | 145 | void ScaledDIBSectionBuffer::calculateScaledBufferSize() { |
| 146 | ScaledPixelBuffer::calculateScaledBufferSize(); |
| 147 | width_ = scaled_width; |
| 148 | height_ = scaled_height; |
| 149 | } |
| 150 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 151 | void ScaledDIBSectionBuffer::fillRect(const Rect &dest, Pixel pix) { |
| 152 | if (scaling) { |
| 153 | src_buffer->fillRect(dest, pix); |
| 154 | scaleRect(dest); |
| 155 | } else { |
| 156 | DIBSectionBuffer::fillRect(dest, pix); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void ScaledDIBSectionBuffer::imageRect(const Rect &dest, const void* pixels, int stride) { |
| 161 | if (scaling) { |
| 162 | src_buffer->imageRect(dest, pixels, stride); |
| 163 | scaleRect(dest); |
| 164 | } else { |
| 165 | DIBSectionBuffer::imageRect(dest, pixels, stride); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void ScaledDIBSectionBuffer::copyRect(const Rect &dest, const Point &move_by_delta) { |
| 170 | if (scaling) { |
| 171 | src_buffer->copyRect(dest, move_by_delta); |
| 172 | scaleRect(dest); |
| 173 | } else { |
| 174 | DIBSectionBuffer::copyRect(dest, move_by_delta); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void ScaledDIBSectionBuffer::maskRect(const Rect& r, const void* pixels, const void* mask_) { |
| 179 | if (scaling) { |
| 180 | src_buffer->maskRect(r, pixels, mask_); |
| 181 | scaleRect(r); |
| 182 | } else { |
| 183 | DIBSectionBuffer::maskRect(r, pixels, mask_); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void ScaledDIBSectionBuffer::maskRect(const Rect& r, Pixel pixel, const void* mask_) { |
| 188 | if (scaling) { |
| 189 | src_buffer->maskRect(r, pixel, mask_); |
| 190 | scaleRect(r); |
| 191 | } else { |
| 192 | DIBSectionBuffer::maskRect(r, pixel, mask_); |
| 193 | } |
| 194 | } |