// File: /workspace/github_cpp_makefile_star2_O2/kebwi[P]Trifid/src/ImageReal.cpp
// Function: ImageReal::readFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool ImageReal::readFile(string filename)
{
	filename_ = filename;
	
	ifstream ifs;
	ifs.open(filename_.c_str());
	
	//In theory, this function could return false on failure instead of dieing.
	if (!ifs)
	{
		string error = "Input filename error (file not found perhaps): " + filename_;
		die(error.c_str());
	}
	
	//Read pgm 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 PGM file, binary PGM files are required");
	if (magicNumber != "P5")
		die("Input file does not appear to be a PGM file");
	/*
	cout << "PGM file opened for reading: " << filename_ << endl;
	cout << "  PGM header data:" << endl;
	cout << "    PGM magic number (should be P5):  " << magicNumber << endl;
	cout << "    Note:                             " << note << endl;
	cout << "    Width, Height:                    " << width << "   " << height << endl;
	cout << "    Max value:                        " << maxValue << endl;
	*/
	//Resize the images
	resize(width, height);
	
	//Read input image
	unsigned short maxPixelValueRead = 0;
	Float_t *imgPtr = img_;
	
	if (maxValue <= 255)	//8 bit image
	{
		unsigned char pixelReadC;
		for (int i = 0; i < totalPixels_; i++)
		{
			assert(ifs);
			pixelReadC = ifs.get();
			if (pixelReadC > maxPixelValueRead)
				maxPixelValueRead = pixelReadC;
			*imgPtr++ = pixelReadC / 255.0;
			assert(ifs);
		}
	}
	else	//16 bit image
	{
		unsigned short pixelReadS;
		char *charPtr;
		for (int i = 0; i < totalPixels_; i++)
		{
			assert(ifs);
			charPtr = (char*)&pixelReadS;
			*charPtr++ = ifs.get();
			*charPtr = ifs.get();
			if (pixelReadS > maxPixelValueRead)
				maxPixelValueRead = pixelReadS;
			*imgPtr++ = pixelReadS / 65535.0;
			assert(ifs);
		}
	}
	ifs.get();
	assert(!ifs);
	ifs.close();
	
	//cout << "  Max pixel value read: " << maxPixelValueRead << endl << endl;
	
	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 0xace6
leaq 0x183ab(%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 0xaf8e
leaq 0x58(%rsp), %r14
leaq 0x38(%rsp), %r15
movq %r14, %rdi
callq 0x30e0
cmpl $0xa, %eax
je 0xad56
movq %r14, %rdi
callq 0x3340
movsbl %al, %esi
movq %r15, %rdi
callq 0x3250
jmp 0xad34
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 0xafad
leaq 0x18342(%rip), %rsi     # 0x230db
leaq 0x18(%rsp), %rdi
callq 0x80ee
testb %al, %al
je 0xadb3
leaq 0x18330(%rip), %rdi     # 0x230de
callq 0x3b3c
leaq 0x18362(%rip), %rsi     # 0x2311c
leaq 0x18(%rsp), %rdi
callq 0x853a
testb %al, %al
je 0xadd4
leaq 0x18350(%rip), %rdi     # 0x2311f
callq 0x3b3c
movl 0x14(%rsp), %esi
movl 0x10(%rsp), %edx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
movq 0x38(%rbx), %r15
cmpl $0x100, 0xc(%rsp)       # imm = 0x100
jge 0xae4f
xorl %r12d, %r12d
leaq 0x58(%rsp), %r14
cmpl 0x30(%rbx), %r12d
jge 0xaeba
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
jne 0xaf12
movq %r14, %rdi
callq 0x3340
movzbl %al, %eax
cvtsi2sd %eax, %xmm0
divsd 0x18198(%rip), %xmm0    # 0x22fc8
movsd %xmm0, (%r15,%r12,8)
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
jne 0xaf31
incq %r12
jmp 0xadfb
xorl %r12d, %r12d
leaq 0x58(%rsp), %r14
cmpl 0x30(%rbx), %r12d
jge 0xaeba
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
jne 0xaf6f
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 0x18125(%rip), %xmm0    # 0x22fc0
movsd %xmm0, (%r15,%r12,8)
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
jne 0xaf50
incq %r12
jmp 0xae57
leaq 0x58(%rsp), %rdi
callq 0x3340
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
je 0xafcc
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 0x18233(%rip), %rdi     # 0x2314c
leaq 0x180d4(%rip), %rsi     # 0x22ff4
leaq 0x1818b(%rip), %rcx     # 0x230b2
movl $0x109, %edx            # imm = 0x109
callq 0x31a0
leaq 0x18214(%rip), %rdi     # 0x2314c
leaq 0x180b5(%rip), %rsi     # 0x22ff4
leaq 0x1816c(%rip), %rcx     # 0x230b2
movl $0x10e, %edx            # imm = 0x10E
callq 0x31a0
leaq 0x181f5(%rip), %rdi     # 0x2314c
leaq 0x18096(%rip), %rsi     # 0x22ff4
leaq 0x1814d(%rip), %rcx     # 0x230b2
movl $0x11e, %edx            # imm = 0x11E
callq 0x31a0
leaq 0x181d6(%rip), %rdi     # 0x2314c
leaq 0x18077(%rip), %rsi     # 0x22ff4
leaq 0x1812e(%rip), %rcx     # 0x230b2
movl $0x117, %edx            # imm = 0x117
callq 0x31a0
leaq 0x1810b(%rip), %rdi     # 0x230a0
leaq 0x18058(%rip), %rsi     # 0x22ff4
leaq 0x1810f(%rip), %rcx     # 0x230b2
movl $0xeb, %edx
callq 0x31a0
leaq 0x180ec(%rip), %rdi     # 0x230a0
leaq 0x18039(%rip), %rsi     # 0x22ff4
leaq 0x180f0(%rip), %rcx     # 0x230b2
movl $0xef, %edx
callq 0x31a0
leaq 0x18178(%rip), %rdi     # 0x2314b
leaq 0x1801a(%rip), %rsi     # 0x22ff4
leaq 0x180d1(%rip), %rcx     # 0x230b2
movl $0x122, %edx            # imm = 0x122
callq 0x31a0
movq %rax, %rbx
jmp 0xb00c
jmp 0xaff2
movq %rax, %rbx
jmp 0xb016
jmp 0xafff
jmp 0xafff
jmp 0xafff
jmp 0xafff
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