image.codec\meta\png_meta_write.mbt
97.7% · 130 / 133 points · 224 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_write - 3
/// - 4
/// 编解码工具模块 - 5
/// 编解码工具模块 Codec utility module - 6
// v250.0.0 PNG 元数据写入 + eXIf chunk 读写 - 7
// PNG text chunk 写入 + PNG eXIf chunk 读取/写入 - 8
- 9
///| - 10
/// 计算 CRC32(PNG 使用的 IEEE 802.3 CRC32) - 11
fn png_crc32(data : Bytes) -> UInt32 { - 12●
let mut crc : UInt32 = 0xFFFFFFFFU - 13●
for i in 0..<data.length() { - 14●
let byte = data[i].to_uint() - 15
crc = crc ^ byte - 16
for _ in 0..<8 { - 17●
if (crc & 1U) != 0U { - 18●
crc = (crc >> 1) ^ 0xEDB88320U - 19
} else { - 20●
crc = crc >> 1 - 21
} - 22
} - 23
} - 24
crc ^ 0xFFFFFFFFU - 25
} - 26
- 27
///| - 28
/// 构造 PNG chunk(length + type + data + CRC) - 29
fn png_build_chunk(chunk_type : String, chunk_data : Bytes) -> Bytes { - 30●
let out : Array[Byte] = [] - 31●
let len = chunk_data.length() - 32
// length (4 bytes BE) - 33●
out.push(((len >> 24) & 0xFF).to_byte()) - 34●
out.push(((len >> 16) & 0xFF).to_byte()) - 35●
out.push(((len >> 8) & 0xFF).to_byte()) - 36●
out.push((len & 0xFF).to_byte()) - 37
// type (4 bytes) - 38●
for c in chunk_type.to_array() { - 39●
out.push(c.to_int().to_byte()) - 40
} - 41
// data - 42
for b in chunk_data { - 43●
out.push(b) - 44
} - 45
// CRC: computed over type + data - 46
let crc_input : Array[Byte] = [] - 47●
for c in chunk_type.to_array() { - 48●
crc_input.push(c.to_int().to_byte()) - 49
} - 50
for b in chunk_data { - 51●
crc_input.push(b) - 52
} - 53●
let crc = png_crc32(Bytes::from_array(crc_input)) - 54●
out.push(((crc >> 24) & 0xFFU).to_byte()) - 55●
out.push(((crc >> 16) & 0xFFU).to_byte()) - 56●
out.push(((crc >> 8) & 0xFFU).to_byte()) - 57●
out.push((crc & 0xFFU).to_byte()) - 58●
Bytes::from_array(out) - 59
} - 60
- 61
///| - 62
/// 将 text chunks 写入 PNG(在 IDAT 前插入 tEXt chunks) - 63
/// 已有同名 keyword 的 tEXt 不做去重,直接追加 - 64
pub fn write_png_text_chunks( - 65
chunks : Array[PngTextChunk], - 66
png_data : Bytes, - 67
) -> Bytes raise @types.LoadError { - 68●
let len = png_data.length() - 69
if len < 8 || - 70●
png_data[0].to_int() != 0x89 || - 71●
png_data[1].to_int() != 0x50 || - 72●
png_data[2].to_int() != 0x4E || - 73●
png_data[3].to_int() != 0x47 { - 74●
raise @types.LoadError::EncodeFailed("输入不是有效 PNG") - 75
} - 76
// 查找 IDAT chunk 位置 - 77
let mut pos = 8 - 78
let mut idat_pos = -1 - 79
while pos + 8 <= len { - 80●
let type0 = png_data[pos + 4].to_int() - 81●
let type1 = png_data[pos + 5].to_int() - 82●
let type2 = png_data[pos + 6].to_int() - 83●
let type3 = png_data[pos + 7].to_int() - 84
if type0 == 0x49 && type1 == 0x44 && type2 == 0x41 && type3 == 0x54 { - 85●
idat_pos = pos - 86
break - 87
} - 88●
let chunk_len = png_data[pos].to_int() * 16777216 + - 89●
png_data[pos + 1].to_int() * 65536 + - 90●
png_data[pos + 2].to_int() * 256 + - 91●
png_data[pos + 3].to_int() - 92
pos = pos + 8 + chunk_len + 4 - 93
} - 94
if idat_pos < 0 { - 95○
raise @types.LoadError::EncodeFailed("PNG 中未找到 IDAT chunk") - 96
} - 97
// 构造 tEXt chunks - 98
let text_chunks : Array[Byte] = [] - 99
for chunk in chunks { - 100●
let payload : Array[Byte] = [] - 101●
for c in chunk.keyword.to_array() { - 102●
let code = c.to_int() - 103
if code < 128 { - 104●
payload.push(code.to_byte()) - 105
} - 106
} - 107●
payload.push(b'\x00') - 108●
for c in chunk.text.to_array() { - 109●
let code = c.to_int() - 110
if code < 128 { - 111●
payload.push(code.to_byte()) - 112
} - 113
} - 114●
let chunk_bytes = png_build_chunk("tEXt", Bytes::from_array(payload)) - 115
for b in chunk_bytes { - 116●
text_chunks.push(b) - 117
} - 118
} - 119
// 拼接:PNG header + existing chunks before IDAT + new tEXt chunks + IDAT and rest - 120
let out : Array[Byte] = [] - 121
for i in 0..<idat_pos { - 122●
out.push(png_data[i]) - 123
} - 124
for b in text_chunks { - 125●
out.push(b) - 126
} - 127
for i in idat_pos..<len { - 128●
out.push(png_data[i]) - 129
} - 130●
Bytes::from_array(out) - 131
} - 132
- 133
///| - 134
/// 从 PNG eXIf chunk 读取 EXIF 数据 - 135
/// 返回原始 EXIF 字节(以 "MM" 或 "II" TIFF 头开头),不含 "Exif\0\0" 前缀 - 136
pub fn read_png_exif_from_bytes(data : Bytes) -> Bytes? { - 137●
let len = data.length() - 138
if len < 8 || - 139●
data[0].to_int() != 0x89 || - 140●
data[1].to_int() != 0x50 || - 141●
data[2].to_int() != 0x4E || - 142●
data[3].to_int() != 0x47 { - 143●
return None - 144
} - 145
let mut pos = 8 - 146
while pos + 8 <= len { - 147●
let chunk_len = data[pos].to_int() * 16777216 + - 148●
data[pos + 1].to_int() * 65536 + - 149●
data[pos + 2].to_int() * 256 + - 150●
data[pos + 3].to_int() - 151●
let type0 = data[pos + 4].to_int() - 152●
let type1 = data[pos + 5].to_int() - 153●
let type2 = data[pos + 6].to_int() - 154●
let type3 = data[pos + 7].to_int() - 155
let data_start = pos + 8 - 156
if data_start + chunk_len > len { - 157○
break - 158
} - 159
// eXIf: type = 0x65 0x58 0x49 0x66 - 160
if type0 == 0x65 && type1 == 0x58 && type2 == 0x49 && type3 == 0x66 { - 161●
let exif : Array[Byte] = [] - 162
for i in 0..<chunk_len { - 163●
exif.push(data[data_start + i]) - 164
} - 165●
return Some(Bytes::from_array(exif)) - 166
} - 167
// IEND - 168
if type0 == 0x49 && type1 == 0x45 && type2 == 0x4E && type3 == 0x44 { - 169●
break - 170
} - 171
pos = data_start + chunk_len + 4 - 172
} - 173
None - 174
} - 175
- 176
///| - 177
/// 将 EXIF 数据写入 PNG eXIf chunk(在 IDAT 前插入) - 178
/// exif_data 应以 "MM" 或 "II" TIFF 头开头 - 179
pub fn write_png_exif_to_bytes( - 180
exif_data : Bytes, - 181
png_data : Bytes, - 182
) -> Bytes raise @types.LoadError { - 183●
let len = png_data.length() - 184
if len < 8 || - 185●
png_data[0].to_int() != 0x89 || - 186●
png_data[1].to_int() != 0x50 || - 187●
png_data[2].to_int() != 0x4E || - 188●
png_data[3].to_int() != 0x47 { - 189●
raise @types.LoadError::EncodeFailed("输入不是有效 PNG") - 190
} - 191
// 查找 IDAT chunk 位置 - 192
let mut pos = 8 - 193
let mut idat_pos = -1 - 194
while pos + 8 <= len { - 195●
let type0 = png_data[pos + 4].to_int() - 196●
let type1 = png_data[pos + 5].to_int() - 197●
let type2 = png_data[pos + 6].to_int() - 198●
let type3 = png_data[pos + 7].to_int() - 199
if type0 == 0x49 && type1 == 0x44 && type2 == 0x41 && type3 == 0x54 { - 200●
idat_pos = pos - 201
break - 202
} - 203●
let chunk_len = png_data[pos].to_int() * 16777216 + - 204●
png_data[pos + 1].to_int() * 65536 + - 205●
png_data[pos + 2].to_int() * 256 + - 206●
png_data[pos + 3].to_int() - 207
pos = pos + 8 + chunk_len + 4 - 208
} - 209
if idat_pos < 0 { - 210○
raise @types.LoadError::EncodeFailed("PNG 中未找到 IDAT chunk") - 211
} - 212●
let exif_chunk = png_build_chunk("eXIf", exif_data) - 213
let out : Array[Byte] = [] - 214
for i in 0..<idat_pos { - 215●
out.push(png_data[i]) - 216
} - 217
for b in exif_chunk { - 218●
out.push(b) - 219
} - 220
for i in idat_pos..<len { - 221●
out.push(png_data[i]) - 222
} - 223●
Bytes::from_array(out) - 224
}