// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
movq %rdi, %r15
leaq 0x9dea(%rip), %rsi      # 0x106c8
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x3fd6
movq %r14, %rdi
movq %r15, %rsi
callq 0x4032
leaq 0xc50(%rsp), %rdi
xorl %r15d, %r15d
movl $0xc00, %edx            # imm = 0xC00
xorl %esi, %esi
callq 0x20e0
movl 0x8(%r14), %eax
movl 0x10(%r14), %ecx
testl %ecx, %ecx
movl $0x0, %ebx
movl %ecx, 0xc(%rsp)
cmovgl %ecx, %ebx
testl %eax, %eax
movl $0x0, %r13d
movl %eax, 0x4(%rsp)
cmovgl %eax, %r13d
movl 0xc(%r14), %eax
testl %eax, %eax
cmovlel %r15d, %eax
movl %eax, 0x8(%rsp)
cmpl 0x8(%rsp), %r15d
je 0x6988
xorl %ebp, %ebp
cmpl %r13d, %ebp
je 0x6983
xorl %r12d, %r12d
cmpq %r12, %rbx
je 0x697f
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %r12d, %ecx
callq 0x41ea
movl %eax, %eax
imulq $0xc, %rax, %rax
addq %rsp, %rax
addq $0xc50, %rax            # imm = 0xC50
incl (%rax,%r12,4)
incq %r12
jmp 0x6952
incl %ebp
jmp 0x694a
incl %r15d
jmp 0x6941
leaq 0x50(%rsp), %r14
xorl %ebx, %ebx
movl $0xc00, %edx            # imm = 0xC00
movq %r14, %rdi
xorl %esi, %esi
callq 0x20e0
cmpq $0x3, %rbx
je 0x69b4
movl 0xc50(%rsp,%rbx,4), %eax
movl %eax, 0x50(%rsp,%rbx,4)
incq %rbx
jmp 0x699e
pushq $0x1
popq %rax
leaq 0xc5c(%rsp), %rcx
cmpq $0x100, %rax            # imm = 0x100
je 0x69ed
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x69e0
movl (%rcx,%rdx,4), %esi
addl (%r14,%rdx,4), %esi
movl %esi, 0xc(%r14,%rdx,4)
incq %rdx
jmp 0x69c9
incq %rax
addq $0xc, %r14
addq $0xc, %rcx
jmp 0x69bf
leaq 0x1850(%rsp), %rdi
xorl %ebx, %ebx
movl $0xc00, %edx            # imm = 0xC00
xorl %esi, %esi
callq 0x20e0
leaq 0x30(%rsp), %r14
movq 0x18(%r14), %rax
movq %rax, 0x20(%rsp)
movl %r13d, %eax
movq %rax, 0x28(%rsp)
movl 0x4(%rsp), %eax
imull 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
xorl %ebp, %ebp
cmpl 0x8(%rsp), %ebp
je 0x6ad6
movq %rbx, 0x18(%rsp)
xorl %r15d, %r15d
cmpq 0x28(%rsp), %r15
je 0x6ac6
movslq %ebx, %r13
addq 0x20(%rsp), %r13
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0x6aba
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
movl %r12d, %ecx
callq 0x41ea
movl %eax, %eax
imulq $0xc, %rax, %rax
leaq (%rsp,%rax), %rcx
addq $0x1850, %rcx           # imm = 0x1850
movl (%rcx,%r12,4), %edx
incl %edx
movl %edx, (%rcx,%r12,4)
leaq (%rsp,%r12,4), %rcx
addq $0x50, %rcx
addl -0xc(%rax,%rcx), %edx
cvtsi2sd %edx, %xmm0
mulsd 0x9a17(%rip), %xmm0     # 0x104b0
cvtsi2sdl 0xc44(%rsp,%r12,4), %xmm1
divsd %xmm1, %xmm0
callq 0x2260
cvttsd2si %xmm0, %eax
movb %al, (%r13,%r12)
incq %r12
jmp 0x6a50
incq %r15
addl 0xc(%rsp), %ebx
jmp 0x6a3a
incl %ebp
movq 0x18(%rsp), %rbx
addl 0x4(%rsp), %ebx
jmp 0x6a28
leaq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x411c
addq $0x2458, %rsp           # imm = 0x2458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop