// File: /workspace/github_c_makefile_star2_O1/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, %r15d
movl %esi, %ebp
movq %rdi, %r14
leaq 0x564e(%rip), %rsi      # 0xca70
callq 0x3420
testq %rax, %rax
je 0x74b5
movq %rax, %rbx
testl %r12d, %r12d
je 0x7440
movq %rbx, %rdi
callq 0x7740
leaq 0x2a8(%rsp), %r12
movq %r12, %rdi
callq 0x33a0
movq %rax, 0x18(%rsp)
leaq -0x78(%rip), %rax       # 0x73e4
movq %rax, (%r12)
leaq 0x350(%rsp), %rdi
callq 0x3230
testl %eax, %eax
je 0x74d9
leaq 0xbb40(%rip), %rax      # 0x12fb8
movq (%rax), %r15
movq %rbx, %rdi
callq 0x32a0
movq %rax, %rcx
leaq 0x5602(%rip), %rsi      # 0xca8f
xorl %ebp, %ebp
movl %ebp, %eax
movq %r15, %rdi
movq %r14, %rdx
callq 0x3290
leaq 0x18(%rsp), %rdi
callq 0x3360
movq %rbx, %rdi
callq 0x3110
movl %ebp, %eax
jmp 0x762e
leaq 0xbafc(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x55ad(%rip), %rsi      # 0xca73
xorl %ebx, %ebx
movl %ebx, %eax
movq %r14, %rdx
callq 0x3290
movl %ebx, %eax
jmp 0x762e
leaq 0x18(%rsp), %r14
movl $0x290, %edx            # imm = 0x290
movq %r14, %rdi
movl $0x50, %esi
callq 0x32f0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3380
movq %r14, %rdi
movl $0x1, %esi
callq 0x33d0
testl %r15d, %r15d
je 0x7515
movl $0x1, 0x58(%rsp)
leaq 0x18(%rsp), %r14
movl $0x1, 0x44(%r14)
movl %ebp, 0x48(%r14)
movl $0x3, 0x38(%r14)
movl $0x0, 0x64(%r14)
movq %r14, %rdi
callq 0x3260
movl 0x88(%r14), %ebp
xorl %eax, %eax
cmpl $0x1, 0x40(%r14)
setne %cl
movb %cl, %al
leal 0x1(,%rax,2), %r12d
movl 0x8c(%r14), %r14d
movl %r14d, %eax
imull %ebp, %eax
imull %r12d, %eax
movl %eax, %edi
addq $0xc, %rdi
callq 0x3330
testq %rax, %rax
je 0x760c
movl %ebp, (%rax)
movl %r14d, 0x4(%rax)
movq %rax, 0x8(%rsp)
movl %r12d, 0x8(%rax)
movl 0xc0(%rsp), %ecx
cmpl 0xa4(%rsp), %ecx
jae 0x75e8
movq %rax, %r13
addq $0xc, %r13
leaq 0x18(%rsp), %r14
leaq 0x10(%rsp), %r15
movl $0x1, %ebp
imull %r12d, %ecx
imull 0xa0(%rsp), %ecx
movl %ecx, %eax
addq %r13, %rax
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x3280
movl 0xc0(%rsp), %eax
movl %eax, %ecx
cmpl 0xa4(%rsp), %eax
jb 0x75b3
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x3140
movq %rbx, %rdi
callq 0x3110
movq %r14, %rdi
callq 0x3360
movq 0x8(%rsp), %rax
jmp 0x762e
leaq 0xb9a5(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x5492(%rip), %rdi      # 0xcaaf
movl $0x13, %esi
movl $0x1, %edx
callq 0x3490
xorl %eax, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq