// File: 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
subq $0x3c0, %rsp            # imm = 0x3C0
movq %rdx, %rax
movq %rsi, %rdx
movq %rdx, -0x300(%rbp)
movq %rdi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2f8(%rbp)
leaq 0x2eca(%rip), %rsi      # 0x62a0
leaq -0x58(%rbp), %rdi
movq %rdi, -0x2f0(%rbp)
callq 0x47c0
movq -0x2f0(%rbp), %rsi
leaq 0x2eb4(%rip), %rdx      # 0x62a8
leaq -0x38(%rbp), %rdi
callq 0x4770
jmp 0x33ff
leaq -0x58(%rbp), %rdi
callq 0x2150
leaq -0x268(%rbp), %rdi
callq 0x2300
jmp 0x3416
movl $0x10, %edi
movl $0x20, %esi
callq 0x48d0
movl %eax, -0x304(%rbp)
jmp 0x342d
movl -0x304(%rbp), %edx
leaq -0x268(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x2100
jmp 0x3445
movq -0x268(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x268(%rbp,%rax), %rdi
callq 0x20f0
movb %al, -0x305(%rbp)
jmp 0x3465
movb -0x305(%rbp), %al
testb $0x1, %al
jne 0x3471
jmp 0x34e8
movq 0x5b78(%rip), %rdi      # 0x8ff0
leaq 0x2e2e(%rip), %rsi      # 0x62ad
callq 0x21e0
movq %rax, -0x310(%rbp)
jmp 0x348d
movq -0x310(%rbp), %rdi
movq 0x5b25(%rip), %rsi      # 0x8fc0
callq 0x2200
jmp 0x34a2
movl $0x1, %edi
callq 0x2260
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x2150
jmp 0x395d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x3954
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x3948
leaq 0x2de4(%rip), %rsi      # 0x62d3
leaq -0x268(%rbp), %rdi
callq 0x21e0
movq %rax, -0x318(%rbp)
jmp 0x3504
movq -0x318(%rbp), %rdi
leaq 0x2dc5(%rip), %rsi      # 0x62d7
callq 0x21e0
movq %rax, -0x320(%rbp)
jmp 0x3520
movq -0x300(%rbp), %rsi
movq -0x320(%rbp), %rdi
callq 0x21d0
movq %rax, -0x328(%rbp)
jmp 0x353c
movq -0x328(%rbp), %rdi
leaq 0x2d90(%rip), %rsi      # 0x62da
callq 0x21e0
movq %rax, -0x330(%rbp)
jmp 0x3558
movq -0x330(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x2320
movq %rax, -0x338(%rbp)
jmp 0x3572
movq -0x338(%rbp), %rdi
leaq 0x2d58(%rip), %rsi      # 0x62d8
callq 0x21e0
movq %rax, -0x340(%rbp)
jmp 0x358e
movq -0x340(%rbp), %rdi
movl $0xc00, %esi            # imm = 0xC00
callq 0x2320
movq %rax, -0x348(%rbp)
jmp 0x35a8
movq -0x348(%rbp), %rdi
leaq 0x2d1f(%rip), %rsi      # 0x62d5
callq 0x21e0
movq %rax, -0x350(%rbp)
jmp 0x35c4
movq -0x350(%rbp), %rdi
leaq 0x2d0e(%rip), %rsi      # 0x62e0
callq 0x21e0
movq %rax, -0x358(%rbp)
jmp 0x35e0
movq -0x358(%rbp), %rdi
leaq 0x2ce7(%rip), %rsi      # 0x62d5
callq 0x21e0
jmp 0x35f5
movq -0x300(%rbp), %rdi
movq $0x0, -0x270(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x2390
movq %rax, -0x360(%rbp)
jmp 0x3619
movq -0x360(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x364(%rbp)
subl $0x62, %eax
je 0x37db
jmp 0x3634
movl -0x364(%rbp), %eax
subl $0x67, %eax
je 0x3718
jmp 0x3645
movl -0x364(%rbp), %eax
subl $0x72, %eax
jne 0x389b
jmp 0x3656
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x378(%rbp)
leaq -0x291(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0x2340
movq -0x370(%rbp), %rdx
leaq 0x2aee(%rip), %rsi      # 0x6173
leaq -0x290(%rbp), %rdi
callq 0x2d20
jmp 0x3693
movq -0x378(%rbp), %rsi
movq -0x2f8(%rbp), %rdi
leaq -0x290(%rbp), %rdx
callq 0x3970
movq %rax, -0x380(%rbp)
jmp 0x36b6
movq -0x380(%rbp), %rax
movq %rax, -0x270(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x2150
leaq -0x291(%rbp), %rdi
callq 0x2220
jmp 0x389b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x3707
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x2150
leaq -0x291(%rbp), %rdi
callq 0x2220
jmp 0x3948
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x390(%rbp)
leaq -0x2b9(%rbp), %rdi
movq %rdi, -0x388(%rbp)
callq 0x2340
movq -0x388(%rbp), %rdx
leaq 0x2a5f(%rip), %rsi      # 0x61a7
leaq -0x2b8(%rbp), %rdi
callq 0x2d20
jmp 0x3756
movq -0x390(%rbp), %rsi
movq -0x2f8(%rbp), %rdi
leaq -0x2b8(%rbp), %rdx
callq 0x3970
movq %rax, -0x398(%rbp)
jmp 0x3779
movq -0x398(%rbp), %rax
movq %rax, -0x270(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x2150
leaq -0x2b9(%rbp), %rdi
callq 0x2220
jmp 0x389b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x37ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x2150
leaq -0x2b9(%rbp), %rdi
callq 0x2220
jmp 0x3948
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x3a8(%rbp)
leaq -0x2e1(%rbp), %rdi
movq %rdi, -0x3a0(%rbp)
callq 0x2340
movq -0x3a0(%rbp), %rdx
leaq 0x2982(%rip), %rsi      # 0x618d
leaq -0x2e0(%rbp), %rdi
callq 0x2d20
jmp 0x3819
movq -0x3a8(%rbp), %rsi
movq -0x2f8(%rbp), %rdi
leaq -0x2e0(%rbp), %rdx
callq 0x3970
movq %rax, -0x3b0(%rbp)
jmp 0x383c
movq -0x3b0(%rbp), %rax
movq %rax, -0x270(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x2150
leaq -0x2e1(%rbp), %rdi
callq 0x2220
jmp 0x389b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x388a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x2150
leaq -0x2e1(%rbp), %rdi
callq 0x2220
jmp 0x3948
movq -0x270(%rbp), %rsi
leaq -0x268(%rbp), %rdi
movl $0x2400000, %edx        # imm = 0x2400000
callq 0x2290
jmp 0x38b5
leaq -0x268(%rbp), %rdi
callq 0x22a0
jmp 0x38c3
movq -0x268(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x268(%rbp,%rax), %rdi
callq 0x20f0
movb %al, -0x3b1(%rbp)
jmp 0x38e3
movb -0x3b1(%rbp), %al
testb $0x1, %al
jne 0x38ef
jmp 0x392a
movq 0x56fa(%rip), %rdi      # 0x8ff0
leaq 0x29e7(%rip), %rsi      # 0x62e4
callq 0x21e0
movq %rax, -0x3c0(%rbp)
jmp 0x390b
movq -0x3c0(%rbp), %rdi
movq 0x56a7(%rip), %rsi      # 0x8fc0
callq 0x2200
jmp 0x3920
movl $0x1, %edi
callq 0x2260
leaq -0x268(%rbp), %rdi
callq 0x22c0
leaq -0x38(%rbp), %rdi
callq 0x2150
addq $0x3c0, %rsp            # imm = 0x3C0
popq %rbp
retq
leaq -0x268(%rbp), %rdi
callq 0x22c0
leaq -0x38(%rbp), %rdi
callq 0x2150
movq -0x60(%rbp), %rdi
callq 0x2330
nopw %cs:(%rax,%rax)