image.codec\meta\xmp_meta.mbt
93.9% · 170 / 181 points · 350 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # XMP 元数据 meta/xmp_meta - 3
/// - 4
/// 编解码工具模块 - 5
/// 编解码工具模块 Codec utility module - 6
// v249.0.0 XMP 元数据解析与写入 - 7
// XMP 基于 XML,嵌入在 JPEG APP1 段中(以 "http://ns.adobe.com/xap/1.0/\0" 开头) - 8
// 纯 MoonBit 实现,无外部 XML 库依赖 - 9
- 10
///| - 11
/// XMP 元数据(常用字段) - 12
#derive(Eq, Default, @debug.Debug) - 13
pub(all) struct XmpInfo { - 14
creator : String - 15
title : String - 16
description : String - 17
subject : String - 18
keywords : String - 19
rights : String - 20
create_date : String - 21
modify_date : String - 22
software : String - 23
rating : Int32 - 24
raw_xml : String - 25
} - 26
- 27
///| - 28
pub extend XmpInfo with Eq::{not_equal, equal} - 29
- 30
///| - 31
pub extend XmpInfo with Default::{default} - 32
- 33
///| - 34
pub extend XmpInfo with @debug.Debug::{to_repr} - 35
- 36
///| - 37
/// XMP 命名空间 URI XMP namespace URI - 38
fn xmp_namespace_uri() -> String { - 39●
"http://ns.adobe.com/xap/1.0/" - 40
} - 41
- 42
///| - 43
/// 从字节中提取 ASCII 字符串(到 \0 或指定长度) - 44
fn xmp_extract_string(data : Bytes, start : Int32, max_len : Int32) -> String { - 45●
let chars : Array[Byte] = [] - 46●
let len = data.length() - 47
for i in 0..<max_len { - 48●
if start + i >= len { - 49○
break - 50
} - 51
let b = data[start + i] - 52
if b == b'\x00' { - 53●
break - 54
} - 55●
chars.push(b) - 56
} - 57●
@encoding/utf8.decode(Bytes::from_array(chars)[:]) catch { - 58●
_ => "" - 59
} - 60
} - 61
- 62
///| - 63
/// 在字符串中查找子串,返回起始位置或 -1 - 64
fn xmp_str_find(haystack : String, needle : String) -> Int32 { - 65●
let h = haystack.to_array() - 66●
let n = needle.to_array() - 67●
let hlen = h.length() - 68●
let nlen = n.length() - 69
if nlen == 0 { - 70○
return 0 - 71
} - 72
if nlen > hlen { - 73●
return -1 - 74
} - 75
let end = hlen - nlen - 76
for i in 0..<=end { - 77●
let mut found = true - 78
for j in 0..<nlen { - 79●
if h[i + j] != n[j] { - 80●
found = false - 81
break - 82
} - 83
} - 84
if found { - 85●
return i - 86
} - 87
} - 88
-1 - 89
} - 90
- 91
///| - 92
/// 在 XML 中提取标签内容(简单解析,非完整 XML 解析器) - 93
fn xmp_extract_tag(xml : String, tag : String) -> String { - 94●
let open_tag = "<" + tag + ">" - 95
let close_tag = "</" + tag + ">" - 96●
let open_idx = xmp_str_find(xml, open_tag) - 97
if open_idx < 0 { - 98●
return "" - 99
} - 100●
let content_start = open_idx + open_tag.length() - 101●
let close_idx = xmp_str_find(xml[content_start:].to_owned(), close_tag) - 102
if close_idx < 0 { - 103○
return "" - 104
} - 105●
xml[content_start:content_start + close_idx].to_owned() - 106
} - 107
- 108
///| - 109
/// 在 XML 中提取属性值 - 110
fn xmp_extract_attr(xml : String, tag : String, attr : String) -> String { - 111●
let search = tag + " " + attr + "=\"" - 112●
let idx = xmp_str_find(xml, search) - 113
if idx < 0 { - 114●
return "" - 115
} - 116○
let val_start = idx + search.length() - 117○
let rest = xml[val_start:].to_owned() - 118○
let end = xmp_str_find(rest, "\"") - 119
if end < 0 { - 120○
return "" - 121
} - 122○
rest[:end].to_owned() - 123
} - 124
- 125
///| - 126
/// 简单整数解析(支持正负号) - 127
fn xmp_parse_int(s : String) -> Int32? { - 128●
if s.length() == 0 { - 129●
return None - 130
} - 131●
let chars = s.to_array() - 132
let mut idx = 0 - 133
let mut neg = false - 134●
if chars[0].to_int() == 0x2D { - 135●
neg = true - 136
idx = 1 - 137●
} else if chars[0].to_int() == 0x2B { - 138●
idx = 1 - 139
} - 140●
if idx >= chars.length() { - 141○
return None - 142
} - 143
let mut result = 0 - 144●
while idx < chars.length() { - 145●
let c = chars[idx].to_int() - 146
if c < 0x30 || c > 0x39 { - 147●
return None - 148
} - 149
result = result * 10 + (c - 0x30) - 150
idx += 1 - 151
} - 152
if neg { - 153●
Some(-result) - 154
} else { - 155●
Some(result) - 156
} - 157
} - 158
- 159
///| - 160
/// 从 JPEG 字节流读取 XMP 元数据 - 161
/// 返回 None 如果不含 XMP 数据 - 162
pub fn read_xmp_from_bytes(data : Bytes) -> XmpInfo? { - 163●
let len = data.length() - 164●
if len < 4 || data[0].to_int() != 0xFF || data[1].to_int() != 0xD8 { - 165●
return None - 166
} - 167
// 查找 APP1 marker with XMP namespace - 168
let mut pos = 2 - 169
let mut xmp_start = -1 - 170
let mut xmp_len = 0 - 171
while pos < len - 1 { - 172●
if data[pos].to_int() == 0xFF && data[pos + 1].to_int() == 0xE1 { - 173●
let seg_len = data[pos + 2].to_int() * 256 + data[pos + 3].to_int() - 2 - 174
let ns_start = pos + 4 - 175●
let ns = xmp_extract_string(data, ns_start, 29) - 176●
if ns == xmp_namespace_uri() { - 177●
xmp_start = ns_start + 29 - 178
xmp_len = seg_len - 29 - 179
break - 180
} - 181
pos += 2 + seg_len + 2 - 182●
} else if data[pos].to_int() == 0xFF { - 183●
let marker = data[pos + 1].to_int() - 184
if marker == 0xDA || marker == 0xD9 { - 185●
break - 186
} - 187●
let seg_len = data[pos + 2].to_int() * 256 + data[pos + 3].to_int() - 188
pos += 2 + seg_len - 189
} else { - 190●
pos += 1 - 191
} - 192
} - 193
if xmp_start < 0 || xmp_start + xmp_len > len { - 194●
return None - 195
} - 196●
let xml = xmp_extract_string(data, xmp_start, xmp_len) - 197●
let creator = xmp_extract_tag(xml, "dc:creator") - 198●
let title = xmp_extract_tag(xml, "dc:title") - 199●
let description = xmp_extract_tag(xml, "dc:description") - 200●
let subject = xmp_extract_tag(xml, "dc:subject") - 201●
let keywords = xmp_extract_tag(xml, "lr:hierarchicalSubject") - 202●
let rights = xmp_extract_tag(xml, "dc:rights") - 203●
let create_date = xmp_extract_attr(xml, "xmp:CreateDate", "xmp:CreateDate") - 204●
let modify_date = xmp_extract_attr(xml, "xmp:ModifyDate", "xmp:ModifyDate") - 205●
let software = xmp_extract_tag(xml, "xmp:CreatorTool") - 206●
let rating_str = xmp_extract_tag(xml, "xmp:Rating") - 207●
let rating = match xmp_parse_int(rating_str) { - 208●
Some(n) => n - 209●
None => 0 - 210
} - 211
Some(XmpInfo::{ - 212
creator, - 213
title, - 214
description, - 215
subject, - 216
keywords, - 217
rights, - 218
create_date, - 219
modify_date, - 220
software, - 221
rating, - 222
raw_xml: xml, - 223
}) - 224
} - 225
- 226
///| - 227
/// 构造 XMP APP1 段数据(不含 FF E1 前缀和长度) - 228
pub fn create_xmp_segment(info : XmpInfo) -> Bytes { - 229●
let ns = xmp_namespace_uri() - 230
let xml_parts : Array[String] = [] - 231●
xml_parts.push("<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>") - 232●
xml_parts.push("<x:xmpmeta xmlns:x='adobe:ns:meta/'>") - 233●
xml_parts.push( - 234
"<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>", - 235
) - 236●
xml_parts.push("<rdf:Description rdf:about=''") - 237●
if info.creator.length() > 0 { - 238●
xml_parts.push(" xmlns:dc='http://purl.org/dc/elements/1.1/'") - 239
} - 240●
if info.software.length() > 0 { - 241●
xml_parts.push(" xmlns:xmp='http://ns.adobe.com/xap/1.0/'>") - 242
} else { - 243●
xml_parts.push(">") - 244
} - 245●
if info.title.length() > 0 { - 246●
xml_parts.push("<dc:title>" + info.title + "</dc:title>") - 247
} - 248●
if info.creator.length() > 0 { - 249●
xml_parts.push("<dc:creator>" + info.creator + "</dc:creator>") - 250
} - 251●
if info.description.length() > 0 { - 252●
xml_parts.push("<dc:description>" + info.description + "</dc:description>") - 253
} - 254●
if info.rights.length() > 0 { - 255●
xml_parts.push("<dc:rights>" + info.rights + "</dc:rights>") - 256
} - 257●
if info.software.length() > 0 { - 258●
xml_parts.push("<xmp:CreatorTool>" + info.software + "</xmp:CreatorTool>") - 259
} - 260
if info.rating > 0 { - 261●
xml_parts.push("<xmp:Rating>" + info.rating.to_string() + "</xmp:Rating>") - 262
} - 263●
xml_parts.push("</rdf:Description>") - 264●
xml_parts.push("</rdf:RDF>") - 265●
xml_parts.push("</x:xmpmeta>") - 266●
xml_parts.push("<?xpacket end='w'?>") - 267
let mut xml = "" - 268
for part in xml_parts { - 269●
xml = xml + part - 270
} - 271
let out : Array[Byte] = [] - 272●
for c in ns.to_array() { - 273●
let code = c.to_int() - 274
if code < 128 { - 275●
out.push(code.to_byte()) - 276
} else { - 277○
out.push(b'?') - 278
} - 279
} - 280●
out.push(b'\x00') - 281●
for c in xml.to_array() { - 282●
let code = c.to_int() - 283
if code < 128 { - 284●
out.push(code.to_byte()) - 285
} else { - 286●
out.push(b'?') - 287
} - 288
} - 289●
Bytes::from_array(out) - 290
} - 291
- 292
///| - 293
/// 将 XMP 写入 JPEG(在 SOI 后插入 APP1,已有则替换) - 294
pub fn write_xmp_to_bytes( - 295
info : XmpInfo, - 296
jpeg_data : Bytes, - 297
) -> Bytes raise @types.LoadError { - 298●
let len = jpeg_data.length() - 299●
if len < 4 || jpeg_data[0].to_int() != 0xFF || jpeg_data[1].to_int() != 0xD8 { - 300●
raise @types.LoadError::EncodeFailed("输入不是有效 JPEG") - 301
} - 302●
let xmp_payload = create_xmp_segment(info) - 303●
let xmp_seg_len = xmp_payload.length() + 2 - 304
let xmp_seg : Array[Byte] = [ - 305
0xFF, - 306
0xE1, - 307●
(xmp_seg_len >> 8).to_byte(), - 308●
(xmp_seg_len & 0xFF).to_byte(), - 309
] - 310
for b in xmp_payload { - 311●
xmp_seg.push(b) - 312
} - 313
// 查找并跳过已有 XMP APP1 - 314
let mut pos = 2 - 315
let mut insert_pos = 2 - 316
while pos < len - 1 { - 317●
if jpeg_data[pos].to_int() == 0xFF && jpeg_data[pos + 1].to_int() == 0xE1 { - 318●
let seg_len = jpeg_data[pos + 2].to_int() * 256 + - 319●
jpeg_data[pos + 3].to_int() - 320●
let ns = xmp_extract_string(jpeg_data, pos + 4, 29) - 321●
if ns == xmp_namespace_uri() { - 322●
insert_pos = pos - 323
break - 324
} - 325
pos += 2 + seg_len - 326●
} else if jpeg_data[pos].to_int() == 0xFF { - 327●
let marker = jpeg_data[pos + 1].to_int() - 328
if marker == 0xDA || marker == 0xD9 { - 329●
insert_pos = pos - 330
break - 331
} - 332●
let seg_len = jpeg_data[pos + 2].to_int() * 256 + - 333●
jpeg_data[pos + 3].to_int() - 334
pos += 2 + seg_len - 335
} else { - 336●
pos += 1 - 337
} - 338
} - 339
let out : Array[Byte] = [] - 340
for i in 0..<insert_pos { - 341●
out.push(jpeg_data[i]) - 342
} - 343
for b in xmp_seg { - 344●
out.push(b) - 345
} - 346
for i in insert_pos..<len { - 347●
out.push(jpeg_data[i]) - 348
} - 349●
Bytes::from_array(out) - 350
}