// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movl (%rdi), %eax
movl 0x4(%rdi), %ecx
movl %eax, (%r12)
movl %ecx, 0x4(%r12)
movl %ecx, %ebx
imull %eax, %ebx
movslq %ebx, %rdi
callq 0x1100
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x17bd
movq %rax, %r15
movq %r14, -0x48(%rbp)
movq 0x8(%r14), %r14
cvtss2sd -0x38(%rbp), %xmm0
movsd 0xb5d(%rip), %xmm1      # 0x2008
divsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
xorl %r12d, %r12d
cmpq $0x100, %r12            # imm = 0x100
je 0x14f4
cvtsi2sd %r12d, %xmm0
movsd 0xb43(%rip), %xmm1      # 0x2010
divsd %xmm1, %xmm0
movsd -0x38(%rbp), %xmm1
callq 0x1160
mulsd 0xb2d(%rip), %xmm0      # 0x2010
cvttsd2si %xmm0, %eax
movb %al, -0x180(%rbp,%r12)
incq %r12
jmp 0x14b7
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
cmpq %rax, %rbx
je 0x1515
movzbl (%r14,%rax), %ecx
movb -0x180(%rbp,%rcx), %cl
movb %cl, (%r14,%rax)
incq %rax
jmp 0x14fb
movq %r14, -0x58(%rbp)
movq -0x48(%rbp), %r13
movl 0x4(%r13), %r12d
movslq (%r13), %rbx
leaq 0x2(%rbx), %rax
movq %rax, -0x38(%rbp)
leaq 0x4(,%rbx,2), %rdi
callq 0x1100
movq %rsp, -0x50(%rbp)
xorl %r14d, %r14d
movb %r14b, (%rax)
andw $0x0, (%rax,%rbx)
movb %r14b, 0x3(%rax,%rbx,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)
pushq $0x2
popq %rdx
movq %r13, %rdi
xorl %esi, %esi
movq %rax, -0x78(%rbp)
movq %rax, %rcx
callq 0x1e1c
cmpl $0x1, -0x3c(%rbp)
setne -0x29(%rbp)
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
testl %r12d, %r12d
cmovlel %r14d, %r12d
movl %r12d, -0x38(%rbp)
movq %rax, -0x68(%rbp)
incq %rax
movq %rax, -0x60(%rbp)
xorl %ebx, %ebx
cmpl -0x38(%rbp), %ebx
je 0x17aa
movl -0x3c(%rbp), %eax
testl %eax, %eax
sete %r13b
cmpl $0x1, %eax
sete %r12b
leal 0x1(%rbx), %r14d
movq -0x48(%rbp), %rdi
movl %r14d, %esi
pushq $0x2
popq %rdx
movq -0x78(%rbp), %rcx
callq 0x1e1c
movsd 0xa37(%rip), %xmm2      # 0x2018
movl %r14d, %ecx
andl $0x1, %ecx
andl $0x1, %ebx
sete %al
andb %al, %r12b
orb -0x29(%rbp), %al
movq -0x60(%rbp), %rsi
pushq $0x1
popq %rdx
cmovneq %rdx, %rsi
movq -0x68(%rbp), %r8
movq %r8, %rdx
movl $0x0, %edi
cmovneq %rdi, %rdx
movq -0x70(%rbp), %rdi
movq (%rdi,%rcx,8), %rcx
addq %rsi, %rcx
addq (%rdi,%rbx,8), %rsi
addq %rdx, %r15
orb %r13b, %r12b
movq -0x58(%rbp), %r10
movl %r8d, %edx
subl $0x1, %edx
jb 0x17a2
testb %r12b, %r12b
je 0x16eb
cmpb $0x7f, (%rsi)
movl $0x0, %r8d
adcb $-0x1, %r8b
movb %r8b, (%r15)
movzbl (%rsi), %edi
movzbl %r8b, %r8d
subl %r8d, %edi
imull $0x7, %edi, %r8d
cvtsi2sd %r8d, %xmm0
movzbl 0x1(%rsi), %r8d
cvtsi2sd %r8d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8d
leal (%rdi,%rdi,2), %r9d
cvtsi2sd %r9d, %xmm0
movb %r8b, 0x1(%rsi)
movzbl -0x1(%rcx), %r8d
cvtsi2sd %r8d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8d
leal (%rdi,%rdi,4), %r9d
cvtsi2sd %r9d, %xmm0
movb %r8b, -0x1(%rcx)
movzbl (%rcx), %r8d
cvtsi2sd %r8d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8d
leaq 0x1(%rsi), %rsi
movb %r8b, (%rcx)
cvtsi2sd %edi, %xmm0
mulsd %xmm2, %xmm0
movzbl 0x1(%rcx), %edi
cvtsi2sd %edi, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movb %dil, 0x1(%rcx)
incq %rcx
incq %r15
jmp 0x162a
testb %al, %al
jne 0x162a
cmpb $0x7f, (%rsi)
movl $0x0, %r8d
adcb $-0x1, %r8b
movb %r8b, (%r15)
movzbl (%r10), %edi
movzbl %r8b, %r8d
subl %r8d, %edi
imull $0x7, %edi, %r8d
cvtsi2sd %r8d, %xmm0
movzbl 0x1(%rsi), %r8d
cvtsi2sd %r8d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8d
leal (%rdi,%rdi,2), %r9d
cvtsi2sd %r9d, %xmm0
movb %r8b, 0x1(%rsi)
movzbl -0x1(%rcx), %r8d
cvtsi2sd %r8d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8d
leal (%rdi,%rdi,4), %r9d
cvtsi2sd %r9d, %xmm0
movb %r8b, -0x1(%rcx)
movzbl (%rcx), %r8d
cvtsi2sd %r8d, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8d
movb %r8b, (%rcx)
cvtsi2sd %edi, %xmm0
mulsd %xmm2, %xmm0
movzbl 0x1(%rcx), %edi
cvtsi2sd %edi, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movb %dil, 0x1(%rcx)
decq %rcx
decq %rsi
decq %r15
jmp 0x162a
movl %r14d, %ebx
jmp 0x15a9
movq -0x50(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x282c(%rip), %rdi      # 0x3ff0
leaq 0x887(%rip), %rsi       # 0x2052
callq 0x1090
pushq $0x1
popq %rdi
callq 0x10c0