// File: /workspace/github_c_makefile_star2_O0/Matthias-Wandel[P]imgcomp/src/jpeg2mem.c
// Function: LoadJPEG
MemImage_t * LoadJPEG(char* FileName, int scale_denom, int discard_colors, int ParseExif)
{
    unsigned long data_size;    // length of the file
    struct jpeg_decompress_struct info; //for our jpeg info
    struct my_error_mgr jerr;
    MemImage_t *MemImage;
    int components;
    FILE* file = fopen(FileName, "rb");

    if(file == NULL) {
       fprintf(Log, "Could not open file: \"%s\"!\n", FileName);
       return NULL;
    }

    if (ParseExif){
        // Get the exif header
        ReadExifPart(file);
    }

    info.err = jpeg_std_error(& jerr.pub);     
    jerr.pub.error_exit = my_error_exit; // Override library's default exit on error.


    if (setjmp(jerr.setjmp_buffer)) {
        // If we get here, the JPEG code has signaled an error.
        // We need to clean up the JPEG object, close the input file, and return.
		fprintf(Log, "Error reading jpeg \"%s\" at %ld\n", FileName, ftell(file));
        jpeg_destroy_decompress(&info);
        fclose(file);
        return NULL;
    }

    jpeg_create_decompress(& info);   // fills info structure

    jpeg_stdio_src(&info, file);    
    jpeg_read_header(&info, TRUE);   // read jpeg file header

    if (discard_colors) info.out_color_space = JCS_GRAYSCALE;

    info.scale_num = 1;
    info.scale_denom = scale_denom;

    info.num_components = 3;
    info.do_fancy_upsampling = FALSE;

    jpeg_start_decompress(&info);    // decompress the file

    components = info.out_color_space == JCS_GRAYSCALE ? 1 : 3;

    data_size = info.output_width 
              * info.output_height * components;

    MemImage = malloc(data_size+offsetof(MemImage_t, pixels));
    if (!MemImage){
        fprintf(Log, "Image malloc failed");
        return 0;
    }
    MemImage->width = info.output_width;
    MemImage->height = info.output_height;
    MemImage->components = components;

    //--------------------------------------------
    // read scanlines one at a time. Assumes an RGB image
    //--------------------------------------------
    while (info.output_scanline < info.output_height){ // loop
        unsigned char * rowptr[1];  // pointer to an array
        // Enable jpeg_read_scanlines() to fill our jdata array
        rowptr[0] = MemImage->pixels + 
            components * info.output_width * info.output_scanline; 
        jpeg_read_scanlines(&info, rowptr, 1);
    }
    //---------------------------------------------------

    jpeg_finish_decompress(&info);   //finish decompressing
    fclose(file);                    //close the file

    jpeg_destroy_decompress(&info);

    return MemImage;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x460, %rsp            # imm = 0x460
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x7111(%rip), %rsi      # 0xfa54
callq 0x3420
movq %rax, -0x440(%rbp)
cmpq $0x0, -0x440(%rbp)
jne 0x8982
leaq 0xc6b8(%rip), %rax      # 0x15018
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x70e9(%rip), %rsi      # 0xfa57
movb $0x0, %al
callq 0x3290
movq $0x0, -0x8(%rbp)
jmp 0x8be0
cmpl $0x0, -0x1c(%rbp)
je 0x8994
movq -0x440(%rbp), %rdi
callq 0x8d20
leaq -0x428(%rbp), %rdi
callq 0x33a0
movq %rax, -0x2b8(%rbp)
leaq -0xce(%rip), %rax       # 0x88e0
movq %rax, -0x428(%rbp)
leaq -0x428(%rbp), %rdi
addq $0xa8, %rdi
callq 0x3230
cmpl $0x0, %eax
je 0x8a39
leaq 0xc644(%rip), %rax      # 0x15018
movq (%rax), %rax
movq %rax, -0x458(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x450(%rbp)
movq -0x440(%rbp), %rdi
callq 0x32a0
movq -0x458(%rbp), %rdi
movq -0x450(%rbp), %rdx
movq %rax, %rcx
leaq 0x7066(%rip), %rsi      # 0xfa73
movb $0x0, %al
callq 0x3290
leaq -0x2b8(%rbp), %rdi
callq 0x3360
movq -0x440(%rbp), %rdi
callq 0x3110
movq $0x0, -0x8(%rbp)
jmp 0x8be0
leaq -0x2b8(%rbp), %rdi
movl $0x50, %esi
movl $0x290, %edx            # imm = 0x290
callq 0x32f0
movq -0x440(%rbp), %rsi
leaq -0x2b8(%rbp), %rdi
callq 0x3380
leaq -0x2b8(%rbp), %rdi
movl $0x1, %esi
callq 0x33d0
cmpl $0x0, -0x18(%rbp)
je 0x8a83
movl $0x1, -0x278(%rbp)
movl $0x1, -0x274(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x270(%rbp)
movl $0x3, -0x280(%rbp)
movl $0x0, -0x254(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x3260
movl -0x278(%rbp), %edx
movl $0x3, %eax
movl $0x1, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, -0x434(%rbp)
movl -0x230(%rbp), %eax
imull -0x22c(%rbp), %eax
imull -0x434(%rbp), %eax
movl %eax, %eax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
addq $0xc, %rdi
callq 0x3330
movq %rax, -0x430(%rbp)
cmpq $0x0, -0x430(%rbp)
jne 0x8b2f
leaq 0xc507(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x6f78(%rip), %rsi      # 0xfa93
movb $0x0, %al
callq 0x3290
movq $0x0, -0x8(%rbp)
jmp 0x8be0
movl -0x230(%rbp), %ecx
movq -0x430(%rbp), %rax
movl %ecx, (%rax)
movl -0x22c(%rbp), %ecx
movq -0x430(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x434(%rbp), %ecx
movq -0x430(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x210(%rbp), %eax
cmpl -0x22c(%rbp), %eax
jae 0x8bb1
movq -0x430(%rbp), %rax
addq $0xc, %rax
movl -0x434(%rbp), %ecx
imull -0x230(%rbp), %ecx
imull -0x210(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x448(%rbp)
leaq -0x448(%rbp), %rsi
leaq -0x2b8(%rbp), %rdi
movl $0x1, %edx
callq 0x3280
jmp 0x8b5e
leaq -0x2b8(%rbp), %rdi
callq 0x3140
movq -0x440(%rbp), %rdi
callq 0x3110
leaq -0x2b8(%rbp), %rdi
callq 0x3360
movq -0x430(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x460, %rsp            # imm = 0x460
popq %rbp
retq
nopl (%rax)