// File: OpenFirework[P]MyJPEG_Decoder/test_main.cpp
// Function: main
int main(int argc, char *argv[]) {
    if(argc !=3 ) {
        std::cout<<"please use as test.bin <jpegpath> <bmppath>\n";
        return  -1;
    }

    MyJPEG::jpegDecoder decoder;
    byte *bgrdata = nullptr;
    int imgwidth = 0;
    int imgheidht = 0;

    /*************jpeg文件解码***********/
    decoder.imgread(argv[1], imgwidth, imgheidht, bgrdata);

    /*************将解码后的bgr数据存储，数据内容为[bgr,bgr,bgr,...,bgr]*************/
    std::ofstream outfile;
    outfile.open("jpeg_decoded_bgr.bin",std::ios::out|std::ios::binary);
    outfile.write((char*)bgrdata, imgwidth*imgheidht*3);
    outfile.close();


    std::cout<<"\n*****jpeg图像解码结束，已将bgr数据存储为二进制数据：jpeg_decoded_bgr.bin******\n\n";
    MyBMP::BMP bmpdecoder;

    /*************BMP文件解码***********/
    byte *bgrdata1 = nullptr;
    int imgwidth1 = 0;
    int imgheidht1 = 0;
    bmpdecoder.decode(argv[2], imgwidth1, imgheidht1, bgrdata1);

    /*************存储BMP文件中的图像数据存储位为二进制bgr数据***********/
    std::ofstream outfile1;
    outfile1.open("bmp_decoded_bgr.bin",std::ios::out|std::ios::binary);
    outfile1.write((char*)bgrdata1, imgwidth1*imgheidht1*3);
    outfile1.close();
    std::cout<<"\n*****bmp图像解码结束，已将bgr数据存储为二进制数据：bmp_decoded_bgr.bin******\n\n";

    /*****将前面解码的jpeg数据转换为bmp数据***********/
    bmpdecoder.encode("jpeg_to_bmp_test.bmp",imgwidth, imgheidht, bgrdata);
    std::cout<<"\n*****测试：将jpeg解码的数据存储位bmp图像，验证正确性，存储文件为：jpeg_to_bmp_test.bmp******\n\n";


    /******resize 测试，将图片缩放位任意尺寸********/
    byte *resizedata = nullptr;
    int resizewidth = 0;
    int resizeheight = 0;
    float scale = 1.5;
    bmpdecoder.resize(bgrdata, imgwidth, imgheidht, resizedata, resizewidth, resizeheight, scale);    
    bmpdecoder.encode("jpeg_resized.bmp",resizewidth, resizeheight, resizedata);
    std::cout<<"图像resize测试（双线型插值法）,图像原尺寸（"<<imgwidth<<","<<imgheidht<<"),resize 后的尺寸为（"<<resizewidth<<","<< \
    resizeheight<<"),resize后的图像为：jpeg_resized.bmp\n";

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

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

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

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x510, %rsp            # imm = 0x510
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x71ae
movq 0x3e3a(%rip), %rdi      # 0xafd0
leaq 0x1767(%rip), %rsi      # 0x8904
callq 0x2130
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x75ee
leaq -0x1c(%rbp), %rdi
movq %rdi, -0x4a8(%rbp)
callq 0x2530
movq -0x4a8(%rbp), %rdi
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x2c(%rbp), %rdx
leaq -0x30(%rbp), %rcx
leaq -0x28(%rbp), %r8
callq 0x3ad0
leaq -0x230(%rbp), %rdi
callq 0x2240
movl $0x10, %edi
movl $0x4, %esi
callq 0x4a50
movl %eax, -0x4a0(%rbp)
jmp 0x7217
movl -0x4a0(%rbp), %edx
leaq 0x170d(%rip), %rsi      # 0x8931
leaq -0x230(%rbp), %rdi
callq 0x2050
jmp 0x7232
movq -0x28(%rbp), %rsi
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %eax
imull %eax, %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdx
leaq -0x230(%rbp), %rdi
callq 0x21b0
jmp 0x7255
leaq -0x230(%rbp), %rdi
callq 0x21d0
jmp 0x7263
movq 0x3d66(%rip), %rdi      # 0xafd0
leaq 0x16d5(%rip), %rsi      # 0x8946
callq 0x2130
jmp 0x7278
leaq -0x274(%rbp), %rdi
callq 0x6110
jmp 0x7286
movq $0x0, -0x280(%rbp)
movl $0x0, -0x284(%rbp)
movl $0x0, -0x288(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq -0x274(%rbp), %rdi
leaq -0x284(%rbp), %rdx
leaq -0x288(%rbp), %rcx
leaq -0x280(%rbp), %r8
callq 0x6120
jmp 0x72d0
leaq -0x488(%rbp), %rdi
callq 0x2240
jmp 0x72de
movl $0x10, %edi
movl $0x4, %esi
callq 0x4a50
movl %eax, -0x4ac(%rbp)
jmp 0x72f5
movl -0x4ac(%rbp), %edx
leaq 0x16aa(%rip), %rsi      # 0x89ac
leaq -0x488(%rbp), %rdi
callq 0x2050
jmp 0x7310
movq -0x280(%rbp), %rsi
movl -0x284(%rbp), %ecx
movl -0x288(%rbp), %eax
imull %eax, %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdx
leaq -0x488(%rbp), %rdi
callq 0x21b0
jmp 0x733c
leaq -0x488(%rbp), %rdi
callq 0x21d0
jmp 0x734a
movq 0x3c7f(%rip), %rdi      # 0xafd0
leaq 0x1668(%rip), %rsi      # 0x89c0
callq 0x2130
jmp 0x735f
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
leaq 0x16b8(%rip), %rsi      # 0x8a24
leaq -0x274(%rbp), %rdi
leaq -0x28(%rbp), %r8
callq 0x6810
jmp 0x737e
movq 0x3c4b(%rip), %rdi      # 0xafd0
leaq 0x16ad(%rip), %rsi      # 0x8a39
callq 0x2130
jmp 0x7393
movq $0x0, -0x490(%rbp)
movl $0x0, -0x494(%rbp)
movl $0x0, -0x498(%rbp)
movl $0x3fc00000, -0x49c(%rbp) # imm = 0x3FC00000
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
movss -0x49c(%rbp), %xmm0
movq %rsp, %rax
leaq -0x498(%rbp), %rsi
movq %rsi, (%rax)
leaq -0x274(%rbp), %rdi
leaq -0x28(%rbp), %rsi
leaq -0x490(%rbp), %r8
leaq -0x494(%rbp), %r9
callq 0x6bf0
jmp 0x73f7
movl -0x494(%rbp), %edx
movl -0x498(%rbp), %ecx
leaq 0x16aa(%rip), %rsi      # 0x8ab4
leaq -0x274(%rbp), %rdi
leaq -0x490(%rbp), %r8
callq 0x6810
jmp 0x741f
movq 0x3baa(%rip), %rdi      # 0xafd0
leaq 0x1698(%rip), %rsi      # 0x8ac5
callq 0x2130
movq %rax, -0x4b8(%rbp)
jmp 0x743b
movq -0x4b8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x2250
movq %rax, -0x4c0(%rbp)
jmp 0x7453
movq -0x4c0(%rbp), %rdi
leaq 0x16a2(%rip), %rsi      # 0x8b03
callq 0x2130
movq %rax, -0x4c8(%rbp)
jmp 0x746f
movq -0x4c8(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x2250
movq %rax, -0x4d0(%rbp)
jmp 0x7487
movq -0x4d0(%rbp), %rdi
leaq 0x1670(%rip), %rsi      # 0x8b05
callq 0x2130
movq %rax, -0x4d8(%rbp)
jmp 0x74a3
movq -0x4d8(%rbp), %rdi
movl -0x494(%rbp), %esi
callq 0x2250
movq %rax, -0x4e0(%rbp)
jmp 0x74be
movq -0x4e0(%rbp), %rdi
leaq 0x1637(%rip), %rsi      # 0x8b03
callq 0x2130
movq %rax, -0x4e8(%rbp)
jmp 0x74da
movq -0x4e8(%rbp), %rdi
movl -0x498(%rbp), %esi
callq 0x2250
movq %rax, -0x4f0(%rbp)
jmp 0x74f5
movq -0x4f0(%rbp), %rdi
leaq 0x161e(%rip), %rsi      # 0x8b21
callq 0x2130
jmp 0x750a
cmpq $0x0, -0x28(%rbp)
je 0x7567
movq -0x28(%rbp), %rax
movq %rax, -0x4f8(%rbp)
cmpq $0x0, %rax
je 0x752e
movq -0x4f8(%rbp), %rdi
callq 0x21a0
jmp 0x7567
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x75e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x488(%rbp), %rdi
callq 0x21f0
jmp 0x75e0
cmpq $0x0, -0x280(%rbp)
je 0x7593
movq -0x280(%rbp), %rax
movq %rax, -0x500(%rbp)
cmpq $0x0, %rax
je 0x7591
movq -0x500(%rbp), %rdi
callq 0x21a0
jmp 0x7593
cmpq $0x0, -0x490(%rbp)
je 0x75bf
movq -0x490(%rbp), %rax
movq %rax, -0x508(%rbp)
cmpq $0x0, %rax
je 0x75bd
movq -0x508(%rbp), %rdi
callq 0x21a0
jmp 0x75bf
movl $0x0, -0x4(%rbp)
leaq -0x488(%rbp), %rdi
callq 0x21f0
leaq -0x230(%rbp), %rdi
callq 0x21f0
jmp 0x75ee
leaq -0x230(%rbp), %rdi
callq 0x21f0
jmp 0x75fa
movl -0x4(%rbp), %eax
addq $0x510, %rsp            # imm = 0x510
popq %rbp
retq
movq -0x238(%rbp), %rdi
callq 0x2280