-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransparentMaker.swift
More file actions
84 lines (69 loc) · 2.82 KB
/
TransparentMaker.swift
File metadata and controls
84 lines (69 loc) · 2.82 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import AppKit
import CoreGraphics
func makeTransparent(inputPath: String, outputPath: String) {
guard let image = NSImage(contentsOfFile: inputPath) else {
print("Error: Could not load image at \(inputPath)")
exit(1)
}
var imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
guard let cgImage = image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else {
print("Error: Could not create CGImage")
exit(1)
}
let width = cgImage.width
let height = cgImage.height
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * width
let bitsPerComponent = 8
let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue
guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else {
print("Error: Could not create CGContext")
exit(1)
}
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
guard let buffer = context.data else {
print("Error: No data in context")
exit(1)
}
let pixelBuffer = buffer.bindMemory(to: UInt8.self, capacity: width * height * bytesPerPixel)
for y in 0..<height {
for x in 0..<width {
let offset = (y * width + x) * bytesPerPixel
let r = pixelBuffer[offset]
let g = pixelBuffer[offset + 1]
let b = pixelBuffer[offset + 2]
// Check if pixel is "white" (R, G, B > 240)
if r > 240 && g > 240 && b > 240 {
pixelBuffer[offset] = 0
pixelBuffer[offset + 1] = 0
pixelBuffer[offset + 2] = 0
pixelBuffer[offset + 3] = 0 // Transparent
}
}
}
guard let outputCgImage = context.makeImage() else {
print("Error: Could not create output CGImage")
exit(1)
}
let newImage = NSImage(cgImage: outputCgImage, size: NSSize(width: width, height: height))
guard let tiffData = newImage.tiffRepresentation,
let bitmap = NSBitmapImageRep(data: tiffData),
let pngData = bitmap.representation(using: .png, properties: [:]) else {
print("Error: Could not convert to PNG")
exit(1)
}
do {
try pngData.write(to: URL(fileURLWithPath: outputPath))
print("Success: Saved transparent image to \(outputPath)")
} catch {
print("Error: \(error)")
exit(1)
}
}
let args = CommandLine.arguments
if args.count < 3 {
print("Usage: TransparentMaker <input> <output>")
exit(1)
}
makeTransparent(inputPath: args[1], outputPath: args[2])