// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq (%rsi), %rsi
leaq 0x17cd3(%rip), %rdi     # 0x1e2cd
xorl %eax, %eax
callq 0x4050
movq (%rbx), %rdi
leaq 0x17cd9(%rip), %rsi     # 0x1e2e4
callq 0x4150
movq %rax, %rbx
testq %rax, %rax
je 0x67bc
leaq 0x12(%rsp), %rdi
movl $0x1, %esi
movl $0xe, %edx
movq %rbx, %rcx
callq 0x4230
cmpq $0xe, %rax
jne 0x67d1
leaq 0x20(%rsp), %rdi
movl $0x1, %esi
movl $0x28, %edx
movq %rbx, %rcx
callq 0x4230
cmpq $0x28, %rax
jne 0x67f0
movl 0x1c(%rsp), %esi
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0x40f0
movl 0x24(%rsp), %r15d
movl 0x28(%rsp), %ebp
movl %ebp, %r12d
negl %r12d
cmovsl %ebp, %r12d
movl %r15d, (%r14)
movl %r12d, 0x4(%r14)
movl %r15d, %ebx
imull %r12d, %ebx
movslq %ebx, %r13
shlq $0x3, %r13
cmpq 0x8(%r14), %r13
jbe 0x66ce
movq 0x18(%r14), %rdi
testq %rdi, %rdi
je 0x66b4
callq 0x8920
movl (%r14), %r15d
movl 0x4(%r14), %r12d
testl %ebx, %ebx
movq $-0x1, %rdi
cmovnsq %r13, %rdi
callq 0x9660
movq %rax, 0x18(%r14)
movq %r13, 0x8(%r14)
movq %r15, (%rsp)
leal (%r15,%r15,2), %r13d
addl $0x3, %r13d
andl $0xffffffc, %r13d       # imm = 0xFFFFFFC
movslq %r12d, %r12
imulq %r13, %r12
movq %r12, %rdi
callq 0x9660
movl $0x1, %esi
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x4230
cmpq %r12, %rax
jne 0x680f
movl 0x4(%r14), %eax
testl %eax, %eax
jle 0x67ac
movq (%rsp), %rcx
leal (%rcx,%rcx,2), %ecx
movslq %ecx, %rcx
subq %rcx, %r13
movq 0x18(%r14), %rcx
movl (%r14), %edx
xorl %esi, %esi
movq %r15, %r14
testl %edx, %edx
jle 0x67a3
movl %esi, %r8d
notl %r8d
addl %eax, %r8d
testl %ebp, %ebp
cmovlel %esi, %r8d
imull %edx, %r8d
movslq %r8d, %r8
leaq (%rcx,%r8,8), %r8
xorl %r9d, %r9d
movzbl (%r14), %r10d
movl %r10d, %r11d
shll $0x8, %r11d
orl %r10d, %r11d
movw %r11w, (%r8,%r9,8)
movzbl 0x1(%r14), %r10d
movl %r10d, %r11d
shll $0x8, %r11d
orl %r10d, %r11d
movw %r11w, 0x2(%r8,%r9,8)
movzbl 0x2(%r14), %r10d
movl %r10d, %r11d
shll $0x8, %r11d
orl %r10d, %r11d
movw %r11w, 0x4(%r8,%r9,8)
addq $0x3, %r14
incq %r9
cmpl %r9d, %edx
jne 0x675a
addq %r13, %r14
incl %esi
cmpl %eax, %esi
jne 0x6739
movq %r15, %rdi
callq 0x8920
movq %rbx, %rdi
callq 0x42a0
testq %rbx, %rbx
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17b0f(%rip), %rdi     # 0x1e2e7
leaq 0x17ae1(%rip), %rsi     # 0x1e2c0
leaq 0x17b14(%rip), %rcx     # 0x1e2fa
movl $0x3f, %edx
callq 0x4100
leaq 0x17b28(%rip), %rdi     # 0x1e31f
leaq 0x17ac2(%rip), %rsi     # 0x1e2c0
leaq 0x17af5(%rip), %rcx     # 0x1e2fa
movl $0x43, %edx
callq 0x4100
leaq 0x17b1c(%rip), %rdi     # 0x1e332
leaq 0x17aa3(%rip), %rsi     # 0x1e2c0
leaq 0x17ad6(%rip), %rcx     # 0x1e2fa
movl $0x5a, %edx
callq 0x4100