// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/psd.c
// Function: do_bitmap
static void do_bitmap(deark *c, lctx *d, const struct image_info *iinfo, dbuf *f,
	i64 pos, i64 len)
{
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	i64 i, j, plane;
	i64 nplanes = 0; // Number of planes to read. May be less than d->num_channels.
	i64 planespan, rowspan, samplespan;
	u8 b;

	if(!de_good_image_dimensions(c, iinfo->width, iinfo->height)) goto done;

	if(iinfo->color_mode==PSD_CM_BITMAP && iinfo->bits_per_channel==1 &&
		iinfo->num_channels==1)
	{
		de_convert_and_write_image_bilevel2(f, 0, iinfo->width, iinfo->height,
			(iinfo->width+7)/8, DE_CVTF_WHITEISZERO, NULL, 0);
		goto done;
	}

	if(iinfo->bits_per_channel!=8 && iinfo->bits_per_channel!=16 &&
		iinfo->bits_per_channel!=32)
	{
		de_err(c, "Unsupported bits/channel: %d", (int)iinfo->bits_per_channel);
		goto done;
	}

	if(iinfo->color_mode==PSD_CM_GRAY && iinfo->num_channels>=1) {
		nplanes = 1;
	}
	else if(iinfo->color_mode==PSD_CM_PALETTE && iinfo->num_channels>=1 && iinfo->bits_per_channel==8) {
		nplanes = 1;
	}
	else if(iinfo->color_mode==PSD_CM_RGB && iinfo->num_channels>=3) {
		nplanes = 3;
	}
	else {
		de_err(c, "This type of image is not supported (color=%d, "
			"num channels=%d, bits/channel=%d)",
			(int)iinfo->color_mode, (int)iinfo->num_channels, (int)iinfo->bits_per_channel);
		goto done;
	}

	img = de_bitmap_create(c, iinfo->width, iinfo->height,
		iinfo->color_mode==PSD_CM_GRAY ? 1 : 3);

	fi = de_finfo_create(c);

	if(iinfo->density.code!=DE_DENSITY_UNKNOWN) {
		fi->density = iinfo->density;
	}

	samplespan = iinfo->bits_per_channel/8;
	rowspan = iinfo->width * samplespan;
	planespan = iinfo->height * rowspan;

	for(plane=0; plane<nplanes; plane++) {
		for(j=0; j<iinfo->height; j++) {
			for(i=0; i<iinfo->width; i++) {
				if(iinfo->bits_per_channel==32) {
					// TODO: The format of 32-bit samples does not seem to be documented.
					// This is little more than a guess.
					double tmpd;
					tmpd = dbuf_getfloat32x(f, pos + plane*planespan + j*rowspan + i*samplespan, d->is_le);
					b = scale_float_to_255(tmpd);
				}
				else {
					b = dbuf_getbyte(f, pos + plane*planespan + j*rowspan + i*samplespan);
				}
				if(iinfo->color_mode==PSD_CM_RGB) {
					de_bitmap_setsample(img, i, j, plane, b);
				}
				else if(iinfo->color_mode==PSD_CM_GRAY) {
					de_bitmap_setpixel_gray(img, i, j, b);
				}
				else if(iinfo->color_mode==PSD_CM_PALETTE) {
					de_bitmap_setpixel_rgb(img, i, j, iinfo->pal[(unsigned int)b]);
				}
			}
		}
	}

	de_bitmap_write_to_file_finfo(img, fi, 0);
done:
	de_bitmap_destroy(img);
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0xfb48d
jmp 0xfb839
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0xfb50d
movq -0x18(%rbp), %rax
cmpq $0x1, 0x20(%rax)
jne 0xfb50d
movq -0x18(%rbp), %rax
cmpq $0x1, 0x18(%rax)
jne 0xfb50d
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq $0x7, %rax
movl $0x8, %esi
cqto
idivq %rsi
movq -0x90(%rbp), %rdx
movq %rax, %r8
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %r9d
xorl %eax, %eax
movq $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x16c000
jmp 0xfb839
movq -0x18(%rbp), %rax
cmpq $0x8, 0x20(%rax)
je 0xfb54f
movq -0x18(%rbp), %rax
cmpq $0x10, 0x20(%rax)
je 0xfb54f
movq -0x18(%rbp), %rax
cmpq $0x20, 0x20(%rax)
je 0xfb54f
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
leaq 0xc4e7b(%rip), %rsi     # 0x1c03be
movb $0x0, %al
callq 0x15bfb0
jmp 0xfb839
movq -0x18(%rbp), %rax
cmpq $0x1, 0x10(%rax)
jne 0xfb572
movq -0x18(%rbp), %rax
cmpq $0x1, 0x18(%rax)
jl 0xfb572
movq $0x1, -0x60(%rbp)
jmp 0xfb5f7
movq -0x18(%rbp), %rax
cmpq $0x2, 0x10(%rax)
jne 0xfb59d
movq -0x18(%rbp), %rax
cmpq $0x1, 0x18(%rax)
jl 0xfb59d
movq -0x18(%rbp), %rax
cmpq $0x8, 0x20(%rax)
jne 0xfb59d
movq $0x1, -0x60(%rbp)
jmp 0xfb5f5
movq -0x18(%rbp), %rax
cmpq $0x3, 0x10(%rax)
jne 0xfb5bd
movq -0x18(%rbp), %rax
cmpq $0x3, 0x18(%rax)
jl 0xfb5bd
movq $0x3, -0x60(%rbp)
jmp 0xfb5f3
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %r8d
leaq 0xc4df4(%rip), %rsi     # 0x1c03db
movb $0x0, %al
callq 0x15bfb0
jmp 0xfb839
jmp 0xfb5f5
jmp 0xfb5f7
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %r8
movl $0x3, %ecx
movl $0x1, %eax
cmpq $0x1, %r8
cmovel %eax, %ecx
callq 0x16b750
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0xfb669
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x28(%rcx), %rdx
movq %rdx, 0x78(%rax)
movq 0x30(%rcx), %rdx
movq %rdx, 0x80(%rax)
movq 0x38(%rcx), %rcx
movq %rcx, 0x88(%rax)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq -0x78(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
imulq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0xfb82a
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jge 0xfb817
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jge 0xfb804
movq -0x18(%rbp), %rax
cmpq $0x20, 0x20(%rax)
jne 0xfb746
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x58(%rbp), %rax
imulq -0x68(%rbp), %rax
addq %rax, %rsi
movq -0x50(%rbp), %rax
imulq -0x70(%rbp), %rax
addq %rax, %rsi
movq -0x48(%rbp), %rax
imulq -0x78(%rbp), %rax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x165e00
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
callq 0xfba50
movb %al, -0x79(%rbp)
jmp 0xfb77a
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x58(%rbp), %rax
imulq -0x68(%rbp), %rax
addq %rax, %rsi
movq -0x50(%rbp), %rax
imulq -0x70(%rbp), %rax
addq %rax, %rsi
movq -0x48(%rbp), %rax
imulq -0x78(%rbp), %rax
addq %rax, %rsi
callq 0x165060
movb %al, -0x79(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x3, 0x10(%rax)
jne 0xfb7a1
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
movzbl -0x79(%rbp), %r8d
callq 0x16b060
jmp 0xfb7f1
movq -0x18(%rbp), %rax
cmpq $0x1, 0x10(%rax)
jne 0xfb7c3
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movzbl -0x79(%rbp), %ecx
callq 0x16b1c0
jmp 0xfb7ef
movq -0x18(%rbp), %rax
cmpq $0x2, 0x10(%rax)
jne 0xfb7ed
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x18(%rbp), %rax
movzbl -0x79(%rbp), %ecx
movl %ecx, %ecx
movl 0x48(%rax,%rcx,4), %ecx
callq 0x16b310
jmp 0xfb7ef
jmp 0xfb7f1
jmp 0xfb7f3
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xfb6d8
jmp 0xfb806
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0xfb6be
jmp 0xfb819
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xfb6a8
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x16afa0
movq -0x38(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15d260
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)