// File: /workspace/github_cpp_makefile_star2_O2/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::hist_equal_cumulative(char const*, char const*, int, int)
void ImageProc::hist_equal_cumulative(const char *infile, const char *outfile, int width, int height)
{
	//########### If image == bw #########
	// Image img_bw("bw", width, height);

	// img_bw.read_image(infile, width, height);

	// int histogram_bins[256] = {0};

	// for(int i = 0; i < height; i++)
	// 	for(int j = 0; j < width; j++)
	// 		if((img_bw(i, j, 0) >= 0) && (img_bw(i, j, 0) < 256))
	// 			histogram_bins[img_bw(i, j, 0)]++;


	// int hist_cumulative[256] = {0};
	// hist_cumulative[0] = histogram_bins[0];
	// for(int i = 1; i < 256; i++)
	// 	hist_cumulative[i] = hist_cumulative[i-1] + histogram_bins[i];


	// for(int i = 0; i < height; i++)
	// 	for(int j = 0; j < width; j++)
	// 		if      (img_bw(i, j, 0) < 0  ) img_bw.imgdata[(i)*width*1 + (j)*1 + 0] = 0  ;
	// 		else if (img_bw(i, j, 0) > 255) img_bw.imgdata[(i)*width*1 + (j)*1 + 0] = 255;
	// 		else    img_bw.imgdata[(i)*width*1 + (j)*1 + 0] = 255 * hist_cumulative[img_bw(i,j,0)] / hist_cumulative[255];


	// img_bw.write_image(outfile, width, height);
	//###################################


	//######## If image == color ########
	Image img_color("color", width, height);

	img_color.read_image(infile, width, height);

	int histogram_bins[256][3] = {0};

	for(int i = 0; i < img_color.rows; i++)
		for(int j = 0; j < img_color.cols; j++)
			for(int k = 0; k < img_color.channels; k++)
				if((img_color.getvalue(i,j,k) >= 0) && (img_color.getvalue(i,j,k) < 256))
					histogram_bins[img_color.getvalue(i,j,k)][k]++;


	int hist_cumulative[256][3] = {0};
	for(int k = 0; k < 3; k++)
		hist_cumulative[0][k] = histogram_bins[0][k];
	for(int i = 1; i < 256; i++)
		for(int k = 0; k < 3; k++)
			hist_cumulative[i][k] = hist_cumulative[i-1][k] + histogram_bins[i][k];

	// std::ofstream file;

	// file.open("rt.txt");
	// for(int i = 0; i < 256; i++)
	// 	file << i << "\t" <<hist_cumulative[i][0] << "\n";
	// file.close();

	// file.open("gt.txt");
	// for(int i = 0; i < 256; i++)
	// 	file << i << "\t" <<hist_cumulative[i][1] << "\n";
	// file.close();

	// file.open("bt.txt");
	// for(int i = 0; i < 256; i++)
	// 	file << i << "\t" <<hist_cumulative[i][2] << "\n";
	// file.close();


	for(int i = 0; i < height; i++)
		for(int j = 0; j < width; j++)
			for(int k = 0; k < 3; k++)
				if      (img_color.getvalue(i,j,k) < 0  ) img_color.setvalue(i,j,k,0)  ;
				else if (img_color.getvalue(i,j,k) > 255) img_color.setvalue(i,j,k,255);
				else    img_color.setvalue(i,j,k,(255 * hist_cumulative[img_color.getvalue(i,j,k)][k] / hist_cumulative[255][k]));


	img_color.write_image(outfile, width, height);
	//###################################
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1838, %rsp           # imm = 0x1838
movl %edx, %r14d
movq %rsi, 0x8(%rsp)
movq %rdi, %r15
leaq 0x9fad(%rip), %rsi      # 0x106c8
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movl %ecx, 0x4(%rsp)
callq 0x3fd6
movq %r12, %rdi
movq %r15, %rsi
callq 0x4032
leaq 0xc30(%rsp), %rdi
xorl %r15d, %r15d
movl $0xc00, %edx            # imm = 0xC00
xorl %esi, %esi
callq 0x20e0
movl 0x8(%r12), %r13d
movl 0xc(%r12), %eax
movl 0x10(%r12), %ebx
movq 0x18(%r12), %r12
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
testl %r13d, %r13d
movl $0x0, %edx
cmovgl %r13d, %edx
testl %eax, %eax
cmovlel %r15d, %eax
imull %ebx, %r13d
xorl %esi, %esi
cmpl %eax, %esi
je 0x67cb
movl %r15d, %edi
xorl %r8d, %r8d
cmpq %rdx, %r8
je 0x67c4
movslq %edi, %r9
addq %r12, %r9
xorl %r10d, %r10d
cmpq %r10, %rcx
je 0x67bd
movzbl (%r9,%r10), %r11d
imulq $0xc, %r11, %r11
addq %rsp, %r11
addq $0xc30, %r11            # imm = 0xC30
incl (%r11,%r10,4)
incq %r10
jmp 0x679c
incq %r8
addl %ebx, %edi
jmp 0x678e
incl %esi
addl %r13d, %r15d
jmp 0x6784
leaq 0x30(%rsp), %r15
xorl %ebp, %ebp
movl $0xc00, %edx            # imm = 0xC00
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movl 0x4(%rsp), %r11d
cmpq $0x3, %rbp
je 0x67fc
movl 0xc30(%rsp,%rbp,4), %eax
movl %eax, 0x30(%rsp,%rbp,4)
incq %rbp
jmp 0x67e6
pushq $0x1
popq %rax
leaq 0xc3c(%rsp), %rcx
cmpq $0x100, %rax            # imm = 0x100
je 0x6835
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x6828
movl (%rcx,%rdx,4), %esi
addl (%r15,%rdx,4), %esi
movl %esi, 0xc(%r15,%rdx,4)
incq %rdx
jmp 0x6811
incq %rax
addq $0xc, %r15
addq $0xc, %rcx
jmp 0x6807
xorl %ecx, %ecx
testl %r14d, %r14d
cmovlel %ecx, %r14d
testl %r11d, %r11d
cmovlel %ecx, %r11d
xorl %esi, %esi
cmpl %r11d, %esi
je 0x689d
movl %ecx, %edi
xorl %r8d, %r8d
cmpq %r14, %r8
je 0x6896
movslq %edi, %r9
addq %r12, %r9
xorl %r10d, %r10d
cmpq $0x3, %r10
je 0x688f
movzbl (%r9,%r10), %eax
imulq $0xc, %rax, %rax
leaq (%rsp,%r10,4), %rdx
addq $0x30, %rdx
imull $0xff, (%rax,%rdx), %eax
cltd
idivl 0xc24(%rsp,%r10,4)
movb %al, (%r9,%r10)
incq %r10
jmp 0x685f
incq %r8
addl %ebx, %edi
jmp 0x6851
incl %esi
addl %r13d, %ecx
jmp 0x6847
leaq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x411c
addq $0x1838, %rsp           # imm = 0x1838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq