image.codec\codec\jpeg_extended.mbt

65.9% · 27 / 41 points · 134 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/// # JPEG 扩展 JPEG Extended
  3. 3///
  4. 4/// JPEG 扩展功能 JPEG extended features
  5. 5
  6. 6///|
  7. 7/// JPEG 扩展信息(包含精度、分量数、渐进标志等)
  8. 8pub struct JpegInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 precision : Int32
  12. 12 num_components : Int32
  13. 13 is_grayscale : Bool
  14. 14 is_progressive : Bool
  15. 15 sof_marker : Int32
  16. 16 sof_name : String
  17. 17 num_channels : Int32
  18. 18 has_alpha : Bool
  19. 19}
  20. 20
  21. 21///|
  22. 22/// 检测数据是否为 JPEG 格式
  23. 23pub fn is_jpeg_format(data : Bytes) -> Bool {
  24. 24● let len = data.length()
  25. 25 if len < 4 {
  26. 26● return false
  27. 27 }
  28. 28● data[0].to_int() == 0xFF &&
  29. 29● data[1].to_int() == 0xD8 &&
  30. 30● data[2].to_int() == 0xFF
  31. 31}
  32. 32
  33. 33///|
  34. 34fn jpeg_sof_name(marker : Int32) -> String {
  35. 35● match marker {
  36. 36● 0xC0 => "Baseline DCT"
  37. 37○ 0xC1 => "Extended sequential DCT"
  38. 38● 0xC2 => "Progressive DCT"
  39. 39○ 0xC3 => "Lossless sequential"
  40. 40○ 0xC5 => "Differential sequential DCT"
  41. 41○ 0xC6 => "Differential progressive DCT"
  42. 42○ 0xC7 => "Differential lossless"
  43. 43○ 0xC9 => "Extended sequential arithmetic"
  44. 44○ 0xCA => "Progressive arithmetic"
  45. 45○ 0xCB => "Lossless arithmetic"
  46. 46○ _ => "unknown"
  47. 47 }
  48. 48}
  49. 49
  50. 50///|
  51. 51fn jpeg_is_sof_marker(marker : Int32) -> Bool {
  52. 52● marker == 0xC0 ||
  53. 53 marker == 0xC1 ||
  54. 54 marker == 0xC2 ||
  55. 55 marker == 0xC3 ||
  56. 56 marker == 0xC5 ||
  57. 57 marker == 0xC6 ||
  58. 58 marker == 0xC7 ||
  59. 59 marker == 0xC9 ||
  60. 60 marker == 0xCA ||
  61. 61 marker == 0xCB
  62. 62}
  63. 63
  64. 64///|
  65. 65/// 解析 JPEG 扩展头部信息
  66. 66///
  67. 67/// 遍历 JPEG markers,查找 SOF marker 提取尺寸、精度、分量数、渐进标志
  68. 68pub fn jpeg_info_extended(
  69. 69 data : Bytes,
  70. 70) -> JpegInfoExtended raise @types.LoadError {
  71. 71● if !is_jpeg_format(data) {
  72. 72● raise @types.LoadError::UnsupportedFormat("Not a JPEG file")
  73. 73 }
  74. 74● let len = data.length()
  75. 75 let mut pos = 2
  76. 76 let mut width = 0
  77. 77 let mut height = 0
  78. 78 let mut precision = 8
  79. 79 let mut num_components = 3
  80. 80 let mut sof_marker = 0xC0
  81. 81 let mut found_sof = false
  82. 82 while pos + 2 <= len {
  83. 83● if data[pos].to_int() != 0xFF {
  84. 84○ pos += 1
  85. 85 continue
  86. 86 }
  87. 87● let marker = data[pos + 1].to_int()
  88. 88 if marker == 0x00 || marker == 0xFF {
  89. 89○ pos += 1
  90. 90 continue
  91. 91 }
  92. 92 if marker == 0xD8 || marker == 0xD9 {
  93. 93○ pos += 2
  94. 94 continue
  95. 95 }
  96. 96 if marker >= 0xD0 && marker <= 0xD7 {
  97. 97○ pos += 2
  98. 98 continue
  99. 99 }
  100. 100 if pos + 4 > len {
  101. 101○ break
  102. 102 }
  103. 103● let seg_len = read_u16_be(data, pos + 2)
  104. 104● if jpeg_is_sof_marker(marker) && pos + 9 <= len {
  105. 105● precision = data[pos + 4].to_int()
  106. 106● height = read_u16_be(data, pos + 5)
  107. 107● width = read_u16_be(data, pos + 7)
  108. 108● num_components = data[pos + 9].to_int()
  109. 109 sof_marker = marker
  110. 110 found_sof = true
  111. 111 break
  112. 112 }
  113. 113 pos += 2 + seg_len
  114. 114 }
  115. 115 if !found_sof || width == 0 || height == 0 {
  116. 116● raise @types.LoadError::DecodeFailed(
  117. 117 "JPEG: could not determine image dimensions",
  118. 118 )
  119. 119 }
  120. 120 let is_progressive = sof_marker == 0xC2
  121. 121 let is_grayscale = num_components == 1
  122. 122 JpegInfoExtended::{
  123. 123 width,
  124. 124 height,
  125. 125 precision,
  126. 126 num_components,
  127. 127 is_grayscale,
  128. 128 is_progressive,
  129. 129 sof_marker,
  130. 130● sof_name: jpeg_sof_name(sof_marker),
  131. 131 num_channels: num_components,
  132. 132 has_alpha: false,
  133. 133 }
  134. 134}