// File: /workspace/github_c_makefile_star2_O1/salextpuru[P]speccy-toochain/src/snatzx/ppmtozx.c
// Function: main
int main()
{
FILE *in;
unsigned char buf[1024];
int w,h;
int x,y,a,b,r,g,c,f,specy,mask;

in=stdin;

fgets(buf,sizeof(buf),in);
fscanf(in,"%d%d\n",&w,&h);

if(buf[0]!='P')
  {
  printf("Stdin not a PNM file!\n");
  exit(1);
  }

switch(buf[1])
  {
  case '6':
    break;
  default:
    fprintf(stderr,"Sorry, raw PPM files only (\"P6\").\n");
    exit(1);
  }

if(w!=256 || h!=192)
  fprintf(stderr,"Must be 256x192.\n"),exit(1);

fgets(buf,sizeof(buf),in);
if(atoi(buf)!=255)
  fprintf(stderr,"Maxval must be 255 (run through `pnmdepth 255').\n"),exit(1);

fread(image,3,256*192,in);

if(in!=stdin) fclose(in);

for(y=0;y<192;y++)
  {
  for(x=0;x<256;x++)
    {
    r=image[(y*256+x)*3  ];
    g=image[(y*256+x)*3+1];
    b=image[(y*256+x)*3+2];
    
    /* BFI anyone? */
    c=0;
    if(r==0 && g==0 && b!=0) c=1;
    if(r!=0 && g==0 && b==0) c=2;
    if(r!=0 && g==0 && b!=0) c=3;
    if(r==0 && g!=0 && b==0) c=4;
    if(r==0 && g!=0 && b!=0) c=5;
    if(r!=0 && g!=0 && b==0) c=6;
    if(r!=0 && g!=0 && b!=0) c=7;
    if(r>240 || g>240 || b>240) c+=8;
    
    speccol[y*256+x]=c;
    }
  }

/* map colours to attrs and generate matching bitmaps */
specy=0;
for(y=0;y<24;y++)
  {
  for(x=0;x<32;x++)
    {
    int histo[16],outc[16],idx,fg,bg,fgbr,bgbr,tmp;
    
    for(f=0;f<16;f++) histo[f]=outc[f]=0;
    
    for(b=0;b<8;b++)
      for(a=0;a<8;a++)
        histo[speccol[(y*8+b)*256+x*8+a]]++;
    
    idx=0;
    for(f=0;f<16;f++)
      {
      if(histo[f])
        {
        outc[idx++]=f;
        if(idx==3)
          fprintf(stderr,
           "warning: more than 2 colours in square (%d,%d), data lost!\n",x,y);
        }
      }
    
    bg=outc[0]; fg=outc[1];
    if((bg&7)>(fg&7)) tmp=bg,bg=fg,fg=tmp;
    
    bgbr=(bg>>3); bg&=7;
    fgbr=(fg>>3); fg&=7;
    
    /* sort out BRIGHT - both must be either on or off */
    if(bgbr+fgbr==1)
      {
      /* but black can be either, which complicates matters...
       * bg is the darker, so that'll be black if either is.
       * (fg could also be black, but that's unimportant.)
       * black is always set to non-bright in speccol[], so
       * we need to make bg `bright' if black, else give an
       * error.
       */
      
      if(bg==0)
        bgbr++;
      else
        fprintf(stderr,
         "warning: impossible BRIGHT mix in square (%d,%d), data lost!\n",x,y);
      }
    
    /* make the attr */
    attr[y*32+x]=fliporig[0]=fgbr*64+8*bg+fg;
    
    /* now make this char's bitmap according to the attr. */
    bg=8*bgbr+bg; fg=8*fgbr+fg;
    
    for(b=0;b<8;b++)
      {
      for(a=0,tmp=0,mask=128;a<8;a++,mask>>=1)
        {
        c=speccol[(y*8+b)*256+x*8+a];
        if(c==fg) tmp|=mask;
        }
      
      bmp[(specy+8*b)*32+x]=fliporig[b+1]=~tmp;
      }

#ifndef NO_FLIP    
    /* now see if flipping fg/bg reduces the number of `1' bits.
     * I do this to reduce the time taken to send via tape. (Zero bits
     * are sent in half the time one bits are.)
     */
    flipped[0]=((fliporig[0]&0xc0)|((fliporig[0]>>3)&7)|8*(fliporig[0]&7));
    for(f=1;f<9;f++) flipped[f]=~fliporig[f];
    for(f=a=b=0;f<9;f++)
      {
      for(c=0,mask=128;c<8;c++,mask>>=1)
        {
        a+=((fliporig[f]&mask)?1:0);
        b+=((flipped [f]&mask)?1:0);
        }
      }

    if(a>b)
      {
      /* flip fg/bg. */
      attr[y*32+x]=flipped[0];
      for(f=0;f<8;f++)
        bmp[(specy+8*f)*32+x]=~flipped[f+1];
      }
#endif	/* !NO_FLIP */
    }
  
  specy++;
  if(specy==8) specy=64;
  if(specy==72) specy=128;
  }


/* now finally write as speccy screen on stdout */

fwrite(bmp,32,192,stdout);
fwrite(attr,32,24,stdout);

exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c8, %rsp            # imm = 0x4C8
movq 0x2e04(%rip), %r15      # 0x3fd8
movq (%r15), %rbx
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
callq 0x1090
leaq 0xe0e(%rip), %rsi       # 0x2004
leaq 0x2c(%rsp), %rdx
leaq 0x28(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
cmpb $0x50, (%r14)
jne 0x16f1
cmpb $0x36, 0xc1(%rsp)
jne 0x16ff
cmpl $0x100, 0x2c(%rsp)      # imm = 0x100
jne 0x1717
cmpl $0xc0, 0x28(%rsp)
jne 0x1717
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
callq 0x1090
movq %r14, %rdi
callq 0x1080
cmpl $0xff, %eax
jne 0x172f
leaq 0x2e10(%rip), %rdi      # 0x4080
movl $0x3, %esi
movl $0xc000, %edx           # imm = 0xC000
movq %rbx, %rcx
callq 0x1060
cmpq (%r15), %rbx
je 0x128f
movq %rbx, %rdi
callq 0x1030
leaq 0x2dec(%rip), %rbx      # 0x4082
leaq 0x26de3(%rip), %rcx     # 0x28080
xorl %eax, %eax
movl $0x2, %edi
movl $0x6, %r10d
movl $0x7, %r11d
movl $0x3, %r9d
movq %rax, 0x18(%rsp)
movq %rbx, 0x8(%rsp)
xorl %r14d, %r14d
movb -0x2(%rbx), %bpl
movb -0x1(%rbx), %r15b
movb (%rbx), %r12b
movl %ebp, %eax
orb %r15b, %al
sete %al
testb %r12b, %r12b
setne %r13b
andb %al, %r13b
testb %r12b, %r12b
movzbl %r13b, %esi
movl %esi, %edx
cmovel %edi, %edx
testb %bpl, %bpl
sete %dil
setne %r13b
testb %r15b, %r15b
sete %r8b
setne %al
testb %r8b, %r13b
cmovel %esi, %edx
testb %r12b, %r12b
movl %edx, %esi
cmovnel %r9d, %esi
testb %r8b, %r13b
movl $0x4, %r8d
cmovel %edx, %esi
testb %r12b, %r12b
movl %esi, %edx
cmovel %r8d, %edx
movl $0x5, %r8d
testb %al, %dil
cmovel %esi, %edx
testb %r12b, %r12b
movl %edx, %esi
cmovnel %r8d, %esi
testb %al, %dil
movl $0x2, %edi
cmovel %edx, %esi
testb %r12b, %r12b
movl %esi, %edx
cmovel %r10d, %edx
testb %al, %r13b
cmovel %esi, %edx
testb %r12b, %r12b
movl %edx, %esi
cmovnel %r11d, %esi
testb %al, %r13b
cmovel %edx, %esi
leal 0x8(%rsi), %eax
cmpb $-0xf, %r12b
movzbl %al, %eax
cmovael %eax, %esi
cmpb $-0xf, %r15b
cmovael %eax, %esi
cmpb $-0xf, %bpl
cmovael %eax, %esi
movb %sil, (%rcx,%r14)
incq %r14
addq $0x3, %rbx
cmpq $0x100, %r14            # imm = 0x100
jne 0x12c3
movq 0x18(%rsp), %rax
incq %rax
movq 0x8(%rsp), %rbx
addq $0x300, %rbx            # imm = 0x300
addq $0x100, %rcx            # imm = 0x100
cmpq $0xc0, %rax
jne 0x12b6
leaq 0x26cbc(%rip), %rax     # 0x28080
leaq 0x32cb5(%rip), %rbx     # 0x34080
leaq 0x347be(%rip), %r14     # 0x35b90
xorl %ecx, %ecx
xorl %ebp, %ebp
movq %rcx, 0x10(%rsp)
shlq $0x5, %rcx
movq %rcx, 0x18(%rsp)
movl %ebp, 0x24(%rsp)
shll $0x5, %ebp
movq %rax, 0x30(%rsp)
movq %rax, %rdi
xorl %r12d, %r12d
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movq %rdi, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movzbl (%rax,%rdx), %esi
incl 0x80(%rsp,%rsi,4)
incq %rdx
cmpq $0x8, %rdx
jne 0x1434
incq %rcx
addq $0x100, %rax            # imm = 0x100
cmpq $0x8, %rcx
jne 0x1432
xorl %r15d, %r15d
xorl %eax, %eax
cmpl $0x0, 0x80(%rsp,%r15,4)
je 0x1476
movslq %eax, %rcx
incl %eax
movl %r15d, 0x40(%rsp,%rcx,4)
cmpl $0x3, %eax
je 0x1481
incq %r15
cmpq $0x10, %r15
jne 0x145c
jmp 0x14ae
movq 0x2b58(%rip), %rax      # 0x3fe0
movq %rdi, %r13
movq (%rax), %rdi
leaq 0xbdc(%rip), %rsi       # 0x2071
movl %r12d, %edx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x1050
movq %r13, %rdi
movl $0x3, %eax
jmp 0x1476
movl 0x40(%rsp), %eax
movl 0x44(%rsp), %ecx
movl %eax, %edx
andl $0x7, %edx
movl %ecx, %esi
andl $0x7, %esi
cmpl %esi, %edx
movl %ecx, %r15d
cmoval %eax, %r15d
cmoval %ecx, %eax
movl %r15d, %r13d
sarl $0x3, %r13d
movl %eax, %edx
andl $0x7, %edx
movq %rdi, 0x8(%rsp)
je 0x14ee
sarl $0x3, %eax
addl %r13d, %eax
cmpl $0x1, %eax
je 0x1644
movl %r15d, %ecx
andl $0x7, %ecx
shll $0x6, %r13d
leal (%r13,%rdx,8), %eax
orl %ecx, %eax
movb %al, 0x32b7b(%rip)      # 0x34080
movq 0x18(%rsp), %rcx
addq %r12, %rcx
leaq 0x32b7c(%rip), %rdx     # 0x34090
movb %al, (%rdx,%rcx)
movq %rdi, %rdx
xorl %esi, %esi
leaq 0x32e6d(%rip), %r13     # 0x34390
movl $0x80, %r8d
xorl %r9d, %r9d
xorl %edi, %edi
movzbl (%rdx,%r9), %r10d
cmpl %r10d, %r15d
movl $0x0, %r10d
cmovel %r8d, %r10d
orl %r10d, %edi
incq %r9
shrl %r8d
cmpq $0x8, %r9
jne 0x152e
notb %dil
leaq 0x1(%rsi), %r8
movb %dil, 0x1(%rsi,%rbx)
shll $0x8, %esi
addl %ebp, %esi
movslq %esi, %rsi
addq %r12, %rsi
movb %dil, (%r13,%rsi)
addq $0x100, %rdx            # imm = 0x100
movq %r8, %rsi
cmpq $0x8, %r8
jne 0x1523
movl %eax, %edx
andb $-0x40, %dl
movl %eax, %esi
shrb $0x3, %sil
andb $0x7, %sil
orb %dl, %sil
shlb $0x3, %al
andb $0x38, %al
orb %sil, %al
movb %al, 0x345f5(%rip)      # 0x35b90
xorl %edx, %edx
movb 0x1(%rdx,%rbx), %sil
notb %sil
movb %sil, 0x1(%rdx,%r14)
incq %rdx
cmpq $0x8, %rdx
jne 0x159d
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
movzbl (%rdx,%rbx), %r8d
movzbl (%rdx,%r14), %r9d
movl $0x80, %r10d
movl $0x8, %r11d
movl %r10d, %r15d
andl %r8d, %r15d
cmpl $0x1, %r15d
sbbl $-0x1, %esi
movl %r10d, %r15d
andl %r9d, %r15d
cmpl $0x1, %r15d
sbbl $-0x1, %edi
shrl %r10d
decl %r11d
jne 0x15cf
incq %rdx
cmpq $0x9, %rdx
jne 0x15b9
cmpl %edi, %esi
jle 0x162c
leaq 0x32a8b(%rip), %rdx     # 0x34090
movb %al, (%rdx,%rcx)
movl %ebp, %eax
xorl %ecx, %ecx
movb 0x1(%rcx,%r14), %dl
incq %rcx
notb %dl
cltq
leaq (%r12,%rax), %rsi
movb %dl, (%r13,%rsi)
addl $0x100, %eax            # imm = 0x100
cmpq $0x8, %rcx
jne 0x160c
incq %r12
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
cmpq $0x20, %r12
jne 0x13f6
jmp 0x1678
movq 0x2995(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
leaq 0xa58(%rip), %rsi       # 0x20ad
movq %rdx, 0x38(%rsp)
movl %r12d, %edx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x1050
movq 0x38(%rsp), %rdx
movq 0x8(%rsp), %rdi
jmp 0x14ee
movl 0x24(%rsp), %ebp
incl %ebp
cmpl $0x8, %ebp
movl $0x40, %eax
cmovel %eax, %ebp
cmpl $0x48, %ebp
movl $0x80, %eax
cmovel %eax, %ebp
movq 0x10(%rsp), %rcx
incq %rcx
movq 0x30(%rsp), %rax
addq $0x800, %rax            # imm = 0x800
cmpq $0x18, %rcx
jne 0x13d6
movq 0x2908(%rip), %rbx      # 0x3fc0
movq (%rbx), %rcx
leaq 0x32cce(%rip), %rdi     # 0x34390
movl $0x20, %esi
movl $0xc0, %edx
callq 0x10b0
movq (%rbx), %rcx
leaq 0x329b5(%rip), %rdi     # 0x34090
movl $0x20, %esi
movl $0x18, %edx
callq 0x10b0
xorl %edi, %edi
callq 0x1040
leaq 0x9f3(%rip), %rdi       # 0x20eb
callq 0x1070
jmp 0x174f
movq 0x28da(%rip), %rax      # 0x3fe0
movq (%rax), %rcx
leaq 0x8fa(%rip), %rdi       # 0x200a
movl $0x22, %esi
jmp 0x1745
movq 0x28c2(%rip), %rax      # 0x3fe0
movq (%rax), %rcx
leaq 0x905(%rip), %rdi       # 0x202d
movl $0x11, %esi
jmp 0x1745
movq 0x28aa(%rip), %rax      # 0x3fe0
movq (%rax), %rcx
leaq 0x8ff(%rip), %rdi       # 0x203f
movl $0x31, %esi
movl $0x1, %edx
callq 0x10b0
movl $0x1, %edi
callq 0x1040