image_win.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "printing/image.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "base/win/scoped_gdi_object.h"
  8. #include "base/win/scoped_hdc.h"
  9. #include "base/win/scoped_select_object.h"
  10. #include "printing/metafile.h"
  11. #include "skia/ext/skia_utils_win.h"
  12. #include "ui/gfx/gdi_util.h" // EMF support
  13. #include "ui/gfx/geometry/rect.h"
  14. namespace printing {
  15. bool Image::LoadMetafile(const Metafile& metafile) {
  16. gfx::Rect rect(metafile.GetPageBounds(1));
  17. // Create a temporary HDC and bitmap to retrieve the rendered data.
  18. base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
  19. BITMAPV4HEADER hdr;
  20. DCHECK_EQ(rect.x(), 0);
  21. DCHECK_EQ(rect.y(), 0);
  22. DCHECK_GE(rect.width(), 0); // Metafile could be empty.
  23. DCHECK_GE(rect.height(), 0);
  24. if (rect.width() < 1 || rect.height() < 1)
  25. return false;
  26. size_ = rect.size();
  27. // The data in this `bitmap` will be tightly-packed 32-bit ARGB data.
  28. gfx::CreateBitmapV4HeaderForARGB888(rect.width(), rect.height(), &hdr);
  29. unsigned char* bits = NULL;
  30. base::win::ScopedBitmap bitmap(
  31. ::CreateDIBSection(hdc.Get(), reinterpret_cast<BITMAPINFO*>(&hdr), 0,
  32. reinterpret_cast<void**>(&bits), NULL, 0));
  33. DCHECK(bitmap.is_valid());
  34. base::win::ScopedSelectObject select_object(hdc.Get(), bitmap.get());
  35. skia::InitializeDC(hdc.Get());
  36. bool success = metafile.Playback(hdc.Get(), NULL);
  37. row_length_ = size_.width() * sizeof(uint32_t);
  38. size_t bytes = row_length_ * size_.height();
  39. DCHECK(bytes);
  40. data_.assign(bits, bits + bytes);
  41. return success;
  42. }
  43. } // namespace printing