// File: /workspace/github_cpp_makefile_star2_O1/ksraj123[P]RAW12-to-bitmap-AVI/src/OutputWriter.cpp
// Function: OutputWriter::WriteBmp(ImageProcessor*)
void OutputWriter::WriteBmp(ImageProcessor* debayeredImg)
{
    BitmapFileHeader flleHeader;
    BitmapInfoHeader infoHeader;

    flleHeader.fileSize   = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) 
                            + TOTAL_PIX * 3;
    flleHeader.dataOffset = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader);

    std::ofstream outputBMP;
    outputBMP.open("result/BMP_output.bmp", std::ios::binary);
    if(!outputBMP)
    {
        std::cerr << "Error: File cannot be opened\n\tExiting" << std::endl;
        exit(1);
    }

    outputBMP.write(reinterpret_cast<const char*>(&flleHeader), sizeof(flleHeader));
    outputBMP.write(reinterpret_cast<const char*>(&infoHeader), sizeof(infoHeader));

    bmpImage = ConvertToBMP(debayeredImg->redChannel, debayeredImg->blueChannel, 
                            debayeredImg->greenChannel);
    outputBMP.write(reinterpret_cast<const char*>(bmpImage), 3 * TOTAL_PIX);
    
    outputBMP.close();
    if (!outputBMP)
    {
        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 $0x238, %rsp            # imm = 0x238
movq %rsi, %r14
movq %rdi, %rbx
movw $0x4d42, -0x26(%rbp)    # imm = 0x4D42
movq $0x2400036, -0x24(%rbp) # imm = 0x2400036
movabsq $0x100000000028, %rax   # imm = 0x100000000028
movq %rax, -0x50(%rbp)
movabsq $0x18000100000c00, %rax # imm = 0x18000100000C00
movq %rax, -0x48(%rbp)
movaps 0x1e23(%rip), %xmm0     # 0x52a0
movups %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x36, -0x1c(%rbp)
leaq -0x250(%rbp), %r15
movq %r15, %rdi
callq 0x2220
leaq 0x1ea4(%rip), %rsi      # 0x534a
movq %r15, %rdi
movl $0x4, %edx
callq 0x2050
movq -0x250(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x230(%rbp,%rax)
jne 0x3582
leaq -0x250(%rbp), %rdi
leaq -0x26(%rbp), %rsi
movl $0xe, %edx
callq 0x21d0
leaq -0x250(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl $0x28, %edx
callq 0x21d0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rcx
callq 0x35ee
movq %rax, (%rbx)
leaq -0x250(%rbp), %rdi
movl $0x2400000, %edx        # imm = 0x2400000
movq %rax, %rsi
callq 0x21d0
leaq -0x250(%rbp), %rdi
callq 0x21e0
movq -0x250(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x230(%rbp,%rax)
jne 0x35a8
movq 0x3a83(%rip), %rax      # 0x6fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x248(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x250(%rbp,%rcx)
callq 0x2130
leaq -0x158(%rbp), %rdi
callq 0x20a0
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x3a67(%rip), %rdi      # 0x6ff0
leaq 0x1d5d(%rip), %rsi      # 0x52ed
movl $0x25, %edx
callq 0x21a0
movq 0x3a4f(%rip), %rdi      # 0x6ff0
callq 0x20d0
jmp 0x35cc
movq 0x3a41(%rip), %rdi      # 0x6ff0
leaq 0x1d6e(%rip), %rsi      # 0x5324
movl $0x25, %edx
callq 0x21a0
movq 0x3a29(%rip), %rdi      # 0x6ff0
callq 0x20d0
movl $0x1, %edi
callq 0x21b0
movq %rax, %rbx
leaq -0x250(%rbp), %rdi
callq 0x21f0
movq %rbx, %rdi
callq 0x2240
nop