// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %r8, %r13
movq %rcx, 0x20(%rsp)
movq %rdx, %r15
movq %rsi, 0x60(%rsp)
movq %rdi, %rbx
movq (%rdx), %rsi
movq 0x8(%rdx), %rdx
callq 0xe6be1
testl %eax, %eax
je 0xa507e
movq 0x10(%r15), %rdx
movq 0x20(%r15), %r8
movq %r8, %rax
xorq $0x1, %rax
orq %rdx, %rax
jne 0xa5086
cmpq $0x1, 0x18(%r15)
jne 0xa50cb
movq (%r15), %r8
movq 0x8(%r15), %rcx
leaq 0x7(%r8), %rax
pushq $0x8
popq %rsi
cqto
idivq %rsi
andl $0x0, 0x8(%rsp)
andq $0x0, (%rsp)
xorl %r14d, %r14d
pushq $0x1
popq %r9
movq 0x20(%rsp), %rdi
xorl %esi, %esi
movq %r8, %rdx
movq %rax, %r8
callq 0xe78af
jmp 0xa52bd
xorl %r14d, %r14d
jmp 0xa52bd
cmpq $0x20, %r8
ja 0xa50cf
movabsq $0x100010100, %rax      # imm = 0x100010100
btq %r8, %rax
jae 0xa50cf
movq 0x18(%r15), %rcx
cmpq $0x3, %rdx
je 0xa5106
cmpq $0x2, %rdx
je 0xa50eb
cmpq $0x1, %rdx
jne 0xa52a9
testq %rcx, %rcx
jle 0xa52a9
pushq $0x1
popq %rax
movq %rax, 0x18(%rsp)
pushq $0x1
jmp 0xa511a
pushq $0x1
popq %r8
leaq 0x825c9(%rip), %rsi     # 0x12769f
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r8d, %edx
xorl %eax, %eax
callq 0xdf2a5
jmp 0xa52bd
testq %rcx, %rcx
setg %al
cmpq $0x8, %r8
sete %sil
testb %al, %sil
je 0xa52a9
pushq $0x1
jmp 0xa5112
cmpq $0x2, %rcx
jle 0xa52a9
pushq $0x3
popq %rax
movq %rax, 0x18(%rsp)
pushq $0x3
popq %rcx
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
callq 0xe7437
movq %rax, %rbp
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
callq 0xdfd1a
cmpl $0x0, 0x28(%r15)
je 0xa5159
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
pushq $0x8
popq %rcx
cqto
idivq %rcx
movq %rax, %rbx
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rax, %rdx
imulq %rbx, %rdx
movq %rcx, %rsi
movq %rdx, 0x50(%rsp)
imulq %rdx, %rsi
movq %rsi, 0x40(%rsp)
xorl %edx, %edx
cmpq 0x18(%rsp), %rdx
je 0xa52e0
movq %rdx, 0x28(%rsp)
movq %r13, 0x48(%rsp)
xorl %r12d, %r12d
cmpq %rcx, %r12
jge 0xa5292
movq %r13, 0x58(%rsp)
xorl %r14d, %r14d
cmpq %rax, %r14
jge 0xa527c
cmpq $0x20, 0x20(%r15)
jne 0xa5208
movq 0x60(%rsp), %rax
movl 0x4(%rax), %edx
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0xe4470
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0xa5217
movb $-0x1, %al
ucomisd 0x630e8(%rip), %xmm0    # 0x1082d8
jae 0xa5219
mulsd 0x6554e(%rip), %xmm0    # 0x10a748
addsd 0x6553e(%rip), %xmm0    # 0x10a740
cvttsd2si %xmm0, %eax
jmp 0xa5219
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0xe3eb4
jmp 0xa5219
xorl %eax, %eax
movq 0x10(%r15), %rcx
movzbl %al, %r8d
cmpq $0x1, %rcx
je 0xa525d
cmpq $0x2, %rcx
je 0xa5248
cmpq $0x3, %rcx
jne 0xa526e
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq 0x28(%rsp), %rcx
callq 0xe712a
jmp 0xa526e
movl 0x48(%r15,%r8,4), %ecx
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xe7287
jmp 0xa526e
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl %r8d, %ecx
callq 0xe71da
incq %r14
movq (%r15), %rax
addq %rbx, %r13
jmp 0xa51b7
incq %r12
movq 0x8(%r15), %rcx
movq 0x58(%rsp), %r13
addq 0x50(%rsp), %r13
jmp 0xa51a6
movq 0x28(%rsp), %rdx
incq %rdx
movq 0x48(%rsp), %r13
addq 0x40(%rsp), %r13
jmp 0xa518e
leaq 0x8240c(%rip), %rsi     # 0x1276bc
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
xorl %ebp, %ebp
movq %rbp, %rdi
callq 0xe7084
movq %rbx, %rdi
movq %r14, %rsi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdfd24
movq %rbp, %rdi
movq 0x30(%rsp), %r14
movq %r14, %rsi
xorl %edx, %edx
callq 0xe70ab
movq 0x38(%rsp), %rbx
jmp 0xa52bf