Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 5 | #include "ui/gfx/gdi_util.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 9 | #include <algorithm> |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 10 | #include <memory> |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 11 | |
Tom Sepez | 978847c | 2025-03-22 03:43:59 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
tomhudson | f0cd274b | 2016-07-27 14:46:10 | [diff] [blame] | 13 | #include "skia/ext/skia_utils_win.h" |
pkasting | 7bc277b | 2014-10-13 20:58:39 | [diff] [blame] | 14 | |
| 15 | namespace gfx { |
| 16 | |
danakj | bdf1e0a | 2020-11-10 18:27:47 | [diff] [blame] | 17 | void CreateBitmapV4HeaderForARGB888(int width, |
| 18 | int height, |
| 19 | BITMAPV4HEADER* hdr) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 20 | // Because bmp v4 header is just an extension, we just create a v3 header and |
| 21 | // copy the bits over to the v4 header. |
| 22 | BITMAPINFOHEADER header_v3; |
danakj | bdf1e0a | 2020-11-10 18:27:47 | [diff] [blame] | 23 | skia::CreateBitmapHeaderForXRGB888(width, height, &header_v3); |
Tom Sepez | 978847c | 2025-03-22 03:43:59 | [diff] [blame] | 24 | UNSAFE_TODO(memset(hdr, 0, sizeof(BITMAPV4HEADER))); |
| 25 | UNSAFE_TODO(memcpy(hdr, &header_v3, sizeof(BITMAPINFOHEADER))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | |
| 27 | // Correct the size of the header and fill in the mask values. |
| 28 | hdr->bV4Size = sizeof(BITMAPV4HEADER); |
| 29 | hdr->bV4RedMask = 0x00ff0000; |
| 30 | hdr->bV4GreenMask = 0x0000ff00; |
| 31 | hdr->bV4BlueMask = 0x000000ff; |
| 32 | hdr->bV4AlphaMask = 0xff000000; |
| 33 | } |
| 34 | |
pkasting | 46a6291 | 2014-10-09 21:30:00 | [diff] [blame] | 35 | float CalculatePageScale(HDC dc, int page_width, int page_height) { |
[email protected] | 1a30dd3 | 2012-01-28 00:56:43 | [diff] [blame] | 36 | int dc_width = GetDeviceCaps(dc, HORZRES); |
| 37 | int dc_height = GetDeviceCaps(dc, VERTRES); |
| 38 | |
| 39 | // If page fits DC - no scaling needed. |
| 40 | if (dc_width >= page_width && dc_height >= page_height) |
| 41 | return 1.0; |
| 42 | |
pkasting | 46a6291 | 2014-10-09 21:30:00 | [diff] [blame] | 43 | float x_factor = |
| 44 | static_cast<float>(dc_width) / static_cast<float>(page_width); |
| 45 | float y_factor = |
| 46 | static_cast<float>(dc_height) / static_cast<float>(page_height); |
[email protected] | 1a30dd3 | 2012-01-28 00:56:43 | [diff] [blame] | 47 | return std::min(x_factor, y_factor); |
| 48 | } |
| 49 | |
| 50 | // Apply scaling to the DC. |
pkasting | 46a6291 | 2014-10-09 21:30:00 | [diff] [blame] | 51 | bool ScaleDC(HDC dc, float scale_factor) { |
[email protected] | 1a30dd3 | 2012-01-28 00:56:43 | [diff] [blame] | 52 | SetGraphicsMode(dc, GM_ADVANCED); |
| 53 | XFORM xform = {0}; |
| 54 | xform.eM11 = xform.eM22 = scale_factor; |
| 55 | return !!ModifyWorldTransform(dc, &xform, MWT_LEFTMULTIPLY); |
| 56 | } |
| 57 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 58 | } // namespace gfx |