简体   繁体   English

在设备(iOS)上编码ASTC?

[英]Encode ASTC on device (iOS)?

Is there any library able to encode image data to ASTC texture on demand? 是否有任何库能够按需将图像数据编码为ASTC纹理? I am aware it is CPU intensive, but interested anyway. 我知道这是CPU密集型的,但无论如何还是有兴趣的。

In iOS 10, Apple added ASTC encoding to the system. 在iOS 10中,Apple向系统添加了ASTC编码。 You can access it either through /usr/include/AppleTextureEncoder.h, /usr/lib/libate.dylib or (simpler) just encode to ASTC using the usual image encoding utils available through CG / ImageIO.framework. 您可以通过/usr/include/AppleTextureEncoder.h、/usr/lib/libate.dylib来访问它,也可以(简单)使用CG / ImageIO.framework提供的常规图像编码工具将其编码为ASTC。 See CGImageDestination and associated objects. 请参阅CGImageDestination和关联的对象。 One can also get to it through image asset catalog compression in Xcode. 也可以通过Xcode中的图像资产目录压缩来实现。

The system ASTC encoder is much faster than the ARM reference encoder. 系统ASTC编码器比ARM参考编码器快得多。 Block sizes 4x4 and 8x8 are supported. 支持块大小为4x4和8x8。 Performance should be similar to JPEG or PNG encoding. 性能应类似于JPEG或PNG编码。

import ImageIO
import MetalKit

let loader: MTKTextureLoader
let srcImage: CGImage
let ktxData = NSMutableData()
let dest = CGImageDestinationCreateWithData(ktxData, "org.khronos.ktx" as CFString, 1, nil)!
CGImageDestinationAddImage(dest, srcImage, 0, ["kCGImagePropertyASTCBlockSize": 0x88] as CFDictionary)
CGImageDestinationFinalize(dest)
try loader.newTexture(data: ktxData as Data, options: [])

The kCGImagePropertyASTCBlockSize option shown gets you 8x8 block size (ie. 2 bits per pixel); 显示的kCGImagePropertyASTCBlockSize选项使您获得8x8的块大小(即,每个像素2位); the only other allowed size is 4x4 (8 bits per pixel), which is the default. 其他唯一允许的大小是4x4(每个像素8位),这是默认设置。

For max performance, add kCGImageDestinationLossyCompressionQuality: 0.0 to the options for CGImageDestinationAddImageFromSource . 为了最大性能,增加kCGImageDestinationLossyCompressionQuality: 0.0为选项CGImageDestinationAddImageFromSource

Other possible flags are kCGImagePropertyASTCUseLZFSE , kCGImagePropertyASTCPreTwiddle , kCGImagePropertyASTCFlipVertically and kCGImagePropertyASTCWeightChannelsEqually (all bools). 其他可能的标志是kCGImagePropertyASTCUseLZFSEkCGImagePropertyASTCPreTwiddlekCGImagePropertyASTCFlipVerticallykCGImagePropertyASTCWeightChannelsEqually (所有kCGImagePropertyASTCWeightChannelsEqually )。

https://github.com/ARM-software/astc-encoder is the reference compressor (created by ARM and AMD). https://github.com/ARM-software/astc-encoder是参考压缩器(由ARM和AMD创建)。 It is probably not hard to get the source code working on an iOS device. 使源代码在iOS设备上工作可能并不难。 It might be prohibitively slow, but it does offer various speed options so maybe one will strike the right balance between quality and speed for you. 它可能会慢得让人望而却步,但是它确实提供了多种速度选择,因此也许您会在质量和速度之间找到合适的平衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM