// Schneidet ein Vorschaubild unten ab: alles unter der letzten Zeile, die sich // vom Grund der Mail (#F4F3EF) unterscheidet, plus etwas Rand. import AppKit let pfad = CommandLine.arguments[1] let bild = NSBitmapImageRep(data: try! Data(contentsOf: URL(fileURLWithPath: pfad)))! let (b, h) = (bild.pixelsWide, bild.pixelsHigh) func grund(_ x: Int, _ y: Int) -> Bool { let f = bild.colorAt(x: x, y: y)!.usingColorSpace(.deviceRGB)! return abs(f.redComponent * 255 - 244) < 6 && abs(f.greenComponent * 255 - 243) < 6 && abs(f.blueComponent * 255 - 239) < 6 } var letzte = 0 zeilen: for y in stride(from: h - 1, through: 0, by: -1) { for x in stride(from: 0, to: b, by: 3) where !grund(x, y) { letzte = y; break zeilen } } let neu = min(h, letzte + 36) let ziel = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: b, pixelsHigh: neu, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)! NSGraphicsContext.saveGraphicsState() NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: ziel) bild.draw(in: NSRect(x: 0, y: 0, width: b, height: neu), from: NSRect(x: 0, y: h - neu, width: b, height: neu), operation: .copy, fraction: 1, respectFlipped: false, hints: nil) NSGraphicsContext.restoreGraphicsState() try! ziel.representation(using: .png, properties: [:])!.write(to: URL(fileURLWithPath: pfad))