// File: /workspace/github_cpp_makefile_star2_O2/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 0x17d79(%rip), %rdi     # 0x1e29d
xorl %eax, %eax
callq 0x4050
movq (%rbx), %rdi
leaq 0x17d7f(%rip), %rsi     # 0x1e2b4
callq 0x4150
movq %rax, %r15
testq %rax, %rax
je 0x66d7
leaq 0x12(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0xe
popq %rdx
movq %r15, %rcx
callq 0x4230
cmpq $0xe, %rax
jne 0x66ec
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x28
popq %rdx
movq %r15, %rcx
callq 0x4230
cmpq $0x28, %rax
jne 0x6705
movl 0x1c(%rsp), %esi
movq %r15, 0x8(%rsp)
movq %r15, %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 0x65ec
movq 0x18(%r14), %rdi
testq %rdi, %rdi
je 0x65d6
callq 0x83f0
movl (%r14), %r15d
movl 0x4(%r14), %r12d
testl %ebx, %ebx
pushq $-0x1
popq %rdi
cmovnsq %r13, %rdi
callq 0x9130
movq %rax, 0x18(%r14)
movq %r13, 0x8(%r14)
leal (%r15,%r15,2), %r13d
addl $0x3, %r13d
andl $0xffffffc, %r13d       # imm = 0xFFFFFFC
movslq %r12d, %r12
imulq %r13, %r12
movq %r12, %rdi
callq 0x9130
movq %rax, %rbx
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
callq 0x4230
cmpq %r12, %rax
jne 0x671e
leal (%r15,%r15,2), %eax
cltq
subq %rax, %r13
movl (%r14), %eax
movl 0x4(%r14), %ecx
movq 0x18(%r14), %rdx
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %r14d
cmovgl %eax, %r14d
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
movq %rbx, %r9
movq 0x8(%rsp), %r15
movq %rbx, %rdi
cmpl %r8d, %esi
je 0x66ca
movl %esi, %r10d
notl %r10d
addl %ecx, %r10d
testl %ebp, %ebp
cmovlel %esi, %r10d
imull %eax, %r10d
movslq %r10d, %r10
leaq (%rdx,%r10,8), %r10
xorl %r11d, %r11d
cmpl %r11d, %r14d
je 0x66c3
movzbl (%r9), %ebx
imull $0x101, %ebx, %ebx      # imm = 0x101
movw %bx, (%r10,%r11,8)
movzbl 0x1(%r9), %ebx
imull $0x101, %ebx, %ebx      # imm = 0x101
movw %bx, 0x2(%r10,%r11,8)
movzbl 0x2(%r9), %ebx
imull $0x101, %ebx, %ebx      # imm = 0x101
movw %bx, 0x4(%r10,%r11,8)
addq $0x3, %r9
incq %r11
jmp 0x6684
addq %r13, %r9
incl %esi
jmp 0x6662
callq 0x83f0
movq %r15, %rdi
callq 0x42a0
testq %r15, %r15
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17bc4(%rip), %rdi     # 0x1e2b7
leaq 0x17b96(%rip), %rsi     # 0x1e290
leaq 0x17bc9(%rip), %rcx     # 0x1e2ca
pushq $0x3f
jmp 0x6735
leaq 0x17be3(%rip), %rdi     # 0x1e2ef
leaq 0x17b7d(%rip), %rsi     # 0x1e290
leaq 0x17bb0(%rip), %rcx     # 0x1e2ca
pushq $0x43
jmp 0x6735
leaq 0x17bdd(%rip), %rdi     # 0x1e302
leaq 0x17b64(%rip), %rsi     # 0x1e290
leaq 0x17b97(%rip), %rcx     # 0x1e2ca
pushq $0x5a
popq %rdx
callq 0x4100
nop