all\all.mbt

0% · 0 / 20 points · 171 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/// # 门面包:`walkzzz/image.mbt/all`
  3. 3///
  4. 4/// 目的只有一个:**下游只 `moon add walkzzz/image.mbt` 一条,就能 `import "walkzzz/image.mbt/all"` 拿到常用的全部东西**。
  5. 5///
  6. 6/// MoonBit 的规则是"依赖会传递、import 不传递"——`moon add walkzzz/image.mbt` 会把六个子模块全部下载好,
  7. 7/// 但下游包直接 `import "walkzzz/image.codec/lib"` 会报
  8. 8/// `exists in global environment, but its containing module is not imported by <你的包>`。
  9. 9/// 本包用 `pub using` 把这些名字从子模块**再导出**一次:下游 import 的是一个**直接依赖(根模块)**里的包,
  10. 10/// 于是不需要额外 add 任何子模块。
  11. 11///
  12. 12/// 想要更多函数(filter / vision / photography / domain 的上百个 API),照旧显式 add 对应子模块——本包只覆盖"高频起步面"。
  13. 13pub using @types {
  14. 14 type Image,
  15. 15 type Image16,
  16. 16 type ImageF,
  17. 17 type AnyImage,
  18. 18 type LoadError,
  19. 19 type ImageError,
  20. 20 type ErrorCode,
  21. 21 type ImageFormat,
  22. 22 type DecodeLimits,
  23. 23 type Region,
  24. 24 type ImageRegion,
  25. 25 type AllocBudget,
  26. 26}
  27. 27
  28. 28///|
  29. 29/// 区域视图(ROI):`pub using` 只能再导出类型,方法要在这里包一层薄函数,
  30. 30/// 否则下游拿到类型却调不到 `Image::region` / `ImageRegion::to_image`。
  31. 31
  32. 32///|
  33. 33/// 取一块区域的零复制视图(越界自动裁剪)。失败不会发生,空区域返回空视图。
  34. 34pub fn region(
  35. 35 img : @types.Image,
  36. 36 x : Int32,
  37. 37 y : Int32,
  38. 38 w : Int32,
  39. 39 h : Int32,
  40. 40) -> @types.ImageRegion {
  41. 41○ img.region(x, y, w, h)
  42. 42}
  43. 43
  44. 44///|
  45. 45/// 用现成 `Region` 取零复制视图。
  46. 46pub fn region_with(img : @types.Image, r : @types.Region) -> @types.ImageRegion {
  47. 47○ img.region_with(r)
  48. 48}
  49. 49
  50. 50///|
  51. 51/// 区域视图宽度(裁剪后的有效宽度)。
  52. 52pub fn region_width(v : @types.ImageRegion) -> Int32 {
  53. 53○ v.width()
  54. 54}
  55. 55
  56. 56///|
  57. 57/// 区域视图高度(裁剪后的有效高度)。
  58. 58pub fn region_height(v : @types.ImageRegion) -> Int32 {
  59. 59○ v.height()
  60. 60}
  61. 61
  62. 62///|
  63. 63/// 区域视图通道数。
  64. 64pub fn region_channels(v : @types.ImageRegion) -> Int32 {
  65. 65○ v.channels()
  66. 66}
  67. 67
  68. 68///|
  69. 69/// 读区域内单通道字节(坐标相对区域左上角,越界返回 0)。
  70. 70pub fn region_get(
  71. 71 v : @types.ImageRegion,
  72. 72 x : Int32,
  73. 73 y : Int32,
  74. 74 c : Int32,
  75. 75) -> Byte {
  76. 76○ v.get(x, y, c)
  77. 77}
  78. 78
  79. 79///|
  80. 80/// 读区域内一个像素的全部通道。
  81. 81pub fn region_pixel(
  82. 82 v : @types.ImageRegion,
  83. 83 x : Int32,
  84. 84 y : Int32,
  85. 85) -> Array[Byte] {
  86. 86○ v.pixel(x, y)
  87. 87}
  88. 88
  89. 89///|
  90. 90/// 默认分配预算(512 MiB)。
  91. 91pub fn alloc_budget_default() -> @types.AllocBudget {
  92. 92○ @types.AllocBudget::default_budget()
  93. 93}
  94. 94
  95. 95///|
  96. 96/// 大图直接出缩略图(面积平均重采样,唯一分配 = 输出图)。
  97. 97pub fn thumbnail(
  98. 98 img : @types.Image,
  99. 99 max_side : Int32,
  100. 100) -> @types.Image raise @types.LoadError {
  101. 101○ @transform.thumbnail(img, max_side)
  102. 102}
  103. 103
  104. 104///|
  105. 105/// 带预算的缩略图:输出超预算直接拒(不分配)。
  106. 106pub fn thumbnail_budgeted(
  107. 107 img : @types.Image,
  108. 108 max_side : Int32,
  109. 109 budget : @types.AllocBudget,
  110. 110) -> @types.Image raise @types.ImageError {
  111. 111○ @transform.thumbnail_budgeted(img, max_side, budget)
  112. 112}
  113. 113
  114. 114///|
  115. 115/// 缩略图输出尺寸(先算后分配)。
  116. 116pub fn thumbnail_dims(
  117. 117 width : Int32,
  118. 118 height : Int32,
  119. 119 max_side : Int32,
  120. 120) -> (Int32, Int32) {
  121. 121○ @transform.thumbnail_dims(width, height, max_side)
  122. 122}
  123. 123
  124. 124///|
  125. 125/// 把区域视图物化为独立 `Image`(唯一一次分配)。
  126. 126pub fn region_to_image(v : @types.ImageRegion) -> @types.Image {
  127. 127○ v.to_image()
  128. 128}
  129. 129
  130. 130///|
  131. 131/// 编解码(格式自动识别、按格式输出、WebP 无损)
  132. 132pub using @lib {
  133. 133 type EncodedImage,
  134. 134 has_alpha,
  135. 135 best_format,
  136. 136 encode_best,
  137. 137 encode_best_limited,
  138. 138 load_from_bytes_auto,
  139. 139 load_16_from_bytes_auto,
  140. 140 load_any_f,
  141. 141 save_image_auto,
  142. 142 save_image_with_quality,
  143. 143 detect_format,
  144. 144 decode_with_limits,
  145. 145 check_decode_budget,
  146. 146 get_image_summary,
  147. 147 encode_png,
  148. 148 encode_jpeg,
  149. 149 encode_bmp,
  150. 150 encode_qoi,
  151. 151 encode_tga,
  152. 152 encode_gif,
  153. 153 encode_webp_lossless,
  154. 154 decode_webp,
  155. 155}
  156. 156
  157. 157///|
  158. 158/// 变换(缩放 / 裁切 / 旋转 / 翻转 / 区域裁剪)
  159. 159pub using @transform {
  160. 160 resize_to_cover,
  161. 161 resize_to_contain,
  162. 162 crop,
  163. 163 crop_region,
  164. 164 resize_subsample,
  165. 165 rotate,
  166. 166 rotate_90,
  167. 167 rotate_180,
  168. 168 rotate_270,
  169. 169 flip_horizontal,
  170. 170 flip_vertical,
  171. 171}