// File: /workspace/github_cpp_makefile_star2_O1/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 $0x268, %rsp            # imm = 0x268
movq %r8, %rbx
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
leaq -0x268(%rbp), %rdi
callq 0x2120
leaq -0x268(%rbp), %rdi
movq %r12, %rsi
movl $0xc, %edx
callq 0x20c0
leaq -0x268(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2170
leaq -0x268(%rbp), %rdi
callq 0x2220
movq %rax, %r12
leaq -0x268(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2170
movq 0x48da(%rip), %rdi      # 0x7fd0
leaq 0x1c03(%rip), %rsi      # 0x5300
movl $0x13, %edx
callq 0x2130
movq 0x48c2(%rip), %rdi      # 0x7fd0
movl %r12d, %esi
callq 0x2210
leaq 0x1ee1(%rip), %rsi      # 0x55fe
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r12, -0x38(%rbp)
movslq %r12d, %r12
movq %r12, %rdi
callq 0x2030
leaq -0x268(%rbp), %rdi
movq %rax, -0x30(%rbp)
movq %rax, %rsi
movq %r12, %rdx
callq 0x2140
leaq -0x268(%rbp), %rdi
callq 0x2060
movq -0x30(%rbp), %r12
movzwl (%r12), %eax
movw %ax, (%r13)
movl 0x2(%r12), %eax
movl %eax, 0x4(%r13)
movzwl 0x6(%r12), %eax
movw %ax, 0x8(%r13)
movzwl 0x8(%r12), %eax
movw %ax, 0xa(%r13)
movl 0xa(%r12), %eax
movl %eax, 0xc(%r13)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
callq 0x3938
movq 0x2e(%r12), %rax
movq %rax, 0x30(%r13)
movups 0xe(%r12), %xmm0
movups 0x1e(%r12), %xmm1
movups %xmm1, 0x20(%r13)
movups %xmm0, 0x10(%r13)
movq 0x2e(%r12), %rax
movq %rax, -0x40(%rbp)
movups 0xe(%r12), %xmm0
movups 0x1e(%r12), %xmm1
movaps %xmm1, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq %rax, 0x20(%rsp)
movaps -0x60(%rbp), %xmm0
movaps -0x50(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x39a2
movl 0x18(%r13), %eax
movl %eax, (%r14)
movl 0x14(%r13), %eax
movl %eax, (%r15)
leal (%rax,%rax,2), %eax
leal 0x1f(,%rax,8), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x1ad8(%rip), %xmm0     # 0x52f8
callq 0x21f0
cvttsd2si %xmm0, %r12d
shll $0x2, %r12d
movl 0xc(%r13), %eax
movq -0x38(%rbp), %rcx
subl %eax, %ecx
cmpl 0x24(%r13), %ecx
je 0x3861
movl (%r14), %eax
imull %r12d, %eax
cmpl %eax, %ecx
je 0x3861
movq 0x4780(%rip), %rdi      # 0x7fd0
leaq 0x1abd(%rip), %rsi      # 0x5314
movl $0x1e, %edx
callq 0x2130
movl (%r14), %eax
imull (%r15), %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x2030
movq %rax, (%rbx)
cmpl $0x0, (%r14)
movq -0x30(%rbp), %rdi
jle 0x38f3
movq %rdi, %rax
addq $0x38, %rax
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl (%r15), %r8d
testl %r8d, %r8d
jle 0x38ea
xorl %esi, %esi
movq %rax, %r13
imull %ecx, %r8d
addl %esi, %r8d
movl (%r14), %r9d
addl %edx, %r9d
imull %r12d, %r9d
movslq %r9d, %r9
movb -0x2(%r13,%r9), %r10b
movq (%rbx), %r11
movslq %r8d, %r8
leaq (%r8,%r8,2), %r8
movb %r10b, (%r11,%r8)
movb -0x1(%r13,%r9), %r10b
movq (%rbx), %r11
movb %r10b, 0x1(%r11,%r8)
movb (%r13,%r9), %r9b
movq (%rbx), %r10
movb %r9b, 0x2(%r10,%r8)
incl %esi
movl (%r15), %r8d
addq $0x3, %r13
cmpl %r8d, %esi
jl 0x389b
incl %ecx
decl %edx
cmpl (%r14), %ecx
jl 0x388e
callq 0x2150
leaq -0x268(%rbp), %rdi
callq 0x2070
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3920
jmp 0x3920
jmp 0x3920
jmp 0x3920
jmp 0x3920
movq %rax, %rbx
leaq -0x268(%rbp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2230
nop