// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rcx, 0x18(%rsp)
movq %rdx, %r15
movq %rsi, 0x50(%rsp)
movq %rdi, %rbx
movq (%rdx), %rsi
movq 0x8(%rdx), %rdx
callq 0xf1db1
testl %eax, %eax
je 0xab7fd
movq 0x10(%r15), %rdx
movq 0x20(%r15), %r8
movq %r8, %rax
xorq $0x1, %rax
orq %rdx, %rax
jne 0xab805
movl $0x1, %r8d
cmpq $0x1, 0x18(%r15)
jne 0xab84f
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 %r15d, %r15d
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %r9d
pushq $0x0
pushq $0x0
callq 0xf2b5b
addq $0x10, %rsp
jmp 0xaba71
xorl %r15d, %r15d
jmp 0xaba71
cmpq $0x20, %r8
ja 0xab84f
movabsq $0x100010100, %rax      # imm = 0x100010100
btq %r8, %rax
jae 0xab84f
movq 0x18(%r15), %rcx
cmpq $0x3, %rdx
je 0xab889
cmpq $0x2, %rdx
je 0xab86b
cmpq $0x1, %rdx
jne 0xaba5d
testq %rcx, %rcx
jle 0xaba5d
movl $0x1, %eax
movq %rax, 0x8(%rsp)
movl $0x1, %ecx
jmp 0xab8a2
leaq 0x89121(%rip), %rsi     # 0x134977
xorl %r15d, %r15d
movq %rbx, %rdi
movl %r8d, %edx
xorl %eax, %eax
callq 0xe9295
jmp 0xaba71
testq %rcx, %rcx
setg %al
cmpq $0x8, %r8
sete %sil
testb %al, %sil
je 0xaba5d
movl $0x1, %eax
jmp 0xab898
cmpq $0x2, %rcx
jle 0xaba5d
movl $0x3, %eax
movq %rax, 0x8(%rsp)
movl $0x3, %ecx
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
callq 0xf266a
movq %rax, %r14
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0xea504
cmpl $0x0, 0x28(%r15)
je 0xab8e0
leaq 0x28(%r15), %rdx
movq 0x10(%rdx), %rcx
movq %rcx, 0x88(%rax)
movupd (%rdx), %xmm0
movupd %xmm0, 0x78(%rax)
movq %rax, 0x20(%rsp)
movq 0x20(%r15), %rax
leaq 0x7(%rax), %rbp
testq %rax, %rax
cmovnsq %rax, %rbp
sarq $0x3, %rbp
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rax, %rdx
imulq %rbp, %rdx
movq %rcx, %rsi
movq %rdx, 0x40(%rsp)
imulq %rdx, %rsi
movq %rsi, 0x30(%rsp)
movq $0x0, 0x10(%rsp)
testq %rcx, %rcx
movq %r12, 0x38(%rsp)
jle 0xaba22
movq %r12, %rbx
xorl %r12d, %r12d
testq %rax, %rax
jle 0xaba11
movq %rbx, 0x48(%rsp)
xorl %r13d, %r13d
cmpq $0x20, 0x20(%r15)
jne 0xab98d
movq 0x50(%rsp), %rax
movl 0x4(%rax), %edx
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0xef2fc
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0xab99c
movb $-0x1, %al
ucomisd 0x699f3(%rip), %xmm0    # 0x115368
jae 0xab99e
mulsd 0x6beb9(%rip), %xmm0    # 0x117838
addsd 0x6bea9(%rip), %xmm0    # 0x117830
cvttsd2si %xmm0, %eax
jmp 0xab99e
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0xeead3
jmp 0xab99e
xorl %eax, %eax
movq 0x10(%r15), %rcx
cmpq $0x1, %rcx
je 0xab9e5
cmpq $0x2, %rcx
je 0xab9cd
cmpq $0x3, %rcx
jne 0xab9f6
movzbl %al, %r8d
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
callq 0xf2355
jmp 0xab9f6
movzbl %al, %eax
movl 0x48(%r15,%rax,4), %ecx
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xf24ba
jmp 0xab9f6
movzbl %al, %ecx
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xf2405
incq %r13
movq (%r15), %rax
addq %rbp, %rbx
cmpq %rax, %r13
jl 0xab945
movq 0x8(%r15), %rcx
movq 0x48(%rsp), %rbx
incq %r12
addq 0x40(%rsp), %rbx
cmpq %rcx, %r12
jl 0xab934
movq 0x10(%rsp), %rsi
incq %rsi
movq 0x38(%rsp), %r12
addq 0x30(%rsp), %r12
movq %rsi, 0x10(%rsp)
cmpq 0x8(%rsp), %rsi
jne 0xab920
movq %r14, %rdi
movq 0x20(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
callq 0xf22d0
movq 0x28(%rsp), %rbx
jmp 0xaba74
leaq 0x88f30(%rip), %rsi     # 0x134994
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
xorl %r14d, %r14d
movq %r14, %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r15, %rsi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xea50e