// File: IsaacLaw12[P]RayTracer/./src/Image.cpp
// Function: Image::save_image(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void Image::save_image(std::string file_name){
    std::ofstream output(file_name);

    if (focus_blur){
        // Blur the rgb matrices based on their distance t_values
        std::cout << "Adding focus blur\n";
        apply_focus_blur();
    }

    if (anti_alias > 0){
        std::cout << "Anti-aliasing image\n";
        apply_anti_alias();
    }

    output << "P3" << "\n";
    output << orig_width << " " << orig_height << " 255" << "\n";
    int red, green, blue;
    for (int i=orig_height-1;i>=0; i--){
        for (int j=0; j<orig_width; j++){
            red = convert_255(red_pixels(j, i));
            green = convert_255(green_pixels(j, i));
            blue = convert_255(blue_pixels(j, i));
            output << red << " " << green << " " << blue << " ";
        }
        output << "\n";
    }
    output.close();
    std::cout << "Image saved at: " << file_name << "\n";
}

// Assembly:
subq $0x2d8, %rsp            # imm = 0x2D8
movq %rsi, 0x98(%rsp)
movq %rdi, 0x2d0(%rsp)
movq %rsi, 0x2c8(%rsp)
movq 0x2d0(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0xc8(%rsp), %rdi
movl $0x10, %edx
callq 0x4650
movq 0xa0(%rsp), %rax
testb $0x1, (%rax)
je 0x52aa
movq 0x95d5b(%rip), %rdi     # 0x9afc0
leaq 0x57dc4(%rip), %rsi     # 0x5d030
callq 0x4360
jmp 0x5273
movq 0xa0(%rsp), %rdi
callq 0x5620
jmp 0x5282
jmp 0x52aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x44e0
jmp 0x560d
movq 0xa0(%rsp), %rax
cmpl $0x0, 0x4(%rax)
jle 0x52de
movq 0x95d01(%rip), %rdi     # 0x9afc0
leaq 0x57d7d(%rip), %rsi     # 0x5d043
callq 0x4360
jmp 0x52cd
movq 0xa0(%rsp), %rdi
callq 0x57f0
jmp 0x52dc
jmp 0x52de
leaq 0x57d73(%rip), %rsi     # 0x5d058
leaq 0xc8(%rsp), %rdi
callq 0x4360
movq %rax, 0x90(%rsp)
jmp 0x52fc
movq 0x90(%rsp), %rdi
leaq 0x5caf2(%rip), %rsi     # 0x61dfd
callq 0x4360
jmp 0x5312
movq 0xa0(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0xc8(%rsp), %rdi
callq 0x45c0
movq %rax, 0x88(%rsp)
jmp 0x5334
movq 0x88(%rsp), %rdi
leaq 0x66c3a(%rip), %rsi     # 0x6bf7d
callq 0x4360
movq %rax, 0x80(%rsp)
jmp 0x5352
movq 0x80(%rsp), %rdi
movq 0xa0(%rsp), %rax
movl 0xc(%rax), %esi
callq 0x45c0
movq %rax, 0x78(%rsp)
jmp 0x5371
movq 0x78(%rsp), %rdi
leaq 0x57cde(%rip), %rsi     # 0x5d05b
callq 0x4360
movq %rax, 0x70(%rsp)
jmp 0x5389
movq 0x70(%rsp), %rdi
leaq 0x5ca68(%rip), %rsi     # 0x61dfd
callq 0x4360
jmp 0x539c
movq 0xa0(%rsp), %rax
movl 0xc(%rax), %eax
subl $0x1, %eax
movl %eax, 0xac(%rsp)
cmpl $0x0, 0xac(%rsp)
jl 0x55a3
movl $0x0, 0xa8(%rsp)
movq 0xa0(%rsp), %rcx
movl 0xa8(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0x5575
movq 0xa0(%rsp), %rdi
addq $0x18, %rdi
movslq 0xa8(%rsp), %rsi
movslq 0xac(%rsp), %rdx
callq 0x61c0
movq %rax, 0x68(%rsp)
jmp 0x540a
movq 0xa0(%rsp), %rdi
movq 0x68(%rsp), %rax
movsd (%rax), %xmm0
callq 0x5b80
movl %eax, 0x64(%rsp)
jmp 0x5426
movq 0xa0(%rsp), %rdi
movl 0x64(%rsp), %eax
movl %eax, 0xb8(%rsp)
addq $0x30, %rdi
movslq 0xa8(%rsp), %rsi
movslq 0xac(%rsp), %rdx
callq 0x61c0
movq %rax, 0x58(%rsp)
jmp 0x5459
movq 0xa0(%rsp), %rdi
movq 0x58(%rsp), %rax
movsd (%rax), %xmm0
callq 0x5b80
movl %eax, 0x54(%rsp)
jmp 0x5475
movq 0xa0(%rsp), %rdi
movl 0x54(%rsp), %eax
movl %eax, 0xb4(%rsp)
addq $0x48, %rdi
movslq 0xa8(%rsp), %rsi
movslq 0xac(%rsp), %rdx
callq 0x61c0
movq %rax, 0x48(%rsp)
jmp 0x54a8
movq 0xa0(%rsp), %rdi
movq 0x48(%rsp), %rax
movsd (%rax), %xmm0
callq 0x5b80
movl %eax, 0x44(%rsp)
jmp 0x54c4
movl 0x44(%rsp), %eax
movl %eax, 0xb0(%rsp)
movl 0xb8(%rsp), %esi
leaq 0xc8(%rsp), %rdi
callq 0x45c0
movq %rax, 0x38(%rsp)
jmp 0x54ea
movq 0x38(%rsp), %rdi
leaq 0x66a87(%rip), %rsi     # 0x6bf7d
callq 0x4360
movq %rax, 0x30(%rsp)
jmp 0x5502
movq 0x30(%rsp), %rdi
movl 0xb4(%rsp), %esi
callq 0x45c0
movq %rax, 0x28(%rsp)
jmp 0x551a
movq 0x28(%rsp), %rdi
leaq 0x66a57(%rip), %rsi     # 0x6bf7d
callq 0x4360
movq %rax, 0x20(%rsp)
jmp 0x5532
movq 0x20(%rsp), %rdi
movl 0xb0(%rsp), %esi
callq 0x45c0
movq %rax, 0x18(%rsp)
jmp 0x554a
movq 0x18(%rsp), %rdi
leaq 0x66a27(%rip), %rsi     # 0x6bf7d
callq 0x4360
jmp 0x555d
jmp 0x555f
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
jmp 0x53ca
leaq 0x5c881(%rip), %rsi     # 0x61dfd
leaq 0xc8(%rsp), %rdi
callq 0x4360
jmp 0x558b
jmp 0x558d
movl 0xac(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x53b1
leaq 0xc8(%rsp), %rdi
callq 0x44a0
jmp 0x55b2
movq 0x95a07(%rip), %rdi     # 0x9afc0
leaq 0x57aa0(%rip), %rsi     # 0x5d060
callq 0x4360
movq %rax, 0x10(%rsp)
jmp 0x55cc
movq 0x98(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x4340
movq %rax, 0x8(%rsp)
jmp 0x55e5
movq 0x8(%rsp), %rdi
leaq 0x5c80c(%rip), %rsi     # 0x61dfd
callq 0x4360
jmp 0x55f8
leaq 0xc8(%rsp), %rdi
callq 0x44e0
addq $0x2d8, %rsp            # imm = 0x2D8
retq
movq 0xc0(%rsp), %rdi
callq 0x45e0
nopw (%rax,%rax)