// File: /workspace/github_cpp_makefile_star2_O1/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 0x2a6b
movq %rax, %r15
movq 0x8(%r13), %rbx
xorps %xmm0, %xmm0
cvtss2sd -0x38(%rbp), %xmm0
movsd 0x1915(%rip), %xmm1     # 0x4008
divsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
xorl %r12d, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd 0x1901(%rip), %xmm1     # 0x4010
divsd %xmm1, %xmm0
movsd -0x38(%rbp), %xmm1
callq 0x21d0
mulsd 0x18eb(%rip), %xmm0     # 0x4010
cvttsd2si %xmm0, %eax
movb %al, -0x180(%rbp,%r12)
incq %r12
cmpq $0x100, %r12            # imm = 0x100
jne 0x26ff
testl %r14d, %r14d
jle 0x275d
movl %r14d, %eax
xorl %ecx, %ecx
movzbl (%rbx,%rcx), %edx
movb -0x180(%rbp,%rdx), %dl
movb %dl, (%rbx,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x2747
movl 0x4(%r13), %eax
movl %eax, -0x38(%rbp)
movslq (%r13), %r12
leaq 0x2(%r12), %r14
leaq 0x4(,%r12,2), %rdi
callq 0x2160
movq %rax, %rdx
movq %rsp, -0x58(%rbp)
xorl %eax, %eax
movb %al, (%rdx)
movw $0x0, (%rdx,%r12)
movq %r12, -0x50(%rbp)
movb %al, 0x3(%rdx,%r12,2)
movq %rsp, %rsi
addq $-0x10, %rsi
movq %rsi, %rsp
movslq %r14d, %rcx
movq %rdx, -0x48(%rbp)
movq %rdx, (%rsi,%rax,8)
incq %rax
addq %rcx, %rdx
cmpq $0x1, %rax
je 0x27a6
movq %rsi, -0x68(%rbp)
movq %r13, -0x78(%rbp)
movq %r13, %rdi
xorl %esi, %esi
movl $0x2, %edx
movq -0x48(%rbp), %rcx
callq 0x3205
cmpl $0x0, -0x38(%rbp)
movq -0x50(%rbp), %rcx
jle 0x2a58
movl -0x3c(%rbp), %eax
cmpl $0x1, %eax
setne -0x29(%rbp)
decl %ecx
leaq 0x2(%rcx), %rdx
movq %rdx, -0x60(%rbp)
incq %rcx
movq %rcx, -0x70(%rbp)
xorl $0x1, %eax
movl %eax, -0x40(%rbp)
xorl %r13d, %r13d
movl %r13d, %r14d
movq %r15, %r12
leal 0x1(%r14), %r13d
movq -0x78(%rbp), %rdi
movl %r13d, %esi
movl $0x2, %edx
movq -0x48(%rbp), %rcx
callq 0x3205
movq -0x50(%rbp), %r8
andl $0x1, %r14d
sete %al
orb -0x29(%rbp), %al
testl %r8d, %r8d
movq -0x70(%rbp), %r15
movl $0x0, %ecx
cmovleq %rcx, %r15
testb %al, %al
cmovneq %rcx, %r15
addq %r12, %r15
testl %r8d, %r8d
jle 0x2a4e
cmpl $0x0, -0x3c(%rbp)
sete %sil
movl %r14d, %edx
testb %al, %al
movq -0x60(%rbp), %rcx
movl $0x1, %edi
cmovneq %rdi, %rcx
movq -0x68(%rbp), %r9
movq (%r9,%rdx,8), %rdi
addq %rcx, %rdi
movl %r13d, %edx
andl $0x1, %edx
addq (%r9,%rdx,8), %rcx
orl -0x40(%rbp), %r14d
sete %dl
orb %sil, %dl
movl %r8d, %esi
movsd 0x1782(%rip), %xmm2     # 0x4018
testb %dl, %dl
je 0x2971
cmpb $0x7f, (%rdi)
movl $0x0, %r9d
adcb $-0x1, %r9b
movb %r9b, (%r15)
movzbl (%rdi), %r8d
movzbl %r9b, %r9d
subl %r9d, %r8d
leal (,%r8,8), %r9d
subl %r8d, %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
movzbl 0x1(%rdi), %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(%rdi)
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(%rdi), %rdi
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 0x2a46
testb %al, %al
jne 0x2a46
cmpb $0x7f, (%rdi)
movl $0x0, %r9d
adcb $-0x1, %r9b
movb %r9b, (%r15)
movzbl (%rbx), %r8d
movzbl %r9b, %r9d
subl %r9d, %r8d
leal (,%r8,8), %r9d
subl %r8d, %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
movzbl 0x1(%rdi), %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(%rdi)
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 %rdi
decq %r15
decl %esi
jne 0x2896
cmpl -0x38(%rbp), %r13d
jne 0x2803
movq -0x58(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x357e(%rip), %rdi      # 0x5ff0
leaq 0x15d9(%rip), %rsi      # 0x4052
callq 0x20e0
movl $0x1, %edi
callq 0x2120