// File: 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
subq $0x340, %rsp            # imm = 0x340
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2d0(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2170
movq -0x10(%rbp), %rax
movq %rax, -0x2c8(%rbp)
movl $0x8, %edi
movl $0x4, %esi
callq 0x4a50
movl %eax, -0x2c0(%rbp)
jmp 0x6178
movl -0x2c0(%rbp), %edx
movq -0x2c8(%rbp), %rsi
leaq -0x230(%rbp), %rdi
callq 0x20d0
jmp 0x6193
xorl %eax, %eax
movl %eax, %esi
leaq -0x230(%rbp), %rdi
movl $0x2, %edx
callq 0x21c0
jmp 0x61aa
leaq -0x230(%rbp), %rdi
callq 0x2260
movq %rdx, -0x2e0(%rbp)
movq %rax, -0x2d8(%rbp)
jmp 0x61c6
movq -0x2e0(%rbp), %rax
movq -0x2d8(%rbp), %rcx
movq %rcx, -0x250(%rbp)
movq %rax, -0x248(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x4a70
movq %rax, -0x2e8(%rbp)
jmp 0x61f7
movq -0x2e8(%rbp), %rax
movl %eax, -0x240(%rbp)
xorl %edx, %edx
movl %edx, %esi
leaq -0x230(%rbp), %rdi
callq 0x21c0
jmp 0x6216
movq 0x4db3(%rip), %rdi      # 0xafd0
leaq 0x248c(%rip), %rsi      # 0x86b0
callq 0x2130
movq %rax, -0x2f0(%rbp)
jmp 0x6232
movq -0x2f0(%rbp), %rdi
movl -0x240(%rbp), %esi
callq 0x2250
movq %rax, -0x2f8(%rbp)
jmp 0x624d
movq -0x2f8(%rbp), %rdi
leaq 0x274f(%rip), %rsi      # 0x89aa
callq 0x2130
jmp 0x6262
movslq -0x240(%rbp), %rdi
callq 0x2030
movq %rax, -0x300(%rbp)
jmp 0x6277
movq -0x300(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x258(%rbp), %rax
movq %rax, -0x260(%rbp)
movq -0x258(%rbp), %rsi
movslq -0x240(%rbp), %rdx
leaq -0x230(%rbp), %rdi
callq 0x2190
jmp 0x62af
leaq -0x230(%rbp), %rdi
callq 0x2060
jmp 0x62bd
movq -0x2d0(%rbp), %rdi
movq -0x258(%rbp), %rax
movw (%rax), %ax
movw %ax, (%rdi)
movq -0x258(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x258(%rbp)
movq -0x258(%rbp), %rax
movl (%rax), %eax
movl %eax, 0x4(%rdi)
movq -0x258(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x258(%rbp)
movq -0x258(%rbp), %rax
movw (%rax), %ax
movw %ax, 0x8(%rdi)
movq -0x258(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x258(%rbp)
movq -0x258(%rbp), %rax
movw (%rax), %ax
movw %ax, 0xa(%rdi)
movq -0x258(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x258(%rbp)
movq -0x258(%rbp), %rax
movl (%rax), %eax
movl %eax, 0xc(%rdi)
movq -0x258(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x258(%rbp)
movups (%rdi), %xmm0
movaps %xmm0, -0x270(%rbp)
movq -0x270(%rbp), %rsi
movq -0x268(%rbp), %rdx
callq 0x6690
jmp 0x637e
movq -0x2d0(%rbp), %rdi
movq -0x258(%rbp), %rax
movq 0x20(%rax), %rcx
movq %rcx, 0x30(%rdi)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq -0x258(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x258(%rbp)
movq 0x30(%rdi), %rax
movq %rax, -0x280(%rbp)
movups 0x10(%rdi), %xmm0
movups 0x20(%rdi), %xmm1
movaps %xmm1, -0x290(%rbp)
movaps %xmm0, -0x2a0(%rbp)
movq -0x280(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x20(%rax)
movaps -0x2a0(%rbp), %xmm0
movaps -0x290(%rbp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x6700
jmp 0x6400
movq -0x2d0(%rbp), %rax
movl 0x18(%rax), %edx
movq -0x20(%rbp), %rcx
movl %edx, (%rcx)
movl 0x14(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x2a4(%rbp)
movq -0x18(%rbp), %rax
imull $0x18, (%rax), %eax
addl $0x1f, %eax
cvtsi2sd %eax, %xmm0
movsd 0x226f(%rip), %xmm1     # 0x86a8
mulsd %xmm1, %xmm0
movsd 0x225b(%rip), %xmm1     # 0x86a0
divsd %xmm1, %xmm0
callq 0x2230
movq -0x2d0(%rbp), %rcx
cvttsd2si %xmm0, %eax
movl %eax, -0x2a8(%rbp)
movl -0x2a8(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x2a4(%rbp)
movl -0x2a4(%rbp), %eax
movq -0x20(%rbp), %rdx
imull (%rdx), %eax
movl %eax, -0x2ac(%rbp)
movl -0x240(%rbp), %eax
subl 0xc(%rcx), %eax
cmpl 0x24(%rcx), %eax
je 0x64e3
movq -0x2d0(%rbp), %rcx
movl -0x240(%rbp), %eax
subl 0xc(%rcx), %eax
cmpl -0x2ac(%rbp), %eax
je 0x64e1
movq 0x4b22(%rip), %rdi      # 0xafd0
leaq 0x220f(%rip), %rsi      # 0x86c4
callq 0x2130
jmp 0x64bc
jmp 0x64e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2070
jmp 0x6676
jmp 0x64e3
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movq -0x18(%rbp), %rax
movl (%rax), %eax
imull %eax, %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x2030
movq %rax, -0x308(%rbp)
jmp 0x6508
movq -0x308(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x2b0(%rbp)
movl -0x2b0(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x6635
movl $0x0, -0x2b4(%rbp)
movl -0x2b4(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x661f
movl -0x2b0(%rbp), %eax
movq -0x18(%rbp), %rcx
imull (%rcx), %eax
addl -0x2b4(%rbp), %eax
movl %eax, -0x2b8(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
subl $0x1, %eax
subl -0x2b0(%rbp), %eax
imull -0x2a4(%rbp), %eax
imull $0x3, -0x2b4(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x2bc(%rbp)
movq -0x258(%rbp), %rax
movslq -0x2bc(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movq (%rax), %rax
imull $0x3, -0x2b8(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x258(%rbp), %rax
movl -0x2bc(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movq (%rax), %rax
imull $0x3, -0x2b8(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x258(%rbp), %rax
movl -0x2bc(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movq (%rax), %rax
imull $0x3, -0x2b8(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x2b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b4(%rbp)
jmp 0x653c
jmp 0x6621
movl -0x2b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2b0(%rbp)
jmp 0x6520
cmpq $0x0, -0x260(%rbp)
je 0x6661
movq -0x260(%rbp), %rax
movq %rax, -0x310(%rbp)
cmpq $0x0, %rax
je 0x665f
movq -0x310(%rbp), %rdi
callq 0x21a0
jmp 0x6661
leaq -0x230(%rbp), %rdi
callq 0x2070
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
movq -0x238(%rbp), %rdi
callq 0x2280
nopw %cs:(%rax,%rax)