blob: e0091877783a70d71a9e81c84c788e19a9bb89a0 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* 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
28using namespace rfb;
29using namespace win32;
30
31ScaledDIBSectionBuffer::ScaledDIBSectionBuffer(HWND window)
32 : src_buffer(0), scaling(false), DIBSectionBuffer(window) {
33 scaled_data = data;
34}
35
36ScaledDIBSectionBuffer::~ScaledDIBSectionBuffer() {
37 if (src_buffer) delete src_buffer;
38}
39
george82bf0adb32006-07-29 10:29:41 +000040void ScaledDIBSectionBuffer::setScaleRatio(double scale_ratio_) {
41 if (scale_ratio == scale_ratio_) return;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000042
george82e4b3c2c2006-09-05 06:43:28 +000043 if (format.depth != 24 && format.depth != 0) throw rfb::UnsupportedPixelFormatException();
george822ac935e2006-05-29 13:57:39 +000044
george82e4b3c2c2006-09-05 06:43:28 +000045 if (scale_ratio_ != 1) scaling = true;
46 else scaling = false;
george82bf0adb32006-07-29 10:29:41 +000047 ScaledPixelBuffer::setScaleRatio(scale_ratio_);
george82e4b3c2c2006-09-05 06:43:28 +000048 recreateBuffers();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000049}
50
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000051void ScaledDIBSectionBuffer::setPF(const PixelFormat &pf_) {
george82e4b3c2c2006-09-05 06:43:28 +000052 if (memcmp(&(ScaledPixelBuffer::pf), &pf_, sizeof(pf_)) == 0) return;
george82e3341e72006-08-02 15:23:39 +000053
george82e4b3c2c2006-09-05 06:43:28 +000054 ScaledPixelBuffer::pf = pf_;
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000055 if (scaling) {
george82e4b3c2c2006-09-05 06:43:28 +000056 if (src_buffer) src_buffer->setPF(pf_);
57 else {
58 src_buffer = new ManagedPixelBuffer(pf_, src_width, src_height);
59 src_data = &(src_buffer->data);
60 }
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +000061 }
62 DIBSectionBuffer::setPF(pf_);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000063 scaled_data = data;
64}
65
66void ScaledDIBSectionBuffer::setSize(int src_width_, int src_height_) {
george82e3341e72006-08-02 15:23:39 +000067 if (src_width == src_width_ && src_height == src_height_) return;
68
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000069 src_width = src_width_;
70 src_height = src_height_;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000071 calculateScaledBufferSize();
george82e4b3c2c2006-09-05 06:43:28 +000072 recreateBuffers();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000073}
74
75void ScaledDIBSectionBuffer::recreateScaledBuffer() {
76 width_ = scaled_width;
77 height_ = scaled_height;
george82e3341e72006-08-02 15:23:39 +000078 if (width_ && height_ && (format.depth != 0)) {
79 DIBSectionBuffer::recreateBuffer();
80 scaled_data = data;
81 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000082}
83
george82e4b3c2c2006-09-05 06:43:28 +000084void ScaledDIBSectionBuffer::recreateBuffers() {
85 width_ = scaled_width;
86 height_ = scaled_height;
87 if (scaled_width && scaled_height && format.depth != 0 && scale_ratio != 0) {
88 if (scaling) {
89 if (src_buffer) {
90 if (src_buffer->width() != src_width || src_buffer->width() != src_height)
91 src_buffer->setSize(src_width, src_height);
92 if (memcmp(&src_buffer->getPF(), &pf, sizeof(pf)) == 0)
93 src_buffer->setPF(pf);
94 } else {
95 src_buffer = new ManagedPixelBuffer(format, src_width, src_height);
96 src_data = &(src_buffer->data);
97 memcpy(src_buffer->data, data, src_width * src_height * (getPF().bpp/8));
98 }
99 }
100 recreateScaledBuffer();
101 if (scaling) {
102 scaleRect(Rect(0, 0, src_width, src_height));
103 } else {
104 memcpy(data, src_buffer->data, src_buffer->area() * (src_buffer->getPF().bpp/8));
105 if (src_buffer) {
106 delete src_buffer;
107 src_buffer = 0;
108 src_data = 0;
109 }
110 }
111 }
112}
113
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000114void ScaledDIBSectionBuffer::fillRect(const Rect &dest, Pixel pix) {
115 if (scaling) {
116 src_buffer->fillRect(dest, pix);
117 scaleRect(dest);
118 } else {
119 DIBSectionBuffer::fillRect(dest, pix);
120 }
121}
122
123void ScaledDIBSectionBuffer::imageRect(const Rect &dest, const void* pixels, int stride) {
124 if (scaling) {
125 src_buffer->imageRect(dest, pixels, stride);
126 scaleRect(dest);
127 } else {
128 DIBSectionBuffer::imageRect(dest, pixels, stride);
129 }
130}
131
132void ScaledDIBSectionBuffer::copyRect(const Rect &dest, const Point &move_by_delta) {
133 if (scaling) {
134 src_buffer->copyRect(dest, move_by_delta);
135 scaleRect(dest);
136 } else {
137 DIBSectionBuffer::copyRect(dest, move_by_delta);
138 }
139}
140
141void ScaledDIBSectionBuffer::maskRect(const Rect& r, const void* pixels, const void* mask_) {
142 if (scaling) {
143 src_buffer->maskRect(r, pixels, mask_);
144 scaleRect(r);
145 } else {
146 DIBSectionBuffer::maskRect(r, pixels, mask_);
147 }
148}
149
150void ScaledDIBSectionBuffer::maskRect(const Rect& r, Pixel pixel, const void* mask_) {
151 if (scaling) {
152 src_buffer->maskRect(r, pixel, mask_);
153 scaleRect(r);
154 } else {
155 DIBSectionBuffer::maskRect(r, pixel, mask_);
156 }
157}