Hi, can someone give me a starting example of how to write an openexr file into disk? I tried the following toy example to write a 2-by-2 checker-box:
import ModelIO
var floatData = UnsafeMutableBufferPointer<Float32>.allocate(capacity: 4)
floatData[0] = Float32(0.99)
floatData[1] = Float32(0.0)
floatData[2] = Float32(0.0)
floatData[3] = Float32(0.99)
var floatBuffer = Data(buffer: floatData)
let exrImg = MDLTexture(data: floatBuffer,
topLeftOrigin: true,
name: "output.exr",
dimensions: SIMD2<Int32>(2,2),
rowStride: 2 * MemoryLayout<Float32>.size,
channelCount: 1,
channelEncoding: MDLTextureChannelEncoding.float32,
isCube: false)
let resourcesURL = URL(fileURLWithPath: "/Users/myDirectory", isDirectory: true)
let fileName = resourcesURL.appendingPathComponent(exrImgTest.name)
if(exrImg.write(to: fileName)){
print("Success! Image written in disk")
} else {
print("could not write image")
}
When I run this I get thread 1: EXC_BAD_ACCESS (code=1, address=0xf0) pointing to the write method of the MDLTexture. If I change the name to output.png the image is written without a problem.
I guess I am missing something basic here. Moreover, how could I write a multiple channel openEXR image to disk?
Post
Replies
Boosts
Views
Activity
I tried it with the following code with a larger image:
import ModelIO
if #available(macOS 11.0, *) {
var floatData = UnsafeMutableBufferPointer<Float32>.allocate(capacity: 16*16)
for i in 0..<256{
if (i%2 == 0){
floatData[i] = Float32(0.99)
} else {
floatData[i] = Float32(0)
}
}
let floatBuffer = Data(buffer: floatData)
let exrImg = MDLTexture(data: floatBuffer,
topLeftOrigin: true,
name: "output.exr",
dimensions: SIMD2<Int32>(16,16),
rowStride: 16 * MemoryLayout<Float32>.size,
channelCount: 1,
channelEncoding: MDLTextureChannelEncoding.float32,
isCube: false)
let resourcesURL = URL(fileURLWithPath: "/Users/myDirectory", isDirectory: true)
let fileName = resourcesURL.appendingPathComponent(exrImg.name)
if(exrImg.write(to: fileName)){
print("Success! Image written in disk")
} else {
print("could not write image")
}
} else {
// Fallback on earlier versions
}
I get the following runtime error:
Error>: kvImagePrintDiagnosticsToConsole: vImageConverter: In this version of the converter, most pixel formats must be packed arrays of samples with no empty space, except as appropriate for kCGImageAlphaNoneSkipFirst/Last. Here, the colorspace has 1 channels, with 1 more for alpha, which should mean (1+1 channels/pixel)*16 bits per channel = 32 bits per pixel. You passed 16 in format->bitsPerPixel.
doubling the size of the buffer and changing the Float32 to Float16 did not work either. The write method returns false.