|
| 1 | +// |
| 2 | +// Orientation.swift |
| 3 | +// OpenSwiftUICore |
| 4 | +// |
| 5 | +// Audited for 6.5.4 |
| 6 | +// Status: Complete |
| 7 | + |
| 8 | +package import OpenCoreGraphicsShims |
| 9 | + |
| 10 | +// MARK: - Image.Orientation |
| 11 | + |
| 12 | +extension Image { |
| 13 | + /// The orientation of an image. |
| 14 | + /// |
| 15 | + /// Many image formats such as JPEG include orientation metadata in the |
| 16 | + /// image data. In other cases, you can specify image orientation |
| 17 | + /// in code. Properly specifying orientation is often important both for |
| 18 | + /// displaying the image and for certain kinds of image processing. |
| 19 | + /// |
| 20 | + /// In OpenSwiftUI, you provide an orientation value when initializing an |
| 21 | + /// ``Image`` from an existing |
| 22 | + /// [CGImage](https://developer.apple.com/documentation/coregraphics/cgimage). |
| 23 | + @frozen |
| 24 | + public enum Orientation: UInt8, CaseIterable, Hashable { |
| 25 | + /// A value that indicates the original pixel data matches the image's |
| 26 | + /// intended display orientation. |
| 27 | + case up = 0 |
| 28 | + |
| 29 | + /// A value that indicates a horizontal flip of the image from the |
| 30 | + /// orientation of its original pixel data. |
| 31 | + case upMirrored = 2 |
| 32 | + |
| 33 | + /// A value that indicates a 180° rotation of the image from the |
| 34 | + /// orientation of its original pixel data. |
| 35 | + case down = 6 |
| 36 | + |
| 37 | + /// A value that indicates a vertical flip of the image from the |
| 38 | + /// orientation of its original pixel data. |
| 39 | + case downMirrored = 4 |
| 40 | + |
| 41 | + /// A value that indicates a 90° counterclockwise rotation from the |
| 42 | + /// orientation of its original pixel data. |
| 43 | + case left = 1 |
| 44 | + |
| 45 | + /// A value that indicates a 90° clockwise rotation and horizontal |
| 46 | + /// flip of the image from the orientation of its original pixel |
| 47 | + /// data. |
| 48 | + case leftMirrored = 3 |
| 49 | + |
| 50 | + /// A value that indicates a 90° clockwise rotation of the image from |
| 51 | + /// the orientation of its original pixel data. |
| 52 | + case right = 7 |
| 53 | + |
| 54 | + /// A value that indicates a 90° counterclockwise rotation and |
| 55 | + /// horizontal flip from the orientation of its original pixel data. |
| 56 | + case rightMirrored = 5 |
| 57 | + |
| 58 | + /// Creates an image orientation from an EXIF orientation value. |
| 59 | + /// |
| 60 | + /// This initializer converts the standard EXIF orientation values (1-8) |
| 61 | + /// to the corresponding `Image.Orientation` cases. |
| 62 | + /// |
| 63 | + /// - Parameter exifValue: An integer representing the EXIF orientation. |
| 64 | + /// Valid values are 1 through 8, corresponding to the standard EXIF |
| 65 | + /// orientation values. |
| 66 | + /// - Returns: The corresponding orientation, or `nil` if the provided |
| 67 | + /// value is not a valid EXIF orientation value. |
| 68 | + @_spi(Private) |
| 69 | + public init?(exifValue: Int) { |
| 70 | + switch exifValue { |
| 71 | + case 1: self = .up |
| 72 | + case 2: self = .upMirrored |
| 73 | + case 3: self = .down |
| 74 | + case 4: self = .downMirrored |
| 75 | + case 5: self = .leftMirrored |
| 76 | + case 6: self = .right |
| 77 | + case 7: self = .rightMirrored |
| 78 | + case 8: self = .left |
| 79 | + default: return nil |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +extension Image.Orientation: ProtobufEnum {} |
| 86 | + |
| 87 | +// MARK: - CoreGraphics + Image.Orientation |
| 88 | + |
| 89 | +extension CGSize { |
| 90 | + package func apply(_ orientation: Image.Orientation) -> CGSize { |
| 91 | + switch orientation { |
| 92 | + case .up, .upMirrored, .down, .downMirrored: |
| 93 | + return self |
| 94 | + case .left, .leftMirrored, .right, .rightMirrored: |
| 95 | + return CGSize(width: height, height: width) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + package func unapply(_ orientation: Image.Orientation) -> CGSize { |
| 100 | + apply(orientation) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +extension CGRect { |
| 105 | + package func apply(_ orientation: Image.Orientation, in size: CGSize) -> CGRect { |
| 106 | + switch orientation { |
| 107 | + case .up: |
| 108 | + self |
| 109 | + case .upMirrored: |
| 110 | + CGRect(x: size.width - x - width, y: y, width: width, height: height) |
| 111 | + case .down: |
| 112 | + CGRect(x: size.width - x - width, y: size.height - y - height, width: width, height: height) |
| 113 | + case .downMirrored: |
| 114 | + CGRect(x: x, y: size.height - y - height, width: width, height: height) |
| 115 | + case .left: |
| 116 | + CGRect(x: size.height - y, y: x, width: height, height: width) |
| 117 | + case .leftMirrored: |
| 118 | + CGRect(x: y - height, y: x, width: height, height: width) |
| 119 | + case .right: |
| 120 | + CGRect(x: y - height, y: size.width - x - width, width: height, height: width) |
| 121 | + case .rightMirrored: |
| 122 | + CGRect(x: size.height - y, y: size.width - x - width, width: height, height: width) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + package func unapply(_ orientation: Image.Orientation, in size: CGSize) -> CGRect { |
| 127 | + switch orientation { |
| 128 | + case .up: |
| 129 | + self |
| 130 | + case .upMirrored: |
| 131 | + CGRect(x: size.width - x - width, y: y, width: width, height: height) |
| 132 | + case .down: |
| 133 | + CGRect(x: size.width - x - width, y: size.height - y - height, width: width, height: height) |
| 134 | + case .downMirrored: |
| 135 | + CGRect(x: x, y: size.height - y - height, width: width, height: height) |
| 136 | + case .left: |
| 137 | + CGRect(x: y, y: size.height - x, width: height, height: width) |
| 138 | + case .leftMirrored: |
| 139 | + CGRect(x: y, y: x + width, width: height, height: width) |
| 140 | + case .right: |
| 141 | + CGRect(x: size.width - y - height, y: x + width, width: height, height: width) |
| 142 | + case .rightMirrored: |
| 143 | + CGRect(x: size.width - y - height, y: size.height - x, width: height, height: width) |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +extension CGAffineTransform { |
| 149 | + package init(orientation: Image.Orientation, in size: CGSize) { |
| 150 | + switch orientation { |
| 151 | + case .up: |
| 152 | + self = .identity |
| 153 | + case .upMirrored: |
| 154 | + self = CGAffineTransform(a: -1, b: 0, c: 0, d: 1, tx: size.width, ty: 0) |
| 155 | + case .down: |
| 156 | + self = CGAffineTransform(a: -1, b: 0, c: 0, d: -1, tx: size.width, ty: size.height) |
| 157 | + case .downMirrored: |
| 158 | + self = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: size.height) |
| 159 | + case .left: |
| 160 | + self = CGAffineTransform(a: 0, b: -1, c: 1, d: 0, tx: 0, ty: size.height) |
| 161 | + case .leftMirrored: |
| 162 | + self = CGAffineTransform(a: 0, b: 1, c: 1, d: 0, tx: 0, ty: 0) |
| 163 | + case .right: |
| 164 | + self = CGAffineTransform(a: 0, b: 1, c: -1, d: 0, tx: size.width, ty: 0) |
| 165 | + case .rightMirrored: |
| 166 | + self = CGAffineTransform(a: 0, b: -1, c: -1, d: 0, tx: size.width, ty: size.height) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + package init(orientation: Image.Orientation, in rect: CGRect) { |
| 171 | + let orientationTransform = CGAffineTransform(orientation: orientation, in: rect.size) |
| 172 | + let translateToOrigin = CGAffineTransform(translationX: rect.x, y: rect.y) |
| 173 | + let translateBack = CGAffineTransform(translationX: -rect.x, y: -rect.y) |
| 174 | + self = translateBack.concatenating(orientationTransform).concatenating(translateToOrigin) |
| 175 | + } |
| 176 | + |
| 177 | + package mutating func apply(_ orientation: Image.Orientation, in size: CGSize) { |
| 178 | + guard orientation != .up else { return } |
| 179 | + let orientationTransform = CGAffineTransform(orientation: orientation, in: size) |
| 180 | + self = orientationTransform.concatenating(self) |
| 181 | + } |
| 182 | + |
| 183 | + package mutating func apply(_ orientation: Image.Orientation) { |
| 184 | + guard orientation != .up else { return } |
| 185 | + let orientationTransform = CGAffineTransform(orientation: orientation, in: CGSize(width: 1, height: 1)) |
| 186 | + self = orientationTransform.concatenating(self) |
| 187 | + } |
| 188 | +} |
0 commit comments