// File: /workspace/github_cpp_makefile_star2_O1/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::hist_equal(char const*, char const*, int, int)
void ImageProc::hist_equal(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];

	// int hist_cumulative_copy[256] = {0};
	// int curr_bin_count = 0, prev_bin_count = 0;
	// double value =  0;
	// 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
	// 		{
	// 			++hist_cumulative_copy[img_bw(i, j, 0)];
	// 			curr_bin_count = hist_cumulative_copy[img_bw(i, j, 0)];
	// 			prev_bin_count = hist_cumulative[img_bw(i, j, 0) - 1];
	// 			value = floor(255.0 * (prev_bin_count + curr_bin_count) / hist_cumulative[255]);
	// 			img_bw.imgdata[(i)*width*1 + (j)*1 + 0] = value;
	// 			value = 0; curr_bin_count = 0; prev_bin_count = 0;
	// 		}

	// 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(i, j, k) >= 0) && (img_color(i, j, k) < 256))
					histogram_bins[img_color(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("rt2.txt");
	// for(int i = 0; i < 256; i++)
	// 	file << i << "\t" <<hist_cumulative[i][0] << "\n";
	// file.close();

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

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

	int hist_cumulative_copy[256][3] = {0};
	int curr_bin_count = 0, prev_bin_count = 0;
	double value =  0;

	for(int i = 0; i < img_color.rows; i++)
		for(int j = 0; j < img_color.cols; 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
				{
					++hist_cumulative_copy[img_color(i, j, k)][k];
					curr_bin_count = hist_cumulative_copy[img_color(i, j, k)][k];
					prev_bin_count = hist_cumulative[img_color(i, j, k) - 1][k];
					value = floor(255.0 * (prev_bin_count + curr_bin_count) / hist_cumulative[255][k]);
					img_color.setvalue(i,j,k,value);
					value = 0; curr_bin_count = 0; prev_bin_count = 0;
				}


	img_color.write_image(outfile, img_color.cols, img_color.rows);
	//###################################
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2458, %rsp           # imm = 0x2458
movq %rsi, 0x20(%rsp)
movq %rdi, %r14
leaq 0x9ce4(%rip), %rsi      # 0x1165c
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x4c6e
movq %r15, %rdi
movq %r14, %rsi
callq 0x4cde
leaq 0xc50(%rsp), %rdi
xorl %r14d, %r14d
movl $0xc00, %edx            # imm = 0xC00
xorl %esi, %esi
callq 0x20c0
movslq 0xc(%r15), %rcx
movq %rcx, (%rsp)
testq %rcx, %rcx
jle 0x7a0a
leaq 0x30(%rsp), %r15
movl 0x8(%r15), %ebx
movl 0x10(%r15), %r13d
testl %ebx, %ebx
jle 0x7a01
xorl %ebp, %ebp
testl %r13d, %r13d
jle 0x79fb
xorl %r12d, %r12d
movq %r15, %rdi
movl %r14d, %esi
movl %ebp, %edx
movl %r12d, %ecx
callq 0x4f14
movl %eax, %eax
leaq (%rax,%rax,2), %rax
leaq (%rsp,%rax,4), %rax
addq $0xc50, %rax            # imm = 0xC50
incl (%rax,%r12,4)
incq %r12
cmpq %r12, %r13
jne 0x79cf
incl %ebp
cmpl %ebx, %ebp
jne 0x79c7
incl %r14d
cmpl (%rsp), %r14d
jne 0x79c1
leaq 0x5c(%rsp), %r14
movl $0xbf4, %edx            # imm = 0xBF4
movq %r14, %rdi
xorl %esi, %esi
callq 0x20c0
leaq 0xc5c(%rsp), %rax
movq -0xc(%rax), %rcx
movq %rcx, -0xc(%r14)
movl -0x4(%rax), %ecx
movl %ecx, -0x4(%r14)
movl $0x1, %ecx
leaq 0x50(%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 0x7a41
incq %rcx
addq $0xc, %rdx
addq $0xc, %rax
cmpq $0x100, %rcx            # imm = 0x100
jne 0x7a3f
leaq 0x1850(%rsp), %rdi
movq $0x0, 0x8(%rsp)
movl $0xc00, %edx            # imm = 0xC00
xorl %esi, %esi
callq 0x20c0
cmpl $0x0, (%rsp)
jle 0x7b72
leaq 0x30(%rsp), %r14
movl 0x8(%r14), %eax
movl 0x10(%r14), %ecx
movq 0x18(%r14), %rdx
movq %rdx, 0x28(%rsp)
movq %rax, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
imull %ecx, %eax
movl %eax, 0x10(%rsp)
xorl %r15d, %r15d
cmpl $0x0, 0x18(%rsp)
jle 0x7b57
movq 0x8(%rsp), %rax
movl %eax, %ebp
xorl %r12d, %r12d
movslq %ebp, %rbx
addq 0x28(%rsp), %rbx
xorl %r13d, %r13d
movq %r14, %rdi
movl %r15d, %esi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x4f14
shll $0x2, %eax
leaq (%rax,%rax,2), %rax
leaq (%rsp,%rax), %rcx
addq $0x1850, %rcx           # imm = 0x1850
movl (%rcx,%r13,4), %edx
incl %edx
movl %edx, (%rcx,%r13,4)
leaq (%rsp,%r13,4), %rcx
addq $0x50, %rcx
addl -0xc(%rcx,%rax), %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
mulsd 0x99f0(%rip), %xmm0     # 0x11510
xorps %xmm1, %xmm1
cvtsi2sdl 0xbf4(%rcx), %xmm1
divsd %xmm1, %xmm0
callq 0x2280
cvttsd2si %xmm0, %eax
movb %al, (%rbx,%r13)
incq %r13
cmpq $0x3, %r13
jne 0x7ad8
incq %r12
addl 0x14(%rsp), %ebp
cmpq 0x18(%rsp), %r12
jne 0x7acd
incq %r15
movq 0x8(%rsp), %rax
addl 0x10(%rsp), %eax
movq %rax, 0x8(%rsp)
cmpq (%rsp), %r15
jne 0x7ab8
leaq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x4e1a
addq $0x2458, %rsp           # imm = 0x2458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop