// File: OpenFirework[P]MyJPEG_Decoder/./jpeg/jpeg_decoder.cpp
// Function: readSOF0(unsigned char*&)
void readSOF0(byte *&jpegdata) {
    std::cout<<"\n************SOF0 INFO*******************\n";
    int length = readSectionLength(jpegdata);
    std::cout<<"本数据段的长度为："<<length<<"\n";
    byte *sofdata = new byte[length-2];  //length 包括前面的长度信息数据所占的2个字节
    memcpy(sofdata, jpegdata, length-2);
    jpegdata = jpegdata + (length-2);

    int Precision = sofdata[0];
    std::cout<<"图片数据精度："<<Precision<<"位精度数据\n";

    int heigh = sofdata[1]*256 + sofdata[2];
    int width = sofdata[3]*256 + sofdata[4];
    std::cout<<"图片高度:"<<heigh<<";图片宽度:"<<width<<"\n";
    image.width = width;
    image.height = heigh;
    int colors = sofdata[5];
    std::cout<<"图片的颜色格式为：";
    if(colors == 1) {
        std::cout<<"灰度图\n";
    } else if(colors == 3) {
        std::cout<<"YCrCb格式彩色图\n";
    } else if(colors == 4) {
        std::cout<<"CMYK格式彩色图\n";
    }  else {
        std::cout<<"解析图像格式错误\n";
    }

    //将每个颜色分量使用的量化表，水平和垂直采样因子存储起来
    int index = 6;
    for(int i=0;i<colors;i++) {
        byte colorid = sofdata[index++];
        subVector[colorid].id = colorid;
        byte tmp = sofdata[index++];
        subVector[colorid].width =  tmp>>4;
        subVector[colorid].height = tmp & 0x0F;
        subVector[colorid].quant = sofdata[index++];

        maxHeight = (maxHeight > subVector[colorid].height ? maxHeight : subVector[colorid].height);
        maxWidth = (maxWidth > subVector[colorid].width ? maxWidth : subVector[colorid].width);
    }

    for(int i=1;i<=colors;i++) {
        std::cout<<"颜色分量ID："<<static_cast<int>(subVector[i].id)<<"\n";
        std::cout<<"水平采样因子："<<static_cast<int>(subVector[i].width)<<"\n";
        std::cout<<"垂直采样因子："<<static_cast<int>(subVector[i].height)<<"\n";
        std::cout<<"量化表ID："<<static_cast<int>(subVector[i].quant)<<"\n";
    }

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq 0x81ed(%rip), %rdi      # 0xafd0
leaq 0x55aa(%rip), %rsi      # 0x8394
callq 0x2130
movq -0x8(%rbp), %rdi
callq 0x3d40
movl %eax, -0xc(%rbp)
movq 0x81ce(%rip), %rdi      # 0xafd0
leaq 0x53e3(%rip), %rsi      # 0x81ec
callq 0x2130
movq %rax, %rdi
movl -0xc(%rbp), %esi
callq 0x2250
movq %rax, %rdi
leaq 0x5b87(%rip), %rsi      # 0x89aa
callq 0x2130
movl -0xc(%rbp), %eax
subl $0x2, %eax
movslq %eax, %rdi
callq 0x2030
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movl -0xc(%rbp), %eax
subl $0x2, %eax
movslq %eax, %rdx
callq 0x2110
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movl -0xc(%rbp), %eax
subl $0x2, %eax
cltq
addq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x1c(%rbp)
movq 0x8153(%rip), %rdi      # 0xafd0
leaq 0x553b(%rip), %rsi      # 0x83bf
callq 0x2130
movq %rax, %rdi
movl -0x1c(%rbp), %esi
callq 0x2250
movq %rax, %rdi
leaq 0x5537(%rip), %rsi      # 0x83d5
callq 0x2130
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
shll $0x8, %eax
movq -0x18(%rbp), %rcx
movzbl 0x2(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x3(%rax), %eax
shll $0x8, %eax
movq -0x18(%rbp), %rcx
movzbl 0x4(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x24(%rbp)
movq 0x80f6(%rip), %rdi      # 0xafd0
leaq 0x5505(%rip), %rsi      # 0x83e6
callq 0x2130
movq %rax, %rdi
movl -0x20(%rbp), %esi
callq 0x2250
movq %rax, %rdi
leaq 0x54f9(%rip), %rsi      # 0x83f4
callq 0x2130
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x2250
movq %rax, %rdi
leaq 0x5a95(%rip), %rsi      # 0x89aa
callq 0x2130
movl -0x24(%rbp), %eax
movl %eax, 0x91a9(%rip)      # 0xc0cc
movl -0x20(%rbp), %eax
movl %eax, 0x919c(%rip)      # 0xc0c8
movq -0x18(%rbp), %rax
movzbl 0x5(%rax), %eax
movl %eax, -0x28(%rbp)
movq 0x8092(%rip), %rdi      # 0xafd0
leaq 0x54be(%rip), %rsi      # 0x8403
callq 0x2130
cmpl $0x1, -0x28(%rbp)
jne 0x2f65
movq 0x8079(%rip), %rdi      # 0xafd0
leaq 0x54c1(%rip), %rsi      # 0x841f
callq 0x2130
jmp 0x2fb2
cmpl $0x3, -0x28(%rbp)
jne 0x2f80
movq 0x805e(%rip), %rdi      # 0xafd0
leaq 0x54b1(%rip), %rsi      # 0x842a
callq 0x2130
jmp 0x2fb0
cmpl $0x4, -0x28(%rbp)
jne 0x2f9b
movq 0x8043(%rip), %rdi      # 0xafd0
leaq 0x54ac(%rip), %rsi      # 0x8440
callq 0x2130
jmp 0x2fae
movq 0x802e(%rip), %rdi      # 0xafd0
leaq 0x54ac(%rip), %rsi      # 0x8455
callq 0x2130
jmp 0x2fb0
jmp 0x2fb2
movl $0x6, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x3101
movq -0x18(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x31(%rbp)
movb -0x31(%rbp), %dl
movzbl -0x31(%rbp), %eax
movl %eax, %ecx
leaq 0x90dc(%rip), %rax      # 0xc0d0
movb %dl, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x32(%rbp)
movzbl -0x32(%rbp), %eax
sarl $0x4, %eax
movb %al, %dl
movzbl -0x31(%rbp), %eax
movl %eax, %ecx
leaq 0x90ab(%rip), %rax      # 0xc0d0
movb %dl, 0x1(%rax,%rcx,4)
movzbl -0x32(%rbp), %eax
andl $0xf, %eax
movb %al, %dl
movzbl -0x31(%rbp), %eax
movl %eax, %ecx
leaq 0x9091(%rip), %rax      # 0xc0d0
movb %dl, 0x2(%rax,%rcx,4)
movq -0x18(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movzbl -0x31(%rbp), %eax
movl %eax, %ecx
leaq 0x906b(%rip), %rax      # 0xc0d0
movb %dl, 0x3(%rax,%rcx,4)
movzbl 0x87e1(%rip), %eax      # 0xb851
movzbl -0x31(%rbp), %ecx
movl %ecx, %edx
leaq 0x9053(%rip), %rcx      # 0xc0d0
movzbl 0x2(%rcx,%rdx,4), %ecx
cmpl %ecx, %eax
jle 0x3091
movb 0x87c5(%rip), %al       # 0xb851
movb %al, -0x39(%rbp)
jmp 0x30a5
movzbl -0x31(%rbp), %eax
movl %eax, %ecx
leaq 0x9032(%rip), %rax      # 0xc0d0
movb 0x2(%rax,%rcx,4), %al
movb %al, -0x39(%rbp)
movb -0x39(%rbp), %al
movb %al, 0x87a3(%rip)       # 0xb851
movzbl 0x879b(%rip), %eax      # 0xb850
movzbl -0x31(%rbp), %ecx
movl %ecx, %edx
leaq 0x900e(%rip), %rcx      # 0xc0d0
movzbl 0x1(%rcx,%rdx,4), %ecx
cmpl %ecx, %eax
jle 0x30d6
movb 0x877f(%rip), %al       # 0xb850
movb %al, -0x3a(%rbp)
jmp 0x30ea
movzbl -0x31(%rbp), %eax
movl %eax, %ecx
leaq 0x8fed(%rip), %rax      # 0xc0d0
movb 0x1(%rax,%rcx,4), %al
movb %al, -0x3a(%rbp)
movb -0x3a(%rbp), %al
movb %al, 0x875d(%rip)       # 0xb850
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2fc0
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0x3209
movq 0x7eb5(%rip), %rdi      # 0xafd0
leaq 0x534d(%rip), %rsi      # 0x846f
callq 0x2130
movq %rax, %rdi
movslq -0x38(%rbp), %rcx
leaq 0x8f9b(%rip), %rax      # 0xc0d0
movzbl (%rax,%rcx,4), %esi
callq 0x2250
movq %rax, %rdi
leaq 0x5862(%rip), %rsi      # 0x89aa
callq 0x2130
movq 0x7e7c(%rip), %rdi      # 0xafd0
leaq 0x5326(%rip), %rsi      # 0x8481
callq 0x2130
movq %rax, %rdi
movslq -0x38(%rbp), %rcx
leaq 0x8f62(%rip), %rax      # 0xc0d0
movzbl 0x1(%rax,%rcx,4), %esi
callq 0x2250
movq %rax, %rdi
leaq 0x5828(%rip), %rsi      # 0x89aa
callq 0x2130
movq 0x7e42(%rip), %rdi      # 0xafd0
leaq 0x5302(%rip), %rsi      # 0x8497
callq 0x2130
movq %rax, %rdi
movslq -0x38(%rbp), %rcx
leaq 0x8f28(%rip), %rax      # 0xc0d0
movzbl 0x2(%rax,%rcx,4), %esi
callq 0x2250
movq %rax, %rdi
leaq 0x57ee(%rip), %rsi      # 0x89aa
callq 0x2130
movq 0x7e08(%rip), %rdi      # 0xafd0
leaq 0x52de(%rip), %rsi      # 0x84ad
callq 0x2130
movq %rax, %rdi
movslq -0x38(%rbp), %rcx
leaq 0x8eee(%rip), %rax      # 0xc0d0
movzbl 0x3(%rax,%rcx,4), %esi
callq 0x2250
movq %rax, %rdi
leaq 0x57b4(%rip), %rsi      # 0x89aa
callq 0x2130
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3108
cmpq $0x0, -0x18(%rbp)
je 0x3229
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0x3227
movq -0x48(%rbp), %rdi
callq 0x21a0
jmp 0x3229
addq $0x50, %rsp
popq %rbp
retq
nop