#ifdef PEECEE
#include "emul.h"
#else
#include <proto/exec.h>
#include <proto/graphics.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "iff_rw.h"

/* - - Static variables  - - - - - - - - - - - - - - - - - - - - - - - */

char *r_msg = "cannot read PIC file";
char *w_msg = "cannot write to coded file";
char *m_msg = "insufficient memory";
char line[80];
int smooth_image = 0;
extern struct BitMap *bm;

void Error(char * s)
{
  printf("\n\aError: %s\n", s);
  exit(1);
}

int main(int numb_arg, char **arg)
{
        BitMapHeader bmhd;
        struct BitMap *bitmap;
        BYTE   *cmap;
        double CPU_time;
        FILE   *temp_file;
        LONG   BodyBytes;

    if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39L)))
    {
        return FALSE;
    }

    if (numb_arg < 3)
    {
        printf("\nCorrect usage:\n\n%s Old iff file New_iff file [WAVELET]\n\n", *arg);
        exit(0);
    }

    /*
     * Check if file already exists
     */
    if ((temp_file = fopen(arg[2], "rb")) != NULL)
    {
        printf("Overwrite file %s? (y = yes, otherwise quit) ", arg[3]);
        fclose(temp_file);
        gets(line);
        if (line[0] != 'y')
        {
            exit(0);
        }
    }

    CPU_time = (double)clock();

    if (!LoadILBM(arg[1], &bmhd, &cmap, &bitmap))
    {
        printf("Could not read picture\n");
        return 1;
    }

    switch (bmhd.compression)
    {
        case 1:     bmhd.compression = 2;       break;
        case 2:     bmhd.compression = 1;       break;
        default:    printf("Big mistake!!\n");  exit(0);
    }
    BodyBytes = SaveILBM(arg[2], &bmhd, cmap, bitmap);
    FreeBitMap(bitmap);
    if (cmap) free(cmap);

    printf("\n  Compressed file size = %ld bytes (%5.2f bpp).\n",
    BodyBytes, 8.0 * BodyBytes / (float) bmhd.w / (float) bmhd.h);
    printf("\n  CPU time = %6.2f s.\n", ((double) clock() - CPU_time) / (double) CLOCKS_PER_SEC);

    CloseLibrary((struct Library *)GfxBase);
    return 0;
}

