// File: /workspace/github_c_makefile_star2_O2/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 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
cmpb $0x50, (%r14)
jne 0x13af
cmpb $0x36, 0xc1(%rsp)
jne 0x13bd
cmpl $0x100, 0x24(%rsp)      # imm = 0x100
jne 0x13d2
cmpl $0xc0, 0x20(%rsp)
jne 0x13d2
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 0x13e7
leaq 0x2e10(%rip), %rdi      # 0x4080
pushq $0x3
popq %rsi
movl $0xc000, %edx           # imm = 0xC000
movq %rbx, %rcx
callq 0x1060
cmpq (%r15), %rbx
je 0x128d
movq %rbx, %rdi
callq 0x1030
leaq 0x26dec(%rip), %rax     # 0x28080
leaq 0x2de7(%rip), %r11      # 0x4082
xorl %ecx, %ecx
pushq $0x6
popq %r9
pushq $0x7
popq %r10
pushq $0x3
popq %r8
cmpq $0xc0, %rcx
je 0x140b
movq %rcx, (%rsp)
movq %r11, 0x8(%rsp)
xorl %ebx, %ebx
cmpq $0x100, %rbx            # imm = 0x100
je 0x1391
movb -0x2(%r11), %bpl
movb -0x1(%r11), %r14b
movb (%r11), %r15b
movl %ebp, %r12d
orb %r14b, %r12b
sete %r12b
testb %r15b, %r15b
setne %r13b
andb %r12b, %r13b
testb %r15b, %r15b
movzbl %r13b, %ecx
movl %ecx, %edx
pushq $0x2
popq %rsi
cmovel %esi, %edx
testb %bpl, %bpl
sete %sil
setne %r12b
testb %r14b, %r14b
sete %dil
setne %r13b
testb %dil, %r12b
cmovel %ecx, %edx
testb %r15b, %r15b
movl %edx, %ecx
cmovnel %r8d, %ecx
testb %dil, %r12b
cmovel %edx, %ecx
testb %r15b, %r15b
movl %ecx, %edx
pushq $0x4
popq %rdi
cmovel %edi, %edx
testb %r13b, %sil
cmovel %ecx, %edx
testb %r15b, %r15b
movl %edx, %ecx
pushq $0x5
popq %rdi
cmovnel %edi, %ecx
testb %r13b, %sil
cmovel %edx, %ecx
testb %r15b, %r15b
movl %ecx, %edx
cmovel %r9d, %edx
testb %r13b, %r12b
cmovel %ecx, %edx
testb %r15b, %r15b
movl %edx, %ecx
cmovnel %r10d, %ecx
testb %r13b, %r12b
cmovel %edx, %ecx
leal 0x8(%rcx), %edx
cmpb $-0xf, %r15b
movzbl %dl, %edx
cmovael %edx, %ecx
cmpb $-0xf, %r14b
cmovael %edx, %ecx
cmpb $-0xf, %bpl
cmovael %edx, %ecx
movb %cl, (%rax,%rbx)
incq %rbx
addq $0x3, %r11
jmp 0x12c1
movq (%rsp), %rcx
incq %rcx
addq $0x100, %rax            # imm = 0x100
movq 0x8(%rsp), %r11
addq $0x300, %r11            # imm = 0x300
jmp 0x12a9
leaq 0xd35(%rip), %rdi       # 0x20eb
callq 0x1070
jmp 0x1403
movq 0x2c1c(%rip), %rax      # 0x3fe0
movq (%rax), %rcx
leaq 0xc3c(%rip), %rdi       # 0x200a
pushq $0x22
jmp 0x13fa
movq 0x2c07(%rip), %rax      # 0x3fe0
movq (%rax), %rcx
leaq 0xc4a(%rip), %rdi       # 0x202d
pushq $0x11
jmp 0x13fa
movq 0x2bf2(%rip), %rax      # 0x3fe0
movq (%rax), %rcx
leaq 0xc47(%rip), %rdi       # 0x203f
pushq $0x31
popq %rsi
pushq $0x1
popq %rdx
callq 0x10b0
pushq $0x1
popq %rdi
callq 0x1040
leaq 0x26c6e(%rip), %rax     # 0x28080
leaq 0x34777(%rip), %r13     # 0x35b90
leaq 0x32c60(%rip), %rbp     # 0x34080
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpq $0x18, %rcx
je 0x16f1
movq %rcx, 0x10(%rsp)
shlq $0x5, %rcx
movq %rcx, 0x38(%rsp)
movl %r15d, 0x1c(%rsp)
shll $0x5, %r15d
movl %r15d, (%rsp)
movq %rax, 0x28(%rsp)
movq %rax, %rcx
xorl %r12d, %r12d
cmpq $0x20, %r12
je 0x16b9
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1479
andl $0x0, 0x40(%rsp,%rax,4)
andl $0x0, 0x80(%rsp,%rax,4)
incq %rax
jmp 0x1461
movq %rcx, 0x8(%rsp)
movq %rcx, %rax
xorl %ecx, %ecx
cmpq $0x8, %rcx
je 0x14ac
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x14a1
movzbl (%rax,%rdx), %esi
incl 0x80(%rsp,%rsi,4)
incq %rdx
jmp 0x148b
incq %rcx
addq $0x100, %rax            # imm = 0x100
jmp 0x1483
xorl %ebx, %ebx
xorl %eax, %eax
cmpq $0x10, %rbx
je 0x14f8
cmpl $0x0, 0x80(%rsp,%rbx,4)
je 0x14ce
movslq %eax, %rcx
incl %eax
movl %ebx, 0x40(%rsp,%rcx,4)
cmpl $0x3, %eax
je 0x14d3
incq %rbx
jmp 0x14b0
movq 0x2b06(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
leaq 0xb8d(%rip), %rsi       # 0x2071
movl %r12d, %edx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x1050
pushq $0x3
popq %rax
jmp 0x14ce
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, %ebx
cmoval %eax, %ebx
cmoval %ecx, %eax
movl %ebx, %r14d
sarl $0x3, %r14d
movl %eax, %edx
andl $0x7, %edx
je 0x1531
sarl $0x3, %eax
addl %r14d, %eax
cmpl $0x1, %eax
je 0x168a
movl %ebx, %ecx
andl $0x7, %ecx
shll $0x6, %r14d
leal (%r14,%rdx,8), %eax
orl %ecx, %eax
movb %al, 0x32b3a(%rip)      # 0x34080
movq %r12, %rcx
orq 0x38(%rsp), %rcx
leaq 0x32b3b(%rip), %rdx     # 0x34090
movb %al, (%rdx,%rcx)
movq 0x8(%rsp), %rdx
xorl %esi, %esi
leaq 0x32e2a(%rip), %r14     # 0x34390
cmpq $0x8, %rsi
je 0x15c4
movq %rsi, %rdi
shlq $0x8, %rdi
movb $-0x80, %r8b
xorl %r10d, %r10d
xorl %r9d, %r9d
cmpq $0x8, %r10
je 0x15a3
movzbl (%rdx,%r10), %r11d
cmpl %r11d, %ebx
je 0x1591
xorl %r11d, %r11d
jmp 0x1595
movzbl %r8b, %r11d
orb %r9b, %r11b
incq %r10
shrb %r8b
movl %r11d, %r9d
jmp 0x157c
notb %r9b
movb %r9b, 0x1(%rsi,%rbp)
incq %rsi
addl %r15d, %edi
orl %r12d, %edi
movslq %edi, %rdi
movb %r9b, (%rdi,%r14)
addq $0x100, %rdx            # imm = 0x100
jmp 0x1566
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, 0x345ac(%rip)      # 0x35b90
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x15fe
movb 0x1(%rdx,%rbp), %sil
notb %sil
movb %sil, 0x1(%rdx,%r13)
incq %rdx
jmp 0x15e6
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
cmpq $0x9, %rdx
je 0x1646
movzbl (%rdx,%rbp), %r8d
movzbl (%rdx,%r13), %r9d
movl $0x80, %r10d
pushq $0x8
popq %r11
subl $0x1, %r11d
jb 0x1641
movl %r10d, %ebx
andl %r8d, %ebx
cmpl $0x1, %ebx
sbbl $-0x1, %edi
movl %r10d, %ebx
andl %r9d, %ebx
cmpl $0x1, %ebx
sbbl $-0x1, %esi
shrl %r10d
jmp 0x161e
incq %rdx
jmp 0x1604
cmpl %esi, %edi
jle 0x1676
leaq 0x32a3f(%rip), %rdx     # 0x34090
movb %al, (%rcx,%rdx)
movl (%rsp), %eax
xorl %ecx, %ecx
cmpq $0x8, %rcx
je 0x1676
movb 0x1(%rcx,%r13), %dl
incq %rcx
notb %dl
cltq
movb %dl, (%rax,%r14)
addl $0x100, %eax            # imm = 0x100
jmp 0x1659
incq %r12
movq 0x8(%rsp), %rcx
addq $0x8, %rcx
incl (%rsp)
jmp 0x1455
movq 0x294f(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
leaq 0xa12(%rip), %rsi       # 0x20ad
movq %rdx, 0x30(%rsp)
movl %r12d, %edx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x1050
movq 0x30(%rsp), %rdx
jmp 0x1531
movl 0x1c(%rsp), %r15d
incl %r15d
cmpl $0x8, %r15d
pushq $0x40
popq %rax
cmovel %eax, %r15d
cmpl $0x48, %r15d
movl $0x80, %eax
cmovel %eax, %r15d
movq 0x10(%rsp), %rcx
incq %rcx
movq 0x28(%rsp), %rax
addq $0x800, %rax            # imm = 0x800
jmp 0x1425
movq 0x28c8(%rip), %r14      # 0x3fc0
movq (%r14), %rcx
leaq 0x32c8e(%rip), %rdi     # 0x34390
pushq $0x20
popq %rbx
movl $0xc0, %edx
movq %rbx, %rsi
callq 0x10b0
movq (%r14), %rcx
leaq 0x32974(%rip), %rdi     # 0x34090
pushq $0x18
popq %rdx
movq %rbx, %rsi
callq 0x10b0
xorl %edi, %edi
callq 0x1040