diff --git a/.swiftlint.yml b/.swiftlint.yml index 5faa84b..792d9a6 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -8,3 +8,10 @@ identifier_name: warning: 0 large_tuple: 5 + +line_length: + warning: 200 + error: 500 + ignores_function_declarations: false + ignores_comments: false + ignores_urls: true diff --git a/Tests/geometrizeTests/Bitmap+Extension.swift b/Tests/geometrizeTests/Bitmap+Extension.swift index 60096e4..7125f14 100644 --- a/Tests/geometrizeTests/Bitmap+Extension.swift +++ b/Tests/geometrizeTests/Bitmap+Extension.swift @@ -4,7 +4,7 @@ import Algorithms @testable import geometrize extension Bitmap { - + func pngData() throws -> Data { let rgba: [PNG.RGBA] = backing.chunks(ofCount: 4).map { PNG.RGBA( @@ -19,7 +19,7 @@ extension Bitmap { try image.compress(stream: &destinationStream) return Data(destinationStream.data) } - + init(pngData data: Data) throws { let bytes = data.withUnsafeBytes { rawBufferPointer in [UInt8](unsafeUninitializedCapacity: rawBufferPointer.count) { buffer, initializedCount in @@ -33,7 +33,7 @@ extension Bitmap { let bitmapData: [UInt8] = rgba.flatMap({ [$0.r, $0.g, $0.b, $0.a] }) self.init(width: image.size.0, height: image.size.1, data: bitmapData) } - + } // Borrowed https://github.com/kelvin13/swift-png/blob/0800c123d29d132cab70a1d492fb18a7e4007380/examples/decode-online/main.swift @@ -50,7 +50,7 @@ extension SourceStream: PNG.Bytestream.Source { self.position = data.startIndex self.available = data.startIndex } - + mutating func read(count: Int) -> [UInt8]? { guard position + count <= data.endIndex else { return nil } guard position + count < available else { @@ -60,17 +60,17 @@ extension SourceStream: PNG.Bytestream.Source { defer { position += count } return [UInt8](data[position ..< position + count]) } - - mutating func reset(position:Int) { + + mutating func reset(position: Int) { precondition(data.indices ~= position) self.position = position } } struct DestinationStream: PNG.Bytestream.Destination { - + private(set) var data: [UInt8] = [] - + mutating func write(_ buffer: [UInt8]) -> Void? { data.append(contentsOf: buffer) } @@ -82,8 +82,7 @@ func waitSignature(stream: inout SourceStream) throws { while true { do { return try stream.signature() - } - catch PNG.LexingError.truncatedSignature { + } catch PNG.LexingError.truncatedSignature { stream.reset(position: position) continue } @@ -95,21 +94,25 @@ func waitChunk(stream: inout SourceStream) throws -> (type: PNG.Chunk, data: [UI while true { do { return try stream.chunk() - } - catch PNG.LexingError.truncatedChunkHeader, PNG.LexingError.truncatedChunkBody { + } catch PNG.LexingError.truncatedChunkHeader, PNG.LexingError.truncatedChunkBody { stream.reset(position: position) continue } } } -func decodeOnline(stream: inout SourceStream, overdraw: Bool, capture: (PNG.Data.Rectangular) throws -> Void) throws -> PNG.Data.Rectangular { +// swiftlint:disable:next cyclomatic_complexity function_body_length +func decodeOnline( + stream: inout SourceStream, + overdraw: Bool, + capture: (PNG.Data.Rectangular) throws -> Void +) throws -> PNG.Data.Rectangular { // lex PNG signature bytes try waitSignature(stream: &stream) // lex header chunk, and preceeding cgbi chunk, if present let (standard, header): (PNG.Standard, PNG.Header) = try { var chunk: (type: PNG.Chunk, data: [UInt8]) = try waitChunk(stream: &stream) - let standard:PNG.Standard + let standard: PNG.Standard switch chunk.type { case .CgBI: standard = .ios @@ -124,9 +127,9 @@ func decodeOnline(stream: inout SourceStream, overdraw: Bool, capture: (PNG.Data fatalError("missing image header") } }() - + var chunk: (type: PNG.Chunk, data: [UInt8]) = try waitChunk(stream: &stream) - + var context: PNG.Context = try { var palette: PNG.Palette? var background: PNG.Background?, @@ -144,13 +147,13 @@ func decodeOnline(stream: inout SourceStream, overdraw: Bool, capture: (PNG.Data palette = try .init(parsing: chunk.data, pixel: header.pixel) case .IDAT: guard let context: PNG.Context = PNG.Context.init( - standard: standard, - header: header, - palette: palette, - background: background, - transparency: transparency, - metadata: metadata, - uninitialized: false) + standard: standard, + header: header, + palette: palette, + background: background, + transparency: transparency, + metadata: metadata, + uninitialized: false) else { fatalError("missing palette") } @@ -159,20 +162,20 @@ func decodeOnline(stream: inout SourceStream, overdraw: Bool, capture: (PNG.Data fatalError("unexpected chunk") default: try metadata.push(ancillary: chunk, pixel: header.pixel, - palette: palette, - background: &background, - transparency: &transparency) + palette: palette, + background: &background, + transparency: &transparency) } chunk = try waitChunk(stream: &stream) } }() - + while chunk.type == .IDAT { try context.push(data: chunk.data, overdraw: overdraw) try capture(context.image) chunk = try waitChunk(stream: &stream) } - + while true { try context.push(ancillary: chunk) guard chunk.type != .IEND