// File: OpenFirework[P]MyJPEG_Decoder/./bmp/bmp_code.cpp
// Function: MyBMP::BMP::encode(char const*, int, int, unsigned char*&)
void MyBMP::BMP::encode(const char *filename, int width, int height, byte *&bgrdata) {
    BITMAPFILEHEADER header;
    BITMAPINFOHEADER info;
    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;
    header.bfType = 0x4d42;
    header.bfSize = datasize + 54;
    header.bfReserved1 = 0;
    header.bfReserved2 = 0;
    header.bfOffBits = 54;

    info.biSize = 40;
    info.biWidth = width;
    info.biHeight = height;
    info.biPlanes = 1;
    info.biBitCount = 24;
    info.biCompression = 0;
    info.biSizeImage = 0;
    info.biXPelsPerMeter = 0;
    info.biYPelsPerMeter = 0;
    info.biClrUsed = 0;
    info.biClrImportant = 0;

    byte *bmpdata = new byte[width*height*3];
    byte *copyptr = bmpdata;
   // 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)*width + w;
            bmpdata[offset*3] = bgrdata[inflect*3];
            bmpdata[offset*3+1] = bgrdata[inflect*3+1];
            bmpdata[offset*3+2] = bgrdata[inflect*3+2];
        }
    }
    int zeros = bytes_per_row-width*3;
    //保存到文件
    std::ofstream outfile;
    outfile.open(filename, std::ios::out|std::ios::binary);
    outfile.write((char*)&header.bfType, sizeof(header.bfType));
    outfile.write((char*)&header.bfSize, sizeof(header.bfSize));
    outfile.write((char*)&header.bfReserved1, sizeof(header.bfReserved1));
    outfile.write((char*)&header.bfReserved2, sizeof(header.bfReserved2));
    outfile.write((char*)&header.bfOffBits, sizeof(header.bfOffBits));

    outfile.write((char*)&info, sizeof(info));
    byte zero = 0;
    for(int h=0;h<height;h++) { 
        outfile.write((char*)bmpdata, width*3);
        bmpdata = bmpdata + width*3;
        for(int i=0;i<zeros; i++) {
            outfile.write((char*)&zero, 1);
        }
    }

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movl $0x0, -0x5c(%rbp)
imull $0x18, -0x14(%rbp), %eax
addl $0x1f, %eax
cvtsi2sd %eax, %xmm0
movsd 0x1e61(%rip), %xmm1     # 0x86a8
mulsd %xmm1, %xmm0
movsd 0x1e4d(%rip), %xmm1     # 0x86a0
divsd %xmm1, %xmm0
callq 0x2230
cvttsd2si %xmm0, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
imull -0x18(%rbp), %eax
movl %eax, -0x64(%rbp)
movw $0x4d42, -0x30(%rbp)    # imm = 0x4D42
movl -0x64(%rbp), %eax
addl $0x36, %eax
movl %eax, -0x2c(%rbp)
movw $0x0, -0x28(%rbp)
movw $0x0, -0x26(%rbp)
movl $0x36, -0x24(%rbp)
movl $0x28, -0x58(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x50(%rbp)
movw $0x1, -0x4c(%rbp)
movw $0x18, -0x4a(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x14(%rbp), %eax
imull -0x18(%rbp), %eax
imull $0x3, %eax, %eax
movslq %eax, %rdi
callq 0x2030
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x69e4
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x69d4
movl -0x7c(%rbp), %eax
imull -0x14(%rbp), %eax
addl -0x80(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x18(%rbp), %eax
subl $0x1, %eax
subl -0x7c(%rbp), %eax
imull -0x14(%rbp), %eax
addl -0x80(%rbp), %eax
movl %eax, -0x88(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
imull $0x3, -0x88(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x70(%rbp), %rax
imull $0x3, -0x84(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x20(%rbp), %rax
movq (%rax), %rax
imull $0x3, -0x88(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x70(%rbp), %rax
imull $0x3, -0x84(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x20(%rbp), %rax
movq (%rax), %rax
imull $0x3, -0x88(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x70(%rbp), %rax
imull $0x3, -0x84(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x6919
jmp 0x69d6
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x6906
movl -0x5c(%rbp), %eax
movl -0x14(%rbp), %edx
movl %edx, %ecx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
movl %eax, -0x8c(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x2240
movq -0x10(%rbp), %rax
movq %rax, -0x2b8(%rbp)
movl $0x10, %edi
movl $0x4, %esi
callq 0x4a50
movl %eax, -0x2ac(%rbp)
jmp 0x6a25
movl -0x2ac(%rbp), %edx
movq -0x2b8(%rbp), %rsi
leaq -0x290(%rbp), %rdi
callq 0x2050
jmp 0x6a40
leaq -0x290(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x2, %edx
callq 0x21b0
jmp 0x6a57
leaq -0x2c(%rbp), %rsi
leaq -0x290(%rbp), %rdi
movl $0x4, %edx
callq 0x21b0
jmp 0x6a6e
leaq -0x28(%rbp), %rsi
leaq -0x290(%rbp), %rdi
movl $0x2, %edx
callq 0x21b0
jmp 0x6a85
leaq -0x26(%rbp), %rsi
leaq -0x290(%rbp), %rdi
movl $0x2, %edx
callq 0x21b0
jmp 0x6a9c
leaq -0x24(%rbp), %rsi
leaq -0x290(%rbp), %rdi
movl $0x4, %edx
callq 0x21b0
jmp 0x6ab3
leaq -0x290(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movl $0x28, %edx
callq 0x21b0
jmp 0x6aca
movb $0x0, -0x29d(%rbp)
movl $0x0, -0x2a4(%rbp)
movl -0x2a4(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x6b94
movq -0x70(%rbp), %rsi
movl -0x14(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdx
leaq -0x290(%rbp), %rdi
callq 0x21b0
jmp 0x6b07
movq -0x70(%rbp), %rax
imull $0x3, -0x14(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x2a8(%rbp)
movl -0x2a8(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jge 0x6b7e
leaq -0x290(%rbp), %rdi
leaq -0x29d(%rbp), %rsi
movl $0x1, %edx
callq 0x21b0
jmp 0x6b4b
jmp 0x6b4d
movl -0x2a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2a8(%rbp)
jmp 0x6b23
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x298(%rbp)
movl %eax, -0x29c(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x21f0
jmp 0x6bdd
jmp 0x6b80
movl -0x2a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2a4(%rbp)
jmp 0x6adb
leaq -0x290(%rbp), %rdi
callq 0x21d0
jmp 0x6ba2
cmpq $0x0, -0x78(%rbp)
je 0x6bc8
movq -0x78(%rbp), %rax
movq %rax, -0x2c0(%rbp)
cmpq $0x0, %rax
je 0x6bc6
movq -0x2c0(%rbp), %rdi
callq 0x21a0
jmp 0x6bc8
leaq -0x290(%rbp), %rdi
callq 0x21f0
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
movq -0x298(%rbp), %rdi
callq 0x2280
nopl (%rax)