// File: /workspace/github_cpp_makefile_star2_O2/OpenFirework[P]MyJPEG_Decoder/./jpeg/jpeg_decoder.cpp
// Function: decodeMCU(unsigned char*&, unsigned char*&)
void decodeMCU(byte *&framedata, byte *&bgrdata) {
    int w = (image.width - 1) / (8*maxWidth) + 1; //水平mcu的个数
    int h = (image.height - 1) / (8*maxHeight) + 1; //垂直mcu的个数

    bmpwidth = image.width;
    bmpheight = image.height;
    bgrdata = new byte[bmpwidth*bmpheight*3];
    int bytes_per_pixel = 3;
    int bytes_per_row = bmpwidth*3;
    
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            MCU mcu = readMCU(framedata);
            mcu.decode();
            RGB **b = mcu.toRGB();
            for (int y = i*8*maxHeight; y < (i+1)*8*maxHeight; y++) {  //整个图片上的垂直坐标
                for (int x = j*8*maxWidth; x < (j+1)*8*maxWidth; x++) {  //整个图片上的水平坐标
                    int by = y - i*8*maxHeight;
                    int bx = x - j*8*maxWidth;
                    if(x < 0 || x >= bmpwidth || y < 0 || y >= bmpheight) {
                        continue;
                    } else {
                        byte *pixel;
                        pixel = bgrdata + ( y * bytes_per_row + x * bytes_per_pixel );
                        *( pixel + 2 ) = b[by][bx].R;
                        *( pixel + 1 ) = b[by][bx].G;
                        *( pixel + 0 ) = b[by][bx].B;
                    }
                }
            } // end for 

            //释放每个MCU转换RBG数据的暂存内存
            for (int i = 0; i < maxHeight*8; i++) { 
                if(b[i] != nullptr) {
                     delete[] b[i];
                }
            } 
            if(b != nullptr) {
                delete[] b;
            }
            
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2028, %rsp           # imm = 0x2028
movq %rsi, %rbx
movq %rdi, -0x50(%rbp)
movabsq $0x300000000, %r15      # imm = 0x300000000
movzbl 0x518a(%rip), %esi      # 0x7830
shll $0x3, %esi
pushq $-0x1
popq %rcx
movl %ecx, %eax
cltd
idivl %esi
movl %eax, -0x30(%rbp)
movzbl 0x5176(%rip), %esi      # 0x7831
shll $0x3, %esi
movl %ecx, %eax
cltd
idivl %esi
movl %eax, -0x2c(%rbp)
andl $0x0, 0x5973(%rip)      # 0x8040
andl $0x0, 0x5970(%rip)      # 0x8044
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x2030
movq %rbx, %r14
movq %rax, (%rbx)
movq %r15, %rbx
cmpl -0x2c(%rbp), %r12d
jg 0x288e
leal (,%r12,8), %r15d
movq %r12, -0x48(%rbp)
leal 0x8(,%r12,8), %eax
movl %eax, -0x34(%rbp)
xorl %r13d, %r13d
xorl %eax, %eax
cmpl -0x30(%rbp), %eax
jg 0x2882
leaq -0x2050(%rbp), %r12
movq %r12, %rdi
movq -0x50(%rbp), %rsi
movq %rax, -0x40(%rbp)
callq 0x2e7e
movq %r12, %rdi
callq 0x2ed2
movq %r12, %rdi
callq 0x2ef6
movq %rax, %r12
movzbl 0x50ea(%rip), %edx      # 0x7831
movl %edx, %eax
imull %r15d, %eax
movq -0x40(%rbp), %rcx
leal 0x8(,%rcx,8), %ecx
movzbl %dl, %esi
imull -0x34(%rbp), %esi
cmpl %esi, %eax
jge 0x2836
movzbl 0x50c1(%rip), %r8d      # 0x7830
movl %r8d, %edx
imull %r13d, %edx
movslq %edx, %rdx
movq %rdx, %rsi
imulq %rbx, %rsi
leaq (%rdx,%rdx,2), %rdi
addq $0x2, %rdi
movzbl %r8b, %r9d
movl %ecx, %r10d
imull %r9d, %r10d
movslq %r10d, %r10
cmpq %r10, %rdx
jge 0x2829
movslq 0x589a(%rip), %r10      # 0x8040
cmpq %r10, %rdx
jge 0x281a
cmpl 0x5893(%rip), %eax      # 0x8044
jge 0x281a
movzbl 0x5076(%rip), %r8d      # 0x7831
imull %r15d, %r8d
movl %eax, %r10d
subl %r8d, %r10d
movq (%r14), %r8
movq %rsi, %r11
sarq $0x20, %r11
movslq %r10d, %r10
imull %r13d, %r9d
movslq %r9d, %r9
imulq $-0x3, %r9, %r9
movq (%r12,%r10,8), %rbx
addq %r9, %rbx
movb -0x2(%rdi,%rbx), %bl
movb %bl, 0x2(%r8,%r11)
movq (%r12,%r10,8), %rbx
addq %r9, %rbx
movb -0x1(%rdi,%rbx), %bl
movb %bl, 0x1(%r8,%r11)
movabsq $0x300000000, %rbx      # imm = 0x300000000
addq (%r12,%r10,8), %r9
movb (%rdi,%r9), %r9b
movb %r9b, (%r8,%r11)
movb 0x5016(%rip), %r8b      # 0x7830
incq %rdx
addq %rbx, %rsi
addq $0x3, %rdi
jmp 0x2788
incl %eax
movb 0x5000(%rip), %dl       # 0x7831
jmp 0x2758
xorl %ebx, %ebx
movzbl %dl, %eax
shll $0x3, %eax
cmpq %rax, %rbx
jae 0x285c
movq (%r12,%rbx,8), %rdi
testq %rdi, %rdi
je 0x2857
callq 0x2160
movb 0x4fda(%rip), %dl       # 0x7831
incq %rbx
jmp 0x2838
testq %r12, %r12
je 0x2869
movq %r12, %rdi
callq 0x2160
movq -0x40(%rbp), %rax
incl %eax
addl $0x8, %r13d
movabsq $0x300000000, %rbx      # imm = 0x300000000
jmp 0x270d
movq -0x48(%rbp), %r12
incl %r12d
jmp 0x26e7
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq