Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aslanyanhaik committed Apr 12, 2020
1 parent ccb66dc commit 55684f2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Sources/Internal/RCBitCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import Foundation

final class RCBitCoder {
struct RCBitCoder {

private let configuration: RCCoderConfiguration

Expand Down
2 changes: 1 addition & 1 deletion Sources/Internal/RCConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ struct RCConstants {
static let imageScale: CGFloat = 0.8
static let dotSizeScale: CGFloat = 0.08
static let dotPatterns: [CGFloat] = [6, 4, 2]
static let dotPointRange = (Float(1.6)...Float(2.2))
static let dotPointRange = (Float(1.3)...Float(2.5))
static let pixelThreshold = 180
}
22 changes: 8 additions & 14 deletions Sources/Internal/RCImageDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@

import UIKit

final class RCImageDecoder {
struct RCImageDecoder {

var size: Int
var padding = 0
lazy var bytesPerRow = self.size
let configuration: RCCoderConfiguration
private let sectionSize: Int

init(size: Int, configuration: RCCoderConfiguration) {
self.size = size
sectionSize = size / 5
self.configuration = configuration
internal let configuration: RCCoderConfiguration
internal var size = 720
internal var bytesPerRow = 720
internal var padding = 0
private var sectionSize: Int {
self.size / 5
}
}

Expand Down Expand Up @@ -63,9 +59,7 @@ extension RCImageDecoder {
return bits
}

func decode(_ image: UIImage, size: Int) throws -> [RCBit] {
self.size = size
self.bytesPerRow = size
func decode(_ image: UIImage) throws -> [RCBit] {
let pixelData = UnsafeMutableRawPointer.allocate(byteCount: size * size, alignment: MemoryLayout<UInt8>.alignment)
let context = generateContext(data: pixelData, size: size, bytesPerRow: self.bytesPerRow)
context?.draw(image.cgImage!, in: CGRect(origin: .zero, size: CGSize(width: size, height: size)))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Internal/RCImageEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import UIKit

final class RCImageEncoder {
struct RCImageEncoder {

let configuration: RCCoderConfiguration

Expand Down
2 changes: 1 addition & 1 deletion Sources/Internal/RCTransformation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import simd
import QuartzCore

final class RCTransformation {
struct RCTransformation {

private let vectors: [SIMD3<Double>]
private static let zeroColumnBeforeLast: double4x3 = {
Expand Down
25 changes: 8 additions & 17 deletions Sources/Public/RCCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,11 @@ import UIKit
public final class RCCoder {

public let configuration: RCCoderConfiguration
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)
internal var size: Int {
get { imageDecoder.size}
set { imageDecoder.size = newValue }
}
internal var padding: Int {
get { imageDecoder.padding }
set { imageDecoder.padding = newValue }
}
internal var bytesPerRow: Int {
get { imageDecoder.bytesPerRow }
set { imageDecoder.bytesPerRow = newValue }
}
internal lazy var imageDecoder = RCImageDecoder(configuration: self.configuration)
internal lazy var imageEncoder = RCImageEncoder(configuration: self.configuration)
internal lazy var bitCoder = RCBitCoder(configuration: self.configuration)

public init(configuration: RCCoderConfiguration = .shortConfiguration) {
public init(configuration: RCCoderConfiguration = .defaultConfiguration) {
self.configuration = configuration
}
}
Expand All @@ -55,7 +43,10 @@ public extension RCCoder {

func decode(_ image: UIImage) throws -> String {
guard image.size.width == image.size.height else { throw RCError.wrongImageSize }
let bits = try imageDecoder.decode(image, size: image.cgImage!.height)
imageDecoder.size = image.cgImage!.height
imageDecoder.padding = 0
imageDecoder.bytesPerRow = image.cgImage!.height
let bits = try imageDecoder.decode(image)
let message = try bitCoder.decode(bits)
return message
}
Expand Down

0 comments on commit 55684f2

Please sign in to comment.