blob: 0c266b00643ebb92792e3d6b7c40d033463f80e7 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +01002 * Copyright 2014 Pierre Ossman for Cendio AB
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20#include <rfb_win32/DIBSectionBuffer.h>
21#include <rfb_win32/DeviceContext.h>
22#include <rfb_win32/BitmapInfo.h>
23#include <rfb/LogWriter.h>
24
25using namespace rfb;
26using namespace win32;
27
28static LogWriter vlog("DIBSectionBuffer");
29
30
31DIBSectionBuffer::DIBSectionBuffer(HWND window_)
Peter Åstrandee3f46a2008-12-10 10:29:06 +000032 : bitmap(0), window(window_), device(0) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000033 memset(&format, 0, sizeof(format));
34 memset(palette, 0, sizeof(palette));
35}
36
37DIBSectionBuffer::DIBSectionBuffer(HDC device_)
38 : bitmap(0), window(0), device(device_) {
39 memset(&format, 0, sizeof(format));
40 memset(palette, 0, sizeof(palette));
41}
42
43DIBSectionBuffer::~DIBSectionBuffer() {
44 if (bitmap)
45 DeleteObject(bitmap);
46}
47
48
49void DIBSectionBuffer::setPF(const PixelFormat& pf) {
50 if (memcmp(&getPF(), &pf, sizeof(pf)) == 0) {
51 vlog.debug("pixel format unchanged by setPF()");
52 return;
53 }
54 format = pf;
55 recreateBuffer();
56 if ((pf.bpp <= 8) && pf.trueColour) {
57 vlog.info("creating %d-bit TrueColour palette", pf.depth);
58 for (int i=0; i < (1<<(pf.depth)); i++) {
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +010059 rdr::U16 r, g, b;
60 pf.rgbFromPixel(i, NULL, &r, &g, &b);
61 palette[i].r = r;
62 palette[i].g = g;
63 palette[i].b = b;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000064 }
65 refreshPalette();
66 }
67}
68
69void DIBSectionBuffer::setSize(int w, int h) {
70 if (width_ == w && height_ == h) {
71 vlog.debug("size unchanged by setSize()");
72 return;
73 }
74 width_ = w;
75 height_ = h;
76 recreateBuffer();
77}
78
79
80// * copyPaletteToDIB MUST NEVER be called on a truecolour DIB! *
81
82void copyPaletteToDIB(Colour palette[256], HDC wndDC, HBITMAP dib) {
83 BitmapDC dibDC(wndDC, dib);
84 RGBQUAD rgb[256];
85 for (unsigned int i=0;i<256;i++) {
86 rgb[i].rgbRed = palette[i].r >> 8;
87 rgb[i].rgbGreen = palette[i].g >> 8;
88 rgb[i].rgbBlue = palette[i].b >> 8;
89 }
90 if (!SetDIBColorTable(dibDC, 0, 256, (RGBQUAD*) rgb))
91 throw rdr::SystemException("unable to SetDIBColorTable", GetLastError());
92}
93
94inline void initMaxAndShift(DWORD mask, int* max, int* shift) {
95 for ((*shift) = 0; (mask & 1) == 0; (*shift)++) mask >>= 1;
96 (*max) = (rdr::U16)mask;
97}
98
99void DIBSectionBuffer::recreateBuffer() {
100 HBITMAP new_bitmap = 0;
101 rdr::U8* new_data = 0;
102
103 if (width_ && height_ && (format.depth != 0)) {
104 BitmapInfo bi;
105 memset(&bi, 0, sizeof(bi));
106 // *** wrong?
107 UINT iUsage = format.trueColour ? DIB_RGB_COLORS : DIB_PAL_COLORS;
108 // ***
109 bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
110 bi.bmiHeader.biBitCount = format.bpp;
111 bi.bmiHeader.biSizeImage = (format.bpp / 8) * width_ * height_;
112 bi.bmiHeader.biPlanes = 1;
113 bi.bmiHeader.biWidth = width_;
114 bi.bmiHeader.biHeight = -height_;
115 bi.bmiHeader.biCompression = (format.bpp > 8) ? BI_BITFIELDS : BI_RGB;
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100116 bi.mask.red = format.pixelFromRGB((rdr::U16)~0, 0, 0);
117 bi.mask.green = format.pixelFromRGB(0, (rdr::U16)~0, 0);
118 bi.mask.blue = format.pixelFromRGB(0, 0, (rdr::U16)~0);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000119
120 // Create a DIBSection to draw into
121 if (device)
122 new_bitmap = ::CreateDIBSection(device, (BITMAPINFO*)&bi.bmiHeader, iUsage,
123 (void**)&new_data, NULL, 0);
124 else
125 new_bitmap = ::CreateDIBSection(WindowDC(window), (BITMAPINFO*)&bi.bmiHeader, iUsage,
126 (void**)&new_data, NULL, 0);
127
128 if (!new_bitmap) {
129 int err = GetLastError();
130 throw rdr::SystemException("unable to create DIB section", err);
131 }
132
133 vlog.debug("recreateBuffer()");
134 } else {
135 vlog.debug("one of area or format not set");
136 }
137
138 if (new_bitmap && bitmap) {
139 vlog.debug("preserving bitmap contents");
140
141 // Copy the contents across
142 if (device) {
143 if (format.bpp <= 8)
144 copyPaletteToDIB(palette, device, new_bitmap);
145 BitmapDC src_dev(device, bitmap);
146 BitmapDC dest_dev(device, new_bitmap);
147 BitBlt(dest_dev, 0, 0, width_, height_, src_dev, 0, 0, SRCCOPY);
148 } else {
149 WindowDC wndDC(window);
150 if (format.bpp <= 8)
151 copyPaletteToDIB(palette, wndDC, new_bitmap);
152 BitmapDC src_dev(wndDC, bitmap);
153 BitmapDC dest_dev(wndDC, new_bitmap);
154 BitBlt(dest_dev, 0, 0, width_, height_, src_dev, 0, 0, SRCCOPY);
155 }
156 }
157
158 if (bitmap) {
159 // Delete the old bitmap
160 DeleteObject(bitmap);
161 bitmap = 0;
162 data = 0;
163 }
164
165 if (new_bitmap) {
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100166 int bpp, depth;
167 bool trueColour;
168 int redMax, greenMax, blueMax;
169 int redShift, greenShift, blueShift;
170
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000171 // Set up the new bitmap
172 bitmap = new_bitmap;
173 data = new_data;
174
175 // Determine the *actual* DIBSection format
176 DIBSECTION ds;
177 if (!GetObject(bitmap, sizeof(ds), &ds))
178 throw rdr::SystemException("GetObject", GetLastError());
179
180 // Correct the "stride" of the DIB
181 // *** This code DWORD aligns each row - is that right???
182 stride = width_;
183 int bytesPerRow = stride * format.bpp/8;
184 if (bytesPerRow % 4) {
185 bytesPerRow += 4 - (bytesPerRow % 4);
186 stride = (bytesPerRow * 8) / format.bpp;
187 vlog.info("adjusting DIB stride: %d to %d", width_, stride);
188 }
189
190 // Calculate the PixelFormat for the DIB
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100191 bpp = depth = ds.dsBm.bmBitsPixel;
192 trueColour = format.trueColour || format.bpp > 8;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000193
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100194 if (trueColour) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000195 // Get the truecolour format used by the DIBSection
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100196 initMaxAndShift(ds.dsBitfields[0], &redMax, &redShift);
197 initMaxAndShift(ds.dsBitfields[1], &greenMax, &greenShift);
198 initMaxAndShift(ds.dsBitfields[2], &blueMax, &blueShift);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000199
200 // Calculate the effective depth
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100201 depth = 0;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000202 Pixel bits = ds.dsBitfields[0] | ds.dsBitfields[1] | ds.dsBitfields[2];
203 while (bits) {
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100204 depth++;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000205 bits = bits >> 1;
206 }
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100207 if (depth > bpp)
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000208 throw Exception("Bad DIBSection format (depth exceeds bpp)");
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100209 }
210
211 format = PixelFormat(bpp, depth, false, trueColour,
212 redMax, greenMax, blueMax,
213 redShift, greenShift, blueShift);
214
215 if (!trueColour) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000216 // Set the DIBSection's palette
217 refreshPalette();
218 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000219 }
220}
221
222void DIBSectionBuffer::refreshPalette() {
223 if (format.bpp > 8) {
224 vlog.error("refresh palette called for truecolor DIB");
225 return;
226 }
227 vlog.debug("refreshing palette");
228 if (device)
229 copyPaletteToDIB(palette, device, bitmap);
230 else
231 copyPaletteToDIB(palette, WindowDC(window), bitmap);
232}
233
234