image.codec\codec\tiff_extended.mbt
93.5% · 72 / 77 points · 155 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # TIFF 扩展 TIFF Extended - 3
/// - 4
/// TIFF 扩展功能 TIFF extended features - 5
- 6
///| - 7
/// TIFF 扩展信息(包含压缩方式、光度解释、方向、分辨率等) - 8
pub struct TiffInfoExtended { - 9
width : Int32 - 10
height : Int32 - 11
bit_depth : Int32 - 12
samples_per_pixel : Int32 - 13
num_channels : Int32 - 14
has_alpha : Bool - 15
compression : Int32 - 16
compression_name : String - 17
photometric : Int32 - 18
photometric_name : String - 19
orientation : Int32 - 20
resolution_unit : Int32 - 21
is_little_endian : Bool - 22
} - 23
- 24
///| - 25
/// 检测数据是否为 TIFF 格式 - 26
pub fn is_tiff_format(data : Bytes) -> Bool { - 27●
let len = data.length() - 28
if len < 8 { - 29●
return false - 30
} - 31
// Little-endian: II + 42 - 32●
if data[0].to_int() == 0x49 && - 33●
data[1].to_int() == 0x49 && - 34●
data[2].to_int() == 0x2A && - 35●
data[3].to_int() == 0x00 { - 36●
return true - 37
} - 38
// Big-endian: MM + 42 - 39●
data[0].to_int() == 0x4D && - 40●
data[1].to_int() == 0x4D && - 41●
data[2].to_int() == 0x00 && - 42●
data[3].to_int() == 0x2A - 43
} - 44
- 45
///| - 46
fn tiff_compression_name(c : Int32) -> String { - 47●
match c { - 48●
1 => "none" - 49●
2 => "CCITT RLE" - 50●
5 => "LZW" - 51●
6 => "JPEG" - 52●
7 => "JPEG lossy" - 53●
8 => "Deflate" - 54●
32773 => "PackBits" - 55●
32946 => "Deflate (old)" - 56●
_ => "unknown" - 57
} - 58
} - 59
- 60
///| - 61
fn tiff_photometric_name(p : Int32) -> String { - 62●
match p { - 63●
0 => "WhiteIsZero" - 64●
1 => "BlackIsZero" - 65●
2 => "RGB" - 66●
3 => "Palette" - 67●
4 => "TransparencyMask" - 68●
5 => "CMYK" - 69●
6 => "YCbCr" - 70●
8 => "CIELab" - 71●
_ => "unknown" - 72
} - 73
} - 74
- 75
///| - 76
/// 解析 TIFF 扩展头部信息 - 77
/// - 78
/// 遍历 IFD 标签,提取尺寸、位深、压缩方式、光度解释、方向、分辨率单位等 - 79
pub fn tiff_info_extended( - 80
data : Bytes, - 81
) -> TiffInfoExtended raise @types.LoadError { - 82●
if !is_tiff_format(data) { - 83●
raise @types.LoadError::UnsupportedFormat("Not a TIFF file") - 84
} - 85●
let len = data.length() - 86●
let little_endian = data[0].to_int() == 0x49 - 87
let ctx = TiffContext::{ data, little_endian, } - 88●
let ifd_offset = tiff_read_u32(ctx, 4) - 89
if ifd_offset < 0 || ifd_offset >= len { - 90●
raise @types.LoadError::DecodeFailed("TIFF IFD 偏移无效") - 91
} - 92●
let entries = tiff_parse_ifd(ctx, ifd_offset) - 93●
let width = match tiff_get_value(entries, ctx, 256) { - 94●
Some(w) => w - 95●
None => raise @types.LoadError::DecodeFailed("TIFF 缺少 ImageWidth") - 96
} - 97●
let height = match tiff_get_value(entries, ctx, 257) { - 98●
Some(h) => h - 99●
None => raise @types.LoadError::DecodeFailed("TIFF 缺少 ImageLength") - 100
} - 101●
let bits_per_sample = match tiff_get_values(entries, ctx, 258) { - 102◐
Some(arr) => if arr.length() > 0 { arr[0] } else { 8 } - 103●
None => 8 - 104
} - 105●
let compression = match tiff_get_value(entries, ctx, 259) { - 106●
Some(c) => c - 107●
None => 1 - 108
} - 109●
let photometric = match tiff_get_value(entries, ctx, 262) { - 110●
Some(p) => p - 111●
None => 1 - 112
} - 113●
let samples_per_pixel = match tiff_get_value(entries, ctx, 277) { - 114●
Some(s) => s - 115●
None => 1 - 116
} - 117●
let orientation = match tiff_get_value(entries, ctx, 274) { - 118○
Some(o) => o - 119●
None => 1 - 120
} - 121●
let resolution_unit = match tiff_get_value(entries, ctx, 296) { - 122○
Some(r) => r - 123●
None => 2 - 124
} - 125●
let extra_samples = match tiff_get_value(entries, ctx, 338) { - 126○
Some(e) => e - 127●
None => 0 - 128
} - 129
if width <= 0 || height <= 0 { - 130●
raise @types.LoadError::DecodeFailed("TIFF 尺寸无效") - 131
} - 132
let has_alpha = extra_samples > 0 || - 133
samples_per_pixel == 4 || - 134
(samples_per_pixel == 2 && photometric == 1) - 135
let num_channels = if has_alpha { - 136○
samples_per_pixel - 137
} else { - 138●
samples_per_pixel - 139
} - 140
TiffInfoExtended::{ - 141
width, - 142
height, - 143
bit_depth: bits_per_sample, - 144
samples_per_pixel, - 145
num_channels, - 146
has_alpha, - 147
compression, - 148●
compression_name: tiff_compression_name(compression), - 149
photometric, - 150●
photometric_name: tiff_photometric_name(photometric), - 151
orientation, - 152
resolution_unit, - 153
is_little_endian: little_endian, - 154
} - 155
}