image.vision\imgir\ir_serialize.mbt

93.1% · 121 / 130 points · 213 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/// # ImgIR 序列化 ImgIR Serialization
  3. 3///
  4. 4/// 管道 ↔ Bytes 双向转换
  5. 5/// 管线↔Bytes 双向转换 Pipeline ↔ Bytes bidirectional conversion
  6. 6// ImgIR 序列化:管道 ↔ Bytes 双向转换。
  7. 7// IR serialization: pipeline ↔ Bytes bidirectional conversion.
  8. 8//
  9. 9// 二进制格式:
  10. 10// 管道 = num_ops(4B LE) + op_1 + op_2 + ...
  11. 11// 操作 = tag(1B) + params(变长)
  12. 12// Int = 4B little-endian
  13. 13// Float = 4B (scaled by 1e6, stored as Int LE)
  14. 14
  15. 15///|
  16. 16fn ir_write_int(buf : Array[Byte], v : Int32) -> Unit {
  17. 17● buf.push((v & 0xFF).to_byte())
  18. 18● buf.push(((v >> 8) & 0xFF).to_byte())
  19. 19● buf.push(((v >> 16) & 0xFF).to_byte())
  20. 20● buf.push(((v >> 24) & 0xFF).to_byte())
  21. 21}
  22. 22
  23. 23///|
  24. 24fn ir_write_float(buf : Array[Byte], v : Float) -> Unit {
  25. 25● let scaled = (v * 1000000.0F).to_int()
  26. 26● ir_write_int(buf, scaled)
  27. 27}
  28. 28
  29. 29///|
  30. 30fn ir_read_int(data : Bytes, offset : Int32) -> Int32 {
  31. 31● data[offset].to_int() |
  32. 32● (data[offset + 1].to_int() << 8) |
  33. 33● (data[offset + 2].to_int() << 16) |
  34. 34● (data[offset + 3].to_int() << 24)
  35. 35}
  36. 36
  37. 37///|
  38. 38fn ir_read_float(data : Bytes, offset : Int32) -> Float {
  39. 39● let scaled = ir_read_int(data, offset)
  40. 40● Float::from_int(scaled) / 1000000.0F
  41. 41}
  42. 42
  43. 43///|
  44. 44/// 序列化单个操作到缓冲区。
  45. 45/// 序列化单个操作到缓冲区 Serialize a single operation to buffer.
  46. 46pub fn ir_serialize_op(op : IROp, buf : Array[Byte]) -> Unit {
  47. 47● match op {
  48. 48● Resize(w, h) => {
  49. 49● buf.push(b'\x01')
  50. 50● ir_write_int(buf, w)
  51. 51● ir_write_int(buf, h)
  52. 52 }
  53. 53● Rotate90 => buf.push(b'\x02')
  54. 54● FlipH => buf.push(b'\x03')
  55. 55○ FlipV => buf.push(b'\x04')
  56. 56● Crop(x, y, w, h) => {
  57. 57● buf.push(b'\x05')
  58. 58● ir_write_int(buf, x)
  59. 59● ir_write_int(buf, y)
  60. 60● ir_write_int(buf, w)
  61. 61● ir_write_int(buf, h)
  62. 62 }
  63. 63● ToGray => buf.push(b'\x06')
  64. 64● Brightness(delta) => {
  65. 65● buf.push(b'\x07')
  66. 66● ir_write_int(buf, delta)
  67. 67 }
  68. 68● GaussianBlur(radius, sigma) => {
  69. 69● buf.push(b'\x08')
  70. 70● ir_write_int(buf, radius)
  71. 71● ir_write_float(buf, sigma)
  72. 72 }
  73. 73● BoxBlur(radius) => {
  74. 74● buf.push(b'\x09')
  75. 75● ir_write_int(buf, radius)
  76. 76 }
  77. 77● Sharpen(amount) => {
  78. 78● buf.push(b'\x0A')
  79. 79● ir_write_float(buf, amount)
  80. 80 }
  81. 81● ToScalarField => buf.push(b'\x0B')
  82. 82● FieldGradient => buf.push(b'\x0C')
  83. 83● FieldLaplacian => buf.push(b'\x0D')
  84. 84○ FieldIntegrate => buf.push(b'\x0E')
  85. 85● FieldClamp(lo, hi) => {
  86. 86● buf.push(b'\x0F')
  87. 87● ir_write_float(buf, lo)
  88. 88● ir_write_float(buf, hi)
  89. 89 }
  90. 90● FieldMask(threshold) => {
  91. 91● buf.push(b'\x10')
  92. 92● ir_write_float(buf, threshold)
  93. 93 }
  94. 94● ScalarFieldToImage => buf.push(b'\x11')
  95. 95● STSnapshot(time) => {
  96. 96● buf.push(b'\x12')
  97. 97● ir_write_float(buf, time)
  98. 98 }
  99. 99● STInterpolate(time) => {
  100. 100● buf.push(b'\x13')
  101. 101● ir_write_float(buf, time)
  102. 102 }
  103. 103 }
  104. 104}
  105. 105
  106. 106///|
  107. 107/// 反序列化单个操作。
  108. 108/// 反序列化单个操作 Deserialize a single operation.
  109. 109pub fn ir_deserialize_op(
  110. 110 data : Bytes,
  111. 111 offset : Int32,
  112. 112) -> (IROp, Int32) raise @types.LoadError {
  113. 113● let tag = data[offset].to_int()
  114. 114 let pos = offset + 1
  115. 115● match tag {
  116. 116● 0x01 => {
  117. 117● let w = ir_read_int(data, pos)
  118. 118● let h = ir_read_int(data, pos + 4)
  119. 119 (Resize(w, h), pos + 8)
  120. 120 }
  121. 121● 0x02 => (Rotate90, pos)
  122. 122● 0x03 => (FlipH, pos)
  123. 123○ 0x04 => (FlipV, pos)
  124. 124● 0x05 => {
  125. 125● let x = ir_read_int(data, pos)
  126. 126● let y = ir_read_int(data, pos + 4)
  127. 127● let w = ir_read_int(data, pos + 8)
  128. 128● let h = ir_read_int(data, pos + 12)
  129. 129 (Crop(x, y, w, h), pos + 16)
  130. 130 }
  131. 131● 0x06 => (ToGray, pos)
  132. 132● 0x07 => {
  133. 133● let delta = ir_read_int(data, pos)
  134. 134 (Brightness(delta), pos + 4)
  135. 135 }
  136. 136● 0x08 => {
  137. 137● let radius = ir_read_int(data, pos)
  138. 138● let sigma = ir_read_float(data, pos + 4)
  139. 139 (GaussianBlur(radius, sigma), pos + 8)
  140. 140 }
  141. 141● 0x09 => {
  142. 142● let radius = ir_read_int(data, pos)
  143. 143 (BoxBlur(radius), pos + 4)
  144. 144 }
  145. 145● 0x0A => {
  146. 146● let amount = ir_read_float(data, pos)
  147. 147 (Sharpen(amount), pos + 4)
  148. 148 }
  149. 149● 0x0B => (ToScalarField, pos)
  150. 150● 0x0C => (FieldGradient, pos)
  151. 151● 0x0D => (FieldLaplacian, pos)
  152. 152○ 0x0E => (FieldIntegrate, pos)
  153. 153● 0x0F => {
  154. 154● let lo = ir_read_float(data, pos)
  155. 155● let hi = ir_read_float(data, pos + 4)
  156. 156 (FieldClamp(lo, hi), pos + 8)
  157. 157 }
  158. 158● 0x10 => {
  159. 159● let threshold = ir_read_float(data, pos)
  160. 160 (FieldMask(threshold), pos + 4)
  161. 161 }
  162. 162● 0x11 => (ScalarFieldToImage, pos)
  163. 163● 0x12 => {
  164. 164● let time = ir_read_float(data, pos)
  165. 165 (STSnapshot(time), pos + 4)
  166. 166 }
  167. 167● 0x13 => {
  168. 168● let time = ir_read_float(data, pos)
  169. 169 (STInterpolate(time), pos + 4)
  170. 170 }
  171. 171 _ =>
  172. 172○ raise @types.LoadError::DecodeFailed(
  173. 173○ "ir_deserialize_op: 未知操作标签 " + tag.to_string(),
  174. 174 )
  175. 175 }
  176. 176}
  177. 177
  178. 178///|
  179. 179/// 序列化管道为 Bytes。
  180. 180/// 序列化管线 Serialize pipeline
  181. 181pub fn ir_serialize(pipeline : IRPipeline) -> Bytes {
  182. 182● let buf : Array[Byte] = []
  183. 183● ir_write_int(buf, pipeline.ops.length())
  184. 184● for i in 0..<pipeline.ops.length() {
  185. 185● ir_serialize_op(pipeline.ops[i], buf)
  186. 186 }
  187. 187● Bytes::from_array(buf)
  188. 188}
  189. 189
  190. 190///|
  191. 191/// 从 Bytes 反序列化管道。
  192. 192/// 从 Bytes 反序列化管线 Deserialize pipeline from Bytes.
  193. 193pub fn ir_deserialize(data : Bytes) -> IRPipeline raise @types.LoadError {
  194. 194● if data.length() < 4 {
  195. 195● raise @types.LoadError::DecodeFailed(
  196. 196 "ir_deserialize: 数据过短,至少需要 4 字节",
  197. 197 )
  198. 198 }
  199. 199● let num_ops = ir_read_int(data, 0)
  200. 200 if num_ops < 0 {
  201. 201○ raise @types.LoadError::DecodeFailed(
  202. 202 "ir_deserialize: 操作数不能为负",
  203. 203 )
  204. 204 }
  205. 205 let ops : Array[IROp] = []
  206. 206 let mut offset = 4
  207. 207 for _ in 0..<num_ops {
  208. 208● let result = ir_deserialize_op(data, offset)
  209. 209● ops.push(result.0)
  210. 210 offset = result.1
  211. 211 }
  212. 212 { ops, }
  213. 213}