#include <devices/printer.h>
#include <intuition/intuition.h>
#include "ifflib.h"

void *IntuitionBase, *GfxBase, *IFFBase;

struct NewScreen ns =
{
0,0, 0,0, 0,  /* LE, TE, W, H, D */
0,0,	      /* pns */
0,	      /* ViewModes */
CUSTOMSCREEN | SCREENBEHIND | SCREENQUIET, /* type */
0L,	      /* font */
(UBYTE *)"View",       /* title */
0L,	      /* gadgets */
0L,	      /* bitmap */
};

struct NewWindow nw =
{
0,0,0,0,       /* LE,TE,W,H */
0,0,	       /* pns */
RAWKEY | MOUSEBUTTONS,	      /* IDCMP */
SIMPLE_REFRESH | BACKDROP | BORDERLESS | ACTIVATE | RMBTRAP, /* flags */
0L,	       /* Gadget */
0L,	       /* checkmark */
(UBYTE *)"",   /* title */
0L,	       /* screen */
0L,	       /* bitmap */
0,0,	       /* min */
0,0,	       /* max */
CUSTOMSCREEN,  /* type */
};

UWORD colors[4096];
UWORD ncolors;

/*
p(hdr)
BitMapHeader *hdr;
{
printf("width: %d,  height: %d\n",hdr->w,hdr->h);
printf("x: %d, y: %d\n",hdr->x,hdr->y);
printf("nPlanes %d\n",(short)hdr->nPlanes);
printf("masking: %d\n",(short)hdr->masking);
printf("compression: %d\n",(short)hdr->compression);
printf("transparentColor: %d\n",hdr->transparentColor);
printf("xAspect: %d, yAspect:%d\n",
   (short)hdr->xAspect,(short)hdr->yAspect);
printf("pageWidth: %d, pageHeight: %d\n",
   hdr->pageWidth,hdr->pageHeight);

if( hdr->viewmodes != GETVIEWMODES )
   {
   printf("ViewModes: 0");
   if( hdr->viewmodes & PFBA ) printf("|PFBA");
   if( hdr->viewmodes & DUALPF ) printf("|DUALPF");
   if( hdr->viewmodes & HIRES ) printf("|HIRES");
   if( hdr->viewmodes & LACE ) printf("|LACE");
   if( hdr->viewmodes & HAM ) printf("|HAM");
   if( hdr->viewmodes & VP_HIDE ) printf("|VP_HIDE");
   if( hdr->viewmodes & GENLOCK_VIDEO ) printf("|GENLOCK_VIDEO");
   if( hdr->viewmodes & GENLOCK_AUDIO ) printf("|GENLOCK_AUDIO");
   if( hdr->viewmodes & EXTRA_HALFBRITE ) printf("|EXTRA_HALFBRITE");
   printf("\n");
   }
}

pvp(s)
struct Screen *s;
{
struct ViewPort *vp = &s->ViewPort;

printf("screen\nDxOffset: %d DyOffset: %d\n",vp->DxOffset,vp->DyOffset);

}
*/

void CleanPort(port)
struct MsgPort *port;
{
extern void *GetMsg();
struct IntuiMessage *msg;
while( msg = GetMsg(port) ) ReplyMsg(msg);
}

main(argc,argv)
short argc;
char *argv[];
{
extern void *OpenLibrary(), CloseLibrary();
extern struct Window *OpenWindow();
extern struct Screen *OpenScreen();
extern struct ViewPort *ViewPortAddress();
extern void *GetMsg();
extern long Wait();
extern long open_prtdev(), DumpRPort();

extern short QueryIFF();
BitMapHeader hdr;
struct IntuiMessage *i;
struct Window *w = 0L;
struct Screen *s = 0L;
UWORD *clrs = colors, its_cool = 1, prt = 0;
struct BitMap *bm;

if( argc > 1 )
   {
   if( !(GfxBase = OpenLibrary("graphics.library",0L)) )
      goto abort;
   if( !(IntuitionBase = OpenLibrary("intuition.library",0L)) )
      goto abort;
   if( !(IFFBase = OpenLibrary(IFFNAME,1L)) )
      goto abort;

   hdr.viewmodes = GETVIEWMODES;

   if( !QueryIFF(argv[1],&hdr) )
      {
      /*p(&hdr); */

      ns.Width = hdr.pageWidth;
      ns.Height = hdr.pageHeight;
      ns.Depth = hdr.nPlanes;

      /* Most IFF pictures don't contain ViewModes,
       * so the viewmodes variable will be un-changed.
       * also older versions of the IFF.LIBRARY don't
       * support the ViewModes variable.  */
      if( hdr.viewmodes == GETVIEWMODES )
	 {
	 if( hdr.pageWidth > 384 && hdr.nPlanes < 5 ) ns.ViewModes |= HIRES;
	 if( hdr.pageHeight > 240 ) ns.ViewModes |= LACE;
	 if( hdr.nPlanes > 5 ) ns.ViewModes |= HAM;
	 }
      else ns.ViewModes = hdr.viewmodes;

      /* open the screen */
      if( !(s = OpenScreen(&ns)) )
	 goto abort;
      /* pvp(s); */

      /* this piece of code handles over-scanned pictures */
      if( ns.ViewModes & HIRES )
	 {
	 if( ns.Width > 640 ) s->ViewPort.DxOffset = -32;
	 }
      else if( ns.Width > 320 ) s->ViewPort.DxOffset = -16;
      if( ns.ViewModes & LACE )
	 {
	 if( ns.Height > 400 ) s->ViewPort.DyOffset = -32;
	 }
      else if( ns.Height > 200 ) s->ViewPort.DyOffset = -16;
      if( s->ViewPort.DxOffset || s->ViewPort.DyOffset ) RethinkDisplay();

      /* set up and open the new window */
      bm = &s->BitMap;
      nw.MaxWidth = nw.MinWidth = nw.Width = hdr.w;
      nw.MinHeight = nw.MaxHeight = nw.Height = hdr.h;
      nw.Screen = s;
      if( !(w = OpenWindow(&nw)) )
	 goto abort;

      /* load the iff picture */
      GetIFF_bitmap(argv[1],&bm,&clrs,&ncolors,0);

      /* load the colors into the screen's view port */
      LoadRGB4(&s->ViewPort,clrs,(long)ncolors);

      /* bring the screen to the front */
      ScreenToFront(s);

      /* input loop */
      while(its_cool)
	 {
	 Wait(1L<<w->UserPort->mp_SigBit);
	 while( i = GetMsg(w->UserPort) )
	    {
	    if( i->Class == RAWKEY )
	       {
	       switch(i->Code)
		  {
		  case 0x40: its_cool = 0; break;
		  case 0x19: prt = 1; break;
		  default: break;
		  }
	       }
	    ReplyMsg(i);
	    }

	 /* this part prints the picture if the user presses
	  * the 'P' key. */
	 if( prt )
	    {
	    if( !open_prtdev() )
	       {
	       DumpRPort(&s->RastPort,&s->ViewPort,(UWORD)0,(UWORD)0,
	       hdr.w,hdr.h,0L,0L,(UWORD)(SPECIAL_FULLCOLS|SPECIAL_FULLROWS));
	       close_prtdev();
	       prt = 0;
	       }
	    CleanPort(w->UserPort);
	    }
	 }
      CleanPort(w->UserPort);
      }

   abort:
   if( w ) CloseWindow(w);
   if( s ) CloseScreen(s);
   if( IFFBase ) CloseLibrary(IFFBase);
   if( IntuitionBase ) CloseLibrary(IntuitionBase);
   if( GfxBase ) CloseLibrary(GfxBase);
   }
}
