// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/ilbm.c
// Function: do_before_image_chunk
static void do_before_image_chunk(deark *c, lctx *d)
{
	if(d->bmhd_changed_flag || d->camg_changed_flag) {
		if(d->ham_flag) {
			if(d->planes_raw==6 || d->planes_raw==5) {
				d->is_ham6 = 1;
			}
			else if(d->planes_raw==8 || d->planes_raw==7) {
				d->is_ham8 = 1;
			}
			else {
				de_warn(c, "Invalid bit depth (%d) for HAM image.", (int)d->planes_raw);
			}
		}

		if(!d->found_cmap && d->planes_raw<=8) {
			de_make_grayscale_palette(d->pal, (i64)1<<(UI)d->planes_raw, 0);
		}

		if(d->planes_raw==6 && d->pal_ncolors==32 && !d->ehb_flag && !d->ham_flag) {
			de_warn(c, "Assuming this is an EHB image");
			d->ehb_flag = 1;
		}
	}

	if(d->cmap_changed_flag) {
		de_memcpy(d->pal, d->pal_raw, (size_t)d->pal_ncolors * sizeof(d->pal_raw[0]));
	}

	if(d->cmap_changed_flag && d->ehb_flag && d->planes_raw==6) {
		UI k;

		// TODO: Should we still do this if the palette already has 64 colors
		// (as it often does)?
		for(k=0; k<32; k++) {
			u8 cr, cg, cb;

			cr = DE_COLOR_R(d->pal[k]);
			cg = DE_COLOR_G(d->pal[k]);
			cb = DE_COLOR_B(d->pal[k]);
			d->pal[k+32] = DE_MAKE_RGB(cr/2, cg/2, cb/2);
		}
	}

	if(d->fixpal_setting && d->cmap_changed_flag) {
		fixup_palette(c, d);
	}

	if(d->cmap_changed_flag) {
		d->pal_is_grayscale = de_is_grayscale_palette(d->pal, 256);
	}

	d->cmap_changed_flag = 0;
	d->bmhd_changed_flag = 0;
	d->camg_changed_flag = 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
cmpb $0x0, 0x35(%rsi)
jne 0x77101
cmpb $0x0, 0x36(%rbx)
je 0x77192
cmpb $0x0, 0x38(%rbx)
je 0x7713c
movq 0x60(%rbx), %rdx
leaq -0x7(%rdx), %rax
cmpq $0x2, %rax
jb 0x77125
leaq -0x5(%rdx), %rax
cmpq $0x1, %rax
ja 0x7712b
movb $0x1, 0x39(%rbx)
jmp 0x7713c
movb $0x1, 0x3a(%rbx)
jmp 0x7713c
leaq 0xa4adf(%rip), %rsi     # 0x11bc11
movq %r14, %rdi
xorl %eax, %eax
callq 0xdf05a
cmpb $0x0, 0x33(%rbx)
jne 0x77160
movq 0x60(%rbx), %rcx
cmpq $0x8, %rcx
jg 0x77160
leaq 0x4d4(%rbx), %rdi
pushq $0x1
popq %rsi
shlq %cl, %rsi
xorl %edx, %edx
callq 0xe87a8
cmpq $0x6, 0x60(%rbx)
jne 0x77192
cmpq $0x20, 0xc8(%rbx)
jne 0x77192
cmpb $0x0, 0x3b(%rbx)
jne 0x77192
cmpb $0x0, 0x38(%rbx)
jne 0x77192
leaq 0xa4ab3(%rip), %rsi     # 0x11bc37
movq %r14, %rdi
xorl %eax, %eax
callq 0xdf05a
movb $0x1, 0x3b(%rbx)
cmpb $0x0, 0x34(%rbx)
je 0x7722d
leaq 0x4d4(%rbx), %r15
leaq 0xd4(%rbx), %rsi
movq 0xc8(%rbx), %r12
leaq (,%r12,4), %rdx
movq %r15, %rdi
callq 0x181e0
cmpb $0x0, 0x3b(%rbx)
je 0x771f8
cmpq $0x6, 0x60(%rbx)
jne 0x771f8
pushq $-0x80
popq %rax
testq %rax, %rax
je 0x771f8
movl 0x554(%rbx,%rax), %ecx
shrl %ecx
andl $0x7f7f7f, %ecx         # imm = 0x7F7F7F
orl $0xff000000, %ecx       # imm = 0xFF000000
movl %ecx, 0x5d4(%rbx,%rax)
addq $0x4, %rax
jmp 0x771d1
cmpb $0x0, 0x2d(%rbx)
sete %al
testq %r12, %r12
setle %cl
orb %al, %cl
jne 0x7721a
movl 0x4(%rbx), %eax
cmpl $0x52474238, %eax       # imm = 0x52474238
je 0x7721a
cmpl $0x5247424e, %eax       # imm = 0x5247424E
jne 0x77242
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
callq 0xe6d06
movl %eax, 0xd0(%rbx)
andw $0x0, 0x34(%rbx)
movb $0x0, 0x36(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
cmpb $0x0, 0x3a(%rbx)
je 0x77275
xorl %eax, %eax
cmpq %rax, %r12
je 0x7721a
movl (%r15,%rax,4), %ecx
movl %ecx, %edx
shrl $0x6, %edx
andl $0xfcfcfc, %ecx         # imm = 0xFCFCFC
andl $0x30303, %edx          # imm = 0x30303
addl %edx, %ecx
addl $0xff000000, %ecx       # imm = 0xFF000000
movl %ecx, (%r15,%rax,4)
incq %rax
jmp 0x7724a
cmpb $0x0, 0x39(%rbx)
jne 0x772aa
xorl %eax, %eax
cmpq %rax, %r12
je 0x77292
testl $0xf0f0f, (%r15,%rax,4) # imm = 0xF0F0F
leaq 0x1(%rax), %rax
je 0x7727d
jmp 0x7721a
leaq 0xa49bc(%rip), %rsi     # 0x11bc55
movq %r14, %rdi
xorl %eax, %eax
callq 0xde896
movq 0xc8(%rbx), %r12
xorl %eax, %eax
testq %r12, %r12
cmovleq %rax, %r12
cmpq %rax, %r12
je 0x772f7
movl (%r15,%rax,4), %ecx
movl %ecx, %edx
shrl $0x14, %edx
movl %ecx, %esi
shrl $0xc, %esi
andl $0xf, %edx
andl $0xf, %esi
shrb $0x4, %cl
movzbl %cl, %ecx
imull $0x11, %ecx, %ecx
imull $0x110000, %edx, %edx   # imm = 0x110000
imull $0x1100, %esi, %esi     # imm = 0x1100
orl %edx, %esi
movzbl %cl, %ecx
addl %esi, %ecx
addl $0xff000000, %ecx       # imm = 0xFF000000
movl %ecx, (%r15,%rax,4)
incq %rax
jmp 0x772b3
cmpb $0x0, 0x34(%rbx)
jne 0x7721a
jmp 0x7722d