// File: /workspace/github_cpp_makefile_star2_O2/brianwu02[P]ImageProcessing/error_diffusion.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
    threshold = 255 / 2;
    
    // create circular buffer. this one here is only 2 'widths' long.

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

    // mtd=0; manually copy row 0 to the circular buffer. Floyd-Steinberg Algorithm
    if (mtd == 0) {
        short buf[1][w+2];

        //short bufer[10];
        //short *buf1 = bufer;

        // pad the buffer
        buf[0][0] = 0;
        buf[0][(w+2) -1] = 0;
        buf[1][0] = 0;
        buf[1][(w+2) -1] = 0;

        for (int i = 0; i < w; i++) {
            buf[0][i+1] = in[i];
        }
        
        for (int y=0; y<h; y++) {
            // manually copy row 1 in to the circular buffer.
            if ((y % 2) == 0) {
                // if y is even, copy to buf[1]
                for (int i = 0; i < w; i++) {
                    buf[1][i+1] = in[y*w+i];
                }
            } else {
                // if y is odd, copy to buf[0]
                for (int i = 0; i < w; i++) {
                    buf[0][i+1] = in[y*w+i];
                }
            }
            
            int p = 1;

            for (int x=0; x<w; x++) {

                *out = (buf[0][p] < threshold) ? 0 : 255;
                short e = (buf[0][p] - *out);
                
                buf[0][p+1] += (e*7/16.0);
                buf[1][p-1] += (e*3/16.0); // error to SW
                buf[1][p] += (e*5/16.0);   // error to S
                buf[1][p+1] += (e*1/16.0); // error to SE
                p++;
                out++;
            }
        }
    } else {
        short buf[2][w+4];

        // pad the buffer, 2 deep.
        buf[0][0] = 0;
        buf[0][1] = 0;
        buf[0][(w+4) -1] = 0;
        buf[0][(w+4) -2] = 0;

        buf[1][0] = 0;
        buf[1][1] = 0;
        buf[1][(w+4) -1] = 0;
        buf[1][(w+4) -2] = 0;

        buf[2][0] = 0;
        buf[2][1] = 0;
        buf[2][(w+4) -1] = 0;
        buf[2][(w+4) -2] = 0;

        // mtd=1; for Jarvis-Judice-Ninke Error diffusion algorithm, need buffers.
        for (int i = 0; i < w; i++) {
            buf[0][i+2] = in[i];
            buf[1][i+2] = in[i+w];
        }
        // circular buffer logic.
        for (int y=0; y<h; y++) {
            if ((y % 3) == 0) {
                for (int i = 0; i < w; i++) {
                    buf[2][i+2] = in[y*w+i];
                }
            }
            if ((y % 3) == 1) {
                for (int i = 0; i < w; i++) {
                   buf[0][i+2] = in[y*w+i];
                }
            }
            if ((y % 3) == 2) {
                for (int i = 0; i < w; i++) {
                    buf[1][i+2] = in[y*w+i];
                }
            }

            int p = 2;
            for (int x=0; x<w; x++) {
                *out = (buf[0][p] < threshold) ? 0 : 255;
                //cout << (int) buf[0][p] << endl;
                short e = (buf[0][p] - *out);

                buf[0][p+1] += (e*7/48.0);
                buf[0][p+2] += (e*5/48.0);
                buf[1][p-2] += (e*3/48.0);
                buf[1][p-1] += (e*5/48.0);
                buf[1][p] += (e*7/48.0);
                buf[1][p+1] += (e*5/48.0);
                buf[1][p+2] += (e*3/48.0);

                buf[2][p-2] += (e*1/48.0);
                buf[2][p-1] += (e*3/48.0);
                buf[2][p] += (e*5/48.0);
                buf[2][p+1] += (e*3/48.0);
                buf[2][p+2] += (e*1/48.0);

                p++;
                out++;
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rcx, %r12
movss %xmm0, -0x30(%rbp)
movl %esi, -0x34(%rbp)
movq %rdi, %r15
movl (%rdi), %eax
movl 0x4(%rdi), %ecx
movl %eax, (%r12)
movl %ecx, 0x4(%r12)
movl %ecx, %r13d
imull %eax, %r13d
movslq %r13d, %rdi
callq 0x1100
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x190f
movq %rax, %rbx
movq 0x8(%r15), %r12
cvtss2sd -0x30(%rbp), %xmm0
movsd 0xb8f(%rip), %xmm1      # 0x2008
divsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
xorl %r14d, %r14d
cmpq $0x100, %r14            # imm = 0x100
je 0x14c2
cvtsi2sd %r14d, %xmm0
movsd 0xb75(%rip), %xmm1      # 0x2010
divsd %xmm1, %xmm0
movsd -0x30(%rbp), %xmm1
callq 0x1160
mulsd 0xb5f(%rip), %xmm0      # 0x2010
cvttsd2si %xmm0, %eax
movb %al, -0x140(%rbp,%r14)
incq %r14
jmp 0x1485
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
cmpq %rax, %r13
je 0x14e5
movzbl (%r12,%rax), %ecx
movb -0x140(%rbp,%rcx), %cl
movb %cl, (%r12,%rax)
incq %rax
jmp 0x14cb
movq %rsp, %rdi
movl 0x4(%r15), %r8d
movslq (%r15), %r15
cmpl $0x0, -0x34(%rbp)
movq %rdi, -0x40(%rbp)
je 0x1766
leal 0x4(%r15), %eax
leaq (%rax,%rax), %rdx
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rsi
andq $-0x10, %rsi
movq %rcx, %r9
subq %rsi, %r9
movq %r9, %rsp
negq %rsi
andl $0x0, (%rcx,%rsi)
andw $0x0, 0x6(%r9,%r15,2)
andw $0x0, 0x4(%r9,%r15,2)
leaq (%r9,%rax,2), %r10
andl $0x0, (%r10)
andw $0x0, 0x6(%r10,%r15,2)
andw $0x0, 0x4(%r10,%r15,2)
leaq (%r9,%rax,4), %r11
andl $0x0, (%r11)
andw $0x0, 0x6(%r11,%r15,2)
andw $0x0, 0x4(%r11,%r15,2)
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %r14d
cmovgl %r15d, %r14d
movq %r15, -0x30(%rbp)
leaq (%r12,%r15), %rsi
cmpq %rcx, %r14
je 0x1595
movzbl (%r12,%rcx), %r15d
movw %r15w, 0x4(%r9,%rcx,2)
movzbl (%rsi,%rcx), %r15d
movw %r15w, 0x4(%r10,%rcx,2)
incq %rcx
jmp 0x1575
xorl %ecx, %ecx
testl %r8d, %r8d
cmovlel %ecx, %r8d
leaq (%rdx,%r9), %r15
addq $0x8, %r15
leaq (%r9,%rax,4), %r13
addq $0x8, %r13
movq 0xa6a(%rip), %xmm0      # 0x2020
movapd 0xa72(%rip), %xmm1      # 0x2030
cmpq %r8, %rcx
je 0x18fc
movl %ecx, %eax
xorl %edx, %edx
pushq $0x3
popq %rsi
divl %esi
xorl %eax, %eax
testl %edx, %edx
je 0x15fe
cmpl $0x1, %edx
jne 0x1613
cmpq %rax, %r14
je 0x1618
movzbl (%r12,%rax), %edx
movw %dx, 0x4(%r9,%rax,2)
incq %rax
jmp 0x15db
movzbl (%r12,%rax), %edx
movw %dx, 0x4(%r11,%rax,2)
incq %rax
cmpq %rax, %r14
jne 0x15f0
jmp 0x1618
movzbl (%r12,%rax), %edx
movw %dx, 0x4(%r10,%rax,2)
incq %rax
cmpq %rax, %r14
jne 0x1605
xorl %eax, %eax
cmpq %rax, %r14
je 0x1757
cmpw $0x7f, 0x4(%r9,%rax,2)
setge %dl
negb %dl
movzwl 0x4(%r9,%rax,2), %esi
movzbl %dl, %edi
subl %edi, %esi
movswl %si, %esi
movd %esi, %xmm2
pshufd $0x50, %xmm2, %xmm2     # xmm2 = xmm2[0,0,1,1]
pmaddwd %xmm0, %xmm2
cvtdq2pd %xmm2, %xmm2
divpd %xmm1, %xmm2
movd 0x6(%r9,%rax,2), %xmm3
pshuflw $0x60, %xmm3, %xmm3     # xmm3 = xmm3[0,0,2,1,4,5,6,7]
psrad $0x10, %xmm3
cvtdq2pd %xmm3, %xmm3
addpd %xmm2, %xmm3
cvttpd2dq %xmm3, %xmm3
pshuflw $0xe8, %xmm3, %xmm3     # xmm3 = xmm3[0,2,2,3,4,5,6,7]
movd %xmm3, 0x6(%r9,%rax,2)
leal (%rsi,%rsi,2), %edi
cvtsi2sd %edi, %xmm4
movswl (%r15,%rax,2), %edi
cvtsi2sd %edi, %xmm5
movq -0x8(%r15,%rax,2), %xmm3
punpcklwd %xmm3, %xmm3    # xmm3 = xmm3[0,0,1,1,2,2,3,3]
psrad $0x10, %xmm3
pshufd $0xee, %xmm3, %xmm6     # xmm6 = xmm3[2,3,2,3]
cvtdq2pd %xmm6, %xmm6
cvtdq2pd %xmm3, %xmm7
cvtsi2sd %esi, %xmm3
unpcklpd %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0]
divpd %xmm1, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
movapd %xmm3, %xmm8
unpckhpd %xmm2, %xmm8            # xmm8 = xmm8[1],xmm2[1]
addpd %xmm7, %xmm8
addpd %xmm2, %xmm6
cvttpd2dq %xmm8, %xmm7
cvttpd2dq %xmm6, %xmm6
unpcklpd %xmm6, %xmm7            # xmm7 = xmm7[0],xmm6[0]
packssdw %xmm7, %xmm7
movq %xmm7, -0x8(%r15,%rax,2)
addsd %xmm5, %xmm4
cvttsd2si %xmm4, %esi
movw %si, (%r15,%rax,2)
movq -0x8(%r13,%rax,2), %xmm4
punpcklwd %xmm4, %xmm4    # xmm4 = xmm4[0,0,1,1,2,2,3,3]
psrad $0x10, %xmm4
cvtdq2pd %xmm4, %xmm5
pshufd $0xee, %xmm4, %xmm4     # xmm4 = xmm4[2,3,2,3]
cvtdq2pd %xmm4, %xmm4
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
addpd %xmm4, %xmm2
addpd %xmm3, %xmm5
cvttpd2dq %xmm5, %xmm4
cvttpd2dq %xmm2, %xmm2
movb %dl, (%rbx,%rax)
unpcklpd %xmm2, %xmm4            # xmm4 = xmm4[0],xmm2[0]
packssdw %xmm4, %xmm4
movq %xmm4, -0x8(%r13,%rax,2)
movswl (%r13,%rax,2), %edx
cvtsi2sd %edx, %xmm2
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %edx
movw %dx, (%r13,%rax,2)
incq %rax
jmp 0x161a
incq %rcx
addq -0x30(%rbp), %r12
addq %rax, %rbx
jmp 0x15be
movl %r15d, %esi
addl $0x2, %esi
movq %rsp, %rcx
leaq 0xf(,%rsi,2), %rdx
andq $-0x10, %rdx
movq %rcx, %rax
subq %rdx, %rax
movq %rax, %rsp
negq %rdx
andw $0x0, (%rcx,%rdx)
andw $0x0, 0x2(%rax,%r15,2)
leaq (%rax,%rsi,2), %rcx
andw $0x0, (%rcx)
andw $0x0, 0x2(%rcx,%r15,2)
xorl %edx, %edx
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %r15d, %ecx
cmpq %rdx, %rcx
je 0x17c5
movzbl (%r12,%rdx), %r9d
movw %r9w, 0x2(%rax,%rdx,2)
incq %rdx
jmp 0x17b0
xorl %edx, %edx
testl %r8d, %r8d
cmovlel %edx, %r8d
movq %r8, %rdi
leaq (%rax,%rsi,2), %rsi
addq $0x2, %rsi
movsd 0x837(%rip), %xmm0      # 0x2018
movq 0x857(%rip), %xmm1      # 0x2040
movapd 0x85f(%rip), %xmm2      # 0x2050
cmpq %rdi, %rdx
je 0x18fc
xorl %r9d, %r9d
movq %r15, %r8
testb $0x1, %dl
jne 0x1819
cmpq %r9, %rcx
je 0x182e
movzbl (%r12,%r9), %r10d
movw %r10w, (%rsi,%r9,2)
incq %r9
jmp 0x1805
cmpq %r9, %rcx
je 0x182e
movzbl (%r12,%r9), %r10d
movw %r10w, 0x2(%rax,%r9,2)
incq %r9
jmp 0x1819
xorl %r9d, %r9d
cmpq %r9, %rcx
je 0x18ee
cmpw $0x7f, 0x2(%rax,%r9,2)
setge %r11b
negb %r11b
movzwl 0x2(%rax,%r9,2), %r10d
movzbl %r11b, %r14d
subl %r14d, %r10d
movswl %r10w, %r10d
imull $0x7, %r10d, %r14d
cvtsi2sd %r14d, %xmm3
movb %r11b, (%rbx,%r9)
movswl 0x4(%rax,%r9,2), %r11d
cvtsi2sd %r11d, %xmm4
mulsd %xmm0, %xmm3
addsd %xmm3, %xmm4
cvttsd2si %xmm4, %r11d
movw %r11w, 0x4(%rax,%r9,2)
movd %r10d, %xmm3
pshufd $0x50, %xmm3, %xmm3     # xmm3 = xmm3[0,0,1,1]
pmaddwd %xmm1, %xmm3
cvtdq2pd %xmm3, %xmm3
mulpd %xmm2, %xmm3
movd -0x2(%rsi,%r9,2), %xmm4
pshuflw $0x60, %xmm4, %xmm4     # xmm4 = xmm4[0,0,2,1,4,5,6,7]
psrad $0x10, %xmm4
cvtdq2pd %xmm4, %xmm4
addpd %xmm3, %xmm4
cvttpd2dq %xmm4, %xmm3
pshuflw $0xe8, %xmm3, %xmm3     # xmm3 = xmm3[0,2,2,3,4,5,6,7]
movd %xmm3, -0x2(%rsi,%r9,2)
cvtsi2sd %r10d, %xmm3
mulsd %xmm0, %xmm3
movswl 0x2(%rsi,%r9,2), %r10d
cvtsi2sd %r10d, %xmm4
addsd %xmm3, %xmm4
cvttsd2si %xmm4, %r10d
movw %r10w, 0x2(%rsi,%r9,2)
incq %r9
jmp 0x1831
incq %rdx
addq %r8, %r12
addq %r9, %rbx
jmp 0x17f1
movq -0x40(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x26da(%rip), %rdi      # 0x3ff0
leaq 0x775(%rip), %rsi       # 0x2092
callq 0x1090
pushq $0x1
popq %rdi
callq 0x10c0
nop