// File: /workspace/github_cpp_makefile_star2_O3/ksraj123[P]RAW12-to-bitmap-AVI/src/OutputWriter.cpp
// Function: OutputWriter::WritePpm(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, ImageProcessor*)
void OutputWriter::WritePpm(std::string chanelName, ImageProcessor* debayeredImg)
{
    std::string outputFileName = "result/" + chanelName + ".ppm";

    std::ofstream outputPPM;
    outputPPM.open(outputFileName, std::ios::out | std::ios::trunc);
    if(!outputPPM)
    {
        std::cerr << "Error: File cannot be opened\n\tExiting" << std::endl;
        exit(1);
    }

    outputPPM << "P6\n" << "# " << chanelName << ".ppm\n" << IMAGE_WIDTH
               << " " << IMAGE_HEIGHT << "\n" << "255" << "\n";

    uint8_t* ppmBin = nullptr;
    switch (chanelName[0])
        {
            case 'r':
                ppmBin = ConvertToPPM(debayeredImg->redChannel, "red");;
                break;
            case 'g':
                ppmBin = ConvertToPPM(debayeredImg->greenChannel, "green");
                break;
            case 'b':
                ppmBin = ConvertToPPM(debayeredImg->blueChannel, "blue");
                break;
        }
    outputPPM.write(reinterpret_cast<const char*> (ppmBin), TOTAL_PIX * 3);

    outputPPM.close();
    if (!outputPPM)
    {
        std::cerr << "Error: File cannot be Closed\n\tExiting" << std::endl;
        exit(1);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x280, %rsp            # imm = 0x280
movq %rdx, %rbx
movq %rsi, %r14
leaq 0x2317(%rip), %rsi      # 0x52e0
leaq -0x2a0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x3df4
leaq 0x2306(%rip), %rsi      # 0x52e8
movq %r15, %rdi
callq 0x2260
leaq -0x30(%rbp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x300f
movq %rdx, -0x40(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x30(%rbp)
jmp 0x3017
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
movq %rdx, -0x38(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0x290(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x304d
movq -0x290(%rbp), %rsi
incq %rsi
callq 0x2180
leaq -0x2a0(%rbp), %rdi
callq 0x2220
leaq -0x2a0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl $0x30, %edx
callq 0x20c0
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x280(%rbp,%rax)
jne 0x32c7
leaq 0x2285(%rip), %rsi      # 0x5313
leaq -0x2a0(%rbp), %rdi
movl $0x3, %edx
callq 0x21a0
leaq 0x2271(%rip), %rsi      # 0x5317
leaq -0x2a0(%rbp), %rdi
movl $0x2, %edx
callq 0x21a0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
leaq -0x2a0(%rbp), %rdi
callq 0x21a0
movq %rax, %r15
leaq 0x2246(%rip), %rsi      # 0x531a
movl $0x5, %edx
movq %rax, %rdi
callq 0x21a0
movq %r15, %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x2230
movq %rax, %r15
leaq 0x2220(%rip), %rsi      # 0x5318
movl $0x1, %edx
movq %rax, %rdi
callq 0x21a0
movq %r15, %rdi
movl $0xc00, %esi            # imm = 0xC00
callq 0x2230
movq %rax, %r15
leaq 0x21f9(%rip), %rsi      # 0x5315
movl $0x1, %edx
movq %rax, %rdi
callq 0x21a0
leaq 0x21f0(%rip), %rsi      # 0x5320
movl $0x3, %edx
movq %r15, %rdi
callq 0x21a0
leaq 0x21d1(%rip), %rsi      # 0x5315
movl $0x1, %edx
movq %r15, %rdi
callq 0x21a0
movq (%r14), %rax
movzbl (%rax), %eax
cmpl $0x62, %eax
je 0x31e9
cmpl $0x67, %eax
je 0x31b1
cmpl $0x72, %eax
jne 0x3231
movq (%rbx), %rbx
leaq -0x90(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1ff0(%rip), %rsi      # 0x5173
leaq 0x1fec(%rip), %rdx      # 0x5176
leaq -0xa0(%rbp), %rdi
callq 0x2c0e
leaq -0xa0(%rbp), %rdx
movq %rbx, %rsi
callq 0x33b4
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
jmp 0x321f
movq 0x10(%rbx), %rbx
leaq -0x70(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1fe3(%rip), %rsi      # 0x51a7
leaq 0x1fe1(%rip), %rdx      # 0x51ac
leaq -0x80(%rbp), %rdi
callq 0x2c0e
leaq -0x80(%rbp), %rdx
movq %rbx, %rsi
callq 0x33b4
movq %rax, %rbx
movq -0x80(%rbp), %rdi
jmp 0x321f
movq 0x8(%rbx), %rbx
leaq -0x50(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1f91(%rip), %rsi      # 0x518d
leaq 0x1f8e(%rip), %rdx      # 0x5191
leaq -0x60(%rbp), %rdi
callq 0x2c0e
leaq -0x60(%rbp), %rdx
movq %rbx, %rsi
callq 0x33b4
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpq %r14, %rdi
je 0x3233
movq (%r14), %rsi
incq %rsi
callq 0x2180
jmp 0x3233
xorl %ebx, %ebx
leaq -0x2a0(%rbp), %rdi
movl $0x2400000, %edx        # imm = 0x2400000
movq %rbx, %rsi
callq 0x21d0
leaq -0x2a0(%rbp), %rdi
callq 0x21e0
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x280(%rbp,%rax)
jne 0x32ed
movq 0x3d55(%rip), %rax      # 0x6fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x298(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x2a0(%rbp,%rcx)
callq 0x2130
leaq -0x1a8(%rbp), %rdi
callq 0x20a0
movq -0x40(%rbp), %rdi
cmpq %r12, %rdi
je 0x32b7
movq -0x30(%rbp), %rsi
incq %rsi
callq 0x2180
addq $0x280, %rsp            # imm = 0x280
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x3d22(%rip), %rdi      # 0x6ff0
leaq 0x2018(%rip), %rsi      # 0x52ed
movl $0x25, %edx
callq 0x21a0
movq 0x3d0a(%rip), %rdi      # 0x6ff0
callq 0x20d0
jmp 0x3311
movq 0x3cfc(%rip), %rdi      # 0x6ff0
leaq 0x2029(%rip), %rsi      # 0x5324
movl $0x25, %edx
callq 0x21a0
movq 0x3ce4(%rip), %rdi      # 0x6ff0
callq 0x20d0
movl $0x1, %edi
callq 0x21b0
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
cmpq %r14, %rdi
je 0x338b
movq -0x90(%rbp), %rsi
jmp 0x3355
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpq %r14, %rdi
je 0x338b
movq -0x50(%rbp), %rsi
jmp 0x3355
movq %rax, %rbx
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x338b
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x2180
jmp 0x338b
jmp 0x3388
jmp 0x3388
jmp 0x3388
movq %rax, %rbx
jmp 0x3397
movq %rax, %rbx
leaq -0x290(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33ac
movq -0x290(%rbp), %rsi
jmp 0x33a4
jmp 0x3388
movq %rax, %rbx
leaq -0x2a0(%rbp), %rdi
callq 0x21f0
movq -0x40(%rbp), %rdi
cmpq %r12, %rdi
je 0x33ac
movq -0x30(%rbp), %rsi
incq %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x2240