// File: kylegranger[P]ImageScaling/linear16.cpp
// Function: CLinear16::WriteBmp(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool CLinear16::WriteBmp(std::string filename)
{
	int size;
	BitmapFileHeader bfh;
	BitmapInfoHeader bmh;

	FILE *f = fopen(filename.c_str(),"wb");
	if (!f) return false;
	size = mWidth*mHeight*3;

	memcpy(&bfh.bfType,"BM",2);
	bfh.bfSize = size + 56;
	bfh.bfOffBits = 54;
	fwrite(&bfh,1,sizeof(bfh),f);

	memset(&bmh,0,sizeof(bmh));
	bmh.biSize = sizeof(bmh);
	bmh.biWidth = mWidth;
	bmh.biHeight = -mHeight;
	bmh.biPlanes = 1;
	bmh.biBitCount = 24;
	bmh.biXPelsPerMeter = 2834;
	bmh.biYPelsPerMeter = 2834;
	fwrite(&bmh,1,sizeof(bmh),f);
	color_t *cptr = mData;
	uint8_t color[4];
	int pad = 0;
	int rowbytes = mWidth * 3;
	int modu = rowbytes & 3;
	if (modu)
		pad = 4 - modu;

	for (int y = 0; y < mHeight; y++)
	{
		for (int x = 0; x < mWidth; x++)
		{
			color[0] = cptr->b * 255 / 65535;
			color[1] = cptr->g * 255 / 65535;
			color[2] = cptr->r * 255 / 65535;
			fwrite(color,1,3,f);
			cptr++;
		}
		if (pad)
			fwrite(color,1,pad,f);
	}
	fwrite(color,1,2,f);
	fclose(f);
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rsi, -0x90(%rbp)
movq %rdi, %rax
movq -0x90(%rbp), %rdi
movq %rax, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
callq 0xf430
movq %rax, %rdi
leaq 0x186e9(%rip), %rsi     # 0x212a7
callq 0x4140
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x8bd7
movb $0x0, -0x1(%rbp)
jmp 0x8da8
movq -0x88(%rbp), %rcx
movl (%rcx), %eax
imull 0x4(%rcx), %eax
imull $0x3, %eax, %eax
movl %eax, -0x1c(%rbp)
movw 0x186b9(%rip), %ax      # 0x212aa
movw %ax, -0x2a(%rbp)
movl -0x1c(%rbp), %eax
addl $0x38, %eax
movl %eax, -0x28(%rbp)
movl $0x36, -0x20(%rbp)
movq -0x60(%rbp), %rcx
leaq -0x2a(%rbp), %rdi
movl $0x1, %esi
movl $0xe, %edx
callq 0x42c0
leaq -0x52(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x4070
movq -0x88(%rbp), %rcx
movl $0x28, -0x52(%rbp)
movl (%rcx), %eax
movl %eax, -0x4e(%rbp)
xorl %eax, %eax
subl 0x4(%rcx), %eax
movl %eax, -0x4a(%rbp)
movw $0x1, -0x46(%rbp)
movw $0x18, -0x44(%rbp)
movl $0xb12, -0x3a(%rbp)     # imm = 0xB12
movl $0xb12, -0x36(%rbp)     # imm = 0xB12
movq -0x60(%rbp), %rcx
leaq -0x52(%rbp), %rdi
movl $0x1, %esi
movl $0x28, %edx
callq 0x42c0
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rcx
movq %rcx, -0x68(%rbp)
movl $0x0, -0x70(%rbp)
imull $0x3, (%rax), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
andl $0x3, %eax
movl %eax, -0x78(%rbp)
cmpl $0x0, -0x78(%rbp)
je 0x8cae
movl $0x4, %eax
subl -0x78(%rbp), %eax
movl %eax, -0x70(%rbp)
movl $0x0, -0x7c(%rbp)
movq -0x88(%rbp), %rcx
movl -0x7c(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x8d84
movl $0x0, -0x80(%rbp)
movq -0x88(%rbp), %rcx
movl -0x80(%rbp), %eax
cmpl (%rcx), %eax
jge 0x8d58
movq -0x68(%rbp), %rax
movzwl (%rax), %eax
imull $0xff, %eax, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
cltd
idivl %ecx
movb %al, -0x6c(%rbp)
movq -0x68(%rbp), %rax
movzwl 0x2(%rax), %eax
imull $0xff, %eax, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
cltd
idivl %ecx
movb %al, -0x6b(%rbp)
movq -0x68(%rbp), %rax
movzwl 0x4(%rax), %eax
imull $0xff, %eax, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
cltd
idivl %ecx
movb %al, -0x6a(%rbp)
leaq -0x6c(%rbp), %rdi
movq -0x60(%rbp), %rcx
movl $0x1, %esi
movl $0x3, %edx
callq 0x42c0
movq -0x68(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x68(%rbp)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x8ccf
cmpl $0x0, -0x70(%rbp)
je 0x8d74
leaq -0x6c(%rbp), %rdi
movslq -0x70(%rbp), %rdx
movq -0x60(%rbp), %rcx
movl $0x1, %esi
callq 0x42c0
jmp 0x8d76
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x8cb5
leaq -0x6c(%rbp), %rdi
movq -0x60(%rbp), %rcx
movl $0x1, %esi
movl $0x2, %edx
callq 0x42c0
movq -0x60(%rbp), %rdi
callq 0x4290
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)