// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movl %ecx, %r12d
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %r15
leaq 0x5d8c(%rip), %rsi      # 0xcab0
callq 0x3440
testq %rax, %rax
je 0x6db2
movq %rax, %r13
testl %r12d, %r12d
je 0x6d42
movq %r13, %rdi
callq 0x701a
leaq 0x2a8(%rsp), %r12
movq %r12, %rdi
callq 0x33c0
movq %rax, 0x18(%rsp)
leaq -0x76(%rip), %rax       # 0x6ce8
movq %rax, (%r12)
leaq 0x350(%rsp), %rdi
callq 0x3230
testl %eax, %eax
je 0x6de3
leaq 0xc24e(%rip), %rax      # 0x12fc8
movq (%rax), %r14
movq %r13, %rdi
callq 0x32b0
movq %rax, %rcx
leaq 0x5d40(%rip), %rsi      # 0xcacf
xorl %ebx, %ebx
movl %ebx, %eax
movq %r14, %rdi
movq %r15, %rdx
callq 0x32a0
leaq 0x18(%rsp), %rdi
callq 0x3370
movq %r13, %rdi
callq 0x3110
jmp 0x6dcf
leaq 0xc20f(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
leaq 0x5cf0(%rip), %rsi      # 0xcab3
xorl %ebx, %ebx
movl %ebx, %eax
movq %r15, %rdx
callq 0x32a0
movl %ebx, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18(%rsp), %r15
pushq $0x50
popq %rsi
movl $0x290, %edx            # imm = 0x290
movq %r15, %rdi
callq 0x3300
movq %r15, %rdi
movq %r13, %rsi
callq 0x3390
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x33f0
testl %r14d, %r14d
je 0x6e1b
movl $0x1, 0x58(%rsp)
leaq 0x18(%rsp), %r14
movl $0x1, 0x44(%r14)
movl %ebp, 0x48(%r14)
movl $0x3, 0x38(%r14)
andl $0x0, 0x64(%r14)
movq %r14, %rdi
callq 0x3270
movl 0x88(%r14), %ebp
xorl %eax, %eax
cmpl $0x1, 0x40(%r14)
setne %cl
movb %cl, %al
leal 0x1(,%rax,2), %r12d
movl 0x8c(%r14), %ebx
movl %ebx, %eax
imull %ebp, %eax
imull %r12d, %eax
movl %eax, %edi
addq $0xc, %rdi
callq 0x3340
testq %rax, %rax
je 0x6f03
movq %r13, 0x8(%rsp)
movl %ebp, (%rax)
movl %ebx, 0x4(%rax)
movq %rax, (%rsp)
movl %r12d, 0x8(%rax)
movq %rax, %r13
addq $0xc, %r13
leaq 0x18(%rsp), %r14
leaq 0x10(%rsp), %r15
pushq $0x1
popq %rbp
movl 0xc0(%rsp), %eax
cmpl %ebx, %eax
jae 0x6edb
imull %r12d, %eax
imull 0xa0(%rsp), %eax
addq %r13, %rax
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x3290
movl 0xa4(%rsp), %ebx
jmp 0x6ea6
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x3140
movq 0x8(%rsp), %rdi
callq 0x3110
movq %r14, %rdi
callq 0x3370
movq (%rsp), %rax
jmp 0x6dd1
leaq 0xc0be(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x5bdb(%rip), %rdi      # 0xcaef
pushq $0x13
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
xorl %eax, %eax
jmp 0x6dd1