// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/bmp.c
// Function: de_run_picjpeg
static void de_run_picjpeg(deark *c, de_module_params *mparams)
{
	i64 bits_offset;
	i64 jpeg_data_len;
	UI lum_setting;
	UI chr_setting;
	int need_errmsg = 0;
	i64 srcpos;
	dbuf *outf = NULL;
	i64 i;
	u32 hdr[7];
	struct de_bmpinfo bi;
	struct de_scan_jpeg_data_ctx sd;

	de_declare_fmt(c, "Pegasus JPEG or KQP");

	(void)fmtutil_get_bmpinfo(c, c->infile, &bi, 0, c->infile->len,
		DE_BMPINFO_HAS_FILEHEADER|DE_BMPINFO_CMPR_IS_4CC|DE_BMPINFO_NOERR);
	// TODO?: Read the palette (for dbg info)

	bits_offset = de_getu32le(10);
	jpeg_data_len = c->infile->len - bits_offset;
	if(jpeg_data_len<2) {
		need_errmsg = 1;
		goto done;
	}

	// Extended BMP header fields
	for(i=0; i<7; i++) {
		hdr[i] = (u32)de_getu32le(54+4*i);
		de_dbg(c, "bmp ext hdr[%u]: %u", (UI)i, (UI)hdr[i]);
	}

	// Validate the extra BMP fields.
	// Not sure what all these are.
	// [5] & [6] are related to sampling factors.
	if(hdr[0]!=44 || hdr[1]!=24 || hdr[2]!=0 || hdr[3]!=2 ||
		hdr[4]!=8)
	{
		need_errmsg = 1;
		goto done;
	}

	de_zeromem(&sd, sizeof(struct de_scan_jpeg_data_ctx));
	de_scan_jpeg_data(c, c->infile, bits_offset, c->infile->len-bits_offset, &sd);
	if(sd.error_flag) {
		need_errmsg = 1;
		goto done;
	}
	if(!sd.found_pic || !sd.found_sof || !sd.found_sos) {
		need_errmsg = 1;
		goto done;
	}
	if(sd.pic_pos >= sd.sof_pos) {
		need_errmsg = 1;
		goto done;
	}

	// Read from "PIC" segment.
	lum_setting = (UI)de_getbyte(sd.pic_pos+9);
	chr_setting = (UI)de_getbyte(sd.pic_pos+10);
	de_dbg(c, "luminance: %u", lum_setting);
	de_dbg(c, "chrominance: %u", chr_setting);

	de_dbg(c, "has DHT: %u", (UI)sd.found_dht);

	outf = dbuf_create_output_file(c, "jpg", NULL, 0);

	srcpos = sd.soi_pos;
	dbuf_copy(c->infile, srcpos, 2, outf);
	srcpos += 2;

	if(sd.found_jfif && sd.jfif_pos==srcpos) {
		// Copy everything before the JFIF version number
		dbuf_copy(c->infile, srcpos, 9, outf);
		// Sometimes the JFIF version number is wrong, so we correct it.
		dbuf_writebyte(outf, 1);
		dbuf_writebyte(outf, 2);
		srcpos += 11;
	}

	// Copy everything up to the PIC segment
	dbuf_copy(c->infile, srcpos, sd.pic_pos-srcpos, outf);

	srcpos = sd.pic_pos + sd.pic_len; // Skip the PIC segment

	// Convert the Photoshop Resources segment to a more standard format
	if(sd.found_8bim && sd._8bim_pos>=srcpos && sd._8bim_pos<sd.sof_pos) {
		// Copy everything up to 8BIM
		dbuf_copy(c->infile, srcpos, sd._8bim_pos-srcpos, outf);

		dbuf_writeu16be(outf, 0xffed);
		dbuf_writeu16be(outf, 2 + 14 + (sd._8bim_len-4));
		dbuf_write(outf, (const u8*)"Photoshop 3.0\0", 14);
		dbuf_copy(c->infile, sd._8bim_pos+4, sd._8bim_len-4, outf);
		srcpos = sd._8bim_pos + sd._8bim_len;
	}

	// Copy everything up to SOF
	dbuf_copy(c->infile, srcpos, sd.sof_pos-srcpos, outf);
	srcpos = sd.sof_pos;

	// Insert DQT segments
	if(!sd.found_dqt) {
		u8 tmptbl[64];

		dbuf_write(outf, (const u8*)"\xff\xdb\x00\x43\x00", 5);
		fmtutil_get_std_jpeg_qtable(0, tmptbl);
		picjpeg_scale_qtable(tmptbl, lum_setting);
		dbuf_write(outf, tmptbl, 64);

		dbuf_write(outf, (const u8*)"\xff\xdb\x00\x43\x01", 5);
		fmtutil_get_std_jpeg_qtable(1, tmptbl);
		picjpeg_scale_qtable(tmptbl, chr_setting);
		dbuf_write(outf, tmptbl, 64);
	}

	// Copy everything up to SOS
	dbuf_copy(c->infile, srcpos, sd.sos_pos-srcpos, outf);
	srcpos = sd.sos_pos;

	// Insert DHT segment
	if(!sd.found_dht) {
		dbuf_write(outf, (const u8*)"\xff\xc4\x01\xa2\x00", 5);
		fmtutil_write_std_jpeg_dht(outf, 0);
		dbuf_writebyte(outf, 0x10);
		fmtutil_write_std_jpeg_dht(outf, 1);
		dbuf_writebyte(outf, 0x01);
		fmtutil_write_std_jpeg_dht(outf, 2);
		dbuf_writebyte(outf, 0x11);
		fmtutil_write_std_jpeg_dht(outf, 3);
	}

	// Copy the rest of the file
	dbuf_copy(c->infile, srcpos, c->infile->len-srcpos, outf);

done:
	if(need_errmsg) {
		de_err(c, "Can't convert this Pegasus JPEG file");
	}
	dbuf_close(outf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rdi, %rbx
leaq 0xd797d(%rip), %rsi     # 0x10acec
callq 0xe07f7
movq 0x18(%rbx), %rsi
movq 0x18(%rsi), %r8
leaq 0xe8(%rsp), %rdx
pushq $0x19
popq %r9
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0xebba5
movq 0x18(%rbx), %rdi
pushq $0xa
popq %rsi
callq 0xe409f
movq 0x18(%rbx), %rcx
movq 0x18(%rcx), %rcx
movq %rax, 0x28(%rsp)
subq %rax, %rcx
cmpq $0x2, %rcx
jge 0x333e2
leaq 0xd7983(%rip), %rsi     # 0x10ad3e
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %r15, %rdi
callq 0xe5591
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x36
popq %r14
leaq 0xd7913(%rip), %r15     # 0x10ad00
xorl %ebp, %ebp
cmpq $0x52, %r14
je 0x3341f
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe409f
movl %eax, 0x4a(%rsp,%r14)
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xde896
addq $0x4, %r14
incl %ebp
jmp 0x333ef
movdqa 0x80(%rsp), %xmm0
pcmpeqd 0xd7300(%rip), %xmm0    # 0x10a730
movmskps %xmm0, %eax
xorl $0xf, %eax
jne 0x333b4
cmpl $0x8, 0x90(%rsp)
jne 0x333b4
movq 0x18(%rbx), %r13
movq 0x18(%r13), %rax
movq %rax, 0x60(%rsp)
leaq 0xa0(%rsp), %r14
movq 0x28(%rsp), %r8
movq $0x0, 0x50(%rsp)
movq $0x0, 0x70(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x48(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movq $0x0, 0x58(%rsp)
movq $0x0, 0x8(%rsp)
xorl %ecx, %ecx
movq $0x0, 0x40(%rsp)
movq $0x0, 0x78(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x68(%rsp)
movq %rcx, 0x10(%rsp)
movq %r8, 0xa0(%rsp)
movq %r8, %r15
leaq 0x4(%r15), %rbp
cmpq 0x60(%rsp), %rbp
jg 0x336f5
movq %r13, %rdi
movq %r14, %rsi
callq 0xe3f2a
cmpb $-0x1, %al
jne 0x333b4
movq %r13, %rdi
movq %r14, %rsi
callq 0xe3f2a
movl %eax, %r12d
leal 0x1(%r12), %eax
cmpb $0x2, %al
jb 0x333b4
cmpq 0x28(%rsp), %r15
jne 0x33542
cmpb $-0x28, %r12b
jne 0x336f5
movq 0xa0(%rsp), %r15
movq 0x28(%rsp), %rax
movq %rax, 0x58(%rsp)
jmp 0x334e0
cmpb $-0x27, %r12b
setne %al
je 0x336f7
movq %rax, 0x20(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0xe3f6e
leaq 0x2(%rax), %r8
movzbl %r12b, %ecx
addl $0xffffff40, %ecx       # imm = 0xFFFFFF40
cmpl $0x22, %ecx
ja 0x336af
leaq 0xd71ef(%rip), %rdx     # 0x10a76c
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
movq 0x30(%rsp), %r12
movq %r15, %rdx
jmpq *%rcx
movb $0x1, %al
movq %rax, 0x38(%rsp)
movq %rdx, %rax
jmp 0x33698
movb $0x1, %al
movq %rax, 0x50(%rsp)
jmp 0x33693
cmpq $0x9, %rax
setge %al
cmpb $0x0, 0x1c(%rsp)
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x33693
movq %r8, 0x20(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
leaq 0xd724a(%rip), %rdx     # 0x10a81f
pushq $0x4
popq %rcx
callq 0xe4d48
testl %eax, %eax
je 0x336c3
movl $0x0, 0x1c(%rsp)
jmp 0x33633
cmpq $0x7, %rax
setge %al
cmpb $0x0, 0x18(%rsp)
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x33693
movq %r8, 0x20(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
leaq 0x1099ba(%rip), %rdx    # 0x13cfd5
pushq $0x4
popq %rcx
callq 0xe4d48
testl %eax, %eax
je 0x336dd
movl $0x0, 0x18(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %r15, %rdx
movq 0x20(%rsp), %r8
jmp 0x3369d
cmpq $0x9, %rax
jl 0x33693
cmpb $0x0, 0x48(%rsp)
jne 0x33693
movq %r13, %rdi
movq %rbp, %rsi
leaq 0xd71b8(%rip), %rdx     # 0x10a819
pushq $0x5
popq %rcx
movq %r8, %rbp
callq 0xe4d48
movq %rbp, %r8
movq %r15, %rdx
testl %eax, %eax
sete %al
movq %rax, 0x48(%rsp)
movq 0x10(%rsp), %rcx
cmoveq %r15, %rcx
movq 0x8(%rsp), %rax
jmp 0x3369d
movb $0x1, %al
movq %rax, 0x70(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %r12, 0x30(%rsp)
movq %rax, 0x8(%rsp)
addq %rdx, %r8
jmp 0x334d0
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %r12
movq 0x10(%rsp), %rcx
movq %r15, %rdx
jmp 0x3369d
movb $0x1, %al
movl %eax, 0x1c(%rsp)
movq %r15, %rdx
movq %r15, 0x40(%rsp)
movq 0x20(%rsp), %r8
movq %r8, 0x78(%rsp)
jmp 0x33693
movb $0x1, %al
movl %eax, 0x18(%rsp)
movq %r15, %rdx
movq %r15, %r12
movq 0x20(%rsp), %r8
movq %r8, 0x68(%rsp)
jmp 0x33693
xorl %eax, %eax
xorl %edx, %edx
cmpb $0x0, 0x1c(%rsp)
movq 0x8(%rsp), %r12
movq 0x78(%rsp), %r13
je 0x333b4
cmpb $0x0, 0x38(%rsp)
je 0x333b4
testb %al, %al
je 0x333b4
cmpq %r12, 0x40(%rsp)
jge 0x333b4
movq %r12, %r14
movq %rdx, 0x28(%rsp)
movq 0x18(%rbx), %rdi
movq 0x40(%rsp), %r15
leaq 0x9(%r15), %rsi
callq 0xe3eb4
movzbl %al, %r12d
movq 0x18(%rbx), %rdi
leaq 0xa(%r15), %rsi
callq 0xe3eb4
movzbl %al, %ebp
leaq 0xd75b3(%rip), %rsi     # 0x10ad14
movq %rbx, %rdi
movl %r12d, 0x38(%rsp)
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
leaq 0xd75a8(%rip), %rsi     # 0x10ad22
movq %rbx, %rdi
movl %ebp, 0x60(%rsp)
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movzbl 0x50(%rsp), %edx
leaq 0xd759c(%rip), %rsi     # 0x10ad32
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
leaq 0xf2e33(%rip), %rsi     # 0x1265da
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xe4e62
movq %rax, %r15
movq 0x18(%rbx), %rdi
pushq $0x2
popq %rdx
movq 0x58(%rsp), %rbp
movq %rbp, %rsi
movq %rax, %rcx
callq 0xe45f4
leaq 0x2(%rbp), %rsi
cmpb $0x0, 0x48(%rsp)
je 0x33813
movq 0x10(%rsp), %rax
cmpq %rsi, %rax
jne 0x33813
movq 0x18(%rbx), %rdi
pushq $0x9
popq %rdx
movq %rax, %rsi
movq %r15, %rcx
callq 0xe45f4
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0xe5bc5
pushq $0x2
popq %rsi
movq %r15, %rdi
callq 0xe5bc5
movq 0x58(%rsp), %rsi
addq $0xd, %rsi
movq 0x18(%rbx), %rdi
movq 0x40(%rsp), %rbp
movq %rbp, %rdx
subq %rsi, %rdx
movq %r15, %rcx
callq 0xe45f4
addq %rbp, %r13
cmpb $0x0, 0x18(%rsp)
movq %r14, %r12
je 0x338a8
movq 0x30(%rsp), %rbp
movq %rbp, %rdx
subq %r13, %rdx
setge %al
cmpq %r12, %rbp
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x338a8
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0xe45f4
movl $0xffed, %esi           # imm = 0xFFED
movq %r15, %rdi
callq 0xe5f1e
movq 0x68(%rsp), %r13
leaq 0xc(%r13), %rsi
movq %r15, %rdi
callq 0xe5f1e
leaq 0xd6f72(%rip), %rsi     # 0x10a7f8
pushq $0xe
popq %rdx
movq %r15, %rdi
callq 0xe46ba
movq 0x18(%rbx), %rdi
leaq 0x4(%rbp), %rsi
leaq -0x4(%r13), %rdx
movq %r15, %rcx
callq 0xe45f4
addq %rbp, %r13
movq 0x18(%rbx), %rdi
movq %r12, %rdx
subq %r13, %rdx
movq %r13, %rsi
movq %r15, %rcx
callq 0xe45f4
cmpb $0x0, 0x70(%rsp)
jne 0x3394a
leaq 0xd6f38(%rip), %rsi     # 0x10a807
pushq $0x5
popq %r12
movq %r15, %rdi
movq %r12, %rdx
callq 0xe46ba
leaq 0xa0(%rsp), %rbp
xorl %edi, %edi
movq %rbp, %rsi
callq 0xee276
movq %rbp, %rdi
movl 0x38(%rsp), %esi
callq 0x346f3
pushq $0x40
popq %r13
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0xe46ba
leaq 0xd6ef8(%rip), %rsi     # 0x10a80d
movq %r15, %rdi
movq %r12, %rdx
callq 0xe46ba
pushq $0x1
popq %rdi
movq %rbp, %rsi
callq 0xee276
movq %rbp, %rdi
movl 0x60(%rsp), %esi
callq 0x346f3
movq %r15, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %r12
movq %r13, %rdx
callq 0xe46ba
movq 0x18(%rbx), %rdi
movq 0x28(%rsp), %r14
movq %r14, %rdx
subq %r12, %rdx
movq %r12, %rsi
movq %r15, %rcx
callq 0xe45f4
cmpb $0x0, 0x50(%rsp)
jne 0x339ca
leaq 0xd6ea1(%rip), %rsi     # 0x10a813
pushq $0x5
popq %rdx
movq %r15, %rdi
callq 0xe46ba
movq %r15, %rdi
xorl %esi, %esi
callq 0xee2bf
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0xe5bc5
pushq $0x1
popq %rbp
movq %r15, %rdi
movl %ebp, %esi
callq 0xee2bf
movq %r15, %rdi
movl %ebp, %esi
callq 0xe5bc5
pushq $0x2
popq %rsi
movq %r15, %rdi
callq 0xee2bf
pushq $0x11
popq %rsi
movq %r15, %rdi
callq 0xe5bc5
pushq $0x3
popq %rsi
movq %r15, %rdi
callq 0xee2bf
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r14, %rdx
movq %r14, %rsi
movq %r15, %rcx
callq 0xe45f4
jmp 0x333c8
movq 0x20(%rsp), %rax
jmp 0x336f9