// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %rbx
movq (%rsi), %rdi
leaq 0x168e7(%rip), %rsi     # 0x1e2b4
callq 0x4150
leaq 0x18(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x20
popq %rdx
movq %rax, 0x8(%rsp)
movq %rax, %rcx
callq 0x4230
cmpq $0x20, %rax
jne 0x7ad4
movl 0x34(%rsp), %eax
movq %rax, 0x10(%rsp)
addl $0xfffff3c5, %eax       # imm = 0xFFFFF3C5
cmpl $0x5, %eax
ja 0x7b31
leaq 0x1684e(%rip), %rcx     # 0x1e260
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x30(%rsp), %eax
pushq $0x2
jmp 0x7a38
imull $0x3, 0x30(%rsp), %eax
jmp 0x7a36
movl 0x30(%rsp), %eax
pushq $0x4
jmp 0x7a38
movl 0x30(%rsp), %eax
pushq $0x8
popq %rcx
cltd
idivl %ecx
cmpl $0x504d4953, 0x18(%rsp) # imm = 0x504D4953
jne 0x7af3
cmpl $0x4543494c, 0x1c(%rsp) # imm = 0x4543494C
jne 0x7b12
movslq (%rbx), %rcx
movslq %eax, %r12
imulq %rcx, %r12
movq %r12, %rdi
callq 0x4140
movq %rax, %r13
movq 0x18(%rbx), %rbp
xorl %r15d, %r15d
pushq $0x1
popq %r14
cmpl 0x4(%rbx), %r15d
jge 0x7ab1
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
callq 0x4230
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
callq 0x7716
movslq (%rbx), %rax
leaq (%rbp,%rax,8), %rbp
incl %r15d
jmp 0x7a78
movq 0x8(%rsp), %rdi
callq 0x42a0
movq %r13, %rdi
callq 0x41c0
movb $0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1697c(%rip), %rdi     # 0x1e457
leaq 0x167ae(%rip), %rsi     # 0x1e290
leaq 0x1698b(%rip), %rcx     # 0x1e474
movl $0x2f7, %edx            # imm = 0x2F7
callq 0x4100
leaq 0x169a4(%rip), %rdi     # 0x1e49e
leaq 0x1678f(%rip), %rsi     # 0x1e290
leaq 0x1696c(%rip), %rcx     # 0x1e474
movl $0x30f, %edx            # imm = 0x30F
callq 0x4100
leaq 0x1699e(%rip), %rdi     # 0x1e4b7
leaq 0x16770(%rip), %rsi     # 0x1e290
leaq 0x1694d(%rip), %rcx     # 0x1e474
movl $0x310, %edx            # imm = 0x310
callq 0x4100
leaq 0x168e3(%rip), %rdi     # 0x1e41b
leaq 0x16751(%rip), %rsi     # 0x1e290
leaq 0x1692e(%rip), %rcx     # 0x1e474
movl $0x30a, %edx            # imm = 0x30A
callq 0x4100