// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rbx
pushq %rax
movq %rdi, %rbx
testl %edx, %edx
je 0xe56d
movq 0x38(%rbx), %rax
movq (%rax), %rdi
movzbl %sil, %ebp
movl %ebp, %esi
movsd %xmm0, (%rsp)
callq 0xd158
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movsd (%rsp), %xmm0
movl %ebp, %esi
callq 0xd158
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdi
movsd (%rsp), %xmm0
movl %ebp, %esi
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0xd158
movslq 0x30(%rbx), %rax
testq %rax, %rax
jle 0xe5ec
movq 0x38(%rbx), %rdi
movq (%rdi), %rcx
movq 0x8(%rdi), %rdx
movq 0x38(%rcx), %rcx
movq 0x38(%rdx), %rdx
movq 0x10(%rdi), %rdi
movq 0x38(%rdi), %rdi
xorpd %xmm3, %xmm3
xorl %r8d, %r8d
movsd (%rcx,%r8,8), %xmm1
maxsd %xmm3, %xmm1
movsd (%rdx,%r8,8), %xmm2
maxsd %xmm1, %xmm2
movsd (%rdi,%r8,8), %xmm3
maxsd %xmm2, %xmm3
incq %r8
cmpq %r8, %rax
jne 0xe598
ucomisd %xmm3, %xmm0
setbe %al
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm3
jne 0xe5d1
jnp 0xe5ec
orb %sil, %al
je 0xe5ec
divsd %xmm3, %xmm0
movq (%rbx), %rax
movq 0x30(%rax), %rax
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %rbp
jmpq *%rax
addq $0x8, %rsp
popq %rbx
popq %rbp
retq
nop