// File: /workspace/github_c_makefile_star2_O1/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 0xe36b5(%rip), %rsi     # 0x117ca4
callq 0xeb6d6
movq 0x18(%rbx), %rsi
movq 0x18(%rsi), %r8
xorl %r12d, %r12d
leaq 0xe8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
movl $0x19, %r9d
callq 0xf7881
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xef37e
movq %rax, %r13
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
subq %r13, %rax
movl $0x0, %r14d
cmpq $0x2, %rax
jge 0x3466f
testb %r12b, %r12b
jne 0x34655
leaq 0xe36ab(%rip), %rsi     # 0x117cf6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %r14, %rdi
callq 0xf0b13
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x36, %r14d
leaq 0xe363c(%rip), %r15     # 0x117cb8
xorl %r12d, %r12d
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef37e
movl %eax, 0x4a(%rsp,%r14)
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
addq $0x4, %r14
incl %r12d
cmpq $0x52, %r14
jne 0x3467f
xorl %r14d, %r14d
cmpl $0x2c, 0x80(%rsp)
movl $0x0, %r12d
jne 0x3463f
cmpl $0x18, 0x84(%rsp)
jne 0x3463f
cmpl $0x0, 0x88(%rsp)
jne 0x3463f
cmpl $0x2, 0x8c(%rsp)
jne 0x3463f
cmpl $0x8, 0x90(%rsp)
jne 0x3463f
movq 0x18(%rbx), %r12
movq 0x18(%r12), %rax
movq %rax, 0x68(%rsp)
subq %r13, %rax
movq %r13, 0xa0(%rsp)
cmpq $0x4, %rax
jge 0x3476f
movb $0x1, %al
xorl %ecx, %ecx
movq $0x0, 0x30(%rsp)
movb $0x1, %sil
xorl %r8d, %r8d
xorl %edx, %edx
movb $0x1, %dil
xorl %r13d, %r13d
xorl %r15d, %r15d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
xorl %ebp, %ebp
movq $0x0, 0x58(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x40(%rsp)
jmp 0x34a35
movq %r13, %r15
addq $0x4, %r13
movq $0x0, 0x30(%rsp)
leaq 0xa0(%rsp), %r14
movq $0x0, 0x70(%rsp)
movq $0x0, 0x78(%rsp)
movl $0x0, 0xc(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x58(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x40(%rsp)
movq %r15, 0x60(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0xef14d
cmpb $-0x1, %al
jne 0x349f1
movq %r12, %rbp
movq %r12, %rdi
movq %r14, %r12
movq %r14, %rsi
callq 0xef14d
movl %eax, %r14d
leal 0x1(%r14), %eax
cmpb $0x2, %al
jb 0x349f1
cmpq 0x60(%rsp), %r15
jne 0x34848
cmpb $-0x28, %r14b
jne 0x349f5
movq %r15, 0x38(%rsp)
movq %r12, %r14
movq %rbp, %r12
jmp 0x348a1
cmpb $-0x27, %r14b
je 0x349f5
movq %rbp, %rdi
movq %r12, %rsi
callq 0xef193
leaq 0x2(%rax), %rsi
movzbl %r14b, %ecx
addl $0xffffff40, %ecx       # imm = 0xFFFFFF40
cmpl $0x22, %ecx
ja 0x349cf
leaq 0xe2ea9(%rip), %rdx     # 0x117724
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
movq %r12, %r14
jmpq *%rcx
movb $0x1, %al
movq %rax, 0x78(%rsp)
movq %r15, 0x18(%rsp)
movq %rbp, %r12
addq %r15, %rsi
movq %rsi, 0xa0(%rsp)
movq 0xa0(%rsp), %r15
leaq 0x4(%r15), %r13
cmpq 0x68(%rsp), %r13
jle 0x347f7
jmp 0x349f5
movb $0x1, %al
movq %rax, 0x30(%rsp)
jmp 0x34893
cmpq $0x9, %rax
setge %al
cmpb $0x0, 0x48(%rsp)
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x34893
movq %rsi, (%rsp)
movl $0x4, %ecx
movq %rbp, %r12
movq %rbp, %rdi
movq %r13, %rsi
leaq 0xe2ee2(%rip), %rdx     # 0x1177d7
callq 0xf025e
testl %eax, %eax
jne 0x34955
movb $0x1, %al
movq %rax, 0x48(%rsp)
movq %r15, 0x28(%rsp)
movq (%rsp), %rsi
movq %rsi, 0x58(%rsp)
jmp 0x34896
cmpq $0x7, %rax
setge %al
cmpb $0x0, 0x50(%rsp)
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x34893
movq %rsi, (%rsp)
movl $0x4, %ecx
movq %rbp, %r12
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x1155ad(%rip), %rdx    # 0x149ef5
callq 0xf025e
testl %eax, %eax
je 0x349d7
movq (%rsp), %rsi
jmp 0x34896
cmpq $0x9, %rax
jl 0x34893
movl 0xc(%rsp), %eax
testb %al, %al
jne 0x34893
movl $0x5, %ecx
movq %rbp, %rdi
movq %rsi, (%rsp)
movq %r13, %rsi
leaq 0xe2e47(%rip), %rdx     # 0x1177d1
movl %eax, %r14d
callq 0xf025e
movq (%rsp), %rsi
testl %eax, %eax
movzbl %r14b, %r14d
movl $0x1, %eax
cmovel %eax, %r14d
movl %r14d, 0xc(%rsp)
movq %r12, %r14
movq %rbp, %r12
movq 0x20(%rsp), %rax
cmoveq %r15, %rax
movq %rax, 0x20(%rsp)
jmp 0x34896
movb $0x1, %al
movq %rax, 0x70(%rsp)
jmp 0x34893
movq %r12, %r14
jmp 0x34893
movb $0x1, %al
movq %rax, 0x50(%rsp)
movq %r15, 0x10(%rsp)
movq (%rsp), %rsi
movq %rsi, 0x40(%rsp)
jmp 0x34896
xorl %eax, %eax
jmp 0x349f7
movb $0x1, %al
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpb $0x0, 0x48(%rsp)
setne %sil
cmpb $0x0, 0x78(%rsp)
setne %dl
andb %sil, %dl
cmpb $0x0, 0xc(%rsp)
setne %r8b
cmpb $0x0, 0x50(%rsp)
sete %dil
cmpb $0x0, 0x70(%rsp)
sete %sil
movq 0x28(%rsp), %rbp
movq 0x38(%rsp), %r13
testb %al, %al
je 0x34d92
andb %dl, %cl
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpb $0x1, %cl
jne 0x3463f
cmpq 0x18(%rsp), %rbp
jge 0x3463f
movb %dil, 0x60(%rsp)
movb %sil, 0x68(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x9(%rbp), %rsi
movq %r8, 0x38(%rsp)
callq 0xef0d5
movzbl %al, %r12d
movq 0x18(%rbx), %rdi
movq %rbp, 0x28(%rsp)
leaq 0xa(%rbp), %rsi
callq 0xef0d5
movzbl %al, %ebp
leaq 0xe3237(%rip), %rsi     # 0x117ccc
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0xe3231(%rip), %rsi     # 0x117cda
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movzbl 0x30(%rsp), %edx
leaq 0xe3229(%rip), %rsi     # 0x117cea
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
leaq 0xfe9e8(%rip), %rsi     # 0x1334ba
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %r14
movq 0x18(%rbx), %rdi
movl $0x2, %edx
movq %r13, %rsi
movq %rax, %rcx
callq 0xefabc
leaq 0x2(%r13), %rsi
cmpq %rsi, 0x20(%rsp)
sete %al
movq 0x38(%rsp), %rcx
andb %al, %cl
cmpb $0x1, %cl
jne 0x34b3f
movq 0x18(%rbx), %rdi
movl $0x9, %edx
movq %r14, %rcx
callq 0xefabc
movq %r14, %rdi
movl $0x1, %esi
callq 0xf11be
movq %r14, %rdi
movl $0x2, %esi
callq 0xf11be
addq $0xd, %r13
movq %r13, %rsi
movq 0x18(%rbx), %rdi
movq 0x28(%rsp), %r13
movq %r13, %rdx
subq %rsi, %rdx
movq %r14, %rcx
callq 0xefabc
movq 0x58(%rsp), %rsi
addq %r13, %rsi
cmpb $0x0, 0x60(%rsp)
jne 0x34bdd
movq 0x10(%rsp), %rax
movq %rax, %rdx
subq %rsi, %rdx
jl 0x34bdd
cmpq 0x18(%rsp), %rax
jge 0x34bdd
movq 0x18(%rbx), %rdi
movq %r14, %rcx
movq %rax, 0x10(%rsp)
callq 0xefabc
movl $0xffed, %esi           # imm = 0xFFED
movq %r14, %rdi
callq 0xf15b0
movq 0x40(%rsp), %r13
leaq 0xc(%r13), %rsi
movq %r14, %rdi
callq 0xf15b0
leaq 0xe2c01(%rip), %rsi     # 0x1177b0
movl $0xe, %edx
movq %r14, %rdi
callq 0xefb82
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rax
leaq 0x4(%rax), %rsi
leaq -0x4(%r13), %rdx
movq %r14, %rcx
callq 0xefabc
addq 0x10(%rsp), %r13
movq %r13, %rsi
movq 0x18(%rbx), %rdi
movq 0x18(%rsp), %rdx
subq %rsi, %rdx
movq %r14, %rcx
callq 0xefabc
cmpb $0x0, 0x68(%rsp)
je 0x34cea
leaq 0xe2bbc(%rip), %rsi     # 0x1177bf
movl $0x5, %edx
movq %r14, %rdi
callq 0xefb82
xorl %r13d, %r13d
leaq 0xa0(%rsp), %rsi
xorl %edi, %edi
callq 0xfa107
movl $0x1, %eax
movl $0xff, %ecx
movzbl 0xa0(%rsp,%r13), %edx
imull %r12d, %edx
movl %edx, %esi
shrl $0x5, %esi
cmpl $0x20, %edx
cmovbl %eax, %esi
cmpl $0x2000, %edx           # imm = 0x2000
cmovael %ecx, %esi
movb %sil, 0xa0(%rsp,%r13)
incq %r13
cmpq $0x40, %r13
jne 0x34c2c
leaq 0xa0(%rsp), %r12
movl $0x40, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0xefb82
leaq 0xe2b48(%rip), %rsi     # 0x1177c5
movl $0x5, %edx
movq %r14, %rdi
callq 0xefb82
movl $0x1, %r13d
movl $0x1, %edi
movq %r12, %rsi
callq 0xfa107
xorl %eax, %eax
movl $0xff, %ecx
movzbl 0xa0(%rsp,%rax), %edx
imull %ebp, %edx
movl %edx, %esi
shrl $0x5, %esi
cmpl $0x20, %edx
cmovbl %r13d, %esi
cmpl $0x2000, %edx           # imm = 0x2000
cmovael %ecx, %esi
movb %sil, 0xa0(%rsp,%rax)
incq %rax
cmpq $0x40, %rax
jne 0x34ca4
leaq 0xa0(%rsp), %rsi
movl $0x40, %edx
movq %r14, %rdi
callq 0xefb82
movq 0x18(%rbx), %rdi
movq %r15, %rdx
movq 0x18(%rsp), %rsi
subq %rsi, %rdx
movq %r14, %rcx
callq 0xefabc
cmpb $0x0, 0x30(%rsp)
jne 0x34d74
leaq 0xe2abc(%rip), %rsi     # 0x1177cb
movl $0x5, %edx
movq %r14, %rdi
callq 0xefb82
movq %r14, %rdi
xorl %esi, %esi
callq 0xfa150
movq %r14, %rdi
movl $0x10, %esi
callq 0xf11be
movq %r14, %rdi
movl $0x1, %esi
callq 0xfa150
movq %r14, %rdi
movl $0x1, %esi
callq 0xf11be
movq %r14, %rdi
movl $0x2, %esi
callq 0xfa150
movq %r14, %rdi
movl $0x11, %esi
callq 0xf11be
movq %r14, %rdi
movl $0x3, %esi
callq 0xfa150
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r15, %rdx
movq %r15, %rsi
movq %r14, %rcx
callq 0xefabc
movb $0x1, %r12b
jmp 0x3463f
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x3463f
movb $0x1, %al
movb $0x1, %cl
jmp 0x349fc