// File: /workspace/github_cpp_makefile_star2_O3/OpenFirework[P]MyJPEG_Decoder/./bmp/bmp_code.cpp
// Function: MyBMP::BMP::decode(char const*, int&, int&, unsigned char*&)
void MyBMP::BMP::decode(const char *filename, int &width, int &height, byte *&bgrdata) {
    std::ifstream infile;
    infile.open(filename, std::ios::in|std::ios::binary);
    infile.seekg(0, infile.end);
    int bmpdatalength = infile.tellg();
    infile.seekg(0, infile.beg);
    std::cout << "bmp data length is " << bmpdatalength<<"\n";
    //all data
    byte *bmpdata = new byte[bmpdatalength];
    byte *copyptr = bmpdata;  //保存首地址，便于以后释放
    infile.read((char*)bmpdata, bmpdatalength);  //读取编码的jpeg文件数据到内存中
    infile.close();

    //read file header
    memcpy(&fileHeader.bfType, bmpdata, sizeof(fileHeader.bfType));
    bmpdata = bmpdata + sizeof(fileHeader.bfType);
    memcpy(&fileHeader.bfSize, bmpdata, sizeof(fileHeader.bfSize));
    bmpdata = bmpdata + sizeof(fileHeader.bfSize);
    memcpy(&fileHeader.bfReserved1, bmpdata, sizeof(fileHeader.bfReserved1));
    bmpdata = bmpdata + sizeof(fileHeader.bfReserved1);
    memcpy(&fileHeader.bfReserved2, bmpdata, sizeof(fileHeader.bfReserved2));
    bmpdata = bmpdata + sizeof(fileHeader.bfReserved2);
    memcpy(&fileHeader.bfOffBits, bmpdata, sizeof(fileHeader.bfOffBits));
    bmpdata = bmpdata + sizeof(fileHeader.bfOffBits);
    showBmpHead(fileHeader);

    //read info header
    memcpy(&infoHeader, bmpdata, sizeof(BITMAPINFOHEADER));
    bmpdata = bmpdata + sizeof(BITMAPINFOHEADER);
    showBmpInfoHead(infoHeader);

    height = infoHeader.biHeight;
    width = infoHeader.biWidth;
   
    int bytes_per_row = 0;  
    int tmp = floor((24*width + 31)*1.0/32);
    bytes_per_row = tmp*4;    
    int datasize = bytes_per_row*height;
    if((bmpdatalength - fileHeader.bfOffBits) != (infoHeader.biSizeImage)) {
        if((bmpdatalength - fileHeader.bfOffBits) != datasize) {
            std::cout<<"******BMP DATA is WRONG******\n";
        }
    }
    //给外部使用的，不能删除
    bgrdata = new byte[height*width*3];

    //bmp的数据存储顺序是从左下到右上，而一般的习惯是从左上到右下，因此做一个转置
    for(int h=0;h<height;h++) {
        for(int w=0;w<width;w++) {
            int offset = h*width + w;
            int inflect = (height - 1 -h)*bytes_per_row + w*3;
            bgrdata[offset*3] = bmpdata[inflect];
            bgrdata[offset*3+1] = bmpdata[inflect+1];
            bgrdata[offset*3+2] = bmpdata[inflect+2];

        }
    }
    
    if(copyptr != nullptr) {
        delete[] copyptr;
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movq %r8, %rbx
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, -0x30(%rbp)
leaq -0x278(%rbp), %r13
movq %r13, %rdi
callq 0x2120
movq %r13, %rdi
movq %r12, %rsi
movl $0xc, %edx
callq 0x20c0
leaq -0x278(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2170
leaq -0x278(%rbp), %rdi
callq 0x2220
movq %rax, %r12
leaq -0x278(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2170
movq 0x4a7e(%rip), %rdi      # 0x7fd0
leaq 0x1da7(%rip), %rsi      # 0x5300
movl $0x13, %edx
callq 0x2130
movq 0x4a66(%rip), %rdi      # 0x7fd0
movl %r12d, %esi
callq 0x2210
leaq 0x2085(%rip), %rsi      # 0x55fe
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r12, -0x40(%rbp)
movslq %r12d, %r12
movq %r12, %rdi
callq 0x2030
movq %rax, %r13
leaq -0x278(%rbp), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x2140
leaq -0x278(%rbp), %rdi
callq 0x2060
movzwl (%r13), %eax
movq -0x30(%rbp), %r12
movw %ax, (%r12)
movl 0x2(%r13), %eax
movl %eax, 0x4(%r12)
movzwl 0x6(%r13), %eax
movw %ax, 0x8(%r12)
movzwl 0x8(%r13), %eax
movw %ax, 0xa(%r12)
movl 0xa(%r13), %eax
movl %eax, 0xc(%r12)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
callq 0x37a8
movq 0x2e(%r13), %rax
movq %rax, 0x30(%r12)
movups 0xe(%r13), %xmm0
movups 0x1e(%r13), %xmm1
movups %xmm1, 0x20(%r12)
movups %xmm0, 0x10(%r12)
movq 0x2e(%r13), %rax
movq %rax, -0x50(%rbp)
movups 0xe(%r13), %xmm0
movq %r13, -0x38(%rbp)
movups 0x1e(%r13), %xmm1
movaps %xmm1, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movq -0x50(%rbp), %rax
movq %rax, 0x20(%rsp)
movaps -0x70(%rbp), %xmm0
movaps -0x60(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x3812
movl 0x18(%r12), %eax
movl %eax, (%r14)
movl 0x14(%r12), %r13d
movl %r13d, (%r15)
leal (%r13,%r13,2), %eax
leal 0x1f(,%rax,8), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x1c77(%rip), %xmm0     # 0x52f8
callq 0x21f0
cvttsd2si %xmm0, %r12d
movq -0x30(%rbp), %rsi
movl 0xc(%rsi), %eax
movq -0x40(%rbp), %rcx
subl %eax, %ecx
movl (%r14), %edx
cmpl 0x24(%rsi), %ecx
je 0x36cb
movl %r12d, %eax
imull %edx, %eax
shll $0x2, %eax
cmpl %eax, %ecx
je 0x36cb
movq 0x491c(%rip), %rdi      # 0x7fd0
leaq 0x1c59(%rip), %rsi      # 0x5314
movl $0x1e, %edx
callq 0x2130
movl (%r14), %edx
movl (%r15), %r13d
movl %edx, -0x30(%rbp)
movl %edx, %eax
imull %r13d, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x2030
movq %rax, (%rbx)
movl -0x30(%rbp), %esi
testl %esi, %esi
jle 0x375f
shll $0x2, %r12d
movq -0x38(%rbp), %rax
addq $0x38, %rax
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %r13d, %r13d
jle 0x3757
xorl %esi, %esi
movq %rax, %rdi
imull %ecx, %r13d
addl %esi, %r13d
movl (%r14), %r8d
addl %edx, %r8d
imull %r12d, %r8d
movslq %r8d, %r8
movb -0x2(%rdi,%r8), %r9b
movq (%rbx), %r10
movslq %r13d, %r11
leaq (%r11,%r11,2), %r11
movb %r9b, (%r10,%r11)
movb -0x1(%rdi,%r8), %r9b
movq (%rbx), %r10
movb %r9b, 0x1(%r10,%r11)
movb (%rdi,%r8), %r8b
movq (%rbx), %r9
movb %r8b, 0x2(%r9,%r11)
incl %esi
movl (%r15), %r13d
addq $0x3, %rdi
cmpl %r13d, %esi
jl 0x3706
movl (%r14), %esi
incl %ecx
decl %edx
cmpl %esi, %ecx
jl 0x36fc
movq -0x38(%rbp), %rdi
callq 0x2150
leaq -0x278(%rbp), %rdi
callq 0x2070
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3790
jmp 0x3790
jmp 0x3790
jmp 0x3790
jmp 0x3790
movq %rax, %rbx
leaq -0x278(%rbp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2230
nop