Category : C Source Code
Archive   : IMDSRC78.ZIP
Filename : LABUTIL.C

 
Output of file : LABUTIL.C contained in archive : IMDSRC78.ZIP
/*** IMDISP module LABUTIL.C

LABUTIL contains routines for processing PDS and FITS labels.

***/

#define __MSC

/* * * * INCLUDE files * * * */

#include
#include
#include
#include
#include
#include "mshell.h"
#include "imdef.h"
#include "imdisp.h"
#include "imdutil.h"
#include "dispio.h"
#include "disputil.h"
#include "fileio.h"
#include "keywutil.h"
#include "textutil.h"
#include "display.h"

/* * * * External functions * * * */

/* * * * Function declarations * * * */

int ExtractLabelString (char *, int, char *, int *, char *);
int GetLabelInteger (char *, int, char *, int, int *, int *);
int GetLabelLong (char *, int, char *, long, long *, int *);
int GetLabelReal (char *, int, char *, float, float *, int *);
int GetLabelString (char *, int, char *, char *, char *, int *);
int InterpretLabel (char *,int,unsigned int *,int *,int *,int *,int *,int *,char *,char *,int);
void DoNoLabel (char *,int,unsigned int *,int *,int *,int *,int *,int *,char *,int *);
void DoFITSLabel (char *,int,unsigned int *,int *,int *,int *,int *,int *,int *,int);
void DoVicarLabel(char *,int,unsigned int *,int *,int *,int *,int *,int *,int *);
int PutLabel (char *, int *, char *);
int PutLabelValue (char *, int *, char *, int);
int MakeLabel (char *, int *, int, int, int, int);
int GetCommand (char *,char *);

/* * * * Global Variables * * * */

int PDSused = 0, switched = 1;
long GiantLabel;
int LastSearch;
float maxlat, minlat, maxlon, minlon;
char Object[32];


int ExtractLabelString (char * LabelBuf, int LabelBufLen, char * Keyword,
int * p_flag, char * value)

/* ExtractLabelString extracts the string "value" following the
keyword in the label buffer.

*/

{
int i, j, k, l, m;
char *searchptr;


/* Find where the keyword is in the buffer */
LabelBuf[LabelBufLen-1] = 0; /* add a null */
searchptr = strstr (LabelBuf, Keyword);
if (searchptr == NULL)
*p_flag = -1;
else
{
j = searchptr - LabelBuf;
if ((j > 0) && (LabelBuf[j-1] > ' '))
{
/* found something like end_object */
*p_flag = 0;
LastSearch += j+6;
}
else
{
LastSearch += j+6;
*p_flag = 1;
l = LabelBufLen - 1;
do /* scan past the keyword */
j++;
while ((LabelBuf[j] != ' ') && (LabelBuf[j] != '=') && (j != l));
if (j == l) *p_flag = 0;

do /* scan to the next non-blank, paren, or non equal char */
j++;
while ( ((LabelBuf[j] <= ' ') || (LabelBuf[j] == '=') ||
(LabelBuf[j] == '(')) && (j < l) );

k = j;
if (LabelBuf[j] == '"')
{
j++; /* if value begins with a quote, read to next quote */
do
k++;
while ( (LabelBuf[k] != '"') && (k <= l) );

m=k+1;
if (LabelBuf[m] == ':' || LabelBuf[m] == ',') /* check for file record offset */
{
do
m++;
while ((LabelBuf[m] != ' ' && LabelBuf[m] !=')') && (m <= l) );
strncpy(value,LabelBuf+k+2,m-k+2);
value[m-k+2] = 0; /* add a null */
*p_flag = atoi(value) + 1;
}
}
else if (LabelBuf[j] == '\'')
{
j++; /* if value begins with a single quote, read to next quote */
do
k++;
while ( (LabelBuf[k] != '\'') && (k <= l) );
m=k+1;
if (LabelBuf[m] == ':' || LabelBuf[m] == ',')
/* check for file record offset */
{
do
m++;
while ((LabelBuf[m] != ' ' && LabelBuf[m] !=')') && (m <= l) );
strncpy(value,LabelBuf+k+2,m-k+2);
value[m-k+2] = 0; /* add a null */
*p_flag = atoi(value) + 1;
}
}
else
{
do /* scan past the value to next space */
k++;
while ( (LabelBuf[k] > ' ') && (k <= l) );
}

strncpy (value, LabelBuf+j, k-j);
value[k-j] = 0;
}
}
}



int GetLabelInteger (char * LabelBuf, int LabelBufLen, char * Keyword,
int defaul, int * p_value, int * p_flag)

/*** GetLabelInteger returns the integer value following the keyword in
the label buffer. If the keyword is not there then the default
value is returned. The flag indicates whether the keyword is
there.

Parameter Type In/out Description
LabelBuf char ptr in The buffer containing the label
LabelBufLen int in The length in bytes of the label buffer
Keyword char ptr in The keyword string to match
defaul int in The value to return if no keyword or value
p_value int ptr out The returned value
p_flag int ptr out The flag: -1 if no keyword match
0 if illegal value, 1 if keyword and value

***/

{ int i;
char stringvalue[80];

ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
if (*p_flag >= 1)
{
/* new patch to eliminate GALILEO SSI file names which begin */
/* with numbers from being interpreted as image offsets. */
/* If there is any non-numeric in the string it is rejected. */
/* MDM 1991-11-14 */
for (i=0;i if (stringvalue[i] < 48 || stringvalue[i] > 57)
{
*p_flag = 0;
*p_value = defaul;
return(0);
}
if (sscanf (stringvalue, "%d", p_value) == 0)
{
*p_flag = 0;
*p_value = defaul;
}
}
else
{
*p_value = defaul;
}
}

int GetLabelLong (char * LabelBuf, int LabelBufLen, char * Keyword,
long defaul, long * p_value, int * p_flag)

/*** GetLabelInteger returns the integer value following the keyword in
the label buffer. If the keyword is not there then the default
value is returned. The flag indicates whether the keyword is
there.

Parameter Type In/out Description
LabelBuf char ptr in The buffer containing the label
LabelBufLen int in The length in bytes of the label buffer
Keyword char ptr in The keyword string to match
defaul long in The value to return if no keyword or value
p_value long ptr out The returned value
p_flag int ptr out The flag: -1 if no keyword match
0 if illegal value, 1 if keyword and value

***/

{
char stringvalue[80];

ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
if (*p_flag == 1)
{
if (sscanf (stringvalue, "%D", p_value) == 0)
{
*p_flag = 0;
*p_value = defaul;
}
}
else
{
*p_value = defaul;
}
}




int GetLabelReal (char * LabelBuf, int LabelBufLen, char * Keyword,
float defaul, float * p_value, int * p_flag)

/*** GetLabelReal returns the real value following the keyword in the
label buffer. If the keyword is not there then the default value
is returned. The flag indicates whether the keyword is there.

Parameter Type In/out Description
LabelBuf char ptr in The buffer containing the label
LabelBufLen int in The length in bytes of the label buffer
Keyword char ptr in The keyword string to match
defaul float in The value to return if no keyword or value
p_value float ptr out The returned value
p_flag int ptr out The flag: -1 if no keyword match
0 if illegal value, 1 if keyword and value

***/

{
char stringvalue[80];

ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
if (*p_flag == 1)
{
if (sscanf (stringvalue, "%f", p_value) == 0)
{
*p_flag = 0;
*p_value = defaul;
}
}
else
{
*p_value = defaul;
}
}

int GetLabelString (char * LabelBuf, int LabelBufLen, char * Keyword,
char * defaul, char * value, int * p_flag)

/*** GetLabelString returns the string value following the keyword in
the label buffer. If the keyword is not there then the default
value is returned. The flag indicates whether the keyword is
there.

Parameter Type In/out Description
LabelBuf char ptr in The buffer containing the label
LabelBufLen int in The length in bytes of the label buffer
Keyword char ptr in The keyword string to match
defaul char ptr in The value to return if no keyword or value
p_value char ptr out The returned value
p_flag int ptr out The flag: -1 if no keyword match
0 if illegal value, 1 if keyword and value
>1 returns record offset +1
***/

{
char stringvalue[255];

ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
if (*p_flag >= 1)
{
strcpy (value, stringvalue);
strupr (value);
}
else
{
strcpy (value, defaul);
}
return(*p_flag);
}

int InterpretLabel (char * buf, int len, unsigned int * p_labelsize,
int * p_nline, int * p_nsamp, int * p_bitsperpix,
int * p_reclen, int * p_lineheadbytes,
char * DetachedFileName, char * DetachedPaletteName,
int unit)

/*** InterpretLabel interprets the label in the buffer to extract the
image information. First something is identified to determine
whether the image has a PDS, FITS or Vicar2 label or no label.
If the image is labeled then the appropriate keyword values are
extracted, otherwise the user is prompted for the into.

Parameter Type In/out Description
buf char ptr in The label buffer
len int in The length in bytes of the label buffer
p_labelsize uns int ptr out The size of the label or header to skip
p_nline int ptr out The number of lines in the image
p_nsamp int ptr out The number of samples in the image
p_bitsperpix int ptr out The pixel format (16,8,4,1)
p_reclen int ptr out The record length in bytes
p_lineheadbytes int ptr out The header bytes at beginning of each line
DetachedFileName char ptr out The name of the file of data
null string if no detached label
DetachedPaletteName char ptr out The name of the detached color palette
null string if no detached palette
unit int in The number to the current file being
processed

***/

{
char FileType[32], SampleType[32], inpstr[32];
char ObjectName[5][32];
int image_loc[5];
char *object_loc;
int images=0;
int i, j, k, flag, irflag, labelrec, bitspixdef;
int totalrecs, labelrecs, labelreclen;
int headerrecs, headerreclen, nlinedef, linereclen, image_offset=1;
int imagerecs, imagereclen, imrecldef;
int *p_linetrailbytes;
int done, bytecount, read_an_end, finallength;
char *tempbufptr;
char line[81];
FILE *labels;

char tmpch;
char *count, tmpname[32];
char colname[32];

strcpy (Source_File_Name, "");

if ((p_linetrailbytes = (int *) malloc(sizeof(int)))==NULL)
{
FatalError( "Not enough memory for trail.\n");
}

/* check for variable length file */
GetLabelString (buf,len,"RECORD_TYPE","FIXED_LENGTH",FileType,&flag);
if (strncmp(FileType,"VARIABLE_LENGTH",15) == 0)
{
TextLine = TextHeight + 5; TextSample = 1;
ClearDisplay(0);
WriteText ("This is a compressed image.");
WriteText (" ");
WriteText ("Use the PCDCOMP program in the SOFTWARE directory");
WriteText ("to decompress it, then display the uncompressed image.");
*p_reclen = 0; *p_bitsperpix = 8;
free(p_linetrailbytes);
return(0);
}

strcpy (DetachedFileName, "");
strcpy (DetachedPaletteName,"");
GiantLabel = 0L;
LastSearch = 0;
strcpy(Object,"^");
/* Check for multiple objects */
do
{
/*** fix this to not need object + trailing blank */
GetLabelString (buf+LastSearch, len-LastSearch, "OBJECT ",
"", ObjectName[images], &flag);
if (flag > 0) /* if an object = argument has been found */
{
strcpy(tmpname, ObjectName[images]);
if ( (count = strrchr(tmpname,'_')) == NULL) /* Look for last _ */
{
/* _ not found */
tmpch= tmpname[0];
}
else
{
/* put last keyword string into tmpname */
strcpy(tmpname,count+1);
tmpch= tmpname[0];
}

/* Check for objects other than IMAGE */
switch(tmpch)
{
case 'I':

if (tmpname[1] == 'M') /* IMAGE */
object_loc = strstr(ObjectName[images],"IMAGE");
else if(tmpname[1] == 'N') /* INTENSITY_SPECTRUM */
object_loc = strstr(ObjectName[images],"INTEN");
else
object_loc = NULL;
break;

case 'S':

object_loc = strstr(ObjectName[images],"SPECT");
break;

case 'P':

object_loc = strstr(ObjectName[images],"POSIT");
break;

case 'T':

if (tmpname[1] == 'A') /* TABLE */
{
object_loc = strstr(ObjectName[images],"TABLE");
/* Added the next line to ignore tables for now, take out if
you want tables to be included - 06/23/90 Ron Baalke */
object_loc = NULL;
}
else if(tmpname[1] == 'E') /* TEXT */
object_loc = strstr(ObjectName[images],"TEXT");
else
object_loc = NULL;
break;

case 'L':

if (tmpname[1] == 'A') /* LABEL */
/* object_loc = strstr(ObjectName[images],"LABEL"); */
object_loc = NULL; /* don't process labels */
else if(tmpname[1] == 'H') /* LHC_POLARIZATION_SPECTRUM */
object_loc = strstr(ObjectName[images],"LHC_P");
else
object_loc = NULL;
break;

case 'R': /* RHC_POLARIZATION_SPECTRUM */

object_loc = strstr(ObjectName[images],"RHC_P");
break;

default:

object_loc = NULL;
break;
}


if (object_loc != NULL &&
((i=object_loc - ObjectName[images]) ==
(strlen(ObjectName[images])-strlen(tmpname) )) )
{
image_loc[images] = LastSearch;
images++;
}
} /* end OBJECT found */
} while (flag >= 0);

if (images == 1) strcat(Object,ObjectName[0]);
if (images > 1)
{
ClearDisplay(0);
TextLine = TextHeight + 5; TextSample = 1;
WriteText ("More than one image object in file.");
for (i=0;i {
sprintf(line," %d. %s",i+1,ObjectName[i]);
WriteText (line);
}
TypeText ("Select the image to be displayed: ");
do
{
AcceptText (inpstr);
flag = sscanf (inpstr, "%d", &i);
} while (!flag);
i--;
if (i < 0 || i > images) i = 0;
strcat(Object,ObjectName[i]);
strcpy(line,ObjectName[i]);
strcpy(ObjectName[0],line);
for (j=0;j {
if (j != i) /* blank out labels for this object */
{
object_loc = strstr(buf+image_loc[j],"END_OBJECT");
if (object_loc != NULL)
for (k=image_loc[j];k<(int)(object_loc-buf);k++) buf[k] = ' ';
}
}
}

/* This part for PDS labels */
GetLabelString (buf, len, "FILE_TYPE", " ", FileType, &flag);
if (flag < 0 && Object[1] != '\0')
{
strcat(Object," ");

/* tmpch=ObjectName[0][0]; */
/* Multiple object fix (start) */
strcpy(tmpname, ObjectName[0]);
if ( (count = strrchr(tmpname,'_')) == NULL) /* Look for last _ */
{
/* _ not found */
tmpch= tmpname[0];
}
else
{
/* put last keyword string into tmpname */
strcpy(tmpname,count+1);
tmpch= tmpname[0];
}
/* Multiple object fix (end) */
GetLabelInteger (buf, len, Object, 0, &image_offset, &flag);
if (flag < 0)
{
switch(tmpch)
{
case 'E':
strcpy(Object,"^TABLE");
break;
case 'I':

/* if(ObjectName[0][1] == 'M') */
if(tmpname[1] == 'M')
strcpy(Object,"^IMAGE"); /* if pointer not found try this */
if(ObjectName[0][1] == 'N')
strcpy(Object,"^INTEN");
break;

case 'S':

strcpy(Object,"^SPECT");
break;

case 'P':

strcpy(Object,"^POSIT");
break;

case 'T':

if(ObjectName[0][1] == 'A')
strcpy(Object,"^TABLE");
if(ObjectName[0][1] == 'E')
strcpy(Object,"^TEXT");
break;

case 'L':

if(ObjectName[0][1] == 'A')
strcpy(Object,"^LABEL");
if(ObjectName[0][1] == 'H')
strcpy(Object,"^LHC_P");
break;

case 'R':

strcpy(Object,"^RHC_P");
break;
}

GetLabelInteger (buf, len, Object, 0, &image_offset, &flag);
}

/* Switch over the various objects */
if (flag >= 0)
switch(tmpch)
{
case 'E':
strcpy(FileType,"TABLE");
break;
case 'I':

/* if(ObjectName[0][1] == 'M') */
if(tmpname[1] == 'M')
strcpy(FileType,"IMAGE");
if(ObjectName[0][1] == 'N')
strcpy(FileType,"INTEN");
break;

case 'S':

strcpy(FileType,"SPECT");
break;

case 'P':

strcpy(FileType,"POSIT");
break;

case 'T':

if(ObjectName[0][1] == 'A')
strcpy(FileType,"TABLE");
if(ObjectName[0][1] == 'E')
strcpy(FileType,"TEXT");
break;

case 'L':

if(ObjectName[0][1] == 'A')
strcpy(FileType,"LABEL");
if(ObjectName[0][1] == 'H')
strcpy(FileType,"LHC_P");
break;

case 'R':

strcpy(FileType,"RHC_P");
break;
}
if (flag == 0)
{
GetLabelString (buf, len, Object, "", DetachedFileName, &flag);
if (flag > 1)
image_offset = flag - 1;
if (flag == 1)
image_offset = 1;
}
}

if ( strncmp( FileType, "IMAGE", 5) == 0
|| strncmp( FileType, "SPECT", 5) == 0
|| strncmp( FileType, "POSIT", 5) == 0
|| strncmp( FileType, "INTEN", 5) == 0
|| strncmp( FileType, "LHC_P", 5) == 0
|| strncmp( FileType, "RHC_P", 5) == 0
|| strncmp( FileType, "TABLE", 5) == 0
|| strncmp( FileType, "TEXT", 4) == 0
|| strncmp( FileType, "LABEL", 5) == 0 )
{
GetLabelInteger (buf, len, "RECORD_BYTES", 512, p_reclen, &flag);
GetLabelInteger (buf, len, "FILE_RECORDS", 513, &totalrecs, &flag);
GetLabelInteger (buf, len, "LABEL_RECORDS", 1, &labelrecs, &flag);
GetLabelInteger (buf, len, "LABEL_RECORD_BYTES", *p_reclen,
&labelreclen, &flag);
GetLabelInteger (buf, len, "HEADER_RECORDS", 0,
&headerrecs, &flag);
GetLabelInteger (buf, len, "HEADER_RECORD_BYTES", *p_reclen,
&headerreclen, &flag);
GetLabelLong (buf, len, "^IMAGE_HISTOGRAM", 0L,
&Histogram, &flag);
if (Histogram != 0L) Histogram = (Histogram-1)* (long)*p_reclen;
GetLabelLong (buf, len, "^PALETTE", 0L,&Palette, &flag);
if (Palette != 0L) Palette = (Palette-1)* (long)*p_reclen;
if (flag == 0)
/* GetLabelString(buf,len,"^PALETTE","",PaletteFileName,&flag); */
GetLabelString(buf,len,"^PALETTE","",DetachedPaletteName,&flag);
if (Object[1] == '\0')
{
GetLabelInteger (buf, len, "^IMAGE ",0,&image_offset, &flag);
if(flag < 1)
GetLabelInteger(buf,len,"^SPECT",0,&image_offset,&flag);
if(flag < 1)
GetLabelInteger(buf,len,"^POSIT",0,&image_offset,&flag);
if(flag < 1)
GetLabelInteger(buf,len,"^INTEN",0,&image_offset,&flag);
if(flag < 1)
GetLabelInteger(buf,len,"^LHC_P",0,&image_offset,&flag);
if(flag < 1)
GetLabelInteger(buf,len,"^RHC_P",0,&image_offset,&flag);
if(flag < 1)
GetLabelInteger(buf,len,"^TABLE",0,&image_offset,&flag);
if(flag < 1)
GetLabelInteger(buf,len,"^TEXT",0,&image_offset,&flag);
if(flag < 1)
GetLabelInteger(buf,len,"^LABEL",0,&image_offset,&flag);
}

if (image_offset == 0)
GiantLabel = (long)labelrecs*(long)labelreclen
+ (long)headerrecs*(long)headerreclen;
else
GiantLabel = (long)(image_offset-1) * (long)*p_reclen;

if (GiantLabel < 65535L)
*p_labelsize = (unsigned int)GiantLabel;
else
*p_labelsize = 0;

/* Find nline */
if (*p_labelsize == 0)
nlinedef = totalrecs;
else
nlinedef = totalrecs - (((*p_labelsize-1) / *p_reclen) + 1);

GetLabelInteger (buf, len, "IMAGE_RECORDS", nlinedef, &imagerecs, &irflag);
GetLabelInteger (buf, len, "IMAGE_LINES", imagerecs, p_nline, &flag);
if (flag < 1)
GetLabelInteger (buf, len, "LINES", imagerecs,p_nline, &flag);
if (flag < 1)
GetLabelInteger(buf,len,"ROWS",imagerecs,p_nline,&flag);

if (irflag < 1) imagerecs = *p_nline;

/* Find reclen */
GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen,p_reclen, &flag);
if (flag < 1)
GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_reclen,&flag);
GetLabelInteger (buf, len, "RECORD_BYTES", *p_reclen, p_reclen, &flag);
imrecldef = *p_reclen * (*p_nline / imagerecs);
GetLabelInteger (buf, len, "IMAGE_RECORD_BYTES", imrecldef,
&imagereclen, &flag);
*p_reclen = imagereclen/ (*p_nline /imagerecs);

/* Find nsamp */
GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen, p_nsamp, &flag);
if (flag < 1)
GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_nsamp,&flag);

/* patch for magellan cdrom MG_0001 version 2 browse images */
if (*p_nsamp == 896 && *p_nline == 1024)
{
GetLabelString(buf,len,"SPACECRAFT_NAME","",line,&flag);
if (flag > 0 && (strncmp(line,"MAGELLAN",8) == 0))
/* switch line and sample values */
*p_nsamp = 1024; *p_nline = 896;
}

GetLabelString(buf,len,"SOURCE_FILE_NAME","",
Source_File_Name,&flag);
GetLabelInteger(buf,len,"SAMPLING_FACTOR",1,&Sampling_Factor,&flag);

GetLabelString(buf,len,"DATA_SET_ID","",line,&flag);
if (flag > 0)
{
if (strncmp(line,"MGN-V-RDRS-5-MIDR-FULL-RES",26) == 0 &&
strncmp(DetachedFileName,"BROWSE.IMG",10) == 0)
strcpy(Source_File_Name,"FFXX.LBL");
else if (strncmp(line,"MGN-V-RDRS-5-MIDR-C1",20) == 0 &&
strncmp(DetachedFileName,"BROWSE.IMG",10) == 0)
strcpy(Source_File_Name,"C1FXX.LBL");
else if (strncmp(line,"MGN-V-RDRS-5-MIDR-C2",20) == 0 &&
strncmp(DetachedFileName,"BROWSE.IMG",10) == 0)
strcpy(Source_File_Name,"C2FXX.LBL");
else if (strncmp(line,"MGN-V-RDRS-5-MIDR-C3",20) == 0 &&
strncmp(DetachedFileName,"BROWSE.IMG",10) == 0)
strcpy(Source_File_Name,"C3FXX.LBL");
else if (strncmp(line,"VO1/VO2-M-VIS-5-DIM",19) == 0)
{
GetLabelString(buf,len,"IMAGE_ID","",
Source_File_Name,&flag);
strcpy(line,"\\");
strncat(line,Source_File_Name,5);
line[2] = 'I';
strcat(line,"XXX");
strcat(line,"\\");
strncat(line,Source_File_Name,8);
line[11] = 'I';
strcat(line,".IMG");
strcpy(Source_File_Name,line);
Sampling_Factor = 4;
}
}
/* Find bitsperpix */
bitspixdef = (8L* (*p_reclen)) / *p_nsamp;
if (bitspixdef <= 1)
bitspixdef = 1;
else
if (bitspixdef <= 4)
bitspixdef = 4;

/* removed next line, AEE 4-21-89
else
bitspixdef = 8;
*/
GetLabelInteger (buf, len, "SAMPLE_BITS", bitspixdef,
p_bitsperpix, &flag);
if (flag < 1)
GetLabelInteger(buf,len,"COLUMN_BITS",bitspixdef,p_bitsperpix,&flag);

GetLabelString(buf,len,"COLUMN_NAME"," ",colname,&flag);
if (strncmp(colname,"(WAVELENGTH",11)==0
|| strncmp(colname,"(POSITION",9)==0)
{
switched= 0;
ByteSwap= TRUE;
}
else
{
switched= 1;
ByteSwap= FALSE;
}

GetLabelInteger (buf, len, "LINE_PREFIX_BYTES", 0,
p_lineheadbytes, &flag);
GetLabelInteger (buf, len, "LINE_SUFFIX_BYTES", 0,
p_linetrailbytes, &flag);
if (p_lineheadbytes != 0 || p_linetrailbytes != 0)
*p_reclen = (((long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1)
+(long)*p_lineheadbytes + (long)*p_linetrailbytes;

/* Check for non-byte swapped sample value */
GetLabelString (buf, len, "SAMPLE_TYPE", "LSB_INTEGER",
SampleType, &flag);
if (flag < 1)
GetLabelString(buf,len,"COLUMN_TYPE","LSB_INTEGER",SampleType,&flag);
if (flag > 0)
{
if (strncmp(SampleType,"MSB_INTEGER",11) == 0) ByteSwap = TRUE;
if (strncmp(SampleType,"UNSIGNED_INTEGER",16) == 0) ByteSwap = TRUE;
if (strncmp(SampleType,"VAX_INTEGER",11) == 0) ByteSwap = FALSE;
if (strncmp(SampleType,"LSB_INTEGER",11) == 0) ByteSwap = FALSE;
}

/* Get plot information for X-axis */

GetLabelReal( buf, len, "START_WAVELENGTH", 0.0, &xstart, &flag);
GetLabelReal( buf, len, "WAVELENGTH_INTERVAL", 1.0, &xinterval, &flag);
GetLabelString( buf, len, "WAVELENGTH_UNIT", "PIXEL", xunit, &flag);

/*
GetLabelReal(buf,len,"START_",1.0,&xstart,&flag);
if(flag >= 1)
{
sprintf(xlabel,"%.12s",strstr(buf,"START_")+6);
strtok(xlabel," ");

strcpy(intervaltoken,xlabel);
strcat(intervaltoken,"_INTERVAL");

strcpy(unittoken,xlabel);
strcat(unittoken,"_UNIT");
GetLabelReal(buf,len,intervaltoken,1.0,&xinterval,&flag);
GetLabelString(buf,len,unittoken,"",xunit,&flag);
}
*/
/* Find maxlat and lon */
GetLabelReal(buf, len, "MAXIMUM_LATITUDE", 0.0, &maxlat, &flag);
GetLabelReal(buf, len, "MINIMUM_LATITUDE", 0.0, &minlat, &flag);
GetLabelReal(buf, len, "MAXIMUM_LONGITUDE", 0.0, &maxlon, &flag);
GetLabelReal(buf, len, "MINIMUM_LONGITUDE", 0.0, &minlon, &flag);
flag = 0;
if (strlen(DetachedFileName) == 0)
GetLabelString (buf, len, "IMAGE_POINTER", "",
DetachedFileName, &flag);
if (flag > 0)
{
GiantLabel = (long)headerrecs*(long)headerreclen;
if (GiantLabel < 65535L)
*p_labelsize = (unsigned int)GiantLabel;
else
*p_labelsize = 0;
}
if (strlen(DetachedPaletteName) == 0)
GetLabelString(buf, len, "PALETTE_POINTER", "",
DetachedPaletteName, &flag);

free(p_linetrailbytes);
PDSused= 1;
return(0);
}

/* This part for VICAR2 labels */
GetLabelString (buf, len, "TYPE", " ", FileType, &flag);
if ( stricmp (FileType, "IMAGE") == 0 )
{
DoVicarLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
p_reclen, p_lineheadbytes, p_linetrailbytes);
return(0);
}

/* This part for FITS labels */
GetLabelString (buf, len, "SIMPLE", " ", FileType, &flag);
if ( (stricmp (FileType, "T") == 0) ||
(stricmp (FileType, "F") == 0) ) /* this is a FITS file */
{
DoFITSLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
p_reclen, p_lineheadbytes, p_linetrailbytes, unit);

PDSused= 0;
return(0);
}

/* Do this if no recognized label */
DoNoLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
p_reclen, p_lineheadbytes, FileType, p_linetrailbytes);
}



void DoVicarLabel (char * buf, int len, unsigned int * p_labelsize,
int * p_nline, int * p_nsamp, int * p_bitsperpix,
int * p_reclen, int * p_lineheadbytes, int * p_linetrailbytes)

/*** DoVicarLabel interprets the Vicar label in the buffer to extract the

image information.

Parameter Type In/out Description
buf char ptr in The label buffer
len int in The length in bytes of the label buffer
p_labelsize uns int ptr out The size of the label or header to skip
p_nline int ptr out The number of lines in the image
p_nsamp int ptr out The number of samples in the image
p_bitsperpix int ptr out The pixel format (16,8,4,1)
p_reclen int ptr out The record length in bytes
p_lineheadbytes int ptr out The header bytes at beginning of each line
p_linetrailbytes int ptr in The number of bytes at the end of each line

***/
{
char format[16];
int flag;

GetLabelInteger (buf, len, "LBLSIZE", 0, p_labelsize, &flag);
GetLabelInteger (buf, len, "NL", 1, p_nline, &flag);
GetLabelInteger (buf, len, "NS", 1, p_nsamp, &flag);
GetLabelString (buf, len, "FORMAT", "BYTE", format, &flag);
if (format[0] == 'H')
*p_bitsperpix = 16;
else
*p_bitsperpix = 8;

*p_reclen = ( (long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1;
GetLabelInteger (buf, len, "RECSIZE", *p_reclen, p_reclen, &flag);
*p_lineheadbytes = 0;

free(p_linetrailbytes);
}


void DoFITSLabel (char * buf, int len, unsigned int * p_labelsize, int * p_nline,
int * p_nsamp, int * p_bitsperpix, int * p_reclen,
int * p_lineheadbytes, int * p_linetrailbytes, int unit)

/*** DoFITSLabel interprets the FITS label in the buffer to extract the
image information.

Parameter Type In/out Description
buf char ptr in The label buffer
len int in The length in bytes of the label buffer
p_labelsize uns int ptr out The size of the label or header to skip
p_nline int ptr out The number of lines in the image
p_nsamp int ptr out The number of samples in the image
p_bitsperpix int ptr out The pixel format (16,8,4,1)
p_reclen int ptr out The record length in bytes
p_lineheadbytes int ptr out The header bytes at beginning of each line
p_linetrailbytes int ptr in The number of bytes at the end of each line
unit int in The number to the current file being
processed

***/
{

int flag, naxis, bytecount, i;
char line[81];
char axis_tmp[8];
int n_axis_tmp;

ByteSwap = TRUE;
GetLabelInteger (buf, len, "NAXIS",0, &naxis, &flag);
if (naxis == 1)
{
GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
*p_nline = 1;
}
else if (naxis == 2)
{
GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
GetLabelInteger (buf, len, "NAXIS2", 1, p_nline, &flag);
GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
}
else if (naxis == 0)
{
*p_nline = 0;
*p_nsamp = 0;
*p_bitsperpix = 16;
/* ClearDisplay(0); Commented out - AW3 */
StatusLine(0,"No lines in image.");
}
else /* naxis greater than 2 */
{
/* Loop over NAXISn w/ n>2 to see if any are greater than 1 */
for (i=3; i<=naxis; i++)
{
sprintf (axis_tmp, "NAXIS%d", i);
GetLabelInteger (buf, len, axis_tmp, 1, &n_axis_tmp, &flag);
if (n_axis_tmp != 1)
{
*p_nline = 0;
*p_nsamp = 0;
*p_bitsperpix = 16;
/* ClearDisplay(0); Commented out - AW3 */
StatusLine(0,"Too many axes.");
}
else /* Treat like a 2-D image */
{
GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
GetLabelInteger (buf, len, "NAXIS2", 1, p_nline, &flag);
GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
}
}
}

*p_reclen = ((long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1;
*p_lineheadbytes = 0;

/** added by DIN /revised - mdm/ to read END in FITS header **/

bytecount = 0;
i = 0; /* changed from i= 1 to i= 0 to handle END when it is in the
last line of label. AEE, 4-4-89 */

/* labels = fdopen(FCB[unit].handle,"r"); */
lseek(FCB[unit].handle,0L,SEEK_SET);
do
{
read(FCB[unit].handle,line,80);
++i;
} while (strnicmp(line,"END",3) != 0);

bytecount = i*80;
i = bytecount/2880;
if (bytecount % 2880 != 0) i++;
*p_labelsize = i*2880;
free(p_linetrailbytes);
}


void DoNoLabel(char * buf, int len, unsigned int * p_labelsize,
int * p_nline, int * p_nsamp, int * p_bitsperpix,
int * p_reclen, int * p_lineheadbytes, char * FileType,
int * p_linetrailbytes)

/*** DoNoLabel prompts the user to enter the image parameters.
PDS-format compressed images are handled (skipped) here, too.

Parameter Type In/out Description
buf char ptr in The label buffer
len int in The length in bytes of the label buffer
p_labelsize uns int ptr out The size of the label or header to skip
p_nline int ptr out The number of lines in the image
p_nsamp int ptr out The number of samples in the image
p_bitsperpix int ptr out The pixel format (16,8,4,1)
p_reclen int ptr out The record length in bytes
p_lineheadbytes int ptr out The header bytes at beginning of each line
FileType char ptr in The type of the file of data (from FILE_TYPE)
p_linetrailbytes int ptr in The number of bytes at the end of each line

***/
{

int flag;
char inpstr[32];

*p_bitsperpix = 8;
*p_labelsize = 0;

if (OneScreen)
{
ClearDisplay(0);

TextLine = TextHeight + 5; TextSample = 1;
TypeText ("Image file does not have a proper label.");

TextLine += TextHeight + 5; TextSample = 1;
TypeText ("Input number of lines : ");
AcceptText (inpstr);
sscanf (inpstr, "%d", p_nline);

TextLine += TextHeight + 5; TextSample = 1;
TypeText ("Input number of samples : ");
AcceptText (inpstr);
sscanf (inpstr, "%d", p_nsamp);

TextLine += TextHeight + 5; TextSample = 1;
TypeText ("Input bits per pixel : ");
AcceptText (inpstr);
sscanf (inpstr, "%d", p_bitsperpix);

TextLine += TextHeight + 5; TextSample = 1;
TypeText ("Input number of header bytes : ");
AcceptText (inpstr);
sscanf (inpstr, "%d", p_labelsize);

ClearDisplay(0);
}
else
{
printf ("%s", "Image file does not have a proper label.\n");
printf ("%s", "Input number of lines : ");
gets (inpstr);
sscanf (inpstr, "%d", p_nline);

printf ("%s", "Input number of samples : ");
gets (inpstr);
sscanf (inpstr, "%d", p_nsamp);

printf ("%s", "Input bits per pixel : ");
gets (inpstr);
sscanf (inpstr, "%d", p_bitsperpix);

printf ("%s", "Input number of header bytes : ");
gets (inpstr);
sscanf (inpstr, "%d", p_labelsize);
}
*p_reclen = ( (long)*p_nsamp*(long)*p_bitsperpix - 1 )/8 + 1;
*p_lineheadbytes = 0;

free(p_linetrailbytes);
}

int PutLabel (char * LabelBuf, int * p_pos, char * LabelString)

{
int i;

for (i = 0; LabelString[i] > 0; i++)
LabelBuf[(*p_pos)++] = LabelString[i];
LabelBuf[(*p_pos)++] = 13;
LabelBuf[(*p_pos)++] = 10;
}


int PutLabelValue (char * LabelBuf, int * p_pos, char * LabelString, int value)

{
char temp1[128], temp2[32];

strcpy (temp1, LabelString);
sprintf (temp2, "%d", value);
strcat (temp1, temp2);
PutLabel (LabelBuf, p_pos, temp1);
}





int MakeLabel (char * Buf, int * p_labelsize, int nline, int nsamp,
int bitsperpix, int reclen)

/*** MakeLabel creates a PDS label in the buffer, having the
appropriate image parameters.

Parameter Type In/out Description
Buf char ptr out The label buffer
p_labelsize int ptr out The size of the label
nline int out The number of lines in the image
nsamp int out The number of samples in the image
bitsperpix int out The pixel format (16,8,4,1)
reclen int out The record length in bytes
***/

{
int j, labelrecs, totalrecs;
long int filesize;
char labstr[128];


labelrecs = (2047/reclen) + 1;
*p_labelsize = reclen*labelrecs;
totalrecs = nline + labelrecs;
filesize = (long)reclen*(long)totalrecs - 20;

j = 0;

sprintf (labstr, "NJPL1I00PDS1%08ld = SFDU_LABEL",
filesize);
PutLabel (Buf, &j, labstr);
PutLabel (Buf, &j, "RECORD_TYPE = FIXED_LENGTH");
PutLabelValue (Buf, &j, "RECORD_BYTES = ", reclen);
PutLabelValue (Buf, &j, "FILE_RECORDS = ", totalrecs);
PutLabelValue (Buf, &j, "LABEL_RECORDS = ", labelrecs);
PutLabelValue (Buf, &j, "^IMAGE = ", labelrecs+1);
PutLabel (Buf, &j, "OBJECT = IMAGE");
PutLabelValue (Buf, &j, " LINES = ", nline);
PutLabelValue (Buf, &j, " LINE_SAMPLES = ", nsamp);
PutLabelValue (Buf, &j, " SAMPLE_BITS = ", bitsperpix);
PutLabel (Buf, &j, "END_OBJECT");
PutLabel (Buf, &j, "END");

while (j < *p_labelsize)
Buf[j++] = ' ';
}


int GetCommand (char * CommandString, char * CommandList)

/*** GetCommand is an integer function that returns the number of the
command that is in the beginning of the CommandString parameter.
The command is assigned a number according to the list of commands
in the CommandList string parameter. The list of commands should
be in uppercase and have spaces between the commmand.

***/

{
int i, j, k, l, n;
char List[NCOMMANDS][35], Word[35], temp[35]; /* Max # of commands */

/* Parse the command list string up into a list of strings */
n = 0;
l = strlen(CommandList) - 1;
k = 0;
do
{
j = k;
while ( (CommandList[j] == ' ') && (j < l) )
j++;
if (j == l) break;
k = j;
do
k++;
while ( (CommandList[k] != ' ') && (k<=l) );
strncpy (List[n], &CommandList[j], k-j);
List[n][k-j] = 0;
n++;
} while (k <= l);


/* Get the first word from the command string */
l = strlen(CommandString);
j = 0;
while ( (CommandString[j] == ' ') && (j < l) )
j++;
k = j;
do
k++;
while ( (CommandString[k] != ' ') && (k<=l) );
strncpy (Word, &CommandString[j], k-j);
Word[k-j] = 0;

/* Search through the list of commands to find a match */
i = n-1;
do
{
strcpy (temp, Word);
temp[strlen(List[i])] = 0;
} while ( (stricmp(temp, List[i]) != 0) && (--i >= 0) );

return (i+1);
}



  3 Responses to “Category : C Source Code
Archive   : IMDSRC78.ZIP
Filename : LABUTIL.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/