// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x368, %rsp            # imm = 0x368
movq %rdi, %r15
leaq 0x40(%rdi), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x57880
movb 0x39(%r15), %al
cmpb $0x1, %al
jne 0x62e8d
movq 0x28(%r15), %rcx
movq (%rcx), %rcx
cmpb $-0x1, (%rcx)
jne 0x63068
cmpb $-0x28, 0x1(%rcx)
je 0x62ece
jmp 0x63068
leaq 0x8(%r15), %rdx
leaq 0x30(%rsp), %r14
pushq $0x3
popq %rsi
pushq $0x2
popq %rbp
movq %r14, %rdi
movl %ebp, %ecx
callq 0x59a20
movq %r14, %rdi
movl %ebp, %esi
callq 0x56df0
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x565f0
cmpq $0xffd8, %r14           # imm = 0xFFD8
jne 0x63068
movb 0x39(%r15), %al
testb $0x1, %al
je 0x62ed7
xorl %r14d, %r14d
jmp 0x62f02
leaq 0x8(%r15), %rdi
leaq 0x9f2e4(%rip), %rsi     # 0x1021c6
callq 0x58b90
leaq 0x80456(%rip), %rsi     # 0xe3344
movq %rax, %rdi
callq 0x5a1d0
movq %rax, %r14
testq %rax, %rax
je 0x63068
leaq 0x2c0(%rsp), %rdi
callq 0x56af0
leaq 0x30(%rsp), %rdi
movq %rax, (%rdi)
leaq -0xd7(%rip), %rcx       # 0x62e47
movq %rcx, (%rax)
pushq $0x50
popq %rsi
movl $0x290, %edx            # imm = 0x290
callq 0x57b20
cmpb $0x1, 0x39(%r15)
jne 0x62f43
movq 0x28(%r15), %rax
testq %rax, %rax
je 0x62f52
movq (%rax), %rsi
jmp 0x62f54
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x584b0
jmp 0x62f62
xorl %esi, %esi
movslq 0x30(%r15), %rdx
leaq 0x30(%rsp), %rdi
callq 0x587b0
leaq 0x30(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x563c0
cmpl $0x1, %eax
jne 0x6304f
movq %r14, 0x10(%rsp)
movq %r15, 0x18(%rsp)
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x576c0
movl 0x94(%r14), %edx
imull 0x88(%r14), %edx
movq 0x8(%r14), %rax
pushq $0x1
popq %rcx
movq %r14, %rdi
movl %ecx, %esi
callq *0x10(%rax)
movq %rax, %r13
movq 0x88(%r14), %rax
leaq 0x20(%rsp), %r15
movq %rax, (%r15)
pushq $-0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x58ea0
leaq 0x8(%rsp), %r14
movl 0xd8(%rsp), %eax
cmpl 0xbc(%rsp), %eax
jae 0x6307d
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x59020
pushq $0x2
popq %rbp
xorl %r12d, %r12d
movslq 0xb8(%rsp), %rax
cmpq %rax, %r12
jge 0x62fd2
movl 0xd8(%rsp), %eax
decl %eax
movl %r12d, 0x8(%rsp)
movl %eax, 0xc(%rsp)
movq (%r13), %rax
movzwl -0x2(%rax,%rbp), %ecx
movzbl (%rax,%rbp), %eax
shll $0x10, %eax
orq %rcx, %rax
movq %rax, 0x28(%rsp)
andl $0x0, 0x20(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x57160
incq %r12
addq $0x3, %rbp
jmp 0x62ffc
leaq 0x30(%rsp), %rdi
callq 0x56510
cmpb $0x0, 0x39(%r15)
jne 0x63068
movq %r14, %rdi
callq 0x575b0
movq %rbx, %rax
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x59040
movq %r14, %rdi
callq 0x56510
movq 0x18(%rsp), %rax
cmpb $0x0, 0x39(%rax)
movq 0x10(%rsp), %r14
je 0x63060
jmp 0x63068
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x565f0
movq %rbx, %rdi
callq 0x58260
nopl (%rax)