// File: /workspace/github_c_makefile_star2_O2/enzo1982[P]smooth/misc/codecs.cpp
// Function: PCIIO::WriteLine(smooth::IO::OutStream*, int)
bool PCIIO::WriteLine(PCIOut out, int y)
{
	/* Check compression type.
	 */
	if (compression != PCI	 &&
	    compression != RLE	 &&
	    compression != BZIP2 &&
	    compression != UNCOMPRESSED) return false;

	/* Write image line.
	 */
	int	*line = new int [sizex];
	int	 ctsize;
	int	 maxpalentries;

	switch (colorspace)
	{
		case Color::RGB:
		case Color::YUV:
		case Color::HSV:
		case Color::CMY:
			ctsize = 3;
			maxpalentries = 338;

			break;
		case Color::CMYK:
			ctsize = 4;
			maxpalentries = 338;

			break;
		case Color::GRAY:
			ctsize = 1;
			maxpalentries = 338;

			break;
		default:
			ctsize = 3;
			maxpalentries = 338;

			break;
	}

	for (int x = 0; x < sizex; x++) line[x] = Color(bmp.GetPixel(Point(x, y))).ConvertTo(colorspace).Downsample(bpcc);

	switch (compression)
	{
		case BZIP2:
		case UNCOMPRESSED:
			for (int x = 0; x < sizex; x++) out->OutputBits(line[x], ctsize * bpcc);

			break;
		case RLE:
			for (int x = 0; x < sizex; x++)
			{
				int	 rle = GetRLE(line, sizex, x, rlebits);

				out->OutputBits(line[x], ctsize * bpcc);
				out->OutputBits(rle, rlebits);

				x += rle;
			}

			break;
		case PCI:
			if (GetSLAL(line, previousLine, sizex))
			{
				out->OutputBits(1, 1);
			}
			else
			{
				out->OutputBits(0, 1);

				ProbeComp(0, sizex, line, previousLine, sizex, ctsize, false, bits, bitssaa);
				ProbeComp(0, sizex, line, previousLine, sizex, ctsize, true, bitsnrle, bitssaanrle);

				int	 dorle = 0;
				int	 dosaa = 0;

				if (bitssaa < bits)
				{
					out->OutputBits(1, 1);
					dosaa = 1;

					if (bitssaa < bitssaanrle)
					{
						out->OutputBits(1, 1);
						dorle = 1;
					}
					else
					{
						out->OutputBits(0, 1);
						dorle = 0;
					}
				}
				else
				{
					out->OutputBits(0, 1);
					dosaa = 0;

					if (bits < bitsnrle)
					{
						out->OutputBits(1, 1);
						dorle = 1;
					}
					else
					{
						out->OutputBits(0, 1);
						dorle = 0;
					}
				}

				int	 allinpal = 0;
				int	 gallinpal = 0;
				int	 mod04 = 0;
				int	 mod16 = 0;

				for (int x = 0; x < sizex; x++)
				{
					int	 previousColor = (x > 0 ? line[x - 1] : 0);

					if ((x % 16) == mod16)
					{
						gallinpal = 1;

						for (int x2 = 0; x2 < 16; x2++)
						{
							if ((x + x2) < sizex && GetPaletteEntry(line[x + x2]) == -1) { gallinpal = 0; break; }
						}

						if (gallinpal == 1) out->OutputBits(1, 1);
						else		    out->OutputBits(0, 1);
					}

					if (((x % 4) == mod04) && !gallinpal)
					{
						allinpal = 1;

						for (int x2 = 0; x2 < 4; x2++)
						{
							if ((x + x2) < sizex && GetPaletteEntry(line[x + x2]) == -1) { allinpal = 0; break; }
						}

						if (allinpal == 1) out->OutputBits(1, 1);
						else		   out->OutputBits(0, 1);
					}

					if ((GetPaletteEntry(line[x]) == -1))
					{
						out->OutputBits(0, 1);

						if (GetDelta(line, previousLine, x, ctsize))
						{
							out->OutputBits(1, 1);

							CompressDelta(line, previousLine, x, ctsize, out);
						}
						else
						{
							out->OutputBits(0, 1);
							out->OutputBits(line[x], ctsize * bpcc);
						}

						if (palentries < maxpalentries)
						{
							palette[palentries] = line[x];
							palentries++;
						}
						else
						{
							RotatePalette(line[x]);
						}
					}
					else
					{
						if (!allinpal && !gallinpal) out->OutputBits(1, 1);

						if (GetSAA(line, previousLine, x) && dosaa)
						{
							out->OutputBits(1, 1);
						}
						else
						{
							if (dosaa) out->OutputBits(0, 1);

							if ((palentries - 1) - GetPaletteEntry(line[x]) < 2)
							{
								out->OutputBits(1, 1);
								out->OutputBits((palentries - 1) - GetPaletteEntry(line[x]), 1);
							}
							else
							{
								out->OutputBits(0, 1);

								if ((palentries - 1)-GetPaletteEntry(line[x]) < 18)
								{
									int	 lbits = Math::Min(GetMinimumBits(palentries - 2), 4);

									out->OutputBits(1, 1);
									out->OutputBits((palentries - 1) - GetPaletteEntry(line[x]) - 2, lbits);
								}
								else
								{
									out->OutputBits(0, 1);

									if ((palentries - 1) - GetPaletteEntry(line[x]) < 82)
									{
										int	 lbits = Math::Min(GetMinimumBits(palentries - 18), 6);

										out->OutputBits(1, 1);
										out->OutputBits((palentries - 1)-GetPaletteEntry(line[x]) - 18, lbits);
									}
									else
									{
										out->OutputBits(0, 1);
										out->OutputBits((palentries - 1)-GetPaletteEntry(line[x]) - 82, GetMinimumBits(palentries - 83));
									}
								}
							}
						}

						RotatePaletteEntry(GetPaletteEntry(line[x]));
					}

					if (line[x] == previousColor)
					{
						int	 pcirle = GetRLE(line, sizex, x, 8);

						if (pcirle >= 2)
						{
							out->OutputBits(1, 1);

							if (pcirle > 9)
							{
								out->OutputBits(1, 1);
								out->OutputBits(pcirle - 2, 8);
							}
							else
							{
								out->OutputBits(0, 1);
								out->OutputBits(pcirle - 2, 3);
							}

							x = x + pcirle;

							allinpal = 0;
							gallinpal = 0;

							mod04 = (x + 1) %  4;
							mod16 = (x + 1) % 16;
						}
						else
						{
							out->OutputBits(0, 1);
						}
					}
					else if ((line[x] == previousLine[x]) && dorle)
					{
						int	 pcirle = GetVRLE(line, previousLine, sizex, x, 8);

						if (pcirle >= 2)
						{
							out->OutputBits(1, 1);

							if (pcirle > 9)
							{
								out->OutputBits(1, 1);
								out->OutputBits(pcirle - 2, 8);
							}
							else
							{
								out->OutputBits(0, 1);
								out->OutputBits(pcirle - 2, 3);
							}

							x = x + pcirle;

							allinpal = 0;
							gallinpal = 0;

							mod04 = (x + 1) %  4;
							mod16 = (x + 1) % 16;
						}
						else
						{
							out->OutputBits(0, 1);
						}
					}
				}
			}

			for (int x = 0; x < sizex; x++) previousLine[x] = line[x];

			break;
	}

	delete [] line;

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, 0x18(%rsp)
movl 0x50(%rdi), %ecx
xorl %eax, %eax
cmpl $0x4, %ecx
ja 0xb18a1
cmpl $0x2, %ecx
je 0xb18a1
movl %edx, %ebp
movq %rsi, 0x10(%rsp)
movq 0x18(%rsp), %rbx
movslq 0x10(%rbx), %r12
leaq (,%r12,4), %rax
testq %r12, %r12
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x577a0
movq %rax, 0x20(%rsp)
movl 0x58(%rbx), %eax
movl %eax, 0xc(%rsp)
leaq 0x8(%rbx), %r13
xorl %r14d, %r14d
leaq 0x78(%rsp), %r15
leaq 0x88(%rsp), %rbx
movslq %r12d, %rax
cmpq %rax, %r14
jge 0xb177f
movl %r14d, 0x60(%rsp)
movl %ebp, 0x64(%rsp)
movq %r15, %rdi
movq %r13, %rsi
leaq 0x60(%rsp), %rdx
callq 0x57000
movq 0x18(%rsp), %r12
movl 0x58(%r12), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x58f90
movl 0x54(%r12), %edx
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x581e0
movl 0x70(%rsp), %eax
movq 0x20(%rsp), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
movl 0x10(%r12), %r12d
jmp 0xb1720
xorl %ebp, %ebp
movl 0xc(%rsp), %eax
cmpl $0x5, %eax
sete %bpl
addl $0x3, %ebp
cmpl $0x6, %eax
pushq $0x1
popq %rax
cmovel %eax, %ebp
movq 0x18(%rsp), %rax
movl 0x50(%rax), %eax
cmpq $0x4, %rax
movq 0x20(%rsp), %r15
ja 0xb1897
leaq 0x51466(%rip), %rcx     # 0x102c1c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %ebx, %ebx
movq 0x18(%rsp), %r14
movslq %r12d, %rax
cmpq %rax, %rbx
jge 0xb1897
movslq (%r15,%rbx,4), %rsi
movl 0x54(%r14), %edx
imull %ebp, %edx
movq 0x10(%rsp), %rdi
callq 0x58f60
incq %rbx
movl 0x10(%r14), %r12d
jmp 0xb17c6
xorl %r13d, %r13d
movq 0x18(%rsp), %r14
cmpl %r12d, %r13d
jge 0xb1897
movl 0x60(%r14), %ecx
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0xb24b4
movl %eax, %ebx
movslq %r13d, %rax
movslq (%r15,%rax,4), %rsi
movl 0x54(%r14), %edx
imull %ebp, %edx
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x58f60
movslq %ebx, %rbx
movl 0x60(%r14), %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x58f60
addl %ebx, %r13d
incl %r13d
movl 0x10(%r14), %r12d
jmp 0xb17f8
movq 0xbabbb(%rip), %rsi     # 0x16c410
movq %r15, %rdi
movl %r12d, %edx
callq 0xb2543
testl %eax, %eax
je 0xb18b3
pushq $0x1
popq %rsi
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rdi
callq 0x58f60
movq 0xbab95(%rip), %rax     # 0x16c410
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
movslq 0x10(%rdx), %rdx
cmpq %rdx, %rcx
jge 0xb1897
movl (%r15,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
jmp 0xb187d
movq %r15, %rdi
callq 0x572d0
movb $0x1, %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, 0x30(%rsp)
movq 0x10(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x58f60
movq 0x18(%rsp), %r15
movl 0x10(%r15), %esi
movq 0xbab32(%rip), %rcx     # 0x16c410
subq $0x8, %rsp
leaq 0xbb14f(%rip), %r14     # 0x16ca38
leaq 0xbb144(%rip), %rbx     # 0x16ca34
xorl %edi, %edi
movq 0x28(%rsp), %rdx
movl %esi, %r8d
movl %ebp, %r9d
pushq %r14
pushq %rbx
pushq $0x0
callq 0xb2572
addq $0x20, %rsp
movl 0x10(%r15), %esi
movq 0xbaafa(%rip), %rcx     # 0x16c410
subq $0x8, %rsp
leaq 0xbb11f(%rip), %r12     # 0x16ca40
leaq 0xbb114(%rip), %r15     # 0x16ca3c
xorl %edi, %edi
movq 0x28(%rsp), %rdx
movl %esi, %r8d
movl %ebp, %r9d
pushq %r12
pushq %r15
pushq $0x1
callq 0xb2572
addq $0x20, %rsp
movl 0xbb0ee(%rip), %eax     # 0x16ca38
movl 0xbb0e4(%rip), %ecx     # 0x16ca34
xorl %esi, %esi
movl %eax, 0x40(%rsp)
movl %ecx, 0x3c(%rsp)
cmpl %ecx, %eax
cmovlq %r14, %rbx
movq %r13, %r14
cmovlq %r12, %r15
setl %sil
movq %r13, %rdi
pushq $0x1
popq %r12
movl %r12d, %edx
callq 0x58f60
movl (%rbx), %eax
movl (%r15), %ecx
movq 0x20(%rsp), %r15
xorl %esi, %esi
movl %eax, 0x54(%rsp)
movl %ecx, 0x50(%rsp)
cmpl %ecx, %eax
setl %sil
movq %r13, %rdi
movl %r12d, %edx
callq 0x58f60
movl $0x0, 0xc(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x2c(%rsp)
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movslq 0x10(%rax), %r12
cmpl %r12d, %r13d
jge 0xb1874
movl %r13d, %eax
testl %r13d, %r13d
movq %rax, 0x58(%rsp)
jle 0xb19e4
movl -0x4(%r15,%rax,4), %eax
movl %eax, 0x44(%rsp)
jmp 0xb19ec
movl $0x0, 0x44(%rsp)
movl %r13d, %eax
andl $0xf, %eax
movslq %r13d, %rbx
cmpl 0x2c(%rsp), %eax
jne 0xb1a4f
leaq (%r15,%rbx,4), %r15
xorl %r14d, %r14d
cmpq $0x10, %r14
je 0xb1a24
leaq (%rbx,%r14), %rax
cmpq %r12, %rax
jge 0xb1a1f
movl (%r15,%r14,4), %edi
callq 0xb294f
cmpl $-0x1, %eax
je 0xb1a30
incq %r14
jmp 0xb1a02
pushq $0x1
popq %rsi
pushq $0x1
popq %rdx
movl %edx, 0xc(%rsp)
jmp 0xb1a3d
xorl %esi, %esi
movl $0x0, 0xc(%rsp)
pushq $0x1
popq %rdx
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x58f60
movq 0x20(%rsp), %r15
movl %r13d, %eax
andl $0x3, %eax
xorl 0x28(%rsp), %eax
orl 0xc(%rsp), %eax
jne 0xb1ac0
movq 0x18(%rsp), %rax
movslq 0x10(%rax), %r12
leaq (%r15,%rbx,4), %r15
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0xb1a91
leaq (%rbx,%r14), %rax
cmpq %r12, %rax
jge 0xb1a8c
movl (%r15,%r14,4), %edi
callq 0xb294f
cmpl $-0x1, %eax
je 0xb1aa0
incq %r14
jmp 0xb1a6f
pushq $0x1
popq %rsi
pushq $0x1
popq %rdx
movl %edx, %eax
movq %rax, 0x30(%rsp)
jmp 0xb1aae
xorl %esi, %esi
movq $0x0, 0x30(%rsp)
pushq $0x1
popq %rdx
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x58f60
movq 0x20(%rsp), %r15
movq 0x58(%rsp), %rax
movl (%r15,%rax,4), %r12d
movl %r12d, %edi
callq 0xb294f
cmpl $-0x1, %eax
je 0xb1b11
movq 0x30(%rsp), %rax
orl 0xc(%rsp), %eax
jne 0xb1aef
movq %r14, %rdi
pushq $0x1
popq %rsi
pushq $0x1
popq %rdx
callq 0x58f60
movl 0x3c(%rsp), %eax
cmpl %eax, 0x40(%rsp)
jge 0xb1b65
movq 0xba910(%rip), %rax     # 0x16c410
cmpl (%rax,%rbx,4), %r12d
jne 0xb1b65
movq %r14, %rdi
pushq $0x1
popq %rsi
jmp 0xb1bb7
movq %r14, %rdi
xorl %esi, %esi
pushq $0x1
popq %rbx
movl %ebx, %edx
callq 0x58f60
movq 0xba8e9(%rip), %rsi     # 0x16c410
movq %r15, %rdi
movl %r13d, %edx
movl %ebp, %ecx
callq 0xb297b
testl %eax, %eax
je 0xb1c35
movq %r14, %rdi
pushq $0x1
popq %rsi
movl %ebx, %edx
callq 0x58f60
movq 0xba8c0(%rip), %rsi     # 0x16c410
movq %r15, %rdi
movl %r13d, %edx
movl %ebp, %ecx
movq %r14, %r8
callq 0xb2c4c
jmp 0xb1c61
movl 0x3c(%rsp), %eax
cmpl %eax, 0x40(%rsp)
jge 0xb1b7c
movq %r14, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x58f60
movl 0xbaeae(%rip), %ebx     # 0x16ca30
movl %r12d, %edi
callq 0xb294f
subl %ebx, %eax
movq %r14, %rdi
cmpl $-0x2, %eax
jl 0xb1bd3
pushq $0x1
popq %rsi
pushq $0x1
popq %rdx
callq 0x58f60
movl 0xbae8b(%rip), %ebx     # 0x16ca30
movl %r12d, %edi
callq 0xb294f
notl %eax
addl %ebx, %eax
movslq %eax, %rsi
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x58f60
movl %r12d, %edi
callq 0xb294f
movl %eax, %edi
callq 0xb3541
jmp 0xb1c8d
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x58f60
movl 0xbae4d(%rip), %ebx     # 0x16ca30
movl %r12d, %edi
callq 0xb294f
subl %ebx, %eax
cmpl $-0x12, %eax
jl 0xb1d85
addl $-0x2, %ebx
movl %ebx, %edi
callq 0xb34ff
movl %eax, %ebx
cmpl $0x4, %eax
pushq $0x4
popq %rax
cmovgel %eax, %ebx
movq %r14, %rdi
pushq $0x1
popq %rsi
pushq $0x1
popq %rdx
callq 0x58f60
movl 0xbae10(%rip), %r14d    # 0x16ca30
movl %r12d, %edi
callq 0xb294f
notl %eax
addl %r14d, %eax
addl $-0x2, %eax
jmp 0xb1de1
movslq %r12d, %rax
movq %rax, 0x48(%rsp)
movq %r14, %rdi
xorl %esi, %esi
movl %ebx, %edx
callq 0x58f60
movq 0x18(%rsp), %rax
movl 0x54(%rax), %edx
imull %ebp, %edx
movq %r14, %rdi
movq 0x48(%rsp), %rsi
callq 0x58f60
movslq 0xbadc8(%rip), %rax     # 0x16ca30
cmpq $0x151, %rax            # imm = 0x151
jg 0xb1c85
leaq 0xba7a9(%rip), %rcx     # 0x16c420
movl %r12d, (%rcx,%rax,4)
incl %eax
movl %eax, 0xbadad(%rip)     # 0x16ca30
jmp 0xb1c8d
movl %r12d, %edi
callq 0xb34bd
cmpl 0x44(%rsp), %r12d
jne 0xb1cac
movq 0x18(%rsp), %rax
movl 0x10(%rax), %esi
movq %r15, %rdi
movl %r13d, %edx
pushq $0x8
popq %rcx
callq 0xb24b4
jmp 0xb1ce7
movl 0x50(%rsp), %eax
cmpl %eax, 0x54(%rsp)
jge 0xb1d7d
movq 0xba74f(%rip), %rsi     # 0x16c410
movq 0x58(%rsp), %rax
cmpl (%rsi,%rax,4), %r12d
jne 0xb1d7d
movq 0x18(%rsp), %rax
movl 0x10(%rax), %edx
movq %r15, %rdi
movl %r13d, %ecx
pushq $0x8
popq %r8
callq 0xb3574
cmpl $0x2, %eax
jl 0xb1d70
movl %eax, %ebx
movq %r14, %rdi
pushq $0x1
popq %rsi
pushq $0x1
popq %r12
movl %r12d, %edx
callq 0x58f60
xorl %esi, %esi
cmpl $0xa, %ebx
setae %sil
leal -0x2(%rbx), %r14d
leal (%rsi,%rsi,4), %r15d
addl $0x3, %r15d
movq 0x10(%rsp), %rdi
movl %r12d, %edx
callq 0x58f60
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq 0x10(%rsp), %r14
movl %r15d, %edx
movq 0x20(%rsp), %r15
callq 0x58f60
leal (%rbx,%r13), %ecx
incl %ecx
leal (%rbx,%r13), %eax
movl %ecx, %edx
andl $0x3, %edx
movl %edx, 0x28(%rsp)
andl $0xf, %ecx
movl %ecx, 0x2c(%rsp)
movq $0x0, 0x30(%rsp)
movl $0x0, 0xc(%rsp)
movl %eax, %r13d
jmp 0xb1d7d
movq %r14, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x58f60
incl %r13d
jmp 0xb19ba
movq %r14, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x58f60
movl 0xbac98(%rip), %ebx     # 0x16ca30
movl %r12d, %edi
callq 0xb294f
subl %ebx, %eax
cmpl $-0x52, %eax
jl 0xb1df3
addl $-0x12, %ebx
movl %ebx, %edi
callq 0xb34ff
movl %eax, %ebx
cmpl $0x6, %eax
pushq $0x6
popq %rax
cmovgel %eax, %ebx
movq %r14, %rdi
pushq $0x1
popq %rsi
pushq $0x1
popq %rdx
callq 0x58f60
movl 0xbac5f(%rip), %r14d    # 0x16ca30
movl %r12d, %edi
callq 0xb294f
notl %eax
addl %r14d, %eax
addl $-0x12, %eax
movq 0x10(%rsp), %r14
movslq %eax, %rsi
movq %r14, %rdi
movl %ebx, %edx
jmp 0xb1bba
movq %r14, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x58f60
movl 0xbac2a(%rip), %ebx     # 0x16ca30
movl %r12d, %edi
callq 0xb294f
notl %eax
addl %ebx, %eax
addl $-0x52, %eax
cltq
movq %rax, 0x48(%rsp)
addl $-0x53, %ebx
movl %ebx, %edi
callq 0xb34ff
movq %r14, %rdi
movq 0x48(%rsp), %rsi
movl %eax, %edx
jmp 0xb1bba
nop