// File: /workspace/github_cpp_makefile_star2_O1/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 0x2313(%rip), %rsi      # 0x52e0
leaq -0x2a0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x3dea
leaq 0x2302(%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 0x3013
movq %rdx, -0x40(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x30(%rbp)
jmp 0x301b
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 0x3051
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 0x32cb
leaq 0x2281(%rip), %rsi      # 0x5313
leaq -0x2a0(%rbp), %rdi
movl $0x3, %edx
callq 0x21a0
leaq 0x226d(%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 0x2242(%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 0x221c(%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 0x21f5(%rip), %rsi      # 0x5315
movl $0x1, %edx
movq %rax, %rdi
callq 0x21a0
leaq 0x21ec(%rip), %rsi      # 0x5320
movl $0x3, %edx
movq %r15, %rdi
callq 0x21a0
leaq 0x21cd(%rip), %rsi      # 0x5315
movl $0x1, %edx
movq %r15, %rdi
callq 0x21a0
movq (%r14), %rax
movzbl (%rax), %eax
cmpl $0x62, %eax
je 0x31ed
cmpl $0x67, %eax
je 0x31b5
cmpl $0x72, %eax
jne 0x3235
movq (%rbx), %rbx
leaq -0x90(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1fec(%rip), %rsi      # 0x5173
leaq 0x1fe8(%rip), %rdx      # 0x5176
leaq -0xa0(%rbp), %rdi
callq 0x2c0e
leaq -0xa0(%rbp), %rdx
movq %rbx, %rsi
callq 0x33b8
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
jmp 0x3223
movq 0x10(%rbx), %rbx
leaq -0x70(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1fdf(%rip), %rsi      # 0x51a7
leaq 0x1fdd(%rip), %rdx      # 0x51ac
leaq -0x80(%rbp), %rdi
callq 0x2c0e
leaq -0x80(%rbp), %rdx
movq %rbx, %rsi
callq 0x33b8
movq %rax, %rbx
movq -0x80(%rbp), %rdi
jmp 0x3223
movq 0x8(%rbx), %rbx
leaq -0x50(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1f8d(%rip), %rsi      # 0x518d
leaq 0x1f8a(%rip), %rdx      # 0x5191
leaq -0x60(%rbp), %rdi
callq 0x2c0e
leaq -0x60(%rbp), %rdx
movq %rbx, %rsi
callq 0x33b8
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpq %r14, %rdi
je 0x3237
movq (%r14), %rsi
incq %rsi
callq 0x2180
jmp 0x3237
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 0x32f1
movq 0x3d51(%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 0x32bb
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 0x3d1e(%rip), %rdi      # 0x6ff0
leaq 0x2014(%rip), %rsi      # 0x52ed
movl $0x25, %edx
callq 0x21a0
movq 0x3d06(%rip), %rdi      # 0x6ff0
callq 0x20d0
jmp 0x3315
movq 0x3cf8(%rip), %rdi      # 0x6ff0
leaq 0x2025(%rip), %rsi      # 0x5324
movl $0x25, %edx
callq 0x21a0
movq 0x3ce0(%rip), %rdi      # 0x6ff0
callq 0x20d0
movl $0x1, %edi
callq 0x21b0
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
cmpq %r14, %rdi
je 0x338f
movq -0x90(%rbp), %rsi
jmp 0x3359
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpq %r14, %rdi
je 0x338f
movq -0x50(%rbp), %rsi
jmp 0x3359
movq %rax, %rbx
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x338f
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x2180
jmp 0x338f
jmp 0x338c
jmp 0x338c
jmp 0x338c
movq %rax, %rbx
jmp 0x339b
movq %rax, %rbx
leaq -0x290(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x33b0
movq -0x290(%rbp), %rsi
jmp 0x33a8
jmp 0x338c
movq %rax, %rbx
leaq -0x2a0(%rbp), %rdi
callq 0x21f0
movq -0x40(%rbp), %rdi
cmpq %r12, %rdi
je 0x33b0
movq -0x30(%rbp), %rsi
incq %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x2240