00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <ctype.h>
00013 #include <math.h>
00014 #include <time.h>
00015 #ifndef __sgi
00016 #include <stdlib.h>
00017 #endif
00018
00019
00020
00021
00022 #include "nb_header.h"
00023
00024
00025
00026
00027 #define STRING 0
00028 #define INTEGER 1
00029 #define FLOAT 2
00030
00031
00032
00033
00034 extern int InputType();
00035 extern void WrongType();
00036 extern void RemoveBlanks();
00037
00038
00039
00040
00041 MARNB_HEADER GetmarNBHeader(FILE *);
00042 MARNB_HEADER SetmarNBHeader(void);
00043 int PutmarNBHeader(int , MARNB_HEADER);
00044
00045
00046
00047
00048 MARNB_HEADER
00049 GetmarNBHeader(FILE *fp)
00050 {
00051 MARNB_HEADER h;
00052 int i,j, itmp, size,fpos, ntok=0;
00053 char str[64], buf[128], *key, *token[20];
00054
00055
00056
00057
00058
00059 h = SetmarNBHeader();
00060
00061
00062 if( fp == NULL ) return( h );
00063
00064 fseek( fp, 0, SEEK_SET );
00065
00066 if ( fread( &h.byteorder, sizeof(int), 1, fp ) < 1 ) {
00067 return( h );
00068 }
00069 fgets( buf, 60, fp );
00070 fpos = 64;
00071
00072
00073
00074
00075 fseek( fp, 192, SEEK_SET );
00076 fpos = 192;
00077
00078
00079
00080
00081
00082 while( fgets(buf,64,fp)!= NULL){
00083
00084
00085 fpos += 64;
00086 fseek( fp, fpos, SEEK_SET );
00087
00088
00089 if(strstr(buf,"END OF HEADER") )
00090 break;
00091 else if(strstr(buf,"REMARK") )
00092 continue;
00093
00094
00095
00096 if ( strlen(buf) < 2 ) continue;
00097
00098
00099 if( buf[0] == '#' || buf[0]=='!' ) continue;
00100
00101
00102
00103 ntok = -1;
00104
00105 for(i=0;i<64;i++) {
00106
00107 if( buf[i] == '\t') buf[i] = ' ';
00108 if( buf[i] == '\f') buf[i] = ' ';
00109 if( buf[i] == '\n') buf[i] = '\0';
00110 if( buf[i] == '\r') buf[i] = '\0';
00111 if( buf[i] == '\0') break;
00112 }
00113
00114 for(i=0;i<strlen(buf);i++) {
00115 if( buf[i] == ' ' ) continue;
00116 ntok++;
00117 for (j=i;j<strlen(buf);j++)
00118 if( buf[j] == ' ') break;
00119 i=j;
00120 }
00121 if (strlen(buf) < 3 ) continue;
00122
00123 key = strtok( buf, " ");
00124
00125
00126 for ( i=0; i<strlen(key); i++ )
00127 if ( isalnum( key[i] ) ) key[i] = toupper( key[i] );
00128
00129 for(i=0;i<ntok;i++) {
00130 token[i] = strtok( NULL, " ");
00131 strcpy( str, token[i] );
00132
00133 for ( j=0; j<strlen( str ); j++ )
00134 if ( isalnum( str[j] ) ) str[j] = toupper( str[j] );
00135 strcpy( token[i] , str );
00136 RemoveBlanks( token[i] );
00137 }
00138
00139
00140 if(!strncmp(key,"PROG",4) && ntok >= 2 ) {
00141 strcpy( h.program, token[0] );
00142 strcpy( h.version, token[2] );
00143 }
00144
00145
00146 else if(!strncmp(key,"SCAL",4) && ntok >= 0 )
00147 if ( InputType( token[0] ) >= INTEGER )
00148 h.scale = atof( token[0] );
00149 else
00150 WrongType( FLOAT, key, token[0] );
00151
00152
00153 else if(!strncmp(key,"PHI",3) && ntok >= 0 )
00154 if ( InputType( token[0] ) >= INTEGER )
00155 h.phioff = atof( token[0] );
00156 else
00157 WrongType( FLOAT, key, token[0] );
00158
00159
00160 else if(!strncmp(key,"CUT",3) && ntok >= 0 )
00161 if ( InputType( token[0] ) >= INTEGER )
00162 h.cutoff = atof( token[0] );
00163 else
00164 WrongType( FLOAT, key, token[0] );
00165
00166
00167 else if(!strncmp(key,"GAIN",4) && ntok >= 0 )
00168 if ( InputType( token[0] ) >= INTEGER )
00169 h.gain = atof( token[0] );
00170 else
00171 WrongType( FLOAT, key, token[0] );
00172
00173 else if(!strncmp(key,"PIXE",4) && ntok >= 0 ) {
00174 for(i=0;i<ntok;i++) {
00175 if ( strstr( token[i], "LEN" ) ) {
00176 i++;
00177 if ( InputType( token[i] ) >= INTEGER ) {
00178 h.pixel_length = atof( token[i] );
00179 }
00180 else
00181 WrongType( FLOAT, key, token[i] );
00182 }
00183 else if ( strstr( token[i], "HEI" ) ) {
00184 i++;
00185 if ( InputType( token[i] ) >= INTEGER ) {
00186 h.pixel_height= atof( token[i] );
00187 }
00188 else
00189 WrongType( FLOAT, key, token[i] );
00190 }
00191 else if ( strstr( token[i], "SUB" ) ) {
00192 i++;
00193 if ( InputType( token[i] ) == INTEGER ) {
00194 h.subpixels = atoi( token[i] );
00195 }
00196 else
00197 WrongType( INTEGER, key, token[i] );
00198 }
00199 }
00200 }
00201
00202
00203
00204 else if(!strncmp(key,"SCAN",4) && ntok >= 0 )
00205 if ( InputType( token[0] ) == INTEGER )
00206 h.scanner = atoi( token[0] );
00207 else
00208 WrongType( INTEGER, key, token[0] );
00209
00210
00211 else if(!strncmp(key,"DATE",4) && ntok >= 0 ) {
00212 for ( i=0; i<strlen( buf ); i++ )
00213 if ( buf[i] == ' ' ) break;
00214 for ( j=i; j<strlen( buf ); j++ )
00215 if ( buf[j] != ' ' ) break;
00216 strcpy( h.date, buf+j );
00217 }
00218
00219
00220 else if(!strncmp(key,"MODE",4) && ntok >= 0 ) {
00221 if ( InputType( token[0] ) != INTEGER )
00222 WrongType( INTEGER, key, token[0] );
00223 else {
00224 size = atoi( token[0] );
00225
00226 for ( j=0, i=0; i<MAX_NBMODE; i++ ) {
00227 if ( h.size[i] == 0 && h.mode <= MAX_NBMODE ) {
00228 h.size[h.mode] = size;
00229 j = h.mode;
00230 h.mode++;
00231 break;
00232 }
00233 else if ( h.size[i] == size ) {
00234 j = i;
00235 break;
00236 }
00237 }
00238
00239 for ( i=1; i<ntok; i++ ) {
00240 if ( strstr( token[i], "POS" ) ) {
00241 i++;
00242 if ( InputType( token[i] ) == INTEGER )
00243 h.fpos[j] = atoi( token[i] );
00244 else
00245 WrongType( INTEGER, key, token[i] );
00246 }
00247 else if ( strstr( token[i], "PIX" ) ) {
00248 i++;
00249 if ( InputType( token[i] ) == INTEGER )
00250 h.pixels[j] = atoi( token[i] );
00251 else
00252 WrongType( INTEGER, key, token[i] );
00253 }
00254 else if ( strstr( token[i], "X" ) ) {
00255 i++;
00256 if ( InputType( token[i] ) == INTEGER )
00257 h.x[j] = atoi( token[i] );
00258 else
00259 WrongType( INTEGER, key, token[i] );
00260 }
00261 else if ( strstr( token[i], "Y" ) ) {
00262 i++;
00263 if ( InputType( token[i] ) == INTEGER )
00264 h.y[j] = atoi( token[i] );
00265 else
00266 WrongType( INTEGER, key, token[i] );
00267 }
00268 else if ( strstr( token[i], "SK" ) ) {
00269 i++;
00270 if ( InputType( token[i] ) == INTEGER )
00271 h.skip[j] = atoi( token[i] );
00272 else
00273 WrongType( INTEGER, key, token[i] );
00274 }
00275 else if ( strstr( token[i], "RO" ) ) {
00276 i++;
00277 if ( InputType( token[i] ) == INTEGER )
00278 h.roff[j] = atoi( token[i] );
00279 else
00280 WrongType( INTEGER, key, token[i] );
00281 }
00282 }
00283 }
00284 }
00285
00286
00287 else if(!strncmp(key,"NUMB",3) && ntok >= 1 ) {
00288 for(i=0;i<ntok;i++) {
00289 if ( strstr( token[i], "PIX" ) ) {
00290 i++;
00291 if ( InputType( token[i] ) == INTEGER ) {
00292 h.tot_pixels = atoi( token[i] );
00293 }
00294 else
00295 WrongType( INTEGER, key, token[i] );
00296 }
00297 else if ( strstr( token[i], "NEI" ) ) {
00298 i++;
00299 if ( InputType( token[i] ) == INTEGER ) {
00300 h.nbs = atoi( token[i] );
00301 }
00302 else
00303 WrongType( INTEGER, key, token[i] );
00304 }
00305 }
00306
00307 }
00308
00309 }
00310
00311
00312
00313
00314
00315 fseek( fp, 4096, SEEK_SET );
00316
00317 return( h );
00318
00319 }
00320
00321
00322
00323
00324 MARNB_HEADER
00325 SetmarNBHeader()
00326 {
00327 MARNB_HEADER h;
00328 int i;
00329
00330 h.byteorder = 1234;
00331 h.pixel_length = 150.0;
00332 h.pixel_height = 150.0;
00333 h.subpixels = 0;
00334 h.scanner = 25;
00335 h.scale = 65535.0;
00336 h.phioff = 0.0;
00337 h.cutoff = 0.0;
00338 h.gain = 1.0;
00339
00340 h.mode = 0;
00341 h.tot_pixels = 0;
00342 h.nbs = 0;
00343
00344 h.date[0] = '\0';
00345 h.program[0] = '\0';
00346 h.version[0] = '\0';
00347
00348 for ( i=0; i<MAX_NBMODE; i++ ) {
00349 h.x[i] = 0;
00350 h.y[i] = 0;
00351 h.size[i] = 0;
00352 h.fpos[i] = 0;
00353 h.pixels[i] = 0;
00354 h.roff[i] = 0;
00355 h.skip[i] = 0;
00356 }
00357
00358 return( h );
00359 }
00360
00361
00362
00363
00364 int
00365 PutmarNBHeader(int fd, MARNB_HEADER h)
00366 {
00367 int i,j,k=0;
00368 int io;
00369 int byte_order=1234;
00370 time_t clock;
00371 char str[128];
00372 char c=' ';
00373
00374 if ( fd < 0 )return(0);
00375
00376 time( &clock );
00377 sprintf( h.date, "%s", (char *)ctime( &clock ));
00378
00379 lseek( fd, 0, SEEK_SET );
00380
00381
00382 io = write( fd, &byte_order, sizeof(int));
00383
00384 while( lseek( fd, 0, SEEK_CUR ) < 128) {
00385 i = write( fd, &c, 1);
00386 if ( i< 1) break;
00387 io += i;
00388 }
00389
00390 lseek( fd, 128, SEEK_SET );
00391
00392 sprintf( str, "mar research NEIGHBOUR CODE");
00393 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00394 io += write( fd, str, 64); k++;
00395
00396 sprintf( str, "PROGRAM %s Version %s\n",h.program,h.version);
00397 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00398 io += write( fd, str, 64); k++;
00399
00400 sprintf( str, "DATE %s",h.date);
00401 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00402 io += write( fd, str, 64); k++;
00403
00404 sprintf( str, "SCANNER %d\n",h.scanner);
00405 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00406 io += write( fd, str, 64); k++;
00407
00408 sprintf( str, "PIXEL LENGTH %1.0f HEIGHT %1.0f SUBPIXELS %d\n",h.pixel_length,h.pixel_height,h.subpixels);
00409 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00410 io += write( fd, str, 64); k++;
00411
00412 sprintf( str, "PHIOFF %1.3f\n",h.phioff);
00413 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00414 io += write( fd, str, 64); k++;
00415
00416 sprintf( str, "CUTOFF %1.3f\n",h.cutoff);
00417 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00418 io += write( fd, str, 64); k++;
00419
00420 sprintf( str, "SCALE %1.3f\n",h.scale);
00421 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00422 io += write( fd, str, 64); k++;
00423
00424 sprintf( str, "GAIN %1.3f\n",h.gain);
00425 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00426 io += write( fd, str, 64); k++;
00427
00428 sprintf( str, "NUMBER PIXELS %d NEIGHBOURS %d\n",h.tot_pixels,h.nbs);
00429 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00430 io += write( fd, str, 64); k++;
00431
00432 for ( j=0; j<MAX_NBMODE; j++ ) {
00433
00434
00435
00436 if ( h.size[j] == 0 ) continue;
00437
00438 sprintf( str, "MODE %4d X %4d Y %4d ROFF %4d SKIP %-d\n",h.size[j],h.x[j],h.y[j],h.roff[j],h.skip[j]);
00439 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00440 io += write( fd, str, 64); k++;
00441 }
00442
00443 for ( j=0; j<MAX_NBMODE; j++ ) {
00444 if ( h.size[j] == 0 ) continue;
00445 sprintf( str, "MODE %4d PIX %-8d POS %-d\n",h.size[j],h.pixels[j],h.fpos[j]);
00446 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00447 io += write( fd, str, 64); k++;
00448 }
00449
00450 sprintf( str, "END OF HEADER\n");
00451 for ( i=strlen(str); i<63; i++ ) str[i]=' '; str[63]='\n';
00452 io += write( fd, str, 64); k++;
00453
00454
00455
00456 while( lseek( fd, 0, SEEK_CUR ) < 4096) {
00457 i = write( fd, &c, 1);
00458 if ( i< 1) break;
00459 io += i;
00460 }
00461
00462 if ( io<4096 ) {
00463 fprintf( stderr, "ERROR: only %d out of 4096 written into nb header!!!\n",io);
00464 return 0;
00465 }
00466
00467 return 1;
00468
00469 }