// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rcx, %r12
movss %xmm0, -0x38(%rbp)
movl %edx, -0x3c(%rbp)
movq %rdi, %r13
movl (%rdi), %eax
movl 0x4(%rdi), %r14d
movl %eax, (%rcx)
movl %r14d, 0x4(%rcx)
imull %eax, %r14d
movslq %r14d, %rdi
callq 0x2160
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x2a65
movq %rax, %r15
movq 0x8(%r13), %rax
movq %rax, -0x50(%rbp)
xorps %xmm0, %xmm0
cvtss2sd -0x38(%rbp), %xmm0
movsd 0x1911(%rip), %xmm1     # 0x4008
divsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
xorl %r12d, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd 0x18fd(%rip), %xmm1     # 0x4010
divsd %xmm1, %xmm0
movsd -0x38(%rbp), %xmm1
callq 0x21d0
mulsd 0x18e7(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %eax
movb %al, -0x180(%rbp,%r12)
incq %r12
cmpq $0x100, %r12            # imm = 0x100
jne 0x2703
testl %r14d, %r14d
movq -0x50(%rbp), %rsi
jle 0x2765
movl %r14d, %eax
xorl %ecx, %ecx
movzbl (%rsi,%rcx), %edx
movb -0x180(%rbp,%rdx), %dl
movb %dl, (%rsi,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x274f
movl 0x4(%r13), %r14d
movslq (%r13), %r12
leaq 0x2(%r12), %rax
movq %rax, -0x38(%rbp)
leaq 0x4(,%r12,2), %rdi
callq 0x2160
movq %rsp, -0x58(%rbp)
xorl %ebx, %ebx
movb %bl, (%rax)
movw $0x0, (%rax,%r12)
movb %bl, 0x3(%rax,%r12,2)
movq %rsp, %rdx
leaq -0x10(%rdx), %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movslq -0x38(%rbp), %rcx
movq %rax, -0x10(%rdx)
addq %rax, %rcx
movq %rcx, -0x8(%rdx)
movq %r13, -0x38(%rbp)
movq %r13, %rdi
xorl %esi, %esi
movl $0x2, %edx
movq %rax, -0x78(%rbp)
movq %rax, %rcx
callq 0x3210
movl %r14d, -0x44(%rbp)
testl %r14d, %r14d
jle 0x2a52
movq %r12, %r13
movl -0x3c(%rbp), %eax
cmpl $0x1, %eax
setne -0x29(%rbp)
leal -0x1(%r12), %ecx
leaq 0x2(%rcx), %rdx
movq %rdx, -0x60(%rbp)
incq %rcx
movq %rcx, -0x68(%rbp)
xorl $0x1, %eax
movl %eax, -0x40(%rbp)
movl %ebx, %r12d
testl %r13d, %r13d
setle %r14b
leal 0x1(%r12), %ebx
movq -0x38(%rbp), %rdi
movl %ebx, %esi
movl $0x2, %edx
movq -0x78(%rbp), %rcx
callq 0x3210
movl %ebx, %esi
movl %ebx, %eax
andl $0x1, %eax
movq -0x70(%rbp), %rdx
movq (%rdx,%rax,8), %rcx
andl $0x1, %r12d
movq (%rdx,%r12,8), %rdx
sete %al
orb -0x29(%rbp), %al
orb %al, %r14b
je 0x2858
testl %r13d, %r13d
jle 0x2a4e
incq %rdx
incq %rcx
jmp 0x2866
movq -0x60(%rbp), %rdi
addq %rdi, %rcx
addq %rdi, %rdx
addq -0x68(%rbp), %r15
movl %esi, %ebx
movsd 0x17a8(%rip), %xmm2     # 0x4018
cmpl $0x0, -0x3c(%rbp)
sete %dil
orl -0x40(%rbp), %r12d
sete %sil
orb %dil, %sil
movl %r13d, %edi
movq -0x50(%rbp), %r14
testb %sil, %sil
je 0x2966
cmpb $0x7f, (%rdx)
movl $0x0, %r9d
adcb $-0x1, %r9b
movb %r9b, (%r15)
movzbl (%rdx), %r8d
movzbl %r9b, %r9d
subl %r9d, %r8d
leal (,%r8,8), %r9d
subl %r8d, %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
movzbl 0x1(%rdx), %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
leal (%r8,%r8,2), %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
movb %r9b, 0x1(%rdx)
movzbl -0x1(%rcx), %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
leal (%r8,%r8,4), %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
movb %r9b, -0x1(%rcx)
movzbl (%rcx), %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
leaq 0x1(%rdx), %rdx
movb %r9b, (%rcx)
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
mulsd %xmm2, %xmm0
movzbl 0x1(%rcx), %r8d
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8d
movb %r8b, 0x1(%rcx)
incq %rcx
incq %r15
jmp 0x2a3b
testb %al, %al
jne 0x2a3b
cmpb $0x7f, (%rdx)
movl $0x0, %r9d
adcb $-0x1, %r9b
movb %r9b, (%r15)
movzbl (%r14), %r8d
movzbl %r9b, %r9d
subl %r9d, %r8d
leal (,%r8,8), %r9d
subl %r8d, %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
movzbl 0x1(%rdx), %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
leal (%r8,%r8,2), %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
movb %r9b, 0x1(%rdx)
movzbl -0x1(%rcx), %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
leal (%r8,%r8,4), %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
movb %r9b, -0x1(%rcx)
movzbl (%rcx), %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
movb %r9b, (%rcx)
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
mulsd %xmm2, %xmm0
movzbl 0x1(%rcx), %r8d
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8d
movb %r8b, 0x1(%rcx)
decq %rcx
decq %rdx
decq %r15
decl %edi
jne 0x288a
cmpl -0x44(%rbp), %ebx
jne 0x2802
jmp 0x2a52
movl %esi, %ebx
jmp 0x2a43
movq -0x58(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3584(%rip), %rdi      # 0x5ff0
leaq 0x15df(%rip), %rsi      # 0x4052
callq 0x20e0
movl $0x1, %edi
callq 0x2120
nop