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

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