image.codec\codec\psd_extended.mbt

65.2% · 30 / 46 points · 116 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/// # PSD 扩展 PSD Extended
  3. 3///
  4. 4/// PSD 扩展功能 PSD extended features
  5. 5
  6. 6///|
  7. 7/// PSD 扩展信息(包含通道数、位深、颜色模式、压缩方式等)
  8. 8pub struct PsdInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 bit_depth : Int32
  12. 12 channel_count : Int32
  13. 13 num_channels : Int32
  14. 14 has_alpha : Bool
  15. 15 color_mode : Int32
  16. 16 color_mode_name : String
  17. 17 compression : Int32
  18. 18 compression_name : String
  19. 19 version : Int32
  20. 20}
  21. 21
  22. 22///|
  23. 23/// 检测数据是否为 PSD 格式
  24. 24pub fn is_psd_format(data : Bytes) -> Bool {
  25. 25● let len = data.length()
  26. 26 if len < 26 {
  27. 27● return false
  28. 28 }
  29. 29 // signature "8BPS" (0x38 0x42 0x50 0x53)
  30. 30● data[0].to_int() == 0x38 &&
  31. 31● data[1].to_int() == 0x42 &&
  32. 32● data[2].to_int() == 0x50 &&
  33. 33● data[3].to_int() == 0x53
  34. 34}
  35. 35
  36. 36///|
  37. 37fn psd_color_mode_name(m : Int32) -> String {
  38. 38● match m {
  39. 39○ 0 => "Bitmap"
  40. 40○ 1 => "Grayscale"
  41. 41○ 2 => "Indexed"
  42. 42● 3 => "RGB"
  43. 43○ 4 => "CMYK"
  44. 44○ 7 => "Multichannel"
  45. 45○ 8 => "Duotone"
  46. 46○ 9 => "Lab"
  47. 47○ _ => "unknown"
  48. 48 }
  49. 49}
  50. 50
  51. 51///|
  52. 52fn psd_compression_name(c : Int32) -> String {
  53. 53● match c {
  54. 54● 0 => "Raw"
  55. 55○ 1 => "RLE"
  56. 56○ 2 => "ZIP without prediction"
  57. 57○ 3 => "ZIP with prediction"
  58. 58○ _ => "unknown"
  59. 59 }
  60. 60}
  61. 61
  62. 62///|
  63. 63/// 解析 PSD 扩展头部信息
  64. 64///
  65. 65/// 解析 PSD 文件头,提取尺寸、通道数、位深、颜色模式、压缩方式等
  66. 66pub fn psd_info_extended(
  67. 67 data : Bytes,
  68. 68) -> PsdInfoExtended raise @types.LoadError {
  69. 69● if !is_psd_format(data) {
  70. 70● raise @types.LoadError::UnsupportedFormat("Not a PSD file")
  71. 71 }
  72. 72● let len = data.length()
  73. 73● let version = read_u16_be(data, 4)
  74. 74● let channel_count = read_u16_be(data, 12)
  75. 75● let height = read_u32_be(data, 14)
  76. 76● let width = read_u32_be(data, 18)
  77. 77● let bit_depth = read_u16_be(data, 22)
  78. 78● let color_mode = read_u16_be(data, 24)
  79. 79 if width == 0 || height == 0 {
  80. 80○ raise @types.LoadError::DecodeFailed("PSD 尺寸无效")
  81. 81 }
  82. 82 // 跳过 3 个 length 前缀段(color mode data / image resources / layer and mask data)
  83. 83 let mut pos = 26
  84. 84 let mut compression = 0
  85. 85 for seg_idx in 0..<3 {
  86. 86● if pos + 4 > len {
  87. 87○ break
  88. 88 }
  89. 89● let seg_len = read_u32_be(data, pos)
  90. 90 pos += 4
  91. 91 if seg_len > len - pos {
  92. 92○ break
  93. 93 }
  94. 94 pos += seg_len
  95. 95 let _ = seg_idx
  96. 96 }
  97. 97 // Image data: compression (2 bytes BE)
  98. 98 if pos + 2 <= len {
  99. 99● compression = read_u16_be(data, pos)
  100. 100 }
  101. 101 let has_alpha = channel_count == 4 || channel_count == 5
  102. 102◐ let num_channels = if channel_count > 4 { 4 } else { channel_count }
  103. 103 PsdInfoExtended::{
  104. 104 width,
  105. 105 height,
  106. 106 bit_depth,
  107. 107 channel_count,
  108. 108 num_channels,
  109. 109 has_alpha,
  110. 110 color_mode,
  111. 111● color_mode_name: psd_color_mode_name(color_mode),
  112. 112 compression,
  113. 113● compression_name: psd_compression_name(compression),
  114. 114 version,
  115. 115 }
  116. 116}