Skip to content

Commit

Permalink
refactored decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
aslanyanhaik committed Apr 10, 2020
1 parent 9bd1a31 commit 1b00681
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
30 changes: 16 additions & 14 deletions Sources/Internal/RCImageDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ final class RCImageDecoder {

var size: Int
var padding = 0
let configuration: RCCoderConfiguration
private let sectionSize: Int

init(size: Int) {
init(size: Int, configuration: RCCoderConfiguration) {
self.size = size
sectionSize = size / 5
self.configuration = configuration
}
}

Expand Down Expand Up @@ -198,20 +200,20 @@ extension RCImageDecoder {
let buffer = UnsafeMutableBufferPointer<UInt8>(start: pixelData.assumingMemoryBound(to: UInt8.self), count: image.width * image.height)
let data = PixelContainer(rows: image.height, items: buffer)
let size = CGFloat(data.columns)
let lineWidth = size * RCConstants.dotSizeScale / 11 * 2 //number of lines including spaces
let mainRadius = (size - lineWidth) / 2
let startAngle = asin(size * RCConstants.dotSizeScale / mainRadius)
let distancePerBit = (CGFloat.pi / 2 - startAngle * 2) / CGFloat(RCConstants.level1BitesCount)
let lineWidth = CGFloat(image.height) * RCConstants.dotSizeScale / 5 //number of lines including spaces
let mainRadius = CGFloat(image.height) / 2
let startAngle = asin(CGFloat(image.height) * RCConstants.dotSizeScale / mainRadius)
var points = [CGPoint]()
[RCConstants.level1BitesCount, RCConstants.level2BitesCount, RCConstants.level3BitesCount].enumerated().forEach { row in
let radius = mainRadius - lineWidth * CGFloat(row.offset * 2)
(0..<4).forEach { group in
let baseDistance = -CGFloat.pi / 2 + CGFloat.pi / 2 * CGFloat(group) + startAngle
(0..<row.element).forEach { bitCount in
let angle = baseDistance + distancePerBit * CGFloat(bitCount) + distancePerBit / 2
let x = cos(angle) * radius
let y = sin(angle) * radius
let point = CGPoint(x: x + size / 2, y: y + size / 2)
let center = CGPoint(x: size / 2, y: size / 2)
(0...3).forEach { offset in
let angle = CGFloat(offset - 1) * CGFloat.pi / 2
zip([0.5, 2.5, 4.5].map({mainRadius - lineWidth * $0}), [configuration.version.topLevelBitesCount, configuration.version.middleLevelBitesCount, configuration.version.bottomLevelBitesCount]) .forEach { (radius, bitCount) in
let bitAngle = (CGFloat.pi / 2 - startAngle * 2) / CGFloat(bitCount)
(0..<bitCount).forEach { bitIndex in
let bitIndexAngle = startAngle + bitAngle * (CGFloat(bitIndex) + 0.5) + angle
let x = cos(bitIndexAngle) * radius
let y = sin(bitIndexAngle) * radius
let point = CGPoint(x: x + center.x, y: y + center.y)
points.append(point)
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Internal/RCImageEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ private extension RCImageEncoder {
let lineWidth = image.size * RCConstants.dotSizeScale / 5 //number of lines including spaces
let mainRadius = image.size / 2
let startAngle = asin(image.size * RCConstants.dotSizeScale / mainRadius)
let cornerRadiusAngle = asin(lineWidth / mainRadius) / 2
let center = CGPoint(x: image.size / 2, y: image.size / 2)
zip([0.5, 2.5, 4.5].map({mainRadius - lineWidth * $0}), [section.topLevel, section.middleLevel, section.bottomLevel]) .forEach { (radius, bitGroups) in
let bitAngle = (CGFloat.pi / 2 - startAngle * 2) / CGFloat(bitGroups.map({$0.count}).reduce(0, +))
bitGroups.forEach { group in
guard group.bit == .one else { return }
let startingPosition = startAngle + bitAngle * CGFloat(group.offset) + angle
let endPosition = startingPosition + bitAngle * CGFloat(group.count)
let linePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startingPosition, endAngle: endPosition, clockwise: true)
let linePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startingPosition + cornerRadiusAngle, endAngle: endPosition - cornerRadiusAngle, clockwise: true)
let cgPath = CGPath(__byStroking: linePath.cgPath, transform: nil, lineWidth: lineWidth, lineCap: .round, lineJoin: .round, miterLimit: 1.0)!
let combinedPath = UIBezierPath(cgPath: cgPath)
path.append(combinedPath)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Public/RCCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import UIKit
public final class RCCoder {

public let configuration: RCCoderConfiguration
private lazy var imageDecoder = RCImageDecoder(size: 720)
private lazy var imageDecoder = RCImageDecoder(size: 720, configuration: self.configuration)
private lazy var imageEncoder = RCImageEncoder(configuration: self.configuration)
private lazy var bitCoder = RCBitCoder(configuration: self.configuration)

Expand Down

0 comments on commit 1b00681

Please sign in to comment.