// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/ilbm.c
// Function: init_imgbody_info
static int init_imgbody_info(deark *c, lctx *d, struct imgbody_info *ibi, int is_thumb)
{
	int retval = 0;

	ibi->is_thumb = is_thumb;

	// Unlike ACBM, it would be messy and slow to convert PBM to the standard ILBM
	// frame buffer format (and back). So we support a special frame buffer format
	// just for PBM.
	ibi->is_pbm = (d->formtype==CODE_PBM);
	// We also have a special nonplanar RGB24 format (is_rgb24).

	if(d->formtype==CODE_RGBN || d->formtype==CODE_RGB8) {
		ibi->is_rgb24 = 1;
		if(((d->formtype==CODE_RGBN && d->planes_raw==13) ||
			(d->formtype==CODE_RGB8 && d->planes_raw==25)) &&
			(d->compression==3 || d->compression==4) &&
			d->masking_code==MASKINGTYPE_NONE)
		{
			;
		}
		else {
			de_err(c, "Unsupported RGBN/RGB8 format");
			goto done;
		}
	}

	if(is_thumb) {
		ibi->width = d->thumb_width;
		ibi->height = d->thumb_height;
	}
	else {
		ibi->width = d->width;
		ibi->height = d->height;
	}

	// Quick & dirty support for -padpix.
	// TODO: Some of these conditions could be removed, with care.
	if(c->padpix && (d->width%16) && !is_thumb && !d->is_anim && d->formtype==CODE_ILBM &&
		!d->ham_flag)
	{
		ibi->width = de_pad_to_n(ibi->width, 16);
	}

	ibi->compression = d->compression;

	ibi->masking_code = d->masking_code;
	// Based on what little data I have, it seems that TINY images do not have
	// a transparency mask, even if the main image does.
	if(is_thumb && ibi->masking_code==MASKINGTYPE_1BITMASK) {
		ibi->masking_code = MASKINGTYPE_NONE;
	}

	if(d->planes_raw==24) {
		ibi->colortype = COLORTYPE_RGB24;
	}
	else {
		ibi->colortype = COLORTYPE_DEFAULT;
	}

	if(ibi->is_pbm || ibi->is_rgb24) {
		ibi->planes_fg = 1;
		ibi->planes_total = 1;
	}
	else {
		ibi->planes_fg = d->planes_raw;
		ibi->planes_total = d->planes_raw;
		if(ibi->masking_code==MASKINGTYPE_1BITMASK) {
			ibi->planes_total++;
		}
	}
	ibi->transparent_color = d->transparent_color;
	ibi->x_aspect = d->x_aspect;
	ibi->y_aspect = d->y_aspect;

	if(ibi->is_pbm) {
		if(d->planes_raw!=8 || d->masking_code==MASKINGTYPE_1BITMASK) {
			de_err(c, "Not a supported PBM format");
			goto done;
		}
	}

	if(ibi->is_pbm) {
		ibi->bytes_per_row_per_plane = ibi->width;
		if(ibi->bytes_per_row_per_plane%2) {
			ibi->bytes_per_row_per_plane++;
		}
		ibi->bits_per_row_per_plane = ibi->bytes_per_row_per_plane * 8;
		// Note: The PBM row size might be adjusted later, after decompression.
	}
	else if(ibi->is_rgb24) {
		ibi->bytes_per_row_per_plane = ibi->width*3;
		ibi->bits_per_row_per_plane = ibi->bytes_per_row_per_plane * 8;
	}
	else {
		ibi->bits_per_row_per_plane = de_pad_to_n(ibi->width, 16);
		ibi->bytes_per_row_per_plane = ibi->bits_per_row_per_plane/8;
	}
	ibi->frame_buffer_rowspan = ibi->bytes_per_row_per_plane * ibi->planes_total;
	ibi->frame_buffer_size = ibi->frame_buffer_rowspan * ibi->height;

	if(ibi->masking_code==MASKINGTYPE_NONE) {
		;
	}
	else if(ibi->masking_code==MASKINGTYPE_COLORKEY) {
		if(ibi->planes_fg<=8 && !d->ham_flag) {
			ibi->use_colorkey_transparency = 1;
		}
	}
	else if(ibi->masking_code==MASKINGTYPE_1BITMASK) {
		;
	}
	else {
		de_warn(c, "This type of transparency is not supported");
	}

	if(ibi->use_colorkey_transparency && ibi->transparent_color<=255) {
		d->pal[ibi->transparent_color] = DE_SET_ALPHA(d->pal[ibi->transparent_color], 0);
	}

	if(ibi->colortype==COLORTYPE_RGB24) {
		;
	}
	else if(ibi->planes_fg<1 || ibi->planes_fg>8) {
		de_err(c, "Bad or unsupported number of planes: %d", (int)ibi->planes_fg);
		goto done;
	}
	retval = 1;

done:
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movl %ecx, 0x5c(%rdx)
movl 0x4(%rsi), %eax
cmpl $0x50424d20, %eax       # imm = 0x50424D20
sete 0x60(%rdx)
cmpl $0x5247424e, %eax       # imm = 0x5247424E
je 0x77335
cmpl $0x52474238, %eax       # imm = 0x52474238
jne 0x77377
movb $0x1, 0x61(%r14)
cmpl $0x52474238, %eax       # imm = 0x52474238
je 0x77351
cmpl $0x5247424e, %eax       # imm = 0x5247424E
jne 0x7736b
cmpq $0xd, 0x60(%r15)
je 0x77358
jmp 0x7736b
cmpq $0x19, 0x60(%r15)
jne 0x7736b
movb 0x68(%r15), %cl
addb $-0x3, %cl
cmpb $0x1, %cl
ja 0x7736b
cmpb $0x0, 0x69(%r15)
je 0x77377
leaq 0xa4921(%rip), %rsi     # 0x11bc93
jmp 0x77440
xorl %ecx, %ecx
testl %ebp, %ebp
setne %cl
shll $0x6, %ecx
movq 0x50(%rcx,%r15), %rdi
movq 0x58(%rcx,%r15), %rcx
movq %rdi, (%r14)
movq %rcx, 0x8(%r14)
cmpb $0x0, 0xd6(%rbx)
je 0x773ca
testl %ebp, %ebp
jne 0x773ca
movq 0x50(%r15), %rcx
andl $0xf, %ecx
je 0x773ca
cmpl $0x494c424d, %eax       # imm = 0x494C424D
jne 0x773ca
cmpl $0x0, (%r15)
jne 0x773ca
cmpb $0x0, 0x38(%r15)
jne 0x773ca
pushq $0x10
popq %rsi
callq 0xdfbb6
movq %rax, %rdi
movq %rax, (%r14)
movb 0x68(%r15), %al
movb %al, 0x20(%r14)
movb 0x69(%r15), %al
xorl %ecx, %ecx
cmpb $0x1, %al
movzbl %al, %eax
cmovnel %eax, %ecx
testl %ebp, %ebp
cmovel %eax, %ecx
movb %cl, 0x21(%r14)
movq 0x60(%r15), %rax
xorl %edx, %edx
cmpq $0x18, %rax
sete %dl
movl %edx, 0x58(%r14)
movb 0x60(%r14), %sil
testb %sil, %sil
jne 0x7740a
cmpb $0x0, 0x61(%r14)
je 0x77488
pushq $0x1
popq %rdx
movq %rdx, 0x10(%r14)
movq %rdx, 0x18(%r14)
movl 0x6c(%r15), %r8d
movl %r8d, 0x24(%r14)
movups 0x70(%r15), %xmm0
movups %xmm0, 0x28(%r14)
testb %sil, %sil
je 0x77459
cmpq $0x8, %rax
jne 0x77439
cmpb $0x1, 0x69(%r15)
jne 0x7746e
leaq 0xa4870(%rip), %rsi     # 0x11bcb0
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x61(%r14)
je 0x774a5
leaq (%rdi,%rdi,2), %rax
movq %rax, 0x40(%r14)
imulq $0x18, %rdi, %rsi
jmp 0x77482
movl %edi, %eax
andl $0x1, %eax
addq %rdi, %rax
movq %rax, 0x40(%r14)
leaq (,%rax,8), %rsi
movq %rsi, 0x38(%r14)
jmp 0x774c5
movq %rax, 0x10(%r14)
movq %rax, 0x18(%r14)
movq %rax, %rdx
cmpb $0x1, %cl
jne 0x77415
leaq 0x1(%rax), %rdx
jmp 0x77411
pushq $0x10
popq %rsi
callq 0xdfbb6
movq %rax, 0x38(%r14)
pushq $0x8
popq %rcx
cqto
idivq %rcx
movq %rax, 0x40(%r14)
movq 0x18(%r14), %rdx
movb 0x21(%r14), %cl
imulq %rax, %rdx
movq %rdx, 0x48(%r14)
imulq 0x8(%r14), %rdx
movq %rdx, 0x50(%r14)
cmpb $0x2, %cl
jb 0x77509
movzbl %cl, %eax
cmpl $0x2, %eax
jne 0x774f8
cmpq $0x8, 0x10(%r14)
jg 0x77509
cmpb $0x0, 0x38(%r15)
jne 0x77509
movb $0x1, 0x22(%r14)
jmp 0x77509
leaq 0xa47cc(%rip), %rsi     # 0x11bccb
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
cmpb $0x0, 0x22(%r14)
je 0x77525
movl 0x24(%r14), %eax
cmpq $0xff, %rax
ja 0x77525
movb $0x0, 0x4d7(%r15,%rax,4)
pushq $0x1
popq %rbp
cmpl $0x1, 0x58(%r14)
je 0x7744c
movq 0x10(%r14), %rdx
leaq -0x9(%rdx), %rax
cmpq $-0x9, %rax
ja 0x7744c
leaq 0xa47aa(%rip), %rsi     # 0x11bcf6
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x7744c