// File: dannye[P]jed/src/encoder.cpp
// Function: main
int main(int argc, char** argv) {
    // validate arguments
    if (argc < 2) {
        std::cout << "Error - Invalid arguments\n";
        return 1;
    }

    for (int i = 1; i < argc; ++i) {
        const std::string filename(argv[i]);

        // read image
        BMPImage image = readBMP(filename);
        // validate image
        if (image.blocks == nullptr) {
            continue;
        }

        // color conversion
        RGBToYCbCr(image);

        // Forward Discrete Cosine Transform
        forwardDCT(image);

        // quantize DCT coefficients
        quantize(image);

        // write JPG file
        const std::size_t pos = filename.find_last_of('.');
        const std::string outFilename = (pos == std::string::npos) ?
            (filename + ".jpg") :
            (filename.substr(0, pos) + ".jpg");
        writeJPG(image, outFilename);

        delete[] image.blocks;
    }
    return 0;
}

// Assembly:
subq $0xe8, %rsp
movl $0x0, 0xe4(%rsp)
movl %edi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
cmpl $0x2, 0xe0(%rsp)
jge 0x4eee
movq 0x50fe(%rip), %rdi      # 0x9fd0
leaq 0x2360(%rip), %rsi      # 0x7239
callq 0x2130
movl $0x1, 0xe4(%rsp)
jmp 0x5168
movl $0x1, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
cmpl 0xe0(%rsp), %eax
jge 0x515d
movq 0xd8(%rsp), %rax
movslq 0xd4(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
leaq 0xaf(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x2240
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
callq 0x21b0
jmp 0x4f51
leaq 0xaf(%rsp), %rdi
callq 0x2170
leaq 0x80(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x2750
jmp 0x4f75
cmpq $0x0, 0x88(%rsp)
jne 0x4fcc
movl $0x4, 0x7c(%rsp)
jmp 0x50d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0xaf(%rsp), %rdi
callq 0x2170
jmp 0x5177
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x514e
leaq 0x80(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x3080
movq 0x10(%rsp), %rdi
callq 0x3e00
jmp 0x4fea
leaq 0x80(%rsp), %rdi
callq 0x3f20
jmp 0x4ff9
leaq 0xb0(%rsp), %rdi
movl $0x2e, %esi
movq $-0x1, %rdx
callq 0x2270
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
movb $0x0, 0x2f(%rsp)
cmpq $-0x1, %rax
jne 0x5044
leaq 0x2226(%rip), %rdx      # 0x7254
leaq 0x50(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x54d0
jmp 0x5042
jmp 0x5080
movq 0x70(%rsp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq 0x30(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x2160
jmp 0x5061
movb $0x1, 0x2f(%rsp)
leaq 0x21e7(%rip), %rdx      # 0x7254
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5560
jmp 0x507e
jmp 0x5080
testb $0x1, 0x2f(%rsp)
jne 0x5089
jmp 0x5093
leaq 0x30(%rsp), %rdi
callq 0x20d0
leaq 0x80(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4bd0
jmp 0x50a7
movq 0x88(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x50c4
movq 0x8(%rsp), %rdi
callq 0x2190
leaq 0x50(%rsp), %rdi
callq 0x20d0
movl $0x0, 0x7c(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x20d0
movl 0x7c(%rsp), %eax
testl %eax, %eax
je 0x50ef
jmp 0x50ed
jmp 0x50f1
jmp 0x50f1
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x4ef9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
testb $0x1, 0x2f(%rsp)
jne 0x5124
jmp 0x512e
leaq 0x30(%rsp), %rdi
callq 0x20d0
jmp 0x514e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x20d0
leaq 0xb0(%rsp), %rdi
callq 0x20d0
jmp 0x5177
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
addq $0xe8, %rsp
retq
movq 0xa0(%rsp), %rdi
callq 0x2230
nopw %cs:(%rax,%rax)
nop