// File: /workspace/github_c_makefile_star2_O2/patriciogonzalezvivo[P]tprint/src/thermalPrinter.cpp
// Function: ThermalPrinter::printImg(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int)
void ThermalPrinter::printImg(const std::string& _path, int _threshold){
    int width = 0;
    int height = 0;
    int comp;

    stbi_set_flip_vertically_on_load(false);
    unsigned char* pixels = stbi_load(_path.c_str(), &width, &height, &comp, STBI_rgb);
    
    int GrayArrayLength = width * height;
    unsigned char * GrayArray = new unsigned char[GrayArrayLength];
    memset(GrayArray,0x00,GrayArrayLength);
    
    int rowBytes        = (width + 7) / 8;                  // Round up to next byte boundary
    int rowBytesClipped = (rowBytes >= 48) ? 48 : rowBytes; // 384 pixels max width
    
    int totalBytes = rowBytesClipped*height;
    uint8_t data[totalBytes];
    memset(data,0x00,totalBytes);
    
    for (int x = 0; x < width;x++) {
        for (int y = 0; y < height; y++) {
            int index = (y*width)*comp+x*comp;

            float brightTemp = 0.0;
            if (comp == 1 || comp == 2){
                brightTemp = pixels[index];
            } else if (comp == 3 || comp == 4 ){
                brightTemp =    0.212655f * (float)pixels[index] + 
                                0.715158f * (float)pixels[index+1] + 
                                0.072187f * (float)pixels[index+2];
                if(brightTemp>255) brightTemp = 255;
            } 
            
            // Brightness correction curve:
            brightTemp =  sqrt(255) * sqrt (brightTemp);
            if (brightTemp > 255) brightTemp = 255;
            if (brightTemp < 0) brightTemp = 0;
            
            int darkness = 255 - floor(brightTemp);
            
            int idx = y*width + x;
            darkness += GrayArray[idx];
            
            if(x<rowBytesClipped*8){
                
                uint8_t pixel;
                if( darkness >= _threshold){
                    darkness -= _threshold;
                    pixel = 0x01;
                } else {
                    pixel = 0x00;
                }
                
                data[y*rowBytesClipped+x/8] += (pixel&0x01)<<(7-x%8);
            }
            
            int darkn8 = round(darkness / 8);
            
            // Atkinson dithering algorithm:  http://verlagmartinkoch.at/software/dither/index.html
            // Distribute error as follows:
            //     [ ]  1/8  1/8
            //1/8  1/8  1/8
            //     1/8
            
            if ((idx + 1) < GrayArrayLength)
                GrayArray[idx + 1] += darkn8;
            if ((idx + 2) < GrayArrayLength)
                GrayArray[idx + 2] += darkn8;
            if ((idx + width - 1) < GrayArrayLength)
                GrayArray[idx + width - 1] += darkn8;
            if ((idx + width) < GrayArrayLength)
                GrayArray[idx + width] += darkn8;
            if ((idx + width + 1) < GrayArrayLength)
                GrayArray[idx + width + 1 ] += darkn8;
            if ((idx + 2 * width) < GrayArrayLength)
                GrayArray[idx + 2 * width] += darkn8;
        }
    }
    
    for (int y=0; y<height; y++) {
        writeBytesRow(&data[y*rowBytesClipped],rowBytesClipped);
    }

    delete []GrayArray;
    delete []pixels;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %edx, -0x50(%rbp)
movq %rdi, -0x80(%rbp)
leaq -0xa8(%rbp), %rbx
andl $0x0, (%rbx)
leaq -0xa4(%rbp), %r12
andl $0x0, (%r12)
andl $0x0, 0x10970(%rip)     # 0x20410
movq (%rsi), %rdi
leaq -0x5c(%rbp), %rcx
pushq $0x3
popq %r8
movq %rbx, %rsi
movq %r12, %rdx
callq 0xdd80
movq %rax, -0x48(%rbp)
movl (%rbx), %ebx
movl (%r12), %r15d
movl %r15d, %r13d
imull %ebx, %r13d
movslq %r13d, %r14
movq %r14, %rdi
callq 0x8040
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rax, %r14
callq 0x81d0
movq %rsp, -0x78(%rbp)
movq %rbx, -0x68(%rbp)
leal 0x7(%rbx), %eax
pushq $0x8
popq %rcx
cltd
idivl %ecx
cmpl $0x179, %ebx            # imm = 0x179
pushq $0x30
popq %rbx
cmovll %eax, %ebx
movl %ebx, %eax
imull %r15d, %eax
leaq 0xf(%rax), %rcx
andq $-0x10, %rcx
movq %rsp, %r12
subq %rcx, %r12
movq %r12, %rsp
movslq %eax, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x81d0
xorl %edi, %edi
movq -0x68(%rbp), %rcx
leal (%rcx,%rcx), %eax
movl %eax, -0x34(%rbp)
movslq %ebx, %rax
movq %rax, -0x70(%rbp)
shll $0x3, %ebx
movl %ebx, -0x58(%rbp)
testl %r15d, %r15d
cmovlel %edi, %r15d
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movl %eax, -0x4c(%rbp)
pushq $0x2
popq %rax
movl %eax, -0x30(%rbp)
movl %ecx, -0x2c(%rbp)
movq %r15, -0x90(%rbp)
movl %r13d, -0x38(%rbp)
movq %r14, -0x98(%rbp)
movq %r12, -0x88(%rbp)
cmpl -0x4c(%rbp), %edi
je 0xfd5f
movq %rdi, %rsi
shrq $0x3, %rsi
addq %r12, %rsi
movl %edi, %eax
notl %eax
andl $0x7, %eax
movl %eax, -0x54(%rbp)
movq %r15, %rbx
movl -0x30(%rbp), %eax
movl %eax, %edx
movl -0x2c(%rbp), %eax
movl %eax, %r12d
movl -0x34(%rbp), %r8d
subq $0x1, %rbx
jb 0xfd40
movl -0x5c(%rbp), %ecx
movq %rdx, -0xa0(%rbp)
leal -0x2(%rdx), %r14d
movl %ecx, %eax
imull %r14d, %eax
leal -0x1(%rcx), %edx
cltq
cmpl $0x1, %edx
ja 0xfbd7
movq -0x48(%rbp), %rcx
movzbl (%rcx,%rax), %eax
cvtsi2ss %eax, %xmm0
jmp 0xfc31
addl $-0x3, %ecx
xorps %xmm0, %xmm0
cmpl $0x1, %ecx
ja 0xfc31
movq -0x48(%rbp), %rdx
movzbl (%rdx,%rax), %ecx
cvtsi2ss %ecx, %xmm1
movzbl 0x1(%rdx,%rax), %ecx
cvtsi2ss %ecx, %xmm0
mulss 0x8cc1(%rip), %xmm0     # 0x188c0
mulss 0x8cbd(%rip), %xmm1     # 0x188c4
addss %xmm0, %xmm1
movzbl 0x2(%rdx,%rax), %eax
cvtsi2ss %eax, %xmm0
mulss 0x8cac(%rip), %xmm0     # 0x188c8
addss %xmm1, %xmm0
ucomiss 0x8c91(%rip), %xmm0     # 0x188b8
jbe 0xfc31
movss 0x8c87(%rip), %xmm0     # 0x188b8
movq %rdi, %r13
ucomiss 0x8c91(%rip), %xmm0     # 0x188cc
movl %r8d, -0x3c(%rbp)
movq %rsi, %r15
jb 0xfc4a
sqrtss %xmm0, %xmm0
jmp 0xfc4f
callq 0x85f0
cvtss2sd %xmm0, %xmm0
mulsd 0x8c95(%rip), %xmm0     # 0x188f0
cvtsd2ss %xmm0, %xmm0
movss 0x8c51(%rip), %xmm1     # 0x188b8
minss %xmm0, %xmm1
xorps %xmm0, %xmm0
maxss %xmm1, %xmm0
callq 0x8130
movss 0x8c39(%rip), %xmm1     # 0x188b8
subss %xmm0, %xmm1
cvttss2si %xmm1, %ecx
movslq %r14d, %rax
movq -0x98(%rbp), %r14
movzbl (%r14,%rax), %eax
addl %ecx, %eax
movq %r13, %rdi
cmpl -0x58(%rbp), %edi
jge 0xfcc5
xorl %edx, %edx
movl -0x50(%rbp), %ecx
cmpl %ecx, %eax
setge %dl
movl $0x0, %esi
cmovgel %ecx, %esi
movl -0x54(%rbp), %ecx
shll %cl, %edx
subl %esi, %eax
movq %r15, %rsi
addb %dl, (%r15)
movl -0x38(%rbp), %r13d
jmp 0xfccc
movl -0x38(%rbp), %r13d
movq %r15, %rsi
movl -0x3c(%rbp), %r8d
cltd
pushq $0x8
popq %rcx
idivl %ecx
movq -0xa0(%rbp), %rdx
leal -0x1(%rdx), %ecx
cmpl %r13d, %ecx
jge 0xfcec
movslq %ecx, %rcx
addb %al, (%r14,%rcx)
cmpl %r13d, %edx
jge 0xfcf8
movslq %edx, %rcx
addb %al, (%r14,%rcx)
movslq %r12d, %rcx
cmpl %r13d, %r12d
jg 0xfd05
addb %al, -0x1(%r14,%rcx)
cmpl %r13d, %r12d
jge 0xfd0e
addb %al, (%r14,%rcx)
leal 0x1(%r12), %ecx
cmpl %r13d, %ecx
jge 0xfd1f
movslq %ecx, %rcx
addb %al, (%r14,%rcx)
cmpl %r13d, %r8d
jge 0xfd2b
movslq %r8d, %rcx
addb %al, (%r14,%rcx)
movq -0x68(%rbp), %rax
addl %eax, %r8d
addl %eax, %r12d
addl %eax, %edx
addq -0x70(%rbp), %rsi
jmp 0xfba1
incq %rdi
incl -0x34(%rbp)
incl -0x2c(%rbp)
incl -0x30(%rbp)
movq -0x90(%rbp), %r15
movq -0x88(%rbp), %r12
jmp 0xfb72
movq -0x80(%rbp), %rbx
movq -0x70(%rbp), %r13
subq $0x1, %r15
jb 0xfd80
movq %rbx, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0xfdaa
addq %r13, %r12
jmp 0xfd67
movq %r14, %rdi
callq 0x8400
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0xfd96
callq 0x8400
movq -0x78(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop