// File: kylegranger[P]ImageScaling/linear16.cpp
// Function: CLinear16::WriteSimple16(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, ESimpleFormat)
bool CLinear16::WriteSimple16(std::string filename, ESimpleFormat format)
{
	int bytesPerPixel;
	TSimpleHeader header;
	int bytesPerLine;
	;

	FILE *f = fopen(filename.c_str(), "wb");
	if (f == NULL) 
		return false;
	switch (format)
	{
	case ESimpleFormatRgba:
	case ESimpleFormatBgra:
		bytesPerPixel = 8;
		break;
	case ESimpleFormatRgb:
	case ESimpleFormatBgr:
		bytesPerPixel = 6;
		break;
	case ESimpleFormatL:
		bytesPerPixel = 2;
		break;
	case ESimpleFormatLa:
		bytesPerPixel = 4;
		break;
	default:
		assert(0);
	return false;
	}

	header.cookieA = 'PMIS';
	header.cookieB = 'ECIL';
	header.datasize = bytesPerPixel * mWidth * mHeight;
	header.width = mWidth;
	header.height = mHeight;
	header.bits = 16;
	header.format = format;
	fwrite(&header,1,sizeof(TSimpleHeader),f);
	bytesPerLine = mWidth * bytesPerPixel;
	uint16_t *dst = (uint16_t *) malloc(bytesPerLine);
	color_t *src = mData;
	for (int i = 0; i < mHeight; i++)
	{
		WriteSimpleScanline(src, dst, format);
		fwrite(dst,1,bytesPerLine,f);
		src += mWidth;
	}
	fclose(f);
	free(dst);
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rsi, -0x78(%rbp)
movq %rdi, %rax
movq -0x78(%rbp), %rdi
movq %rax, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
callq 0xf430
movq %rax, %rdi
leaq 0x1693f(%rip), %rsi     # 0x212a7
callq 0x4140
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xa981
movb $0x0, -0x1(%rbp)
jmp 0xaadc
movl -0x1c(%rbp), %eax
addl $0xfffff3c5, %eax       # imm = 0xFFFFF3C5
movl %eax, %ecx
movq %rcx, -0x80(%rbp)
subl $0x5, %eax
ja 0xa9cc
movq -0x80(%rbp), %rax
leaq 0x16835(%rip), %rcx     # 0x211d4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x8, -0x20(%rbp)
jmp 0xa9eb
movl $0x6, -0x20(%rbp)
jmp 0xa9eb
movl $0x2, -0x20(%rbp)
jmp 0xa9eb
movl $0x4, -0x20(%rbp)
jmp 0xa9eb
leaq 0x169e4(%rip), %rdi     # 0x213b7
leaq 0x16830(%rip), %rsi     # 0x2120a
movl $0x2d5, %edx            # imm = 0x2D5
leaq 0x169d3(%rip), %rcx     # 0x213b9
callq 0x40f0
movq -0x70(%rbp), %rax
movl $0x504d4953, -0x40(%rbp) # imm = 0x504D4953
movl $0x4543494c, -0x3c(%rbp) # imm = 0x4543494C
movl -0x20(%rbp), %ecx
imull (%rax), %ecx
imull 0x4(%rax), %ecx
movslq %ecx, %rcx
movq %rcx, -0x38(%rbp)
movl (%rax), %ecx
movl %ecx, -0x30(%rbp)
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x10, -0x28(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x50(%rbp), %rcx
leaq -0x40(%rbp), %rdi
movl $0x1, %esi
movl $0x20, %edx
callq 0x42c0
movq -0x70(%rbp), %rax
movl (%rax), %eax
imull -0x20(%rbp), %eax
movl %eax, -0x44(%rbp)
movslq -0x44(%rbp), %rdi
callq 0x4130
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x58(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movq -0x70(%rbp), %rcx
movl -0x64(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0xaac6
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x1c(%rbp), %ecx
callq 0xa230
movq -0x58(%rbp), %rdi
movslq -0x44(%rbp), %rdx
movq -0x50(%rbp), %rcx
movl $0x1, %esi
callq 0x42c0
movq -0x70(%rbp), %rax
movl (%rax), %ecx
movq -0x60(%rbp), %rax
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xaa6d
movq -0x50(%rbp), %rdi
callq 0x4290
movq -0x58(%rbp), %rdi
callq 0x41b0
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)