/* Shows DEGAS  PI1/PC1 pictures on your PC! */


#include <stdio.h>
#include <dos.h>

FILE* f;
union REGS regs;

unsigned char far *screen;
unsigned int id;

main(int argc,char **argv)
{
unsigned char fname[80];

       if(argc > 1)
       {
               if(! (f = fopen(argv[1],"rb")) )
               {
                       printf("Couldn't find %s!\n",argv[1]);
                       exit(1);
               }

               screen = MK_FP(0xA000,0);
               setpal();

               if(id & 0x8000)
                       load_pc();
               else
                       load_pi();

               getch();

               set_text();
       }
       else
       {
               printf("DEGASHOW Atari Degas Elite picture viewer\n");
               printf("usage: degashow <filename.p?1>\n");
       }
}


load_pc()
{
unsigned int i,plane,scrstart;
unsigned int addr,scan,nextline;
unsigned char byte,b;

       dis_refresh();

       for(scan=0;scan < 200;scan++)
       {
               for(plane=0;plane < 4; plane++)              /* four bitplanes */
               {
                       addr = 320*scan;
                       nextline = addr + 320;
                       do
                       {
                               byte = (unsigned char)getc(f);

                               if(byte < 128)
                               {
                                       for(i=0;i < byte+1; i++)
                                       {
                                               b = (unsigned char)getc(f);

                                               screen[addr++] |= (b >> 7 & 1) << plane;
                                               screen[addr++] |= (b >> 6 & 1) << plane;
                                               screen[addr++] |= (b >> 5 & 1) << plane;
                                               screen[addr++] |= (b >> 4 & 1) << plane;
                                               screen[addr++] |= (b >> 3 & 1) << plane;
                                               screen[addr++] |= (b >> 2 & 1) << plane;
                                               screen[addr++] |= (b >> 1 & 1) << plane;
                                               screen[addr++] |= (b & 1) << plane;
                                       }
                               }
                               else if(byte > 128)
                               {
                                       b = (unsigned char)getc(f);

                                       for(i=0;i < (256-byte)+1; i++)
                                       {
                                               screen[addr++] |= (b >> 7 & 1) << plane;
                                               screen[addr++] |= (b >> 6 & 1) << plane;
                                               screen[addr++] |= (b >> 5 & 1) << plane;
                                               screen[addr++] |= (b >> 4 & 1) << plane;
                                               screen[addr++] |= (b >> 3 & 1) << plane;
                                               screen[addr++] |= (b >> 2 & 1) << plane;
                                               screen[addr++] |= (b >> 1 & 1) << plane;
                                               screen[addr++] |= (b & 1) << plane;
                                       }
                               }
                       } while(addr < nextline);
               }
       }
       fclose(f);
       ena_refresh();
}

load_pi()
{
unsigned int i,plane;
unsigned int addr,scan,nextline,offset;
unsigned char b;

       dis_refresh();

       for(scan=0;scan < 200;scan++)
       {
               addr = 320*scan;
               nextline = addr+320;
               offset = 0;

               do
               {
                       for(plane=0;plane<4;plane++)
                       {
                               addr = 320*scan+offset;

                               for (i=0;i<2;i++)
                               {
                                       b = (unsigned char)getc(f);

                                       screen[addr++] |= (b >> 7 & 1) << plane;
                                       screen[addr++] |= (b >> 6 & 1) << plane;
                                       screen[addr++] |= (b >> 5 & 1) << plane;
                                       screen[addr++] |= (b >> 4 & 1) << plane;
                                       screen[addr++] |= (b >> 3 & 1) << plane;
                                       screen[addr++] |= (b >> 2 & 1) << plane;
                                       screen[addr++] |= (b >> 1 & 1) << plane;
                                       screen[addr++] |= (b & 1) << plane;
                               }
                       }
                       offset += 16;
               } while(addr < nextline);
       }
       fclose(f);
       ena_refresh();
}



setpal()
{
int i;
unsigned colword;
unsigned char  red,green,blue;

       set_mcga();

       id = read16bit();

       for(i=0;i < 16 ;i++)
       {
               colword = read16bit();
               blue =  (unsigned char)((colword & 7) * 9) ;
               colword >>= 4;
               green =  (unsigned char)((colword & 7) * 9) ;
               colword >>= 4;
               red =  (unsigned char)((colword & 7) * 9) ;

               regs.h.ah = 0x10;
               regs.h.al = 0x10;
               regs.x.bx = i;
               regs.h.ch = green;
               regs.h.cl = blue;
               regs.h.dh = red;
               int86(0x10,&regs,&regs);
       }
}


int read16bit()
{
       int c1, c2;
       c1 = getc(f);
       c2 = getc(f);
       return to16bit(c1,c2);
}

int to16bit(c1,c2)
int c1, c2;
{
       return ((c1 & 0xff ) << 8) + (c2 & 0xff);
}


set_mcga()
{
       regs.h.ah = 0;                            /* MCGA mode, 320x200 */
       regs.h.al = 0x13;
       int86(0x10,&regs,&regs);
}

set_text()
{
       regs.h.ah = 0;                            /* MCGA mode, 320x200 */
       regs.h.al = 3;
       int86(0x10,&regs,&regs);
}

dis_refresh()
{
       regs.h.ah = 0x12;
       regs.h.al = 1;
       regs.h.bl = 0x36;
       int86(0x10,&regs,&regs);
}

ena_refresh()
{
       regs.h.ah = 0x12;
       regs.h.al = 0;
       regs.h.bl = 0x36;
       int86(0x10,&regs,&regs);
}