File: image_unittest_util_apple.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (61 lines) | stat: -rw-r--r-- 2,187 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/image/image_unittest_util.h"

#import <CoreGraphics/CoreGraphics.h>

#include "base/apple/scoped_cftyperef.h"
#include "base/bit_cast.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_MAC)
#import <AppKit/AppKit.h>
#elif BUILDFLAG(IS_IOS)
#import <UIKit/UIKit.h>
#endif

namespace gfx::test {

// The |x| and |y| coordinates are interpreted as scale-independent on the Mac
// and on iOS.
SkColor GetPlatformImageColor(PlatformImage image, int x, int y) {
#if BUILDFLAG(IS_MAC)
  CGImageRef image_ref = [image CGImageForProposedRect:nil
                                               context:nil
                                                 hints:nil];
  CGRect target_pixel =
      CGRectMake(x * CGImageGetWidth(image_ref) / image.size.width,
                 y * CGImageGetHeight(image_ref) / image.size.height, 1, 1);
#elif BUILDFLAG(IS_IOS)
  CGImageRef image_ref = image.CGImage;
  CGRect target_pixel = CGRectMake(x * image.scale, y * image.scale, 1, 1);
#endif

  // Start by extracting the target pixel into a 1x1 CGImage.
  base::apple::ScopedCFTypeRef<CGImageRef> pixel_image(
      CGImageCreateWithImageInRect(image_ref, target_pixel));

  // Draw that pixel into a 1x1 bitmap context.
  base::apple::ScopedCFTypeRef<CGColorSpaceRef> color_space(
      CGColorSpaceCreateDeviceRGB());
  base::apple::ScopedCFTypeRef<CGContextRef> bitmap_context(
      CGBitmapContextCreate(
          /*data=*/nullptr,
          /*width=*/1,
          /*height=*/1,
          /*bitsPerComponent=*/8,
          /*bytesPerRow=*/4, color_space.get(),
          kCGImageAlphaPremultipliedFirst |
              static_cast<CGImageAlphaInfo>(kCGBitmapByteOrder32Host)));
  CGContextDrawImage(bitmap_context.get(), CGRectMake(0, 0, 1, 1),
                     pixel_image.get());

  // The CGBitmapContext has the same memory layout as SkColor, so we can just
  // read an SkColor straight out of the context.
  return *reinterpret_cast<SkColor*>(
      CGBitmapContextGetData(bitmap_context.get()));
}

}  // namespace gfx::test