// File: kylegranger[P]ImageScaling/linear16.cpp
// Function: CLinear16::ReadBmp(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool CLinear16::ReadBmp(std::string filename)
{
	size_t res;
    printf("CLinear16::ReadBmp %s\n", filename.c_str());

	FILE *f = fopen(filename.c_str(),"rb");
	if (!f) 
	{
		return false;
	}

	BitmapFileHeader bfh;
	res = fread(&bfh,1,sizeof(bfh),f);
    assert(res == sizeof(bfh));

	BitmapInfoHeader bmh;
	res = fread(&bmh,1,sizeof(bmh),f);
    assert(res == sizeof(bmh));
    fseek (f, bfh.bfOffBits, SEEK_SET);


	bool isFlipped = (bmh.biHeight > 0);
	mWidth = bmh.biWidth;
	mHeight = isFlipped ? bmh.biHeight : -bmh.biHeight ;
	
	auto numPixels = mWidth * mHeight;
	auto size = numPixels * sizeof(color_t);
	if (size > mDataSize)
	{
		if (mData) delete [] mData;
		mData = new color_t[numPixels];
		assert(mData);
		mDataSize = size;
	}

    size_t scanlineSize = 0xffffffc & (mWidth * 3 + 3);
    size_t padding = scanlineSize - mWidth * 3;
	uint8_t *buffer = new uint8_t[ scanlineSize * mHeight];
    size = scanlineSize * mHeight;
	res = fread(buffer,1, size,f);
    assert(res == size);

	uint8_t *cptr = buffer;
	for (int y = 0; y < mHeight; y++)
	{
		color_t *c = isFlipped ? mData + (mHeight - y - 1) * mWidth : mData + y * mWidth;
		for (int x = 0; x < mWidth; x++)
		{
			c->b = cptr[0] * 65535 / 255;
			c->g = cptr[1] * 65535 / 255;
			c->r = cptr[2] * 65535 / 255;
			cptr += 3;
			c++;
		}
        cptr += padding;
	}

	delete [] buffer;
	fclose(f);
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rsi, -0xc0(%rbp)
movq %rdi, %rax
movq -0xc0(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
movq %rax, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb8(%rbp)
callq 0xf430
movq %rax, %rsi
leaq 0x18eb1(%rip), %rdi     # 0x21236
movb $0x0, %al
callq 0x4050
movq -0xb0(%rbp), %rdi
callq 0xf430
movq %rax, %rdi
leaq 0x18eab(%rip), %rsi     # 0x2124d
callq 0x4140
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x83bb
movb $0x0, -0x1(%rbp)
jmp 0x87aa
movq -0x28(%rbp), %rcx
leaq -0x36(%rbp), %rdi
movl $0x1, %esi
movl $0xe, %edx
callq 0x4220
movq %rax, -0x20(%rbp)
cmpq $0xe, -0x20(%rbp)
jne 0x83df
jmp 0x83fe
leaq 0x18e6a(%rip), %rdi     # 0x21250
leaq 0x18e1d(%rip), %rsi     # 0x2120a
movl $0x3f, %edx
leaq 0x18e6a(%rip), %rcx     # 0x21263
callq 0x40f0
movq -0x28(%rbp), %rcx
leaq -0x5e(%rbp), %rdi
movl $0x1, %esi
movl $0x28, %edx
callq 0x4220
movq %rax, -0x20(%rbp)
cmpq $0x28, -0x20(%rbp)
jne 0x8422
jmp 0x8441
leaq 0x18e5f(%rip), %rdi     # 0x21288
leaq 0x18dda(%rip), %rsi     # 0x2120a
movl $0x43, %edx
leaq 0x18e27(%rip), %rcx     # 0x21263
callq 0x40f0
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x40e0
movq -0xb8(%rbp), %rax
cmpl $0x0, -0x56(%rbp)
setg %cl
andb $0x1, %cl
movb %cl, -0x5f(%rbp)
movl -0x5a(%rbp), %ecx
movl %ecx, (%rax)
testb $0x1, -0x5f(%rbp)
je 0x847b
movl -0x56(%rbp), %eax
movl %eax, -0xc4(%rbp)
jmp 0x8486
xorl %eax, %eax
subl -0x56(%rbp), %eax
movl %eax, -0xc4(%rbp)
movq -0xb8(%rbp), %rcx
movl -0xc4(%rbp), %eax
movl %eax, 0x4(%rcx)
movl (%rcx), %eax
imull 0x4(%rcx), %eax
movl %eax, -0x64(%rbp)
movslq -0x64(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq 0x8(%rcx), %rax
jbe 0x8557
movq -0xb8(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x84ed
movq -0xb8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, %rax
je 0x84eb
movq -0xd0(%rbp), %rdi
callq 0xbcb0
jmp 0x84ed
movslq -0x64(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xcb90
movq %rax, %rcx
movq -0xb8(%rbp), %rax
movq %rcx, 0x18(%rax)
cmpq $0x0, 0x18(%rax)
je 0x8529
jmp 0x8548
leaq 0x18cd4(%rip), %rdi     # 0x21204
leaq 0x18cd3(%rip), %rsi     # 0x2120a
movl $0x51, %edx
leaq 0x18d20(%rip), %rcx     # 0x21263
callq 0x40f0
movq -0xb8(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0xb8(%rbp), %rax
imull $0x3, (%rax), %ecx
addl $0x3, %ecx
andl $0xffffffc, %ecx        # imm = 0xFFFFFFC
movslq %ecx, %rcx
movq %rcx, -0x78(%rbp)
movq -0x78(%rbp), %rcx
imull $0x3, (%rax), %edx
movslq %edx, %rdx
subq %rdx, %rcx
movq %rcx, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movslq 0x4(%rax), %rax
imulq %rax, %rdi
callq 0xcb90
movq -0xb8(%rbp), %rcx
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rax
movslq 0x4(%rcx), %rcx
imulq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x88(%rbp), %rdi
movq -0x70(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x4220
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x70(%rbp), %rax
jne 0x85da
jmp 0x85f9
leaq 0x18cba(%rip), %rdi     # 0x2129b
leaq 0x18c22(%rip), %rsi     # 0x2120a
movl $0x5a, %edx
leaq 0x18c6f(%rip), %rcx     # 0x21263
callq 0x40f0
movq -0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movq -0xb8(%rbp), %rcx
movl -0x94(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x877d
testb $0x1, -0x5f(%rbp)
je 0x865a
movq -0xb8(%rbp), %rdx
movq 0x18(%rdx), %rax
movl 0x4(%rdx), %ecx
subl -0x94(%rbp), %ecx
subl $0x1, %ecx
imull (%rdx), %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
jmp 0x867f
movq -0xb8(%rbp), %rdx
movq 0x18(%rdx), %rax
movl -0x94(%rbp), %ecx
imull (%rdx), %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
movq -0xb8(%rbp), %rcx
movl -0xa4(%rbp), %eax
cmpl (%rcx), %eax
jge 0x8757
movq -0x90(%rbp), %rax
movzbl (%rax), %eax
imull $0xffff, %eax, %eax     # imm = 0xFFFF
movl $0xff, %ecx
cltd
idivl %ecx
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, (%rax)
movq -0x90(%rbp), %rax
movzbl 0x1(%rax), %eax
imull $0xffff, %eax, %eax     # imm = 0xFFFF
movl $0xff, %ecx
cltd
idivl %ecx
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x90(%rbp), %rax
movzbl 0x2(%rax), %eax
imull $0xffff, %eax, %eax     # imm = 0xFFFF
movl $0xff, %ecx
cltd
idivl %ecx
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x90(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x90(%rbp)
movq -0xa0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xa0(%rbp)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x8697
movq -0x80(%rbp), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x8611
movq -0x88(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, %rax
je 0x879d
movq -0xe0(%rbp), %rdi
callq 0xbcb0
movq -0x28(%rbp), %rdi
callq 0x4290
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax,%rax)