-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathEncodeBasic.swift
59 lines (55 loc) · 1.78 KB
/
EncodeBasic.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import JPEG
import JPEGSystem
let path:String = "examples/encode-basic/karlie-milan-sp12-2011",
size:(x:Int, y:Int) = (400, 665)
guard let rgb:[JPEG.RGB] = (System.File.Source.open(path: "\(path).rgb")
{
guard let data:[UInt8] = $0.read(count: 3 * size.x * size.y)
else
{
fatalError("failed to read from file '\(path).rgb'")
}
return (0 ..< size.x * size.y).map
{
(i:Int) -> JPEG.RGB in
.init(data[3 * i], data[3 * i + 1], data[3 * i + 2])
}
})
else
{
fatalError("failed to open file '\(path).rgb'")
}
for factor:(luminance:(Int, Int), chrominance:(Int, Int), name:String) in
[
((1, 1), (1, 1), "4-4-4"),
((1, 2), (1, 1), "4-4-0"),
((2, 1), (1, 1), "4-2-2"),
((2, 2), (1, 1), "4-2-0"),
]
{
let layout:JPEG.Layout<JPEG.Common> = .init(
format: .ycc8,
process: .baseline,
components:
[
1: (factor: factor.luminance, qi: 0 as JPEG.Table.Quantization.Key),
2: (factor: factor.chrominance, qi: 1 as JPEG.Table.Quantization.Key),
3: (factor: factor.chrominance, qi: 1 as JPEG.Table.Quantization.Key),
],
scans:
[
.sequential((1, \.0, \.0)),
.sequential((2, \.1, \.1), (3, \.1, \.1))
])
let jfif:JPEG.JFIF = .init(version: .v1_2, density: (1, 1, .centimeters))
let image:JPEG.Data.Rectangular<JPEG.Common> =
.pack(size: size, layout: layout, metadata: [.jfif(jfif)], pixels: rgb)
for level:Double in [0.0, 0.125, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0]
{
try image.compress(path: "\(path)-\(factor.name)-\(level).jpg", quanta:
[
0: JPEG.CompressionLevel.luminance( level).quanta,
1: JPEG.CompressionLevel.chrominance(level).quanta
])
}
}