// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rdx, %rbx
movq %rsi, %r14
leaq 0x17d5(%rip), %rsi      # 0x42e0
leaq -0x2a0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x35d3
leaq 0x17c4(%rip), %rdx      # 0x42e8
leaq -0xa0(%rbp), %rdi
movq %r15, %rsi
callq 0x35aa
leaq -0x2a0(%rbp), %rdi
callq 0x2090
leaq -0x2a0(%rbp), %rdi
callq 0x21f0
leaq -0x2a0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
pushq $0x30
popq %rdx
callq 0x20a0
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x280(%rbp,%rax)
jne 0x2d0d
leaq 0x1792(%rip), %rsi      # 0x4313
leaq -0x2a0(%rbp), %rdi
callq 0x2130
leaq 0x1783(%rip), %rsi      # 0x4317
movq %rax, %rdi
callq 0x2130
movq %rax, %rdi
movq %r14, %rsi
callq 0x2120
leaq 0x176c(%rip), %rsi      # 0x431a
movq %rax, %rdi
callq 0x2130
movq %rax, %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x2200
leaq 0x174e(%rip), %rsi      # 0x4318
movq %rax, %rdi
callq 0x2130
movq %rax, %rdi
movl $0xc00, %esi            # imm = 0xC00
callq 0x2200
leaq 0x172f(%rip), %rsi      # 0x4315
movq %rax, %rdi
callq 0x2130
leaq 0x172b(%rip), %rsi      # 0x4320
movq %rax, %rdi
callq 0x2130
leaq 0x1711(%rip), %rsi      # 0x4315
movq %rax, %rdi
callq 0x2130
movq (%r14), %rax
movzbl (%rax), %eax
cmpl $0x62, %eax
je 0x2c7e
cmpl $0x67, %eax
je 0x2c51
cmpl $0x72, %eax
jne 0x2cb0
movq (%rbx), %rbx
leaq 0x1544(%rip), %rsi      # 0x4173
leaq -0x80(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2812
leaq -0x80(%rbp), %rdx
movq %rbx, %rsi
callq 0x2daa
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
jmp 0x2ca9
movq 0x10(%rbx), %rbx
leaq 0x154b(%rip), %rsi      # 0x41a7
leaq -0x60(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2812
leaq -0x60(%rbp), %rdx
movq %rbx, %rsi
callq 0x2daa
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
jmp 0x2ca9
movq 0x8(%rbx), %rbx
leaq 0x1504(%rip), %rsi      # 0x418d
leaq -0x40(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2812
leaq -0x40(%rbp), %rdx
movq %rbx, %rsi
callq 0x2daa
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x2090
jmp 0x2cb2
xorl %ebx, %ebx
leaq -0x2a0(%rbp), %rdi
movl $0x2400000, %edx        # imm = 0x2400000
movq %rbx, %rsi
callq 0x2190
leaq -0x2a0(%rbp), %rdi
callq 0x21b0
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x280(%rbp,%rax)
jne 0x2d2a
leaq -0x2a0(%rbp), %rdi
callq 0x21c0
leaq -0xa0(%rbp), %rdi
callq 0x2090
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x32dc(%rip), %rdi      # 0x5ff0
leaq 0x15d2(%rip), %rsi      # 0x42ed
callq 0x2130
movq %rax, %rdi
callq 0x20b0
jmp 0x2d45
movq 0x32bf(%rip), %rdi      # 0x5ff0
leaq 0x15ec(%rip), %rsi      # 0x4324
callq 0x2130
movq %rax, %rdi
callq 0x20b0
pushq $0x1
popq %rdi
callq 0x2160
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
jmp 0x2d66
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
jmp 0x2d66
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x2090
jmp 0x2d89
jmp 0x2d86
jmp 0x2d86
jmp 0x2d86
movq %rax, %rbx
jmp 0x2d95
movq %rax, %rbx
leaq -0x2a0(%rbp), %rdi
jmp 0x2d9c
jmp 0x2d86
movq %rax, %rbx
leaq -0x2a0(%rbp), %rdi
callq 0x21c0
leaq -0xa0(%rbp), %rdi
callq 0x2090
movq %rbx, %rdi
callq 0x2210
nop