// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rsi, -0x78(%rbp)
movq %rdi, %rax
movq -0x78(%rbp), %rdi
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
callq 0xf430
movq %rax, %rdi
leaq 0x16728(%rip), %rsi     # 0x2124d
callq 0x4140
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rcx
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
movl $0x20, %edx
callq 0x4220
movq %rax, -0x50(%rbp)
cmpq $0x20, -0x50(%rbp)
jne 0xab52
jmp 0xab71
leaq 0x1689a(%rip), %rdi     # 0x213f3
leaq 0x166aa(%rip), %rsi     # 0x2120a
movl $0x2f7, %edx            # imm = 0x2F7
leaq 0x168a4(%rip), %rcx     # 0x21410
callq 0x40f0
movl -0x1c(%rbp), %eax
addl $0xfffff3c5, %eax       # imm = 0xFFFFF3C5
movl %eax, %ecx
movq %rcx, -0x80(%rbp)
subl $0x5, %eax
ja 0xabe1
movq -0x80(%rbp), %rax
leaq 0x1665d(%rip), %rcx     # 0x211ec
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x20(%rbp), %eax
shll $0x2, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x14(%rbp)
jmp 0xac00
imull $0x3, -0x20(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x14(%rbp)
jmp 0xac00
movl -0x20(%rbp), %eax
shll $0x0, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x14(%rbp)
jmp 0xac00
movl -0x20(%rbp), %eax
shll %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x14(%rbp)
jmp 0xac00
leaq 0x167cf(%rip), %rdi     # 0x213b7
leaq 0x1661b(%rip), %rsi     # 0x2120a
movl $0x30a, %edx            # imm = 0x30A
leaq 0x16815(%rip), %rcx     # 0x21410
callq 0x40f0
cmpl $0x504d4953, -0x38(%rbp) # imm = 0x504D4953
jne 0xac0b
jmp 0xac2a
leaq 0x16828(%rip), %rdi     # 0x2143a
leaq 0x165f1(%rip), %rsi     # 0x2120a
movl $0x30f, %edx            # imm = 0x30F
leaq 0x167eb(%rip), %rcx     # 0x21410
callq 0x40f0
cmpl $0x4543494c, -0x34(%rbp) # imm = 0x4543494C
jne 0xac35
jmp 0xac54
leaq 0x16817(%rip), %rdi     # 0x21453
leaq 0x165c7(%rip), %rsi     # 0x2120a
movl $0x310, %edx            # imm = 0x310
leaq 0x167c1(%rip), %rcx     # 0x21410
callq 0x40f0
movq -0x70(%rbp), %rax
movl (%rax), %eax
imull -0x14(%rbp), %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%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 0xacdd
movq -0x58(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
movq -0x48(%rbp), %rcx
movl $0x1, %esi
callq 0x4220
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x1c(%rbp), %ecx
callq 0xa5b0
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 0xac84
movq -0x48(%rbp), %rdi
callq 0x4290
movq -0x58(%rbp), %rdi
callq 0x41b0
movb $0x1, %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)