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.4KB

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