Updating trunk VERSION from 903.0 to 904.0
[chromium-blink-merge.git] / printing / image_mac.cc
blob1ed9e0c642628b7fde3c38a9380bcd58cefadbce
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.
5 #include "printing/image.h"
7 #include <ApplicationServices/ApplicationServices.h>
9 #include "base/mac/scoped_cftyperef.h"
10 #include "printing/metafile.h"
11 #include "ui/gfx/rect.h"
13 namespace printing {
15 bool Image::LoadMetafile(const Metafile& metafile) {
16 // The printing system uses single-page metafiles (page indexes are 1-based).
17 const unsigned int page_number = 1;
18 gfx::Rect rect(metafile.GetPageBounds(page_number));
19 if (rect.width() < 1 || rect.height() < 1)
20 return false;
22 size_ = rect.size();
23 row_length_ = size_.width() * sizeof(uint32);
24 size_t bytes = row_length_ * size_.height();
25 DCHECK(bytes);
27 data_.resize(bytes);
28 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space(
29 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
30 base::mac::ScopedCFTypeRef<CGContextRef> bitmap_context(
31 CGBitmapContextCreate(&*data_.begin(), size_.width(), size_.height(),
32 8, row_length_, color_space,
33 kCGImageAlphaPremultipliedLast));
34 DCHECK(bitmap_context.get());
36 metafile.RenderPage(page_number, bitmap_context,
37 CGRectMake(0, 0, size_.width(), size_.height()),
38 true, false, false, false);
40 return true;
43 } // namespace printing