You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DIBSectionBuffer.cxx 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2014 Pierre Ossman for Cendio AB
  3. *
  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. #include <rfb_win32/DIBSectionBuffer.h>
  20. #include <rfb_win32/DeviceContext.h>
  21. #include <rfb_win32/BitmapInfo.h>
  22. #include <rfb/LogWriter.h>
  23. using namespace rfb;
  24. using namespace win32;
  25. static LogWriter vlog("DIBSectionBuffer");
  26. DIBSectionBuffer::DIBSectionBuffer(HWND window_)
  27. : bitmap(0), window(window_), device(0) {
  28. }
  29. DIBSectionBuffer::DIBSectionBuffer(HDC device_)
  30. : bitmap(0), window(0), device(device_) {
  31. }
  32. DIBSectionBuffer::~DIBSectionBuffer() {
  33. if (bitmap)
  34. DeleteObject(bitmap);
  35. }
  36. inline void initMaxAndShift(DWORD mask, int* max, int* shift) {
  37. for ((*shift) = 0; (mask & 1) == 0; (*shift)++) mask >>= 1;
  38. (*max) = (rdr::U16)mask;
  39. }
  40. void DIBSectionBuffer::initBuffer(const PixelFormat& pf, int w, int h) {
  41. HBITMAP new_bitmap = 0;
  42. rdr::U8* new_data = 0;
  43. if (!pf.trueColour)
  44. throw rfb::Exception("palette format not supported");
  45. format = pf;
  46. if (w && h && (format.depth != 0)) {
  47. BitmapInfo bi;
  48. memset(&bi, 0, sizeof(bi));
  49. UINT iUsage = DIB_RGB_COLORS;
  50. bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  51. bi.bmiHeader.biBitCount = format.bpp;
  52. bi.bmiHeader.biSizeImage = (format.bpp / 8) * w * h;
  53. bi.bmiHeader.biPlanes = 1;
  54. bi.bmiHeader.biWidth = w;
  55. bi.bmiHeader.biHeight = -h;
  56. bi.bmiHeader.biCompression = (format.bpp > 8) ? BI_BITFIELDS : BI_RGB;
  57. bi.mask.red = format.pixelFromRGB((rdr::U16)~0, 0, 0);
  58. bi.mask.green = format.pixelFromRGB(0, (rdr::U16)~0, 0);
  59. bi.mask.blue = format.pixelFromRGB(0, 0, (rdr::U16)~0);
  60. // Create a DIBSection to draw into
  61. if (device)
  62. new_bitmap = ::CreateDIBSection(device, (BITMAPINFO*)&bi.bmiHeader, iUsage,
  63. (void**)&new_data, NULL, 0);
  64. else
  65. new_bitmap = ::CreateDIBSection(WindowDC(window), (BITMAPINFO*)&bi.bmiHeader, iUsage,
  66. (void**)&new_data, NULL, 0);
  67. if (!new_bitmap) {
  68. int err = GetLastError();
  69. throw rdr::SystemException("unable to create DIB section", err);
  70. }
  71. vlog.debug("recreateBuffer()");
  72. } else {
  73. vlog.debug("one of area or format not set");
  74. }
  75. if (new_bitmap && bitmap) {
  76. vlog.debug("preserving bitmap contents");
  77. // Copy the contents across
  78. if (device) {
  79. BitmapDC src_dev(device, bitmap);
  80. BitmapDC dest_dev(device, new_bitmap);
  81. BitBlt(dest_dev, 0, 0, w, h, src_dev, 0, 0, SRCCOPY);
  82. } else {
  83. WindowDC wndDC(window);
  84. BitmapDC src_dev(wndDC, bitmap);
  85. BitmapDC dest_dev(wndDC, new_bitmap);
  86. BitBlt(dest_dev, 0, 0, w, h, src_dev, 0, 0, SRCCOPY);
  87. }
  88. }
  89. if (bitmap) {
  90. // Delete the old bitmap
  91. DeleteObject(bitmap);
  92. bitmap = 0;
  93. setBuffer(0, 0, NULL, 0);
  94. }
  95. if (new_bitmap) {
  96. int bpp, depth;
  97. int redMax, greenMax, blueMax;
  98. int redShift, greenShift, blueShift;
  99. int new_stride;
  100. // Set up the new bitmap
  101. bitmap = new_bitmap;
  102. // Determine the *actual* DIBSection format
  103. DIBSECTION ds;
  104. if (!GetObject(bitmap, sizeof(ds), &ds))
  105. throw rdr::SystemException("GetObject", GetLastError());
  106. // Correct the "stride" of the DIB
  107. // *** This code DWORD aligns each row - is that right???
  108. new_stride = w;
  109. int bytesPerRow = new_stride * format.bpp/8;
  110. if (bytesPerRow % 4) {
  111. bytesPerRow += 4 - (bytesPerRow % 4);
  112. new_stride = (bytesPerRow * 8) / format.bpp;
  113. vlog.info("adjusting DIB stride: %d to %d", w, new_stride);
  114. }
  115. setBuffer(w, h, new_data, new_stride);
  116. // Calculate the PixelFormat for the DIB
  117. bpp = depth = ds.dsBm.bmBitsPixel;
  118. // Get the truecolour format used by the DIBSection
  119. initMaxAndShift(ds.dsBitfields[0], &redMax, &redShift);
  120. initMaxAndShift(ds.dsBitfields[1], &greenMax, &greenShift);
  121. initMaxAndShift(ds.dsBitfields[2], &blueMax, &blueShift);
  122. // Calculate the effective depth
  123. depth = 0;
  124. Pixel bits = ds.dsBitfields[0] | ds.dsBitfields[1] | ds.dsBitfields[2];
  125. while (bits) {
  126. depth++;
  127. bits = bits >> 1;
  128. }
  129. if (depth > bpp)
  130. throw Exception("Bad DIBSection format (depth exceeds bpp)");
  131. format = PixelFormat(bpp, depth, false, true,
  132. redMax, greenMax, blueMax,
  133. redShift, greenShift, blueShift);
  134. }
  135. }