/* * LIBML library - read, write and manipulate MATLAB MAT-files * Copyright (C) 1994 Michael J. Maurer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Michael Maurer * Durand Bldg - Room 232 * Stanford, CA 94305-4055 * (415) 723-1024 */ static char rcsid[]="$Id$"; /****************************************************************************** pgm2mat.c Function: Convert PGM file to Matlab (level 1) format. Michael Maurer, July 1993. ******************************************************************************/ /* $Log$ */ #include #include #include #include #include "libml.h" #define PGM2MAT_C #include "pgm2mat.p" #include "libml.p" char *prog; int main(argc,argv) int argc; char *argv[]; { FILE *fin; FILE *fout; gray **G,maxv; int mr,nc,mtype; char *pname=NULL; prog=argv[0]; pgm_init(&argc,argv); if (argc>1 && pm_keymatch("-n",argv[1],2)) { pname=argv[2]; argv+=2; argc-=2; } if (argc>1 && pm_keymatch("-",argv[1],1)) pm_usage("[-n name] [pgmfile [matfile]]"); fin=(argc>1) ? pm_openr(argv[1]) : stdin; G=pgm_readpgm(fin,&nc,&mr,&maxv); fclose(fin); fout=(argc>2) ? fopen(argv[2],"w") : stdout; if (fout==NULL) { fprintf(stderr,"[main] fopen %s\n",argv[2]); return 1; } if (pname==NULL) { if (argc>2) { if ((pname=strrchr(argv[2],'/'))==NULL) pname=argv[2]; pname=strtok(pname,".;"); } else pname="pgm"; } fixstr(pname); mtype = ML_TYPE + ML_ROW + (maxv<256 ? ML_UCHAR : ML_USHORT); if (mlwrite(fout,mtype,pname,mr,nc,0,G[0],NULL)) { fprintf(stderr,"[main] mlwrite failed\n"); return 1; } fclose(fout); return 0; } #include static char * fixstr(s1) char *s1; { char *s0=s1; s1--; while (*++s1) if (!isalnum(*s1)) *s1='_'; return s0; }