/* ppm2AGA main module                */
/* written 1993-94 by Günther Röhrich */
/* This is version 1.3                */

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>
#include <libraries/iffparse.h>
#include <iffp/ilbmapp.h>
#include <dos/dos.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#ifndef __GNUC__
#include <pragmas/intuition_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <pragmas/exec_pragmas.h>
#endif

#include "ppm2AGA.h"

#ifdef __GNUC__
#include <signal.h>
#define MYSTRCMP strcasecmp
#define MYSTRNCMP strncasecmp
#else
#define MYSTRCMP strcmp
#define MYSTRNCMP strncmp
#endif


/* externals used by this module */

extern int ppm2ilbm(char *PPMfile, ULONG Mode, int NumPlanes, ULONG MaxMem); 
#ifdef AZTEC_C
extern int Enable_Abort; /* Needed to disable default CTRL-C handling */
#endif

/* globals defined in this module */

char  *ver = "\0$VER: ppm2AGA 1.3 (4.6.94)";
struct Library *IntuitionBase = NULL;
struct Library *GfxBase = NULL;
struct Library *IFFParseBase = NULL;
int floyd=0; /* indicates if FS-dithering should be used */
char *ILBMfile;
int ExactColor=0; /* indicates if shifting colors is allowed */
int GfxEnable=0;  /* indicate if Gfx is enabled */
int VGAenable=0;  /* indicate VGA-mode */
int jpegAGA=0;    

void PLError(char *ErrorString); /* prints the message, cleans up and quits */
void PLExit(int returnvalue); /* cleans up and quits */
void pm_message(char* format, ... );
int AbortCheck(void);
void PLProgress(ULONG Value, ULONG MaxValue);


#ifdef __GNUC__
int GCCabort=0;
/* this is called when CTRL-C occurs */
void GCCAbortHandler(void)
{
  GCCabort=1;
}
#endif

void PLUsage(void)
{
 PLError("Usage: ppm2AGA infile outfile [switches]\n"
         "switches: -HAM8    use HAM8-conversion (default)\n"
         "          -HAM6    use HAM6-conversion\n"
         "          -CMAPn   use COLORMAP conversion with n planes\n"
         "          -E       use exact colors (only COLORMAP mode)\n"
         "          -FS      enable FS-dithering (not available for HAM8)\n"
         "          -Mx      load pictures up to x size into memory\n"
         "          -2       enable 2-pass processing for HAM mode\n" 
         "          -D       display picture during processing\n"
         "          -VGA     use VGA screenmode\n"
         "          -jpegAGA create colormap file for jpegAGA\n");
}


main(int argc, char *argv[])
{
 int Error;
 int i;
 ULONG Mode=HAM8;
 int NumPlanes = 63;
 int Pass = 1; /* 2-pass disabled by default */
 ULONG MaxMem = 1000000;

 #ifdef AZTEC_C             /* Disable Aztec C CTRL-C handling */
 Enable_Abort = 0;
 #endif
 #ifdef __GNUC__            /* Modify GNU C CTRL-C handling */
 signal(SIGINT, GCCAbortHandler);
 #endif

 puts("ppm2AGA V1.3 written by Günther Röhrich.");
 
 /* remove the comments for beta versions */
/*  puts("Preliminary version. DO NOT SPREAD IT!");  */


 /* Open all the libraries we need */

 if(!(IntuitionBase = OpenLibrary((UBYTE *)"intuition.library", 33)))
   PLError("Can't open intuition.library V33 or higher.\n");

 if(!(GfxBase = OpenLibrary((UBYTE *)"graphics.library",33)))
   PLError("Can't open graphics.library V33 or higher.\n");
 
 /* iffparse.library V37 works also with Kickstart 1.2/1.3 */

 if(!(IFFParseBase = OpenLibrary((UBYTE *)"iffparse.library",36)))
   PLError("Can't open iffparse.library V36 or higher.\n");

 if(argc < 3) PLUsage();
 
 for(i=3; i<argc; i++)
 {
   #ifndef __GNUC__
   strupr(argv[i]);
   #endif
   if(!MYSTRNCMP(argv[i], "-HAM8", 5))
   {
     Mode = HAM8;
     NumPlanes = 63;
   }
   else if(!MYSTRNCMP(argv[i], "-HAM6", 5))
   {
     Mode = HAM6;
     NumPlanes = 15;
   }
   else if(!MYSTRNCMP(argv[i], "-CMAP", 5))
   {
     Mode = COLORMAP;
     NumPlanes = (int)strtoul(&argv[i][5], NULL, 0);
     if(NumPlanes < 1 || NumPlanes > 8)
     PLError("Only 1 to 8 planes allowed.\n");
   }
   else if(!MYSTRNCMP(argv[i], "-M", 2))
   {
     MaxMem = strtoul(&argv[i][2], NULL, 0);
   }
   else if(!MYSTRNCMP(argv[i], "-FS", 3))
   {
     floyd = 1;
   }
   else if(!MYSTRNCMP(argv[i], "-E", 2))
   {
     ExactColor = 1;
   }
   else if(!MYSTRNCMP(argv[i], "-D", 2))
   {
     GfxEnable = 1;
   }
   else if(!MYSTRNCMP(argv[i], "-2", 2))
   {
     Pass = 0;
   }
   else if(!MYSTRNCMP(argv[i], "-VGA", 4))
   {
     VGAenable = 1;
   }
   else if(!MYSTRNCMP(argv[i], "-jpegAGA", 8))
   {
     jpegAGA = 1;
   }
   else PLUsage();
   if( (jpegAGA == 1) && (Pass == 0 || Mode != HAM8)) PLUsage(); 
 }    
 
 ILBMfile = argv[2];
 if(Mode == HAM8 || Mode == HAM6) NumPlanes = NumPlanes + Pass;
 Error = ppm2ilbm(argv[1], Mode, NumPlanes, MaxMem);

 /* if(Error) pm_message("Error, no picture created.\n"); */

 PLExit(Error);  
}

void PLError(char *ErrorString)
{
 pm_message(ErrorString);
 PLExit(10);
}




/* NOTE: if you want to implement a GUI you only have to */
/* change the functions below, not the complete program  */


/* this is called on exit */

void PLExit(int returnvalue)
{
 if(IntuitionBase) CloseLibrary(IntuitionBase);
 if(GfxBase)       CloseLibrary(GfxBase);
 if(IFFParseBase)  CloseLibrary(IFFParseBase);
 exit(returnvalue);
}


/* this is called to see if we must abort */

int AbortCheck(void)
{
 #ifndef __GNUC__
 return SetSignal(0L,0L)&SIGBREAKF_CTRL_C;
 #else
 return GCCabort;
 #endif 
}


/* this is called for progress reports */
/* not called in this version */

void PLProgress(ULONG Value, ULONG MaxValue)
{
 /* we do nothing special right now */
 printf("\015Progress: %5.2f%%", Value * 100.0 / (MaxValue + 1));
}


/* this is called when a message must be printed */

void pm_message(char* format, ... )
{
  va_list args;

  va_start( args, format );
  vprintf(format, args );
  va_end( args );
}
