image.codec\codec\psd_decode.mbt

93.5% · 29 / 31 points · 120 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 Decode
  3. 3///
  4. 4/// Adobe PSD 格式解码 Adobe PSD format decoding
  5. 5// 纯 MoonBit PSD 解码器
  6. 6// 支持 8-bit RGB(channelCount=3)/ RGBA(channelCount=4)、无压缩(compression=0)
  7. 7// 大端序字节流,按通道排列像素数据交错为 RGBRGBRGB
  8. 8// 规范: Adobe Photoshop PSD File Format + stb_image.h:6126-6280 stbi__psd_load
  9. 9
  10. 10///|
  11. 11/// 解码 PSD 字节流为 Image,支持 8-bit RGB(channelCount=3)/ RGBA(channelCount=4)、无压缩(compression=0),像素数据按通道排列(RRRGGGBBB),交错为 Image.data 格式(RGBRGBRGB) Decode PSD byte stream to Image, Supports 8-bit RGB (channelCount=3) / RGBA (channelCount=4), uncompressed (compression=0), Pixel data stored per-channel (RRRGGGBBB), interleaved to Image.data format (RGBRGBRGB)
  12. 12
  13. 13///|
  14. 14/// 参数 Parameters:
  15. 15/// - `data` - PSD 字节流 PSD byte stream
  16. 16///
  17. 17/// 返回 Return:
  18. 18/// - 解码后的 8-bit Image Decoded 8-bit Image
  19. 19///
  20. 20/// 错误 Errors:
  21. 21/// - `LoadError::DecodeFailed` - 数据过短、8BPS 签名无效、不支持的颜色模式/压缩 Data too short, invalid 8BPS signature, unsupported color mode/compression
  22. 22pub fn decode_psd_pure(data : Bytes) -> @types.Image raise @types.LoadError {
  23. 23● let len = data.length()
  24. 24 // Header 26 字节
  25. 25 if len < 26 {
  26. 26● raise @types.LoadError::DecodeFailed(
  27. 27 "PSD 数据过短:无法读取完整 header",
  28. 28 )
  29. 29 }
  30. 30 // signature "8BPS" (0x38 0x42 0x50 0x53)
  31. 31● if data[0].to_int() != 0x38 ||
  32. 32● data[1].to_int() != 0x42 ||
  33. 33● data[2].to_int() != 0x50 ||
  34. 34● data[3].to_int() != 0x53 {
  35. 35● raise @types.LoadError::DecodeFailed("PSD signature 错误,非 \"8BPS\"")
  36. 36 }
  37. 37 // version (2 字节 BE),必须为 1
  38. 38● let version = read_u16_be(data, 4)
  39. 39 if version != 1 {
  40. 40● raise @types.LoadError::DecodeFailed("PSD version 错误,仅支持 1")
  41. 41 }
  42. 42 // reserved(6 字节) 跳过
  43. 43 // channelCount (2 字节 BE)
  44. 44● let channel_count = read_u16_be(data, 12)
  45. 45 if channel_count != 3 && channel_count != 4 {
  46. 46● raise @types.LoadError::DecodeFailed(
  47. 47 "不支持的 PSD channelCount,仅支持 3(RGB)和 4(RGBA)",
  48. 48 )
  49. 49 }
  50. 50 // h (4 字节 BE)
  51. 51● let h = read_u32_be(data, 14)
  52. 52 // w (4 字节 BE)
  53. 53● let w = read_u32_be(data, 18)
  54. 54 // bitdepth (2 字节 BE),仅支持 8
  55. 55● let bitdepth = read_u16_be(data, 22)
  56. 56 if bitdepth != 8 {
  57. 57● raise @types.LoadError::DecodeFailed(
  58. 58 "不支持的 PSD bitdepth,仅支持 8",
  59. 59 )
  60. 60 }
  61. 61 // colorMode (2 字节 BE),仅支持 3(RGB)
  62. 62● let color_mode = read_u16_be(data, 24)
  63. 63 if color_mode != 3 {
  64. 64● raise @types.LoadError::DecodeFailed(
  65. 65 "不支持的 PSD colorMode,仅支持 3(RGB)",
  66. 66 )
  67. 67 }
  68. 68 // 尺寸校验
  69. 69 if w == 0 || h == 0 {
  70. 70● raise @types.LoadError::DecodeFailed("PSD 尺寸无效")
  71. 71 }
  72. 72 // 跳过 3 个 length 前缀段(color mode data / image resources / layer and mask data)
  73. 73 let mut pos = 26
  74. 74 for _ in 0..<3 {
  75. 75● if pos + 4 > len {
  76. 76○ raise @types.LoadError::DecodeFailed(
  77. 77 "PSD 数据过短:无法读取 4 字节 BE",
  78. 78 )
  79. 79 }
  80. 80● let seg_len = read_u32_be(data, pos)
  81. 81 pos += 4
  82. 82 if seg_len > len - pos {
  83. 83● raise @types.LoadError::DecodeFailed("PSD 段数据越界")
  84. 84 }
  85. 85 pos += seg_len
  86. 86 }
  87. 87 // Image data: compression (2 字节 BE)
  88. 88 if pos + 2 > len {
  89. 89○ raise @types.LoadError::DecodeFailed(
  90. 90 "PSD 数据过短:无法读取 2 字节 BE",
  91. 91 )
  92. 92 }
  93. 93● let compression = read_u16_be(data, pos)
  94. 94 pos += 2
  95. 95 if compression != 0 {
  96. 96● raise @types.LoadError::DecodeFailed(
  97. 97 "不支持的 PSD compression,仅支持 0(无压缩)",
  98. 98 )
  99. 99 }
  100. 100 // 像素数据:按通道排列,每通道 w*h 字节
  101. 101 let pixel_count = w * h
  102. 102 let pixel_bytes = channel_count * pixel_count
  103. 103 if pos + pixel_bytes > len {
  104. 104● raise @types.LoadError::DecodeFailed("PSD 像素数据不足")
  105. 105 }
  106. 106 // 交错:data[i * channel_count + c] = raw[c * w * h + i]
  107. 107● let interleaved : Array[Byte] = Array::make(pixel_bytes, b'\x00')
  108. 108 for c in 0..<channel_count {
  109. 109● let channel_base = c * pixel_count
  110. 110 for i in 0..<pixel_count {
  111. 111● interleaved[i * channel_count + c] = data[pos + channel_base + i]
  112. 112 }
  113. 113 }
  114. 114 @types.Image::{
  115. 115 width: w,
  116. 116 height: h,
  117. 117 channels: channel_count,
  118. 118● data: Bytes::from_array(interleaved),
  119. 119 }
  120. 120}