// File: 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
movq %rsp, %rbp
subq $0x330, %rsp            # imm = 0x330
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x2d8(%rbp)
addq $0x8, %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x4060
leaq -0x218(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0x4420
movq -0x2d0(%rbp), %rdi
callq 0x4160
movq -0x2c8(%rbp), %rdi
movq %rax, %rsi
movl $0x8, %edx
callq 0x4230
jmp 0x16263
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x41b0
movb %al, -0x2d9(%rbp)
jmp 0x16283
movb -0x2d9(%rbp), %al
testb $0x1, %al
jne 0x1628f
jmp 0x1630d
movq -0x2d8(%rbp), %rdx
addq $0x8, %rdx
leaq 0x33eb7(%rip), %rsi     # 0x4a158
leaq -0x248(%rbp), %rdi
callq 0xe460
jmp 0x162af
leaq -0x248(%rbp), %rdi
callq 0x4160
movq %rax, %rdi
callq 0x5360
jmp 0x162c5
leaq -0x248(%rbp), %rdi
callq 0x46c8
jmp 0x1630d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x168a8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x46c8
jmp 0x168a8
leaq -0x268(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
callq 0x4500
leaq -0x288(%rbp), %rdi
callq 0x4500
movq -0x2e8(%rbp), %rsi
leaq -0x218(%rbp), %rdi
callq 0x44b0
jmp 0x16341
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x2ec(%rbp)
jmp 0x16355
movl -0x2ec(%rbp), %eax
cmpl $0xa, %eax
jne 0x16362
jmp 0x163b0
leaq 0x33e1f(%rip), %rdi     # 0x4a188
leaq 0x33d6c(%rip), %rsi     # 0x4a0dc
movl $0xeb, %edx
leaq 0x33e1e(%rip), %rcx     # 0x4a19a
callq 0x4240
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x46c8
leaq -0x268(%rbp), %rdi
callq 0x46c8
jmp 0x168a8
jmp 0x163b2
leaq -0x218(%rbp), %rdi
callq 0x4130
movl %eax, -0x2f0(%rbp)
jmp 0x163c6
movl -0x2f0(%rbp), %eax
cmpl $0xa, %eax
je 0x163fe
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x2f4(%rbp)
jmp 0x163e5
movl -0x2f4(%rbp), %eax
movsbl %al, %esi
leaq -0x288(%rbp), %rdi
callq 0x4220
jmp 0x163fc
jmp 0x163b2
leaq -0x218(%rbp), %rdi
leaq -0x28c(%rbp), %rsi
callq 0x4210
movq %rax, -0x300(%rbp)
jmp 0x1641a
movq -0x300(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0x4210
movq %rax, -0x308(%rbp)
jmp 0x16436
movq -0x308(%rbp), %rdi
leaq -0x294(%rbp), %rsi
callq 0x4210
jmp 0x1644b
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x30c(%rbp)
jmp 0x1645f
movl -0x30c(%rbp), %eax
cmpl $0xa, %eax
jne 0x1646c
jmp 0x1648b
leaq 0x33d15(%rip), %rdi     # 0x4a188
leaq 0x33c62(%rip), %rsi     # 0x4a0dc
movl $0xef, %edx
leaq 0x33d14(%rip), %rcx     # 0x4a19a
callq 0x4240
leaq 0x33d31(%rip), %rsi     # 0x4a1c3
leaq -0x268(%rbp), %rdi
callq 0xe570
movb %al, -0x30d(%rbp)
jmp 0x164a6
movb -0x30d(%rbp), %al
testb $0x1, %al
jne 0x164b2
jmp 0x164c2
leaq 0x33d0d(%rip), %rdi     # 0x4a1c6
callq 0x5360
jmp 0x164c0
jmp 0x164c2
leaq 0x33d3b(%rip), %rsi     # 0x4a204
leaq -0x268(%rbp), %rdi
callq 0xec60
movb %al, -0x30e(%rbp)
jmp 0x164dd
movb -0x30e(%rbp), %al
testb $0x1, %al
jne 0x164e9
jmp 0x164f9
leaq 0x33d17(%rip), %rdi     # 0x4a207
callq 0x5360
jmp 0x164f7
jmp 0x164f9
movq -0x2d8(%rbp), %rdi
movl -0x28c(%rbp), %esi
movl -0x290(%rbp), %edx
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
jmp 0x16517
movq -0x2d8(%rbp), %rax
movw $0x0, -0x296(%rbp)
movq 0x38(%rax), %rax
movq %rax, -0x2a0(%rbp)
cmpl $0xff, -0x294(%rbp)
jg 0x16687
movl $0x0, -0x2a8(%rbp)
movq -0x2d8(%rbp), %rcx
movl -0x2a8(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x16682
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x30f(%rbp)
jmp 0x16582
movb -0x30f(%rbp), %al
testb $0x1, %al
jne 0x1658e
jmp 0x16590
jmp 0x165af
leaq 0x33c9d(%rip), %rdi     # 0x4a234
leaq 0x33b3e(%rip), %rsi     # 0x4a0dc
movl $0x109, %edx            # imm = 0x109
leaq 0x33bf0(%rip), %rcx     # 0x4a19a
callq 0x4240
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x314(%rbp)
jmp 0x165c3
movl -0x314(%rbp), %eax
movb %al, -0x2a1(%rbp)
movzbl -0x2a1(%rbp), %eax
movzwl -0x296(%rbp), %ecx
cmpl %ecx, %eax
jle 0x165ef
movzbl -0x2a1(%rbp), %eax
movw %ax, -0x296(%rbp)
movzbl -0x2a1(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x33aae(%rip), %xmm1    # 0x4a0b0
divsd %xmm1, %xmm0
movq -0x2a0(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x2a0(%rbp)
movsd %xmm0, (%rax)
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x315(%rbp)
jmp 0x1663f
movb -0x315(%rbp), %al
testb $0x1, %al
jne 0x1664b
jmp 0x1664d
jmp 0x1666c
leaq 0x33be0(%rip), %rdi     # 0x4a234
leaq 0x33a81(%rip), %rsi     # 0x4a0dc
movl $0x10e, %edx            # imm = 0x10E
leaq 0x33b33(%rip), %rcx     # 0x4a19a
callq 0x4240
jmp 0x1666e
movl -0x2a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2a8(%rbp)
jmp 0x1654c
jmp 0x1680e
movl $0x0, -0x2bc(%rbp)
movq -0x2d8(%rbp), %rcx
movl -0x2bc(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x1680c
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x316(%rbp)
jmp 0x166c7
movb -0x316(%rbp), %al
testb $0x1, %al
jne 0x166d3
jmp 0x166d5
jmp 0x166f4
leaq 0x33b58(%rip), %rdi     # 0x4a234
leaq 0x339f9(%rip), %rsi     # 0x4a0dc
movl $0x117, %edx            # imm = 0x117
leaq 0x33aab(%rip), %rcx     # 0x4a19a
callq 0x4240
leaq -0x2aa(%rbp), %rax
movq %rax, -0x2b8(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x31c(%rbp)
jmp 0x16716
movl -0x31c(%rbp), %eax
movb %al, %cl
movq -0x2b8(%rbp), %rax
movq %rax, %rdx
incq %rdx
movq %rdx, -0x2b8(%rbp)
movb %cl, (%rax)
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x320(%rbp)
jmp 0x16748
movl -0x320(%rbp), %eax
movb %al, %cl
movq -0x2b8(%rbp), %rax
movb %cl, (%rax)
movzwl -0x2aa(%rbp), %eax
movzwl -0x296(%rbp), %ecx
cmpl %ecx, %eax
jle 0x16779
movw -0x2aa(%rbp), %ax
movw %ax, -0x296(%rbp)
movzwl -0x2aa(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x3391c(%rip), %xmm1    # 0x4a0a8
divsd %xmm1, %xmm0
movq -0x2a0(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x2a0(%rbp)
movsd %xmm0, (%rax)
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x321(%rbp)
jmp 0x167c9
movb -0x321(%rbp), %al
testb $0x1, %al
jne 0x167d5
jmp 0x167d7
jmp 0x167f6
leaq 0x33a56(%rip), %rdi     # 0x4a234
leaq 0x338f7(%rip), %rsi     # 0x4a0dc
movl $0x11e, %edx            # imm = 0x11E
leaq 0x339a9(%rip), %rcx     # 0x4a19a
callq 0x4240
jmp 0x167f8
movl -0x2bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2bc(%rbp)
jmp 0x16691
jmp 0x1680e
leaq -0x218(%rbp), %rdi
callq 0x4460
jmp 0x1681c
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x41b0
movb %al, -0x322(%rbp)
jmp 0x1683c
movb -0x322(%rbp), %al
testb $0x1, %al
jne 0x16848
jmp 0x1684a
jmp 0x16869
leaq 0x339e2(%rip), %rdi     # 0x4a233
leaq 0x33884(%rip), %rsi     # 0x4a0dc
movl $0x122, %edx            # imm = 0x122
leaq 0x33936(%rip), %rcx     # 0x4a19a
callq 0x4240
leaq -0x218(%rbp), %rdi
callq 0x40b0
jmp 0x16877
leaq -0x288(%rbp), %rdi
callq 0x46c8
leaq -0x268(%rbp), %rdi
callq 0x46c8
leaq -0x218(%rbp), %rdi
callq 0x40d0
movb $0x1, %al
andb $0x1, %al
addq $0x330, %rsp            # imm = 0x330
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x40d0
movq -0x220(%rbp), %rdi
callq 0x4620