image.codec\meta\iptc_meta.mbt

92.5% · 196 / 212 points · 368 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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