// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rdi, -0x50(%rbp)
movzbl 0x5fc2(%rip), %ebx      # 0x8830
movzbl 0x5fbc(%rip), %ecx      # 0x8831
shll $0x3, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cltd
idivl %ecx
movl %eax, %r15d
xorl %eax, %eax
movl %eax, 0x67b5(%rip)      # 0x9040
movq $0x0, -0x38(%rbp)
movl %eax, 0x67ab(%rip)      # 0x9044
xorl %edi, %edi
callq 0x2030
movq %r14, -0x58(%rbp)
movq %rax, (%r14)
movl %r15d, -0x44(%rbp)
testl %r15d, %r15d
js 0x2ac7
movabsq $0x300000000, %r14      # imm = 0x300000000
shll $0x3, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cltd
idivl %ebx
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
js 0x2ab3
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 -0x2058(%rbp), %rbx
movq %rbx, %rdi
movq -0x50(%rbp), %rsi
callq 0x3139
movq %rbx, %rdi
callq 0x3348
movq %rbx, %rdi
callq 0x334e
movq %rbx, %rdi
callq 0x3354
movq %rbx, %rdi
callq 0x3176
movq %rax, %r15
movzbl 0x5efc(%rip), %r8d      # 0x8831
movl %r12d, %eax
imull %r8d, %eax
movl %r8d, %ecx
imull -0x30(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2a61
movq -0x40(%rbp), %rcx
leal (,%rcx,8), %r9d
leal 0x8(,%rcx,8), %edx
movb 0x5ecb(%rip), %sil      # 0x8830
movl %r9d, -0x48(%rbp)
movzbl %sil, %r10d
movl %r9d, %ecx
imull %r10d, %ecx
movl %edx, %edi
imull %r10d, %edi
cmpl %edi, %ecx
jge 0x2a4f
movl %r13d, %ecx
imull %r10d, %ecx
movslq %ecx, %rdi
movl 0x66ad(%rip), %r11d     # 0x9040
movl 0x66ab(%rip), %ecx      # 0x9044
movq %rdi, %r8
imulq %r14, %r8
leaq (%rdi,%rdi,2), %r9
addq $0x2, %r9
movslq %r11d, %rbx
cmpq %rbx, %rdi
jge 0x2a24
cmpl %ecx, %eax
jge 0x2a24
movzbl 0x5e76(%rip), %ecx      # 0x8831
imull %r12d, %ecx
movl %eax, %esi
subl %ecx, %esi
movq -0x58(%rbp), %rcx
movq (%rcx), %rcx
movq %r8, %r11
sarq $0x20, %r11
movslq %esi, %rsi
movq (%r15,%rsi,8), %rbx
imull %r13d, %r10d
movslq %r10d, %r10
leaq (%r10,%r10,2), %r10
subq %r10, %rbx
movb -0x2(%rbx,%r9), %bl
movb %bl, 0x2(%rcx,%r11)
movq (%r15,%rsi,8), %rbx
subq %r10, %rbx
movb -0x1(%rbx,%r9), %bl
movb %bl, 0x1(%rcx,%r11)
movq (%r15,%rsi,8), %rsi
subq %r10, %rsi
movb (%rsi,%r9), %sil
movb %sil, (%rcx,%r11)
movl 0x6629(%rip), %r11d     # 0x9040
movl 0x6627(%rip), %ecx      # 0x9044
movb 0x5e0c(%rip), %sil      # 0x8830
incq %rdi
movzbl %sil, %r10d
movl %edx, %ebx
imull %r10d, %ebx
movslq %ebx, %rbx
addq %r14, %r8
addq $0x3, %r9
cmpq %rbx, %rdi
jl 0x29a8
movb 0x5de6(%rip), %r8b      # 0x8831
movl -0x48(%rbp), %r9d
incl %eax
movzbl %r8b, %ecx
imull -0x30(%rbp), %ecx
cmpl %ecx, %eax
jl 0x2969
testb %r8b, %r8b
je 0x2a8e
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
testq %rdi, %rdi
je 0x2a7d
callq 0x2150
movb 0x5db4(%rip), %r8b      # 0x8831
incq %rbx
movzbl %r8b, %eax
shll $0x3, %eax
cmpq %rax, %rbx
jb 0x2a68
jmp 0x2a93
testq %r15, %r15
je 0x2a9b
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 0x28f7
movq -0x38(%rbp), %rcx
leal 0x1(%rcx), %eax
cmpl -0x44(%rbp), %ecx
movq %rax, -0x38(%rbp)
jne 0x28cc
addq $0x2038, %rsp           # imm = 0x2038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop