// File: /workspace/github_cpp_makefile_star2_O3/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 %ecx, %ebp
movl %edx, %r14d
movq %rsi, 0x8(%rsp)
movq %rdi, %r15
leaq 0x9fa1(%rip), %rsi      # 0x116c8
leaq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x4c56
movq %r12, %rdi
movq %r15, %rsi
callq 0x4cb2
leaq 0xc30(%rsp), %rdi
xorl %r15d, %r15d
movl $0xc00, %edx            # imm = 0xC00
xorl %esi, %esi
callq 0x20c0
movslq 0xc(%r12), %rax
testq %rax, %rax
jle 0x77c4
movl 0x18(%rsp), %ecx
movl 0x20(%rsp), %edx
movq 0x28(%rsp), %rsi
movl %ecx, %edi
imull %edx, %edi
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0x77b9
movl %r15d, %r9d
xorl %r10d, %r10d
testl %edx, %edx
jle 0x77ae
movslq %r9d, %r11
addq %rsi, %r11
xorl %r12d, %r12d
movzbl (%r11,%r12), %r13d
leaq (%r13,%r13,2), %r13
leaq (%rsp,%r13,4), %r13
addq $0xc30, %r13            # imm = 0xC30
incl (%r13,%r12,4)
incq %r12
cmpq %r12, %rdx
jne 0x778c
incq %r10
addl %edx, %r9d
cmpq %rcx, %r10
jne 0x777f
incq %r8
addl %edi, %r15d
cmpq %rax, %r8
jne 0x7775
leaq 0x3c(%rsp), %r15
movl $0xbf4, %edx            # imm = 0xBF4
movq %r15, %rdi
xorl %esi, %esi
callq 0x20c0
leaq 0xc3c(%rsp), %rax
movq -0xc(%rax), %rcx
movq %rcx, -0xc(%r15)
movl -0x4(%rax), %ecx
movl %ecx, -0x4(%r15)
movl $0x1, %ecx
leaq 0x30(%rsp), %rdx
xorl %esi, %esi
movl (%rax,%rsi,4), %edi
addl (%rdx,%rsi,4), %edi
movl %edi, 0xc(%rdx,%rsi,4)
incq %rsi
cmpq $0x3, %rsi
jne 0x77fb
incq %rcx
addq $0xc, %rdx
addq $0xc, %rax
cmpq $0x100, %rcx            # imm = 0x100
jne 0x77f9
testl %ebp, %ebp
jle 0x789a
movq 0x28(%rsp), %rcx
movl 0x20(%rsp), %esi
movl %r14d, %edi
movl 0x18(%rsp), %r8d
imull %esi, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
testl %r14d, %r14d
jle 0x788f
movl %r9d, %r11d
xorl %r15d, %r15d
movslq %r11d, %r12
addq %rcx, %r12
xorl %r13d, %r13d
movzbl (%r12,%r13), %eax
leaq (%rax,%rax,2), %rax
leaq (%rsp,%r13,4), %rbx
addq $0x30, %rbx
movl (%rbx,%rax,4), %edx
movl %edx, %eax
shll $0x8, %eax
subl %edx, %eax
cltd
idivl 0xbf4(%rbx)
movb %al, (%r12,%r13)
incq %r13
cmpq $0x3, %r13
jne 0x7855
incq %r15
addl %esi, %r11d
cmpq %rdi, %r15
jne 0x784c
incl %r10d
addl %r8d, %r9d
cmpl %ebp, %r10d
jne 0x7841
leaq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x4dee
addq $0x1838, %rsp           # imm = 0x1838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop