2个版本
0.1.1 | 2020年8月4日 |
---|---|
0.1.0 | 2020年8月4日 |
#355 在 压缩 中
41KB
822 行
crnlib
这是一个从 crunch/crnlib 的移植,许可可在文件末尾找到
用法
use std::io::prelude::*;
let sample = "samples/test.crn";
let mut file = std::fs::File::open(sample).expect("open sample crn file");
let mut buffer = Vec::new();
let header = Header::parse(&buffer).expect("parse");
let tables = header.get_table(&buffer).expect("read table");
let level0 = header.unpack_level(&tables, &buffer, 0).expect("unpack");
// level0 contains DXT encoded image content, which could be read by image
use image::ImageDecoder;
let (width0, height0) = header.get_level_info(0).expect("get level info");
let variant = match header.format {
Format::Dxt1 => image::dxt::DXTVariant::DXT1,
Format::Dxt3 => image::dxt::DXTVariant::DXT3,
Format::Dxt5 => image::dxt::DXTVariant::DXT5,
format => unimplemented!("image does not support format {:?}", format),
};
let decoder = image::dxt::DxtDecoder::new(std::io::Cursor::new(&level0), width0 as u32, height0 as u32, variant).expect("new image");
let mut raw = vec![0; decoder.total_bytes() as usize];
let color_type = decoder.color_type();
decoder.read_image(&mut raw).expect("decode dxt");
let f = std::fs::File::create(std::path::Path::new(sample).with_extension("tga")).expect("create sample tga file");
let encoder = image::tga::TgaEncoder::new(f);
encoder.encode(&raw, width0 as u32, height0 as u32, color_type).expect("encode tga");
表格文档
- 任何表格都包含2棵huffman树
- 第二棵树的max_symbol_count的最大值为14位
- 第一个临时树的"symbol_count",符号被重新排序为
[ShortZero, LongZero, ShortRepeat, LongRepeat, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 16]
. - 临时树也有键
0..=16
以及ShortZero, LongZero, ShortRepeat, LongRepeat
,这些参数带有3, 7, 2, 6
位的参数。 - 第二棵树的深度数组(长度为max_symbol_count)使用第一棵树进行编码。
- 使用symbol_depth存储huffman树。
- symbol_depth表示码长(位数)。
- 将码从深度小的键到深度大的键分配给键,然后按键的顺序。
Crunch许可
crunch/crnlib uses a modified ZLIB license. Specifically, it's the same as zlib except that
public credits for using the library are *required*.
Copyright (c) 2010-2016 Richard Geldreich, Jr. All rights reserved.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software.
2. If you use this software in a product, this acknowledgment in the product
documentation or credits is required:
"Crunch Library Copyright (c) 2010-2016 Richard Geldreich, Jr."
3. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
4. This notice may not be removed or altered from any source distribution.
依赖项
~1.5–2.2MB
~52K SLoC