Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriyvan committed Oct 4, 2022
1 parent 4013679 commit 13fd435
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
7 changes: 7 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
59 changes: 31 additions & 28 deletions Tests/geometrizeTests/Bitmap+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Algorithms
@testable import geometrize

extension Bitmap {

func pngData() throws -> Data {
let rgba: [PNG.RGBA<UInt8>] = backing.chunks(ofCount: 4).map {
PNG.RGBA(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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?,
Expand All @@ -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")
}
Expand All @@ -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
Expand Down

0 comments on commit 13fd435

Please sign in to comment.