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