// File: /workspace/github_c_makefile_star2_O1/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
movq $0x0, 0x10(%rsp)
movl $0x48, %esi
callq 0xe969b
movq %rax, %r12
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
leaq 0x8(%r12), %rax
movq %rax, 0x30(%r15)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xef169
movq %rax, %r14
leaq 0xf1fac(%rip), %rsi     # 0x11358c
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movl %r14d, %r13d
andl $0x3, %r13d
shrl $0xf, %r14d
andl $0x1, %r14d
movl %r14d, (%r12)
movq %r12, %r14
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
leaq 0xf1f89(%rip), %rsi     # 0x1135a0
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
movl (%r12), %edx
leaq 0x1079f9(%rip), %rsi    # 0x129028
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movl $0xc8, %eax
leaq 0xf1bee(%rip), %rcx     # 0x113240
movslq (%rcx,%r13,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movl $0x140, %ecx            # imm = 0x140
movl $0x4, %edi
jmp 0x2169e
movl $0x190, %eax            # imm = 0x190
movl $0x280, %ecx            # imm = 0x280
movl $0x1, %edi
jmp 0x2169e
leaq 0xf1f35(%rip), %rsi     # 0x1135b4
xorl %r12d, %r12d
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
jmp 0x21a52
movl $0x280, %ecx            # imm = 0x280
movl $0x2, %edi
movq %rdi, (%r15)
movq %rcx, 0x10(%r15)
movq %rax, 0x18(%r15)
callq 0xea7f2
movq %rax, 0x8(%r15)
movl 0x10(%r15), %edx
movl 0x18(%r15), %ecx
leaq 0xf1f1a(%rip), %rsi     # 0x1135db
xorl %ebp, %ebp
movq %rbx, %rdi
movl %eax, %r8d
xorl %eax, %eax
callq 0xe9226
leaq 0xf1f41(%rip), %rdx     # 0x113618
cmpl $0x0, (%r14)
leaq 0x1234ee(%rip), %r12    # 0x144bd0
movq %r14, 0x8(%rsp)
je 0x216f3
movl $0x1, %ebp
jmp 0x217d2
movq 0x18(%rbx), %rax
cmpq $0x7d42, 0x18(%rax)     # imm = 0x7D42
movq %r12, %rdx
jl 0x217d2
movl $0x1, %r13d
movl $0x7d22, %r14d          # imm = 0x7D22
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef169
cmpq $0xf, %rax
jg 0x217ca
testq %rax, %rax
cmovnel %ebp, %r13d
addq $0x2, %r14
cmpq $0x7d32, %r14           # imm = 0x7D32
jne 0x21716
movl $0x7d32, %r14d          # imm = 0x7D32
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef169
cmpq $0x2, %rax
jg 0x217ca
testq %rax, %rax
cmovnel %ebp, %r13d
addq $0x2, %r14
cmpq $0x7d3a, %r14           # imm = 0x7D3A
jne 0x21748
movl $0x7d3a, %r14d          # imm = 0x7D3A
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef169
cmpq $0x80, %rax
jg 0x217ca
testq %rax, %rax
cmovnel %ebp, %r13d
addq $0x2, %r14
cmpq $0x7d42, %r14           # imm = 0x7D42
jne 0x21776
testl %r13d, %r13d
movq 0x8(%rsp), %r14
leaq 0xf1e6b(%rip), %rcx     # 0x113618
je 0x217c0
movq 0x18(%rbx), %rax
cmpq $0x7d44, 0x18(%rax)     # imm = 0x7D44
movq %r12, %rdx
jg 0x217d2
movl $0x1, %ebp
movq %rcx, %rdx
jmp 0x217d2
movq %r12, %rdx
movq 0x8(%rsp), %r14
movl %ebp, 0x4(%r14)
cmpl $0x0, (%r14)
movl 0x8(%r15), %ecx
leaq 0xf1e3a(%rip), %rax     # 0x11361f
cmoveq %rax, %r12
leaq 0xf1e0a(%rip), %rsi     # 0x1135fa
movq %rbx, %rdi
movq %r12, %r8
xorl %eax, %eax
callq 0xeb703
movq 0x18(%rbx), %rsi
movq 0x8(%r15), %r9
movq 0x30(%r15), %rcx
movl $0x0, (%rsp)
movl $0x2, %edx
movl $0x10, %r8d
movq %rbx, %rdi
callq 0xf8c62
movq %rbx, %rdi
movq %r15, %rsi
callq 0x23ae2
cmpl $0x0, (%r14)
je 0x218a6
movl $0x1, 0x28(%r15)
movl $0x7d00, %esi           # imm = 0x7D00
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, 0x20(%r15)
movq %rax, %rdi
callq 0xf0e3f
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
addq $-0x22, %rdx
movq 0x20(%r15), %rcx
leaq 0x10(%rsp), %r8
movl $0x22, %esi
callq 0xfad1a
testl %eax, %eax
je 0x21a4f
movq 0x20(%r15), %rdi
callq 0xf0ea4
movl 0x10(%rsp), %edx
leaq 0xf1d8d(%rip), %rsi     # 0x113622
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x10(%rsp), %r12
jmp 0x218ed
movq 0x18(%rbx), %rax
movq 0x18(%rax), %r14
movl $0x7d00, %r12d          # imm = 0x7D00
cmpq $0x7d21, %r14           # imm = 0x7D21
jg 0x218d8
addq $-0x22, %r14
leaq 0xf1d75(%rip), %rsi     # 0x11363d
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9b27
movq %r14, %r12
movq 0x18(%rbx), %rdi
movl $0x22, %esi
movq %r12, %rdx
callq 0xf1988
movq %rax, 0x20(%r15)
leaq 0x42(%r12), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jne 0x219e7
leaq 0x22(%r12), %r13
leaq 0xf1d78(%rip), %rbp     # 0x113684
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef169
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe93cf
incq %r14
addq $0x2, %r13
cmpq $0x4, %r14
jne 0x2190f
leaq 0x2a(%r12), %r13
leaq 0xf1d57(%rip), %rbp     # 0x11369d
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef169
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe93cf
incq %r14
addq $0x2, %r13
cmpq $0x4, %r14
jne 0x21949
leaq 0x32(%r12), %r13
leaq 0xf1d37(%rip), %rbp     # 0x1136b7
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef169
movq %rbx, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe93cf
incq %r14
addq $0x2, %r13
cmpq $0x4, %r14
jne 0x21983
addq $0x3a, %r12
leaq 0xf1d19(%rip), %r13     # 0x1136d2
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef169
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe93cf
incq %r14
addq $0x2, %r12
cmpq $0x4, %r14
jne 0x219bc
movq 0x8(%r15), %rsi
movq 0x30(%r15), %rdi
callq 0xf2544
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 0xf2cab
movq %rax, 0x38(%r15)
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xf9323
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf900c
movq 0x38(%r15), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xf2917
movq 0x8(%rsp), %r14
jmp 0x21a52
xorl %r12d, %r12d
testq %r15, %r15
je 0x21a74
movq 0x20(%r15), %rdi
callq 0xf0b13
movq 0x38(%r15), %rdi
callq 0xf28d4
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea9b3
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq