// File: /workspace/github_c_makefile_star2_O0/enzo1982[P]smooth/classes/graphics/imageloader/jpeg.cpp
// Function: smooth::GUI::ImageLoaderJPEG::Load()
const S::GUI::Bitmap &S::GUI::ImageLoaderJPEG::Load()
{
	/* Make sure bitmap is initialized.
	 */
	bitmap = NIL;

	/* Check magic number.
	 */
	if	( gotBuffer && (buffer[0] != 0xFF || buffer[1] != 0xD8))			     return bitmap;
	else if (!gotBuffer && InStream(STREAM_FILE, fileName, IS_READ).InputNumberRaw(2) != 0xFFD8) return bitmap;

	/* Open stream.
	 */
	FILE	 *stream = NIL;

	if (!gotBuffer)
	{
#ifdef __WIN32__
		stream = _wfopen(fileName, L"rb");
#else
		stream = fopen(fileName.ConvertTo("UTF-8"), "rb");
#endif

		if (stream == NIL) return bitmap;
	}

	/* This struct contains the JPEG decompression parameters and pointers to
	 * working space (which is allocated as needed by the JPEG library).
	 */
	jpeg_decompress_struct	 cinfo;

	/* We set up the normal JPEG error routines, then override error_exit.
	 */
	jpeg_error_mgr	 jerr;

	cinfo.err = jpeg_std_error(&jerr);
	cinfo.err->error_exit = my_error_exit;

	/* Now we can initialize the JPEG decompression object.
	 */
	jpeg_create_decompress(&cinfo);

	/* Specify data source (eg, a file)
	 */
	if (gotBuffer) jpeg_mem_src(&cinfo, buffer, buffer.Size());
	else	       jpeg_stdio_src(&cinfo, stream);

	/* Read file parameters with jpeg_read_header()
	 */
	if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)
	{
		jpeg_destroy_decompress(&cinfo);

		if (!gotBuffer) fclose(stream);

		return bitmap;
	}

	/* We can ignore the return value from jpeg_read_header since
	 *   (a) suspension is not possible with the stdio data source, and
	 *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
	 * See libjpeg.doc for more info.
	 */

	/* Start decompressor
	 */
	jpeg_start_decompress(&cinfo);

	/* We can ignore the return value since suspension is not possible
	 * with the stdio data source.
	 */

	/* We may need to do some setup of our own at this point before reading
	 * the data.  After jpeg_start_decompress() we have the correct scaled
	 * output image dimensions available, as well as the output colormap
	 * if we asked for color quantization.
	 * In this example, we need to make an output work buffer of the right size.
	 */

	/* JSAMPLEs per row in output buffer
	 */
	int		 row_stride = cinfo.output_width * cinfo.output_components;

	/* Make a one-row-high sample array that will go away when done with image
	 */
	JSAMPARRAY	 buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

	bitmap.CreateBitmap(Size(cinfo.output_width, cinfo.output_height));

	/* Here we use the library's state variable cinfo.output_scanline as the
	 * loop counter, so that we don't have to keep track ourselves.
	 */
	while (cinfo.output_scanline < cinfo.output_height)
	{
		/* jpeg_read_scanlines expects an array of pointers to scanlines.
		 * Here the array is only one element long, but you could ask for
		 * more than one scanline at a time if that's more convenient.
		 */
		jpeg_read_scanlines(&cinfo, buffer, 1);

		/* TODO: The copying step needs lots of optimization
		 */
		for (Int x = 0; x < (signed) cinfo.output_width; x++)
		{
			bitmap.SetPixel(Point(x, cinfo.output_scanline - 1), Color(buffer[0][3 * x], buffer[0][3 * x + 1], buffer[0][3 * x + 2], Color::RGB));
		}
	}

	/* We can ignore the return value since suspension is not possible
	 * with the stdio data source.
	 */
	jpeg_finish_decompress(&cinfo);

	/* This is an important step since it will release a good deal of memory.
	 */
	jpeg_destroy_decompress(&cinfo);

	/* After finish_decompress, we can close the input file.
	 * Here we postpone it until after no more JPEG errors are possible,
	 * so as to simplify the setjmp error logic above.  (Actually, I don't
	 * think that jpeg_destroy can do an error exit, but why assume anything...)
	 */
	if (!gotBuffer) fclose(stream);

	/* At this point you may want to check to see whether any corrupt-data
	 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
	 */

	/* And we're done!
	 */
	return bitmap;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4f0, %rsp            # imm = 0x4F0
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x4b0(%rbp)
addq $0x40, %rdi
xorl %esi, %esi
callq 0x5ac20
movq -0x4b0(%rbp), %rax
testb $0x1, 0x39(%rax)
je 0x713b1
movq -0x4b0(%rbp), %rdi
addq $0x20, %rdi
xorl %esi, %esi
callq 0x717e0
movzbl (%rax), %eax
cmpl $0xff, %eax
jne 0x7139d
movq -0x4b0(%rbp), %rdi
addq $0x20, %rdi
movl $0x1, %esi
callq 0x717e0
movzbl (%rax), %eax
cmpl $0xd8, %eax
je 0x713b1
movq -0x4b0(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x8(%rbp)
jmp 0x717c4
movq -0x4b0(%rbp), %rax
movb 0x39(%rax), %cl
movb $0x0, -0x119(%rbp)
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x4b1(%rbp)
jne 0x71435
movq -0x4b0(%rbp), %rdx
addq $0x8, %rdx
leaq -0x118(%rbp), %rdi
movq %rdi, -0x4d0(%rbp)
movl $0x3, %esi
movl $0x2, %ecx
movl %ecx, -0x4c4(%rbp)
callq 0x5d200
movq -0x4d0(%rbp), %rdi
movl -0x4c4(%rbp), %esi
movb $0x1, -0x119(%rbp)
callq 0x59fd0
movq %rax, -0x4c0(%rbp)
jmp 0x7141f
movq -0x4c0(%rbp), %rax
cmpq $0xffd8, %rax           # imm = 0xFFD8
setne %al
movb %al, -0x4b1(%rbp)
movb -0x4b1(%rbp), %al
movb %al, -0x4d1(%rbp)
testb $0x1, -0x119(%rbp)
jne 0x7144c
jmp 0x71458
leaq -0x118(%rbp), %rdi
callq 0x59710
movb -0x4d1(%rbp), %al
testb $0x1, %al
jne 0x71464
jmp 0x714a6
movq -0x4b0(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x8(%rbp)
jmp 0x717c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
testb $0x1, -0x119(%rbp)
jne 0x71495
jmp 0x714a1
leaq -0x118(%rbp), %rdi
callq 0x59710
jmp 0x717d1
jmp 0x714a8
movq -0x4b0(%rbp), %rax
movq $0x0, -0x138(%rbp)
testb $0x1, 0x39(%rax)
jne 0x7150d
movq -0x4b0(%rbp), %rdi
addq $0x8, %rdi
leaq 0x134714(%rip), %rsi    # 0x1a5be6
callq 0x5c120
movq %rax, %rdi
leaq 0x115e63(%rip), %rsi    # 0x187344
callq 0x5db10
movq %rax, -0x138(%rbp)
cmpq $0x0, -0x138(%rbp)
jne 0x7150b
movq -0x4b0(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x8(%rbp)
jmp 0x717c4
jmp 0x7150d
leaq -0x470(%rbp), %rdi
callq 0x59c80
movq %rax, -0x3c8(%rbp)
movq -0x3c8(%rbp), %rax
leaq -0x20e(%rip), %rcx      # 0x71320
movq %rcx, (%rax)
leaq -0x3c8(%rbp), %rdi
movl $0x50, %esi
movl $0x290, %edx            # imm = 0x290
callq 0x5aef0
movq -0x4b0(%rbp), %rax
testb $0x1, 0x39(%rax)
je 0x71593
movq -0x4b0(%rbp), %rdi
addq $0x20, %rdi
callq 0x71100
movq -0x4b0(%rbp), %rdi
movq %rax, -0x4e0(%rbp)
addq $0x20, %rdi
callq 0x71140
movq -0x4e0(%rbp), %rsi
movslq %eax, %rdx
leaq -0x3c8(%rbp), %rdi
callq 0x5bcc0
jmp 0x715a6
movq -0x138(%rbp), %rsi
leaq -0x3c8(%rbp), %rdi
callq 0x5b950
leaq -0x3c8(%rbp), %rdi
movl $0x1, %esi
callq 0x59480
cmpl $0x1, %eax
je 0x715f5
leaq -0x3c8(%rbp), %rdi
callq 0x59610
movq -0x4b0(%rbp), %rax
testb $0x1, 0x39(%rax)
jne 0x715e1
movq -0x138(%rbp), %rdi
callq 0x5a8f0
movq -0x4b0(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x8(%rbp)
jmp 0x717c4
leaq -0x3c8(%rbp), %rdi
callq 0x5aa20
movl -0x340(%rbp), %eax
imull -0x334(%rbp), %eax
movl %eax, -0x474(%rbp)
movq -0x3c0(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x474(%rbp), %edx
leaq -0x3c8(%rbp), %rdi
movl $0x1, %ecx
movl %ecx, %esi
callq *%rax
movq %rax, %rcx
movq -0x4b0(%rbp), %rax
movq %rcx, -0x480(%rbp)
addq $0x40, %rax
movq %rax, -0x4e8(%rbp)
movl -0x340(%rbp), %esi
movl -0x33c(%rbp), %edx
leaq -0x488(%rbp), %rdi
callq 0x59260
movq -0x4e8(%rbp), %rdi
leaq -0x488(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5c4d0
movl -0x320(%rbp), %eax
cmpl -0x33c(%rbp), %eax
jae 0x71784
movq -0x480(%rbp), %rsi
leaq -0x3c8(%rbp), %rdi
movl $0x1, %edx
callq 0x5c680
movl $0x0, -0x48c(%rbp)
movl -0x48c(%rbp), %eax
cmpl -0x340(%rbp), %eax
jge 0x7177f
movq -0x4b0(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x4f0(%rbp)
movl -0x48c(%rbp), %esi
movl -0x320(%rbp), %edx
subl $0x1, %edx
leaq -0x494(%rbp), %rdi
callq 0x5c2c0
movq -0x480(%rbp), %rax
movq (%rax), %rax
imull $0x3, -0x48c(%rbp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %esi
movq -0x480(%rbp), %rax
movq (%rax), %rax
imull $0x3, -0x48c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
movq -0x480(%rbp), %rax
movq (%rax), %rax
imull $0x3, -0x48c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %ecx
leaq -0x4a8(%rbp), %rdi
xorl %r8d, %r8d
callq 0x5a6b0
movq -0x4f0(%rbp), %rdi
leaq -0x494(%rbp), %rsi
leaq -0x4a8(%rbp), %rdx
callq 0x5a3d0
movl -0x48c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48c(%rbp)
jmp 0x716b5
jmp 0x71681
leaq -0x3c8(%rbp), %rdi
callq 0x5c6a0
leaq -0x3c8(%rbp), %rdi
callq 0x59610
movq -0x4b0(%rbp), %rax
testb $0x1, 0x39(%rax)
jne 0x717b5
movq -0x138(%rbp), %rdi
callq 0x5a8f0
movq -0x4b0(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x4f0, %rsp            # imm = 0x4F0
popq %rbp
retq
movq -0x128(%rbp), %rdi
callq 0x5b6b0
nopl (%rax)