// File: /workspace/github_cpp_makefile_star2_O1/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 0xe73a
leaq 0x1d99d(%rip), %rsi     # 0x2c0a8
movq %rsp, %rdi
movq %r14, %rdx
callq 0x9ffb
movq (%rsp), %rdi
callq 0x403c
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe73a
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 0xea48
leaq 0x50(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %r14, %rdi
callq 0x3100
cmpl $0xa, %eax
je 0xe7a8
movq %r14, %rdi
callq 0x3380
movsbl %al, %esi
movq %r15, %rdi
callq 0x3280
jmp 0xe786
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 0xea67
leaq 0x1d928(%rip), %rsi     # 0x2c113
movq %rsp, %rdi
callq 0x30c0
testl %eax, %eax
jne 0xe803
leaq 0x1db45(%rip), %rdi     # 0x2c343
callq 0x403c
leaq 0x1db77(%rip), %rsi     # 0x2c381
movq %rsp, %rdi
callq 0x30c0
testl %eax, %eax
je 0xe822
leaq 0x1db67(%rip), %rdi     # 0x2c384
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 0xe8c5
testl %eax, %eax
jle 0xe954
xorl %r15d, %r15d
leaq 0x50(%rsp), %r14
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
jne 0xe9cc
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x3380
movzbl %al, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x1d77b(%rip), %xmm0    # 0x2c000
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x38(%rax), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r12
cmpq $0x3, %r12
jne 0xe86b
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
jne 0xe9eb
incq %r15
movslq 0x30(%rbx), %rax
cmpq %rax, %r15
jl 0xe854
jmp 0xe954
testl %eax, %eax
jle 0xe954
xorl %r15d, %r15d
leaq 0x50(%rsp), %r14
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
jne 0xea0a
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 0x1d6df(%rip), %xmm0    # 0x2bff8
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x38(%rax), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r12
cmpq $0x3, %r12
jne 0xe8ec
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
jne 0xea29
incq %r15
movslq 0x30(%rbx), %rax
cmpq %rax, %r15
jl 0xe8d5
leaq 0x50(%rsp), %rdi
callq 0x3380
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
je 0xea86
leaq 0x50(%rsp), %rdi
callq 0x3080
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0xe993
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3310
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xe9ae
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 0x1d7b1(%rip), %rdi     # 0x2c184
leaq 0x1d8d0(%rip), %rsi     # 0x2c2aa
leaq 0x1d934(%rip), %rcx     # 0x2c315
movl $0xf7, %edx
callq 0x31c0
leaq 0x1d792(%rip), %rdi     # 0x2c184
leaq 0x1d8b1(%rip), %rsi     # 0x2c2aa
leaq 0x1d915(%rip), %rcx     # 0x2c315
movl $0x101, %edx            # imm = 0x101
callq 0x31c0
leaq 0x1d773(%rip), %rdi     # 0x2c184
leaq 0x1d892(%rip), %rsi     # 0x2c2aa
leaq 0x1d8f6(%rip), %rcx     # 0x2c315
movl $0x10a, %edx            # imm = 0x10A
callq 0x31c0
leaq 0x1d754(%rip), %rdi     # 0x2c184
leaq 0x1d873(%rip), %rsi     # 0x2c2aa
leaq 0x1d8d7(%rip), %rcx     # 0x2c315
movl $0x116, %edx            # imm = 0x116
callq 0x31c0
leaq 0x1d689(%rip), %rdi     # 0x2c0d8
leaq 0x1d854(%rip), %rsi     # 0x2c2aa
leaq 0x1d8b8(%rip), %rcx     # 0x2c315
movl $0xdb, %edx
callq 0x31c0
leaq 0x1d66a(%rip), %rdi     # 0x2c0d8
leaq 0x1d835(%rip), %rsi     # 0x2c2aa
leaq 0x1d899(%rip), %rcx     # 0x2c315
movl $0xdf, %edx
callq 0x31c0
leaq 0x1d6f6(%rip), %rdi     # 0x2c183
leaq 0x1d816(%rip), %rsi     # 0x2c2aa
leaq 0x1d87a(%rip), %rcx     # 0x2c315
movl $0x11a, %edx            # imm = 0x11A
callq 0x31c0
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0xeae5
jmp 0xeab5
movq %rax, %rbx
jmp 0xeaf7
jmp 0xeac2
jmp 0xeac2
jmp 0xeac2
jmp 0xeac2
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0xeadc
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3310
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xeaf7
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x50(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x3500
nop