// File: /workspace/github_c_makefile_star2_O1/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 %rbp
pushq %r14
pushq %rbx
movq %rsi, %rbx
movq %rdi, %r14
cmpb $0x0, 0x35(%rsi)
jne 0x7aeb6
cmpb $0x0, 0x36(%rbx)
je 0x7af49
cmpb $0x0, 0x38(%rbx)
je 0x7aef1
movq 0x60(%rbx), %rdx
leaq -0x7(%rdx), %rax
cmpq $0x2, %rax
jb 0x7aeda
leaq -0x5(%rdx), %rax
cmpq $0x1, %rax
ja 0x7aee0
movb $0x1, 0x39(%rbx)
jmp 0x7aef1
movb $0x1, 0x3a(%rbx)
jmp 0x7aef1
leaq 0xadd3a(%rip), %rsi     # 0x128c21
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9b27
cmpb $0x0, 0x33(%rbx)
jne 0x7af17
movq 0x60(%rbx), %rcx
cmpq $0x8, %rcx
jg 0x7af17
leaq 0x4d4(%rbx), %rdi
movl $0x1, %esi
shlq %cl, %rsi
xorl %edx, %edx
callq 0xf41f4
cmpq $0x6, 0x60(%rbx)
jne 0x7af49
cmpq $0x20, 0xc8(%rbx)
jne 0x7af49
cmpb $0x0, 0x3b(%rbx)
jne 0x7af49
cmpb $0x0, 0x38(%rbx)
jne 0x7af49
leaq 0xadd0c(%rip), %rsi     # 0x128c47
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9b27
movb $0x1, 0x3b(%rbx)
movb 0x34(%rbx), %bpl
testb %bpl, %bpl
je 0x7afa5
leaq 0x4d4(%rbx), %rdi
leaq 0xd4(%rbx), %rsi
movq 0xc8(%rbx), %rdx
shlq $0x2, %rdx
callq 0x181e0
cmpb $0x0, 0x3b(%rbx)
je 0x7afa5
cmpq $0x6, 0x60(%rbx)
jne 0x7afa5
movq $-0x20, %rax
movl 0x554(%rbx,%rax,4), %ecx
shrl %ecx
andl $0x7f7f7f, %ecx         # imm = 0x7F7F7F
orl $0xff000000, %ecx       # imm = 0xFF000000
movl %ecx, 0x5d4(%rbx,%rax,4)
incq %rax
jne 0x7af84
testb %bpl, %bpl
je 0x7b04a
cmpb $0x0, 0x2d(%rbx)
je 0x7b04a
movq 0xc8(%rbx), %rax
testq %rax, %rax
jle 0x7b04a
movl 0x4(%rbx), %ecx
cmpl $0x52474238, %ecx       # imm = 0x52474238
je 0x7b04a
cmpl $0x5247424e, %ecx       # imm = 0x5247424E
je 0x7b04a
cmpb $0x0, 0x3a(%rbx)
je 0x7b014
xorl %ecx, %ecx
movl 0x4d4(%rbx,%rcx,4), %edx
movl %edx, %esi
shrl $0x6, %esi
andl $0xfcfcfc, %edx         # imm = 0xFCFCFC
andl $0x30303, %esi          # imm = 0x30303
addl %esi, %edx
addl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, 0x4d4(%rbx,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x7afe3
jmp 0x7b04a
cmpb $0x0, 0x39(%rbx)
jne 0x7b042
xorl %ecx, %ecx
testl $0xf0f0f, 0x4d4(%rbx,%rcx,4) # imm = 0xF0F0F
jne 0x7b04a
incq %rcx
cmpq %rcx, %rax
jne 0x7b01c
leaq 0xadc2d(%rip), %rsi     # 0x128c65
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
callq 0x7bc1b
cmpb $0x0, 0x34(%rbx)
je 0x7b067
leaq 0x4d4(%rbx), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0xf2544
movl %eax, 0xd0(%rbx)
movw $0x0, 0x34(%rbx)
movb $0x0, 0x36(%rbx)
popq %rbx
popq %r14
popq %rbp
retq