[alt.sources] imagetomat -- writes a matrix matlab can read

emv@math.lsa.umich.edu (Edward Vielmetti) (12/29/89)

Archive-name: imagetomat/release

[ Found this hiding in comp.ai.vision of all places.  --Ed ]

Date: Fri, 8 Dec 89 15:03:17 PST
From: ramin@scotty.Stanford.EDU (Ramin Samadani)
Subject: raster to matlab

Regarding raster to matlab conversion. I've taken some licensed stuff out of
the code we use and came up with the following which has the main parts 
for converting a file containing unsigned chars (rows*cols of them) to a
matlab readable format. It is currently hardwired for a vax but could
easily be modified for suns and macs, etc. Also, since I took the licensed
stuff out, the rows and cols are hardwired right now but that should be
easy to fix. The code follows, with no guarantees at all! 

	Ramin Samadani


/* imagetomat.c - writes a matrix matlab can
 * 					read. Double format output,byte
 *					format input for now.
 *
 * usage: matrix-name <in.hpl >out.mat
 *
 * to load: cc -o name name.c -O
 *
 * Ramin Samadani - 6 May 88
 */
int rows = 128;
int cols = 128;

#include <stdio.h>
typedef struct {
	long type; /*type*/
	long mrows; /* row dimension */
	long ncols; /* column dimension */
	long imagf; /* flag indicating imag part */
	long namlen; /* name length (including NULL) */
	} Fmatrix;
char *prog;

main(argc,argv)
        int argc;
        char *argv[];
{
/* VARIABLES */
    int rows,cols, i,j;
    unsigned char *ifr;
    double *ofr;
/*
 * Matlab declarations 
 */
    char *pname; /* pointer to matrix name */
    float *pr; /* pointer to real data */
    FILE *fp;
    Fmatrix x;
    int mn;

    prog = argv[0];

/*
 * check passed parameters
 */

    if (argc < 2) {
        fprintf(stderr,"use: %s matrix name <filein  >fileout\n",prog);
        exit(1);
    }
    if ((pname = (char *) calloc(80,sizeof(char))) == NULL) {
        fprintf(stderr,"%s: can't allocate matrix name\n",prog);
        exit(1);
    }
    pname = argv[1];
    x.type = 2000;
    x.mrows = (long) cols;
    x.ncols = (long) rows;
    x.imagf = 0;
    x.namlen = strlen(pname) + 1;
    fprintf(stderr,"matrix %s has %ld rows, %ld cols, double precision\n",
    	pname, x.mrows,x.ncols);
    rows = rows; cols = cols;

    if ((ifr = (unsigned char *) calloc(rows*cols,sizeof(char))) == NULL){
        fprintf(stderr,"%s: can't allocate input frame\n",prog);
        exit(1);
    }
    if ((ofr = (double *) calloc(rows*cols,sizeof(double))) == NULL){
        fprintf(stderr,"%s: can't allocate output frame\n",prog);
        exit(1);
    }
    if (read(0,ifr,rows*cols*sizeof(char)) == -1) {
        fprintf(stderr,"%s: can't read frame\n",prog);
        exit(1);
    }

/* MAIN PROCESSING */
    mn = x.mrows*x.ncols;
    for (i = 0; i < mn; i++) {
        ofr[i] = (double) (ifr[i]&0377);
    }
    

/*
 * write the matrix
 */
    if(write(1,&x,sizeof(Fmatrix)) != sizeof(Fmatrix)) {
    	fprintf(stderr,"%s: can't write matrix header\n",prog);
    	exit(1);
    }
    if(write(1,pname,(int)x.namlen*sizeof(char)) != 
    		(int)x.namlen*sizeof(char)) {
        fprintf(stderr,"%s: can't write name of matrix\n",prog);
        exit(1);
    }
    if (write(1,ofr,mn*sizeof(double)) != mn*sizeof(double)){
        fprintf(stderr,"%s: can't write matrix data\n",prog);
        exit(1);
    }
}