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