// File: /workspace/github_cpp_makefile_star2_O1/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 0x16baf(%rip), %rsi     # 0x1f228
callq 0x4150
movq %rax, %r14
leaq 0x8(%rsp), %rdi
movl $0x1, %esi
movl $0x20, %edx
movq %rax, %rcx
callq 0x4230
cmpq $0x20, %rax
jne 0x87a4
movl 0x24(%rsp), %r12d
leal -0xc3b(%r12), %eax
cmpl $0x5, %eax
ja 0x8801
leaq 0x16b15(%rip), %rcx     # 0x1f1d4
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 0x870b
movl 0x20(%rsp), %eax
leal (%rax,%rax,2), %ecx
leal (%rax,%rax,2), %eax
addl $0x7, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
jmp 0x8708
movl 0x20(%rsp), %ecx
leal 0x3(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x2, %eax
jmp 0x870b
movl 0x20(%rsp), %ecx
leal 0x7(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x3, %eax
cmpl $0x504d4953, 0x8(%rsp)  # imm = 0x504D4953
jne 0x87c3
cmpl $0x4543494c, 0xc(%rsp)  # imm = 0x4543494C
jne 0x87e2
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 0x8783
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 0x83b0
movslq (%rbx), %rax
leaq (%rbp,%rax,8), %rbp
incl %r14d
cmpl 0x4(%rbx), %r14d
jl 0x874d
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 0x16c20(%rip), %rdi     # 0x1f3cb
leaq 0x16a52(%rip), %rsi     # 0x1f204
leaq 0x16c2f(%rip), %rcx     # 0x1f3e8
movl $0x2f7, %edx            # imm = 0x2F7
callq 0x4100
leaq 0x16c48(%rip), %rdi     # 0x1f412
leaq 0x16a33(%rip), %rsi     # 0x1f204
leaq 0x16c10(%rip), %rcx     # 0x1f3e8
movl $0x30f, %edx            # imm = 0x30F
callq 0x4100
leaq 0x16c42(%rip), %rdi     # 0x1f42b
leaq 0x16a14(%rip), %rsi     # 0x1f204
leaq 0x16bf1(%rip), %rcx     # 0x1f3e8
movl $0x310, %edx            # imm = 0x310
callq 0x4100
leaq 0x16b87(%rip), %rdi     # 0x1f38f
leaq 0x169f5(%rip), %rsi     # 0x1f204
leaq 0x16bd2(%rip), %rcx     # 0x1f3e8
movl $0x30a, %edx            # imm = 0x30A
callq 0x4100