image.codec\meta\png_meta.mbt

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