// File: 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
movq %rsp, %rbp
subq $0x1890, %rsp           # imm = 0x1890
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
leaq 0x105e2(%rip), %rsi     # 0x1c5e8
leaq -0x38(%rbp), %rdi
movq %rdi, -0x1880(%rbp)
callq 0x7eb0
movq -0x1880(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
callq 0x7f70
jmp 0xc02e
leaq -0xc50(%rbp), %rdi
xorl %esi, %esi
movl $0xc00, %edx            # imm = 0xC00
callq 0x2100
movl $0x0, -0xc54(%rbp)
movl -0xc54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xc168
movl $0x0, -0xc58(%rbp)
movl -0xc58(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0xc152
movl $0x0, -0xc5c(%rbp)
movl -0xc5c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0xc13c
movl -0xc54(%rbp), %esi
movl -0xc58(%rbp), %edx
movl -0xc5c(%rbp), %ecx
leaq -0x38(%rbp), %rdi
callq 0x83b0
cmpl $0x0, %eax
jl 0xc126
movl -0xc54(%rbp), %esi
movl -0xc58(%rbp), %edx
movl -0xc5c(%rbp), %ecx
leaq -0x38(%rbp), %rdi
callq 0x83b0
cmpl $0x100, %eax            # imm = 0x100
jge 0xc126
movl -0xc54(%rbp), %esi
movl -0xc58(%rbp), %edx
movl -0xc5c(%rbp), %ecx
leaq -0x38(%rbp), %rdi
callq 0x83b0
movslq %eax, %rcx
leaq -0xc50(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movslq -0xc5c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0xc126
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x1b2c0
jmp 0xc43b
jmp 0xc128
movl -0xc5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc5c(%rbp)
jmp 0xc07d
jmp 0xc13e
movl -0xc58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc58(%rbp)
jmp 0xc064
jmp 0xc154
movl -0xc54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc54(%rbp)
jmp 0xc04b
leaq -0x1860(%rbp), %rdi
xorl %esi, %esi
movl $0xc00, %edx            # imm = 0xC00
callq 0x2100
movl $0x0, -0x1864(%rbp)
cmpl $0x3, -0x1864(%rbp)
jge 0xc1bb
movslq -0x1864(%rbp), %rax
movl -0xc50(%rbp,%rax,4), %ecx
movslq -0x1864(%rbp), %rax
movl %ecx, -0x1860(%rbp,%rax,4)
movl -0x1864(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1864(%rbp)
jmp 0xc185
movl $0x1, -0x1868(%rbp)
cmpl $0x100, -0x1868(%rbp)   # imm = 0x100
jge 0xc271
movl $0x0, -0x186c(%rbp)
cmpl $0x3, -0x186c(%rbp)
jge 0xc25b
movl -0x1868(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq -0x1860(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movslq -0x186c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movslq -0x1868(%rbp), %rcx
leaq -0xc50(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movslq -0x186c(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movslq -0x1868(%rbp), %rcx
leaq -0x1860(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movslq -0x186c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x186c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x186c(%rbp)
jmp 0xc1df
jmp 0xc25d
movl -0x1868(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1868(%rbp)
jmp 0xc1c5
movl $0x0, -0x1870(%rbp)
movl -0x1870(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0xc414
movl $0x0, -0x1874(%rbp)
movl -0x1874(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xc3fe
movl $0x0, -0x1878(%rbp)
cmpl $0x3, -0x1878(%rbp)
jge 0xc3e8
movl -0x1870(%rbp), %esi
movl -0x1874(%rbp), %edx
movl -0x1878(%rbp), %ecx
leaq -0x38(%rbp), %rdi
callq 0x83b0
cmpl $0x0, %eax
jge 0xc2fd
movl -0x1870(%rbp), %esi
movl -0x1874(%rbp), %edx
movl -0x1878(%rbp), %ecx
leaq -0x38(%rbp), %rdi
xorl %r8d, %r8d
callq 0x83f0
jmp 0xc3d2
movl -0x1870(%rbp), %esi
movl -0x1874(%rbp), %edx
movl -0x1878(%rbp), %ecx
leaq -0x38(%rbp), %rdi
callq 0x83b0
cmpl $0xff, %eax
jle 0xc345
movl -0x1870(%rbp), %esi
movl -0x1874(%rbp), %edx
movl -0x1878(%rbp), %ecx
leaq -0x38(%rbp), %rdi
movl $0xff, %r8d
callq 0x83f0
jmp 0xc3d0
movl -0x1870(%rbp), %eax
movl %eax, -0x188c(%rbp)
movl -0x1874(%rbp), %eax
movl %eax, -0x1884(%rbp)
movl -0x1878(%rbp), %eax
movl %eax, -0x1888(%rbp)
movl -0x1870(%rbp), %esi
movl -0x1874(%rbp), %edx
movl -0x1878(%rbp), %ecx
leaq -0x38(%rbp), %rdi
callq 0x83b0
movl -0x188c(%rbp), %esi
movl -0x1888(%rbp), %ecx
movslq %eax, %rdx
leaq -0x1860(%rbp), %rax
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movslq -0x1878(%rbp), %rdx
imull $0xff, (%rax,%rdx,4), %eax
movslq -0x1878(%rbp), %rdi
cltd
idivl -0xc6c(%rbp,%rdi,4)
movl -0x1884(%rbp), %edx
movl %eax, %r8d
leaq -0x38(%rbp), %rdi
callq 0x83f0
jmp 0xc3d2
jmp 0xc3d4
movl -0x1878(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1878(%rbp)
jmp 0xc2ad
jmp 0xc3ea
movl -0x1874(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1874(%rbp)
jmp 0xc294
jmp 0xc400
movl -0x1870(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1870(%rbp)
jmp 0xc27b
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
leaq -0x38(%rbp), %rdi
callq 0x8150
jmp 0xc429
leaq -0x38(%rbp), %rdi
callq 0x1b2c0
addq $0x1890, %rsp           # imm = 0x1890
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x2360
nopw %cs:(%rax,%rax)