// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/macpaint.c
// Function: do_read_patterns
static void do_read_patterns(deark *c, lctx *d)
{
	i64 pos1;
	i64 cell_idx;
	i64 i, j;
	const i64 cell_width = 19;
	const i64 cell_height = 16;
	const i64 cells_per_row = 19;
	int is_blank;
	de_bitmap *gallery = NULL;
	de_bitmap *rawimg = NULL;
	u32 patcrc;
	const char *patsetname;
	de_finfo *fi = NULL;
	de_ucstring *tmpname = NULL;
	struct de_crcobj *crc32o;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "header (continued)");
	de_dbg_indent(c, 1);
	pos1 = d->hdr_pos + 4;

	de_dbg(c, "brush patterns at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);
	crc32o = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE);
	de_crcobj_addslice(crc32o, c->infile, pos1, 38*8);
	patcrc = de_crcobj_getval(crc32o);
	de_crcobj_destroy(crc32o);
	patsetname = get_pattern_set_info(patcrc, &is_blank);
	de_dbg(c, "brush patterns crc: 0x%08x (%s)", (unsigned int)patcrc, patsetname);

	rawimg = de_bitmap_create(c, 8, 38*8, 1);
	rawimg->is_internal = 1;
	de_convert_image_bilevel(c->infile, pos1, 1, rawimg, DE_CVTF_WHITEISZERO);

	if(c->extract_level<2) {
		goto done;
	}

	if(is_blank) {
		de_dbg(c, "brush patterns are blank: not extracting");
		goto done;
	}

	gallery = de_bitmap_create(c, (cell_width+1)*19+1, (cell_height+1)*2+1, 1);

	for(cell_idx=0; cell_idx<38; cell_idx++) {
		i64 cell_x, cell_y; // dst cell pos in cells
		i64 cell_xpos, cell_ypos; // dst cell pos in pixels

		cell_x = cell_idx%cells_per_row;
		cell_y = cell_idx/cells_per_row;

		cell_xpos = (cell_width+1)*cell_x+1;
		cell_ypos = (cell_height+1)*cell_y+1;

		for(j=0; j<cell_height; j++) {
			i64 yoffs;

			// The cell sizes and "brush origin" are believed to be correct for
			// at least one version of MacPaint, under some conditions.
			yoffs = (cell_ypos+j+4)%8;

			for(i=0; i<cell_width; i++) {
				de_colorsample x;
				i64 xoffs;

				xoffs = (cell_xpos+i+7)%8;

				x = DE_COLOR_K(de_bitmap_getpixel(rawimg, xoffs, 8*cell_idx + yoffs));
				de_bitmap_setpixel_gray(gallery, cell_xpos+i, cell_ypos+j, x);
			}
		}
	}

	tmpname = ucstring_create(c);
	if(d->filename && c->filenames_from_file) {
		ucstring_append_ucstring(tmpname, d->filename);
		ucstring_append_sz(tmpname, ".", DE_ENCODING_LATIN1);
	}
	ucstring_append_sz(tmpname, "pat", DE_ENCODING_LATIN1);
	fi = de_finfo_create(c);
	de_finfo_set_name_from_ucstring(c, fi, tmpname, 0);
	de_bitmap_write_to_file_finfo(gallery, fi, DE_CREATEFLAG_IS_AUX|DE_CREATEFLAG_IS_BWIMG);

done:
	de_bitmap_destroy(gallery);
	de_bitmap_destroy(rawimg);
	de_finfo_destroy(c, fi);
	ucstring_destroy(tmpname);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x13, -0x38(%rbp)
movq $0x10, -0x40(%rbp)
movq $0x13, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x8c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
leaq 0xe1e4b(%rip), %rsi     # 0x1bb7e1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
addq $0x4, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0xe1e2a(%rip), %rsi     # 0x1bb7f4
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x10, %esi
callq 0x15eac0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rdx
movl $0x130, %ecx            # imm = 0x130
callq 0x15f500
movq -0x88(%rbp), %rdi
callq 0x15f340
movl %eax, -0x64(%rbp)
movq -0x88(%rbp), %rdi
callq 0x15f2e0
movl -0x64(%rbp), %edi
leaq -0x4c(%rbp), %rsi
callq 0xd9fb0
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movl -0x64(%rbp), %edx
movq -0x70(%rbp), %rcx
leaq 0xe1dbc(%rip), %rsi     # 0x1bb80a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x8, %esi
movl $0x130, %edx            # imm = 0x130
movl $0x1, %ecx
callq 0x16b750
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movb $0x1, 0x38(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x60(%rbp), %rcx
movl $0x1, %edx
movl $0x1, %r8d
callq 0x16bee0
movq -0x8(%rbp), %rax
cmpl $0x2, 0x90(%rax)
jge 0xd9aab
jmp 0xd9ccc
cmpl $0x0, -0x4c(%rbp)
je 0xd9ac8
movq -0x8(%rbp), %rdi
leaq 0xe1d6e(%rip), %rsi     # 0x1bb82a
movb $0x0, %al
callq 0x15b0d0
jmp 0xd9ccc
movq -0x8(%rbp), %rdi
movl $0x17d, %esi            # imm = 0x17D
movl $0x23, %edx
movl $0x1, %ecx
callq 0x16b750
movq %rax, -0x58(%rbp)
movq $0x0, -0x20(%rbp)
cmpq $0x26, -0x20(%rbp)
jge 0xd9c37
movq -0x20(%rbp), %rax
movl $0x13, %ecx
cqto
idivq %rcx
movq %rdx, -0x98(%rbp)
movq -0x20(%rbp), %rax
movl $0x13, %ecx
cqto
idivq %rcx
movq %rax, -0xa0(%rbp)
imulq $0x14, -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
imulq $0x11, -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq $0x0, -0x30(%rbp)
cmpq $0x10, -0x30(%rbp)
jge 0xd9c24
movq -0xb0(%rbp), %rax
addq -0x30(%rbp), %rax
addq $0x4, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rdx, -0xb8(%rbp)
movq $0x0, -0x28(%rbp)
cmpq $0x13, -0x28(%rbp)
jge 0xd9c11
movq -0xa8(%rbp), %rax
addq -0x28(%rbp), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rdx, -0xc8(%rbp)
movq -0x60(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movq -0x20(%rbp), %rdx
shlq $0x3, %rdx
addq -0xb8(%rbp), %rdx
callq 0x16b540
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0xb9(%rbp)
movq -0x58(%rbp), %rdi
movq -0xa8(%rbp), %rsi
addq -0x28(%rbp), %rsi
movq -0xb0(%rbp), %rdx
addq -0x30(%rbp), %rdx
movzbl -0xb9(%rbp), %ecx
callq 0x16b1c0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xd9b82
jmp 0xd9c13
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xd9b4f
jmp 0xd9c26
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xd9aec
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0xd9c85
movq -0x8(%rbp), %rax
movzbl 0xd0(%rax), %eax
cmpl $0x0, %eax
je 0xd9c85
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x171460
movq -0x80(%rbp), %rdi
leaq 0xed528(%rip), %rsi     # 0x1c71a3
movl $0x5, %edx
callq 0x1715e0
movq -0x80(%rbp), %rdi
leaq 0xe421a(%rip), %rsi     # 0x1bdeaa
movl $0x5, %edx
callq 0x1715e0
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x58(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl $0x9, %edx
callq 0x16afa0
movq -0x58(%rbp), %rdi
callq 0x16af60
movq -0x60(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x15d260
movq -0x80(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0x15b5a0
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)