// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x260, %rsp            # imm = 0x260
movq %rdi, %rbx
leaq 0x8(%rdi), %r14
movq %r14, %rdi
callq 0x3150
leaq 0x58(%rsp), %r15
movq %r15, %rdi
callq 0x3310
movq 0x8(%rbx), %rsi
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x3190
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
je 0xba30
leaq 0x17661(%rip), %rsi     # 0x23070
leaq 0x18(%rsp), %rdi
movq %r14, %rdx
callq 0x8078
movq 0x18(%rsp), %rdi
callq 0x3b3c
leaq 0x18(%rsp), %rdi
callq 0x3548
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x58(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x3370
leaq 0x58(%rsp), %rdi
callq 0x3340
cmpl $0xa, %eax
jne 0xbd0a
leaq 0x58(%rsp), %r14
leaq 0x38(%rsp), %r15
movq %r14, %rdi
callq 0x30e0
cmpl $0xa, %eax
je 0xbaa0
movq %r14, %rdi
callq 0x3340
movsbl %al, %esi
movq %r15, %rdi
callq 0x3250
jmp 0xba7e
leaq 0x58(%rsp), %rdi
leaq 0x14(%rsp), %rsi
callq 0x3180
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x3180
leaq 0xc(%rsp), %rsi
movq %rax, %rdi
callq 0x3180
leaq 0x58(%rsp), %rdi
callq 0x3340
cmpl $0xa, %eax
jne 0xbd29
leaq 0x175f8(%rip), %rsi     # 0x230db
leaq 0x18(%rsp), %rdi
callq 0x80ee
testb %al, %al
je 0xbafd
leaq 0x17813(%rip), %rdi     # 0x2330b
callq 0x3b3c
leaq 0x17845(%rip), %rsi     # 0x23349
leaq 0x18(%rsp), %rdi
callq 0x853a
testb %al, %al
je 0xbb1e
leaq 0x17833(%rip), %rdi     # 0x2334c
callq 0x3b3c
movl 0x14(%rsp), %esi
movl 0x10(%rsp), %edx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
cmpl $0x100, 0xc(%rsp)       # imm = 0x100
jge 0xbbcf
xorl %r15d, %r15d
leaq 0x58(%rsp), %r14
movslq 0x30(%rbx), %rax
cmpq %rax, %r15
jge 0xbc74
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
jne 0xbccc
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0xbb9d
movq %r14, %rdi
callq 0x3340
movzbl %al, %eax
cvtsi2sd %eax, %xmm0
divsd 0x17442(%rip), %xmm0    # 0x22fc8
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x38(%rax), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r12
jmp 0xbb69
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
incq %r15
testb $0x5, 0x78(%rsp,%rax)
je 0xbb45
leaq 0x17595(%rip), %rdi     # 0x2314c
leaq 0x176b4(%rip), %rsi     # 0x23272
leaq 0x17718(%rip), %rcx     # 0x232dd
movl $0x101, %edx            # imm = 0x101
callq 0x31a0
xorl %r15d, %r15d
leaq 0x58(%rsp), %r14
movslq 0x30(%rbx), %rax
cmpq %rax, %r15
jge 0xbc74
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
jne 0xbceb
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0xbc42
movq %r14, %rdi
callq 0x3340
movl %eax, %ebp
movq %r14, %rdi
callq 0x3340
shll $0x8, %eax
movzbl %bpl, %ecx
orl %eax, %ecx
movzwl %cx, %eax
cvtsi2sd %eax, %xmm0
divsd 0x17395(%rip), %xmm0    # 0x22fc0
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x38(%rax), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r12
jmp 0xbbfb
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
incq %r15
testb $0x5, 0x78(%rsp,%rax)
je 0xbbd7
leaq 0x174f0(%rip), %rdi     # 0x2314c
leaq 0x1760f(%rip), %rsi     # 0x23272
leaq 0x17673(%rip), %rcx     # 0x232dd
movl $0x116, %edx            # imm = 0x116
callq 0x31a0
leaq 0x58(%rsp), %rdi
callq 0x3340
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
je 0xbd48
leaq 0x58(%rsp), %rdi
callq 0x3080
leaq 0x38(%rsp), %rdi
callq 0x3548
leaq 0x18(%rsp), %rdi
callq 0x3548
leaq 0x58(%rsp), %rdi
callq 0x3090
movb $0x1, %al
addq $0x260, %rsp            # imm = 0x260
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17479(%rip), %rdi     # 0x2314c
leaq 0x17598(%rip), %rsi     # 0x23272
leaq 0x175fc(%rip), %rcx     # 0x232dd
movl $0xf7, %edx
callq 0x31a0
leaq 0x1745a(%rip), %rdi     # 0x2314c
leaq 0x17579(%rip), %rsi     # 0x23272
leaq 0x175dd(%rip), %rcx     # 0x232dd
movl $0x10a, %edx            # imm = 0x10A
callq 0x31a0
leaq 0x1738f(%rip), %rdi     # 0x230a0
leaq 0x1755a(%rip), %rsi     # 0x23272
leaq 0x175be(%rip), %rcx     # 0x232dd
movl $0xdb, %edx
callq 0x31a0
leaq 0x17370(%rip), %rdi     # 0x230a0
leaq 0x1753b(%rip), %rsi     # 0x23272
leaq 0x1759f(%rip), %rcx     # 0x232dd
movl $0xdf, %edx
callq 0x31a0
leaq 0x173fc(%rip), %rdi     # 0x2314b
leaq 0x1751c(%rip), %rsi     # 0x23272
leaq 0x17580(%rip), %rcx     # 0x232dd
movl $0x11a, %edx            # imm = 0x11A
callq 0x31a0
movq %rax, %rbx
jmp 0xbd88
jmp 0xbd6e
movq %rax, %rbx
jmp 0xbd92
jmp 0xbd7b
jmp 0xbd7b
jmp 0xbd7b
jmp 0xbd7b
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3548
leaq 0x18(%rsp), %rdi
callq 0x3548
leaq 0x58(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x34d0