-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_imageloader.py
More file actions
66 lines (53 loc) · 2.67 KB
/
patch_imageloader.py
File metadata and controls
66 lines (53 loc) · 2.67 KB
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
import re
def update_file():
with open("Sources/HeyFoSCore/Processing/ImageLoader.swift", "r") as f:
text = f.read()
# Add baseImageProperties
prop_decl = """ private let logger = Logger(label: "com.heyfos.imageloader")
/// Stored metadata from the first loaded image to apply to the output
public var baseImageProperties: CFDictionary?
"""
text = text.replace(' private let logger = Logger(label: "com.heyfos.imageloader")', prop_decl)
# Update loadCGImage
old_loadCG = """ private func loadCGImage(from url: URL) -> CGImage? {
if let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil) {
return CGImageSourceCreateImageAtIndex(imageSource, 0, nil)
}
return nil
}"""
new_loadCG = """ private func loadCGImage(from url: URL) -> CGImage? {
if let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil) {
// Capture metadata from the first loaded image
if self.baseImageProperties == nil {
self.baseImageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)
}
return CGImageSourceCreateImageAtIndex(imageSource, 0, nil)
}
return nil
}"""
text = text.replace(old_loadCG, new_loadCG)
# Update saveTexture
old_save = """ // Add compression properties to reduce file size
let properties: [CFString: Any] = [
kCGImageDestinationLossyCompressionQuality: 0.9,
kCGImagePropertyTIFFCompression: 5 // LZW compression
]
CGImageDestinationAddImage(destination, cgImage, properties as CFDictionary)"""
new_save = """ // Prepare metadata dictionary combining base metadata with our settings
var properties: [CFString: Any] = [:]
if let base = self.baseImageProperties as? [CFString: Any] {
properties = base
// Strip dimensions since we may have cropped the image during alignment
properties.removeValue(forKey: kCGImagePropertyPixelWidth)
properties.removeValue(forKey: kCGImagePropertyPixelHeight)
properties.removeValue(forKey: kCGImagePropertyOrientation)
}
properties[kCGImageDestinationLossyCompressionQuality] = 0.9
properties[kCGImagePropertyTIFFCompression] = 5 // LZW compression
CGImageDestinationAddImage(destination, cgImage, properties as CFDictionary)"""
text = text.replace(old_save, new_save)
with open("Sources/HeyFoSCore/Processing/ImageLoader.swift", "w") as f:
f.write(text)
print("Patched ImageLoader.swift")
if __name__ == "__main__":
update_file()