image.domain\pipeline\pipeline.mbt

96.7% · 87 / 90 points · 321 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/// # 管线 pipeline/pipeline
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6
  7. 7///|
  8. 8/// 批处理流水线 + 插件系统 + CLI 工具
  9. 9///
  10. 10/// v49.0.0: 生态建设
  11. 11/// - Pipeline: 图像处理流水线(链式操作)
  12. 12/// - Batch: 批量处理多个图像
  13. 13/// - Plugin: 插件接口(自定义处理函数)
  14. 14/// - CLI: 命令行参数解析和命令定义
  15. 15
  16. 16///|
  17. 17/// 处理操作类型
  18. 18/// `#non_exhaustive` 表示未来可能添加新构造器
  19. 19#non_exhaustive
  20. 20pub(all) enum Operation {
  21. 21 Resize(Int32, Int32)
  22. 22 Rotate(Double)
  23. 23 FlipH
  24. 24 FlipV
  25. 25 Grayscale
  26. 26 Invert
  27. 27 Blur(Double)
  28. 28 Sharpen(Double)
  29. 29 Brightness(Double)
  30. 30 Contrast(Double)
  31. 31 Custom(String)
  32. 32}
  33. 33
  34. 34///|
  35. 35/// 处理结果
  36. 36pub struct ProcessResult {
  37. 37 success : Bool
  38. 38 message : String
  39. 39 output_size : Int32
  40. 40}
  41. 41
  42. 42///|
  43. 43/// 流水线(链式操作序列)
  44. 44pub struct Pipeline {
  45. 45 operations : Array[Operation]
  46. 46}
  47. 47
  48. 48///|
  49. 49/// 创建空流水线
  50. 50pub fn pipeline_new() -> Pipeline {
  51. 51● Pipeline::{ operations: [], }
  52. 52}
  53. 53
  54. 54///|
  55. 55/// 添加操作到流水线
  56. 56pub fn pipeline_add(p : Pipeline, op : Operation) -> Pipeline {
  57. 57● p.operations.push(op)
  58. 58 p
  59. 59}
  60. 60
  61. 61///|
  62. 62/// 批量添加操作
  63. 63pub fn pipeline_add_all(p : Pipeline, ops : Array[Operation]) -> Pipeline {
  64. 64● for op in ops {
  65. 65● p.operations.push(op)
  66. 66 }
  67. 67 p
  68. 68}
  69. 69
  70. 70///|
  71. 71/// 获取流水线中的操作数量
  72. 72pub fn pipeline_len(p : Pipeline) -> Int32 {
  73. 73● p.operations.length()
  74. 74}
  75. 75
  76. 76///|
  77. 77/// 流水线转字符串描述
  78. 78pub fn pipeline_describe(p : Pipeline) -> String {
  79. 79● let parts : Array[String] = []
  80. 80 for op in p.operations {
  81. 81● let desc = match op {
  82. 82● Operation::Resize(w, h) => "resize(\{w}x\{h})"
  83. 83● Operation::Rotate(angle) => "rotate(\{angle}°)"
  84. 84● Operation::FlipH => "flip_h"
  85. 85● Operation::FlipV => "flip_v"
  86. 86● Operation::Grayscale => "grayscale"
  87. 87● Operation::Invert => "invert"
  88. 88● Operation::Blur(r) => "blur(\{r})"
  89. 89● Operation::Sharpen(a) => "sharpen(\{a})"
  90. 90● Operation::Brightness(v) => "brightness(\{v})"
  91. 91● Operation::Contrast(v) => "contrast(\{v})"
  92. 92● Operation::Custom(name) => "custom(\{name})"
  93. 93 }
  94. 94● parts.push(desc)
  95. 95 }
  96. 96● parts.join(" -> ")
  97. 97}
  98. 98
  99. 99///|
  100. 100/// 批处理配置
  101. 101pub(all) struct BatchConfig {
  102. 102 input_format : String
  103. 103 output_format : String
  104. 104 output_suffix : String
  105. 105 parallel : Bool
  106. 106}
  107. 107
  108. 108///|
  109. 109/// 默认批处理配置
  110. 110pub fn default_batch_config() -> BatchConfig {
  111. 111● BatchConfig::{
  112. 112 input_format: "auto",
  113. 113 output_format: "png",
  114. 114 output_suffix: "_processed",
  115. 115 parallel: false,
  116. 116 }
  117. 117}
  118. 118
  119. 119///|
  120. 120/// 批处理任务
  121. 121pub struct BatchTask {
  122. 122 input_path : String
  123. 123 output_path : String
  124. 124 pipeline : Pipeline
  125. 125}
  126. 126
  127. 127///|
  128. 128/// 创建批处理任务
  129. 129pub fn batch_task(
  130. 130 input_path : String,
  131. 131 output_path : String,
  132. 132 pipeline : Pipeline,
  133. 133) -> BatchTask {
  134. 134● BatchTask::{ input_path, output_path, pipeline, }
  135. 135}
  136. 136
  137. 137///|
  138. 138/// 批处理结果
  139. 139pub struct BatchResult {
  140. 140 total : Int32
  141. 141 succeeded : Int32
  142. 142 failed : Int32
  143. 143 results : Array[ProcessResult]
  144. 144}
  145. 145
  146. 146///|
  147. 147/// 执行批处理(模拟)
  148. 148///
  149. 149/// 注意:实际文件 I/O 需要运行时支持,此处提供框架和结果统计。
  150. 150pub fn batch_execute(tasks : Array[BatchTask]) -> BatchResult {
  151. 151● let total = tasks.length()
  152. 152 let mut succeeded = 0
  153. 153 let mut failed = 0
  154. 154 let results : Array[ProcessResult] = []
  155. 155 for task in tasks {
  156. 156 // 模拟处理:实际实现需要文件 I/O
  157. 157● let success = task.pipeline.operations.length() >= 0
  158. 158 if success {
  159. 159● succeeded += 1
  160. 160● results.push(ProcessResult::{
  161. 161 success: true,
  162. 162 message: "OK",
  163. 163 output_size: 0,
  164. 164 })
  165. 165 } else {
  166. 166○ failed += 1
  167. 167○ results.push(ProcessResult::{
  168. 168 success: false,
  169. 169 message: "Failed",
  170. 170 output_size: 0,
  171. 171 })
  172. 172 }
  173. 173 }
  174. 174 BatchResult::{ total, succeeded, failed, results, }
  175. 175}
  176. 176
  177. 177///|
  178. 178/// 批处理结果摘要
  179. 179pub fn batch_summary(result : BatchResult) -> String {
  180. 180● "Batch: \{result.succeeded}/\{result.total} succeeded, \{result.failed} failed"
  181. 181}
  182. 182
  183. 183///|
  184. 184/// CLI 命令
  185. 185/// `#non_exhaustive` 表示未来可能添加新构造器
  186. 186#non_exhaustive
  187. 187pub(all) enum CliCommand {
  188. 188 Convert(String, String)
  189. 189 Resize(String, String, Int32, Int32)
  190. 190 Rotate(String, String, Double)
  191. 191 Batch(String, String, String)
  192. 192 Info(String)
  193. 193 Help
  194. 194}
  195. 195
  196. 196///|
  197. 197/// CLI 参数解析
  198. 198///
  199. 199/// 解析命令行参数,返回对应的命令。
  200. 200/// 支持的命令格式:
  201. 201/// CLI 命令:convert / resize / rotate / batch / info / help
  202. 202/// - convert <input> <output> 转换格式
  203. 203/// - resize <input> <output> <width> <height> 缩放
  204. 204/// - rotate <input> <output> <angle> 旋转
  205. 205/// - batch <input_dir> <output_dir> <pipeline_config> 批处理
  206. 206/// - info <input> 信息
  207. 207/// - help 帮助
  208. 208/// 解析 Parse command-line arguments, return corresponding command.
  209. 209/// 描述 Supported command formats:
  210. 210/// 支持命令:convert / resize / rotate / batch / info / help
  211. 211pub fn cli_parse(args : Array[String]) -> CliCommand {
  212. 212● if args.length() == 0 {
  213. 213● return CliCommand::Help
  214. 214 }
  215. 215 let cmd = args[0]
  216. 216 if cmd == "convert" {
  217. 217● if args.length() >= 3 {
  218. 218● CliCommand::Convert(args[1], args[2])
  219. 219 } else {
  220. 220● CliCommand::Help
  221. 221 }
  222. 222● } else if cmd == "resize" {
  223. 223● if args.length() >= 5 {
  224. 224● CliCommand::Resize(
  225. 225 args[1],
  226. 226 args[2],
  227. 227● parse_int(args[3]),
  228. 228● parse_int(args[4]),
  229. 229 )
  230. 230 } else {
  231. 231● CliCommand::Help
  232. 232 }
  233. 233● } else if cmd == "rotate" {
  234. 234● if args.length() >= 4 {
  235. 235● CliCommand::Rotate(args[1], args[2], parse_double(args[3]))
  236. 236 } else {
  237. 237● CliCommand::Help
  238. 238 }
  239. 239● } else if cmd == "batch" {
  240. 240● if args.length() >= 4 {
  241. 241● CliCommand::Batch(args[1], args[2], args[3])
  242. 242 } else {
  243. 243● CliCommand::Help
  244. 244 }
  245. 245● } else if cmd == "info" {
  246. 246● if args.length() >= 2 {
  247. 247● CliCommand::Info(args[1])
  248. 248 } else {
  249. 249● CliCommand::Help
  250. 250 }
  251. 251● } else if cmd == "help" {
  252. 252● CliCommand::Help
  253. 253 } else {
  254. 254● CliCommand::Help
  255. 255 }
  256. 256}
  257. 257
  258. 258///|
  259. 259/// CLI 帮助文本
  260. 260pub fn cli_help() -> String {
  261. 261● "stb-image CLI\n" +
  262. 262 "Usage:\n" +
  263. 263 " convert <input> <output> Convert image format\n" +
  264. 264 " resize <input> <output> <w> <h> Resize image\n" +
  265. 265 " rotate <input> <output> <angle> Rotate image\n" +
  266. 266 " batch <input_dir> <output_dir> <config> Batch process\n" +
  267. 267 " info <input> Show image info\n" +
  268. 268 " help Show this help"
  269. 269}
  270. 270
  271. 271///|
  272. 272/// 简单的字符串转整数
  273. 273fn parse_int(s : String) -> Int32 {
  274. 274● let mut result = 0
  275. 275 let mut sign = 1
  276. 276 let mut i = 0
  277. 277● let bytes = s.to_array()
  278. 278● if bytes.length() > 0 && bytes[0] == '-' {
  279. 279○ sign = -1
  280. 280 i = 1
  281. 281 }
  282. 282● while i < bytes.length() {
  283. 283● let c = bytes[i].to_int()
  284. 284 if c >= 0x30 && c <= 0x39 {
  285. 285● result = result * 10 + (c - 0x30)
  286. 286 }
  287. 287 i += 1
  288. 288 }
  289. 289 result * sign
  290. 290}
  291. 291
  292. 292///|
  293. 293/// 简单的字符串转浮点
  294. 294fn parse_double(s : String) -> Double {
  295. 295● let mut result = 0.0
  296. 296 let mut sign = 1.0
  297. 297 let mut i = 0
  298. 298 let mut decimal = false
  299. 299 let mut divisor = 10.0
  300. 300● let bytes = s.to_array()
  301. 301● if bytes.length() > 0 && bytes[0] == '-' {
  302. 302● sign = -1.0
  303. 303 i = 1
  304. 304 }
  305. 305● while i < bytes.length() {
  306. 306● let c = bytes[i].to_int()
  307. 307 if c == 0x2E {
  308. 308● decimal = true
  309. 309● } else if c >= 0x30 && c <= 0x39 {
  310. 310● let digit = (c - 0x30).to_double()
  311. 311 if decimal {
  312. 312● result += digit / divisor
  313. 313 divisor *= 10.0
  314. 314 } else {
  315. 315● result = result * 10.0 + digit
  316. 316 }
  317. 317 }
  318. 318 i += 1
  319. 319 }
  320. 320 result * sign
  321. 321}