Skip to content

Commit

Permalink
changed version scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
aslanyanhaik committed Apr 11, 2020
1 parent 1b00681 commit 7d89d01
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
12 changes: 6 additions & 6 deletions Sources/Internal/RCImageDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension RCImageDecoder {
func decode(_ image: UIImage, size: Int) throws -> [RCBit] {
self.size = size
let pixelData = UnsafeMutableRawPointer.allocate(byteCount: size * size, alignment: MemoryLayout<UInt8>.alignment)
let context = generateContext(data: pixelData)
let context = generateContext(data: pixelData, size: size)
context?.draw(image.cgImage!, in: CGRect(origin: .zero, size: CGSize(width: size, height: size)))
let bits = try process(pointer: pixelData.assumingMemoryBound(to: UInt8.self))
pixelData.deallocate()
Expand Down Expand Up @@ -168,7 +168,7 @@ extension RCImageDecoder {
}

private func fixPerspective(_ data: UnsafeMutablePointer<UInt8>, points: [CGPoint]) throws -> CGImage {
guard let context = generateContext(data: data), let cgImage = context.makeImage() else {
guard let context = generateContext(data: data, size: size), let cgImage = context.makeImage() else {
throw RCError.decoding
}
let image = UIGraphicsImageRenderer(size: CGSize(width: CGFloat(size), height: CGFloat(size))).image { context in
Expand All @@ -195,7 +195,7 @@ extension RCImageDecoder {

private func decode(_ image: CGImage) -> [RCBit] {
let pixelData = UnsafeMutableRawPointer.allocate(byteCount: image.width * image.height, alignment: MemoryLayout<UInt8>.alignment)
let context = generateContext(data: pixelData)
let context = generateContext(data: pixelData, size: image.width)
context?.draw(image, in: CGRect(origin: .zero, size: CGSize(width: image.width, height: image.height)))
let buffer = UnsafeMutableBufferPointer<UInt8>(start: pixelData.assumingMemoryBound(to: UInt8.self), count: image.width * image.height)
let data = PixelContainer(rows: image.height, items: buffer)
Expand All @@ -218,15 +218,15 @@ extension RCImageDecoder {
}
}
}
let bits = points.map { data[Int($0.x), Int($0.y)] > 200 ? RCBit.zero : RCBit.one }
let bits = points.map { data[Int($0.x), Int($0.y)] > RCConstants.pixelThreshold ? RCBit.zero : RCBit.one }
pixelData.deallocate()
return bits
}
}

extension RCImageDecoder {
private func generateContext(data: UnsafeMutableRawPointer?) -> CGContext? {
return CGContext(data: data, width: self.size, height: self.size, bitsPerComponent: 8, bytesPerRow: self.size, space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.none.rawValue)
private func generateContext(data: UnsafeMutableRawPointer?, size: Int) -> CGContext? {
return CGContext(data: data, width: size, height: size, bitsPerComponent: 8, bytesPerRow: size, space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.none.rawValue)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Public/RCCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class RCCoder {
private lazy var imageEncoder = RCImageEncoder(configuration: self.configuration)
private lazy var bitCoder = RCBitCoder(configuration: self.configuration)

public init(configuration: RCCoderConfiguration = .defaultConfiguration) {
public init(configuration: RCCoderConfiguration = .shortConfiguration) {
self.configuration = configuration
}
}
Expand Down
10 changes: 4 additions & 6 deletions Sources/Public/RCCoderConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,24 @@ public extension RCCoderConfiguration {
case v1

var maxBitesPerSection: Int {
switch self {
case .v1: return 72
}
topLevelBitesCount + middleLevelBitesCount + bottomLevelBitesCount //should be multiple of 8
}

var topLevelBitesCount: Int {
switch self {
case .v1: return 24
case .v1: return 16
}
}

var middleLevelBitesCount: Int {
switch self {
case .v1: return 24
case .v1: return 16
}
}

var bottomLevelBitesCount: Int {
switch self {
case .v1: return 24
case .v1: return 16
}
}

Expand Down

0 comments on commit 7d89d01

Please sign in to comment.