File: libics_read.c

package info (click to toggle)
libics 1.5.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,280 kB
  • sloc: sh: 8,735; ansic: 5,994; makefile: 125
file content (605 lines) | stat: -rw-r--r-- 19,901 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
 * libics: Image Cytometry Standard file reading and writing.
 *
 * Copyright (C) 2000-2010 Cris Luengo and others
 * email: clluengo@users.sourceforge.net
 *
 * Large chunks of this library written by
 *    Bert Gijsbers
 *    Dr. Hans T.M. van der Voort
 * And also Damir Sudar, Geert van Kempen, Jan Jitze Krol,
 * Chiel Baarslag and Fons Laan.
 *
 * 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.
 */
/*
 * FILE : libics_read.c
 *
 * The following library functions are contained in this file:
 *
 *   IcsReadIcs()
 *   IcsVersion()
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libics_intern.h"

/*
 * Find the index for "bits", which should be the first parameter...
 */
static int IcsGetBitsParam (char Order[ICS_MAXDIM+1][ICS_STRLEN_TOKEN], int Parameters)
{
   int ii;

   for (ii = 0; ii < Parameters; ii++) {
      if (strcmp (Order[ii], ICS_ORDER_BITS) == 0) {
         return ii;
      }
   }

   return -1;
}

/*
 * Like fgets(), gets a string from a stream. However, does not stop at
 * newline character, but at 'sep'. It retains the 'sep' character at the end
 * of the string; a null byte is appended.
 * Also, it implements the solution to the CR/LF pair problem caused by
 * some windows applications. If 'sep' is LF, it might be prepended by a CR.
 */
static char* IcsFGetStr (char* line, int n, FILE* fi, char sep)
{
   /* n == ICS_LINE_LENGTH */
   int ii = 0;
   int ch;

   /* Imitate fgets() */
   while (ii < n-1) {
      ch = getc (fi);
      if (ch == EOF)
         break;

      /* Skip CR if next char is LF and sep is LF. */
      if (ch == '\r' && sep == '\n') {
         ch = getc (fi);
         if (ch != sep && ch != EOF) {
            ungetc (ch, fi);
            ch = '\r';
         }
      }

      line[ii] = (char)ch;
      ii++;
      if ((char)ch == sep)
         break;
   }
   line[ii] = '\0';
   if (ii != 0) {
      /* Read at least a 'sep' character */
      return line;
   }
   else
      /* EOF at first getc() call */
      return NULL;
}

/*
 * Read the two ICS separators from file. There is a special case for ICS
 * headers which are erroneously written under Windows in text mode causing
 * a newline separator to be prepended by a carriage return.  Therefore when
 * the second separator is a carriage return and the first separator is not
 * a newline then peek at the third character to see if it is a newline.  If
 * so then use newline as the second separator. Return IcsErr_FReadIcs on
 * read errors and IcsErr_NotIcsFile on premature end-of-file.
 */
static Ics_Error GetIcsSeparators (FILE* fi, char* seps)
{
   int sep1;
   int sep2;
   int sep3;

   sep1 = fgetc (fi);
   if (sep1 == EOF) {
      return (ferror (fi)) ? IcsErr_FReadIcs : IcsErr_NotIcsFile;
   }
   sep2 = fgetc (fi);
   if (sep2 == EOF) {
      return (ferror (fi)) ? IcsErr_FReadIcs : IcsErr_NotIcsFile;
   }
   if (sep1 == sep2) {
      return IcsErr_NotIcsFile;
   }
   if (sep2 == '\r' && sep1 != '\n') {
      sep3 = fgetc (fi);
      if (sep3 == EOF) {
         return (ferror (fi)) ? IcsErr_FReadIcs : IcsErr_NotIcsFile;
      } else {
         if (sep3 == '\n') {
            sep2 = '\n';
         } else {
            ungetc (sep3, fi);
         }
      }
   }
   seps[0] = sep1;
   seps[1] = sep2;
   seps[2] = '\0';

   return IcsErr_Ok;
}

static Ics_Error GetIcsVersion (FILE* fi, char const* seps, int* ver)
{
   ICSINIT;
   char* word;
   char line[ICS_LINE_LENGTH];

   ICSTR( IcsFGetStr (line, ICS_LINE_LENGTH, fi, seps[1]) == NULL, IcsErr_FReadIcs );
   word = strtok (line, seps);
   ICSTR( word == NULL, IcsErr_NotIcsFile );
   ICSTR( strcmp (word, ICS_VERSION) != 0, IcsErr_NotIcsFile );
   word = strtok (NULL , seps);
   ICSTR( word == NULL, IcsErr_NotIcsFile );
   if (strcmp (word, "1.0") == 0) {
      *ver = 1;
   }
   else if (strcmp (word, "2.0") == 0) {
      *ver = 2;
   }
   else {
      error = IcsErr_NotIcsFile;
   }

   return error;
}

static Ics_Error GetIcsFileName (FILE* fi, char const* seps)
{
   ICSINIT;
   char* word;
   char line[ICS_LINE_LENGTH];

   ICSTR( IcsFGetStr (line, ICS_LINE_LENGTH, fi, seps[1]) == NULL, IcsErr_FReadIcs );
   word = strtok (line, seps);
   ICSTR( word == NULL, IcsErr_NotIcsFile );
   ICSTR( strcmp (word, ICS_FILENAME) != 0, IcsErr_NotIcsFile );
   /* The rest of the line is discarded */

   return error;
}

static Ics_Token GetIcsToken (char* str, Ics_SymbolList* ListSpec)
{
   int ii;
   Ics_Token token = ICSTOK_NONE;

   if (str != NULL) {
      for (ii = 0; ii < ListSpec->Entries; ii++) {
         if (strcmp (ListSpec->List[ii].Name, str) == 0) {
            token = ListSpec->List[ii].Token;
         }
      }
   }

   return token;
}

static Ics_Error GetIcsCat (char* str, char const* seps, Ics_Token* Cat,
                            Ics_Token* SubCat, Ics_Token* SubSubCat)
{
   ICSINIT;
   char* token, buffer[ICS_LINE_LENGTH];
   *SubCat = *SubSubCat = ICSTOK_NONE;

   IcsStrCpy (buffer, str, ICS_LINE_LENGTH);
   token = strtok (buffer, seps);
   *Cat = GetIcsToken (token, &G_Categories);
   ICSTR( *Cat == ICSTOK_NONE, IcsErr_MissCat );
   if ((*Cat != ICSTOK_HISTORY) && (*Cat != ICSTOK_END)) {
      token = strtok (NULL, seps);
      *SubCat = GetIcsToken (token, &G_SubCategories);
      ICSTR( *SubCat == ICSTOK_NONE, IcsErr_MissSubCat );
      if (*SubCat == ICSTOK_SPARAMS) {
         token = strtok (NULL, seps);
         *SubSubCat = GetIcsToken (token, &G_SubSubCategories);
         ICSTR( *SubSubCat == ICSTOK_NONE , IcsErr_MissSensorSubSubCat );
      }
   }

   /* Copy the remaining stuff into 'str' */
   if ((token = strtok (NULL, seps)) != NULL) {
      strcpy (str, token);
   }
   while ((token = strtok (NULL, seps)) != NULL) {
      IcsAppendChar (str, seps[0]);
      strcat (str, token);
   }

   return error;
}

Ics_Error IcsReadIcs (Ics_Header* IcsStruct, char const* filename, int forcename, int forcelocale)
{
   ICSDECL;
   ICS_INIT_LOCALE;
   FILE* fp;
   int end = 0, ii, jj, bits;
   char seps[3], *ptr, *data;
   char line[ICS_LINE_LENGTH];
   Ics_Token cat, subCat, subSubCat;
   /* These are temporary buffers to hold the data read until it is
    * copied to the Ics_Header structure. This is needed because the
    * Ics_Header structure is made to look more like we like to see
    * images, compared to the way the data is written in the ICS file.
    */
   Ics_Format Format = IcsForm_unknown;
   int Sign = 1;
   int Parameters = 0;
   char Order[ICS_MAXDIM+1][ICS_STRLEN_TOKEN];
   size_t Sizes[ICS_MAXDIM+1];
   double Origin[ICS_MAXDIM+1];
   double Scale[ICS_MAXDIM+1];
   char Label[ICS_MAXDIM+1][ICS_STRLEN_TOKEN];
   char Unit[ICS_MAXDIM+1][ICS_STRLEN_TOKEN];
   for (ii = 0; ii < ICS_MAXDIM+1; ii++)
   {
      Sizes[ii] = 1;
      Origin[ii] = 0.0;
      Scale[ii] = 1.0;
      Order[ii][0] = '\0';
      Label[ii][0] = '\0';
      Unit[ii][0] = '\0';
   }

   IcsInit (IcsStruct);
   IcsStruct->FileMode = IcsFileMode_read;

   IcsStrCpy (IcsStruct->Filename, filename, ICS_MAXPATHLEN);
   ICSXR( IcsOpenIcs (&fp, IcsStruct->Filename, forcename) );

   if (forcelocale) {
      ICS_SET_LOCALE;
   }

   ICSCX( GetIcsSeparators (fp, seps) );

   ICSCX( GetIcsVersion (fp, seps, &(IcsStruct->Version)) );
   ICSCX( GetIcsFileName (fp, seps) );

   while (!end && !error && (IcsFGetStr (line, ICS_LINE_LENGTH, fp, seps[1]) != NULL)) {
      if (GetIcsCat (line, seps, &cat, &subCat, &subSubCat) != IcsErr_Ok)
         continue;
      ptr = strtok (line, seps);
      ii = 0;
      switch (cat) {
         case ICSTOK_END:
            end = 1;
            if (IcsStruct->SrcFile[0] == '\0') {
               IcsStruct->SrcOffset = (size_t) ftell (fp);
               IcsStrCpy (IcsStruct->SrcFile, IcsStruct->Filename, ICS_MAXPATHLEN);
            }
            break;
         case ICSTOK_SOURCE:
            switch (subCat) {
               case ICSTOK_FILE:
                  if (ptr != NULL) {
                     IcsStrCpy (IcsStruct->SrcFile, ptr, ICS_MAXPATHLEN);
                  }
                  break;
               case ICSTOK_OFFSET:
                  if (ptr != NULL) {
                     IcsStruct->SrcOffset = IcsStrToSize (ptr);
                  }
                  break;
               default:
                  break;
            }
            break;
         case ICSTOK_LAYOUT:
            switch (subCat) {
               case ICSTOK_PARAMS:
                  if (ptr != NULL) {
                     Parameters = atoi (ptr);
                     if (Parameters > ICS_MAXDIM+1) {
                        error = IcsErr_TooManyDims;
                     }
                  }
                  break;
               case ICSTOK_ORDER:
                  while (ptr!= NULL && ii < ICS_MAXDIM+1) {
                     IcsStrCpy (Order[ii++], ptr, ICS_STRLEN_TOKEN);
                     ptr = strtok (NULL, seps);
                  }
                  break;
               case ICSTOK_SIZES:
                  while (ptr!= NULL && ii < ICS_MAXDIM+1) {
                     Sizes[ii++] = IcsStrToSize (ptr);
                     ptr = strtok (NULL, seps);
                  }
                  break;
               case ICSTOK_COORD:
                  if (ptr != NULL) {
                     IcsStrCpy (IcsStruct->Coord, ptr, ICS_STRLEN_TOKEN);
                  }
                  break;
               case ICSTOK_SIGBIT:
                  if (ptr != NULL) {
                     IcsStruct->Imel.SigBits = IcsStrToSize (ptr);
                  }
                  break;
               default:
                  error = IcsErr_MissLayoutSubCat;
            }
            break;
         case ICSTOK_REPRES:
            switch (subCat) {
               case ICSTOK_FORMAT:
                  switch (GetIcsToken (ptr, &G_Values)) {
                     case ICSTOK_FORMAT_INTEGER:
                        Format = IcsForm_integer;
                        break;
                     case ICSTOK_FORMAT_REAL:
                        Format = IcsForm_real;
                        break;
                     case ICSTOK_FORMAT_COMPLEX:
                        Format = IcsForm_complex;
                        break;
                     default:
                        Format = IcsForm_unknown;
                  }
                  break;
               case ICSTOK_SIGN:
                  if (GetIcsToken (ptr, &G_Values) == ICSTOK_SIGN_UNSIGNED) {
                     Sign = 0;
                  }
                  else {
                     Sign = 1;
                  }
                  break;
               case ICSTOK_SCILT:
                  if (ptr!= NULL) {
                     IcsStrCpy (IcsStruct->ScilType, ptr, ICS_STRLEN_TOKEN);
                  }
                  break;
               case ICSTOK_COMPR:
                  switch (GetIcsToken (ptr, &G_Values)) {
                     case ICSTOK_COMPR_UNCOMPRESSED:
                        IcsStruct->Compression = IcsCompr_uncompressed;
                        break;
                     case ICSTOK_COMPR_COMPRESS:
                        if (IcsStruct->Version==1) {
                           IcsStruct->Compression = IcsCompr_compress;
                        }
                        else { /* A version 2.0 file never uses COMPRESS, maybe it means GZIP? */
                           IcsStruct->Compression = IcsCompr_gzip;
                        }
                        break;
                     case ICSTOK_COMPR_GZIP:
                        IcsStruct->Compression = IcsCompr_gzip;
                        break;
                     default:
                        error = IcsErr_UnknownCompression;
                  }
                  break;
               case ICSTOK_BYTEO:
                  while (ptr!= NULL && ii < ICS_MAX_IMEL_SIZE) {
                     IcsStruct->ByteOrder[ii++] = atoi (ptr);
                     ptr = strtok (NULL, seps);
                  }
                  break;
               default:
                  error = IcsErr_MissRepresSubCat;
               break;
            }
            break;
         case ICSTOK_PARAM:
            switch (subCat) {
               case ICSTOK_ORIGIN:
                  while (ptr!= NULL && ii < ICS_MAXDIM+1) {
                     Origin[ii++] = atof (ptr);
                     ptr = strtok (NULL, seps);
                  }
                  break;
               case ICSTOK_SCALE:
                  while (ptr!= NULL && ii < ICS_MAXDIM+1) {
                     Scale[ii++] = atof (ptr);
                     ptr = strtok (NULL, seps);
                  }
                  break;
               case ICSTOK_UNITS:
                  while (ptr!= NULL && ii < ICS_MAXDIM+1) {
                     IcsStrCpy (Unit[ii++], ptr, ICS_STRLEN_TOKEN);
                     ptr = strtok (NULL, seps);
                  }
                  break;
               case ICSTOK_LABELS:
                  while (ptr!= NULL && ii < ICS_MAXDIM+1) {
                     IcsStrCpy (Label[ii++], ptr, ICS_STRLEN_TOKEN);
                     ptr = strtok (NULL, seps);
                  }
                  break;
               default:
                  error = IcsErr_MissParamSubCat;
            }
            break;
         case ICSTOK_HISTORY:
            if (ptr != NULL) {
               data = strtok (NULL, seps+1); /* This will get the rest of the line */
               if (data == NULL) { /* data is not allowed to be "", but ptr is */
                  data = ptr;
                  ptr = "";
               }
               /* The next portion is to avoid having IcsInternAddHistory
                * return IcsErr_LineOverflow. */
               ii = strlen (ptr);
               if (ii+1 > ICS_STRLEN_TOKEN) {
                  ptr[ICS_STRLEN_TOKEN-1] = '\0';
                  ii = ICS_STRLEN_TOKEN-1;
               }
               jj = strlen (ICS_HISTORY);
               if ((strlen (data) + ii + jj + 4) > ICS_LINE_LENGTH) {
                  data[ICS_LINE_LENGTH - ii - jj - 4] = '\0';
               }
               error = IcsInternAddHistory (IcsStruct, ptr, data, seps);
            }
            break;
         case ICSTOK_SENSOR:
            switch (subCat) {
               case ICSTOK_TYPE:
                  if (ptr != NULL) {
                     IcsStrCpy (IcsStruct->Type, ptr, ICS_STRLEN_TOKEN);
                  }
                  break;
               case ICSTOK_MODEL:
                  if (ptr != NULL) {
                     IcsStrCpy (IcsStruct->Model, ptr, ICS_STRLEN_OTHER);
                  }
                  break;
               case ICSTOK_SPARAMS:
                  switch (subSubCat) {
                     case ICSTOK_CHANS:
                        if (ptr != NULL) {
                           IcsStruct->SensorChannels = atoi (ptr);
                           if (IcsStruct->SensorChannels > ICS_MAX_LAMBDA) {
                              error = IcsErr_TooManyChans;
                           }
                        }
                        break;
                     case ICSTOK_PINHRAD:
                        while (ptr != NULL && ii < ICS_MAX_LAMBDA) {
                           IcsStruct->PinholeRadius[ii++] = atof (ptr);
                           ptr = strtok (NULL, seps);
                        }
                        break;
                     case ICSTOK_LAMBDEX:
                        while (ptr != NULL && ii < ICS_MAX_LAMBDA) {
                           IcsStruct->LambdaEx[ii++] = atof (ptr);
                           ptr = strtok (NULL, seps);
                        }
                        break;
                     case ICSTOK_LAMBDEM:
                        while (ptr != NULL && ii < ICS_MAX_LAMBDA) {
                           IcsStruct->LambdaEm[ii++] = atof (ptr);
                           ptr = strtok (NULL, seps);
                        }
                        break;
                     case ICSTOK_PHOTCNT:
                        while (ptr != NULL && ii < ICS_MAX_LAMBDA) {
                           IcsStruct->ExPhotonCnt[ii++] = atoi (ptr);
                           ptr = strtok(NULL, seps);
                        }
                        break;
                     case ICSTOK_REFRIME:
                        if (ptr != NULL) {
                           IcsStruct->RefrInxMedium = atof (ptr);
                        }
                        break;
                     case ICSTOK_NUMAPER:
                        if (ptr != NULL) {
                           IcsStruct->NumAperture = atof (ptr);
                        }
                        break;
                     case ICSTOK_REFRILM:
                        if (ptr != NULL) {
                           IcsStruct->RefrInxLensMedium = atof (ptr);
                        }
                        break;
                     case ICSTOK_PINHSPA:
                        if (ptr != NULL) {
                           IcsStruct->PinholeSpacing = atof (ptr);
                        }
                        break;
                     default:
                        error = IcsErr_MissSensorSubSubCat;
                  }
                  break;
               default:
                  error = IcsErr_MissSensorSubCat;
            }
            break;
         default:
            error = IcsErr_MissCat;
      }
   }

   if (!error) {
      bits = IcsGetBitsParam (Order, Parameters);
      if (bits < 0) {
         error = IcsErr_MissBits;
      }
      else {
         IcsGetDataTypeProps (&(IcsStruct->Imel.DataType), Format, Sign, Sizes[bits]);
         for (jj = 0, ii = 0; ii < Parameters; ii++) {
            if (ii == bits) {
               IcsStruct->Imel.Origin = Origin[ii];
               IcsStruct->Imel.Scale = Scale[ii];
               strcpy (IcsStruct->Imel.Unit, Unit[ii]);
            }
            else {
               IcsStruct->Dim[jj].Size = Sizes[ii];
               IcsStruct->Dim[jj].Origin = Origin[ii];
               IcsStruct->Dim[jj].Scale = Scale[ii];
               strcpy (IcsStruct->Dim[jj].Order, Order[ii]);
               strcpy (IcsStruct->Dim[jj].Label, Label[ii]);
               strcpy (IcsStruct->Dim[jj].Unit, Unit[ii]);
               jj++;
            }
         }
         IcsStruct->Dimensions = Parameters - 1;
      }
   }

   if (forcelocale) {
      ICS_REVERT_LOCALE;
   }

   if (fclose (fp) == EOF) {
      ICSCX (IcsErr_FCloseIcs); /* Don't overwrite any previous error. */
   }
   return error;
}

/*
 * Read the first 3 lines of an ICS file to see which version it is.
 * It returns 0 if it is not an ICS file, or the version number if it is.
 */
int IcsVersion (char const* filename, int forcename)
{
   ICSDECL;
   ICS_INIT_LOCALE;
   int version;
   FILE* fp;
   char FileName[ICS_MAXPATHLEN];
   char seps[3];

   IcsStrCpy (FileName, filename, ICS_MAXPATHLEN);
   error = IcsOpenIcs (&fp, FileName, forcename);
   ICSTR( error, 0 );
   version = 0;
   ICS_SET_LOCALE;
   if (!error) error = GetIcsSeparators (fp, seps);
   if (!error) error = GetIcsVersion (fp, seps, &version);
   if (!error) error = GetIcsFileName (fp, seps);
   ICS_REVERT_LOCALE;
   if (fclose (fp) == EOF) {
      return 0;
   }
   if (error)
      return 0;
   else
      return version;
}