// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/atari-img.c
// Function: de_run_degas
static void de_run_degas(deark *c, de_module_params *mparams)
{
	degasctx *d = NULL;
	struct atari_img_decode_data *adata = NULL;
	de_finfo *fi = NULL;
	i64 pos;
	unsigned int format_code, resolution_code;
	int is_grayscale;
	i64 cmpr_bytes_consumed = 0;

	d = de_malloc(c, sizeof(degasctx));
	adata = de_malloc(c, sizeof(struct atari_img_decode_data));

	adata->pal = d->pal;

	pos = 0;
	format_code = (unsigned int)de_getu16be(pos);
	de_dbg(c, "format code: 0x%04x", format_code);
	resolution_code = format_code & 0x0003;
	d->compression_code = (format_code & 0x8000)>>15;
	de_dbg_indent(c, 1);
	de_dbg(c, "resolution code: %u", resolution_code);
	de_dbg(c, "compression code: %u", d->compression_code);
	de_dbg_indent(c, -1);
	pos += 2;

	switch(resolution_code) {
	case 0:
		adata->bpp = 4;
		adata->w = 320;
		adata->h = 200;
		break;
	case 1:
		adata->bpp = 2;
		adata->w = 640;
		adata->h = 200;
		break;
	case 2:
		adata->bpp = 1;
		adata->w = 640;
		adata->h = 400;
		break;
	default:
		de_dbg(c, "Invalid or unsupported resolution (%u)", resolution_code);
		goto done;
	}
	adata->ncolors = de_pow2(adata->bpp);

	de_dbg(c, "dimensions: %d"DE_CHAR_TIMES"%d, colors: %d", (int)adata->w, (int)adata->h, (int)adata->ncolors);

	d->degas_elite_flag = is_degas_elite(c, d);
	de_declare_fmtf(c, "DEGAS%s %d-color %scompressed",
		d->degas_elite_flag?" Elite":"",
		(int)adata->ncolors,
		d->compression_code?"":"un");

	fmtutil_read_atari_palette(c, c->infile, pos, adata->pal, 16, adata->ncolors, 0);
	pos += 2*16;
	fix_dark_pal(c, adata);

	if(d->compression_code) {
		adata->was_compressed = 1;
		adata->unc_pixels = dbuf_create_membuf(c, 32000, 1);
		dbuf_enable_wbuffer(adata->unc_pixels);

		if(!fmtutil_decompress_packbits(c->infile, pos, c->infile->len-pos, adata->unc_pixels, &cmpr_bytes_consumed))
			goto done;
		dbuf_flush(adata->unc_pixels);

		de_dbg(c, "Compressed bytes found: %d", (int)cmpr_bytes_consumed);
		pos += cmpr_bytes_consumed;
	}
	else {
		i64 avail_bytes = 32000;
		if(pos+32000 > c->infile->len) {
			avail_bytes = c->infile->len - pos;
			de_warn(c, "Unexpected end of file (expected 32000 bytes, got %d)", (int)avail_bytes);
		}
		adata->unc_pixels = dbuf_open_input_subfile(c->infile, pos, avail_bytes);
		pos += avail_bytes;
	}

	if(pos + 32 == c->infile->len) {
		do_degas_anim_fields(c, d, pos);
	}

	is_grayscale = de_is_grayscale_palette(adata->pal, adata->ncolors);

	adata->img = de_bitmap_create(c, adata->w, adata->h, is_grayscale?1:3);

	fi = de_finfo_create(c);
	fmtutil_atari_set_standard_density(c, adata, fi);

	fmtutil_atari_decode_image(c, adata);

	de_bitmap_write_to_file_finfo(adata->img, fi, 0);

done:
	if(adata) {
		dbuf_close(adata->unc_pixels);
		de_bitmap_destroy(adata->img);
		de_free(c, adata);
	}
	de_finfo_destroy(c, fi);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
andq $0x0, 0x8(%rsp)
pushq $0x48
popq %rsi
callq 0xdecaf
movq %rax, %r13
pushq $0x40
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r15
leaq 0x8(%r13), %rax
movq %rax, 0x30(%r15)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xe3f46
movq %rax, %r14
leaq 0xe5610(%rip), %rsi     # 0x1065dc
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
movl %r14d, %r12d
andl $0x3, %r12d
shrl $0xf, %r14d
andl $0x1, %r14d
movl %r14d, (%r13)
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
leaq 0xe55f0(%rip), %rsi     # 0x1065f0
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movl (%r13), %edx
leaq 0xfb000(%rip), %rsi     # 0x11c018
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0xe525c(%rip), %rax     # 0x106290
movslq (%rax,%r12,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movaps 0xe51ec(%rip), %xmm0    # 0x106230
pushq $0x4
jmp 0x21078
movaps 0xe51f1(%rip), %xmm0    # 0x106240
pushq $0x1
jmp 0x21078
leaq 0xe55aa(%rip), %rsi     # 0x106604
xorl %r12d, %r12d
pushq $0x3
popq %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
jmp 0x2135d
movaps 0xe51da(%rip), %xmm0    # 0x106250
pushq $0x2
popq %rdi
movq %rdi, (%r15)
movups %xmm0, 0x10(%r15)
callq 0xdfba1
movq %rax, 0x8(%r15)
movl 0x10(%r15), %edx
movl 0x18(%r15), %ecx
leaq 0xe5592(%rip), %rsi     # 0x10662b
movq %rbx, %rdi
movl %eax, %r8d
xorl %eax, %eax
callq 0xde896
leaq 0xe55bb(%rip), %r12     # 0x106668
movq %r13, 0x10(%rsp)
cmpl $0x0, (%r13)
jne 0x210d4
movq 0x18(%rbx), %rax
cmpq $0x7d42, 0x18(%rax)     # imm = 0x7D42
jge 0x2139f
leaq 0x116b8e(%rip), %r12    # 0x137c60
xorl %ebp, %ebp
movq 0x10(%rsp), %r13
movl %ebp, 0x4(%r13)
movl 0x8(%r15), %ecx
cmpl $0x0, (%r13)
leaq 0xe5582(%rip), %rax     # 0x10666f
leaq 0x116b6c(%rip), %r8     # 0x137c60
cmoveq %rax, %r8
leaq 0xe554b(%rip), %rsi     # 0x10664a
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe0824
movq 0x18(%rbx), %rsi
movq 0x8(%r15), %r9
movq 0x30(%r15), %rcx
andl $0x0, (%rsp)
pushq $0x2
popq %rdx
pushq $0x10
popq %r8
movq %rbx, %rdi
callq 0xecea5
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2333b
cmpl $0x0, (%r13)
je 0x211ab
movl $0x1, 0x28(%r15)
pushq $0x1
popq %rdx
movl $0x7d00, %esi           # imm = 0x7D00
movq %rbx, %rdi
callq 0xe59cd
movq %rax, 0x20(%r15)
movq %rax, %rdi
callq 0xe589c
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
addq $-0x22, %rdx
movq 0x20(%r15), %rcx
pushq $0x22
popq %rsi
leaq 0x8(%rsp), %r8
callq 0xeed6a
testl %eax, %eax
je 0x2135a
movq 0x20(%r15), %rdi
callq 0xe58e8
movl 0x8(%rsp), %edx
leaq 0xe54d8(%rip), %rsi     # 0x106672
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x8(%rsp), %r12
jmp 0x211f0
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %r14
movl $0x7d00, %r12d          # imm = 0x7D00
cmpq $0x7d21, %r14           # imm = 0x7D21
jg 0x211e1
addq $-0x22, %r14
leaq 0xe54c0(%rip), %rsi     # 0x10668d
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%rbx), %rdi
movq %r14, %r12
pushq $0x22
popq %rsi
movq %r12, %rdx
callq 0xe6248
movq %rax, 0x20(%r15)
leaq 0x42(%r12), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jne 0x212f2
leaq 0x22(%r12), %r13
leaq 0xe54c5(%rip), %rbp     # 0x1066d4
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x2123f
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f46
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xde9f1
incq %r14
addq $0x2, %r13
jmp 0x21212
leaq 0x2a(%r12), %r13
leaq 0xe54a2(%rip), %rbp     # 0x1066ed
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x2127b
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f46
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xde9f1
incq %r14
addq $0x2, %r13
jmp 0x2124e
leaq 0x32(%r12), %r13
leaq 0xe5480(%rip), %rbp     # 0x106707
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x212b7
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f46
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xde9f1
incq %r14
addq $0x2, %r13
jmp 0x2128a
addq $0x3a, %r12
leaq 0xe5460(%rip), %r13     # 0x106722
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x212f2
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f46
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xde9f1
incq %r14
addq $0x2, %r12
jmp 0x212c5
movq 0x8(%r15), %rsi
movq 0x30(%r15), %rdi
callq 0xe6d06
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x1(,%rcx,2), %ecx
movq %rbx, %rdi
callq 0xe7437
movq %rax, 0x38(%r15)
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xed4e6
movq %rbx, %rdi
movq %r15, %rsi
callq 0xed22a
movq 0x38(%r15), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xe70ab
movq 0x10(%rsp), %r13
jmp 0x2135d
xorl %r12d, %r12d
movq 0x20(%r15), %rdi
callq 0xe5591
movq 0x38(%r15), %rdi
callq 0xe7084
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfd24
movq %rbx, %rdi
movq %r13, %rsi
callq 0xded32
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %r13
movl $0x7d22, %r14d          # imm = 0x7D22
xorl %ebp, %ebp
cmpq $0x7d32, %r14           # imm = 0x7D32
je 0x213d7
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe3f46
cmpq $0xf, %rax
jg 0x210cb
testq %rax, %rax
cmovnel %ebp, %r13d
addq $0x2, %r14
jmp 0x213ab
movl $0x7d32, %r14d          # imm = 0x7D32
xorl %ebp, %ebp
cmpq $0x7d3a, %r14           # imm = 0x7D3A
je 0x2140b
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe3f46
cmpq $0x2, %rax
jg 0x210cb
testq %rax, %rax
cmovnel %ebp, %r13d
addq $0x2, %r14
jmp 0x213df
movl $0x7d3a, %r14d          # imm = 0x7D3A
xorl %ebp, %ebp
cmpq $0x7d42, %r14           # imm = 0x7D42
je 0x21441
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe3f46
cmpq $0x80, %rax
jg 0x210cb
testq %rax, %rax
cmovnel %ebp, %r13d
addq $0x2, %r14
jmp 0x21413
pushq $0x1
popq %rbp
testl %r13d, %r13d
je 0x210d4
movq 0x18(%rbx), %rax
cmpq $0x7d45, 0x18(%rax)     # imm = 0x7D45
jge 0x210cb
jmp 0x210d4