ios - 目标 C 到工会的快速转换
问题描述
嗨,我正在尝试将以下目标 c 代码转换为 swift,但努力将支持的联合转换为 C 但不直接在 swift 中。
我不确定如何转换以下联合类型并将其传递给 MTLTexture getbytes?
union {
float f[2];
unsigned char bytes[8];
} u;
最后一部分,我想用 log 语句打印这些浮点值。
如果我能对下面的代码片段进行快速转换,那就太好了。
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
id<MTLCommandQueue> queue = [device newCommandQueue];
id<MTLCommandBuffer> commandBuffer = [queue commandBuffer];
MTKTextureLoader *textureLoader = [[MTKTextureLoader alloc] initWithDevice:device];
id<MTLTexture> sourceTexture = [textureLoader newTextureWithCGImage:image.CGImage options:nil error:nil];
CGColorSpaceRef srcColorSpace = CGColorSpaceCreateDeviceRGB();
CGColorSpaceRef dstColorSpace = CGColorSpaceCreateDeviceGray();
CGColorConversionInfoRef conversionInfo = CGColorConversionInfoCreate(srcColorSpace, dstColorSpace);
MPSImageConversion *conversion = [[MPSImageConversion alloc] initWithDevice:device
srcAlpha:MPSAlphaTypeAlphaIsOne
destAlpha:MPSAlphaTypeAlphaIsOne
backgroundColor:nil
conversionInfo:conversionInfo];
MTLTextureDescriptor *grayTextureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR16Unorm
width:sourceTexture.width
height:sourceTexture.height
mipmapped:false];
grayTextureDescriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead;
id<MTLTexture> grayTexture = [device newTextureWithDescriptor:grayTextureDescriptor];
[conversion encodeToCommandBuffer:commandBuffer sourceTexture:sourceTexture destinationTexture:grayTexture];
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:grayTexture.pixelFormat
width:sourceTexture.width
height:sourceTexture.height
mipmapped:false];
textureDescriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead;
id<MTLTexture> texture = [device newTextureWithDescriptor:textureDescriptor];
MPSImageLaplacian *imageKernel = [[MPSImageLaplacian alloc] initWithDevice:device];
[imageKernel encodeToCommandBuffer:commandBuffer sourceTexture:grayTexture destinationTexture:texture];
MPSImageStatisticsMeanAndVariance *meanAndVariance = [[MPSImageStatisticsMeanAndVariance alloc] initWithDevice:device];
MTLTextureDescriptor *varianceTextureDescriptor = [MTLTextureDescriptor
texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Float
width:2
height:1
mipmapped:NO];
varianceTextureDescriptor.usage = MTLTextureUsageShaderWrite;
id<MTLTexture> varianceTexture = [device newTextureWithDescriptor:varianceTextureDescriptor];
[meanAndVariance encodeToCommandBuffer:commandBuffer sourceTexture:texture destinationTexture:varianceTexture];
[commandBuffer commit];
[commandBuffer waitUntilCompleted];
union {
float f[2];
unsigned char bytes[8];
} u;
MTLRegion region = MTLRegionMake2D(0, 0, 2, 1);
[varianceTexture getBytes:u.bytes bytesPerRow:2 * 4 fromRegion:region mipmapLevel: 0];
NSLog(@"mean: %f", u.f[0] * 255);
NSLog(@"variance: %f", u.f[1] * 255 * 255);
如果我能迅速得到这个代表会很棒吗?
解决方案
您可以使用 Struct 代替,就像这样。并添加一个扩展来获取日志记录的描述。
struct u {
var bytes: [UInt8] = [0,0,0,0, 0,0,0,0]
var f: [Float32] {
set {
var f = newValue
memcpy(&bytes, &f, 8)
}
get {
var f: [Float32] = [0,0]
var b = bytes
memcpy(&f, &b, 8)
return Array(f)
}
}
}
extension u: CustomStringConvertible {
var description: String {
let bytesString = (bytes.map{ "\($0)"}).joined(separator: " ")
return "floats : \(f[0]) \(f[1]) - bytes : \(bytesString)"
}
}
var test = u()
print(test)
test.f = [3.14, 1.618]
print(test)
test.bytes = [195, 245, 72, 64, 160, 26, 207, 63]
print(test)
日志:
floats : 0.0 0.0 - bytes : 0 0 0 0 0 0 0 0
floats : 3.14 1.618 - bytes : 195 245 72 64 160 26 207 63
floats : 3.14 1.618 - bytes : 195 245 72 64 160 26 207 63
推荐阅读
- android - Retrofit 2.X - 响应与呼叫 - 推荐使用什么?
- c - Valgrind 检测到未初始化的值并跳转,但在我的代码中没有发生
- firebase-hosting - Firebase 托管存在问题
- javascript - 离线获取json文件
- python - Ipyleaflet:创建自己的任意非地理坐标系底图
- influxdb - 使用通量的 influxdb 查询失败
- python - 即使我已经安装了 sklearn,我也无法导入 sklearn
- html - 将文本区域拆分为多个子区域,最后两个字段应使用 css 进行颜色编码
- javascript - 基于位置 JavaScript 将元素分配给数组
- python - 如何将 HEIC 格式从文件夹重命名为 png/jpeg