// File: brianwu02[P]ImageProcessing/error_diffusion1.cpp
// Function: error_diffusion(imageS*, int, int, float, imageS*)
void error_diffusion(imageP I1, int mtd, int serpentine, float gamma, imageP I2) {
    int i, total, threshold;
    unsigned char *in;
    unsigned char *out;
    unsigned char gammaCorrectionLUT[256];
    unsigned char thresholdingLUT[256];

    total = I1->width * I1->height;
    I2->width = I1->width;
    I2->height = I1->height;

    I2->image = (unsigned char *) malloc(total);

    out = I2->image;
    in = I1->image;

    if (I2->image == NULL) {
        cerr << "Not enough memory\n";
        exit(1);
    }

    // create gamma correction lookup table
    for (i=0; i<256; i++) {
        gammaCorrectionLUT[i] = (int) (pow((double) i / 255.0, (1.0 / gamma)) * 255.0);
    }

    // gamma correct the input Image
    for (i=0; i<total; i++) {
        in[i] = gammaCorrectionLUT[in[i]];
    }

    // create thresholding scale

    int h = I1->height;
    int w = I1->width;

    // create buffer.
    unsigned char *buf;
    buf = (unsigned char *) malloc(2 * (w + 2)); // rows + 2 pads for each row.

    // pad with 0's
    buf[0] = 0;
    buf[w] = 0;
    buf[w+1] = 0;
    buf[2*(w+2)-1] = 0;

    int bufRowsRequired = 2;

    // create pointers to buffer.
    unsigned char *in1, *in2;

    // create array of pointers
    unsigned char *arrayOfPointers[bufRowsRequired];

    for(int i=0; i<bufRowsRequired; i++) {
        // store pointers in array.
        arrayOfPointers[i] = &buf[i*(w+2)];
    }

    // mtd = 0 -> Floyd Steinberg error Diffusion
    // mtd = 1 -> Jarvis Jundice error Diffusion
    
    threshold = 255 / 2;
    copyToBufferPadded(I1, 0, 2, buf);

    for (int y=0; y<h; y++) {
        copyToBufferPadded(I1, (y+1), 2, buf);
            in2 = arrayOfPointers[(y+1) % 2] + 1;
            in1 = arrayOfPointers[y % 2] + 1;
            if (serpentine == 1 and (y % 2) == 1) {
                for (int i=0; i<w; i++) {
                    in1++;
                    in2++;
                    out++;
                }
            }

        for (int x=0; x<w; x++) {
            if (serpentine == 0 or (serpentine == 1 and (y % 2) == 0)) {
                *out = (*in1 < threshold) ? 0 : 255;
                short e = *in1 - *out;

                in1[1] += (e*7/16.0);
                in2[-1] += (e*3/16.0);
                in2[0] += (e*5/16.0);
                in2[1] += (e*1/16.0);
                
                in1++;
                in2++;
                out++;

            } else if (serpentine == 1 and (y % 2) == 1) {
                *out = (*in1 < threshold) ? 0 : 255;
                short e = *in - *out;
                
                in1[1] += (e*7/16.0);
                in2[-1] += (e*3/16.0);
                in2[0] += (e*5/16.0);
                in2[1] += (e*1/16.0);

                in1--;
                in2--;
                out--;
                
            }
                
        }
    }


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movss %xmm0, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
imull 0x4(%rcx), %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movslq -0x28(%rbp), %rdi
callq 0x1120
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x15dd
movq 0x3a29(%rip), %rdi      # 0x4ff0
leaq 0x1a84(%rip), %rsi      # 0x3052
callq 0x10a0
movl $0x1, %edi
callq 0x10e0
movl $0x0, -0x24(%rbp)
cmpl $0x100, -0x24(%rbp)     # imm = 0x100
jge 0x1640
cvtsi2sdl -0x24(%rbp), %xmm0
movsd 0x1a16(%rip), %xmm1     # 0x3010
divsd %xmm1, %xmm0
movss -0x14(%rbp), %xmm1
cvtss2sd %xmm1, %xmm2
movsd 0x1a09(%rip), %xmm1     # 0x3018
divsd %xmm2, %xmm1
callq 0x1180
movsd 0x19f0(%rip), %xmm1     # 0x3010
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movslq -0x24(%rbp), %rax
movb %cl, -0x140(%rbp,%rax)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x15e4
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x1678
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movb -0x140(%rbp,%rax), %dl
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1647
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x244(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x248(%rbp)
movl -0x248(%rbp), %ecx
movl %ecx, %eax
leal 0x4(%rax,%rax), %eax
movslq %eax, %rdi
callq 0x1120
movq %rax, -0x250(%rbp)
movq -0x250(%rbp), %rax
movb $0x0, (%rax)
movq -0x250(%rbp), %rax
movslq -0x248(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x250(%rbp), %rcx
movslq -0x248(%rbp), %rax
movb $0x0, 0x1(%rax,%rcx)
movq -0x250(%rbp), %rax
movl -0x248(%rbp), %edx
movl %edx, %ecx
leal 0x3(%rcx,%rcx), %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movl $0x2, -0x254(%rbp)
movl -0x254(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x270(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x298(%rbp)
movq %rcx, %rsp
movq %rax, -0x278(%rbp)
movl $0x0, -0x27c(%rbp)
movl -0x27c(%rbp), %eax
cmpl -0x254(%rbp), %eax
jge 0x178c
movq -0x298(%rbp), %rax
movq -0x250(%rbp), %rdx
movl -0x27c(%rbp), %ecx
movl -0x248(%rbp), %esi
addl $0x2, %esi
imull %esi, %ecx
movslq %ecx, %rcx
addq %rcx, %rdx
movslq -0x27c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x27c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x27c(%rbp)
jmp 0x173c
movl $0x7f, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x250(%rbp), %rcx
xorl %esi, %esi
movl $0x2, %edx
callq 0x25a0
movl $0x0, -0x280(%rbp)
movl -0x280(%rbp), %eax
cmpl -0x244(%rbp), %eax
jge 0x1b7b
movq -0x8(%rbp), %rdi
movl -0x280(%rbp), %esi
addl $0x1, %esi
movq -0x250(%rbp), %rcx
movl $0x2, %edx
callq 0x25a0
movl -0x280(%rbp), %eax
addl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movq -0x298(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
addq $0x1, %rax
movq %rax, -0x268(%rbp)
movl -0x280(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movq -0x298(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
addq $0x1, %rax
movq %rax, -0x260(%rbp)
cmpl $0x1, -0x10(%rbp)
jne 0x18a9
movl -0x280(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x1, %edx
jne 0x18a9
movl $0x0, -0x284(%rbp)
movl -0x284(%rbp), %eax
cmpl -0x248(%rbp), %eax
jge 0x18a7
movq -0x260(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x260(%rbp)
movq -0x268(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x268(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movl -0x284(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x284(%rbp)
jmp 0x1858
jmp 0x18a9
movl $0x0, -0x288(%rbp)
movl -0x288(%rbp), %eax
cmpl -0x248(%rbp), %eax
jge 0x1b65
cmpl $0x0, -0x10(%rbp)
je 0x18ec
cmpl $0x1, -0x10(%rbp)
jne 0x1a10
movl -0x280(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1a10
movq -0x260(%rbp), %rax
movzbl (%rax), %eax
movl -0x2c(%rbp), %ecx
subl %ecx, %eax
setge %cl
negb %cl
movq -0x40(%rbp), %rax
movb %cl, (%rax)
movq -0x260(%rbp), %rax
movzbl (%rax), %eax
movq -0x40(%rbp), %rcx
movzbl (%rcx), %ecx
subl %ecx, %eax
movw %ax, -0x28a(%rbp)
movswl -0x28a(%rbp), %ecx
movl %ecx, %eax
shll $0x3, %eax
subl %ecx, %eax
cvtsi2sd %eax, %xmm2
movsd 0x16ce(%rip), %xmm0     # 0x3008
mulsd %xmm0, %xmm2
movq -0x260(%rbp), %rax
movzbl 0x1(%rax), %ecx
cvtsi2sd %ecx, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %ecx
movb %cl, 0x1(%rax)
movswl -0x28a(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
movq -0x268(%rbp), %rax
movzbl -0x1(%rax), %ecx
cvtsi2sd %ecx, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %ecx
movb %cl, -0x1(%rax)
movswl -0x28a(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,4), %eax
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
movq -0x268(%rbp), %rax
movzbl (%rax), %ecx
cvtsi2sd %ecx, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %ecx
movb %cl, (%rax)
movswl -0x28a(%rbp), %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm0, %xmm1
movq -0x268(%rbp), %rax
movzbl 0x1(%rax), %ecx
cvtsi2sd %ecx, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movb %cl, 0x1(%rax)
movq -0x260(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x260(%rbp)
movq -0x268(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x268(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1b4f
cmpl $0x1, -0x10(%rbp)
jne 0x1b4d
movl -0x280(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x1, %edx
jne 0x1b4d
movq -0x260(%rbp), %rax
movzbl (%rax), %eax
movl -0x2c(%rbp), %ecx
subl %ecx, %eax
setge %cl
negb %cl
movq -0x40(%rbp), %rax
movb %cl, (%rax)
movq -0x38(%rbp), %rax
movzbl (%rax), %eax
movq -0x40(%rbp), %rcx
movzbl (%rcx), %ecx
subl %ecx, %eax
movw %ax, -0x28c(%rbp)
movswl -0x28c(%rbp), %ecx
movl %ecx, %eax
shll $0x3, %eax
subl %ecx, %eax
cvtsi2sd %eax, %xmm2
movsd 0x158c(%rip), %xmm0     # 0x3008
mulsd %xmm0, %xmm2
movq -0x260(%rbp), %rax
movzbl 0x1(%rax), %ecx
cvtsi2sd %ecx, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %ecx
movb %cl, 0x1(%rax)
movswl -0x28c(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
movq -0x268(%rbp), %rax
movzbl -0x1(%rax), %ecx
cvtsi2sd %ecx, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %ecx
movb %cl, -0x1(%rax)
movswl -0x28c(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,4), %eax
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
movq -0x268(%rbp), %rax
movzbl (%rax), %ecx
cvtsi2sd %ecx, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %ecx
movb %cl, (%rax)
movswl -0x28c(%rbp), %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm0, %xmm1
movq -0x268(%rbp), %rax
movzbl 0x1(%rax), %ecx
cvtsi2sd %ecx, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movb %cl, 0x1(%rax)
movq -0x260(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x260(%rbp)
movq -0x268(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x268(%rbp)
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1b4f
jmp 0x1b51
movl -0x288(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x288(%rbp)
jmp 0x18b3
jmp 0x1b67
movl -0x280(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x280(%rbp)
jmp 0x17b4
movq -0x270(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)