image.codec\meta\png_meta.mbt
100% · 43 / 43 points · 143 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # PNG 元数据 meta/png_meta - 3
/// - 4
/// 编解码工具模块 - 5
/// 编解码工具模块 Codec utility module - 6
// PNG 元数据读取(纯 MoonBit):text chunks (tEXt/iTXt) - 7
- 8
///| - 9
- 10
///| - 11
/// PNG text chunk(关键字-文本对) - 12
#derive(Eq, Default, @debug.Debug) - 13
pub(all) struct PngTextChunk { - 14
keyword : String - 15
text : String - 16
} - 17
- 18
///| - 19
pub extend PngTextChunk with @debug.Debug::{to_repr} - 20
- 21
///| - 22
pub extend PngTextChunk with Eq::{not_equal, equal} - 23
- 24
///| - 25
pub extend PngTextChunk with Default::{default} - 26
- 27
///| - 28
fn read_png_bytes_as_string(data : Bytes, start : Int32, end : Int32) -> String { - 29●
let chars : Array[Byte] = [] - 30
for i in start..<end { - 31●
chars.push(data[i]) - 32
} - 33●
@encoding/utf8.decode(Bytes::from_array(chars)[:]) catch { - 34●
_ => "" - 35
} - 36
} - 37
- 38
///| - 39
/// 从 PNG 字节流读取 text chunks(tEXt 和 iTXt)。 - 40
/// 返回关键字-文本对数组。不含 EXIF 等其他元数据。 - 41
pub fn read_png_text_chunks(data : Bytes) -> Array[PngTextChunk] { - 42●
let result : Array[PngTextChunk] = [] - 43●
let len = data.length() - 44
// PNG signature: 8 bytes - 45
if len < 8 || - 46●
data[0].to_int() != 0x89 || - 47●
data[1].to_int() != 0x50 || - 48●
data[2].to_int() != 0x4E || - 49●
data[3].to_int() != 0x47 { - 50●
return result - 51
} - 52
let mut pos = 8 - 53
while pos + 8 <= len { - 54
// chunk length (4 bytes BE) + type (4 bytes) - 55●
let chunk_len = data[pos].to_int() * 16777216 + - 56●
data[pos + 1].to_int() * 65536 + - 57●
data[pos + 2].to_int() * 256 + - 58●
data[pos + 3].to_int() - 59●
let type0 = data[pos + 4].to_int() - 60●
let type1 = data[pos + 5].to_int() - 61●
let type2 = data[pos + 6].to_int() - 62●
let type3 = data[pos + 7].to_int() - 63
let data_start = pos + 8 - 64
if data_start + chunk_len > len { - 65●
break - 66
} - 67
// tEXt: type = 0x74 0x45 0x58 0x74 - 68
if type0 == 0x74 && type1 == 0x45 && type2 == 0x58 && type3 == 0x74 { - 69
// tEXt: keyword + 0x00 + text - 70●
let mut sep = -1 - 71
for i in 0..<chunk_len { - 72●
if data[data_start + i] == b'\x00' { - 73●
sep = i - 74
break - 75
} - 76
} - 77
if sep >= 0 { - 78●
let keyword = read_png_bytes_as_string( - 79
data, - 80
data_start, - 81
data_start + sep, - 82
) - 83●
let text = read_png_bytes_as_string( - 84
data, - 85
data_start + sep + 1, - 86
data_start + chunk_len, - 87
) - 88●
result.push(PngTextChunk::{ keyword, text, }) - 89
} - 90
} - 91
// iTXt: type = 0x69 0x54 0x58 0x74 - 92
if type0 == 0x69 && type1 == 0x54 && type2 == 0x58 && type3 == 0x74 { - 93
// iTXt: keyword + 0x00 + compression_flag + compression_method + language_tag + 0x00 + translated_keyword + 0x00 + text - 94●
let mut sep = -1 - 95
for i in 0..<chunk_len { - 96●
if data[data_start + i] == b'\x00' { - 97●
sep = i - 98
break - 99
} - 100
} - 101
if sep >= 0 && sep + 2 < chunk_len { - 102●
let keyword = read_png_bytes_as_string( - 103
data, - 104
data_start, - 105
data_start + sep, - 106
) - 107●
let comp_flag = data[data_start + sep + 1].to_int() - 108
// 跳过 compression_method (1 byte) + language_tag + 0x00 + translated_keyword + 0x00 - 109
let mut text_start = sep + 3 // skip comp_flag + comp_method - 110
// 跳过 language tag (until 0x00) - 111
while text_start < chunk_len && data[data_start + text_start] != b'\x00' { - 112●
text_start += 1 - 113
} - 114
text_start += 1 // skip 0x00 - 115
// 跳过 translated keyword (until 0x00) - 116
while text_start < chunk_len && data[data_start + text_start] != b'\x00' { - 117●
text_start += 1 - 118
} - 119
text_start += 1 // skip 0x00 - 120
if text_start <= chunk_len { - 121●
let text = if comp_flag == 0 { - 122●
read_png_bytes_as_string( - 123
data, - 124
data_start + text_start, - 125
data_start + chunk_len, - 126
) - 127
} else { - 128
// 压缩文本不支持(需要 zlib 解压) - 129●
"[compressed]" - 130
} - 131●
result.push(PngTextChunk::{ keyword, text, }) - 132
} - 133
} - 134
} - 135
// IEND 标志着 PNG 结束 - 136
if type0 == 0x49 && type1 == 0x45 && type2 == 0x4E && type3 == 0x44 { - 137●
break - 138
} - 139
// 前进到下一个 chunk: length(4) + type(4) + data(chunk_len) + CRC(4) - 140
pos = data_start + chunk_len + 4 - 141
} - 142
result - 143
}