// File: 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
movq %rsp, %rbp
subq $0x350, %rsp            # imm = 0x350
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
addq $0x8, %rdi
movq %rdi, -0x2c8(%rbp)
callq 0x4060
leaq -0x218(%rbp), %rdi
movq %rdi, -0x2c0(%rbp)
callq 0x4420
movq -0x2c8(%rbp), %rdi
callq 0x4160
movq -0x2c0(%rbp), %rdi
movq %rax, %rsi
movl $0x8, %edx
callq 0x4230
jmp 0x17d23
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x41b0
movb %al, -0x2d1(%rbp)
jmp 0x17d43
movb -0x2d1(%rbp), %al
testb $0x1, %al
jne 0x17d4f
jmp 0x17dcd
movq -0x2d0(%rbp), %rdx
addq $0x8, %rdx
leaq 0x323f7(%rip), %rsi     # 0x4a158
leaq -0x248(%rbp), %rdi
callq 0xe460
jmp 0x17d6f
leaq -0x248(%rbp), %rdi
callq 0x4160
movq %rax, %rdi
callq 0x5360
jmp 0x17d85
leaq -0x248(%rbp), %rdi
callq 0x46c8
jmp 0x17dcd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x1840c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x46c8
jmp 0x1840c
leaq -0x268(%rbp), %rdi
movq %rdi, -0x2e0(%rbp)
callq 0x4500
leaq -0x288(%rbp), %rdi
callq 0x4500
movq -0x2e0(%rbp), %rsi
leaq -0x218(%rbp), %rdi
callq 0x44b0
jmp 0x17e01
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x2e4(%rbp)
jmp 0x17e15
movl -0x2e4(%rbp), %eax
cmpl $0xa, %eax
jne 0x17e22
jmp 0x17e70
leaq 0x3235f(%rip), %rdi     # 0x4a188
leaq 0x3252a(%rip), %rsi     # 0x4a35a
movl $0xdb, %edx
leaq 0x32589(%rip), %rcx     # 0x4a3c5
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 0x1840c
jmp 0x17e72
leaq -0x218(%rbp), %rdi
callq 0x4130
movl %eax, -0x2e8(%rbp)
jmp 0x17e86
movl -0x2e8(%rbp), %eax
cmpl $0xa, %eax
je 0x17ebe
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x2ec(%rbp)
jmp 0x17ea5
movl -0x2ec(%rbp), %eax
movsbl %al, %esi
leaq -0x288(%rbp), %rdi
callq 0x4220
jmp 0x17ebc
jmp 0x17e72
leaq -0x218(%rbp), %rdi
leaq -0x28c(%rbp), %rsi
callq 0x4210
movq %rax, -0x2f8(%rbp)
jmp 0x17eda
movq -0x2f8(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0x4210
movq %rax, -0x300(%rbp)
jmp 0x17ef6
movq -0x300(%rbp), %rdi
leaq -0x294(%rbp), %rsi
callq 0x4210
jmp 0x17f0b
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x304(%rbp)
jmp 0x17f1f
movl -0x304(%rbp), %eax
cmpl $0xa, %eax
jne 0x17f2c
jmp 0x17f4b
leaq 0x32255(%rip), %rdi     # 0x4a188
leaq 0x32420(%rip), %rsi     # 0x4a35a
movl $0xdf, %edx
leaq 0x3247f(%rip), %rcx     # 0x4a3c5
callq 0x4240
leaq 0x32271(%rip), %rsi     # 0x4a1c3
leaq -0x268(%rbp), %rdi
callq 0xe570
movb %al, -0x305(%rbp)
jmp 0x17f66
movb -0x305(%rbp), %al
testb $0x1, %al
jne 0x17f72
jmp 0x17f82
leaq 0x3247a(%rip), %rdi     # 0x4a3f3
callq 0x5360
jmp 0x17f80
jmp 0x17f82
leaq 0x324a8(%rip), %rsi     # 0x4a431
leaq -0x268(%rbp), %rdi
callq 0xec60
movb %al, -0x306(%rbp)
jmp 0x17f9d
movb -0x306(%rbp), %al
testb $0x1, %al
jne 0x17fa9
jmp 0x17fb9
leaq 0x32484(%rip), %rdi     # 0x4a434
callq 0x5360
jmp 0x17fb7
jmp 0x17fb9
movq -0x2d0(%rbp), %rdi
movl -0x28c(%rbp), %esi
movl -0x290(%rbp), %edx
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
jmp 0x17fd7
movw $0x0, -0x296(%rbp)
cmpl $0xff, -0x294(%rbp)
jg 0x18190
movl $0x0, -0x29c(%rbp)
movq -0x2d0(%rbp), %rcx
movl -0x29c(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x1818b
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x307(%rbp)
jmp 0x18030
movb -0x307(%rbp), %al
testb $0x1, %al
jne 0x1803c
jmp 0x1803e
jmp 0x1805d
leaq 0x321ef(%rip), %rdi     # 0x4a234
leaq 0x3230e(%rip), %rsi     # 0x4a35a
movl $0xf7, %edx
leaq 0x3236d(%rip), %rcx     # 0x4a3c5
callq 0x4240
movl $0x0, -0x2a0(%rbp)
cmpl $0x3, -0x2a0(%rbp)
jge 0x18128
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x30c(%rbp)
jmp 0x18088
movl -0x30c(%rbp), %eax
movb %al, -0x297(%rbp)
movzbl -0x297(%rbp), %eax
movzwl -0x296(%rbp), %ecx
cmpl %ecx, %eax
jle 0x180b4
movzbl -0x297(%rbp), %eax
movw %ax, -0x296(%rbp)
movq -0x2d0(%rbp), %rdi
movzbl -0x297(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x31fe2(%rip), %xmm1    # 0x4a0b0
divsd %xmm1, %xmm0
movsd %xmm0, -0x320(%rbp)
addq $0x38, %rdi
movslq -0x2a0(%rbp), %rsi
callq 0xf8c0
movq (%rax), %rdi
movl -0x29c(%rbp), %esi
callq 0xef70
movq %rax, -0x318(%rbp)
jmp 0x18101
movq -0x318(%rbp), %rax
movsd -0x320(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x2a0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2a0(%rbp)
jmp 0x18067
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x321(%rbp)
jmp 0x18148
movb -0x321(%rbp), %al
testb $0x1, %al
jne 0x18154
jmp 0x18156
jmp 0x18175
leaq 0x320d7(%rip), %rdi     # 0x4a234
leaq 0x321f6(%rip), %rsi     # 0x4a35a
movl $0x101, %edx            # imm = 0x101
leaq 0x32255(%rip), %rcx     # 0x4a3c5
callq 0x4240
jmp 0x18177
movl -0x29c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x29c(%rbp)
jmp 0x17ffa
jmp 0x18372
movl $0x0, -0x2b4(%rbp)
movq -0x2d0(%rbp), %rcx
movl -0x2b4(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x18370
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x322(%rbp)
jmp 0x181d0
movb -0x322(%rbp), %al
testb $0x1, %al
jne 0x181dc
jmp 0x181de
jmp 0x181fd
leaq 0x3204f(%rip), %rdi     # 0x4a234
leaq 0x3216e(%rip), %rsi     # 0x4a35a
movl $0x10a, %edx            # imm = 0x10A
leaq 0x321cd(%rip), %rcx     # 0x4a3c5
callq 0x4240
movl $0x0, -0x2b8(%rbp)
cmpl $0x3, -0x2b8(%rbp)
jge 0x1830d
leaq -0x2a2(%rbp), %rax
movq %rax, -0x2b0(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x328(%rbp)
jmp 0x18236
movl -0x328(%rbp), %eax
movb %al, %cl
movq -0x2b0(%rbp), %rax
movq %rax, %rdx
incq %rdx
movq %rdx, -0x2b0(%rbp)
movb %cl, (%rax)
leaq -0x218(%rbp), %rdi
callq 0x4460
movl %eax, -0x32c(%rbp)
jmp 0x18268
movl -0x32c(%rbp), %eax
movb %al, %cl
movq -0x2b0(%rbp), %rax
movb %cl, (%rax)
movzwl -0x2a2(%rbp), %eax
movzwl -0x296(%rbp), %ecx
cmpl %ecx, %eax
jle 0x18299
movw -0x2a2(%rbp), %ax
movw %ax, -0x296(%rbp)
movq -0x2d0(%rbp), %rdi
movzwl -0x2a2(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x31df5(%rip), %xmm1    # 0x4a0a8
divsd %xmm1, %xmm0
movsd %xmm0, -0x340(%rbp)
addq $0x38, %rdi
movslq -0x2b8(%rbp), %rsi
callq 0xf8c0
movq (%rax), %rdi
movl -0x2b4(%rbp), %esi
callq 0xef70
movq %rax, -0x338(%rbp)
jmp 0x182e6
movq -0x338(%rbp), %rax
movsd -0x340(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x2b8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b8(%rbp)
jmp 0x18207
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x341(%rbp)
jmp 0x1832d
movb -0x341(%rbp), %al
testb $0x1, %al
jne 0x18339
jmp 0x1833b
jmp 0x1835a
leaq 0x31ef2(%rip), %rdi     # 0x4a234
leaq 0x32011(%rip), %rsi     # 0x4a35a
movl $0x116, %edx            # imm = 0x116
leaq 0x32070(%rip), %rcx     # 0x4a3c5
callq 0x4240
jmp 0x1835c
movl -0x2b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b4(%rbp)
jmp 0x1819a
jmp 0x18372
leaq -0x218(%rbp), %rdi
callq 0x4460
jmp 0x18380
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x41b0
movb %al, -0x342(%rbp)
jmp 0x183a0
movb -0x342(%rbp), %al
testb $0x1, %al
jne 0x183ac
jmp 0x183ae
jmp 0x183cd
leaq 0x31e7e(%rip), %rdi     # 0x4a233
leaq 0x31f9e(%rip), %rsi     # 0x4a35a
movl $0x11a, %edx            # imm = 0x11A
leaq 0x31ffd(%rip), %rcx     # 0x4a3c5
callq 0x4240
leaq -0x218(%rbp), %rdi
callq 0x40b0
jmp 0x183db
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 $0x350, %rsp            # imm = 0x350
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x40d0
movq -0x220(%rbp), %rdi
callq 0x4620
nopw %cs:(%rax,%rax)