image.codec\codec\hdr_encode.mbt
98.4% · 63 / 64 points · 146 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # HDR 编码 HDR Encode - 3
/// - 4
/// HDR Radiance 格式编码 HDR Radiance format encoding - 5
// 纯 MoonBit HDR 编码(全目标可用:native/wasm/js) - 6
// RGBE 格式编码,使用 RLE 压缩扫描线 - 7
// 仅依赖 @types,不依赖 @core/FFI - 8
- 9
///| - 10
/// RLE 编码单个通道扫描线 - 11
fn rle_encode_scanline_channel( - 12
channel_data : Array[Byte], - 13
scanline_len : Int32, - 14
) -> Array[Byte] { - 15●
let out : Array[Byte] = [] - 16
let mut pos = 0 - 17
while pos < scanline_len { - 18
// 查找重复 run - 19●
let mut run_len = 1 - 20
while pos + run_len < scanline_len && - 21
channel_data[pos + run_len] == channel_data[pos] && - 22
run_len < 127 { - 23●
run_len += 1 - 24
} - 25
if run_len >= 4 { - 26
// RLE 编码 - 27●
out.push((128 + run_len).to_byte()) - 28●
out.push(channel_data[pos]) - 29
pos += run_len - 30
} else { - 31
// 查找非重复 run - 32●
let mut lit_len = 1 - 33
while pos + lit_len < scanline_len && lit_len < 128 { - 34
// 检查是否开始新的重复 run - 35●
let mut next_run = 0 - 36
while pos + lit_len + next_run < scanline_len && - 37
channel_data[pos + lit_len + next_run] == - 38
channel_data[pos + lit_len] && - 39
next_run < 3 { - 40●
next_run += 1 - 41
} - 42
if next_run >= 4 { - 43○
break - 44
} - 45
lit_len += 1 - 46
} - 47
// 非重复编码 - 48●
out.push(lit_len.to_byte()) - 49
for i in 0..<lit_len { - 50●
out.push(channel_data[pos + i]) - 51
} - 52
pos += lit_len - 53
} - 54
} - 55
out - 56
} - 57
- 58
///| - 59
/// HDR 编码(纯 MoonBit,全目标可用)。输入 ImageF(32-bit float 像素数据),返回 HDR 格式字节流。 HDR encoding (pure MoonBit, all targets). Input ImageF (32-bit float pixel data), returns HDR format byte stream. - 60
- 61
///| - 62
/// 参数 Parameters: - 63
/// - `img` - 输入 ImageF Input ImageF - 64
/// - 65
/// 返回 Return: - 66
/// - HDR 字节流 HDR byte stream - 67
/// - 68
/// 错误 Errors: - 69
/// - `LoadError::EncodeFailed` - channels 不为 3 Channels not 3 - 70
pub fn encode_hdr_pure(img : @types.ImageF) -> Bytes raise @types.LoadError { - 71●
if img.channels != 3 { - 72●
raise @types.LoadError::EncodeFailed( - 73
"HDR 编码需要 3 通道 float 图像", - 74
) - 75
} - 76
let width = img.width - 77
let height = img.height - 78
let pixel_count = width * height - 79
// 构建输出 - 80
let out : Array[Byte] = [] - 81
// 写入 header - 82
let header_str = "#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n" - 83●
let header_bytes = @encoding/utf8.encode(header_str) - 84●
for i in 0..<header_bytes.length() { - 85●
out.push(header_bytes[i]) - 86
} - 87
// 写入尺寸行: "-Y HEIGHT +X WIDTH\n" - 88●
let dim_str = "-Y " + height.to_string() + " +X " + width.to_string() + "\n" - 89●
let dim_bytes = @encoding/utf8.encode(dim_str) - 90●
for i in 0..<dim_bytes.length() { - 91●
out.push(dim_bytes[i]) - 92
} - 93
// 将 float 像素转换为 RGBE - 94●
let rgbe : Array[Byte] = Array::make(pixel_count * 4, b'\x00') - 95
for i in 0..<pixel_count { - 96
// 读取 little-endian float - 97●
let r_int = img.data[i * 12].to_int() | - 98●
(img.data[i * 12 + 1].to_int() << 8) | - 99●
(img.data[i * 12 + 2].to_int() << 16) | - 100●
(img.data[i * 12 + 3].to_int() << 24) - 101●
let g_int = img.data[i * 12 + 4].to_int() | - 102●
(img.data[i * 12 + 5].to_int() << 8) | - 103●
(img.data[i * 12 + 6].to_int() << 16) | - 104●
(img.data[i * 12 + 7].to_int() << 24) - 105●
let b_int = img.data[i * 12 + 8].to_int() | - 106●
(img.data[i * 12 + 9].to_int() << 8) | - 107●
(img.data[i * 12 + 10].to_int() << 16) | - 108●
(img.data[i * 12 + 11].to_int() << 24) - 109●
let r = Float::reinterpret_from_int(r_int) - 110●
let g = Float::reinterpret_from_int(g_int) - 111●
let b = Float::reinterpret_from_int(b_int) - 112●
let (rb, gb, bb, eb) = float_to_rgbe(r, g, b) - 113
rgbe[i * 4] = rb - 114
rgbe[i * 4 + 1] = gb - 115
rgbe[i * 4 + 2] = bb - 116
rgbe[i * 4 + 3] = eb - 117
} - 118
// RLE 编码每行 - 119
if width < 8 || width > 32767 { - 120
// 小图或大图:不压缩 - 121●
for i in 0..<(pixel_count * 4) { - 122●
out.push(rgbe[i]) - 123
} - 124
} else { - 125
// RLE 编码 - 126●
for y in 0..<height { - 127
// 写入行头 [2, 2, width_hi, width_lo] - 128●
out.push(b'\x02') - 129●
out.push(b'\x02') - 130●
out.push((width >> 8).to_byte()) - 131●
out.push((width & 0xFF).to_byte()) - 132
// 4 通道分别 RLE 编码 - 133
for c in 0..<4 { - 134●
let channel_data : Array[Byte] = [] - 135
for x in 0..<width { - 136●
channel_data.push(rgbe[(y * width + x) * 4 + c]) - 137
} - 138●
let encoded = rle_encode_scanline_channel(channel_data, width) - 139●
for i in 0..<encoded.length() { - 140●
out.push(encoded[i]) - 141
} - 142
} - 143
} - 144
} - 145●
Bytes::from_array(out) - 146
}