gpt4 book ai didi

compression - 快速压缩 : doesn't decompress LZ4 image

转载 作者:行者123 更新时间:2023-12-04 13:46:16 30 4
gpt4 key购买 nike

我正在尝试使用 Swift 压缩包解压缩 lz4 压缩的 png 图像,但代码以零大小退出。
我的代码如下,解压后的文件预计为240Kb。

[更新 2]
我在 Apple 的文档中偶然发现了这一点:

The frame is documented here so that you can easily wrap another LZ4 encoder/decoder to produce/consume the same data stream if necessary. An LZ4 encoded buffer is a sequence of blocks, each of which begins with a header. There are three possible headers:

A compressed block header consists of the octets 0x62, 0x76, 0x34, and 0x31, followed by the size in bytes of the decoded (plaintext) data represented by the block and the size (in bytes) of the encoded data stored in the block. Both size fields are stored as (possibly unaligned) 32-bit little-endian values. The compressed block header is followed immediately by the actual LZ4-encoded data stream.

An end of stream header consists of the octets 0x62, 0x76, 0x34, and 0x24 and marks the end of the LZ4 frame. No further data may be written or read beyond this header.



所以我相应地添加了页眉和页脚并且它有效。但是——总有一个“但是”——, 在解压缩之前如何知道“解码(明文)数据的字节大小”?

[更新 1]

我提供了一个 LZ4 文件供您测试,以及从主包中解压缩它的代码用法。

LZ4 文件用 sw "LZ4 命令行界面 64 位 v1.8.0, by Yann Collet"压缩,在互联网上广泛可用

https://drive.google.com/file/d/1eQ204LJs_xsHRwJ_jUcl1Up9NFo8monO/view?usp=sharing

LZ4解压用法
    if let url = Bundle.main.url(forResource: "TestImage300.png", withExtension: "lz4") {
if let compressed = try? Data(contentsOf: url) {
if let decompressed = compressed.lz4Decompress() {
if let image = UIImage(data: decompressed) {
self.sourceImages.append(image)
print("Unittest: Decompressed image as \(image)")
}
}
}
}

LZ4 解压数据扩展代码:
import Foundation
import Compression

extension Data {

func lz4Decompress() -> Data? {
let destinationBufferSize: size_t = 480000
let destinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: destinationBufferSize)

let sourceBufferSize = count

let decompressed = withUnsafeBytes { (sourceBuffer: UnsafePointer<UInt8>) -> Data? in

let size = compression_decode_buffer(
destinationBuffer, destinationBufferSize,
sourceBuffer, sourceBufferSize,
nil, // scratch buffer automatically handled
COMPRESSION_LZ4
)

if size == 0 {
print("Error ")
return nil
}

print("Original compressed size: \(sourceBufferSize) | Decompressed size: \(size)")

return Data(bytesNoCopy: destinationBuffer, count: size, deallocator: .free)
}
return decompressed
}
}

最佳答案

let intArray: [Int8] =  [-16, 1, 1, 39, 0, 19, 11, -30, 7, 10, 29, 14, 0, 0, 0, 0, 96, 9, 6, 0, 1, 2, 0, 17, 14, 6, 0, 2, 2, 0, 18, 14, 7, 0, 65, 0, 0, 0, -51, 6, 0, 0, 2, 0, 0, 43, 0, 16, 2, 9, 0, -1, 13, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 10, 4, 0, 64, 33, -105, 58, 115, 0, 12, 2, 0, 80, 0, 0, 0, 0, 0]

let uintArray = intArray.map { UInt8(bitPattern: $0) }

//对于可见性,无符号 uintArray 是 [240, 1, 1, 39, 0, 19, 11, 226, 7, 10, 29, 14, 0, 0, 0, 0, 96, 9, 6, 0, 1 , 2, 0, 17, 14, 6, 0, 2, 2, 0, 18, 14, 7, 0, 65, 0, 0, 0, 205, 6, 0, 0, 2, 0, 0, 43 , 0, 16, 2, 9, 0, 255, 13, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0 , 0, 7, 0, 0, 0, 8, 0, 0, 0, 10, 4, 0, 64, 33, 151, 58, 115, 0, 12, 2, 0, 80, 0, 0, 0 , 0, 0]
        var encodedData = Data.init(bytes:uintArray)

let decodedCapacity = 205
let decodedDestinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: decodedCapacity)

let decodedData = encodedData.withUnsafeBytes {
(encodedSourceBuffer: UnsafePointer<UInt8>) -> Data? in

let decodedCharCount = compression_decode_buffer(decodedDestinationBuffer,
decodedCapacity,
encodedSourceBuffer,
encodedData.count,
nil,
COMPRESSION_LZ4_RAW)

if decodedCharCount == 0 {
fatalError("Decoding failed.")
}

print("Before: \(encodedSourceBuffer) | After: \(decodedCharCount)")

return Data(bytesNoCopy: decodedDestinationBuffer, count: decodedCharCount, deallocator: .free)

}

如果您没有在压缩中定义 header ,您应该使用 COMPRESSION_LZ4_RAW

关于compression - 快速压缩 : doesn't decompress LZ4 image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47603583/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com