简体   繁体   中英

Encode ASTC on device (iOS)?

Is there any library able to encode image data to ASTC texture on demand? I am aware it is CPU intensive, but interested anyway.

In iOS 10, Apple added ASTC encoding to the system. 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. See CGImageDestination and associated objects. One can also get to it through image asset catalog compression in Xcode.

The system ASTC encoder is much faster than the ARM reference encoder. Block sizes 4x4 and 8x8 are supported. Performance should be similar to JPEG or PNG encoding.

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); the only other allowed size is 4x4 (8 bits per pixel), which is the default.

For max performance, add kCGImageDestinationLossyCompressionQuality: 0.0 to the options for CGImageDestinationAddImageFromSource .

Other possible flags are kCGImagePropertyASTCUseLZFSE , kCGImagePropertyASTCPreTwiddle , kCGImagePropertyASTCFlipVertically and kCGImagePropertyASTCWeightChannelsEqually (all bools).

https://github.com/ARM-software/astc-encoder is the reference compressor (created by ARM and AMD). It is probably not hard to get the source code working on an iOS device. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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