next up previous contents index
Next: Examples Using Table Data Up: Examples Using Image Data Previous: An Application in FORTRAN

An Application in C

The following program copymi is written in C and copies an image from one data format to another. It is the application which implements the MIDAS COPY/II command. In this example also the usage of the SCFGET and SCFPUT routines instead of SCIGET and SCIPUT is demonstrated.
This program is tied into MIDAS via the following procedure, copy.prg:
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++
!
!.COPYRIGHT   (C) 1989 European Southern Observatory
!.IDENT       copy.prg
!.AUTHOR      Klaus Banse,  ESO - Garching
!.KEYWORDS    data conversion, copying
!.PURPOSE     copy images (and convert data formats)
!.VERSION     1.0    Creation    890414
!
!-------------------------------------------------------
!
DEFINE/PARAM P1 ? IMA "Enter source frame: "
DEFINE/PARAM P2 ? IMA "Enter destination frame: "
DEFINE/PARAM P3 R4 C "Enter destination format: "
DEFINE/PARAM P5 20000 N "Enter mapping buffer size: "
WRITE/KEYWORD IN_A {P1}
WRITE/KEYWORD OUT_A {P2}
WRITE/KEYWORD INPUTI/I/20/1 {P5}
RUN MID_EXE:copymi
To run the application execute the procedure via
Midas 001> @@ copy inframe outframe I2

/* @(#)copymi.c	6.4 (ESO-IPG) 10/19/93 16:56:29 */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.COPYRIGHT    (c)  1988   European Southern Observatory
.IDENT        copymi.c
.LANGUAGE     C
.AUTHOR       K. Banse   ESO/IPG
.KEYWORDS     Format Conversion
.PURPOSE      Read data frame in input format, convert it to output format
              and store in output frame
.COMMENT      Formats supported are the MIDAS formats:
              I1, I2, R4
.VERSION      1.00  890414:   Creation (adapted from COPYMI.FOR)  KB
.VERSION      1.10  890512:   use SCFGET, SCFPUT to make it faster
.VERSION      1.20  890628:   use SCKGETC and add "delete option"
.VERSION      1.30  910228:   copy also LHCUTS
.VERSION      1.40  910417:   use SCDCOP to copy all descriptors in one go
.VERSION      1.50  911004:   take care of over/underflow
.VERSION      2.00  930929:   long int -> int, unit -> *int
---------------------------------------------------------------------*/

#include   <midas_def.h>

main()

{

char      infile[64], outfile[64], cbuf[12];
char      *pntra, *pntrb;
char      dely[4];

int  unit;
int  maxdim, naxis, npix[6], imnoa, imnob, imnow1, imnow2;
int  felm, nochunk, mapsize, size;
int  info[5], null, nval;

int  i, n, stat, fmtin, fmtout;
int  uflow, oflow;

float   cuts[4];

double  start[6], step[6];


SCSPRO("copymi");            

stat = SCKGETC("IN_A",1,60,&nval,infile);  		/* get input frame   */
stat = SCKGETC("OUT_A",1,60,&nval,outfile);  		/* get output frame  */
stat = SCKGETC("P3",1,10,&nval,cbuf); 		/* get output format */
stat = SCKGETC("P4",1,4,&nval,dely);  		/* get delete flag   */

stat = SCKRDI("INPUTI",20,1,&nval,&mapsize,&unit,&null);  /* mapping size */

stat = SCFINF(infile,2,info);			/* get source format  */
fmtin =  info[1];
fmtout = check_fmt(cbuf);
if (fmtout < 0) SCETER(2,"invalid data format...");

stat = SCFOPN(infile,info[1],0,F_IMA_TYPE,&imnoa);
stat = SCDRDI(imnoa,"NAXIS",1,1,&nval,&naxis,&unit,&null);
maxdim = 6;						/* current limit  */
if (naxis > maxdim) SCETER(3,"Too many dimensions...");

stat = SCDRDI(imnoa,"NPIX",1,naxis,&nval,npix,&unit,&null);
stat = SCDRDD(imnoa,"START",1,naxis,&nval,start,&unit,&null);
stat = SCDRDD(imnoa,"STEP",1,naxis,&nval,step,&unit,&null);
stat = SCDRDR(imnoa,"LHCUTS",1,4,&nval,cuts,&unit,&null);

size = 1;					/*  get total size of frame  */
for (n=0; n< naxis; n++)
   size *= npix[n];

stat = SCFCRE(outfile,fmtout,F_O_MODE,F_IMA_TYPE,size,&imnob);

stat = SCDWRI(imnob,"NAXIS",&naxis,1,1,&unit);
stat = SCDWRI(imnob,"NPIX",npix,1,naxis,&unit);
stat = SCDWRD(imnob,"START",start,1,naxis,&unit);
stat = SCDWRD(imnob,"STEP",step,1,naxis,&unit);

uflow = 0; oflow = 0;


/*  calculate no. of chunks needed  */

if (size <= mapsize)
   {
   mapsize = size;
   nochunk = 1;
   }

else
   nochunk = ((size-1) / mapsize) + 1;


/*  allocate virtual memory for in/out frame buffers  */

stat = SCFCRE("work1",fmtin,F_X_MODE,F_IMA_TYPE,mapsize,&imnow1);
stat = SCFMAP(imnow1,F_X_MODE,1,mapsize,&nval,&pntra);

if (fmtin != fmtout)
   {
   stat = SCFCRE("work2",fmtout,F_X_MODE,F_IMA_TYPE,mapsize,&imnow2);
   stat = SCFMAP(imnow2,F_X_MODE,1,mapsize,&nval,&pntrb);
   }
else
   pntrb = pntra;


/*  now we do the conversion   */

felm = 1;
for (i=0; i<nochunk; i++)
   {
   stat = SCFGET(imnoa,felm,mapsize,&nval,pntra);
   if (fmtin != fmtout) 
      conv_pix(pntrb,pntra,fmtout,fmtin,nval);
   stat = SCFPUT(imnob,felm,nval,pntrb);
   felm += nval;
   }


/*  handle over, underflow  */

if (fmtout == D_I1_FORMAT)
   {
   if (cuts[2] < 0) uflow = 1;
   if (cuts[3] > 255) oflow = 1;
   }
else if (fmtout == D_I2_FORMAT)
   {
   if (cuts[2] < -32768) uflow = 1;
   if (cuts[3] > 32767) oflow = 1;
   }
else if (fmtout == D_UI2_FORMAT)
   {
   if (cuts[2] < 0) uflow = 1;
   if (cuts[3] > 65534) oflow = 1;
   }
if (uflow == 1)
   SCTPUT("Warning: Format conversion results in underflow...");
if (oflow == 1)
   SCTPUT("Warning: Format conversion results in overflow...");


stat = SCDCOP(imnoa,imnob,1,cbuf);	/*  copy all descriptors */


/* if delete flag set, delete the input frame  */

if ((dely[0] == 'D') || (dely[0] == 'd')) SCFDEL(infile);

SCSEPI();
}


------------------------------

The code of the functions called by the main program is omitted. 
The interested reader is referred to the actual MIDAS code.

------------------------------



Send comments to web@eso.org
Last update: 1998-10-23