image.codec\meta\png_meta_write.mbt

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