image.codec\codec\tiff_extended.mbt

93.5% · 72 / 77 points · 155 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/// # TIFF 扩展 TIFF Extended
  3. 3///
  4. 4/// TIFF 扩展功能 TIFF extended features
  5. 5
  6. 6///|
  7. 7/// TIFF 扩展信息(包含压缩方式、光度解释、方向、分辨率等)
  8. 8pub struct TiffInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 bit_depth : Int32
  12. 12 samples_per_pixel : Int32
  13. 13 num_channels : Int32
  14. 14 has_alpha : Bool
  15. 15 compression : Int32
  16. 16 compression_name : String
  17. 17 photometric : Int32
  18. 18 photometric_name : String
  19. 19 orientation : Int32
  20. 20 resolution_unit : Int32
  21. 21 is_little_endian : Bool
  22. 22}
  23. 23
  24. 24///|
  25. 25/// 检测数据是否为 TIFF 格式
  26. 26pub fn is_tiff_format(data : Bytes) -> Bool {
  27. 27● let len = data.length()
  28. 28 if len < 8 {
  29. 29● return false
  30. 30 }
  31. 31 // Little-endian: II + 42
  32. 32● if data[0].to_int() == 0x49 &&
  33. 33● data[1].to_int() == 0x49 &&
  34. 34● data[2].to_int() == 0x2A &&
  35. 35● data[3].to_int() == 0x00 {
  36. 36● return true
  37. 37 }
  38. 38 // Big-endian: MM + 42
  39. 39● data[0].to_int() == 0x4D &&
  40. 40● data[1].to_int() == 0x4D &&
  41. 41● data[2].to_int() == 0x00 &&
  42. 42● data[3].to_int() == 0x2A
  43. 43}
  44. 44
  45. 45///|
  46. 46fn tiff_compression_name(c : Int32) -> String {
  47. 47● match c {
  48. 48● 1 => "none"
  49. 49● 2 => "CCITT RLE"
  50. 50● 5 => "LZW"
  51. 51● 6 => "JPEG"
  52. 52● 7 => "JPEG lossy"
  53. 53● 8 => "Deflate"
  54. 54● 32773 => "PackBits"
  55. 55● 32946 => "Deflate (old)"
  56. 56● _ => "unknown"
  57. 57 }
  58. 58}
  59. 59
  60. 60///|
  61. 61fn tiff_photometric_name(p : Int32) -> String {
  62. 62● match p {
  63. 63● 0 => "WhiteIsZero"
  64. 64● 1 => "BlackIsZero"
  65. 65● 2 => "RGB"
  66. 66● 3 => "Palette"
  67. 67● 4 => "TransparencyMask"
  68. 68● 5 => "CMYK"
  69. 69● 6 => "YCbCr"
  70. 70● 8 => "CIELab"
  71. 71● _ => "unknown"
  72. 72 }
  73. 73}
  74. 74
  75. 75///|
  76. 76/// 解析 TIFF 扩展头部信息
  77. 77///
  78. 78/// 遍历 IFD 标签,提取尺寸、位深、压缩方式、光度解释、方向、分辨率单位等
  79. 79pub fn tiff_info_extended(
  80. 80 data : Bytes,
  81. 81) -> TiffInfoExtended raise @types.LoadError {
  82. 82● if !is_tiff_format(data) {
  83. 83● raise @types.LoadError::UnsupportedFormat("Not a TIFF file")
  84. 84 }
  85. 85● let len = data.length()
  86. 86● let little_endian = data[0].to_int() == 0x49
  87. 87 let ctx = TiffContext::{ data, little_endian, }
  88. 88● let ifd_offset = tiff_read_u32(ctx, 4)
  89. 89 if ifd_offset < 0 || ifd_offset >= len {
  90. 90● raise @types.LoadError::DecodeFailed("TIFF IFD 偏移无效")
  91. 91 }
  92. 92● let entries = tiff_parse_ifd(ctx, ifd_offset)
  93. 93● let width = match tiff_get_value(entries, ctx, 256) {
  94. 94● Some(w) => w
  95. 95● None => raise @types.LoadError::DecodeFailed("TIFF 缺少 ImageWidth")
  96. 96 }
  97. 97● let height = match tiff_get_value(entries, ctx, 257) {
  98. 98● Some(h) => h
  99. 99● None => raise @types.LoadError::DecodeFailed("TIFF 缺少 ImageLength")
  100. 100 }
  101. 101● let bits_per_sample = match tiff_get_values(entries, ctx, 258) {
  102. 102◐ Some(arr) => if arr.length() > 0 { arr[0] } else { 8 }
  103. 103● None => 8
  104. 104 }
  105. 105● let compression = match tiff_get_value(entries, ctx, 259) {
  106. 106● Some(c) => c
  107. 107● None => 1
  108. 108 }
  109. 109● let photometric = match tiff_get_value(entries, ctx, 262) {
  110. 110● Some(p) => p
  111. 111● None => 1
  112. 112 }
  113. 113● let samples_per_pixel = match tiff_get_value(entries, ctx, 277) {
  114. 114● Some(s) => s
  115. 115● None => 1
  116. 116 }
  117. 117● let orientation = match tiff_get_value(entries, ctx, 274) {
  118. 118○ Some(o) => o
  119. 119● None => 1
  120. 120 }
  121. 121● let resolution_unit = match tiff_get_value(entries, ctx, 296) {
  122. 122○ Some(r) => r
  123. 123● None => 2
  124. 124 }
  125. 125● let extra_samples = match tiff_get_value(entries, ctx, 338) {
  126. 126○ Some(e) => e
  127. 127● None => 0
  128. 128 }
  129. 129 if width <= 0 || height <= 0 {
  130. 130● raise @types.LoadError::DecodeFailed("TIFF 尺寸无效")
  131. 131 }
  132. 132 let has_alpha = extra_samples > 0 ||
  133. 133 samples_per_pixel == 4 ||
  134. 134 (samples_per_pixel == 2 && photometric == 1)
  135. 135 let num_channels = if has_alpha {
  136. 136○ samples_per_pixel
  137. 137 } else {
  138. 138● samples_per_pixel
  139. 139 }
  140. 140 TiffInfoExtended::{
  141. 141 width,
  142. 142 height,
  143. 143 bit_depth: bits_per_sample,
  144. 144 samples_per_pixel,
  145. 145 num_channels,
  146. 146 has_alpha,
  147. 147 compression,
  148. 148● compression_name: tiff_compression_name(compression),
  149. 149 photometric,
  150. 150● photometric_name: tiff_photometric_name(photometric),
  151. 151 orientation,
  152. 152 resolution_unit,
  153. 153 is_little_endian: little_endian,
  154. 154 }
  155. 155}