// File: /workspace/github_cpp_makefile_star2_O1/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 0x18367(%rip), %rdi     # 0x1f211
xorl %eax, %eax
callq 0x4050
movq (%rbx), %rdi
leaq 0x1836d(%rip), %rsi     # 0x1f228
callq 0x4150
movq %rax, %rbx
testq %rax, %rax
je 0x705a
leaq 0x12(%rsp), %rdi
movl $0x1, %esi
movl $0xe, %edx
movq %rbx, %rcx
callq 0x4230
cmpq $0xe, %rax
jne 0x706f
leaq 0x20(%rsp), %rdi
movl $0x1, %esi
movl $0x28, %edx
movq %rbx, %rcx
callq 0x4230
cmpq $0x28, %rax
jne 0x708e
movl 0x1c(%rsp), %esi
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0x40f0
movl 0x24(%rsp), %eax
movl 0x28(%rsp), %ebp
movl %eax, (%r14)
movl %ebp, %r12d
negl %r12d
cmovsl %ebp, %r12d
movl %r12d, 0x4(%r14)
imull %eax, %r12d
movslq %r12d, %r15
shlq $0x3, %r15
cmpq 0x8(%r14), %r15
jbe 0x6f74
movq 0x18(%r14), %rdi
testq %rdi, %rdi
je 0x6f59
callq 0x91b0
testl %r12d, %r12d
movq $-0x1, %rdi
cmovnsq %r15, %rdi
callq 0x9ef0
movq %rax, 0x18(%r14)
movq %r15, 0x8(%r14)
movslq (%r14), %rax
leaq (%rax,%rax,2), %r15
leal 0x3(%r15), %r13d
andl $0xffffffc, %r13d       # imm = 0xFFFFFFC
movslq 0x4(%r14), %r12
imulq %r13, %r12
movq %r12, %rdi
callq 0x9ef0
movl $0x1, %esi
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
callq 0x4230
cmpq %r12, %rax
jne 0x70ad
movl 0x4(%r14), %eax
testl %eax, %eax
movq %rbx, %rdi
movq 0x8(%rsp), %rbx
jle 0x704d
subq %r15, %r13
movq 0x18(%r14), %rcx
movl (%r14), %edx
xorl %esi, %esi
movq %rdi, %r14
testl %edx, %edx
jle 0x7044
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 0x6ffb
addq %r13, %r14
incl %esi
cmpl %eax, %esi
jne 0x6fda
callq 0x91b0
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 0x181b5(%rip), %rdi     # 0x1f22b
leaq 0x18187(%rip), %rsi     # 0x1f204
leaq 0x181ba(%rip), %rcx     # 0x1f23e
movl $0x3f, %edx
callq 0x4100
leaq 0x181ce(%rip), %rdi     # 0x1f263
leaq 0x18168(%rip), %rsi     # 0x1f204
leaq 0x1819b(%rip), %rcx     # 0x1f23e
movl $0x43, %edx
callq 0x4100
leaq 0x181c2(%rip), %rdi     # 0x1f276
leaq 0x18149(%rip), %rsi     # 0x1f204
leaq 0x1817c(%rip), %rcx     # 0x1f23e
movl $0x5a, %edx
callq 0x4100