/* mat2cps.c */

#include <stdio.h>
#include <ctype.h>
#include <math.h>

#define BOOLEAN char
#define FALSE 0
#define VERS 1.2 

main(argc,argv)
int argc;
char *argv[];
{
    FILE *fd_inp;
    FILE *fd_out;

    char name[20];
    int type, imagf;
    double *inpr, *inpi;   /* matrix real and imaginary: imaginary not used */

/******    double sqrt();   UNDEFINED _sqrt...    ******/
    int M, N, MN;  /* size of original matrix  */
    int m, n, mn;
    int msd, lsd;  /* least most significant digits to output in hex */
    unsigned char inp_file[256]; /* char *filename gives 4 characters/byte */
    unsigned char out_file[256];

    /* HELP MESSAGE */
    if (argc < 2)  {   /* give help when only program name given (no args) */
        fprintf(stderr,"%s: convert matlab 3 by whatever to color ps\n",
                argv[0]);
        fprintf(stderr,"version  %f              Steve Mann; 1990\n",VERS);
        fprintf(stderr,"---------------------------------------------------");
          fprintf(stderr,"----------------------------\n");
        fprintf(stderr,"%s deleteme.mat chirplet_growler.eps  ",argv[0]);
          fprintf(stderr,"converts deleteme.mat to eps file\n");
        fprintf(stderr,"%s deleteme.mat | lpr -Pps            ",argv[0]);
          fprintf(stderr,"stdout (without creating a file)\n");
        fprintf(stderr,"---------------------------------------------------");
          fprintf(stderr,"----------------------------\n");
        exit(0);
    }

    strcpy(inp_file, argv[1]);  /* for now input file is always first */
    fd_inp = fopen(inp_file,"rb");
    if (fd_inp==NULL)  {perror(argv[0]); exit(-1);}

    /* read in input matrix */
    if (loadmat(fd_inp, &type, name, &M, &N, &imagf, &inpr, &inpi)) {
		printf("\nRead error when calling loadmat\n");
    }
    fclose(fd_inp);
    fprintf(stderr,"%s: input matrix has been read in\n",argv[0]);

    /* output stream may be stdout or file */
    if (argc < 3) {     /* only one extra command line arg given */
        fd_out = stdout;
        fprintf(stderr,"%s : output is stdout\n",argv[0]);
    }
    else {    /* output is a file */
        strcpy(out_file, argv[argc-1]);   /* last argument is outfile */
        fprintf(stderr,"%s ; output file = %s\n", argv[0], out_file);
        fd_out = fopen(out_file, "w");
        if (fd_out==NULL) {perror(argv[0]); exit(-1);}
    }

    /* write out PostScript header */
    fprintf(fd_out, "%%!PS-Adobe-2.0 EPSF-1.2\n");
    fprintf(fd_out, "%%%%Creator: mat2eps, by Steve Mann, 1990\n");
    fprintf(fd_out, "/picstr 3 string def\n");
    fprintf(fd_out, "/imagefile\n");
    fprintf(fd_out, "{ %d %d 8 [%d 0 0 -%d 0 %d]\n",M,N,M,N,N);
    fprintf(fd_out, "{ currentfile picstr readhexstring pop }\n");
    fprintf(fd_out, "false 3 xxxNOT FOR LONG: black and white colorimage } def\n");
    fprintf(fd_out, "54 144 translate\n");
    fprintf(fd_out, "504 504 scale\n");
    fprintf(fd_out, "imagefile\n");

    MN = M*N;

    for (mn=0; mn<MN; mn++) {  /* put into one long column */
        msd = (unsigned char)inpr[mn]/16;
        lsd = (unsigned char)inpr[mn]%16;
        fprintf(fd_out, "%x%x\n",msd,lsd);
    }

    fprintf(fd_out, "showpage\n");

    fprintf(stderr,"%s done: converted %d by %d (%d long) mat to %d lines of hex text\n"
           ,argv[0], M, N, M*N, M*N);
}


