[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/image.h" |
| 6 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include "base/macros.h" |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 11 | #include "base/win/scoped_gdi_object.h" |
| 12 | #include "base/win/scoped_hdc.h" |
| 13 | #include "base/win/scoped_select_object.h" |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 14 | #include "printing/metafile.h" |
tomhudson | 828dddb | 2015-12-04 14:34:16 | [diff] [blame] | 15 | #include "skia/ext/skia_utils_win.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 16 | #include "ui/gfx/gdi_util.h" // EMF support |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 17 | #include "ui/gfx/geometry/rect.h" |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 18 | |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 19 | namespace printing { |
| 20 | |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 21 | bool Image::LoadMetafile(const Metafile& metafile) { |
Etienne Bergeron | 1a8383b8 | 2019-04-17 19:03:18 | [diff] [blame] | 22 | gfx::Rect rect(metafile.GetPageBounds(1)); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 23 | |
| 24 | // Create a temporary HDC and bitmap to retrieve the rendered data. |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 25 | base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL)); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 26 | BITMAPV4HEADER hdr; |
| 27 | DCHECK_EQ(rect.x(), 0); |
| 28 | DCHECK_EQ(rect.y(), 0); |
| 29 | DCHECK_GE(rect.width(), 0); // Metafile could be empty. |
| 30 | DCHECK_GE(rect.height(), 0); |
| 31 | |
| 32 | if (rect.width() < 1 || rect.height() < 1) |
| 33 | return false; |
| 34 | |
| 35 | size_ = rect.size(); |
| 36 | gfx::CreateBitmapV4Header(rect.width(), rect.height(), &hdr); |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 37 | unsigned char* bits = NULL; |
| 38 | base::win::ScopedBitmap bitmap( |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 39 | ::CreateDIBSection(hdc.Get(), reinterpret_cast<BITMAPINFO*>(&hdr), 0, |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 40 | reinterpret_cast<void**>(&bits), NULL, 0)); |
anpol | 280746c | 2015-12-18 08:39:03 | [diff] [blame] | 41 | DCHECK(bitmap.is_valid()); |
| 42 | base::win::ScopedSelectObject select_object(hdc.Get(), bitmap.get()); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 43 | |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 44 | skia::InitializeDC(hdc.Get()); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 45 | |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 46 | bool success = metafile.Playback(hdc.Get(), NULL); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 47 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 48 | row_length_ = size_.width() * sizeof(uint32_t); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 49 | size_t bytes = row_length_ * size_.height(); |
| 50 | DCHECK(bytes); |
| 51 | |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 52 | data_.assign(bits, bits + bytes); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 53 | |
| 54 | return success; |
| 55 | } |
| 56 | |
| 57 | } // namespace printing |