Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
SusanDoggie committed Nov 1, 2017
1 parent 81488f4 commit 3367276
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 70 deletions.
6 changes: 5 additions & 1 deletion Doggie.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
0A2107161E5BEEBB00F1E00E /* Shape.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A21070C1E5BEEBB00F1E00E /* Shape.swift */; };
0A21F0251F061AE2009C4490 /* PixelBlender.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A21F0231F061AE2009C4490 /* PixelBlender.swift */; };
0A2406741F38109900B0EC28 /* iccNamedColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2406731F38109900B0EC28 /* iccNamedColor.swift */; };
0A26A0921FA98A1B008BEADB /* CompressionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A26A0901FA98994008BEADB /* CompressionTest.swift */; };
0A2A643F1E5C45EE000E21BB /* PDFParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2A643D1E5C45EE000E21BB /* PDFParser.swift */; };
0A32D9991F28825F0027F384 /* Tensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A32D9971F28825F0027F384 /* Tensor.swift */; };
0A36612C1EEA54AB00216A26 /* CGPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A36612A1EEA54AB00216A26 /* CGPath.swift */; };
Expand Down Expand Up @@ -263,6 +264,7 @@
0A21070C1E5BEEBB00F1E00E /* Shape.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Shape.swift; sourceTree = "<group>"; };
0A21F0231F061AE2009C4490 /* PixelBlender.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PixelBlender.swift; sourceTree = "<group>"; };
0A2406731F38109900B0EC28 /* iccNamedColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iccNamedColor.swift; sourceTree = "<group>"; };
0A26A0901FA98994008BEADB /* CompressionTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompressionTest.swift; sourceTree = "<group>"; };
0A2A643D1E5C45EE000E21BB /* PDFParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PDFParser.swift; sourceTree = "<group>"; };
0A32D9971F28825F0027F384 /* Tensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tensor.swift; sourceTree = "<group>"; };
0A36612A1EEA54AB00216A26 /* CGPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CGPath.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -598,10 +600,11 @@
08AF77AE1A3A10510042491F /* Tests */ = {
isa = PBXGroup;
children = (
0A7A4BFF1F738D18005F44CD /* XMLTest.swift */,
0AFCA14F1D9E1F130034AB10 /* AtomicTest.swift */,
0A26A0901FA98994008BEADB /* CompressionTest.swift */,
08BFF9B91B0715ED00EC88CB /* FourierTest.swift */,
0A8D98A21E53ED0F0050E112 /* ImageTest.swift */,
0A7A4BFF1F738D18005F44CD /* XMLTest.swift */,
);
name = Tests;
path = Tests/DoggieTests;
Expand Down Expand Up @@ -1240,6 +1243,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0A26A0921FA98A1B008BEADB /* CompressionTest.swift in Sources */,
0A92062D1F14A29E00AF5C49 /* ImageTest.swift in Sources */,
0A7A4C001F738D18005F44CD /* XMLTest.swift in Sources */,
0ACD21331EEBA55000C5811F /* AtomicTest.swift in Sources */,
Expand Down
17 changes: 15 additions & 2 deletions Sources/Doggie/Compression/CompressionCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,21 @@ import Foundation

public protocol CompressionCodec {

func process(data: Data) throws -> Data
func process<C : RangeReplaceableCollection>(_ source: Data, _ output: inout C) throws where C.Element == UInt8

func final() throws -> Data
func final<C : RangeReplaceableCollection>(_ output: inout C) throws where C.Element == UInt8
}

extension CompressionCodec {

@_inlineable
public func process(_ source: Data) throws -> Data {

var result = Data(capacity: source.count)

try self.process(source, &result)
try self.final(&result)

return result
}
}
81 changes: 31 additions & 50 deletions Sources/Doggie/Compression/zlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,54 +109,44 @@ extension Deflate {

extension Deflate {

private func _process(_ capacity: Int, _ flag: Int32) throws -> Data {
private func _process<C : RangeReplaceableCollection>(_ flag: Int32, _ output: inout C) throws where C.Element == UInt8 {

var result = Data(capacity: capacity)
var buffer = [UInt8](repeating: 0, count: 4096)

stream.avail_out = 0

var written = 0

while stream.avail_in != 0 || stream.avail_out == 0 {
try buffer.withUnsafeMutableBufferPointer { buf in

result.count = written + 32

try result.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<Bytef>) in
repeat {

stream.next_out = bytes.advanced(by: written)
stream.avail_out = 32
stream.next_out = buf.baseAddress
stream.avail_out = 4096

let status = deflate(&stream, flag)

guard status == Z_OK || status == Z_BUF_ERROR || status == Z_STREAM_END else { throw Error(code: status, msg: stream.msg) }

written = result.count - Int(stream.avail_out)
}
output.append(contentsOf: buf.prefix(4096 - Int(stream.avail_out)))

} while stream.avail_in != 0 || stream.avail_out == 0
}

result.count -= Int(stream.avail_out)
stream.avail_out = 0

return result
}

public func process(data: Data) throws -> Data {
public func process<C : RangeReplaceableCollection>(_ source: Data, _ output: inout C) throws where C.Element == UInt8 {

return try data.withUnsafeBytes { (bytes: UnsafePointer<Bytef>) in
try source.withUnsafeBytes { (bytes: UnsafePointer<Bytef>) in

stream.next_in = UnsafeMutablePointer<Bytef>(mutating: bytes)
stream.avail_in = uInt(data.count)
stream.avail_in = uInt(source.count)

return try _process(data.count, Z_NO_FLUSH)
try _process(Z_NO_FLUSH, &output)
}
}

public func final() throws -> Data {
public func final<C : RangeReplaceableCollection>(_ output: inout C) throws where C.Element == UInt8 {

stream.next_in = nil
stream.avail_in = 0

return try _process(0, Z_FINISH)
try _process(Z_FINISH, &output)
}
}

Expand Down Expand Up @@ -204,53 +194,44 @@ extension Inflate {

extension Inflate {

private func _process(_ capacity: Int, _ flag: Int32) throws -> Data {

var result = Data(capacity: capacity)
private func _process<C : RangeReplaceableCollection>(_ flag: Int32, _ output: inout C) throws where C.Element == UInt8 {

stream.avail_out = 0
var buffer = [UInt8](repeating: 0, count: 4096)

var written = 0

while stream.avail_in != 0 || stream.avail_out == 0 {

result.count = written + 32
try buffer.withUnsafeMutableBufferPointer { buf in

try result.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<Bytef>) in
repeat {

stream.next_out = bytes.advanced(by: written)
stream.avail_out = 32
stream.next_out = buf.baseAddress
stream.avail_out = 4096

let status = inflate(&stream, flag)

guard status == Z_OK || status == Z_BUF_ERROR || status == Z_STREAM_END else { throw Error(code: status, msg: stream.msg) }

written = result.count - Int(stream.avail_out)
}
output.append(contentsOf: buf.prefix(4096 - Int(stream.avail_out)))

} while stream.avail_in != 0 || stream.avail_out == 0
}

result.count -= Int(stream.avail_out)
stream.avail_out = 0

return result
}

public func process(data: Data) throws -> Data {
public func process<C : RangeReplaceableCollection>(_ source: Data, _ output: inout C) throws where C.Element == UInt8 {

return try data.withUnsafeBytes { (bytes: UnsafePointer<Bytef>) in
try source.withUnsafeBytes { (bytes: UnsafePointer<Bytef>) in

stream.next_in = UnsafeMutablePointer<Bytef>(mutating: bytes)
stream.avail_in = uInt(data.count)
stream.avail_in = uInt(source.count)

return try _process(data.count, Z_NO_FLUSH)
try _process(Z_NO_FLUSH, &output)
}
}

public func final() throws -> Data {
public func final<C : RangeReplaceableCollection>(_ output: inout C) throws where C.Element == UInt8 {

stream.next_in = nil
stream.avail_in = 0

return try _process(0, Z_FINISH)
try _process(Z_FINISH, &output)
}
}

3 changes: 1 addition & 2 deletions Sources/Doggie/Font/Decoder/WOFFDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ struct WOFFDecoder : FontDecoder {
if record.compLength == record.origLength {
table[record.tag] = data.dropFirst(Int(record.offset)).prefix(Int(record.origLength))
} else {
let inflate = try Inflate()
table[record.tag] = try inflate.process(data: data.dropFirst(Int(record.offset)).prefix(Int(record.compLength))) + inflate.final()
table[record.tag] = try Inflate().process(data.dropFirst(Int(record.offset)).prefix(Int(record.compLength)))
}
}
self.faces = [try SFNTFontFace(table: table)]
Expand Down
9 changes: 1 addition & 8 deletions Sources/Doggie/ImageCodec/Decoder/PNGDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,7 @@ struct PNGDecoder : ImageRepDecoder {

func decompress(data: Data, compression: UInt8) -> Data? {
switch compression {
case 0:
do {
let inflate = try Inflate()
return try? inflate.process(data: data) + inflate.final()
} catch let error {
print(error)
return nil
}
case 0: return try? Inflate().process(data)
default: return nil
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Doggie/ImageCodec/Encoder/PNGEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct PNGEncoder : ImageRepEncoder {

private static func iCCP<C>(_ colorSpace: ColorSpace<C>) -> PNGChunk? {

if let iccData = colorSpace.iccData, let deflate = try? Deflate(), let data = try? deflate.process(data: iccData) + deflate.final() {
if let iccData = colorSpace.iccData, let data = try? Deflate(windowBits: 15).process(iccData) {

var iccp = Data()

Expand Down Expand Up @@ -179,7 +179,7 @@ struct PNGEncoder : ImageRepEncoder {
body(&scanline, destination.pointee)
}

compressed.append(try deflate.process(data: filter0(scanline, previous, bitsPerPixel)))
try deflate.process(filter0(scanline, previous, bitsPerPixel), &compressed)

previous = scanline
}
Expand All @@ -206,15 +206,15 @@ struct PNGEncoder : ImageRepEncoder {
buffer += 1
}

compressed.append(try deflate.process(data: filter0(scanline, previous, bitsPerPixel)))
try deflate.process(filter0(scanline, previous, bitsPerPixel), &compressed)

previous = scanline
}
}
}
}

compressed.append(try deflate.final())
try deflate.final(&compressed)

} catch {
return nil
Expand Down
4 changes: 1 addition & 3 deletions Sources/Doggie/PDF/PDFEncoding/PDFFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ fileprivate func _PDFFilterDecode(_ name: PDFDocument.Name, _ data: Data) throws
case "ASCIIHexDecode": return try PDFASCIIHexDecode(data)
case "ASCII85Decode": return try PDFASCII85Decode(data)
case "LZWDecode": return try PDFLZWDecode(data)
case "FlateDecode":
let inflate = try Inflate()
return try inflate.process(data: data) + inflate.final()
case "FlateDecode": return try Inflate().process(data)
case "RunLengthDecode": return try PDFRunLengthDecode(data)
default: return data
}
Expand Down
Loading

0 comments on commit 3367276

Please sign in to comment.