// File: /workspace/github_cpp_makefile_star2_O1/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 %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 0xd720
leaq 0x1e9b7(%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 0xd720
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
xorl %eax, %eax
movq %rax, -0x8(%r12)
movb %al, (%r12)
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 0xd9f5
leaq 0x50(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %r14, %rdi
callq 0x3100
cmpl $0xa, %eax
je 0xd792
movq %r14, %rdi
callq 0x3380
movsbl %al, %esi
movq %r15, %rdi
callq 0x3280
jmp 0xd770
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 0xda14
leaq 0x1e93e(%rip), %rsi     # 0x2c113
movq %rsp, %rdi
callq 0x30c0
testl %eax, %eax
jne 0xd7ed
leaq 0x1e92e(%rip), %rdi     # 0x2c116
callq 0x403c
leaq 0x1e960(%rip), %rsi     # 0x2c154
movq %rsp, %rdi
callq 0x30c0
testl %eax, %eax
je 0xd80c
leaq 0x1e950(%rip), %rdi     # 0x2c157
callq 0x403c
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
movq 0x38(%rbx), %r15
cmpl $0x100, 0x24(%rsp)      # imm = 0x100
movl 0x30(%rbx), %eax
jge 0xd891
testl %eax, %eax
jle 0xd901
xorl %r12d, %r12d
leaq 0x50(%rsp), %r14
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
jne 0xd998
movq %r14, %rdi
callq 0x3380
movzbl %al, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x1e794(%rip), %xmm0    # 0x2c000
movsd %xmm0, (%r15,%r12,8)
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
jne 0xd979
incq %r12
cmpl 0x30(%rbx), %r12d
jl 0xd83e
jmp 0xd901
testl %eax, %eax
jle 0xd901
xorl %r12d, %r12d
leaq 0x50(%rsp), %r14
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
jne 0xd9d6
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 0x1e71a(%rip), %xmm0    # 0x2bff8
movsd %xmm0, (%r15,%r12,8)
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
jne 0xd9b7
incq %r12
cmpl 0x30(%rbx), %r12d
jl 0xd89d
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %r12
callq 0x3380
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
je 0xda33
leaq 0x50(%rsp), %rdi
callq 0x3080
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0xd945
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3310
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0xd95b
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 0x1e804(%rip), %rdi     # 0x2c184
leaq 0x1e6a5(%rip), %rsi     # 0x2c02c
leaq 0x1e75c(%rip), %rcx     # 0x2c0ea
movl $0x10e, %edx            # imm = 0x10E
callq 0x31c0
leaq 0x1e7e5(%rip), %rdi     # 0x2c184
leaq 0x1e686(%rip), %rsi     # 0x2c02c
leaq 0x1e73d(%rip), %rcx     # 0x2c0ea
movl $0x109, %edx            # imm = 0x109
callq 0x31c0
leaq 0x1e7c6(%rip), %rdi     # 0x2c184
leaq 0x1e667(%rip), %rsi     # 0x2c02c
leaq 0x1e71e(%rip), %rcx     # 0x2c0ea
movl $0x11e, %edx            # imm = 0x11E
callq 0x31c0
leaq 0x1e7a7(%rip), %rdi     # 0x2c184
leaq 0x1e648(%rip), %rsi     # 0x2c02c
leaq 0x1e6ff(%rip), %rcx     # 0x2c0ea
movl $0x117, %edx            # imm = 0x117
callq 0x31c0
leaq 0x1e6dc(%rip), %rdi     # 0x2c0d8
leaq 0x1e629(%rip), %rsi     # 0x2c02c
leaq 0x1e6e0(%rip), %rcx     # 0x2c0ea
movl $0xeb, %edx
callq 0x31c0
leaq 0x1e6bd(%rip), %rdi     # 0x2c0d8
leaq 0x1e60a(%rip), %rsi     # 0x2c02c
leaq 0x1e6c1(%rip), %rcx     # 0x2c0ea
movl $0xef, %edx
callq 0x31c0
leaq 0x1e749(%rip), %rdi     # 0x2c183
leaq 0x1e5eb(%rip), %rsi     # 0x2c02c
leaq 0x1e6a2(%rip), %rcx     # 0x2c0ea
movl $0x122, %edx            # imm = 0x122
callq 0x31c0
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0xda9f
jmp 0xdaac
jmp 0xda67
movq %rax, %rbx
jmp 0xdaac
jmp 0xda7c
jmp 0xda7c
jmp 0xda72
movq %rax, %rbx
leaq 0x10(%rsp), %r12
jmp 0xda7f
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0xda96
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3310
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0xdaac
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x50(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x3500