// File: 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
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1f8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x22d30
leaq -0x40(%rbp), %rdi
callq 0x22d30
movq -0x10(%rbp), %rax
movq %rax, -0x1f0(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x56d0
movq -0x1f0(%rbp), %rsi
movq -0x1e8(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x20460
jmp 0x22904
leaq -0x28(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2c6e0
jmp 0x22913
leaq -0x60(%rbp), %rdi
callq 0x5788
leaq -0x61(%rbp), %rdi
callq 0x5420
leaq -0x1a0(%rbp), %rdi
callq 0x2bcb0
jmp 0x22933
leaq -0x28(%rbp), %rdi
callq 0x22f40
testb $0x1, %al
jne 0x22942
jmp 0x2294d
xorl %eax, %eax
movq %rax, -0x200(%rbp)
jmp 0x22961
leaq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x22f80
movq %rax, -0x200(%rbp)
movq -0x200(%rbp), %rax
movq %rax, -0x208(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x22fa0
movq -0x208(%rbp), %rdx
movl %eax, %eax
movl %eax, %ecx
leaq -0x1a0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x2be70
jmp 0x22995
leaq -0x1a0(%rbp), %rdi
callq 0x2bde0
movl %eax, -0x20c(%rbp)
jmp 0x229a9
movl -0x20c(%rbp), %esi
movq 0x4561a(%rip), %rdi     # 0x67fd0
callq 0x5580
movq %rax, -0x218(%rbp)
jmp 0x229c4
movq -0x218(%rbp), %rdi
movq 0x455de(%rip), %rsi     # 0x67fb0
callq 0x53f0
jmp 0x229d9
leaq -0x1a0(%rbp), %rdi
callq 0x2bdb0
movl %eax, -0x21c(%rbp)
jmp 0x229ed
movl -0x21c(%rbp), %esi
movq 0x455d6(%rip), %rdi     # 0x67fd0
callq 0x5580
movq %rax, -0x228(%rbp)
jmp 0x22a08
movq -0x228(%rbp), %rdi
movq 0x4559a(%rip), %rsi     # 0x67fb0
callq 0x53f0
jmp 0x22a1d
leaq -0x1a0(%rbp), %rdi
callq 0x2bde0
movl %eax, -0x22c(%rbp)
jmp 0x22a31
movl -0x22c(%rbp), %eax
cmpl $0x3, %eax
jb 0x22a5f
leaq -0x1a0(%rbp), %rdi
callq 0x2bdb0
movl %eax, -0x230(%rbp)
jmp 0x22a50
movl -0x230(%rbp), %eax
cmpl $0x18, %eax
jae 0x22b2a
movq 0x4558a(%rip), %rdi     # 0x67ff0
leaq 0x28cbb(%rip), %rsi     # 0x4b728
callq 0x5390
movq %rax, -0x238(%rbp)
jmp 0x22a7b
movq -0x238(%rbp), %rdi
movq 0x45527(%rip), %rsi     # 0x67fb0
callq 0x53f0
jmp 0x22a90
movq 0x45559(%rip), %rdi     # 0x67ff0
leaq 0x28cd0(%rip), %rsi     # 0x4b76e
callq 0x5390
movq %rax, -0x240(%rbp)
jmp 0x22aac
movq -0x240(%rbp), %rdi
movq 0x454f6(%rip), %rsi     # 0x67fb0
callq 0x53f0
jmp 0x22ac1
movl $0x1, %edi
callq 0x54d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x22aee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5788
leaq -0x61(%rbp), %rdi
callq 0x5420
jmp 0x22ce6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x22ce6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x2bcd0
jmp 0x22ce6
leaq -0x1a0(%rbp), %rdi
callq 0x2bd70
movl %eax, -0x244(%rbp)
jmp 0x22b3e
movl -0x244(%rbp), %eax
movl %eax, -0x1a4(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x2bd90
movl %eax, -0x248(%rbp)
jmp 0x22b5e
movq -0x1f8(%rbp), %rdi
movl -0x248(%rbp), %eax
movl %eax, -0x1a8(%rbp)
movl -0x1a4(%rbp), %esi
movl -0x1a8(%rbp), %edx
callq 0x224b0
jmp 0x22b84
leaq -0x40(%rbp), %rdi
callq 0x22de0
movq %rax, %rcx
movq -0x1f8(%rbp), %rax
movq %rcx, -0x1b0(%rbp)
movq (%rax), %rax
movq %rax, -0x1b8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x22e50
movq %rax, -0x1c0(%rbp)
leaq -0x1b0(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x22e10
testb $0x1, %al
jne 0x22bd4
jmp 0x22cbf
leaq -0x1b0(%rbp), %rdi
callq 0x22e80
movzbl (%rax), %eax
cvtsi2sd %eax, %xmm0
movsd 0x28b31(%rip), %xmm1    # 0x4b720
divsd %xmm1, %xmm0
movq -0x1b8(%rbp), %rax
movsd %xmm0, (%rax)
leaq -0x1b0(%rbp), %rdi
xorl %esi, %esi
callq 0x22ea0
movq %rax, -0x1c8(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x22e80
movzbl (%rax), %eax
cvtsi2sd %eax, %xmm0
movsd 0x28af2(%rip), %xmm1    # 0x4b720
divsd %xmm1, %xmm0
movq -0x1b8(%rbp), %rax
movsd %xmm0, 0x8(%rax)
leaq -0x1b0(%rbp), %rdi
xorl %esi, %esi
callq 0x22ea0
movq %rax, -0x1d0(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x22e80
movzbl (%rax), %eax
cvtsi2sd %eax, %xmm0
movsd 0x28ab2(%rip), %xmm1    # 0x4b720
divsd %xmm1, %xmm0
movq -0x1b8(%rbp), %rax
movsd %xmm0, 0x10(%rax)
leaq -0x1b0(%rbp), %rdi
xorl %esi, %esi
callq 0x22ea0
movq %rax, -0x1d8(%rbp)
leaq -0x1b0(%rbp), %rdi
xorl %esi, %esi
callq 0x22ea0
movq %rax, -0x1e0(%rbp)
movq -0x1b8(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x1b8(%rbp)
jmp 0x22ba8
leaq -0x1a0(%rbp), %rdi
callq 0x2bcd0
leaq -0x40(%rbp), %rdi
callq 0x22ee0
leaq -0x28(%rbp), %rdi
callq 0x22ee0
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x22ee0
leaq -0x28(%rbp), %rdi
callq 0x22ee0
movq -0x70(%rbp), %rdi
callq 0x56c0
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)