image.codec\codec\hdr_extended.mbt

94.1% · 48 / 51 points · 119 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/// # HDR 扩展 HDR Extended
  3. 3///
  4. 4/// HDR 扩展功能 HDR extended features
  5. 5
  6. 6///|
  7. 7/// HDR 扩展信息(包含尺寸、通道数等)
  8. 8pub struct HdrInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 num_channels : Int32
  12. 12 has_alpha : Bool
  13. 13 format : String
  14. 14}
  15. 15
  16. 16///|
  17. 17/// 检测数据是否为 HDR (Radiance RGBE) 格式
  18. 18pub fn is_hdr_format(data : Bytes) -> Bool {
  19. 19● let len = data.length()
  20. 20 if len < 2 {
  21. 21● return false
  22. 22 }
  23. 23● data[0].to_int() == 0x23 && data[1].to_int() == 0x3F
  24. 24}
  25. 25
  26. 26///|
  27. 27/// 解析 HDR 扩展头部信息
  28. 28pub fn hdr_info_extended(
  29. 29 data : Bytes,
  30. 30) -> HdrInfoExtended raise @types.LoadError {
  31. 31● if !is_hdr_format(data) {
  32. 32● raise @types.LoadError::UnsupportedFormat("Not an HDR file")
  33. 33 }
  34. 34● let len = data.length()
  35. 35 let mut pos = 2
  36. 36 // 跳过 magic 行
  37. 37● while pos < len && data[pos].to_int() != 0x0A {
  38. 38● pos += 1
  39. 39 }
  40. 40 pos += 1
  41. 41 // 跳过 header 行,查找尺寸行
  42. 42 let mut format = "RGBE"
  43. 43 while pos < len {
  44. 44● let b = data[pos].to_int()
  45. 45 if b == 0x2D || b == 0x2B {
  46. 46● break
  47. 47 }
  48. 48 // 检查 FORMAT= 行
  49. 49 if pos + 7 <= len &&
  50. 50● data[pos].to_int() == 0x46 &&
  51. 51● data[pos + 1].to_int() == 0x4F &&
  52. 52● data[pos + 2].to_int() == 0x52 &&
  53. 53● data[pos + 3].to_int() == 0x4D &&
  54. 54● data[pos + 4].to_int() == 0x41 &&
  55. 55● data[pos + 5].to_int() == 0x54 &&
  56. 56● data[pos + 6].to_int() == 0x3D {
  57. 57 // FORMAT= 后面的值
  58. 58● let fmt_start = pos + 7
  59. 59 let mut fmt_end = fmt_start
  60. 60● while fmt_end < len && data[fmt_end].to_int() != 0x0A {
  61. 61● fmt_end += 1
  62. 62 }
  63. 63 let fmt_bytes : Array[Byte] = []
  64. 64 for i in fmt_start..<fmt_end {
  65. 65● fmt_bytes.push(data[i])
  66. 66 }
  67. 67● format = @encoding/utf8.decode(Bytes::from_array(fmt_bytes)) catch {
  68. 68○ _ => "RGBE"
  69. 69 }
  70. 70 }
  71. 71 // 跳过当前行
  72. 72● while pos < len && data[pos].to_int() != 0x0A {
  73. 73● pos += 1
  74. 74 }
  75. 75 pos += 1
  76. 76 }
  77. 77 // 解析尺寸行: "-Y HEIGHT +X WIDTH\n"
  78. 78 let mut width = 0
  79. 79 let mut height = 0
  80. 80 let vals : Array[Int32] = []
  81. 81● while pos < len && data[pos].to_int() != 0x0A {
  82. 82● let b = data[pos].to_int()
  83. 83 if b == 0x2D || b == 0x2B {
  84. 84● pos += 1
  85. 85 // 跳过字母 (Y/X)
  86. 86 while pos < len &&
  87. 87● data[pos].to_int() != 0x2D &&
  88. 88● data[pos].to_int() != 0x2B &&
  89. 89● data[pos].to_int() != 0x0A {
  90. 90● let c = data[pos].to_int()
  91. 91 if c >= 0x30 && c <= 0x39 {
  92. 92● let mut num = 0
  93. 93 while pos < len {
  94. 94● let d = data[pos].to_int()
  95. 95 if d >= 0x30 && d <= 0x39 {
  96. 96● num = num * 10 + (d - 0x30)
  97. 97 pos += 1
  98. 98 } else {
  99. 99● break
  100. 100 }
  101. 101 }
  102. 102● vals.push(num)
  103. 103 } else {
  104. 104● pos += 1
  105. 105 }
  106. 106 }
  107. 107 } else {
  108. 108○ pos += 1
  109. 109 }
  110. 110 }
  111. 111● if vals.length() >= 2 {
  112. 112● height = vals[0]
  113. 113 width = vals[1]
  114. 114 }
  115. 115 if width == 0 || height == 0 {
  116. 116○ raise @types.LoadError::DecodeFailed("HDR 尺寸无效")
  117. 117 }
  118. 118 HdrInfoExtended::{ width, height, num_channels: 3, has_alpha: false, format, }
  119. 119}