您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DIBSectionBuffer.cxx 5.2KB

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