/* Ilbm.library C application:

   cc +p ShowPic.c
   as -cd FileInterface.asm
   as -cd IlbmInterface.asm
   ln -o ShowPic ShowPic.o FileInterface.o IlbmInterface.o -lc32 
*/

#include "math.h"
#include "functions.h"    /* Manx C declarations */
#include "intuition/intuition.h"

#include "exec/tasks.h"
#include "exec/types.h"
#include "exec/memory.h"
#include "graphics/gfxbase.h"
#include "graphics/rastport.h"
#include "graphics/gfx.h"
#include "graphics/view.h"
#include "graphics/text.h"
#include "intuition/intuitionbase.h"

 /* These INCLUDE files come with the ilbm and requester libs */
#include "ILBM_Lib.h"
#include "FileIO.h"


/*-------------------------------defines---------------------------------*/

#define INTUITION_REV 33L
#define GRAPHICS_REV  33L
#define DEPTH         4


struct IntuitionBase *IntuitionBase=0L;
struct GfxBase       *GfxBase=0L;
struct Window        *back_wind=0L;
struct Screen        *main_scrn=0L;

 /* Data for the Dissidents Requester library */
struct RequesterBase *RequesterBase=0L;
struct FileIO        *myFileIO=0L;

 /* Data for the Dissidents ILBM library */
struct ILBMBase      *ILBMBase=0L;
ILBMFrame     			myILBMFrame;

struct TextAttr my_font_attr={(UBYTE *)"topaz.font",TOPAZ_EIGHTY,\
                         FS_NORMAL, FPF_ROMFONT};

struct NewScreen ns={0,0,640,200, DEPTH, 0,1, HIRES,
          SCREENBEHIND | CUSTOMSCREEN, &my_font_attr, (UBYTE *)"Screen",
          NULL, NULL };

struct NewWindow b_nw={0,0,640,200, -1,-1, GADGETDOWN | RAWKEY |\
                     GADGETUP | MENUPICK | MOUSEMOVE,
                     SMART_REFRESH | ACTIVATE | BACKDROP |\
                     WINDOWDEPTH | REPORTMOUSE | BORDERLESS,
                     NULL, NULL, (UBYTE *)"Background Window", NULL,NULL,
                     640,200,640,200, CUSTOMSCREEN};


VOID open_all(), damp_mop();

/*------------------------------start of main()----------------------------*/

main( argc, argv )
LONG argc;
UBYTE *argv[];
{
   UBYTE *string;
   IFFP  Result;
   struct IntuiMessage *mes;

   ULONG secs=250; /* Display for 5 secs */

 /* We expect at least the filename for the first arg. Optionally, any
    additional arg is perceived as a flag to trigger resaving the picture
    as it appears in this window. */
   if( argc > 3 || argc < 2 )
   {
      printf("USAGE: ShowPic filename [resave]\n");
      exit();
   }

   open_all();

 /* The FileIO is for the file requester lib. See Fish Disc #203 for the
    documentation and examples */
   if( (myFileIO = GetFileIO()) == NULL) damp_mop();
   myFileIO->X = 6;
   myFileIO->Y = 11;
   myFileIO->DrawMode = JAM2;
   myFileIO->PenA = 0;
   myFileIO->PenB = 1;

 /* We can open our own window/screen and load pictures there by setting
    the ILBMFrame's iWindow and iScreen fields to this address. Any pic
    that is larger than our window/screen dimensions will be scaled to
    fit. Also, any picture intended for a different view mode than our
    screen (or with more colors) may come out "weird". The main advantage
    of loading into an open window is that we are certain what dimensions
    the screen/window will be. If you had a program which loaded a company
    logo in an IFF ILBM FORM when it started, you would certainly make sure
    that the ILBM file was the proper dimensions for your opened screen.
    In a dissidents product, SpeakerSim, there is a feature for loading in
    an ILBM beneath the speaker plot graph. Since the mouse coordinates
    figure prominantly in obtaining info from the user, it would have been
    too much overhead to tailor all functions to work in both HIRES, LORES,
    and INTERLACE simultaneously. So, there are several "versions" of
    SpeakerSim, each one's graphing routines optimized for the dimensions
    of the main screen. Since the ILBM is meant to be a low-key, background
    beneath the speaker plot, the picture is scaled to fit. Obviously, its
    best to load an ILBM that fits that version of SpeakerSim, but the em-
    phasis is on the graphing, not the background. This is an example of why
    you might load into an existing window.
    Otherwise, we could have the lib open an appropriately sized
    backdrop window/screen by setting iWindow and iScreen to 0. In this
    case, the lib will place the addresses in the ILBMFrame fields. We
    could then do a ModifyIDCMP after the load. Eventually, we'll have
    to CloseWindow and CloseScreen. With this method, the image is never
    scaled (currently the lib doesn't allow for overscan images) as long
    the pic's BMHD w=pageWidth and h=pageHeight, and the image is a standard
    amiga screen dimension. Unfortunately, you won't know whether you have
    a HAM, LORES, HIRES, etc until after the load.

    Both options are shown here. First the picture is loaded into a
    640 by 200, HIRES screen with 4 bitplanes. If you load a LORES pic
    it will scale to fill the screen. If that pic has > 16 colors, some colors
    may not look right. If you load a pic with > 4 planes, it may also
    look weird. If you load a 640by200 pic with 4 planes, it will look
    just right. Next the window/screen is closed, and the picture is
    reloaded with the lib opening a window/screen for us. No scaling.
    Lastly, if you supplied a 2nd arg on the CLI line, it will simply
    resave the picture. This is just for the sake of demonstration. */

   if((main_scrn=(struct Screen *)OpenScreen(&ns)) == NULL) damp_mop();
   b_nw.Screen=main_scrn;
   if((back_wind=(struct Window *)OpenWindow(&b_nw)) == NULL) damp_mop();
   ScreenToFront(main_scrn);

   myILBMFrame.iScreen = main_scrn;
   myILBMFrame.iWindow = back_wind;
   myILBMFrame.iUserFlags = 3;     /* no pointer, and hidden title bar */

   Result=LoadIFFToWindow( argv[1], &myILBMFrame );
   if( !Result )
      Delay( secs );
   else
      {
   /* Get the IFFP error message and display it in a auto requester */
      string=GetIFFPMsg( Result );
      AutoMessage( string, back_wind );
      }

 if(back_wind)
 {                /* drain the IDCMP */
   while( mes=(struct IntuiMessage *)GetMsg(back_wind->UserPort))
     ReplyMsg(mes);
   CloseWindow( back_wind );
   back_wind=0;
 }

 if( main_scrn )     CloseScreen( main_scrn );
 main_scrn=0;

   /* Now the lib opens the window for us */
   myILBMFrame.iScreen = 0;
   myILBMFrame.iWindow = 0;
   myILBMFrame.iUserFlags = 3;     /* no pointer, and hidden title bar */

   Result=LoadIFFToWindow( argv[1], &myILBMFrame );
   if( !Result )
      {
      Delay( secs );
      main_scrn=myILBMFrame.iScreen;
      back_wind=myILBMFrame.iWindow;
      }
   else
      {
   /* Get the IFFP error message and display it in a auto requester */
      string=GetIFFPMsg( Result );
      AutoMessage( string, back_wind );
      }

   if( argv[2] )
   {
      if( SaveWindowToIFF( argv[1], back_wind ))
         AutoMessage("Save OK", back_wind );
      else
         AutoMessage("Save error", back_wind );
   }

   damp_mop();

}          /* end of main() */


/*---------opens Intuition, graphics, requester, and ilbm libs--------*/

VOID open_all()
{

 if( (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library", INTUITION_REV)) == NULL )
    damp_mop();

 if( (GfxBase=(struct GfxBase *)OpenLibrary("graphics.library", GRAPHICS_REV)) == NULL )
    damp_mop();

 if( (RequesterBase=(struct RequesterBase *)OpenLibrary("requester.library", 0L)) == NULL )
   {
    printf("Need the dissidents requester.library on boot disk\n");
    damp_mop();
   }

 if( (ILBMBase=(struct ILBMBase *)OpenLibrary("ilbm.library", 0L)) == NULL )
   {
    printf("Need the dissidents ilbm.library on boot disk\n");
    damp_mop();
   }
}


/*---------------closes windows, screen, libs---------------*/

VOID damp_mop()
{
 struct IntuiMessage *mes;

 if( RequesterBase )
 {
 ReleaseFileIO( myFileIO ); /* ReleaseFileIO checks for myFileIO==NULL */
 }

 if(back_wind)
 {                /* drain the IDCMP */
   while( mes=(struct IntuiMessage *)GetMsg(back_wind->UserPort))
     ReplyMsg(mes);
   ClearMenuStrip( back_wind );
   CloseWindow( back_wind );
 }

 if( main_scrn )     CloseScreen( main_scrn );
 if( ILBMBase )      CloseLibrary( ILBMBase );
 if( RequesterBase ) CloseLibrary( RequesterBase );
 if( GfxBase )       CloseLibrary( GfxBase );
 if( IntuitionBase ) CloseLibrary( IntuitionBase );

 exit(FALSE);

}
