Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / color_profile_mac.mm
1 // Copyright 2014 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
5 #include "ui/gfx/color_profile.h"
6
7 #import <Cocoa/Cocoa.h>
8
9 #include "base/mac/mac_util.h"
10 #include "ui/gfx/mac/coordinate_conversion.h"
11
12 namespace {
13
14 NSScreen* GetNSScreenFromBounds(const gfx::Rect& bounds) {
15   NSScreen* screen = nil;
16   int overlap = 0;
17
18   for (NSScreen* monitor in [NSScreen screens]) {
19     gfx::Rect monitor_rect = gfx::ScreenRectFromNSRect([monitor frame]);
20     gfx::Rect overlap_rect = gfx::IntersectRects(monitor_rect, bounds);
21     int overlap_size = overlap_rect.width() * overlap_rect.height();
22     if (overlap_size > overlap) {
23       overlap = overlap_size;
24       screen = monitor;
25     }
26   }
27
28   return screen;
29 }
30
31 }  // namespace
32
33 namespace gfx {
34
35 bool GetDisplayColorProfile(const gfx::Rect& bounds,
36                             std::vector<char>* profile) {
37   DCHECK(profile->empty());
38
39   NSScreen* screen = GetNSScreenFromBounds(bounds);
40   if (!screen || bounds.IsEmpty())
41     return false;
42   NSColorSpace* color_space = [screen colorSpace];
43   if (!color_space)
44     return false;
45
46   if ([color_space isEqual:[NSColorSpace sRGBColorSpace]])
47     return true;
48   NSData* profile_data = [color_space ICCProfileData];
49   const char* data = static_cast<const char*>([profile_data bytes]);
50   size_t length = [profile_data length];
51   if (data && !gfx::InvalidColorProfileLength(length))
52     profile->assign(data, data + length);
53   return true;
54 }
55
56 void ReadColorProfile(std::vector<char>* profile) {
57   CGColorSpaceRef monitor_color_space(base::mac::GetSystemColorSpace());
58   base::ScopedCFTypeRef<CFDataRef> icc_profile(
59       CGColorSpaceCopyICCProfile(monitor_color_space));
60   if (!icc_profile)
61     return;
62   size_t length = CFDataGetLength(icc_profile);
63   if (gfx::InvalidColorProfileLength(length))
64     return;
65   const unsigned char* data = CFDataGetBytePtr(icc_profile);
66   profile->assign(data, data + length);
67 }
68
69 }  // namespace gfx