// File: /workspace/github_cpp_makefile_star2_O3/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 0xd65e
leaq 0x1da51(%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 0xd65e
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 0xd933
leaq 0x50(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %r14, %rdi
callq 0x3100
cmpl $0xa, %eax
je 0xd6d0
movq %r14, %rdi
callq 0x3380
movsbl %al, %esi
movq %r15, %rdi
callq 0x3280
jmp 0xd6ae
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 0xd952
leaq 0x1d9d8(%rip), %rsi     # 0x2b0eb
movq %rsp, %rdi
callq 0x30c0
testl %eax, %eax
jne 0xd72b
leaq 0x1d9c8(%rip), %rdi     # 0x2b0ee
callq 0x403c
leaq 0x1d9fa(%rip), %rsi     # 0x2b12c
movq %rsp, %rdi
callq 0x30c0
testl %eax, %eax
je 0xd74a
leaq 0x1d9ea(%rip), %rdi     # 0x2b12f
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 0xd7cf
testl %eax, %eax
jle 0xd83f
leaq 0x50(%rsp), %r14
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x70(%rsp,%rax), %eax
xorl %r12d, %r12d
testb $0x5, %al
jne 0xd8d6
movq %r14, %rdi
callq 0x3380
movzbl %al, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x1d82f(%rip), %xmm0    # 0x2afd8
movsd %xmm0, (%r15,%r12,8)
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
movl 0x70(%rsp,%rax), %eax
testb $0x5, %al
jne 0xd8b7
incq %r12
cmpl 0x30(%rbx), %r12d
jl 0xd787
jmp 0xd83f
testl %eax, %eax
jle 0xd83f
leaq 0x50(%rsp), %r14
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x70(%rsp,%rax), %eax
xorl %r12d, %r12d
testb $0x5, %al
jne 0xd914
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 0x1d7b5(%rip), %xmm0    # 0x2afd0
movsd %xmm0, (%r15,%r12,8)
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
movl 0x70(%rsp,%rax), %eax
testb $0x5, %al
jne 0xd8f5
incq %r12
cmpl 0x30(%rbx), %r12d
jl 0xd7e6
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %r12
callq 0x3380
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
je 0xd971
leaq 0x50(%rsp), %rdi
callq 0x3080
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0xd883
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3310
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0xd899
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 0x1d89e(%rip), %rdi     # 0x2b15c
leaq 0x1d73f(%rip), %rsi     # 0x2b004
leaq 0x1d7f6(%rip), %rcx     # 0x2b0c2
movl $0x10e, %edx            # imm = 0x10E
callq 0x31c0
leaq 0x1d87f(%rip), %rdi     # 0x2b15c
leaq 0x1d720(%rip), %rsi     # 0x2b004
leaq 0x1d7d7(%rip), %rcx     # 0x2b0c2
movl $0x109, %edx            # imm = 0x109
callq 0x31c0
leaq 0x1d860(%rip), %rdi     # 0x2b15c
leaq 0x1d701(%rip), %rsi     # 0x2b004
leaq 0x1d7b8(%rip), %rcx     # 0x2b0c2
movl $0x11e, %edx            # imm = 0x11E
callq 0x31c0
leaq 0x1d841(%rip), %rdi     # 0x2b15c
leaq 0x1d6e2(%rip), %rsi     # 0x2b004
leaq 0x1d799(%rip), %rcx     # 0x2b0c2
movl $0x117, %edx            # imm = 0x117
callq 0x31c0
leaq 0x1d776(%rip), %rdi     # 0x2b0b0
leaq 0x1d6c3(%rip), %rsi     # 0x2b004
leaq 0x1d77a(%rip), %rcx     # 0x2b0c2
movl $0xeb, %edx
callq 0x31c0
leaq 0x1d757(%rip), %rdi     # 0x2b0b0
leaq 0x1d6a4(%rip), %rsi     # 0x2b004
leaq 0x1d75b(%rip), %rcx     # 0x2b0c2
movl $0xef, %edx
callq 0x31c0
leaq 0x1d7e3(%rip), %rdi     # 0x2b15b
leaq 0x1d685(%rip), %rsi     # 0x2b004
leaq 0x1d73c(%rip), %rcx     # 0x2b0c2
movl $0x122, %edx            # imm = 0x122
callq 0x31c0
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0xd9dd
jmp 0xd9ea
jmp 0xd9a5
movq %rax, %rbx
jmp 0xd9ea
jmp 0xd9ba
jmp 0xd9ba
jmp 0xd9b0
movq %rax, %rbx
leaq 0x10(%rsp), %r12
jmp 0xd9bd
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0xd9d4
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3310
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0xd9ea
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x50(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x3500