-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathDecodeAdvanced.swift
147 lines (137 loc) · 4 KB
/
DecodeAdvanced.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import JPEG
import JPEGSystem
extension String
{
static
func pad(_ string:String, left count:Int) -> Self
{
.init(repeating: " ", count: count - string.count) + string
}
static
func pad(_ string:String, right count:Int) -> Self
{
string + .init(repeating: " ", count: count - string.count)
}
}
let path:String = "examples/decode-advanced/karlie-2019.jpg"
guard let spectral:JPEG.Data.Spectral<JPEG.Common> = try .decompress(path: path)
else
{
fatalError("failed to open file '\(path)'")
}
print("""
'\(path)' (\(spectral.layout.format))
{
size : (\(spectral.size.x), \(spectral.size.y))
process : \(spectral.layout.process)
precision : \(spectral.layout.format.precision)
components :
[
\(spectral.layout.residents.sorted(by: { $0.key < $1.key }).map
{
let (component, qi):(JPEG.Component, JPEG.Table.Quantization.Key) =
spectral.layout.planes[$0.value]
return "\($0.key): (\(component.factor.x), \(component.factor.y), qi: \(qi))"
}.joined(separator: "\n "))
]
scans :
[
\(spectral.layout.scans.map
{
"[band: \($0.band), bits: \($0.bits)]: \($0.components.map(\.ci))"
}.joined(separator: "\n "))
]
}
""")
for metadata:JPEG.Metadata in spectral.metadata
{
switch metadata
{
case .application(let a, data: let data):
Swift.print("metadata (application \(a), \(data.count) bytes)")
case .comment(data: let data):
Swift.print("""
comment
{
'\(String.init(decoding: data, as: Unicode.UTF8.self))'
}
""")
case .jfif(let jfif):
Swift.print(jfif)
case .exif(let exif):
Swift.print(exif)
if let (type, count, box):(JPEG.EXIF.FieldType, Int, JPEG.EXIF.Box) = exif[tag: 315],
case .ascii = type
{
let artist:String = .init(decoding: (0 ..< count).map
{
exif[box.asOffset + $0, as: UInt8.self]
}, as: Unicode.ASCII.self)
print("artist: \(artist)")
}
}
}
let keys:Set<JPEG.Table.Quantization.Key> = .init(spectral.layout.planes.map(\.qi))
for qi:JPEG.Table.Quantization.Key in keys.sorted()
{
let q:Int = spectral.quanta.index(forKey: qi)
let table:JPEG.Table.Quantization = spectral.quanta[q]
print("quantization table \(qi):")
print("""
┌ \(String.init(repeating: " ", count: 4 * 8)) ┐
\((0 ..< 8).map
{
(h:Int) in
"""
│ \((0 ..< 8).map
{
(k:Int) in
String.pad("\(table[z: JPEG.Table.Quantization.z(k: k, h: h)]) ", left: 4)
}.joined()) │
"""
}.joined(separator: "\n"))
└ \(String.init(repeating: " ", count: 4 * 8)) ┘
""")
}
let planar:JPEG.Data.Planar<JPEG.Common> = spectral.idct()
for (p, plane):(Int, JPEG.Data.Planar<JPEG.Common>.Plane) in planar.enumerated()
{
print("""
plane \(p)
{
size: (\(plane.size.x), \(plane.size.y))
}
""")
let samples:[UInt8] = plane.indices.map
{
(i:(x:Int, y:Int)) in
.init(clamping: plane[x: i.x, y: i.y])
}
let planepath:String = "\(path)-\(p).\(plane.size.x)x\(plane.size.y).gray"
guard let _:Void = (System.File.Destination.open(path: planepath)
{
guard let _:Void = $0.write(samples)
else
{
fatalError("failed to write to file '\(planepath)'")
}
})
else
{
fatalError("failed to open file '\(planepath)'")
}
}
let rectangular:JPEG.Data.Rectangular<JPEG.Common> = planar.interleaved(cosite: false)
let rgb:[JPEG.RGB] = rectangular.unpack(as: JPEG.RGB.self)
guard let _:Void = (System.File.Destination.open(path: "\(path).rgb")
{
guard let _:Void = $0.write(rgb.flatMap{ [$0.r, $0.g, $0.b] })
else
{
fatalError("failed to write to file '\(path).rgb'")
}
})
else
{
fatalError("failed to open file '\(path).rgb'")
}