// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %r15
movq %rsi, 0x50(%rsp)
movq %rdi, %rbx
movq (%rdx), %rsi
movq 0x8(%rdx), %rdx
callq 0xf241d
testl %eax, %eax
je 0xac536
movq 0x10(%r15), %rdx
testq %rdx, %rdx
jne 0xac2ee
cmpq $0x1, 0x20(%r15)
jne 0xac2ee
cmpq $0x1, 0x18(%r15)
jne 0xac2ee
movq (%r15), %rdx
movq 0x8(%r15), %rcx
leaq 0x7(%rdx), %rax
leaq 0xe(%rdx), %r8
testq %rax, %rax
cmovnsq %rax, %r8
sarq $0x3, %r8
xorl %r14d, %r14d
movq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %r9d
pushq $0x0
pushq $0x0
callq 0xf319c
addq $0x10, %rsp
jmp 0xac56c
movq 0x20(%r15), %r8
cmpq $0x20, %r8
ja 0xac555
movabsq $0x100010100, %rax      # imm = 0x100010100
btq %r8, %rax
jae 0xac555
cmpq $0x1, %rdx
jne 0xac327
movl $0x1, %eax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x18(%r15)
jg 0xac36d
cmpq $0x3, %rdx
je 0xac358
cmpq $0x2, %rdx
jne 0xac53b
cmpq $0x8, %r8
jne 0xac34e
movl $0x1, %eax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x18(%r15)
jg 0xac36d
cmpq $0x3, %rdx
jne 0xac53b
movl $0x3, %eax
movq %rax, 0x8(%rsp)
cmpq $0x2, 0x18(%r15)
jle 0xac53b
xorl %eax, %eax
cmpq $0x1, %rdx
setne %al
movq (%r15), %rsi
movq 0x8(%r15), %rdx
leal 0x1(,%rax,2), %ecx
movq %rbx, %rdi
callq 0xf2cab
movq %rax, %rbp
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
callq 0xea9a9
cmpl $0x0, 0x28(%r15)
je 0xac3bb
leaq 0x28(%r15), %rdx
movq 0x10(%rdx), %rcx
movq %rcx, 0x88(%rax)
movupd (%rdx), %xmm0
movupd %xmm0, 0x78(%rax)
movq %rax, 0x30(%rsp)
movq 0x20(%r15), %rax
leaq 0x7(%rax), %r14
testq %rax, %rax
cmovnsq %rax, %r14
sarq $0x3, %r14
movq (%r15), %rax
imulq %r14, %rax
movq 0x8(%r15), %rcx
movq %rax, 0x48(%rsp)
imulq %rax, %rcx
movq %rcx, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
cmpq $0x0, 0x8(%r15)
jle 0xac4fb
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
cmpq $0x0, (%r15)
jle 0xac4df
movq 0x10(%rsp), %rbx
xorl %r13d, %r13d
cmpq $0x20, 0x20(%r15)
jne 0xac467
movq 0x50(%rsp), %rax
movl 0x4(%rax), %edx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0xef935
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0xac476
movb $-0x1, %al
ucomisd 0x68e69(%rip), %xmm0    # 0x1152b8
jae 0xac478
mulsd 0x6b27f(%rip), %xmm0    # 0x1176d8
addsd 0x6b26f(%rip), %xmm0    # 0x1176d0
cvttsd2si %xmm0, %eax
jmp 0xac478
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0xef0d5
jmp 0xac478
xorl %eax, %eax
movq 0x10(%r15), %rcx
cmpq $0x1, %rcx
je 0xac4bf
cmpq $0x2, %rcx
je 0xac4a7
cmpq $0x3, %rcx
jne 0xac4d0
movzbl %al, %r8d
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x18(%rsp), %rcx
callq 0xf299c
jmp 0xac4d0
movzbl %al, %eax
movl 0x48(%r15,%rax,4), %ecx
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xf2afd
jmp 0xac4d0
movzbl %al, %ecx
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xf2a4a
incq %r13
addq %r14, %rbx
cmpq (%r15), %r13
jl 0xac41f
incq %r12
movq 0x10(%rsp), %rax
addq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq 0x8(%r15), %r12
jl 0xac40d
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x40(%rsp), %rax
addq %rax, 0x28(%rsp)
movq %rcx, 0x18(%rsp)
cmpq 0x8(%rsp), %rcx
jne 0xac3f5
movq %rbp, %rdi
movq 0x30(%rsp), %r14
movq %r14, %rsi
xorl %edx, %edx
callq 0xf2917
movq 0x38(%rsp), %rbx
jmp 0xac56e
xorl %r14d, %r14d
jmp 0xac56c
movl 0x18(%r15), %ecx
leaq 0x88056(%rip), %rsi     # 0x13459c
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0xac56c
leaq 0x88023(%rip), %rsi     # 0x13457f
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r8d, %edx
xorl %eax, %eax
callq 0xe9df7
xorl %ebp, %ebp
movq %rbp, %rdi
callq 0xf28d4
movq %rbx, %rdi
movq %r14, %rsi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xea9b3