1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// 投影分析与文本行分割
// 水平/垂直投影、文本行检测、字符分割

///|
/// 投影结果结构体
pub(all) struct ProjectionResult {
  projection : Array[Int]
  peaks : Array[Int]
  valleys : Array[Int]
  mean : Double
  stddev : Double
}

///|
/// 水平投影(按行统计前景像素数)
pub fn horizontal_projection(
  img : @types.Image,
) -> ProjectionResult raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "horizontal_projection: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "horizontal_projection: 仅支持单通道二值图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "horizontal_projection: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }

  let w = img.width
  let h = img.height
  let data = img.data
  let projection : Array[Int] = Array::make(h, 0)
  for y in 0..<h {
    for x in 0..<w {
      if data[y * w + x].to_int() > 128 {
        projection[y] += 1
      }
    }
  }
  analyze_projection(projection)
}

///|
/// 垂直投影(按列统计前景像素数)
pub fn vertical_projection(
  img : @types.Image,
) -> ProjectionResult raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "vertical_projection: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "vertical_projection: 仅支持单通道二值图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "vertical_projection: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }

  let w = img.width
  let h = img.height
  let data = img.data
  let projection : Array[Int] = Array::make(w, 0)
  for y in 0..<h {
    for x in 0..<w {
      if data[y * w + x].to_int() > 128 {
        projection[x] += 1
      }
    }
  }
  analyze_projection(projection)
}

///|
/// 分析投影(找峰值、谷值、均值、标准差)
fn analyze_projection(projection : Array[Int]) -> ProjectionResult {
  let n = projection.length()
  // 计算均值和标准差
  let mut sum = 0
  let mut sum_sq = 0
  for i in 0..<n {
    sum += projection[i]
    sum_sq += projection[i] * projection[i]
  }
  let mean = if n > 0 { sum.to_double() / n.to_double() } else { 0.0 }
  let variance = if n > 0 {
    sum_sq.to_double() / n.to_double() - mean * mean
  } else {
    0.0
  }
  let stddev = if variance > 0.0 { variance.sqrt() } else { 0.0 }
  // 找峰值(局部最大值)
  let peaks : Array[Int] = []
  for i in 1..<(n - 1) {
    if projection[i] > projection[i - 1] &&
      projection[i] >= projection[i + 1] &&
      projection[i].to_double() > mean {
      peaks.push(i)
    }
  }
  // 找谷值(局部最小值)
  let valleys : Array[Int] = []
  for i in 1..<(n - 1) {
    if projection[i] < projection[i - 1] &&
      projection[i] <= projection[i + 1] &&
      projection[i].to_double() < mean {
      valleys.push(i)
    }
  }
  ProjectionResult::{ projection, peaks, valleys, mean, stddev }
}

///|
/// 文本行结构体
pub(all) struct TextLineInfo {
  y_start : Int
  y_end : Int
  height : Int
  x_start : Int
  x_end : Int
  width : Int
  pixel_count : Int
  baseline : Int
}

///|
/// 检测文本行(基于水平投影)
pub fn detect_text_lines_projection(
  img : @types.Image,
  min_height? : Int = 5,
  max_gap? : Int = 3,
) -> Array[TextLineInfo] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_text_lines_projection: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "detect_text_lines_projection: 仅支持单通道二值图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "detect_text_lines_projection: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if min_height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_text_lines_projection: min_height必须大于0",
    )
  }
  if max_gap < 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_text_lines_projection: max_gap不能为负数",
    )
  }

  let w = img.width
  let h = img.height
  let data = img.data
  // 水平投影
  let projection : Array[Int] = Array::make(h, 0)
  for y in 0..<h {
    for x in 0..<w {
      if data[y * w + x].to_int() > 128 {
        projection[y] += 1
      }
    }
  }
  // 找文本行(连续的非零行)
  let lines : Array[TextLineInfo] = []
  let mut in_line = false
  let mut start_y = 0
  let mut gap_count = 0
  for y in 0..<h {
    if projection[y] > 0 {
      if !in_line {
        in_line = true
        start_y = y
      }
      gap_count = 0
    } else if in_line {
      gap_count += 1
      if gap_count > max_gap {
        // 结束当前行
        let end_y = y - gap_count
        let line_height = end_y - start_y + 1
        if line_height >= min_height {
          // 计算这一行的水平范围和像素数
          let mut min_x = w
          let mut max_x = 0
          let mut pixel_count = 0
          let mut baseline_y = start_y
          let mut max_proj = 0
          for yy in start_y..<(end_y + 1) {
            if projection[yy] > max_proj {
              max_proj = projection[yy]
              baseline_y = yy
            }
            for x in 0..<w {
              if data[yy * w + x].to_int() > 128 {
                pixel_count += 1
                if x < min_x {
                  min_x = x
                }
                if x > max_x {
                  max_x = x
                }
              }
            }
          }
          if max_x > min_x {
            lines.push(TextLineInfo::{
              y_start: start_y,
              y_end: end_y,
              height: line_height,
              x_start: min_x,
              x_end: max_x,
              width: max_x - min_x + 1,
              pixel_count,
              baseline: baseline_y,
            })
          }
        }
        in_line = false
        gap_count = 0
      }
    }
  }
  // 处理最后一行
  if in_line {
    let end_y = h - 1
    let line_height = end_y - start_y + 1
    if line_height >= min_height {
      let mut min_x = w
      let mut max_x = 0
      let mut pixel_count = 0
      for yy in start_y..<(end_y + 1) {
        for x in 0..<w {
          if data[yy * w + x].to_int() > 128 {
            pixel_count += 1
            if x < min_x {
              min_x = x
            }
            if x > max_x {
              max_x = x
            }
          }
        }
      }
      if max_x > min_x {
        lines.push(TextLineInfo::{
          y_start: start_y,
          y_end: end_y,
          height: line_height,
          x_start: min_x,
          x_end: max_x,
          width: max_x - min_x + 1,
          pixel_count,
          baseline: start_y + line_height / 2,
        })
      }
    }
  }
  lines
}

///|
/// 字符分割结构体
pub(all) struct CharSegment {
  x_start : Int
  x_end : Int
  width : Int
  y_start : Int
  y_end : Int
  height : Int
  pixel_count : Int
}

///|
/// 文本行字符分割(基于垂直投影)
pub fn segment_chars_projection(
  img : @types.Image,
  line : TextLineInfo,
  min_width? : Int = 2,
  max_gap? : Int = 2,
) -> Array[CharSegment] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "segment_chars_projection: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "segment_chars_projection: 仅支持单通道二值图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "segment_chars_projection: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if line.width <= 0 || line.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "segment_chars_projection: 文本行尺寸必须大于0",
    )
  }
  if min_width <= 0 {
    raise @types.LoadError::DecodeFailed(
      "segment_chars_projection: min_width必须大于0",
    )
  }
  if max_gap < 0 {
    raise @types.LoadError::DecodeFailed(
      "segment_chars_projection: max_gap不能为负数",
    )
  }

  let w = img.width
  let data = img.data
  let y_start = line.y_start
  let y_end = line.y_end
  let x_start = line.x_start
  let x_end = line.x_end
  // 垂直投影(只在文本行范围内)
  let projection : Array[Int] = Array::make(w, 0)
  for x in x_start..<(x_end + 1) {
    for y in y_start..<(y_end + 1) {
      if data[y * w + x].to_int() > 128 {
        projection[x] += 1
      }
    }
  }
  // 找字符(连续的非零列)
  let chars : Array[CharSegment] = []
  let mut in_char = false
  let mut start_x = 0
  let mut gap_count = 0
  for x in x_start..<(x_end + 1) {
    if projection[x] > 0 {
      if !in_char {
        in_char = true
        start_x = x
      }
      gap_count = 0
    } else if in_char {
      gap_count += 1
      if gap_count > max_gap {
        let end_x = x - gap_count
        let char_width = end_x - start_x + 1
        if char_width >= min_width {
          // 计算字符的垂直范围和像素数
          let mut min_y = y_end
          let mut max_y = y_start
          let mut pixel_count = 0
          for xx in start_x..<(end_x + 1) {
            for yy in y_start..<(y_end + 1) {
              if data[yy * w + xx].to_int() > 128 {
                pixel_count += 1
                if yy < min_y {
                  min_y = yy
                }
                if yy > max_y {
                  max_y = yy
                }
              }
            }
          }
          chars.push(CharSegment::{
            x_start: start_x,
            x_end: end_x,
            width: char_width,
            y_start: min_y,
            y_end: max_y,
            height: max_y - min_y + 1,
            pixel_count,
          })
        }
        in_char = false
        gap_count = 0
      }
    }
  }
  // 处理最后一个字符
  if in_char {
    let end_x = x_end
    let char_width = end_x - start_x + 1
    if char_width >= min_width {
      let mut min_y = y_end
      let mut max_y = y_start
      let mut pixel_count = 0
      for xx in start_x..<(end_x + 1) {
        for yy in y_start..<(y_end + 1) {
          if data[yy * w + xx].to_int() > 128 {
            pixel_count += 1
            if yy < min_y {
              min_y = yy
            }
            if yy > max_y {
              max_y = yy
            }
          }
        }
      }
      chars.push(CharSegment::{
        x_start: start_x,
        x_end: end_x,
        width: char_width,
        y_start: min_y,
        y_end: max_y,
        height: max_y - min_y + 1,
        pixel_count,
      })
    }
  }
  chars
}

///|
/// 提取文本行图像
pub fn extract_text_line_image(
  img : @types.Image,
  line : TextLineInfo,
  padding? : Int = 2,
) -> @types.Image raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_text_line_image: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "extract_text_line_image: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "extract_text_line_image: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if line.width <= 0 || line.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_text_line_image: 文本行尺寸必须大于0",
    )
  }
  if padding < 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_text_line_image: padding不能为负数",
    )
  }

  let w = img.width
  let h = img.height
  let ch = img.channels
  let x_start = max(0, line.x_start - padding)
  let x_end = min(w - 1, line.x_end + padding)
  let y_start = max(0, line.y_start - padding)
  let y_end = min(h - 1, line.y_end + padding)
  let new_w = x_end - x_start + 1
  let new_h = y_end - y_start + 1
  let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  let data = img.data
  for y in 0..<new_h {
    for x in 0..<new_w {
      for c in 0..<ch {
        out[(y * new_w + x) * ch + c] = data[((y_start + y) * w + (x_start + x)) *
          ch +
          c]
      }
    }
  }
  @types.Image::{
    width: new_w,
    height: new_h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 提取字符图像
pub fn extract_char_image(
  img : @types.Image,
  char : CharSegment,
  target_size? : Int = 32,
  padding? : Int = 2,
) -> @types.Image raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_char_image: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "extract_char_image: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "extract_char_image: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if char.width <= 0 || char.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_char_image: 字符尺寸必须大于0",
    )
  }
  if target_size <= 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_char_image: target_size必须大于0",
    )
  }
  if padding < 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_char_image: padding不能为负数",
    )
  }

  let w = img.width
  let h = img.height
  let ch = img.channels
  let x_start = max(0, char.x_start - padding)
  let x_end = min(w - 1, char.x_end + padding)
  let y_start = max(0, char.y_start - padding)
  let y_end = min(h - 1, char.y_end + padding)
  let src_w = x_end - x_start + 1
  let src_h = y_end - y_start + 1
  // 缩放到目标尺寸(保持宽高比,居中)
  let scale_w = target_size.to_double() / src_w.to_double()
  let scale_h = target_size.to_double() / src_h.to_double()
  let scale = if scale_w < scale_h { scale_w } else { scale_h }
  let new_w = max(1, (src_w.to_double() * scale).to_int())
  let new_h = max(1, (src_h.to_double() * scale).to_int())
  let offset_x = (target_size - new_w) / 2
  let offset_y = (target_size - new_h) / 2
  let out : Array[Byte] = Array::make(target_size * target_size * ch, b'\x00')
  let data = img.data
  // 最近邻缩放
  for y in 0..<new_h {
    for x in 0..<new_w {
      let src_x = x_start + x * src_w / new_w
      let src_y = y_start + y * src_h / new_h
      for c in 0..<ch {
        out[((offset_y + y) * target_size + (offset_x + x)) * ch + c] = data[(
            src_y * w + src_x
          ) *
          ch +
          c]
      }
    }
  }
  @types.Image::{
    width: target_size,
    height: target_size,
    channels: ch,
    data: Bytes::from_array(out),
  }
}