// File: /workspace/github_cpp_makefile_star2_O2/leobru[P]ACPUfont/splitter.cc
// Function: main
int main(int argc, char**argv) {
    int full, full_thr, cir_thr;
    char p2[3];
    int top_heavy_corr = 0;
    int opt;
    const char * postproc = NULL;
    bool do_dump_marked = false;
    while ((opt = getopt(argc, argv, "v:h:t:m")) != -1) {
        switch (opt) {
        case 'v':
            vsize = atoi(optarg);
            break;
        case 'h':
            hsize = atoi(optarg);
            break;
        case 't':
            top_heavy_corr = atoi(optarg);
            break;
        case 'm':
            do_dump_marked = true;
            break;
        case '?':
            fprintf(stderr, "Usage: splitter [-t N] [-v N] [-h N] [-m] [postprocessing command]\n");
            exit(1);
        }
    }
    if (optind < argc) {
        postproc = argv[optind];
        if (!*postproc) postproc = NULL;
    }
    Picture array(NULL, postproc);
    // Find local minima (darkest spots)
    vector<int> horcenters = centers(smooth(array.average_lines(0, array.width-1), vsize/4), vsize);
    vector<int> leftcenters = centers(smooth(array.average_lines(0, array.width/2), vsize/4), vsize);
    vector<int> rightcenters = centers(smooth(array.average_lines(array.width/2+1, array.width-1), vsize/4), vsize);

    double skew = 0;
    for (int i = 0; i < horcenters.size(); ++i) {
        skew += rightcenters[i]-leftcenters[i];
    }
    skew /= horcenters.size();

    // Find average interval
    double interv = 0;
    for (uint i = 1; i < horcenters.size(); ++i) interv += horcenters[i]-horcenters[i-1];
    interv /= horcenters.size()-1;
    printf("Found %d lines, average line interval = %.2f\n", horcenters.size(), interv);

    vector<int> vercenters = centers(smooth(array.average_columns(0, array.height-1), hsize/4), hsize);
    interv = 0;
    for (uint i = 1; i < vercenters.size(); ++i) interv += vercenters[i]-vercenters[i-1];
    interv /= vercenters.size()-1;
    printf("Found %d columns, average pitch = %.2f\n", vercenters.size(), interv);
    if (do_dump_marked) {
        array.dump_marked(&horcenters, &vercenters);
        exit(0);
    }
    
    for (int line = 0; line < (int)horcenters.size(); ++line) {
        // Looking from the right, find the column of the apostrophe (that's when whiteness drops)
        uint col = vercenters.size()-1;
        double avg = array.rectavg(horcenters[line], vercenters[col], hsize/2, vsize/2);
        for (; --col; ) {
            double cur = array.rectavg(horcenters[line], vercenters[col], hsize/2, vsize/2);
            if (cur < avg*0.95) break;
            avg = cur;
        }
        // 'col' is the column number of GOST 0137
        col -= 0137;
        fprintf(stderr, "Dumping line %d starting from column %d\n", line, col);
	for (int gost = 0; gost <= 0137; ++gost) {
            int hci = horcenters[line];
            int v4 = (vercenters.size()+2)/4, v2 = (vercenters.size()+1)/2;
            int curcol = gost + col;
            int hc = curcol < v4  ? hci-skew : curcol < v2 ? hci-skew/2 : curcol < v2+v4 ? hci+skew/2 : hci+skew;
            int vc = vercenters[curcol];
            // improve(array, hc, vc);
            array.dump(hc+top_heavy_corr, vc, line, col, gost);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movq %rsi, %r14
movl %edi, %ebp
xorl %r12d, %r12d
leaq 0x1b92(%rip), %r15      # 0x3050
movq 0x3af3(%rip), %rbx      # 0x4fb8
movl $0x0, 0x2c(%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1060
cmpl $0x68, %eax
je 0x150d
cmpl $0x76, %eax
je 0x14fd
cmpl $-0x1, %eax
je 0x1530
cmpl $0x6d, %eax
je 0x151d
cmpl $0x74, %eax
je 0x1522
cmpl $0x3f, %eax
jne 0x14cd
jmp 0x1a18
movq (%rbx), %rdi
callq 0x11b0
movl %eax, 0x3c11(%rip)      # 0x511c
jmp 0x14cd
movq (%rbx), %rdi
callq 0x11b0
movl %eax, 0x3bfd(%rip)      # 0x5118
jmp 0x14cd
movb $0x1, %r12b
jmp 0x14cd
movq (%rbx), %rdi
callq 0x11b0
movl %eax, 0x2c(%rsp)
jmp 0x14cd
movq 0x3aa1(%rip), %rax      # 0x4fd8
movslq (%rax), %rax
cmpl %ebp, %eax
jge 0x154d
movq (%r14,%rax,8), %rax
xorl %edx, %edx
cmpb $0x0, (%rax)
cmovneq %rax, %rdx
jmp 0x154f
xorl %edx, %edx
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x1ba4
movl 0x41c(%r14), %ecx
decl %ecx
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x1daa
movl 0x3b9d(%rip), %eax      # 0x511c
pushq $0x4
popq %rbp
cltd
idivl %ebp
leaq 0x88(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movl %eax, %edx
callq 0x1363
movl 0x3b7d(%rip), %edx      # 0x511c
leaq 0x50(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x1404
leaq 0x88(%rsp), %rdi
callq 0x265e
leaq 0x38(%rsp), %rdi
callq 0x265e
leaq 0xb8(%rsp), %rsi
movl 0x41c(%rsi), %eax
pushq $0x2
popq %r15
cltd
idivl %r15d
leaq 0x10(%rsp), %rdi
xorl %edx, %edx
movl %eax, %ecx
callq 0x1daa
movl 0x3b2a(%rip), %eax      # 0x511c
cltd
idivl %ebp
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl %eax, %edx
callq 0x1363
movl 0x3b10(%rip), %edx      # 0x511c
leaq 0x88(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x1404
leaq 0x38(%rsp), %rdi
callq 0x265e
leaq 0x10(%rsp), %rdi
callq 0x265e
leaq 0xb8(%rsp), %rsi
movl 0x41c(%rsi), %ecx
movl %ecx, %eax
cltd
idivl %r15d
leal 0x1(%rax), %edx
decl %ecx
leaq 0x70(%rsp), %rdi
callq 0x1daa
movl 0x3ac1(%rip), %eax      # 0x511c
cltd
idivl %ebp
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl %eax, %edx
callq 0x1363
movl 0x3aa7(%rip), %edx      # 0x511c
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1404
leaq 0x10(%rsp), %rdi
callq 0x265e
leaq 0x70(%rsp), %rdi
callq 0x265e
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rsi
subq %rax, %rsi
sarq $0x2, %rsi
movq 0x38(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %edx, %edx
movq 0x88(%rsp), %rdi
cmpq %rdx, %rsi
je 0x16e8
movl (%rcx,%rdx,4), %r8d
subl (%rdi,%rdx,4), %r8d
cvtsi2sd %r8d, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
incq %rdx
jmp 0x16c1
cvtsi2sd %rsi, %xmm0
movsd %xmm0, 0x30(%rsp)
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
leal 0x1(%rcx), %edx
cmpq %rdx, %rsi
jbe 0x1714
movl (%rax,%rdx,4), %edx
subl (%rax,%rcx,4), %edx
cvtsi2sd %edx, %xmm1
addsd %xmm1, %xmm0
incq %rcx
jmp 0x16f9
leaq -0x1(%rsi), %rax
movq %rax, %xmm1
punpckldq 0x18eb(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x18f3(%rip), %xmm1     # 0x3020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x1958(%rip), %rdi      # 0x309c
movb $0x1, %al
callq 0x1040
leaq 0xb8(%rsp), %rsi
movl 0x420(%rsi), %ecx
decl %ecx
leaq 0xa0(%rsp), %rdi
xorl %edx, %edx
callq 0x1e44
movl 0x39a8(%rip), %eax      # 0x5118
cltd
idivl %ebp
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movl %eax, %edx
callq 0x1363
movl 0x398b(%rip), %edx      # 0x5118
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x1404
movsd 0x8(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x265e
leaq 0xa0(%rsp), %rdi
callq 0x265e
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rsi
subq %rax, %rsi
sarq $0x2, %rsi
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
leal 0x1(%rcx), %edx
cmpq %rdx, %rsi
jbe 0x17f7
movl (%rax,%rdx,4), %edx
subl (%rax,%rcx,4), %edx
cvtsi2sd %edx, %xmm1
addsd %xmm1, %xmm0
incq %rcx
jmp 0x17dc
leaq -0x1(%rsi), %rax
movq %rax, %xmm1
punpckldq 0x1808(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1810(%rip), %xmm1     # 0x3020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x18a3(%rip), %rdi      # 0x30ca
movb $0x1, %al
callq 0x1040
testb $0x1, %r12b
jne 0x1a3c
movsd 0x1800(%rip), %xmm0     # 0x3040
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
leaq 0xb8(%rsp), %r14
xorl %r12d, %r12d
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r12
jge 0x19cc
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rbp
subq %rcx, %rbp
shrq $0x2, %rbp
leal -0x1(%rbp), %edx
movl (%rax,%r12,4), %esi
movl (%rcx,%rdx,4), %ecx
movl 0x3883(%rip), %eax      # 0x5118
cltd
idivl %r15d
movl %eax, %r8d
movl 0x387a(%rip), %eax      # 0x511c
cltd
idivl %r15d
movq %r14, %rdi
movl %ecx, %edx
movl %r8d, %ecx
movl %eax, %r8d
callq 0x2038
leal -0x61(%rbp), %eax
addl $-0x2, %ebp
movl %eax, %r13d
testl %ebp, %ebp
je 0x191c
movq 0x50(%rsp), %rax
movl (%rax,%r12,4), %esi
movq 0x10(%rsp), %rax
movl (%rax,%rbp,4), %ecx
movl 0x383e(%rip), %eax      # 0x5118
cltd
idivl %r15d
movl %eax, %r8d
movl 0x3835(%rip), %eax      # 0x511c
cltd
idivl %r15d
movq %r14, %rdi
movl %ecx, %edx
movl %r8d, %ecx
movl %eax, %r8d
movsd %xmm0, 0x30(%rsp)
callq 0x2038
movsd 0x30(%rsp), %xmm1
mulsd 0x1739(%rip), %xmm1     # 0x3048
leal -0x1(%r13), %eax
decq %rbp
ucomisd %xmm0, %xmm1
jbe 0x18bc
movq 0x36ad(%rip), %rax      # 0x4fd0
movq (%rax), %rdi
leaq 0x17c5(%rip), %rsi      # 0x30f2
movl %r12d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x1190
movl %r13d, %ebx
xorl %ebp, %ebp
cmpq $0x60, %rbp
je 0x19c4
movq 0x50(%rsp), %rsi
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
leaq 0x2(%rcx), %rdx
shrq $0x2, %rdx
cvtsi2sdl (%rsi,%r12,4), %xmm0
cmpl %edx, %ebx
jge 0x1975
subsd 0x8(%rsp), %xmm0
jmp 0x199b
incq %rcx
shrq %rcx
cmpl %ecx, %ebx
jge 0x1987
subsd 0x68(%rsp), %xmm0
jmp 0x199b
addl %edx, %ecx
cmpl %ecx, %ebx
jge 0x1995
addsd 0x68(%rsp), %xmm0
jmp 0x199b
addsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %esi
movslq %ebx, %rcx
movl (%rax,%rcx,4), %edx
addl 0x2c(%rsp), %esi
movq %r14, %rdi
movl %r12d, %ecx
movl %r13d, %r8d
movl %ebp, %r9d
callq 0x2130
incq %rbp
incl %ebx
jmp 0x193f
incq %r12
jmp 0x1857
leaq 0x10(%rsp), %rdi
callq 0x279c
leaq 0x38(%rsp), %rdi
callq 0x279c
leaq 0x88(%rsp), %rdi
callq 0x279c
leaq 0x50(%rsp), %rdi
callq 0x279c
leaq 0xb8(%rsp), %rdi
callq 0x23de
xorl %eax, %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x35b1(%rip), %rax      # 0x4fd0
movq (%rax), %rcx
leaq 0x162f(%rip), %rdi      # 0x3058
pushq $0x43
popq %rsi
pushq $0x1
popq %rdx
callq 0x1200
pushq $0x1
popq %rdi
callq 0x1170
leaq 0xb8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1ee8
xorl %edi, %edi
callq 0x1170
jmp 0x1af1
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x265e
jmp 0x1a71
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x265e
jmp 0x1afe
movq %rax, %rbx
jmp 0x1afe
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x265e
jmp 0x1a97
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x265e
jmp 0x1b08
movq %rax, %rbx
jmp 0x1b08
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x265e
jmp 0x1aba
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x265e
jmp 0x1b15
movq %rax, %rbx
jmp 0x1b15
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x265e
jmp 0x1ae0
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x265e
jmp 0x1b1f
movq %rax, %rbx
jmp 0x1b1f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x279c
leaq 0x38(%rsp), %rdi
callq 0x279c
leaq 0x88(%rsp), %rdi
callq 0x279c
leaq 0x50(%rsp), %rdi
callq 0x279c
leaq 0xb8(%rsp), %rdi
callq 0x23de
movq %rbx, %rdi
callq 0x11d0