[iOS] Cleanup ios/chrome/ios_chrome.gyp
[chromium-blink-merge.git] / ui / gfx / color_profile_mac.mm
blob1d4b00ef6c09b710e1bfc61a3d4da2d697e18c50
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.
5 #include "ui/gfx/color_profile.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/mac_util.h"
10 #include "base/mac/scoped_cftyperef.h"
11 #include "ui/gfx/mac/coordinate_conversion.h"
13 namespace {
15 NSScreen* GetNSScreenFromBounds(const gfx::Rect& bounds) {
16   NSScreen* screen = nil;
17   int overlap = 0;
19   for (NSScreen* monitor in [NSScreen screens]) {
20     gfx::Rect monitor_rect = gfx::ScreenRectFromNSRect([monitor frame]);
21     gfx::Rect overlap_rect = gfx::IntersectRects(monitor_rect, bounds);
22     int overlap_size = overlap_rect.width() * overlap_rect.height();
23     if (overlap_size > overlap) {
24       overlap = overlap_size;
25       screen = monitor;
26     }
27   }
29   return screen;
32 }  // namespace
34 namespace gfx {
36 bool GetDisplayColorProfile(const gfx::Rect& bounds,
37                             std::vector<char>* profile) {
38   DCHECK(profile->empty());
40   NSScreen* screen = GetNSScreenFromBounds(bounds);
41   if (!screen || bounds.IsEmpty())
42     return false;
43   NSColorSpace* color_space = [screen colorSpace];
44   if (!color_space)
45     return false;
47   if ([color_space isEqual:[NSColorSpace sRGBColorSpace]])
48     return true;
49   NSData* profile_data = [color_space ICCProfileData];
50   const char* data = static_cast<const char*>([profile_data bytes]);
51   size_t length = [profile_data length];
52   if (data && !gfx::InvalidColorProfileLength(length))
53     profile->assign(data, data + length);
54   return true;
57 GFX_EXPORT bool GetDisplayColorProfile(gfx::NativeWindow window,
58                                        std::vector<char>* profile) {
59   DCHECK(profile->empty());
61   NSColorSpace* color_space = [window colorSpace];
62   if (!color_space || NSIsEmptyRect([window frame]))
63     return false;
65   if ([color_space isEqual:[NSColorSpace sRGBColorSpace]])
66     return true;
67   NSData* profile_data = [color_space ICCProfileData];
68   const char* data = static_cast<const char*>([profile_data bytes]);
69   size_t length = [profile_data length];
70   if (data && !gfx::InvalidColorProfileLength(length))
71     profile->assign(data, data + length);
72   return true;
75 void ReadColorProfile(std::vector<char>* profile) {
76   CGColorSpaceRef monitor_color_space(base::mac::GetSystemColorSpace());
77   base::ScopedCFTypeRef<CFDataRef> icc_profile(
78       CGColorSpaceCopyICCProfile(monitor_color_space));
79   if (!icc_profile)
80     return;
81   size_t length = CFDataGetLength(icc_profile);
82   if (gfx::InvalidColorProfileLength(length))
83     return;
84   const unsigned char* data = CFDataGetBytePtr(icc_profile);
85   profile->assign(data, data + length);
88 }  // namespace gfx