-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathInMemory.swift
95 lines (86 loc) · 2.02 KB
/
InMemory.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
import JPEG
import JPEGSystem
extension System
{
struct Blob
{
private(set)
var data:[UInt8],
position:Int
}
}
extension System.Blob:JPEG.Bytestream.Source, JPEG.Bytestream.Destination
{
init(_ data:[UInt8])
{
self.data = data
self.position = data.startIndex
}
mutating
func read(count:Int) -> [UInt8]?
{
guard self.position + count <= data.endIndex
else
{
return nil
}
defer
{
self.position += count
}
return .init(self.data[self.position ..< self.position + count])
}
mutating
func write(_ bytes:[UInt8]) -> Void?
{
self.data.append(contentsOf: bytes)
return ()
}
}
let path:String = "examples/in-memory/karlie-2011.jpg"
guard let data:[UInt8] = (System.File.Source.open(path: path)
{
(source:inout System.File.Source) -> [UInt8]? in
guard let count:Int = source.count
else
{
return nil
}
return source.read(count: count)
} ?? nil)
else
{
fatalError("failed to open or read file '\(path)'")
}
var blob:System.Blob = .init(data)
// read from blob
let spectral:JPEG.Data.Spectral<JPEG.Common> = try .decompress(stream: &blob)
let image:JPEG.Data.Rectangular<JPEG.Common> = spectral.idct().interleaved()
let rgb:[JPEG.RGB] = image.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'")
}
// write to blob
blob = .init([])
try spectral.compress(stream: &blob)
guard let _:Void = (System.File.Destination.open(path: "\(path).jpg")
{
guard let _:Void = $0.write(blob.data)
else
{
fatalError("failed to write to file '\(path).jpg'")
}
})
else
{
fatalError("failed to open file '\(path).jpg'")
}