// File: 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
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x1e(%rbp), %rdi
callq 0x48f0
leaq -0x46(%rbp), %rdi
callq 0x4910
movl $0x2400036, -0x1c(%rbp) # imm = 0x2400036
movl $0x36, -0x14(%rbp)
leaq -0x248(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x2300
movq -0x260(%rbp), %rdi
leaq 0x283b(%rip), %rsi      # 0x630a
movl $0x4, %edx
callq 0x2040
jmp 0x3adb
movq -0x248(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x248(%rbp,%rax), %rdi
callq 0x20f0
movb %al, -0x269(%rbp)
jmp 0x3afb
movb -0x269(%rbp), %al
testb $0x1, %al
jne 0x3b07
jmp 0x3b65
movq 0x54e2(%rip), %rdi      # 0x8ff0
leaq 0x2798(%rip), %rsi      # 0x62ad
callq 0x21e0
movq %rax, -0x278(%rbp)
jmp 0x3b23
movq -0x278(%rbp), %rdi
movq 0x548f(%rip), %rsi      # 0x8fc0
callq 0x2200
jmp 0x3b38
movl $0x1, %edi
callq 0x2260
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x22c0
jmp 0x3c68
leaq -0x248(%rbp), %rdi
leaq -0x1e(%rbp), %rsi
movl $0xe, %edx
callq 0x2290
jmp 0x3b7c
leaq -0x248(%rbp), %rdi
leaq -0x46(%rbp), %rsi
movl $0x28, %edx
callq 0x2290
jmp 0x3b93
movq -0x268(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x10(%rax), %rcx
callq 0x3c80
movq %rax, -0x280(%rbp)
jmp 0x3bb7
movq -0x268(%rbp), %rax
movq -0x280(%rbp), %rcx
movq %rcx, (%rax)
movq (%rax), %rsi
leaq -0x248(%rbp), %rdi
movl $0x2400000, %edx        # imm = 0x2400000
callq 0x2290
jmp 0x3bde
leaq -0x248(%rbp), %rdi
callq 0x22a0
jmp 0x3bec
movq -0x248(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x248(%rbp,%rax), %rdi
callq 0x20f0
movb %al, -0x281(%rbp)
jmp 0x3c0c
movb -0x281(%rbp), %al
testb $0x1, %al
jne 0x3c18
jmp 0x3c53
movq 0x53d1(%rip), %rdi      # 0x8ff0
leaq 0x26be(%rip), %rsi      # 0x62e4
callq 0x21e0
movq %rax, -0x290(%rbp)
jmp 0x3c34
movq -0x290(%rbp), %rdi
movq 0x537e(%rip), %rsi      # 0x8fc0
callq 0x2200
jmp 0x3c49
movl $0x1, %edi
callq 0x2260
leaq -0x248(%rbp), %rdi
callq 0x22c0
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
movq -0x250(%rbp), %rdi
callq 0x2330
nopw %cs:(%rax,%rax)