aboutsummaryrefslogtreecommitdiffstats
path: root/win/rfb_win32/DIBSectionBuffer.cxx
blob: 0c266b00643ebb92792e3d6b7c40d033463f80e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * Copyright 2014 Pierre Ossman for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#include <rfb_win32/DIBSectionBuffer.h>
#include <rfb_win32/DeviceContext.h>
#include <rfb_win32/BitmapInfo.h>
#include <rfb/LogWriter.h>

using namespace rfb;
using namespace win32;

static LogWriter vlog("DIBSectionBuffer");


DIBSectionBuffer::DIBSectionBuffer(HWND window_)
  : bitmap(0), window(window_), device(0) {
  memset(&format, 0, sizeof(format));
  memset(palette, 0, sizeof(palette));
}

DIBSectionBuffer::DIBSectionBuffer(HDC device_)
  : bitmap(0), window(0), device(device_) {
  memset(&format, 0, sizeof(format));
  memset(palette, 0, sizeof(palette));
}

DIBSectionBuffer::~DIBSectionBuffer() {
  if (bitmap)
    DeleteObject(bitmap);
}


void DIBSectionBuffer::setPF(const PixelFormat& pf) {
  if (memcmp(&getPF(), &pf, sizeof(pf)) == 0) {
    vlog.debug("pixel format unchanged by setPF()");
    return;
  }
  format = pf;
  recreateBuffer();
  if ((pf.bpp <= 8) && pf.trueColour) {
    vlog.info("creating %d-bit TrueColour palette", pf.depth);
    for (int i=0; i < (1<<(pf.depth)); i++) {
      rdr::U16 r, g, b;
      pf.rgbFromPixel(i, NULL, &r, &g, &b);
      palette[i].r = r;
      palette[i].g = g;
      palette[i].b = b;
    }
    refreshPalette();
  }
}

void DIBSectionBuffer::setSize(int w, int h) {
  if (width_ == w && height_ == h) {
    vlog.debug("size unchanged by setSize()");
    return;
  }
  width_ = w;
  height_ = h;
  recreateBuffer();
}


// * copyPaletteToDIB MUST NEVER be called on a truecolour DIB! *

void copyPaletteToDIB(Colour palette[256], HDC wndDC, HBITMAP dib) {
  BitmapDC dibDC(wndDC, dib);
  RGBQUAD rgb[256];
  for (unsigned int i=0;i<256;i++) {
    rgb[i].rgbRed = palette[i].r >> 8;
    rgb[i].rgbGreen = palette[i].g >> 8;
    rgb[i].rgbBlue = palette[i].b >> 8;
  }
  if (!SetDIBColorTable(dibDC, 0, 256, (RGBQUAD*) rgb))
    throw rdr::SystemException("unable to SetDIBColorTable", GetLastError());
}

inline void initMaxAndShift(DWORD mask, int* max, int* shift) {
  for ((*shift) = 0; (mask & 1) == 0; (*shift)++) mask >>= 1;
  (*max) = (rdr::U16)mask;
}

void DIBSectionBuffer::recreateBuffer() {
  HBITMAP new_bitmap = 0;
  rdr::U8* new_data = 0;

  if (width_ && height_ && (format.depth != 0)) {
    BitmapInfo bi;
    memset(&bi, 0, sizeof(bi));
    // *** wrong?
    UINT iUsage = format.trueColour ? DIB_RGB_COLORS : DIB_PAL_COLORS;
    // ***
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bi.bmiHeader.biBitCount = format.bpp;
    bi.bmiHeader.biSizeImage = (format.bpp / 8) * width_ * height_;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biWidth = width_;
    bi.bmiHeader.biHeight = -height_;
    bi.bmiHeader.biCompression = (format.bpp > 8) ? BI_BITFIELDS : BI_RGB;
    bi.mask.red = format.pixelFromRGB((rdr::U16)~0, 0, 0);
    bi.mask.green = format.pixelFromRGB(0, (rdr::U16)~0, 0);
    bi.mask.blue = format.pixelFromRGB(0, 0, (rdr::U16)~0);

    // Create a DIBSection to draw into
    if (device)
      new_bitmap = ::CreateDIBSection(device, (BITMAPINFO*)&bi.bmiHeader, iUsage,
                                      (void**)&new_data, NULL, 0);
    else
      new_bitmap = ::CreateDIBSection(WindowDC(window), (BITMAPINFO*)&bi.bmiHeader, iUsage,
                                      (void**)&new_data, NULL, 0);

    if (!new_bitmap) {
      int err = GetLastError();
      throw rdr::SystemException("unable to create DIB section", err);
    }

    vlog.debug("recreateBuffer()");
  } else {
    vlog.debug("one of area or format not set");
  }

  if (new_bitmap && bitmap) {
    vlog.debug("preserving bitmap contents");

    // Copy the contents across
    if (device) {
      if (format.bpp <= 8)
        copyPaletteToDIB(palette, device, new_bitmap);
      BitmapDC src_dev(device, bitmap);
      BitmapDC dest_dev(device, new_bitmap);
      BitBlt(dest_dev, 0, 0, width_, height_, src_dev, 0, 0, SRCCOPY);
    } else {
      WindowDC wndDC(window);
      if (format.bpp <= 8)
        copyPaletteToDIB(palette, wndDC, new_bitmap);
      BitmapDC src_dev(wndDC, bitmap);
      BitmapDC dest_dev(wndDC, new_bitmap);
      BitBlt(dest_dev, 0, 0, width_, height_, src_dev, 0, 0, SRCCOPY);
    }
  }
  
  if (bitmap) {
    // Delete the old bitmap
    DeleteObject(bitmap);
    bitmap = 0;
    data = 0;
  }

  if (new_bitmap) {
    int bpp, depth;
    bool trueColour;
    int redMax, greenMax, blueMax;
    int redShift, greenShift, blueShift;

    // Set up the new bitmap
    bitmap = new_bitmap;
    data = new_data;

    // Determine the *actual* DIBSection format
    DIBSECTION ds;
    if (!GetObject(bitmap, sizeof(ds), &ds))
      throw rdr::SystemException("GetObject", GetLastError());

    // Correct the "stride" of the DIB
    // *** This code DWORD aligns each row - is that right???
    stride = width_;
    int bytesPerRow = stride * format.bpp/8;
    if (bytesPerRow % 4) {
      bytesPerRow += 4 - (bytesPerRow % 4);
      stride = (bytesPerRow * 8) / format.bpp;
      vlog.info("adjusting DIB stride: %d to %d", width_, stride);
    }

    // Calculate the PixelFormat for the DIB
    bpp = depth = ds.dsBm.bmBitsPixel;
    trueColour = format.trueColour || format.bpp > 8;

    if (trueColour) {
      // Get the truecolour format used by the DIBSection
      initMaxAndShift(ds.dsBitfields[0], &redMax, &redShift);
      initMaxAndShift(ds.dsBitfields[1], &greenMax, &greenShift);
      initMaxAndShift(ds.dsBitfields[2], &blueMax, &blueShift);

      // Calculate the effective depth
      depth = 0;
      Pixel bits = ds.dsBitfields[0] | ds.dsBitfields[1] | ds.dsBitfields[2];
      while (bits) {
        depth++;
        bits = bits >> 1;
      }
      if (depth > bpp)
        throw Exception("Bad DIBSection format (depth exceeds bpp)");
    }

    format = PixelFormat(bpp, depth, false, trueColour,
                         redMax, greenMax, blueMax,
                         redShift, greenShift, blueShift);

    if (!trueColour) {
      // Set the DIBSection's palette
      refreshPalette();
    }
  }
}

void DIBSectionBuffer::refreshPalette() {
  if (format.bpp > 8) {
    vlog.error("refresh palette called for truecolor DIB");
    return;
  }
  vlog.debug("refreshing palette");
  if (device)
    copyPaletteToDIB(palette, device, bitmap);
  else
    copyPaletteToDIB(palette, WindowDC(window), bitmap);
}