// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %r8, %r14
movl %ecx, %ebx
movl %edx, %r15d
movq %rsi, %r12
leal (%r15,%r15,2), %eax
leal 0x1f(,%rax,8), %eax
cvtsi2sd %eax, %xmm0
mulsd 0x1852(%rip), %xmm0     # 0x52f8
callq 0x21f0
cvttsd2si %xmm0, %eax
shll $0x2, %eax
movl %eax, -0x34(%rbp)
imull %ebx, %eax
movw $0x4d42, -0x44(%rbp)    # imm = 0x4D42
addl $0x36, %eax
movl %eax, -0x40(%rbp)
movabsq $0x3600000000, %rax     # imm = 0x3600000000
movq %rax, -0x3c(%rbp)
movl $0x28, -0x80(%rbp)
movl %r15d, -0x7c(%rbp)
movl %ebx, -0x78(%rbp)
movl $0x180001, -0x74(%rbp)  # imm = 0x180001
movl %r15d, %eax
imull %ebx, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
xorpd %xmm0, %xmm0
movupd %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
callq 0x2030
movl %ebx, -0x30(%rbp)
testl %ebx, %ebx
jle 0x3b7b
movq (%r14), %rdx
movslq %r15d, %rcx
movl -0x30(%rbp), %ebx
movl %r15d, %r8d
leaq -0x1(%rbx), %rsi
leaq (%rcx,%rcx,2), %rcx
imulq %rcx, %rsi
addq %rsi, %rdx
addq $0x2, %rdx
movq %rcx, %rsi
negq %rsi
leaq 0x2(%rax), %rdi
leaq (%r8,%r8,2), %r8
xorl %r9d, %r9d
testl %r15d, %r15d
jle 0x3b6d
xorl %r10d, %r10d
movb -0x2(%rdx,%r10), %r11b
movb %r11b, -0x2(%rdi,%r10)
movb -0x1(%rdx,%r10), %r11b
movb %r11b, -0x1(%rdi,%r10)
movb (%rdx,%r10), %r11b
movb %r11b, (%rdi,%r10)
addq $0x3, %r10
cmpq %r10, %r8
jne 0x3b48
incq %r9
addq %rsi, %rdx
addq %rcx, %rdi
cmpq %rbx, %r9
jne 0x3b40
movq %rax, -0x50(%rbp)
leaq -0x280(%rbp), %r14
movq %r14, %rdi
callq 0x2200
movq %r14, %rdi
movq %r12, %rsi
movl $0x14, %edx
callq 0x2050
leaq -0x280(%rbp), %rdi
leaq -0x44(%rbp), %rsi
movl $0x2, %edx
callq 0x2160
leaq -0x40(%rbp), %rsi
leaq -0x280(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
leaq -0x3c(%rbp), %rsi
leaq -0x280(%rbp), %rdi
movl $0x2, %edx
callq 0x2160
leaq -0x3a(%rbp), %rsi
leaq -0x280(%rbp), %rdi
movl $0x2, %edx
callq 0x2160
leaq -0x38(%rbp), %rsi
leaq -0x280(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
leaq -0x280(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movl $0x28, %edx
callq 0x2160
movb $0x0, -0x29(%rbp)
cmpl $0x0, -0x30(%rbp)
jle 0x3c7d
leal (%r15,%r15,2), %eax
subl %eax, -0x34(%rbp)
movslq %eax, %rdx
xorl %ebx, %ebx
leaq -0x280(%rbp), %r12
leaq -0x29(%rbp), %r13
movq -0x50(%rbp), %r14
movq %rdx, -0x58(%rbp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x2160
movl -0x34(%rbp), %eax
movl %eax, %r15d
testl %eax, %eax
jle 0x3c6f
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x2160
decl %r15d
jne 0x3c5a
movq -0x58(%rbp), %rdx
addq %rdx, %r14
incl %ebx
cmpl -0x30(%rbp), %ebx
jne 0x3c45
leaq -0x280(%rbp), %rdi
callq 0x2180
movq -0x50(%rbp), %rdi
callq 0x2150
movq 0x432f(%rip), %rax      # 0x7fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x278(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x280(%rbp,%rcx)
callq 0x20e0
leaq -0x188(%rbp), %rdi
callq 0x2080
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3ce0
jmp 0x3ce0
jmp 0x3ce0
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x2230
nop