// File: /workspace/github_c_makefile_star2_O1/enzo1982[P]smooth/misc/codecs.cpp
// Function: PCIIO::ReadLine(smooth::IO::InStream*, int)
bool PCIIO::ReadLine(PCIIn in, int y)
{
	/* Check compression type.
	 */
	if (compression != PCI	 &&
	    compression != RLE	 &&
	    compression != BZIP2 &&
	    compression != UNCOMPRESSED) return false;

	/* Read 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;
	}

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

			break;
		case RLE:
			for (int x = 0; x < sizex; x++)
			{
				int	 col = in->InputBits(ctsize * bpcc);
				int	 rle = in->InputBits(rlebits);

				for (int i = x; i <= x + rle; i++) line[i] = col;

				x += rle;
			}

			break;
		case PCI:
			if (in->InputBits(1) == 1)
			{
				DecompressSLAL(line, previousLine, sizex);
			}
			else
			{
				int	 allinpal = 0;
				int	 gallinpal = 0;
				int	 mod04 = 0;
				int	 mod16 = 0;

				int	 dosaa = in->InputBits(1);
				int	 dorle = in->InputBits(1);

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

					if ( (x % 16) == mod16		     ) gallinpal = in->InputBits(1);
					if (((x %  4) == mod04) && !gallinpal) allinpal	 = in->InputBits(1);

					int	 col = 0;
					int	 inpal = 1;

					if (!allinpal && !gallinpal) inpal = in->InputBits(1);

					if (inpal == 0)
					{
						if (in->InputBits(1)) col = DecompressDelta(line, previousLine, x, ctsize, in);
						else		      col = in->InputBits(ctsize * bpcc);

						if (palentries < maxpalentries)
						{
							palette[palentries] = col;
							palentries++;
						}
						else
						{
							RotatePalette(col);
						}
					}
					else
					{
						int	 saa = 0;

						if (dosaa) saa = in->InputBits(1);

						if (saa)
						{
							col = previousLine[x];
						}
						else
						{
							if (in->InputBits(1) == 1)
							{
								col = palette[(palentries - 1) - in->InputBits(1)];
							}
							else
							{
								if (in->InputBits(1) == 1)
								{
									int	 lbits = Math::Min(GetMinimumBits(palentries - 2), 4);

									col = palette[(palentries - 1) - in->InputBits(lbits) - 2];
								}
								else
								{
									if (in->InputBits(1) == 1)
									{
										int	 lbits = Math::Min(GetMinimumBits(palentries - 18), 6);

										col = palette[(palentries - 1) - in->InputBits(lbits) - 18];
									}
									else
									{
										col = palette[(palentries - 1) - in->InputBits(GetMinimumBits(palentries - 83)) - 82];
									}
								}
							}
						}

						int	 paletteEntry = GetPaletteEntry(col);

						if (paletteEntry >= 0) RotatePaletteEntry(paletteEntry);
					}

					line[x] = col;

					if (col == previousColor)
					{
						if (in->InputBits(1) == 1)
						{
							int	 pcirle;

							if (in->InputBits(1) == 1) pcirle = in->InputBits(8) + 2;
							else			   pcirle = in->InputBits(3) + 2;

							for (int i = x + 1; i <= x + pcirle; i++) line[i] = line[x];

							x = x + pcirle;

							allinpal = 0;
							gallinpal = 0;

							mod04 = (x + 1) %  4;
							mod16 = (x + 1) % 16;
						}
					}
					else if ((col == previousLine[x]) && dorle)
					{
						if (in->InputBits(1) == 1)
						{
							int	 pcirle;

							if (in->InputBits(1) == 1) pcirle = in->InputBits(8) + 2;
							else			   pcirle = in->InputBits(3) + 2;

							for (int i = x + 1; i <= x + pcirle; i++) line[i] = previousLine[i];

							x = x + pcirle;

							allinpal = 0;
							gallinpal = 0;

							mod04 = (x + 1) %  4;
							mod16 = (x + 1) % 16;
						}
					}
				}
			}

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

			break;
	}

	for (int x = 0; x < sizex; x++) bmp.SetPixel(Point(x, y), Color(line[x], colorspace).Upsample(bpcc).ConvertTo(Color::RGB));

	delete [] line;

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl 0x50(%rdi), %ebx
xorl %eax, %eax
cmpl $0x4, %ebx
ja 0xb8b37
cmpl $0x2, %ebx
je 0xb8b37
movq %rsi, %r12
movq %rdi, %r14
movl %edx, 0x30(%rsp)
movslq 0x10(%rdi), %rax
leaq (,%rax,4), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x56670
movq %rax, %r15
movl 0x58(%r14), %eax
xorl %ecx, %ecx
cmpl $0x5, %eax
sete %cl
addl $0x3, %ecx
cmpl $0x6, %eax
movl $0x1, %ebp
cmovnel %ecx, %ebp
cmpl $0x4, %ebx
ja 0xb8ab0
movl %ebx, %eax
leaq 0x5b6d2(%rip), %rcx     # 0x113b7c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, 0x10(%r14)
jle 0xb8ab0
xorl %ebx, %ebx
movl 0x54(%r14), %esi
imull %ebp, %esi
movq %r12, %rdi
callq 0x572d0
movl %eax, (%r15,%rbx,4)
incq %rbx
movslq 0x10(%r14), %rax
cmpq %rax, %rbx
jl 0xb84c0
jmp 0xb8ab0
cmpl $0x0, 0x10(%r14)
jle 0xb8ab0
xorl %ebx, %ebx
movl 0x54(%r14), %esi
imull %ebp, %esi
movq %r12, %rdi
callq 0x572d0
movq %rax, %r13
movl 0x60(%r14), %esi
movq %r12, %rdi
callq 0x572d0
leal (%rbx,%rax), %ecx
testl %eax, %eax
js 0xb8533
movslq %ebx, %rdx
cmpl %ecx, %edx
cmovlel %ecx, %ebx
leaq (%r15,%rdx,4), %rax
subl %edx, %ebx
incl %ebx
xorl %edx, %edx
movl %r13d, (%rax,%rdx,4)
incq %rdx
cmpl %edx, %ebx
jne 0xb8528
incl %ecx
movl %ecx, %ebx
cmpl 0x10(%r14), %ecx
jl 0xb84f1
jmp 0xb8ab0
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
cmpq $0x1, %rax
jne 0xb857f
movslq 0x10(%r14), %rax
testq %rax, %rax
jle 0xb8a8d
movq 0xbece7(%rip), %rcx     # 0x177250
xorl %edx, %edx
movl (%rcx,%rdx,4), %esi
movl %esi, (%r15,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0xb856b
jmp 0xb8a8d
movl %ebp, 0x1c(%rsp)
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
movq %rax, 0x50(%rsp)
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
movq %rax, 0x48(%rsp)
cmpl $0x0, 0x10(%r14)
jle 0xb8a8d
xorl %ebp, %ebp
leaq 0xbeca5(%rip), %rbx     # 0x177260
movl $0x0, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x20(%rsp)
movq %r14, 0x38(%rsp)
movl $0x0, %eax
testl %ebp, %ebp
jle 0xb85ec
movl %ebp, %eax
movl -0x4(%r15,%rax,4), %eax
movl %eax, 0x34(%rsp)
leal 0xf(%rbp), %eax
testl %ebp, %ebp
cmovnsl %ebp, %eax
andl $-0x10, %eax
movl %ebp, %ecx
subl %eax, %ecx
cmpl 0x14(%rsp), %ecx
jne 0xb8615
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
movq %rax, %r13
leal 0x3(%rbp), %eax
testl %ebp, %ebp
cmovnsl %ebp, %eax
andl $-0x4, %eax
movl %ebp, %ecx
subl %eax, %ecx
xorl 0x18(%rsp), %ecx
orl %r13d, %ecx
jne 0xb863f
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
orl %r13d, %eax
movq %r13, 0x28(%rsp)
jne 0xb8663
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
testl %eax, %eax
je 0xb874a
cmpl $0x0, 0x50(%rsp)
je 0xb868e
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
testl %eax, %eax
je 0xb868e
movslq %ebp, %rax
shlq $0x2, %rax
addq 0xbebc7(%rip), %rax     # 0x177250
jmp 0xb88b7
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
cmpq $0x1, %rax
jne 0xb86cb
movslq 0xbf1c8(%rip), %rbx     # 0x177870
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
notq %rax
addq %rbx, %rax
leaq 0xbeb9e(%rip), %rbx     # 0x177260
leaq (%rbx,%rax,4), %rax
jmp 0xb88b7
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
cmpq $0x1, %rax
jne 0xb8780
movl 0xbf188(%rip), %eax     # 0x177870
addl $-0x2, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movl $0x2, %edi
movl %ebx, %esi
callq 0x56010
ucomisd 0x8(%rsp), %xmm0
ja 0xb8714
incl %ebx
cmpl $0x21, %ebx
jne 0xb86f7
xorl %ebx, %ebx
cmpl $0x4, %ebx
movl $0x4, %eax
cmovgel %eax, %ebx
movslq 0xbf14a(%rip), %r14     # 0x177870
movq %r12, %rdi
movl %ebx, %esi
callq 0x572d0
notq %rax
addq %r14, %rax
leaq 0xbeb23(%rip), %rbx     # 0x177260
leaq (%rbx,%rax,4), %rax
addq $-0x8, %rax
jmp 0xb87fd
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
testq %rax, %rax
je 0xb8807
movq 0xbeae9(%rip), %rsi     # 0x177250
movq %r15, %rdi
movl %ebp, %edx
movl 0x1c(%rsp), %ecx
movq %r12, %r8
callq 0xba129
movl %eax, %r13d
jmp 0xb881b
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
movslq 0xbf0dc(%rip), %r13     # 0x177870
cmpq $0x1, %rax
jne 0xb886c
addl $-0x12, %r13d
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movl $0x2, %edi
movl %ebx, %esi
callq 0x56010
ucomisd 0x8(%rsp), %xmm0
ja 0xb87cc
incl %ebx
cmpl $0x21, %ebx
jne 0xb87af
xorl %ebx, %ebx
cmpl $0x6, %ebx
movl $0x6, %eax
cmovgel %eax, %ebx
movslq 0xbf092(%rip), %r14     # 0x177870
movq %r12, %rdi
movl %ebx, %esi
callq 0x572d0
notq %rax
addq %r14, %rax
leaq 0xbea6b(%rip), %rbx     # 0x177260
leaq (%rbx,%rax,4), %rax
addq $-0x48, %rax
movq 0x38(%rsp), %r14
jmp 0xb88b7
movl 0x54(%r14), %esi
imull 0x1c(%rsp), %esi
movq %r12, %rdi
callq 0x572d0
movq %rax, %r13
movslq 0xbf04e(%rip), %rbx     # 0x177870
cmpq $0x151, %rbx            # imm = 0x151
jg 0xb8844
leaq 0xbea2e(%rip), %rax     # 0x177260
movl %r13d, (%rax,%rbx,4)
leal 0x1(%rbx), %eax
movl %eax, 0xbf031(%rip)     # 0x177870
jmp 0xb88e7
leal -0x1(%rbx), %edx
shlq $0x2, %rdx
leaq 0xbea0e(%rip), %rdi     # 0x177260
leaq 0xbea0b(%rip), %rsi     # 0x177264
callq 0x556d0
leaq 0xbe9fb(%rip), %rax     # 0x177260
movl %r13d, -0x4(%rax,%rbx,4)
jmp 0xb88e7
leal -0x53(%r13), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movl $0x2, %edi
movl %ebx, %esi
callq 0x56010
ucomisd 0x8(%rsp), %xmm0
ja 0xb8899
incl %ebx
cmpl $0x21, %ebx
jne 0xb887c
xorl %ebx, %ebx
movq %r12, %rdi
movl %ebx, %esi
callq 0x572d0
subq %rax, %r13
leaq 0xbe9b3(%rip), %rbx     # 0x177260
leaq (%rbx,%r13,4), %rax
addq $-0x14c, %rax           # imm = 0xFEB4
movl (%rax), %r13d
movl 0xbefb0(%rip), %edi     # 0x177870
movq %rdi, %rax
decq %rax
testl %edi, %edi
jle 0xb88d9
movl %eax, %ecx
decl %edi
decq %rax
cmpl %r13d, (%rbx,%rcx,4)
jne 0xb88c6
jmp 0xb88de
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
testl %edi, %edi
js 0xb88e7
callq 0xba01f
movslq %ebp, %rbx
movl %r13d, (%r15,%rbx,4)
cmpl 0x34(%rsp), %r13d
jne 0xb8997
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
cmpq $0x1, %rax
movq 0x28(%rsp), %r13
jne 0xb8a70
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
xorl %ecx, %ecx
cmpq $0x1, %rax
sete %cl
leal (%rcx,%rcx,4), %esi
addl $0x3, %esi
movq %r12, %rdi
callq 0x572d0
addl %ebp, %eax
addl $0x2, %eax
cmpl %eax, %ebp
jge 0xb8959
movslq %eax, %rcx
movq %rbx, %rdx
movl (%r15,%rbx,4), %esi
movl %esi, 0x4(%r15,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0xb8948
leal 0x1(%rax), %ecx
leal 0x4(%rax), %edx
leal 0x10(%rax), %esi
testl %ecx, %ecx
cmovnsl %ecx, %edx
cmovnsl %ecx, %esi
andl $-0x4, %edx
negl %edx
leal (%rax,%rdx), %ecx
incl %ecx
movl %ecx, 0x18(%rsp)
andl $-0x10, %esi
negl %esi
leal (%rax,%rsi), %ecx
incl %ecx
movl %ecx, 0x14(%rsp)
movq $0x0, 0x20(%rsp)
xorl %r13d, %r13d
jmp 0xb8a72
cmpl $0x0, 0x48(%rsp)
je 0xb8a60
movq 0xbe8a7(%rip), %rax     # 0x177250
cmpl (%rax,%rbx,4), %r13d
jne 0xb8a60
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
cmpq $0x1, %rax
leaq 0xbe895(%rip), %rbx     # 0x177260
movq 0x28(%rsp), %r13
jne 0xb8a89
movq %r12, %rdi
movl $0x1, %esi
callq 0x572d0
xorl %ecx, %ecx
cmpq $0x1, %rax
sete %cl
leal (%rcx,%rcx,4), %esi
addl $0x3, %esi
movq %r12, %rdi
callq 0x572d0
addl %ebp, %eax
addl $0x2, %eax
cmpl %eax, %ebp
jge 0xb8a25
movq 0xbe846(%rip), %rcx     # 0x177250
movslq %ebp, %rsi
movslq %eax, %rdx
movl 0x4(%rcx,%rsi,4), %edi
movl %edi, 0x4(%r15,%rsi,4)
leaq 0x1(%rsi), %rdi
movq %rdi, %rsi
cmpq %rdi, %rdx
jne 0xb8a10
leal 0x1(%rax), %ecx
leal 0x4(%rax), %edx
leal 0x10(%rax), %esi
testl %ecx, %ecx
cmovnsl %ecx, %edx
cmovnsl %ecx, %esi
andl $-0x4, %edx
negl %edx
leal (%rax,%rdx), %ecx
incl %ecx
movl %ecx, 0x18(%rsp)
andl $-0x10, %esi
negl %esi
leal (%rax,%rsi), %ecx
incl %ecx
movl %ecx, 0x14(%rsp)
movq $0x0, 0x20(%rsp)
xorl %r13d, %r13d
jmp 0xb8a79
movl %ebp, %eax
leaq 0xbe7f7(%rip), %rbx     # 0x177260
movq 0x28(%rsp), %r13
jmp 0xb8a79
movl %ebp, %eax
leaq 0xbe7e7(%rip), %rbx     # 0x177260
incl %eax
movl %eax, %ebp
cmpl 0x10(%r14), %eax
jl 0xb85dc
jmp 0xb8a8d
movl %ebp, %eax
jmp 0xb8a79
cmpl $0x0, 0x10(%r14)
jle 0xb8ab0
movq 0xbe7b5(%rip), %rax     # 0x177250
xorl %ecx, %ecx
movl (%r15,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
movslq 0x10(%r14), %rdx
cmpq %rdx, %rcx
jl 0xb8a9d
cmpl $0x0, 0x10(%r14)
movl 0x30(%rsp), %ebp
movq %r15, %r12
jle 0xb8b2d
movq %r14, %r15
leaq 0x8(%r14), %rax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0x68(%rsp), %r13
leaq 0x78(%rsp), %rbx
movl %r14d, 0x40(%rsp)
movl %ebp, 0x44(%rsp)
movslq (%r12,%r14,4), %rax
movl 0x58(%r15), %ecx
movq %rax, 0x60(%rsp)
movl %ecx, 0x58(%rsp)
movl 0x54(%r15), %edx
movq %r13, %rdi
leaq 0x58(%rsp), %rsi
callq 0x57840
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x57d10
movq 0x8(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %rbx, %rdx
callq 0x56080
incq %r14
movslq 0x10(%r15), %rax
cmpq %rax, %r14
jl 0xb8ad7
movq %r12, %rdi
callq 0x561e0
movb $0x1, %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq