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

	d = de_malloc(c, sizeof(tinyctx));

	adata = de_malloc(c, sizeof(struct atari_img_decode_data));
	adata->pal = d->pal;
	adata->was_compressed = 1;

	d->res_code = de_getbyte(pos);
	pos++;
	de_dbg(c, "resolution code: %u", (UI)d->res_code);

	switch(d->res_code) {
	case 0: case 3:
		adata->bpp = 4;
		adata->w = 320;
		adata->h = 200;
		break;
	case 1: case 4:
		adata->bpp = 2;
		adata->w = 640;
		adata->h = 200;
		break;
	case 2: case 5:
		adata->bpp = 1;
		adata->w = 640;
		adata->h = 400;
		break;
	default:
		de_err(c, "Invalid resolution code (%u). This is not a Tiny Stuff file.",
			(UI)d->res_code);
		goto done;
	}

	de_dbg_dimensions(c, adata->w, adata->h);
	adata->ncolors = de_pow2(adata->bpp);
	de_dbg(c, "colors: %d", (int)adata->ncolors);

	if(d->res_code>=3) {
		de_warn(c, "This image uses palette cycling animation, which is not supported.");
		pos += 4; // skip animation_info
	}

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

	d->num_control_bytes = de_getu16be(pos);
	pos += 2;
	de_dbg(c, "number of RLE control bytes: %d", (int)d->num_control_bytes);

	d->num_data_words = de_getu16be(pos);
	pos += 2;
	d->data_words_nbytes = 2*d->num_data_words;
	de_dbg(c, "number of RLE data words: %d (%d bytes)", (int)d->num_data_words,
		(int)d->data_words_nbytes);

	// It seems that files are often padded to the next multiple of 128 bytes,
	// so don't warn about that.
	expected_min_file_size = pos + d->num_control_bytes + d->data_words_nbytes;
	expected_max_file_size = de_pad_to_n(expected_min_file_size, 128);
	de_dbg(c, "expected file size: %"I64_FMT" or %"I64_FMT, expected_min_file_size,
		expected_max_file_size);
	if(c->infile->len<expected_min_file_size || c->infile->len>expected_max_file_size) {
		de_warn(c, "Expected file size to be %"I64_FMT", but it is %"I64_FMT".",
			expected_min_file_size, c->infile->len);
	}

	adata->unc_pixels = dbuf_create_membuf(c, 32000, 1);

	if(!tiny_uncompress(c, d, adata, pos)) {
		goto done;
	}

	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);

	do_tinystuff_image(c, d, adata);
	de_bitmap_write_to_file_finfo(adata->img, fi, 0);

done:
	if(adata) {
		de_bitmap_destroy(adata->img);
		dbuf_close(adata->unc_pixels);
		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 $0x48, %rsp
movq %rdi, %r14
movl $0xa8, %esi
callq 0xdecaf
movq %rax, %rbx
pushq $0x40
popq %rsi
movq %r14, %rdi
callq 0xdecaf
movq %rax, %r15
leaq 0x20(%rbx), %rax
movq %rax, 0x30(%r15)
movl $0x1, 0x28(%r15)
movq 0x18(%r14), %rdi
xorl %esi, %esi
callq 0xe3eb4
movb %al, (%rbx)
movzbl %al, %edx
leaq 0xe46d4(%rip), %rsi     # 0x1065f0
movq %r14, %rdi
xorl %eax, %eax
callq 0xde896
movzbl (%rbx), %edx
cmpl $0x6, %edx
movq %rbx, 0x8(%rsp)
jae 0x21fa9
leal (,%rdx,8), %eax
leaq 0xe43bf(%rip), %rcx     # 0x106300
movq (%rax,%rcx), %rcx
leaq 0xe43e4(%rip), %rsi     # 0x106330
movq (%rax,%rsi), %rsi
movl %edx, %eax
leaq 0xe4407(%rip), %rdx     # 0x106360
movq (%rdx,%rax,8), %rdx
movq %rcx, (%r15)
movq %rsi, 0x10(%r15)
movq %rdx, 0x18(%r15)
movq %r14, %rdi
callq 0xdf038
movq (%r15), %rdi
callq 0xdfba1
movq %rax, 0x8(%r15)
leaq 0xe46bc(%rip), %rsi     # 0x10663f
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
cmpb $0x3, (%rbx)
jb 0x21fc1
leaq 0xe4960(%rip), %rsi     # 0x1068fb
movq %r14, %rdi
xorl %eax, %eax
callq 0xdf05a
pushq $0x5
jmp 0x21fc3
leaq 0xe490e(%rip), %rsi     # 0x1068be
xorl %ebx, %ebx
movq %r14, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x224ae
pushq $0x1
popq %r12
movq 0x18(%r14), %rsi
movq 0x8(%r15), %r9
movq 0x30(%r15), %rcx
andl $0x0, (%rsp)
pushq $0x10
popq %r8
movq %r14, %rdi
movq %r12, %rdx
callq 0xecea5
movq %r14, %rdi
movq %r15, %rsi
callq 0x2333b
leaq 0x20(%r12), %rsi
movq 0x18(%r14), %rdi
callq 0xe3f46
movq 0x8(%rsp), %r13
movq %rax, 0x8(%r13)
movq %r12, %rbx
orq $0x22, %rbx
leaq 0xe492a(%rip), %rsi     # 0x10693e
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xe3f46
movq %rax, 0x10(%r13)
addq $0x24, %r12
leaq (%rax,%rax), %rcx
movq %rcx, 0x18(%r13)
leaq 0xe491b(%rip), %rsi     # 0x10695e
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x8(%r13), %rbx
addq %r12, %rbx
addq 0x18(%r13), %rbx
movl $0x80, %esi
movq %rbx, %rdi
callq 0xdfbb6
movq %rax, %r13
leaq 0xe4915(%rip), %rsi     # 0x106986
movq %r14, %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r14), %rax
movq 0x18(%rax), %rcx
cmpq %rbx, %rcx
setge %al
cmpq %r13, %rcx
setle %dl
testb %dl, %al
jne 0x220ad
leaq 0xe4905(%rip), %rsi     # 0x1069a5
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xdf05a
pushq $0x1
popq %rbp
movl $0x7d00, %esi           # imm = 0x7D00
movq %r14, %rdi
movl %ebp, %edx
callq 0xe59cd
movq %rax, 0x20(%r15)
xorl %eax, %eax
cmpq $0x1, (%r15)
sete %al
pushq $0xa
popq %rcx
pushq $0x14
popq %rdx
cmoveq %rcx, %rdx
pushq $0x50
popq %rcx
movl $0xa0, %esi
cmoveq %rcx, %rsi
incq %rax
movq 0x8(%rsp), %rbx
movq %rax, 0x68(%rbx)
movq %rdx, 0x60(%rbx)
movq %rsi, 0x70(%rbx)
leaq 0xe48d5(%rip), %rsi     # 0x1069d2
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x8(%rbx), %rsi
addq $0x2, %rsi
movq %r14, %rdi
callq 0xdecaf
movq 0x18(%r14), %rdi
movq 0x8(%rbx), %rcx
movq %rax, 0x18(%rsp)
movq %rax, %rsi
movq %r12, %rdx
callq 0xe3cb7
addq 0x8(%rbx), %r12
leaq 0xe48ae(%rip), %rsi     # 0x1069eb
movq %r14, 0x20(%rsp)
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
andl $0x0, 0x78(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x80(%rbx)
movups %xmm0, 0x90(%rbx)
andq $0x0, 0xa0(%rbx)
leaq 0x30(%rsp), %r13
xorl %eax, %eax
xorl %r14d, %r14d
testl %eax, %eax
jne 0x222f9
movq 0x8(%rsp), %rax
cmpq 0x8(%rax), %r14
jge 0x222f9
leaq 0x1(%r14), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
movzbl (%rax,%r14), %ebx
testb %bl, %bl
js 0x22203
cmpq $0x1, %rbx
je 0x22253
testl %ebx, %ebx
jne 0x222b2
movq 0x10(%rsp), %rdi
addq 0x18(%rsp), %rdi
callq 0xe3f3b
movq %rax, %rbx
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r13, %rsi
movq %r12, %rdx
pushq $0x2
popq %rcx
callq 0xe3cb7
testq %rbx, %rbx
movl $0x0, %eax
cmovleq %rax, %rbx
subq $0x1, %rbx
jb 0x22249
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x23398
jmp 0x221eb
orq $-0x100, %rbx
movq 0x20(%rsp), %r14
testq %rbx, %rbx
je 0x2223f
movq 0x18(%r14), %rdi
movq %r13, %rsi
movq %r12, %rdx
pushq $0x2
popq %rcx
callq 0xe3cb7
addq $0x2, %r12
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x23398
incq %rbx
jmp 0x2220f
movq 0x8(%rsp), %r14
jmp 0x222e8
addq $0x3, %r14
addq $0x2, %r12
jmp 0x222a8
movq 0x10(%rsp), %rdi
addq 0x18(%rsp), %rdi
callq 0xe3f3b
movq %rax, %rbx
testq %rax, %rax
movl $0x0, %eax
cmovleq %rax, %rbx
subq $0x1, %rbx
jb 0x222a4
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r13, %rsi
movq %r12, %rdx
pushq $0x2
popq %rcx
callq 0xe3cb7
addq $0x2, %r12
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x23398
jmp 0x22271
addq $0x3, %r14
movq %r14, %rcx
movq 0x8(%rsp), %r14
jmp 0x222ed
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r13, %rsi
movq %r12, %rdx
pushq $0x2
popq %rcx
callq 0xe3cb7
movq 0x8(%rsp), %r14
subq $0x1, %rbx
jb 0x222e4
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x23398
jmp 0x222ce
addq $0x2, %r12
movq 0x10(%rsp), %rcx
movl 0x78(%r14), %eax
movq %rcx, %r14
jmp 0x22176
movq 0x8(%rsp), %rbx
movq 0xa0(%rbx), %rdx
leaq 0xe46f5(%rip), %rsi     # 0x106a01
movq 0x20(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0xde896
movq 0xa0(%rbx), %rdx
cmpq $0x3e7f, %rdx           # imm = 0x3E7F
jg 0x2233c
leaq 0xe46e7(%rip), %rsi     # 0x106a19
movq %r14, %rdi
xorl %eax, %eax
callq 0xdf05a
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0xded32
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 %r14, %rdi
callq 0xe7437
movq %rax, 0x38(%r15)
movq %r14, %rdi
callq 0xdfd1a
movq %rax, %rbx
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xed4e6
movq (%r15), %r12
cmpq $0x4, %r12
je 0x223d2
cmpq $0x2, %r12
je 0x223d2
cmpq $0x1, %r12
jne 0x224a0
movq 0x8(%rsp), %rax
movq 0x70(%rax), %rcx
movq 0x20(%r15), %rdi
movq 0x30(%r15), %r8
movq 0x38(%r15), %r9
andl $0x0, (%rsp)
pushq $0x1
popq %rdx
xorl %esi, %esi
callq 0xe7cbc
jmp 0x224a0
andq $0x0, 0x28(%rsp)
cmpl $0x2, %r12d
movq %rbx, 0x18(%rsp)
je 0x223f4
cmpl $0x4, %r12d
jne 0x2249b
movl $0x140, %eax            # imm = 0x140
jmp 0x223f9
movl $0x280, %eax            # imm = 0x280
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
cmpq $0xc8, %rbx
je 0x2249b
xorl %r14d, %r14d
cmpq 0x10(%rsp), %r14
jae 0x2248e
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x22437
movq 0x20(%r15), %rdi
leaq 0x28(%rsp), %rsi
callq 0xe3f6e
movl %eax, 0x30(%rsp,%r13,4)
incq %r13
jmp 0x2241a
xorl %r13d, %r13d
cmpq $0x10, %r13
je 0x22488
movb $0xf, %cl
subb %r13b, %cl
movl %ebp, %eax
shll %cl, %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rcx, %r12
je 0x22468
movl 0x30(%rsp,%rcx,4), %esi
andl %eax, %esi
movl %ebp, %edi
shll %cl, %edi
testl %esi, %esi
cmovel %esi, %edi
addl %edi, %edx
incq %rcx
jmp 0x2244d
movq 0x30(%r15), %rax
movq 0x38(%r15), %rdi
movq %r13, %rsi
orq %r14, %rsi
movl %edx, %ecx
movl (%rax,%rcx,4), %ecx
movq %rbx, %rdx
callq 0xe7287
incq %r13
jmp 0x2243a
addq $0x10, %r14
jmp 0x22410
incq %rbx
movq 0x20(%rsp), %r14
jmp 0x22400
movq 0x18(%rsp), %rbx
movq 0x38(%r15), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xe70ab
movq 0x38(%r15), %rdi
callq 0xe7084
movq 0x20(%r15), %rdi
callq 0xe5591
movq %r14, %rdi
movq %r15, %rsi
callq 0xded32
movq %r14, %rdi
movq %rbx, %rsi
callq 0xdfd24
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0xded32
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq