// File: rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::hist_equal_proc(Image&, Image&)
void ImageProc::hist_equal_proc(Image &img_orig, Image &img_modified)
{
	//Image img_bw("bw", width, height);

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

	int histogram_bins[256] = {0};

	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			if((img_orig.getvalue(i,j,0) >= 0) && (img_orig.getvalue(i,j,0) < 256))
				histogram_bins[img_orig.getvalue(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 < img_modified.rows; i++)
		for(int j = 0; j < img_modified.cols; j++)
			if      (img_orig.getvalue(i,j,0) < 0  ) img_modified.setvalue(i,j,0,0);
			else if (img_orig.getvalue(i,j,0) > 255) img_modified.setvalue(i,j,0,255);
			else    img_modified.setvalue(i,j,0,(255 * hist_cumulative[img_orig.getvalue(i,j,0)] \
															      / hist_cumulative[255]));


	//img_modified.write_image(outfile, width, height);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x840, %rsp            # imm = 0x840
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x410(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x2100
movl $0x0, -0x414(%rbp)
movl -0x414(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x10a9c
movl $0x0, -0x418(%rbp)
movl -0x418(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x10a86
movq -0x8(%rbp), %rdi
movl -0x414(%rbp), %esi
movl -0x418(%rbp), %edx
xorl %ecx, %ecx
callq 0x83b0
cmpl $0x0, %eax
jl 0x10a70
movq -0x8(%rbp), %rdi
movl -0x414(%rbp), %esi
movl -0x418(%rbp), %edx
xorl %ecx, %ecx
callq 0x83b0
cmpl $0x100, %eax            # imm = 0x100
jge 0x10a70
movq -0x8(%rbp), %rdi
movl -0x414(%rbp), %esi
movl -0x418(%rbp), %edx
xorl %ecx, %ecx
callq 0x83b0
cltq
movl -0x410(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x410(%rbp,%rax,4)
jmp 0x10a72
movl -0x418(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x418(%rbp)
jmp 0x109fd
jmp 0x10a88
movl -0x414(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x414(%rbp)
jmp 0x109e0
leaq -0x820(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x2100
movl -0x410(%rbp), %eax
movl %eax, -0x820(%rbp)
movl $0x1, -0x824(%rbp)
cmpl $0x100, -0x824(%rbp)    # imm = 0x100
jge 0x10b10
movl -0x824(%rbp), %eax
subl $0x1, %eax
cltq
movl -0x820(%rbp,%rax,4), %ecx
movslq -0x824(%rbp), %rax
addl -0x410(%rbp,%rax,4), %ecx
movslq -0x824(%rbp), %rax
movl %ecx, -0x820(%rbp,%rax,4)
movl -0x824(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x824(%rbp)
jmp 0x10ac5
movl $0x0, -0x828(%rbp)
movl -0x828(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x10c5c
movl $0x0, -0x82c(%rbp)
movl -0x82c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x10c46
movq -0x8(%rbp), %rdi
movl -0x828(%rbp), %esi
movl -0x82c(%rbp), %edx
xorl %ecx, %ecx
callq 0x83b0
cmpl $0x0, %eax
jge 0x10b86
movq -0x10(%rbp), %rdi
movl -0x828(%rbp), %esi
movl -0x82c(%rbp), %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x83f0
jmp 0x10c30
movq -0x8(%rbp), %rdi
movl -0x828(%rbp), %esi
movl -0x82c(%rbp), %edx
xorl %ecx, %ecx
callq 0x83b0
cmpl $0xff, %eax
jle 0x10bc3
movq -0x10(%rbp), %rdi
movl -0x828(%rbp), %esi
movl -0x82c(%rbp), %edx
xorl %ecx, %ecx
movl $0xff, %r8d
callq 0x83f0
jmp 0x10c2e
movq -0x10(%rbp), %rax
movq %rax, -0x840(%rbp)
movl -0x828(%rbp), %eax
movl %eax, -0x834(%rbp)
movl -0x82c(%rbp), %eax
movl %eax, -0x830(%rbp)
movq -0x8(%rbp), %rdi
movl -0x828(%rbp), %esi
movl -0x82c(%rbp), %edx
xorl %ecx, %ecx
callq 0x83b0
movq -0x840(%rbp), %rdi
movl -0x834(%rbp), %esi
cltq
imull $0xff, -0x820(%rbp,%rax,4), %eax
cltd
idivl -0x424(%rbp)
movl -0x830(%rbp), %edx
movl %eax, %r8d
xorl %ecx, %ecx
callq 0x83f0
jmp 0x10c30
jmp 0x10c32
movl -0x82c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x82c(%rbp)
jmp 0x10b37
jmp 0x10c48
movl -0x828(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x828(%rbp)
jmp 0x10b1a
addq $0x840, %rsp            # imm = 0x840
popq %rbp
retq
nopw %cs:(%rax,%rax)