image.core\types\image_types.mbt

38.5% · 5 / 13 points · 532 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/// # 核心图像类型 Core Image Types
  3. 3///
  4. 4/// 图像处理库的核心类型定义,提供 8-bit / 16-bit / float 三种像素精度的图像值对象、统一错误类型、格式枚举,以及安全算术函数的公开别名。 Core type definitions for the image processing library, providing 8-bit / 16-bit / float pixel precision image value objects, unified error type, format enum, and public aliases for safe arithmetic functions.
  5. 5
  6. 6///
  7. 7/// 核心类型 Core types:
  8. 8/// - `Image` - 8-bit 无符号像素图像 8-bit unsigned pixel image
  9. 9/// - `Image16` - 16-bit 无符号像素图像 16-bit unsigned pixel image
  10. 10/// - `ImageF` - 32-bit IEEE float 像素图像 32-bit IEEE float pixel image
  11. 11/// - `ImageInfo` - 图像几何信息(无像素数据)Image geometry info (no pixel data)
  12. 12/// - `AnyImage` - 统一图像容器 Unified image container
  13. 13/// - `GifAnimation` - GIF 动画解码结果 GIF animation decode result
  14. 14/// - `PngAnimation` - APNG 动画解码结果 APNG animation decode result
  15. 15/// - `LoadError` - 加载/解码/编码错误 Load/decode/encode error
  16. 16/// - `ImageFormat` - 图像格式枚举 Image format enum
  17. 17///
  18. 18/// 核心函数 Core functions:
  19. 19/// - `check_dims` - 校验图像维度安全 Validate image dimension safety
  20. 20/// - `dims_valid_check` - 纯函数维度校验 Pure function dimension validation
  21. 21/// - `safe_mul` / `safe_mul3` - 溢出安全乘法 Overflow-safe multiplication
  22. 22/// - `clamp_pixel_value` / `clamp_byte` / `clamp_coord` - 钳制函数 Clamp functions
  23. 23///
  24. 24/// 依赖 Dependencies:
  25. 25/// - `walkzzz/image.core/safe_math` - 安全算术与形式化证明 Safe arithmetic with formal proofs
  26. 26
  27. 27///|
  28. 28/// 解码结果值对象,承载图像几何元信息与像素数据(8-bit 无符号像素) Decoded image value object carrying geometry metadata and pixel data (8-bit unsigned pixels)
  29. 29///
  30. 30/// 像素数据存储在 `data` 字段中,按行优先排列,每像素 `channels` 个字节。典型通道数:1(灰度)、3(RGB)、4(RGBA)。 Pixel data is stored in the `data` field, row-major, with `channels` bytes per pixel. Typical channel counts: 1 (grayscale), 3 (RGB), 4 (RGBA).
  31. 31
  32. 32///|
  33. 33/// 字段 Fields:
  34. 34/// - `width` - 图像宽度(像素)Image width in pixels
  35. 35/// - `height` - 图像高度(像素)Image height in pixels
  36. 36/// - `channels` - 通道数(1/3/4)Number of channels (1/3/4)
  37. 37/// - `data` - 像素数据(长度 = width * height * channels)Pixel data (length = width * height * channels)
  38. 38#derive(Eq, @debug.Debug)
  39. 39pub(all) struct Image {
  40. 40 width : Int32
  41. 41 height : Int32
  42. 42 channels : Int32
  43. 43 data : Bytes
  44. 44}
  45. 45
  46. 46///|
  47. 47pub extend Image with @debug.Debug::{to_repr}
  48. 48
  49. 49///|
  50. 50pub extend Image with Eq::{not_equal, equal}
  51. 51
  52. 52///|
  53. 53/// 16-bit 解码结果值对象,像素数据为 16-bit 无符号整数(little-endian,每像素 2 字节) 16-bit decoded image value object, pixel data as 16-bit unsigned integers (little-endian, 2 bytes per pixel)
  54. 54///
  55. 55/// 字段 Fields:
  56. 56/// - `width` - 图像宽度(像素)Image width in pixels
  57. 57/// - `height` - 图像高度(像素)Image height in pixels
  58. 58/// - `channels` - 通道数(1/3/4)Number of channels (1/3/4)
  59. 59/// - `data` - 像素数据(长度 = width * height * channels * 2)Pixel data (length = width * height * channels * 2)
  60. 60#derive(Eq, @debug.Debug)
  61. 61pub(all) struct Image16 {
  62. 62 width : Int32
  63. 63 height : Int32
  64. 64 channels : Int32
  65. 65 data : Bytes
  66. 66}
  67. 67
  68. 68///|
  69. 69pub extend Image16 with @debug.Debug::{to_repr}
  70. 70
  71. 71///|
  72. 72pub extend Image16 with Eq::{not_equal, equal}
  73. 73
  74. 74///|
  75. 75/// HDR float 解码结果值对象,像素数据为 32-bit IEEE float(little-endian,每像素 4 字节) HDR float decoded image value object, pixel data as 32-bit IEEE float (little-endian, 4 bytes per pixel)
  76. 76///
  77. 77/// 字段 Fields:
  78. 78/// - `width` - 图像宽度(像素)Image width in pixels
  79. 79/// - `height` - 图像高度(像素)Image height in pixels
  80. 80/// - `channels` - 通道数(1/3/4)Number of channels (1/3/4)
  81. 81/// - `data` - 像素数据(长度 = width * height * channels * 4)Pixel data (length = width * height * channels * 4)
  82. 82#derive(Eq, @debug.Debug)
  83. 83pub(all) struct ImageF {
  84. 84 width : Int32
  85. 85 height : Int32
  86. 86 channels : Int32
  87. 87 data : Bytes
  88. 88}
  89. 89
  90. 90///|
  91. 91pub extend ImageF with @debug.Debug::{to_repr}
  92. 92
  93. 93///|
  94. 94pub extend ImageF with Eq::{not_equal, equal}
  95. 95
  96. 96///|
  97. 97/// 图像信息查询结果,仅含几何元信息,不加载像素数据 Image info query result, geometry metadata only, no pixel data loaded
  98. 98///
  99. 99/// 字段 Fields:
  100. 100/// - `width` - 图像宽度(像素)Image width in pixels
  101. 101/// - `height` - 图像高度(像素)Image height in pixels
  102. 102/// - `channels` - 通道数(1/3/4)Number of channels (1/3/4)
  103. 103#derive(Eq, @debug.Debug)
  104. 104pub(all) struct ImageInfo {
  105. 105 width : Int32
  106. 106 height : Int32
  107. 107 channels : Int32
  108. 108}
  109. 109
  110. 110///|
  111. 111pub extend ImageInfo with @debug.Debug::{to_repr}
  112. 112
  113. 113///|
  114. 114pub extend ImageInfo with Eq::{not_equal, equal}
  115. 115
  116. 116///|
  117. 117/// 动画 GIF 解码结果,包含多帧图像和每帧延迟(毫秒) Animated GIF decode result, containing multiple frames and per-frame delays (milliseconds)
  118. 118///
  119. 119/// 字段 Fields:
  120. 120/// - `frames` - 帧图像数组 Array of frame images
  121. 121/// - `delays` - 每帧延迟时间(毫秒)Per-frame delay in milliseconds
  122. 122#derive(Eq, @debug.Debug)
  123. 123pub(all) struct GifAnimation {
  124. 124 frames : Array[Image]
  125. 125 delays : Array[Int32]
  126. 126}
  127. 127
  128. 128///|
  129. 129pub extend GifAnimation with @debug.Debug::{to_repr}
  130. 130
  131. 131///|
  132. 132pub extend GifAnimation with Eq::{not_equal, equal}
  133. 133
  134. 134///|
  135. 135/// 加载失败领域错误 Load error domain type
  136. 136///
  137. 137/// 变体 Variants:
  138. 138/// - `FileIO(String)` - 文件 I/O 错误 File I/O error
  139. 139/// - `UnsupportedFormat(String)` - 不支持的图像格式 Unsupported image format
  140. 140/// - `DecodeFailed(String)` - 解码失败 Decode failure
  141. 141/// - `EncodeFailed(String)` - 编码失败 Encode failure
  142. 142pub(all) suberror LoadError {
  143. 143 FileIO(String)
  144. 144 UnsupportedFormat(String)
  145. 145 DecodeFailed(String)
  146. 146 EncodeFailed(String)
  147. 147}
  148. 148
  149. 149///|
  150. 150/// `LoadError` 实现 `Show` — 分类前缀 + 消息,使 `"\{err}"` 插值与 `inspect` 开箱可用(下游调试体验)。
  151. 151/// `LoadError` implements `Show` — category prefix + message, making interpolation and `inspect` work out of the box.
  152. 152pub impl Show for LoadError with fn output(self, logger) {
  153. 153○ logger.write_string(
  154. 154○ match self {
  155. 155○ FileIO(msg) => "FileIO: \{msg}"
  156. 156○ UnsupportedFormat(msg) => "UnsupportedFormat: \{msg}"
  157. 157○ DecodeFailed(msg) => "DecodeFailed: \{msg}"
  158. 158○ EncodeFailed(msg) => "EncodeFailed: \{msg}"
  159. 159 },
  160. 160 )
  161. 161}
  162. 162
  163. 163///|
  164. 164/// 补齐 `Show` 的 `to_string` 派生方法。Add the derived `to_string` from `Show`.
  165. 165pub extend LoadError with Show::{output, to_string}
  166. 166
  167. 167///|
  168. 168/// 图像格式枚举 Image format enum
  169. 169///
  170. 170/// 标识支持的图像编解码格式。`#non_exhaustive` 表示未来可能添加新格式。
  171. 171/// Identifies supported image codec formats. `#non_exhaustive` means new formats may be added in the future.
  172. 172#derive(Eq, @debug.Debug)
  173. 173#non_exhaustive
  174. 174pub(all) enum ImageFormat {
  175. 175 Png
  176. 176 Jpeg
  177. 177 Bmp
  178. 178 Gif
  179. 179 Tga
  180. 180 Psd
  181. 181 Hdr
  182. 182 Pnm
  183. 183 Qoi
  184. 184 Webp
  185. 185 Tiff
  186. 186 Ico
  187. 187 Cur
  188. 188 Icns
  189. 189 Apng
  190. 190 Avif
  191. 191 Heif
  192. 192 JpegXl
  193. 193 Unknown
  194. 194}
  195. 195
  196. 196///|
  197. 197pub extend ImageFormat with @debug.Debug::{to_repr}
  198. 198
  199. 199///|
  200. 200pub extend ImageFormat with Eq::{not_equal, equal}
  201. 201
  202. 202///|
  203. 203/// resize 滤波器类型 Resize filter type
  204. 204///
  205. 205/// 用于图像缩放时的插值算法选择。 Selects interpolation algorithm for image resizing.
  206. 206/// `#non_exhaustive` 表示未来可能添加新构造器
  207. 207#derive(Eq, @debug.Debug)
  208. 208#non_exhaustive
  209. 209pub(all) enum ResizeFilter {
  210. 210 Default
  211. 211 Box
  212. 212 Triangle
  213. 213 CubicBSPline
  214. 214 CatmullROM
  215. 215 Mitchell
  216. 216 PointSample
  217. 217}
  218. 218
  219. 219///|
  220. 220pub extend ResizeFilter with @debug.Debug::{to_repr}
  221. 221
  222. 222///|
  223. 223pub extend ResizeFilter with Eq::{not_equal, equal}
  224. 224
  225. 225///|
  226. 226/// resize 边缘处理模式 Resize edge handling mode
  227. 227///
  228. 228/// 定义图像缩放时如何处理超出边界的像素坐标。 Defines how pixel coordinates beyond image boundaries are handled during resizing.
  229. 229/// `#non_exhaustive` 表示未来可能添加新构造器
  230. 230#derive(Eq, @debug.Debug)
  231. 231#non_exhaustive
  232. 232pub(all) enum ResizeEdge {
  233. 233 Clamp
  234. 234 Reflect
  235. 235 Wrap
  236. 236 Zero
  237. 237}
  238. 238
  239. 239///|
  240. 240pub extend ResizeEdge with @debug.Debug::{to_repr}
  241. 241
  242. 242///|
  243. 243pub extend ResizeEdge with Eq::{not_equal, equal}
  244. 244
  245. 245///|
  246. 246/// APNG 动画解码结果 APNG animation decode result
  247. 247///
  248. 248/// 字段 Fields:
  249. 249/// - `frames` - 帧图像数组 Array of frame images
  250. 250/// - `delays` - 每帧延迟时间(毫秒)Per-frame delay in milliseconds
  251. 251#derive(Eq, @debug.Debug)
  252. 252pub(all) struct PngAnimation {
  253. 253 frames : Array[Image]
  254. 254 delays : Array[Int32] // 毫秒
  255. 255}
  256. 256
  257. 257///|
  258. 258pub extend PngAnimation with @debug.Debug::{to_repr}
  259. 259
  260. 260///|
  261. 261pub extend PngAnimation with Eq::{not_equal, equal}
  262. 262
  263. 263///|
  264. 264/// 图像维度上限(单边最大像素数) Maximum image dimension limit (max pixels per side)
  265. 265///
  266. 266/// 防止恶意图像声明超大尺寸导致 OOM 或整数溢出。 Prevents malicious images from declaring oversized dimensions causing OOM or integer overflow.
  267. 267/// 65535 覆盖常见图像格式最大尺寸(PNG/TIFF 32-bit、JPEG 65500、BMP 32-bit)。 65535 covers max sizes of common formats (PNG/TIFF 32-bit, JPEG 65500, BMP 32-bit).
  268. 268pub const MAX_IMAGE_DIMENSION : Int32 = 65535
  269. 269
  270. 270///| 总像素数上限 — 维度守卫之上的内存安全闸门(发起人确认取值 5e8)。
  271. 271
  272. 272///| 内存峰值:RGB 3 通道 ≈ 1.5 GB / ImageF HDR float 3 通道 ≈ 12 GB。
  273. 273
  274. 274///| Total pixel budget — memory-safety gate on top of per-dimension guards
  275. 275
  276. 276///|
  277. 277/// (confirmed value 5e8; peak memory: RGB ≈ 1.5 GB, ImageF HDR ≈ 12 GB).
  278. 278pub const MAX_TOTAL_PIXELS : Int32 = 500000000
  279. 279
  280. 280///|
  281. 281/// 校验图像维度是否在安全范围内。 Validate image dimensions are within safe bounds.
  282. 282///
  283. 283/// 检查 width/height 为正数且不超过 MAX_IMAGE_DIMENSION,且 width * height * channels 不超过 Int 最大值(防溢出)。 Checks width/height are positive and do not exceed MAX_IMAGE_DIMENSION, and width * height * channels does not overflow Int (prevents overflow).
  284. 284
  285. 285///|
  286. 286/// 参数 Parameters:
  287. 287/// - `width` - 图像宽度 Image width
  288. 288/// - `height` - 图像高度 Image height
  289. 289/// - `channels` - 通道数 Number of channels
  290. 290///
  291. 291/// 错误 Errors:
  292. 292/// - `LoadError::DecodeFailed` - 维度无效、超过上限或溢出 Invalid dimensions, exceeds limit, or overflow
  293. 293///
  294. 294/// 示例 Example:
  295. 295/// ```mbt check
  296. 296/// test "doc_check_dims_valid" {
  297. 297/// @types.check_dims(1, 1, 3)
  298. 298/// assert_true(true)
  299. 299/// }
  300. 300/// ```
  301. 301///
  302. 302/// ```mbt check
  303. 303/// test "doc_check_dims_total_pixels_ok" {
  304. 304/// @types.check_dims(25000, 20000, 3) // 5e8 边界内(总像素上限 5 亿)
  305. 305/// assert_true(true)
  306. 306/// }
  307. 307/// ```
  308. 308///
  309. 309/// ```mbt check
  310. 310/// test "doc_check_dims_total_pixels_exceeded" {
  311. 311/// try {
  312. 312/// @types.check_dims(25001, 20000, 3) // 500,020,000 > 5e8
  313. 313/// assert_true(false)
  314. 314/// } catch {
  315. 315/// @types.LoadError::DecodeFailed(_) => assert_true(true)
  316. 316/// _ => assert_true(false)
  317. 317/// }
  318. 318/// }
  319. 319/// ```
  320. 320pub fn check_dims(
  321. 321 width : Int32,
  322. 322 height : Int32,
  323. 323 channels : Int32,
  324. 324) -> Unit raise LoadError {
  325. 325● if width <= 0 || height <= 0 || channels <= 0 {
  326. 326● raise LoadError::DecodeFailed(
  327. 327 "图像尺寸无效: width=\{width}, height=\{height}, channels=\{channels}",
  328. 328 )
  329. 329 }
  330. 330 if width > MAX_IMAGE_DIMENSION || height > MAX_IMAGE_DIMENSION {
  331. 331● raise LoadError::DecodeFailed(
  332. 332 "图像尺寸超过上限 \{MAX_IMAGE_DIMENSION}: width=\{width}, height=\{height}",
  333. 333 )
  334. 334 }
  335. 335 // 检查 width * height * channels 是否溢出
  336. 336 // Int 在 wasm/native 为 32 位有符号,最大值 2147483647
  337. 337 // 先算 width * height,再乘 channels
  338. 338 let max_int = 2147483647
  339. 339 if width > max_int / height {
  340. 340● raise LoadError::DecodeFailed(
  341. 341 "图像像素总数溢出: \{width} * \{height}",
  342. 342 )
  343. 343 }
  344. 344 let pixel_count = width * height
  345. 345 if pixel_count > MAX_TOTAL_PIXELS {
  346. 346● raise LoadError::DecodeFailed(
  347. 347 "图像像素总数超过内存安全上限 \{MAX_TOTAL_PIXELS}: \{pixel_count} 像素(\{pixel_count / 1000000} M pixels) Total pixels exceed the memory-safety budget",
  348. 348 )
  349. 349 }
  350. 350 if pixel_count > max_int / channels {
  351. 351○ raise LoadError::DecodeFailed(
  352. 352 "图像数据大小溢出: \{pixel_count} * \{channels}",
  353. 353 )
  354. 354 }
  355. 355}
  356. 356
  357. 357///|
  358. 358/// 判断维度是否在安全范围内(纯函数,可用于证明前提)。 Check if dimensions are within safe bounds (pure function, usable as proof precondition).
  359. 359///
  360. 360/// 返回 true 当且仅当 width/height/channels 均为正、不超过 MAX_IMAGE_DIMENSION、且乘积不溢出 Int32。 Returns true iff width/height/channels are all positive, do not exceed MAX_IMAGE_DIMENSION, and product does not overflow Int32.
  361. 361
  362. 362///|
  363. 363/// 参数 Parameters:
  364. 364/// - `w` - 宽度 Width
  365. 365/// - `h` - 高度 Height
  366. 366/// - `c` - 通道数 Channels
  367. 367///
  368. 368/// 返回 Return:
  369. 369/// - 维度是否合法 Whether dimensions are valid
  370. 370pub let dims_valid_check : (Int32, Int32, Int32) -> Bool = @safe_math.dims_valid_check
  371. 371
  372. 372///|
  373. 373/// 安全乘法:返回 a * b,溢出时返回 None。 Safe multiplication: returns a * b, or None on overflow.
  374. 374///
  375. 375/// 用于在分配大数组前检查是否会溢出。 Used to check for overflow before allocating large arrays.
  376. 376/// 实际实现和形式化证明在 core/safe_math 包中。 Actual implementation and formal proof are in the core/safe_math package.
  377. 377///
  378. 378/// 参数 Parameters:
  379. 379/// - `a` - 乘数 Multiplicand
  380. 380/// - `b` - 乘数 Multiplier
  381. 381///
  382. 382/// 返回 Return:
  383. 383/// - `Some(result)` - 乘积 The product
  384. 384/// - `None` - 溢出 Overflow occurred
  385. 385pub let safe_mul : (Int32, Int32) -> Int32? = @safe_math.safe_mul
  386. 386
  387. 387///|
  388. 388/// 安全三参数乘法:返回 a * b * c,溢出时返回 None。 Safe three-argument multiplication: returns a * b * c, or None on overflow.
  389. 389///
  390. 390/// 常用于计算 w * h * channels 的数组大小。 Commonly used to compute array size as w * h * channels.
  391. 391/// 实际实现和形式化证明在 core/safe_math 包中。 Actual implementation and formal proof are in the core/safe_math package.
  392. 392///
  393. 393/// 参数 Parameters:
  394. 394/// - `a` - 第一乘数 First multiplicand
  395. 395/// - `b` - 第二乘数 Second multiplicand
  396. 396/// - `c` - 第三乘数 Third multiplicand
  397. 397///
  398. 398/// 返回 Return:
  399. 399/// - `Some(result)` - 乘积 The product
  400. 400/// - `None` - 溢出 Overflow occurred
  401. 401pub let safe_mul3 : (Int32, Int32, Int32) -> Int32? = @safe_math.safe_mul3
  402. 402
  403. 403///|
  404. 404/// 通用整数钳制:将 v 限制在 [lo, hi] 范围内。 General integer clamp: constrains v to [lo, hi] range.
  405. 405///
  406. 406/// 实际实现在 core/safe_math 包中。 Actual implementation is in the core/safe_math package.
  407. 407///
  408. 408/// 参数 Parameters:
  409. 409/// - `v` - 输入值 Input value
  410. 410/// - `lo` - 下界 Lower bound
  411. 411/// - `hi` - 上界 Upper bound
  412. 412///
  413. 413/// 返回 Return:
  414. 414/// - 钳制后的值 Clamped value
  415. 415pub let clamp_int : (Int32, Int32, Int32) -> Int32 = @safe_math.clamp_int
  416. 416
  417. 417///|
  418. 418/// 像素值钳制:将 value 限制在 [0, 255] 范围内。 Pixel value clamp: constrains value to [0, 255] range.
  419. 419///
  420. 420/// 实际实现和形式化证明在 core/safe_math 包中。 Actual implementation and formal proof are in the core/safe_math package.
  421. 421///
  422. 422/// 参数 Parameters:
  423. 423/// - `value` - 输入像素值 Input pixel value
  424. 424///
  425. 425/// 返回 Return:
  426. 426/// - 钳制后的像素值(0-255)Clamped pixel value (0-255)
  427. 427pub let clamp_pixel_value : (Int32) -> Int32 = @safe_math.clamp_pixel_value
  428. 428
  429. 429///|
  430. 430/// 字节钳制:将 Int32 值限制在 [0, 255] 并转为 Byte。 Byte clamp: constrains Int32 value to [0, 255] and converts to Byte.
  431. 431///
  432. 432/// 等价于 clamp_pixel_value(v).to_byte(),用于像素赋值。 Equivalent to clamp_pixel_value(v).to_byte(), used for pixel assignment.
  433. 433/// 实际实现在 core/safe_math 包中。 Actual implementation is in the core/safe_math package.
  434. 434///
  435. 435/// 参数 Parameters:
  436. 436/// - `v` - 输入值 Input value
  437. 437///
  438. 438/// 返回 Return:
  439. 439/// - 钳制后的字节 Clamped byte
  440. 440pub let clamp_byte : (Int32) -> Byte = @safe_math.clamp_byte
  441. 441
  442. 442///|
  443. 443/// 坐标钳制:将坐标 v 限制在 [0, max_val-1] 范围内。 Coordinate clamp: constrains coordinate v to [0, max_val-1] range.
  444. 444///
  445. 445/// 用于图像像素坐标的边界检查。 Used for boundary checking of image pixel coordinates.
  446. 446/// 实际实现在 core/safe_math 包中。 Actual implementation is in the core/safe_math package.
  447. 447///
  448. 448/// 参数 Parameters:
  449. 449/// - `v` - 坐标值 Coordinate value
  450. 450/// - `max_val` - 最大值(上限为 max_val-1)Maximum value (upper bound is max_val-1)
  451. 451///
  452. 452/// 返回 Return:
  453. 453/// - 钳制后的坐标 Clamped coordinate
  454. 454pub let clamp_coord : (Int32, Int32) -> Int32 = @safe_math.clamp_coord
  455. 455
  456. 456///|
  457. 457/// 浮点钳制:将 Float 值 v 限制在 [lo, hi] 范围内。 Float clamp: constrains Float value v to [lo, hi] range.
  458. 458///
  459. 459/// 用于 16-bit/float 图像处理的范围约束。 Used for range constraints in 16-bit/float image processing.
  460. 460/// 实际实现在 core/safe_math 包中。 Actual implementation is in the core/safe_math package.
  461. 461///
  462. 462/// 参数 Parameters:
  463. 463/// - `v` - 输入值 Input value
  464. 464/// - `lo` - 下界 Lower bound
  465. 465/// - `hi` - 上界 Upper bound
  466. 466///
  467. 467/// 返回 Return:
  468. 468/// - 钳制后的值 Clamped value
  469. 469pub let clamp_float : (Float, Float, Float) -> Float = @safe_math.clamp_float
  470. 470
  471. 471///|
  472. 472/// 双精度浮点钳制:将 Double 值 v 限制在 [lo, hi] 范围内。 Double clamp: constrains Double value v to [lo, hi] range.
  473. 473///
  474. 474/// 用于高精度图像处理(HDR/频域)的范围约束。 Used for range constraints in high-precision image processing (HDR/frequency domain).
  475. 475/// 实际实现在 core/safe_math 包中。 Actual implementation is in the core/safe_math package.
  476. 476///
  477. 477/// 参数 Parameters:
  478. 478/// - `v` - 输入值 Input value
  479. 479/// - `lo` - 下界 Lower bound
  480. 480/// - `hi` - 上界 Upper bound
  481. 481///
  482. 482/// 返回 Return:
  483. 483/// - 钳制后的值 Clamped value
  484. 484pub let clamp_double : (Double, Double, Double) -> Double = @safe_math.clamp_double
  485. 485
  486. 486///|
  487. 487/// 整数最大值:返回 a 和 b 中较大的那个。 Integer maximum: returns the larger of a and b.
  488. 488///
  489. 489/// 实际实现在 core/safe_math 包中。 Actual implementation is in the core/safe_math package.
  490. 490///
  491. 491/// 参数 Parameters:
  492. 492/// - `a` - 第一个值 First value
  493. 493/// - `b` - 第二个值 Second value
  494. 494///
  495. 495/// 返回 Return:
  496. 496/// - 较大值 The larger value
  497. 497pub let int_max : (Int32, Int32) -> Int32 = @safe_math.int_max
  498. 498
  499. 499///|
  500. 500/// 整数最小值:返回 a 和 b 中较小的那个。 Integer minimum: returns the smaller of a and b.
  501. 501///
  502. 502/// 实际实现在 core/safe_math 包中。 Actual implementation is in the core/safe_math package.
  503. 503///
  504. 504/// 参数 Parameters:
  505. 505/// - `a` - 第一个值 First value
  506. 506/// - `b` - 第二个值 Second value
  507. 507///
  508. 508/// 返回 Return:
  509. 509/// - 较小值 The smaller value
  510. 510pub let int_min : (Int32, Int32) -> Int32 = @safe_math.int_min
  511. 511
  512. 512// ============================================================================
  513. 513// Section: 文档测试数据常量 Doc test data constants
  514. 514// 供跨包文档测试复用的最小合法图像字节串。
  515. 515// Minimal valid image byte strings for cross-package doc test reuse.
  516. 516// ============================================================================
  517. 517
  518. 518///|
  519. 519/// 文档测试用 1×1 24-bit BMP 字节串(58 字节,红色像素 R=255 G=0 B=0) 1×1 24-bit BMP byte string for doc tests (58 bytes, red pixel R=255 G=0 B=0)
  520. 520pub const DOC_TEST_BMP_1X1 : Bytes = b"\x42\x4d\x3a\x00\x00\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00"
  521. 521
  522. 522///|
  523. 523/// 文档测试用 1×1 RGBA PNG 字节串(73 字节,红色像素 R=255 G=0 B=0 A=255) 1×1 RGBA PNG byte string for doc tests (73 bytes, red pixel R=255 G=0 B=0 A=255)
  524. 524pub const DOC_TEST_PNG_1X1_RGBA : Bytes = b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\xeb\x89\x2f\x0b\x00\x00\x00\x10\x49\x44\x41\x54\x78\x01\x01\x05\x00\xfa\xff\x00\xff\x00\x00\xff\x05\x00\x01\xff\x0b\x15\xdf\x08\x00\x00\x00\x00\x49\x45\x4e\x44\xf0\x2b\x1d\x59"
  525. 525
  526. 526///|
  527. 527/// 文档测试用 1×1 RGB PNM (P6) 字节串(14 字节,红色像素 R=255 G=0 B=0) 1×1 RGB PNM (P6) byte string for doc tests (14 bytes, red pixel R=255 G=0 B=0)
  528. 528pub const DOC_TEST_PNM_1X1 : Bytes = b"P6\n1 1\n255\n\xff\x00\x00"
  529. 529
  530. 530///|
  531. 531/// 文档测试用 1×1 RGBA QOI 字节串(26 字节,红色像素 R=255 G=0 B=0 A=255) 1×1 RGBA QOI byte string for doc tests (26 bytes, red pixel R=255 G=0 B=0 A=255)
  532. 532pub const DOC_TEST_QOI_1X1 : Bytes = b"\x71\x6f\x69\x66\x00\x00\x00\x01\x00\x00\x00\x01\x04\x00\x00\xfe\x00\xff\x00\x00\x00\x00\x00\x00\x00\x01"