// File: /workspace/github_cpp_makefile_star2_O2/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 0xb93b
movq 0x38(%rbx), %rax
movq (%rax), %rdi
movzbl %sil, %ebp
movl %ebp, %esi
movsd %xmm0, (%rsp)
callq 0xa7c2
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movsd (%rsp), %xmm0
movl %ebp, %esi
callq 0xa7c2
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdi
movsd (%rsp), %xmm0
movl %ebp, %esi
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0xa7c2
movl 0x30(%rbx), %eax
movq 0x38(%rbx), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
xorpd %xmm3, %xmm3
cmpq %rdx, %rax
je 0xb989
movq (%rcx), %rdi
movq 0x8(%rcx), %r8
movq 0x38(%rdi), %rdi
movsd (%rdi,%rdx,8), %xmm1
maxsd %xmm3, %xmm1
movq 0x38(%r8), %rdi
movsd (%rdi,%rdx,8), %xmm2
maxsd %xmm1, %xmm2
movq 0x10(%rcx), %rdi
movq 0x38(%rdi), %rdi
movsd (%rdi,%rdx,8), %xmm3
maxsd %xmm2, %xmm3
incq %rdx
jmp 0xb94d
ucomisd %xmm3, %xmm0
setbe %al
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm3
jne 0xb99c
jnp 0xb9b7
orb %sil, %al
je 0xb9b7
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