Cotton code
67 lines · 3.5 KB · SwiftRaw
// THE PICTURE A LOOK IS MADE FROM (coordinator, 27 Nov, NEXT-SMALL-PICTURE.md). A real library already holds small// derivatives, and `.fastFormat` answers with whatever it has: on the Mac's real photos every one of 2,889 pictures// came back under 128 px when 360 was asked, every look was "small" for the core (FULL_FROM 256) and no face got a// vector. So a look never silently keeps an undersized picture.//// The rule, a pure function so it is tested without PhotoKit://   1. Ask for high quality first (`.highQualityFormat` at the size needed, drawn from what the phone holds; the//      network stays off unless the caller is the Wi-Fi pass). On the Mac's real photos it gave 360 px or more for all//      but 12 of 2,839, where `.fastFormat` gave under 128 px every time (coordinator, NEXT-GROUPING.md).//   2. Check the long edge of what came back against what is enough: the core's FULL_FROM (256 px), or the original's//      own long edge when the original is smaller than that.//   3. Short of it, ask the other way (`.fastFormat`: now and then the phone holds a bigger derivative than it draws).//   4. Still short: the look is made from the largest picture that came, and recorded as SMALL, owed a closer look//      (`AlbumsItem.State.small`), counted on its own and asked again when a bigger picture can be had.#if os(iOS) || ALBUMS_HARNESSimport CoreGraphicsimport Foundationpublic enum LocalPicture: @unchecked Sendable {    /// A picture large enough to look at fully. `degraded`: PhotoKit marked it a stand-in.    case picture(CGImage, degraded: Bool)    /// The largest picture the phone gave, short of enough: a look is made from it, owed a closer one.    case small(CGImage, degraded: Bool)    /// No picture on the phone: only in iCloud (Optimise Storage).    case onlyInCloud    case failed(String)}/// In the order asked.public enum AlbumsPictureMode: String, Sendable, CaseIterable {    case highQuality, fast}enum AlbumsPicture {    /// The core's FULL_FROM (crates/cotton-core/src/albums/look.rs): a look at full detail needs this long edge.    static let full = 256    /// What is enough for a photo whose original has `originalLongEdge` pixels on its long edge.    static func enough(originalLongEdge: Int) -> Int {        originalLongEdge > 0 ? min(full, originalLongEdge) : full    }    /// The picture for one look, asking each way in turn until one is enough.    static func ask(originalLongEdge: Int, fetch: (AlbumsPictureMode) async -> LocalPicture) async -> LocalPicture {        let enough = enough(originalLongEdge: originalLongEdge)        var best: (image: CGImage, degraded: Bool)?        var nothing: LocalPicture = .failed("not asked")        for mode in AlbumsPictureMode.allCases {            switch await fetch(mode) {            case .picture(let image, let degraded), .small(let image, let degraded):                if max(image.width, image.height) >= enough { return .picture(image, degraded: degraded) }                if best.map({ max($0.image.width, $0.image.height) < max(image.width, image.height) }) ?? true {                    best = (image, degraded)                }            case .onlyInCloud:                nothing = .onlyInCloud            case .failed(let why):                if case .failed = nothing { nothing = .failed(why) }            }        }        // Short of enough every way: the largest that came, owed a closer look; else what said why nothing came.        if let best { return .small(best.image, degraded: best.degraded) }        return nothing    }}#endif