image.codec\meta\iptc_meta.mbt
92.5% · 196 / 212 points · 368 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # IPTC 元数据 meta/iptc_meta - 3
/// - 4
/// 编解码工具模块 - 5
/// 编解码工具模块 Codec utility module - 6
// v250.0.0 IPTC IIM 元数据解析与写入 - 7
// IPTC IIM (Information Interchange Model) 存储在 JPEG APP13 段中 - 8
// 以 "Photoshop 3.0\0" + "8BIM\x04\x04" 开头,后跟记录序列 - 9
- 10
///| - 11
/// IPTC IIM 元数据(常用字段) - 12
#derive(Eq, Default, @debug.Debug) - 13
pub(all) struct IptcInfo { - 14
title : String - 15
keywords : String - 16
creator : String - 17
creator_title : String - 18
city : String - 19
country : String - 20
caption : String - 21
caption_writer : String - 22
date_created : String - 23
category : String - 24
urgency : Int32 - 25
raw_records : Array[(Int32, Int32, String)] - 26
} - 27
- 28
///| - 29
pub extend IptcInfo with Eq::{not_equal, equal} - 30
- 31
///| - 32
pub extend IptcInfo with Default::{default} - 33
- 34
///| - 35
pub extend IptcInfo with @debug.Debug::{to_repr} - 36
- 37
///| - 38
/// Photoshop 3.0 头部 Photoshop 3.0 header - 39
fn iptc_photoshop_header() -> String { - 40●
"Photoshop 3.0" - 41
} - 42
- 43
///| - 44
/// 从字节提取 ASCII 字符串 - 45
fn iptc_extract_string(data : Bytes, start : Int32, max_len : Int32) -> String { - 46●
let chars : Array[Byte] = [] - 47●
let len = data.length() - 48
for i in 0..<max_len { - 49●
if start + i >= len { - 50○
break - 51
} - 52
let b = data[start + i] - 53
if b == b'\x00' { - 54●
break - 55
} - 56●
chars.push(b) - 57
} - 58●
@encoding/utf8.decode(Bytes::from_array(chars)[:]) catch { - 59○
_ => "" - 60
} - 61
} - 62
- 63
///| - 64
/// 从 JPEG 字节流读取 IPTC IIM 元数据 - 65
/// 返回 None 如果不含 IPTC 数据 - 66
pub fn read_iptc_from_bytes(data : Bytes) -> IptcInfo? { - 67●
let len = data.length() - 68●
if len < 4 || data[0].to_int() != 0xFF || data[1].to_int() != 0xD8 { - 69●
return None - 70
} - 71
// 查找 APP13 marker with Photoshop 3.0 header - 72
let mut pos = 2 - 73
let mut iptc_start = -1 - 74
let mut iptc_len = 0 - 75
while pos < len - 1 { - 76●
if data[pos].to_int() == 0xFF && data[pos + 1].to_int() == 0xED { - 77●
let seg_len = data[pos + 2].to_int() * 256 + data[pos + 3].to_int() - 2 - 78●
let header = iptc_extract_string(data, pos + 4, 14) - 79●
if header == iptc_photoshop_header() { - 80
// 跳过 "Photoshop 3.0\0" (14 bytes) + "8BIM" (4) + 0x04 0x04 (2) + length(4) - 81●
let bim_start = pos + 4 + 14 - 82
if bim_start + 10 < pos + 4 + seg_len { - 83
// Check "8BIM" - 84●
if data[bim_start].to_int() == 0x38 && - 85●
data[bim_start + 1].to_int() == 0x42 && - 86●
data[bim_start + 2].to_int() == 0x49 && - 87●
data[bim_start + 3].to_int() == 0x4D { - 88
// Check resource type 0x0404 (IPTC IIM) - 89●
if data[bim_start + 4].to_int() == 0x04 && - 90●
data[bim_start + 5].to_int() == 0x04 { - 91●
let iptc_data_len = data[bim_start + 6].to_int() * 16777216 + - 92●
data[bim_start + 7].to_int() * 65536 + - 93●
data[bim_start + 8].to_int() * 256 + - 94●
data[bim_start + 9].to_int() - 95
iptc_start = bim_start + 10 - 96
iptc_len = iptc_data_len - 97
break - 98
} - 99
} - 100
} - 101
} - 102
pos += 2 + seg_len + 2 - 103●
} else if data[pos].to_int() == 0xFF { - 104●
let marker = data[pos + 1].to_int() - 105
if marker == 0xDA || marker == 0xD9 { - 106●
break - 107
} - 108○
let seg_len = data[pos + 2].to_int() * 256 + data[pos + 3].to_int() - 109
pos += 2 + seg_len - 110
} else { - 111○
pos += 1 - 112
} - 113
} - 114
if iptc_start < 0 || iptc_start + iptc_len > len { - 115●
return None - 116
} - 117
// 解析 IPTC 记录 - 118
let mut title = "" - 119
let mut keywords = "" - 120
let mut creator = "" - 121
let mut creator_title = "" - 122
let mut city = "" - 123
let mut country = "" - 124
let mut caption = "" - 125
let mut caption_writer = "" - 126
let mut date_created = "" - 127
let mut category = "" - 128
let mut urgency = 0 - 129
let raw_records : Array[(Int32, Int32, String)] = [] - 130
let mut rpos = iptc_start - 131
let rend = iptc_start + iptc_len - 132
while rpos + 5 <= rend { - 133
// 检查 tag marker 0x1C - 134●
if data[rpos].to_int() != 0x1C { - 135○
rpos += 1 - 136
continue - 137
} - 138●
let record_num = data[rpos + 1].to_int() - 139●
let dataset_num = data[rpos + 2].to_int() - 140●
let data_len = data[rpos + 3].to_int() * 256 + data[rpos + 4].to_int() - 141
let val_start = rpos + 5 - 142
if val_start + data_len > rend { - 143○
break - 144
} - 145●
let val = iptc_extract_string(data, val_start, data_len) - 146●
raw_records.push((record_num, dataset_num, val)) - 147●
match (record_num, dataset_num) { - 148●
(2, 5) => title = val - 149●
(2, 25) => keywords = val - 150●
(2, 80) => creator = val - 151●
(2, 85) => creator_title = val - 152●
(2, 90) => city = val - 153●
(2, 101) => country = val - 154●
(2, 120) => caption = val - 155●
(2, 122) => caption_writer = val - 156●
(2, 55) => date_created = val - 157●
(2, 15) => category = val - 158
(2, 10) => - 159●
match xmp_iptc_parse_urgency(val) { - 160●
Some(n) => urgency = n - 161○
None => () - 162
} - 163○
_ => () - 164
} - 165
rpos = val_start + data_len - 166
} - 167
Some(IptcInfo::{ - 168
title, - 169
keywords, - 170
creator, - 171
creator_title, - 172
city, - 173
country, - 174
caption, - 175
caption_writer, - 176
date_created, - 177
category, - 178
urgency, - 179
raw_records, - 180
}) - 181
} - 182
- 183
///| - 184
/// 简单整数解析(IPTC urgency 0-8) - 185
fn xmp_iptc_parse_urgency(s : String) -> Int32? { - 186●
if s.length() == 0 { - 187○
return None - 188
} - 189●
let chars = s.to_array() - 190●
if chars.length() == 1 { - 191●
let c = chars[0].to_int() - 192
if c >= 0x30 && c <= 0x39 { - 193●
return Some(c - 0x30) - 194
} - 195
} - 196
None - 197
} - 198
- 199
///| - 200
/// 构造 IPTC 记录字节 - 201
fn iptc_build_record(record : Int32, dataset : Int32, value : String) -> Bytes { - 202●
let out : Array[Byte] = [] - 203●
out.push(0x1C) - 204●
out.push(record.to_byte()) - 205●
out.push(dataset.to_byte()) - 206●
let vlen = value.length() - 207●
out.push(((vlen >> 8) & 0xFF).to_byte()) - 208●
out.push((vlen & 0xFF).to_byte()) - 209●
for c in value.to_array() { - 210●
let code = c.to_int() - 211
if code < 128 { - 212●
out.push(code.to_byte()) - 213
} else { - 214○
out.push(b'?') - 215
} - 216
} - 217●
Bytes::from_array(out) - 218
} - 219
- 220
///| - 221
/// 构造 IPTC APP13 段数据(不含 FF ED 前缀和长度) - 222
pub fn create_iptc_segment(info : IptcInfo) -> Bytes { - 223
// 构造 IPTC 记录 - 224●
let records : Array[Byte] = [] - 225●
if info.title.length() > 0 { - 226●
let r = iptc_build_record(2, 5, info.title) - 227
for b in r { - 228●
records.push(b) - 229
} - 230
} - 231●
if info.keywords.length() > 0 { - 232●
let r = iptc_build_record(2, 25, info.keywords) - 233
for b in r { - 234●
records.push(b) - 235
} - 236
} - 237●
if info.creator.length() > 0 { - 238●
let r = iptc_build_record(2, 80, info.creator) - 239
for b in r { - 240●
records.push(b) - 241
} - 242
} - 243●
if info.creator_title.length() > 0 { - 244●
let r = iptc_build_record(2, 85, info.creator_title) - 245
for b in r { - 246●
records.push(b) - 247
} - 248
} - 249●
if info.city.length() > 0 { - 250●
let r = iptc_build_record(2, 90, info.city) - 251
for b in r { - 252●
records.push(b) - 253
} - 254
} - 255●
if info.country.length() > 0 { - 256●
let r = iptc_build_record(2, 101, info.country) - 257
for b in r { - 258●
records.push(b) - 259
} - 260
} - 261●
if info.caption.length() > 0 { - 262●
let r = iptc_build_record(2, 120, info.caption) - 263
for b in r { - 264●
records.push(b) - 265
} - 266
} - 267●
if info.caption_writer.length() > 0 { - 268●
let r = iptc_build_record(2, 122, info.caption_writer) - 269
for b in r { - 270●
records.push(b) - 271
} - 272
} - 273●
if info.date_created.length() > 0 { - 274●
let r = iptc_build_record(2, 55, info.date_created) - 275
for b in r { - 276●
records.push(b) - 277
} - 278
} - 279●
if info.category.length() > 0 { - 280●
let r = iptc_build_record(2, 15, info.category) - 281
for b in r { - 282●
records.push(b) - 283
} - 284
} - 285
if info.urgency > 0 && info.urgency <= 9 { - 286●
let r = iptc_build_record(2, 10, info.urgency.to_string()) - 287
for b in r { - 288●
records.push(b) - 289
} - 290
} - 291●
let iptc_data = Bytes::from_array(records) - 292●
let iptc_data_len = iptc_data.length() - 293
// Photoshop 3.0\0 + 8BIM + 0x04 0x04 + 4-byte length + IPTC data - 294
let out : Array[Byte] = [] - 295●
let header = iptc_photoshop_header() - 296●
for c in header.to_array() { - 297●
out.push(c.to_int().to_byte()) - 298
} - 299●
out.push(b'\x00') - 300●
out.push(0x38) // '8' - 301●
out.push(0x42) // 'B' - 302●
out.push(0x49) // 'I' - 303●
out.push(0x4D) // 'M' - 304●
out.push(0x04) - 305●
out.push(0x04) - 306●
out.push(((iptc_data_len >> 24) & 0xFF).to_byte()) - 307●
out.push(((iptc_data_len >> 16) & 0xFF).to_byte()) - 308●
out.push(((iptc_data_len >> 8) & 0xFF).to_byte()) - 309●
out.push((iptc_data_len & 0xFF).to_byte()) - 310
for b in iptc_data { - 311●
out.push(b) - 312
} - 313●
Bytes::from_array(out) - 314
} - 315
- 316
///| - 317
/// 将 IPTC 写入 JPEG(在 SOI 后插入 APP13,已有则替换) - 318
pub fn write_iptc_to_bytes( - 319
info : IptcInfo, - 320
jpeg_data : Bytes, - 321
) -> Bytes raise @types.LoadError { - 322●
let len = jpeg_data.length() - 323●
if len < 4 || jpeg_data[0].to_int() != 0xFF || jpeg_data[1].to_int() != 0xD8 { - 324●
raise @types.LoadError::EncodeFailed("输入不是有效 JPEG") - 325
} - 326●
let iptc_payload = create_iptc_segment(info) - 327●
let iptc_seg_len = iptc_payload.length() + 2 - 328
let iptc_seg : Array[Byte] = [ - 329
0xFF, - 330
0xED, - 331●
(iptc_seg_len >> 8).to_byte(), - 332●
(iptc_seg_len & 0xFF).to_byte(), - 333
] - 334
for b in iptc_payload { - 335●
iptc_seg.push(b) - 336
} - 337
// 查找并跳过已有 IPTC APP13 - 338
let mut pos = 2 - 339
let mut insert_pos = 2 - 340
while pos < len - 1 { - 341●
if jpeg_data[pos].to_int() == 0xFF && jpeg_data[pos + 1].to_int() == 0xED { - 342○
insert_pos = pos - 343
break - 344●
} else if jpeg_data[pos].to_int() == 0xFF { - 345●
let marker = jpeg_data[pos + 1].to_int() - 346
if marker == 0xDA || marker == 0xD9 { - 347●
insert_pos = pos - 348
break - 349
} - 350○
let seg_len = jpeg_data[pos + 2].to_int() * 256 + - 351○
jpeg_data[pos + 3].to_int() - 352
pos += 2 + seg_len - 353
} else { - 354○
pos += 1 - 355
} - 356
} - 357
let out : Array[Byte] = [] - 358
for i in 0..<insert_pos { - 359●
out.push(jpeg_data[i]) - 360
} - 361
for b in iptc_seg { - 362●
out.push(b) - 363
} - 364
for i in insert_pos..<len { - 365●
out.push(jpeg_data[i]) - 366
} - 367●
Bytes::from_array(out) - 368
}