// File: brianwu02[P]ImageProcessing/convolve.cpp
// Function: main
int main(int argc, char** argv) {
    std::string kernelString;
    std::string str;
    imageP I1, I2, paddedImg;
    kernelP k;

    cout << "infile: " << argv[1] << endl;
    cout << "kernel File: " << argv[2] << endl;
    cout << "outfile: " << argv[3] << endl;
    float *kernelValues;
    kernelValues = openKernelFile(argv[2]);

    I1 = IP_readImage(argv[1]);
    I2 = NEWIMAGE;
    paddedImg = NEWIMAGE;
    k = NEWKERNEL;

    
    k->height = kernelValues[0];
    k->width = kernelValues[1];
    int ksize = k->height * k->width;
    k->kernel = (float *) malloc(4 * ksize); // stupid. have to multiply by 4 bytes. man why does c++ suck.
    float *kimg = k->kernel;
    
    // copy kernel values to another pointer, yes inefficient but it works ok!
    for (int i=0; i<9; i++) {
        kimg[i] = kernelValues[i+2];
    }

    // hard code some sample values
    // weighted average
    /*kimg[0] = 1;
    kimg[1] = 2;
    kimg[2] = 1;
    kimg[3] = 2;
    kimg[4] = 4;
    kimg[5] = 2;
    kimg[6] = 1;
    kimg[7] = 2;
    kimg[8] = 1;
    */
    
    /*kimg[0] = 0;
    kimg[1] = -1;
    kimg[2] = 0;
    kimg[3] =  -1;
    kimg[4] = 5;
    kimg[5] = -1;
    kimg[6] = 0;
    kimg[7] = -1;
    kimg[8] = 0;
    */
    
    /*
    kimg[0] = -1.0;
    kimg[1] = -1.0;
    kimg[2] = -1.0;
    kimg[3] = -1.0;
    kimg[4] = 8.0;
    kimg[5] = -1.0;
    kimg[6] = -1.0;
    kimg[7] = -1.0;
    kimg[8] = -1.0;
    */

    /*
    kimg[0] = 1;
    kimg[1] = 1;
    kimg[2] = 1;
    kimg[3] = 1;
    kimg[4] = 1;
    kimg[5] = 1;
    kimg[6] = 1;
    kimg[7] = 1;
    kimg[8] = 1;
    */
    /*
    kimg[0] = -10;
    kimg[1] = -10;
    kimg[2] = -10;
    kimg[3] = 0;
    kimg[4] = 0;
    kimg[5] = 0;
    kimg[6] = 10;
    kimg[7] = 10;
    kimg[8] = 10;
    */

    /*
    kimg[0] = -10;
    kimg[1] = 0;
    kimg[2] = 10;
    kimg[3] = -10;
    kimg[4] = 0;
    kimg[5] = 10;
    kimg[6] = -10;
    kimg[7] = 0;
    kimg[8] = 10;
    */

    // fix the size.
    int sz = 3;
    padImage(I1, sz, paddedImg);

    convolve(I1, paddedImg, k, I2);
    IP_saveImage(I2, argv[3]);
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x22b0
leaq -0x50(%rbp), %rdi
callq 0x22b0
movq 0x49ce(%rip), %rdi      # 0x6fd0
leaq 0x29fb(%rip), %rsi      # 0x5004
callq 0x21c0
movq %rax, -0xa8(%rbp)
jmp 0x2617
movq -0xa8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x21c0
movq %rax, -0xb0(%rbp)
jmp 0x2634
movq -0xb0(%rbp), %rdi
movq 0x497e(%rip), %rsi      # 0x6fc0
callq 0x21d0
jmp 0x2649
movq 0x4980(%rip), %rdi      # 0x6fd0
leaq 0x29b6(%rip), %rsi      # 0x500d
callq 0x21c0
movq %rax, -0xb8(%rbp)
jmp 0x2665
movq -0xb8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x21c0
movq %rax, -0xc0(%rbp)
jmp 0x2682
movq -0xc0(%rbp), %rdi
movq 0x4930(%rip), %rsi      # 0x6fc0
callq 0x21d0
jmp 0x2697
movq 0x4932(%rip), %rdi      # 0x6fd0
leaq 0x2976(%rip), %rsi      # 0x501b
callq 0x21c0
movq %rax, -0xc8(%rbp)
jmp 0x26b3
movq -0xc8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x21c0
movq %rax, -0xd0(%rbp)
jmp 0x26d0
movq -0xd0(%rbp), %rdi
movq 0x48e2(%rip), %rsi      # 0x6fc0
callq 0x21d0
jmp 0x26e5
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x28a0
movq %rax, -0xd8(%rbp)
jmp 0x26fb
movq -0xd8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4210
movq %rax, -0xe0(%rbp)
jmp 0x271f
movq -0xe0(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x10, %edi
callq 0x2280
movq %rax, -0x60(%rbp)
movl $0x10, %edi
callq 0x2280
movq %rax, -0x68(%rbp)
movl $0x10, %edi
callq 0x2280
movq %rax, -0x70(%rbp)
movq -0x88(%rbp), %rax
cvttss2si (%rax), %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x88(%rbp), %rax
cvttss2si 0x4(%rax), %ecx
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
movq -0x70(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x70(%rbp), %rcx
imull (%rcx), %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x2280
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
cmpl $0x9, -0x9c(%rbp)
jge 0x2826
movq -0x88(%rbp), %rax
movl -0x9c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x98(%rbp), %rax
movslq -0x9c(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x27c1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x2130
leaq -0x30(%rbp), %rdi
callq 0x2130
jmp 0x2894
movl $0x3, -0xa0(%rbp)
movq -0x58(%rbp), %rdi
movl -0xa0(%rbp), %esi
movq -0x68(%rbp), %rdx
callq 0x4760
jmp 0x2845
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x2d40
jmp 0x285c
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x4530
jmp 0x286f
movl $0x1, -0x4(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x2130
leaq -0x30(%rbp), %rdi
callq 0x2130
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x23b0
nopl (%rax)