Files

45 lines
1.7 KiB
Swift

import Foundation
@testable import Inkless
import Testing
struct ImagingTests {
@Test func packsLeftmostDotIntoLowestBit() {
var dots = [UInt8](repeating: 0, count: 16)
dots[0] = 1
dots[9] = 1
let bitmap = DotBitmap(width: 16, height: 1, dots: dots)
#expect([UInt8](bitmap.packedRows()) == [0b0000_0001, 0b0000_0010])
}
@Test func packsGrayAsInvertedNibblesHighFirst() {
let bitmap = GrayBitmap(width: 4, height: 1, luma: [255, 0, 128, 16])
#expect([UInt8](bitmap.packedNibbles()) == [0x0F, 0x7E])
}
@Test func padsShortJobsWithPaper() {
let raster = PrintRaster.dots(.blank(rows: 10)).padded(toMinimumHeight: 90)
#expect(raster.height == 90)
#expect(raster.blackRatio == 0)
}
@Test(arguments: DitherAlgorithm.allCases)
func ditherKeepsExtremesAndAverageTone(algorithm: DitherAlgorithm) {
let side = 64
let white = algorithm.apply(to: .init(repeating: 1, count: side * side), width: side, height: side)
let black = algorithm.apply(to: .init(repeating: 0, count: side * side), width: side, height: side)
#expect(white.allSatisfy { $0 == 0 })
#expect(black.allSatisfy { $0 == 1 })
guard algorithm != .threshold else { return }
let grey = algorithm.apply(to: .init(repeating: 0.5, count: side * side), width: side, height: side)
let ratio = Double(grey.reduce(0) { $0 + Int($1) }) / Double(grey.count)
#expect(abs(ratio - 0.5) < 0.15)
}
@Test func textRendersToHeadWidth() throws {
let raster = try #require(TextRenderer.render("Milk\nBread", style: TextStyle()))
#expect(raster.width == PrintHead.dots)
#expect(raster.blackRatio > 0)
}
}