// File: kebwi[P]Trifid/src/Image3ChnlReal.cpp
// Function: Image3ChnlReal::normalize(double, bool, bool)
void Image3ChnlReal::normalize(Float_t maxVal, bool normalizeUpToo, bool independentChannels)
{
	if (independentChannels)
	{
		img_[0]->normalize(maxVal, normalizeUpToo);
		img_[1]->normalize(maxVal, normalizeUpToo);
		img_[2]->normalize(maxVal, normalizeUpToo);
	}
	else
	{
		Float_t maxCurrentVal = 0;
		for (int i = 0; i < totalPixels_; i++)
		{
			if ((*img_[0])[i] > maxCurrentVal)
				maxCurrentVal = (*img_[0])[i];
			if ((*img_[1])[i] > maxCurrentVal)
				maxCurrentVal = (*img_[1])[i];
			if ((*img_[2])[i] > maxCurrentVal)
				maxCurrentVal = (*img_[2])[i];
		}
		//cout << "maxCurrentVal before RGB normalization: " << maxCurrentVal << endl;
		/*
		int minCurrentVal = 999999;
		for (int i = 0; i < totalPixels_; i++)
		{
			if ((*img_[0])[i] < minCurrentVal)
				minCurrentVal = (*img_[0])[i];
			if ((*img_[1])[i] < minCurrentVal)
				minCurrentVal = (*img_[1])[i];
			if ((*img_[2])[i] < minCurrentVal)
				minCurrentVal = (*img_[2])[i];
		}
		cout << "minCurrentVal before RGB normalization: " << minCurrentVal << endl;
		*/
		if (maxCurrentVal < maxVal && !normalizeUpToo)
			return;
		if (maxCurrentVal == 0)
			return;
		
		Float_t scalar = maxVal / maxCurrentVal;
		
		scale(scalar);
		/*
		maxCurrentVal = 0;
		for (int i = 0; i < totalPixels_; i++)
		{
			if ((*img_[0])[i] > maxCurrentVal)
				maxCurrentVal = (*img_[0])[i];
			if ((*img_[1])[i] > maxCurrentVal)
				maxCurrentVal = (*img_[1])[i];
			if ((*img_[2])[i] > maxCurrentVal)
				maxCurrentVal = (*img_[2])[i];
		}
		cout << "maxCurrentVal after RGB normalization: " << maxCurrentVal << endl;
		minCurrentVal = 999999;
		for (int i = 0; i < totalPixels_; i++)
		{
			if ((*img_[0])[i] < minCurrentVal)
				minCurrentVal = (*img_[0])[i];
			if ((*img_[1])[i] < minCurrentVal)
				minCurrentVal = (*img_[1])[i];
			if ((*img_[2])[i] < minCurrentVal)
				minCurrentVal = (*img_[2])[i];
		}
		cout << "minCurrentVal after RGB normalization: " << minCurrentVal << endl;
		*/
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %dl, %al
movb %sil, %cl
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
andb $0x1, %cl
movb %cl, -0x11(%rbp)
andb $0x1, %al
movb %al, -0x12(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
testb $0x1, -0x12(%rbp)
je 0x17b58
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xf8c0
movq (%rax), %rdi
movsd -0x10(%rbp), %xmm0
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x15a10
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
movl $0x1, %esi
callq 0xf8c0
movq (%rax), %rdi
movsd -0x10(%rbp), %xmm0
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x15a10
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
movl $0x2, %esi
callq 0xf8c0
movq (%rax), %rdi
movsd -0x10(%rbp), %xmm0
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x15a10
jmp 0x17cb7
xorps %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x38(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x17c6f
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xf8c0
movq (%rax), %rdi
movl -0x24(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
ucomisd -0x20(%rbp), %xmm0
jbe 0x17bc3
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xf8c0
movq (%rax), %rdi
movl -0x24(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
movl $0x1, %esi
callq 0xf8c0
movq (%rax), %rdi
movl -0x24(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
ucomisd -0x20(%rbp), %xmm0
jbe 0x17c11
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
movl $0x1, %esi
callq 0xf8c0
movq (%rax), %rdi
movl -0x24(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
movl $0x2, %esi
callq 0xf8c0
movq (%rax), %rdi
movl -0x24(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
ucomisd -0x20(%rbp), %xmm0
jbe 0x17c5f
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
movl $0x2, %esi
callq 0xf8c0
movq (%rax), %rdi
movl -0x24(%rbp), %esi
callq 0xef70
movsd (%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x17c61
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x17b67
movsd -0x20(%rbp), %xmm1
movsd -0x10(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x17c87
testb $0x1, -0x11(%rbp)
jne 0x17c87
jmp 0x17cb7
movsd -0x20(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x17c99
jp 0x17c99
jmp 0x17cb7
movq -0x38(%rbp), %rdi
movsd -0x10(%rbp), %xmm0
divsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
movq (%rdi), %rax
callq *0x30(%rax)
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)