Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions Sources/AssetKit/BOM/BOMTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,45 @@ struct BOMTree {
leaf.writeBE(valueBlockIDs[i])
leaf.writeBE(keyBlockIDs[i])
}
if leaf.offset < Int(blockSize) {
leaf.writeZeros(Int(blockSize) - leaf.offset)
// BOM trees whose keys all have the same length store them inline:
// the header gives that length and actool follows the leaf entries
// with 4 zero bytes and a copy of every key, sizing the leaf
// blockSize + the total key length. With keys of different lengths
// (image names in FACETKEYS, appearance names), the header says
// 0xFFFFFFFF and the leaf has no copy. App Store Connect reads keys
// through this copy when the length is fixed: without it, or with a
// fixed length on keys that differ, it reports the app icon as
// missing (ITMS-90022, 90023, 91111).
let keyLengths = Set(sorted.map(\.key.count))
let fixedKeyLength: Int? = keyLengths.count == 1 ? keyLengths.first : nil
if fixedKeyLength != nil {
leaf.writeZeros(4)
for entry in sorted {
leaf.write(entry.key)
}
}
let leafSize = Int(blockSize) + (fixedKeyLength != nil ? sorted.reduce(0) { $0 + $1.key.count } : 0)
if leaf.offset < leafSize {
leaf.writeZeros(leafSize - leaf.offset)
}
let leafID = bom.addBlock(leaf.data)

// Tree header block. actool's tree headers are 29 bytes -- 21 bytes
// of fixed fields plus 8 trailing zeros (probably reserved/align).
// Match the layout so all trees in our output are the same size as
// the reference.
// Tree header block, 29 bytes like actool's. After the fixed fields
// actool writes the key length as a big-endian u32 when all keys
// have the same length (18 or 20 for RENDITIONS, 7 for a FACETKEYS
// tree holding only "AppIcon"), 0xFFFFFFFF when they differ, then 4
// zero bytes. App Store Connect never finishes processing a build
// whose catalog leaves it at 0: the upload stays in PROCESSING with
// no error.
var header = ByteWriter()
header.writeBE(treeMagic)
header.writeBE(UInt32(1)) // version
header.writeBE(leafID) // childBlockID
header.writeBE(blockSize)
header.writeBE(UInt32(sorted.count)) // pathCount
header.write(byte: 0) // isPathInternal
header.writeZeros(8) // trailing reserved
header.writeBE(fixedKeyLength.map { UInt32($0) } ?? 0xFFFF_FFFF) // key length
header.writeZeros(4) // reserved
return bom.addBlock(header.data)
}

Expand Down
10 changes: 9 additions & 1 deletion Sources/AssetKit/CAR/CSIWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ enum CSIWriter {
.bitmapFlag,
.bytesPerRow(width: body.width),
])
let payload = MLECBody.encode(width: body.width, height: body.height, pixelsBGRA: body.pixelsBGRA)
// actool writes 1 in the MLEC field after the magic for images and 3
// for app icons (see MLECBody). With 3, iOS draws an image's
// transparent pixels black.
let payload = MLECBody.encode(
width: body.width,
height: body.height,
pixelsBGRA: body.pixelsBGRA,
pixelMode: body.kind == .appIcon ? .opaque : .alpha
)

// Bit 4 (0x10): generic-image category; set for `.image`, cleared
// for `.appIcon`. Matches what UIImage(named:) walks for imageset
Expand Down
42 changes: 35 additions & 7 deletions Sources/AssetKit/CAR/MLECBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Foundation
/// Layout verified against actool's reference Assets.car:
///
/// MLEC magic 4 bytes
/// compressionType u32 (0 = raw, 3 = LZFSE)
/// bytesPerPixel u32 (4 for BGRA8)
/// pixelMode u32 (1 = with alpha, 3 = opaque, see PixelMode)
/// compression u32 (4 = LZFSE)
/// chunkCount u32 (1 or 3)
/// then chunkCount * KCBC chunks
///
Expand All @@ -23,8 +23,36 @@ import Foundation
/// single chunk covering the whole image. The 3-chunk split is mimicry
/// rather than a correctness requirement: CoreUI accepts both layouts.
enum MLECBody {
static func encode(width: UInt32, height: UInt32, pixelsBGRA: [UInt8]) -> Data {
let bytesPerRow = Int(width) * 4
/// Rows are padded to a multiple of 32 bytes, like actool, and the
/// `bytesPerRow` TLV says so. App Store Connect never finishes processing
/// a build whose catalog stores tightly packed rows that are not already
/// a multiple of 32 bytes (a 180 px icon: 720 bytes instead of 736).
static func bytesPerRow(width: UInt32) -> Int {
(Int(width) * 4 + 31) & ~31
}

/// The u32 after the MLEC magic. actool writes 1 for every image with
/// transparency (534 of 534 renditions in Xcode's own catalogs) and 3 only
/// in app icon catalogs (the store icon, icon atlases). iOS treats a 3
/// bitmap as opaque: its transparent pixels come out black.
enum PixelMode: UInt32 {
case alpha = 1
case opaque = 3
}

static func encode(width: UInt32, height: UInt32, pixelsBGRA: [UInt8], pixelMode: PixelMode = .alpha) -> Data {
let packedRow = Int(width) * 4
let bytesPerRow = Self.bytesPerRow(width: width)
var padded = pixelsBGRA
if bytesPerRow != packedRow {
padded = [UInt8](repeating: 0, count: bytesPerRow * Int(height))
for row in 0..<Int(height) {
padded.replaceSubrange(
row * bytesPerRow..<row * bytesPerRow + packedRow,
with: pixelsBGRA[row * packedRow..<(row + 1) * packedRow]
)
}
}
let canChunkInThree = height % 3 == 0
let chunkCount: UInt32 = canChunkInThree ? 3 : 1
let rowsPerChunk = height / chunkCount
Expand All @@ -33,14 +61,14 @@ enum MLECBody {
for i in 0..<Int(chunkCount) {
let start = i * Int(rowsPerChunk) * bytesPerRow
let end = start + Int(rowsPerChunk) * bytesPerRow
let slice = Array(pixelsBGRA[start..<end])
let slice = Array(padded[start..<end])
chunks.append((rows: rowsPerChunk, payload: LZFSE.encode(slice)))
}

var w = ByteWriter()
w.writeFourCC("MLEC")
w.writeLE(UInt32(3)) // compressionType = 3 (LZFSE)
w.writeLE(UInt32(4)) // bytesPerPixel (BGRA8 = 4)
w.writeLE(pixelMode.rawValue) // 1 = with alpha, 3 = opaque (app icons)
w.writeLE(UInt32(4)) // compression: 4 = LZFSE
w.writeLE(chunkCount)

for chunk in chunks {
Expand Down
4 changes: 1 addition & 3 deletions Sources/AssetKit/CAR/TVLEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ enum TVLEntry {
case .bytesPerRow(let width):
w.writeLE(UInt32(1007))
w.writeLE(UInt32(4))
let bytesPerRow = width * 4
let aligned = (bytesPerRow + 15) & ~15
w.writeLE(aligned)
w.writeLE(UInt32(MLECBody.bytesPerRow(width: width)))
}
}
}
12 changes: 12 additions & 0 deletions Sources/AssetKit/Compression/LZFSE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ enum LZFSE {
precondition(encoded > 0, "LZFSE encoding failed for \(input.count)-byte buffer")
return Array(output.prefix(encoded))
}

/// Decodes an LZFSE stream whose decoded size is at most `capacity`.
/// Only used to check the encoder's output.
static func decode(_ input: [UInt8], capacity: Int = 1 << 24) -> [UInt8] {
var output = [UInt8](repeating: 0, count: capacity)
let decoded = input.withUnsafeBufferPointer { inBuf -> Int in
output.withUnsafeMutableBufferPointer { outBuf in
lzfse_decode_buffer(outBuf.baseAddress!, capacity, inBuf.baseAddress!, input.count, nil)
}
}
return Array(output.prefix(decoded))
}
}
43 changes: 43 additions & 0 deletions Tests/AssetKitTests/RowPaddingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Foundation
import Testing
@testable import AssetKit

@Suite("Bitmap row padding")
struct RowPaddingTests {
@Test("Rows are padded to 32 bytes, in the TLV and in the pixels")
func rowPadding() throws {
for (width, expected) in [(20, 96), (29, 128), (87, 352), (167, 672), (180, 736), (256, 1024)] {
#expect(MLECBody.bytesPerRow(width: UInt32(width)) == expected, "\(width)")
}
// 29 px wide, 3 rows: each stored row is 128 bytes, the last 12 zero
let width = 29, height = 3
let pixels = [UInt8](repeating: 0xAB, count: width * 4 * height)
let body = MLECBody.encode(width: UInt32(width), height: UInt32(height), pixelsBGRA: pixels)
func le32(_ o: Int) -> Int {
Int(body[body.startIndex + o]) | Int(body[body.startIndex + o + 1]) << 8
| Int(body[body.startIndex + o + 2]) << 16 | Int(body[body.startIndex + o + 3]) << 24
}
// 3 rows divide by 3: one KCBC chunk per row. MLEC header 16 bytes, chunk header 20 bytes
var offset = 16
for _ in 0..<3 {
let size = le32(offset + 16)
let raw = LZFSE.decode(Array(body[body.startIndex + offset + 20..<body.startIndex + offset + 20 + size]))
#expect(raw.count == 128)
#expect(raw.prefix(116).allSatisfy { $0 == 0xAB })
#expect(raw.suffix(12).allSatisfy { $0 == 0 })
offset += 20 + size
}
}
}

@Suite("Bitmap pixel mode")
struct PixelModeTests {
@Test("Images are written with alpha (1), app icons as opaque (3)")
func pixelMode() {
let pixels = [UInt8](repeating: 0, count: 4 * 4 * 4)
let image = MLECBody.encode(width: 4, height: 4, pixelsBGRA: pixels, pixelMode: .alpha)
let icon = MLECBody.encode(width: 4, height: 4, pixelsBGRA: pixels, pixelMode: .opaque)
#expect(Array(image[image.startIndex + 4..<image.startIndex + 12]) == [1, 0, 0, 0, 4, 0, 0, 0])
#expect(Array(icon[icon.startIndex + 4..<icon.startIndex + 12]) == [3, 0, 0, 0, 4, 0, 0, 0])
}
}