// File: /workspace/github_cpp_makefile_star2_O1/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 $0x2038, %rsp           # imm = 0x2038
movq %rsi, %rbx
movq %rdi, -0x50(%rbp)
movzbl 0x5ff1(%rip), %r15d     # 0x8830
movzbl 0x5feb(%rip), %ecx      # 0x8831
shll $0x3, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cltd
idivl %ecx
movl %eax, %r14d
xorl %eax, %eax
movl %eax, 0x67e4(%rip)      # 0x9040
movq $0x0, -0x38(%rbp)
movl %eax, 0x67da(%rip)      # 0x9044
xorl %edi, %edi
callq 0x2030
movq %rbx, -0x60(%rbp)
movq %rax, (%rbx)
movl %r14d, -0x44(%rbp)
testl %r14d, %r14d
js 0x2a7f
movabsq $0x300000000, %r14      # imm = 0x300000000
shll $0x3, %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cltd
idivl %r15d
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
js 0x2a6b
movq -0x38(%rbp), %rax
leal (,%rax,8), %r12d
leal 0x8(,%rax,8), %eax
movl %eax, -0x30(%rbp)
xorl %r13d, %r13d
movq $0x0, -0x40(%rbp)
leaq -0x2060(%rbp), %rbx
movq %rbx, %rdi
movq -0x50(%rbp), %rsi
callq 0x32ca
movq %rbx, %rdi
callq 0x34ce
movq %rbx, %rdi
callq 0x34de
movq %rbx, %rdi
callq 0x34ee
movq %rbx, %rdi
callq 0x3312
movq %rax, %r15
movzbl 0x5f2a(%rip), %ecx      # 0x8831
movl %r12d, %eax
imull %ecx, %eax
imull -0x30(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2a1b
movq -0x40(%rbp), %rcx
leal (,%rcx,8), %edx
movl %edx, -0x48(%rbp)
leal 0x8(,%rcx,8), %edx
movslq %r13d, %rcx
movq %rcx, -0x58(%rbp)
movzbl 0x5ef3(%rip), %r10d     # 0x8830
movl -0x48(%rbp), %edi
imull %r10d, %edi
movl %edx, %r8d
imull %r10d, %r8d
cmpl %r8d, %edi
jge 0x2a06
movq -0x58(%rbp), %rdi
imulq %r10, %rdi
movl %r10d, %r8d
imull %r13d, %r8d
movslq %r8d, %r8
leaq (%r8,%r8,2), %r9
addq $0x2, %r9
imulq %r14, %r8
movslq 0x66c7(%rip), %r11      # 0x9040
cmpq %r11, %rdi
jge 0x29e4
cmpl 0x66c0(%rip), %eax      # 0x9044
jge 0x29e4
movzbl 0x5ea3(%rip), %r11d     # 0x8831
imull %r12d, %r11d
movl %eax, %ebx
subl %r11d, %ebx
movq -0x60(%rbp), %rcx
movq (%rcx), %r11
movq %r8, %rsi
sarq $0x20, %rsi
movslq %ebx, %rbx
movq (%r15,%rbx,8), %rcx
imull %r13d, %r10d
movslq %r10d, %r10
leaq (%r10,%r10,2), %r10
subq %r10, %rcx
movb -0x2(%rcx,%r9), %cl
movb %cl, 0x2(%r11,%rsi)
movq (%r15,%rbx,8), %rcx
subq %r10, %rcx
movb -0x1(%rcx,%r9), %cl
movb %cl, 0x1(%r11,%rsi)
movq (%r15,%rbx,8), %rcx
subq %r10, %rcx
movb (%rcx,%r9), %cl
movb %cl, (%r11,%rsi)
incq %rdi
movzbl 0x5e41(%rip), %r10d     # 0x8830
movslq %edx, %rcx
imulq %r10, %rcx
addq %r14, %r8
addq $0x3, %r9
cmpq %rcx, %rdi
jl 0x2972
incl %eax
movzbl 0x5e22(%rip), %edi      # 0x8831
imull -0x30(%rbp), %edi
cmpl %edi, %eax
jl 0x2935
cmpb $0x0, 0x5e0f(%rip)      # 0x8831
je 0x2a46
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
testq %rdi, %rdi
je 0x2a34
callq 0x2150
incq %rbx
movzbl 0x5df3(%rip), %eax      # 0x8831
shll $0x3, %eax
cmpq %rax, %rbx
jb 0x2a26
testq %r15, %r15
je 0x2a53
movq %r15, %rdi
callq 0x2150
movq -0x40(%rbp), %rcx
leal 0x1(%rcx), %eax
addl $0x8, %r13d
cmpl -0x2c(%rbp), %ecx
movq %rax, -0x40(%rbp)
jne 0x28ca
movq -0x38(%rbp), %rcx
leal 0x1(%rcx), %eax
cmpl -0x44(%rbp), %ecx
movq %rax, -0x38(%rbp)
jne 0x289f
addq $0x2038, %rsp           # imm = 0x2038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop