// File: /workspace/github_cpp_makefile_star2_O3/kebwi[P]Trifid/src/Image3ChnlReal.cpp
// Function: Image3ChnlReal::readFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool Image3ChnlReal::readFile(string filename)
{
	filename_ = filename;
	
	ifstream ifs;
	ifs.open(filename_.c_str());
	if (!ifs)
	{
		string error = "Input filename error (file not found perhaps): " + filename_;
		die(error.c_str());
	}
	
	//Read ppm header
	string magicNumber, note;
	int width, height, maxValue;
	ifs >> magicNumber;
	assert(ifs.get() == '\n');
	while (ifs.peek() != '\n')
		note += ifs.get();
	ifs >> width >> height >> maxValue;
	assert(ifs.get() == '\n');
	
	if (magicNumber == "P3")
		die("Input file is a ASCII PPM file, binary PPM files are required");
	if (magicNumber != "P6")
		die("Input file does not appear to be a PPM file");
	/*
	cout << "PPM file read: " << filename_ << endl;
	cout << "  PPM header data:" << endl;
	cout << "    PPM magic number (should be P6):  " << magicNumber << endl;
	cout << "    Note:                             " << note << endl;
	cout << "    Width, Height:                    " << width << "   " << height << endl;
	cout << "    Max value:                        " << maxValue << endl;
	*/
	resize(width, height);
	
	//Read input image
	unsigned short maxPixelValueRead = 0;
	
	if (maxValue <= 255)	//8 bit image
	{
		unsigned char pixelReadC;
		for (int i = 0; i < totalPixels_; i++)
		{
			assert(ifs);
			
			for (int j = 0; j < 3; j++)
			{
				pixelReadC = ifs.get();
				if (pixelReadC > maxPixelValueRead)
					maxPixelValueRead = pixelReadC;
				(*img_[j])[i] = pixelReadC / 255.0;
			}
			
			assert(ifs);
		}
	}
	else	//16 bit image
	{
		unsigned short pixelReadS;
		char *charPtr;
		for (int i = 0; i < totalPixels_; i++)
		{
			assert(ifs);
			
			for (int j = 0; j < 3; j++)
			{
				charPtr = (char*)&pixelReadS;
				*charPtr++ = ifs.get();
				*charPtr = ifs.get();
				if (pixelReadS > maxPixelValueRead)
					maxPixelValueRead = pixelReadS;
				(*img_[j])[i] = pixelReadS / 65535.0;
			}
			
			assert(ifs);
		}
	}
	ifs.get();
	assert(!ifs);
	ifs.close();
	
	//cout << "  Max pixel value read: " << maxPixelValueRead << endl << endl;
	
	//In theory, this function could return false on failure instead of dieing.
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rdi, %rbx
leaq 0x8(%rdi), %r14
movq %r14, %rdi
callq 0x3170
leaq 0x50(%rsp), %r15
movq %r15, %rdi
callq 0x3350
movq 0x8(%rbx), %rsi
movq %r15, %rdi
movl $0x8, %edx
callq 0x31b0
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
je 0xe678
leaq 0x1ca37(%rip), %rsi     # 0x2b080
movq %rsp, %rdi
movq %r14, %rdx
callq 0x9fad
movq (%rsp), %rdi
callq 0x403c
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe678
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x10(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x40(%rsp), %r13
movq %r13, -0x10(%r13)
movq %rax, -0x8(%r13)
movb %al, (%r13)
leaq 0x50(%rsp), %rdi
movq %rsp, %rsi
callq 0x33b0
leaq 0x50(%rsp), %rdi
callq 0x3380
cmpl $0xa, %eax
jne 0xe986
leaq 0x50(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %r14, %rdi
callq 0x3100
cmpl $0xa, %eax
je 0xe6e6
movq %r14, %rdi
callq 0x3380
movsbl %al, %esi
movq %r15, %rdi
callq 0x3280
jmp 0xe6c4
leaq 0x50(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x31a0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x31a0
leaq 0x24(%rsp), %rsi
movq %rax, %rdi
callq 0x31a0
leaq 0x50(%rsp), %rdi
callq 0x3380
cmpl $0xa, %eax
jne 0xe9a5
leaq 0x1c9c2(%rip), %rsi     # 0x2b0eb
movq %rsp, %rdi
callq 0x30c0
testl %eax, %eax
jne 0xe741
leaq 0x1cbdf(%rip), %rdi     # 0x2b31b
callq 0x403c
leaq 0x1cc11(%rip), %rsi     # 0x2b359
movq %rsp, %rdi
callq 0x30c0
testl %eax, %eax
je 0xe760
leaq 0x1cc01(%rip), %rdi     # 0x2b35c
callq 0x403c
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
cmpl $0x100, 0x24(%rsp)      # imm = 0x100
movl 0x30(%rbx), %eax
jge 0xe803
testl %eax, %eax
jle 0xe892
leaq 0x50(%rsp), %r14
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x70(%rsp,%rax), %eax
xorl %r15d, %r15d
testb $0x5, %al
jne 0xe90a
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x3380
movzbl %al, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x1c816(%rip), %xmm0    # 0x2afd8
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x38(%rax), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r12
cmpq $0x3, %r12
jne 0xe7a8
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
movl 0x70(%rsp,%rax), %eax
testb $0x5, %al
jne 0xe929
incq %r15
movslq 0x30(%rbx), %rcx
cmpq %rcx, %r15
jl 0xe79d
jmp 0xe892
testl %eax, %eax
jle 0xe892
leaq 0x50(%rsp), %r14
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x70(%rsp,%rax), %eax
xorl %r15d, %r15d
testb $0x5, %al
jne 0xe948
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x3380
movl %eax, %ebp
movq %r14, %rdi
callq 0x3380
shll $0x8, %eax
movzbl %bpl, %ecx
orl %eax, %ecx
movzwl %cx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x1c77a(%rip), %xmm0    # 0x2afd0
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x38(%rax), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r12
cmpq $0x3, %r12
jne 0xe829
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
movl 0x70(%rsp,%rax), %eax
testb $0x5, %al
jne 0xe967
incq %r15
movslq 0x30(%rbx), %rcx
cmpq %rcx, %r15
jl 0xe81e
leaq 0x50(%rsp), %rdi
callq 0x3380
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
je 0xe9c4
leaq 0x50(%rsp), %rdi
callq 0x3080
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0xe8d1
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3310
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xe8ec
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x50(%rsp), %rdi
callq 0x3090
movb $0x1, %al
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c84b(%rip), %rdi     # 0x2b15c
leaq 0x1c96a(%rip), %rsi     # 0x2b282
leaq 0x1c9ce(%rip), %rcx     # 0x2b2ed
movl $0xf7, %edx
callq 0x31c0
leaq 0x1c82c(%rip), %rdi     # 0x2b15c
leaq 0x1c94b(%rip), %rsi     # 0x2b282
leaq 0x1c9af(%rip), %rcx     # 0x2b2ed
movl $0x101, %edx            # imm = 0x101
callq 0x31c0
leaq 0x1c80d(%rip), %rdi     # 0x2b15c
leaq 0x1c92c(%rip), %rsi     # 0x2b282
leaq 0x1c990(%rip), %rcx     # 0x2b2ed
movl $0x10a, %edx            # imm = 0x10A
callq 0x31c0
leaq 0x1c7ee(%rip), %rdi     # 0x2b15c
leaq 0x1c90d(%rip), %rsi     # 0x2b282
leaq 0x1c971(%rip), %rcx     # 0x2b2ed
movl $0x116, %edx            # imm = 0x116
callq 0x31c0
leaq 0x1c723(%rip), %rdi     # 0x2b0b0
leaq 0x1c8ee(%rip), %rsi     # 0x2b282
leaq 0x1c952(%rip), %rcx     # 0x2b2ed
movl $0xdb, %edx
callq 0x31c0
leaq 0x1c704(%rip), %rdi     # 0x2b0b0
leaq 0x1c8cf(%rip), %rsi     # 0x2b282
leaq 0x1c933(%rip), %rcx     # 0x2b2ed
movl $0xdf, %edx
callq 0x31c0
leaq 0x1c790(%rip), %rdi     # 0x2b15b
leaq 0x1c8b0(%rip), %rsi     # 0x2b282
leaq 0x1c914(%rip), %rcx     # 0x2b2ed
movl $0x11a, %edx            # imm = 0x11A
callq 0x31c0
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0xea23
jmp 0xe9f3
movq %rax, %rbx
jmp 0xea35
jmp 0xea00
jmp 0xea00
jmp 0xea00
jmp 0xea00
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0xea1a
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3310
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xea35
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x50(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x3500
nop