OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/gdi_util.h" | 5 #include "ui/gfx/gdi_util.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
8 | 9 |
9 namespace gfx { | 10 namespace gfx { |
10 | 11 |
11 void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { | 12 void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { |
12 CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); | 13 CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); |
13 } | 14 } |
14 | 15 |
15 void CreateBitmapHeaderWithColorDepth(int width, int height, int color_depth, | 16 void CreateBitmapHeaderWithColorDepth(int width, int height, int color_depth, |
16 BITMAPINFOHEADER* hdr) { | 17 BITMAPINFOHEADER* hdr) { |
17 // These values are shared with gfx::PlatformDevice | 18 // These values are shared with gfx::PlatformDevice |
18 hdr->biSize = sizeof(BITMAPINFOHEADER); | 19 hdr->biSize = sizeof(BITMAPINFOHEADER); |
19 hdr->biWidth = width; | 20 hdr->biWidth = width; |
20 hdr->biHeight = -height; // minus means top-down bitmap | 21 hdr->biHeight = -height; // minus means top-down bitmap |
21 hdr->biPlanes = 1; | 22 hdr->biPlanes = 1; |
22 hdr->biBitCount = color_depth; | 23 hdr->biBitCount = color_depth; |
23 hdr->biCompression = BI_RGB; // no compression | 24 hdr->biCompression = BI_RGB; // no compression |
24 hdr->biSizeImage = 0; | 25 hdr->biSizeImage = 0; |
25 hdr->biXPelsPerMeter = 1; | 26 hdr->biXPelsPerMeter = 1; |
26 hdr->biYPelsPerMeter = 1; | 27 hdr->biYPelsPerMeter = 1; |
27 hdr->biClrUsed = 0; | 28 hdr->biClrUsed = 0; |
28 hdr->biClrImportant = 0; | 29 hdr->biClrImportant = 0; |
29 } | 30 } |
30 | 31 |
31 | |
32 void CreateBitmapV4Header(int width, int height, BITMAPV4HEADER* hdr) { | 32 void CreateBitmapV4Header(int width, int height, BITMAPV4HEADER* hdr) { |
33 // Because bmp v4 header is just an extension, we just create a v3 header and | 33 // Because bmp v4 header is just an extension, we just create a v3 header and |
34 // copy the bits over to the v4 header. | 34 // copy the bits over to the v4 header. |
35 BITMAPINFOHEADER header_v3; | 35 BITMAPINFOHEADER header_v3; |
36 CreateBitmapHeader(width, height, &header_v3); | 36 CreateBitmapHeader(width, height, &header_v3); |
37 memset(hdr, 0, sizeof(BITMAPV4HEADER)); | 37 memset(hdr, 0, sizeof(BITMAPV4HEADER)); |
38 memcpy(hdr, &header_v3, sizeof(BITMAPINFOHEADER)); | 38 memcpy(hdr, &header_v3, sizeof(BITMAPINFOHEADER)); |
39 | 39 |
40 // Correct the size of the header and fill in the mask values. | 40 // Correct the size of the header and fill in the mask values. |
41 hdr->bV4Size = sizeof(BITMAPV4HEADER); | 41 hdr->bV4Size = sizeof(BITMAPV4HEADER); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 112 } |
113 | 113 |
114 // Apply scaling to the DC. | 114 // Apply scaling to the DC. |
115 bool ScaleDC(HDC dc, double scale_factor) { | 115 bool ScaleDC(HDC dc, double scale_factor) { |
116 SetGraphicsMode(dc, GM_ADVANCED); | 116 SetGraphicsMode(dc, GM_ADVANCED); |
117 XFORM xform = {0}; | 117 XFORM xform = {0}; |
118 xform.eM11 = xform.eM22 = scale_factor; | 118 xform.eM11 = xform.eM22 = scale_factor; |
119 return !!ModifyWorldTransform(dc, &xform, MWT_LEFTMULTIPLY); | 119 return !!ModifyWorldTransform(dc, &xform, MWT_LEFTMULTIPLY); |
120 } | 120 } |
121 | 121 |
| 122 void StretchDIBits(HDC hdc, int dest_x, int dest_y, int dest_w, int dest_h, |
| 123 int src_x, int src_y, int src_w, int src_h, void* pixels, |
| 124 const BITMAPINFO* bitmap_info) { |
| 125 // When blitting a rectangle that touches the bottom, left corner of the |
| 126 // bitmap, StretchDIBits looks at it top-down! For more details, see |
| 127 // http://wiki.allegro.cc/index.php?title=StretchDIBits. |
| 128 int rv; |
| 129 int bitmap_h = -bitmap_info->bmiHeader.biHeight; |
| 130 int bottom_up_src_y = bitmap_h - src_y - src_h; |
| 131 if (bottom_up_src_y == 0 && src_x == 0 && src_h != bitmap_h) { |
| 132 rv = ::StretchDIBits(hdc, |
| 133 dest_x, dest_h + dest_y - 1, dest_w, -dest_h, |
| 134 src_x, bitmap_h - src_y + 1, src_w, -src_h, |
| 135 pixels, bitmap_info, DIB_RGB_COLORS, SRCCOPY); |
| 136 } else { |
| 137 rv = ::StretchDIBits(hdc, |
| 138 dest_x, dest_y, dest_w, dest_h, |
| 139 src_x, bottom_up_src_y, src_w, src_h, |
| 140 pixels, bitmap_info, DIB_RGB_COLORS, SRCCOPY); |
| 141 } |
| 142 DCHECK(rv != GDI_ERROR); |
| 143 } |
| 144 |
122 } // namespace gfx | 145 } // namespace gfx |
OLD | NEW |