// File: /workspace/github_cpp_makefile_star2_O3/kylegranger[P]ImageScaling/linear16.cpp
// Function: CLinear16::ReadSimple16(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool CLinear16::ReadSimple16(std::string filename)
{
	int bytesPerPixel;
	TSimpleHeader header;
	int bytesPerLine;
	FILE *f = fopen(filename.c_str(), "rb");

	size_t res = fread(&header,1,sizeof(TSimpleHeader),f);
    assert(res == sizeof(TSimpleHeader));

	switch (header.format)
	{
	case ESimpleFormatRgba:
	case ESimpleFormatBgra:
		bytesPerPixel = 4 * header.bits / 8;
		break;
	case ESimpleFormatRgb:
	case ESimpleFormatBgr:
		bytesPerPixel = 3 * header.bits / 8;
		break;
	case ESimpleFormatL:
		bytesPerPixel = 1 * header.bits / 8;
		break;
	case ESimpleFormatLa:
		bytesPerPixel = 2 * header.bits / 8;
		break;
	default:
		assert(0);
		return false;
	}


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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq (%rsi), %rdi
leaq 0x1654d(%rip), %rsi     # 0x1e2e4
callq 0x4150
movq %rax, %r14
leaq 0x8(%rsp), %rdi
movl $0x1, %esi
movl $0x20, %edx
movq %rax, %rcx
callq 0x4230
cmpq $0x20, %rax
jne 0x7ec2
movl 0x24(%rsp), %r12d
leal -0xc3b(%r12), %eax
cmpl $0x5, %eax
ja 0x7f1f
leaq 0x164b3(%rip), %rcx     # 0x1e290
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x20(%rsp), %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
jmp 0x7e29
movl 0x20(%rsp), %eax
leal (%rax,%rax,2), %ecx
leal (%rax,%rax,2), %eax
addl $0x7, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
jmp 0x7e26
movl 0x20(%rsp), %ecx
leal 0x3(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x2, %eax
jmp 0x7e29
movl 0x20(%rsp), %ecx
leal 0x7(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x3, %eax
cmpl $0x504d4953, 0x8(%rsp)  # imm = 0x504D4953
jne 0x7ee1
cmpl $0x4543494c, 0xc(%rsp)  # imm = 0x4543494C
jne 0x7f00
movslq (%rbx), %rcx
movslq %eax, %rdi
imulq %rcx, %rdi
movq %rdi, (%rsp)
callq 0x4140
movq %rax, %r13
cmpl $0x0, 0x4(%rbx)
movq %r14, %r15
jle 0x7ea1
movq 0x18(%rbx), %rbp
xorl %r14d, %r14d
movl $0x1, %esi
movq %r13, %rdi
movq (%rsp), %rdx
movq %r15, %rcx
callq 0x4230
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movl %r12d, %ecx
callq 0x7ad6
movslq (%rbx), %rax
leaq (%rbp,%rax,8), %rbp
incl %r14d
cmpl 0x4(%rbx), %r14d
jl 0x7e6b
movq %r15, %rdi
callq 0x42a0
movq %r13, %rdi
callq 0x41c0
movb $0x1, %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x165be(%rip), %rdi     # 0x1e487
leaq 0x163f0(%rip), %rsi     # 0x1e2c0
leaq 0x165cd(%rip), %rcx     # 0x1e4a4
movl $0x2f7, %edx            # imm = 0x2F7
callq 0x4100
leaq 0x165e6(%rip), %rdi     # 0x1e4ce
leaq 0x163d1(%rip), %rsi     # 0x1e2c0
leaq 0x165ae(%rip), %rcx     # 0x1e4a4
movl $0x30f, %edx            # imm = 0x30F
callq 0x4100
leaq 0x165e0(%rip), %rdi     # 0x1e4e7
leaq 0x163b2(%rip), %rsi     # 0x1e2c0
leaq 0x1658f(%rip), %rcx     # 0x1e4a4
movl $0x310, %edx            # imm = 0x310
callq 0x4100
leaq 0x16525(%rip), %rdi     # 0x1e44b
leaq 0x16393(%rip), %rsi     # 0x1e2c0
leaq 0x16570(%rip), %rcx     # 0x1e4a4
movl $0x30a, %edx            # imm = 0x30A
callq 0x4100