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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <rfb/LogWriter.h>
  19. #include <rfb_win32/DIBSectionBuffer.h>
  20. #include <rfb_win32/Win32Util.h>
  21. using namespace rfb;
  22. using namespace win32;
  23. static LogWriter vlog("DIBSection");
  24. DIBSectionBuffer::DIBSectionBuffer(HWND window_)
  25. : bitmap(0), device(0), window(window_) {
  26. memset(&format, 0, sizeof(format));
  27. memset(palette, 0, sizeof(palette));
  28. }
  29. DIBSectionBuffer::DIBSectionBuffer(HDC device_)
  30. : bitmap(0), window(0), device(device_) {
  31. memset(&format, 0, sizeof(format));
  32. memset(palette, 0, sizeof(palette));
  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. format = pf;
  44. recreateBuffer();
  45. if ((pf.bpp <= 8) && pf.trueColour) {
  46. vlog.debug("creating %d-bit TrueColor palette", pf.depth);
  47. for (int i=0; i < (1<<(pf.depth)); i++) {
  48. palette[i].b = ((((i >> pf.blueShift) & pf.blueMax) * 65535) + pf.blueMax/2) / pf.blueMax;
  49. palette[i].g = ((((i >> pf.greenShift) & pf.greenMax) * 65535) + pf.greenMax/2) / pf.greenMax;
  50. palette[i].r = ((((i >> pf.redShift) & pf.redMax) * 65535) + pf.redMax/2) / pf.redMax;
  51. }
  52. refreshPalette();
  53. }
  54. }
  55. void DIBSectionBuffer::setSize(int w, int h) {
  56. if (width_ == w && height_ == h) {
  57. vlog.debug("size unchanged by setSize()");
  58. return;
  59. }
  60. width_ = w;
  61. height_ = h;
  62. recreateBuffer();
  63. }
  64. // * copyPaletteToDIB MUST NEVER be called on a truecolour DIB! *
  65. void copyPaletteToDIB(Colour palette[256], HDC wndDC, HBITMAP dib) {
  66. BitmapDC dibDC(wndDC, dib);
  67. RGBQUAD rgb[256];
  68. for (unsigned int i=0;i<256;i++) {
  69. rgb[i].rgbRed = palette[i].r >> 8;
  70. rgb[i].rgbGreen = palette[i].g >> 8;
  71. rgb[i].rgbBlue = palette[i].b >> 8;
  72. }
  73. if (!SetDIBColorTable(dibDC, 0, 256, (RGBQUAD*) rgb))
  74. throw rdr::SystemException("unable to SetDIBColorTable", GetLastError());
  75. }
  76. inline void initMaxAndShift(DWORD mask, int* max, int* shift) {
  77. for ((*shift) = 0; (mask & 1) == 0; (*shift)++) mask >>= 1;
  78. (*max) = (rdr::U16)mask;
  79. }
  80. void DIBSectionBuffer::recreateBuffer() {
  81. HBITMAP new_bitmap = 0;
  82. rdr::U8* new_data = 0;
  83. if (width_ && height_ && (format.depth != 0)) {
  84. BitmapInfo bi;
  85. memset(&bi, 0, sizeof(bi));
  86. // *** wrong?
  87. UINT iUsage = format.trueColour ? DIB_RGB_COLORS : DIB_PAL_COLORS;
  88. // ***
  89. bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  90. bi.bmiHeader.biBitCount = format.bpp;
  91. bi.bmiHeader.biSizeImage = (format.bpp / 8) * width_ * height_;
  92. bi.bmiHeader.biPlanes = 1;
  93. bi.bmiHeader.biWidth = width_;
  94. bi.bmiHeader.biHeight = -height_;
  95. bi.bmiHeader.biCompression = (format.bpp > 8) ? BI_BITFIELDS : BI_RGB;
  96. bi.mask.red = format.redMax << format.redShift;
  97. bi.mask.green = format.greenMax << format.greenShift;
  98. bi.mask.blue = format.blueMax << format.blueShift;
  99. vlog.debug("area=%d, bpp=%d", width_ * height_, format.bpp);
  100. // Create a DIBSection to draw into
  101. if (device)
  102. new_bitmap = ::CreateDIBSection(device, (BITMAPINFO*)&bi.bmiHeader, iUsage,
  103. (void**)&new_data, NULL, 0);
  104. else
  105. new_bitmap = ::CreateDIBSection(WindowDC(window), (BITMAPINFO*)&bi.bmiHeader, iUsage,
  106. (void**)&new_data, NULL, 0);
  107. if (!new_bitmap) {
  108. int err = GetLastError();
  109. throw rdr::SystemException("unable to create DIB section", err);
  110. }
  111. vlog.debug("recreateBuffer()");
  112. } else {
  113. vlog.debug("one of area or format not set");
  114. }
  115. if (new_bitmap && bitmap) {
  116. vlog.debug("preserving bitmap contents");
  117. // Copy the contents across
  118. if (device) {
  119. if (format.bpp <= 8)
  120. copyPaletteToDIB(palette, device, new_bitmap);
  121. BitmapDC src_dev(device, bitmap);
  122. BitmapDC dest_dev(device, new_bitmap);
  123. BitBlt(dest_dev, 0, 0, width_, height_, src_dev, 0, 0, SRCCOPY);
  124. } else {
  125. WindowDC wndDC(window);
  126. if (format.bpp <= 8)
  127. copyPaletteToDIB(palette, wndDC, new_bitmap);
  128. BitmapDC src_dev(wndDC, bitmap);
  129. BitmapDC dest_dev(wndDC, new_bitmap);
  130. BitBlt(dest_dev, 0, 0, width_, height_, src_dev, 0, 0, SRCCOPY);
  131. }
  132. }
  133. if (bitmap) {
  134. // Delete the old bitmap
  135. DeleteObject(bitmap);
  136. bitmap = 0;
  137. data = 0;
  138. }
  139. if (new_bitmap) {
  140. // Set up the new bitmap
  141. bitmap = new_bitmap;
  142. data = new_data;
  143. // Determine the *actual* DIBSection format
  144. DIBSECTION ds;
  145. if (!GetObject(bitmap, sizeof(ds), &ds))
  146. throw rdr::SystemException("GetObject", GetLastError());
  147. // Correct the "stride" of the DIB
  148. // *** This code DWORD aligns each row - is that right???
  149. stride = width_;
  150. int bytesPerRow = stride * format.bpp/8;
  151. if (bytesPerRow % 4) {
  152. bytesPerRow += 4 - (bytesPerRow % 4);
  153. stride = (bytesPerRow * 8) / format.bpp;
  154. vlog.info("adjusting DIB stride: %d to %d", width_, stride);
  155. }
  156. // Calculate the PixelFormat for the DIB
  157. format.bigEndian = 0;
  158. format.bpp = format.depth = ds.dsBm.bmBitsPixel;
  159. format.trueColour = format.trueColour || format.bpp > 8;
  160. if (format.bpp > 8) {
  161. // Get the truecolour format used by the DIBSection
  162. initMaxAndShift(ds.dsBitfields[0], &format.redMax, &format.redShift);
  163. initMaxAndShift(ds.dsBitfields[1], &format.greenMax, &format.greenShift);
  164. initMaxAndShift(ds.dsBitfields[2], &format.blueMax, &format.blueShift);
  165. // Calculate the effective depth
  166. format.depth = 0;
  167. Pixel bits = ds.dsBitfields[0] | ds.dsBitfields[1] | ds.dsBitfields[2];
  168. while (bits) {
  169. format.depth++;
  170. bits = bits >> 1;
  171. }
  172. } else {
  173. // Set the DIBSection's palette
  174. refreshPalette();
  175. }
  176. }
  177. }
  178. void DIBSectionBuffer::refreshPalette() {
  179. if (format.bpp > 8) {
  180. vlog.error("refresh palette called for truecolor DIB");
  181. return;
  182. }
  183. vlog.debug("refreshing palette");
  184. if (device)
  185. copyPaletteToDIB(palette, device, bitmap);
  186. else
  187. copyPaletteToDIB(palette, WindowDC(window), bitmap);
  188. }