// File: /workspace/github_cpp_makefile_star2_O3/mhe[P]raytracerframework_cpp/image.cpp
// Function: Image::read_png(char*)
void Image::read_png(char* filename)
{
	std::vector<unsigned char> buffer, image;
	 //load the image file with given filename
	LodePNG::loadFile(buffer, filename);
	
	 //decode the png
	LodePNG::Decoder decoder;
	decoder.decode(image, buffer.empty() ? 0 : &buffer[0], (unsigned)buffer.size());
	cout << decoder.getChannels() << endl;
	cout << decoder.getBpp() << endl;
	
	if (decoder.getChannels()<3 || decoder.getBpp()<24) {
		cerr << "Error: only color (RGBA), 8 bit per channel png images are supported." << endl;
		cerr << "Either convert your image or change the sourcecode." << endl;
		exit(1);
	}
	int w = decoder.getWidth();
	int h = decoder.getHeight();
	set_extent(w,h);
	
	// now convert the image data
	std::vector<unsigned char>::iterator imageIterator = image.begin();
	Color *currentPixel = _pixel;
	while (imageIterator != image.end()) {
		currentPixel->r = (*imageIterator)/255.0;
		imageIterator++;
		currentPixel->g = (*imageIterator)/255.0;
		imageIterator++;
		currentPixel->b = (*imageIterator)/255.0;
		imageIterator++;
		// Let's just ignore the alpha channel
		imageIterator++; 
		currentPixel++;
	}	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x170, %rsp            # imm = 0x170
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, -0x50(%rbp)
xorl %eax, %eax
movq %rax, -0x40(%rbp)
movq %rax, -0x20(%rbp)
movapd %xmm0, -0x30(%rbp)
leaq -0x178(%rbp), %rdi
leaq -0x11(%rbp), %rdx
callq 0x19dea
leaq -0x50(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x21b8e
leaq -0x168(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x19b4e
movq -0x168(%rbp), %rsi
incq %rsi
callq 0x42d0
leaq -0x178(%rbp), %rdi
callq 0x216f2
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
xorl %edx, %edx
cmpq %rcx, %rax
cmovneq %rax, %rdx
subl %eax, %ecx
leaq -0x178(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x217a0
leaq -0x178(%rbp), %rdi
callq 0x21762
movl %eax, %esi
movq 0x2143e(%rip), %rdi     # 0x3afd0
callq 0x41d0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x42a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x4050
movq %rax, %rdi
callq 0x41e0
leaq -0x178(%rbp), %rdi
callq 0x21740
movl %eax, %esi
movq 0x213fa(%rip), %rdi     # 0x3afd0
callq 0x41d0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x42a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x4050
movq %rax, %rdi
callq 0x41e0
leaq -0x178(%rbp), %rdi
callq 0x21762
cmpl $0x3, %eax
jb 0x19cf6
leaq -0x178(%rbp), %rdi
callq 0x21740
cmpl $0x17, %eax
jbe 0x19cf6
leaq -0x178(%rbp), %rdi
callq 0x2172c
movl %eax, %r14d
leaq -0x178(%rbp), %rdi
callq 0x21736
movq %rbx, %rdi
movl %r14d, %esi
movl %eax, %edx
callq 0x1992c
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
je 0x19cb4
movq (%rbx), %rcx
movsd 0x16aac(%rip), %xmm0    # 0x30718
movzbl (%rax), %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rcx)
movzbl 0x1(%rax), %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rcx)
movzbl 0x2(%rax), %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rcx)
addq $0x4, %rax
addq $0x18, %rcx
cmpq -0x28(%rbp), %rax
jne 0x19c6c
leaq -0x178(%rbp), %rdi
callq 0x216fc
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x19cd5
movq -0x20(%rbp), %rsi
subq %rdi, %rsi
callq 0x42d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x19cea
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x42d0
addq $0x170, %rsp            # imm = 0x170
popq %rbx
popq %r14
popq %rbp
retq
movq 0x212f3(%rip), %rdi     # 0x3aff0
leaq 0x16a1c(%rip), %rsi     # 0x30720
movl $0x45, %edx
callq 0x4350
movq 0x212db(%rip), %rdi     # 0x3aff0
callq 0x4130
movq 0x212cf(%rip), %rdi     # 0x3aff0
leaq 0x16a3e(%rip), %rsi     # 0x30766
movl $0x33, %edx
callq 0x4350
movq 0x212b7(%rip), %rdi     # 0x3aff0
callq 0x4130
movl $0x1, %edi
callq 0x43a0
jmp 0x19d77
jmp 0x19d70
movq %rax, %rbx
leaq -0x168(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x19d86
movq -0x168(%rbp), %rsi
incq %rsi
callq 0x42d0
jmp 0x19d86
movq %rax, %rbx
jmp 0x19d86
jmp 0x19d77
movq %rax, %rbx
leaq -0x178(%rbp), %rdi
callq 0x216fc
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x19d9b
movq -0x20(%rbp), %rsi
subq %rdi, %rsi
callq 0x42d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x19db0
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4550