Skip to content

Instantly share code, notes, and snippets.

@victusfate
Last active August 29, 2015 14:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victusfate/584dba0faef807c6ed66 to your computer and use it in GitHub Desktop.
Save victusfate/584dba0faef807c6ed66 to your computer and use it in GitHub Desktop.
parse ACV after effects color files, playground
import Foundation
// read a file, OSx type playground, relative path first then
// let url: NSURL! = NSBundle.mainBundle().URLForResource("adjusted", withExtension: "AAE")
func int16WithBytes(bytesFull: [UInt8], offset: Int) -> UInt16 {
let bytes = Array(bytesFull[offset..<offset+2])
let u16raw = UnsafePointer<UInt16>(bytes).memory
let u16 = CFSwapInt16BigToHost(u16raw)
// println("u16: \(u16)")
return u16
}
//let bytes:[UInt8] = [0x01, 0x02]
func parseACVFileData(url: NSURL) -> ([Double],[Double],[Double],[Double]) {
var fileData = NSData(contentsOfURL: url)
println("size of fileData \(fileData!.length)")
let nbytes = fileData!.length / sizeof(UInt8)
// create array of appropriate length:
var bytes = [UInt8](count: nbytes, repeatedValue: 0)
// copy bytes into array
fileData!.getBytes(&bytes, length:nbytes * sizeof(UInt8))
if (nbytes == 0)
{
NSLog("failed to init ACVFile with data")
return ([],[],[],[])
}
var offset = Int(0)
var version = int16WithBytes(bytes, offset)
offset += 2
var totalCurves = int16WithBytes(bytes, offset)
offset += 2
var curves = [[Double]](count: 0, repeatedValue:[Double]())
var pointRate = 1.0 / 255
println(totalCurves)
// The following is the data for each curve specified by count above
for var x = 0; x < Int(totalCurves); x++ {
var pointCount = int16WithBytes(bytes, offset)
offset += 2
var points = [Double]()
// point count * 4
// Curve points. Each curve point is a pair of short integers where
// the first number is the output value (vertical coordinate on the
// Curves dialog graph) and the second is the input value. All coordinates have range 0 to 255.
for var y = 0; y < Int(pointCount); y++ {
var y = int16WithBytes(bytes, offset)
offset += 2
var x = int16WithBytes(bytes, offset)
offset += 2
points.append(Double(x) * pointRate)
points.append(Double(y) * pointRate)
}
curves.append(points)
}
var composite = curves[0]
var redCurvePoints = curves[1]
var greenCurvePoints = curves[2]
var blueCurvePoints = curves[3]
return (composite,redCurvePoints,greenCurvePoints,blueCurvePoints)
}
let files = [
"ClassicCurve",
"NewburyCurve",
"WarmCurve",
"WaldenCurve",
"BloomCurve",
"MeadowCurve"
]
for file in files {
var url: NSURL! = NSBundle.mainBundle().URLForResource(file, withExtension: "ACV")
println(url)
var controlPoints = parseACVFileData(url)
println(file)
println("composite: \(controlPoints.0),")
println("red: \(controlPoints.1),")
println("green: \(controlPoints.2),")
println("blue: \(controlPoints.3)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment