openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
IGBheader.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // openCARP is an open cardiac electrophysiology simulator.
3 //
4 // Copyright (C) 2020 openCARP project
5 //
6 // This program is licensed under the openCARP Academic Public License (APL)
7 // v1.0: You can use and redistribute it and/or modify it in non-commercial
8 // academic environments under the terms of APL as published by the openCARP
9 // project v1.0, or (at your option) any later version. Commercial use requires
10 // a commercial license (info@opencarp.org).
11 //
12 // This program is distributed without any warranty; see the openCARP APL for
13 // more details.
14 //
15 // You should have received a copy of the openCARP APL along with this program
16 // and can find it online: http://www.opencarp.org/license
17 // ----------------------------------------------------------------------------
18 
27 #include "IGBheader.h"
28 
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <iostream>
32 #include <assert.h>
33 #include <math.h>
34 
35 namespace opencarp {
36 
37 using namespace std;
38 
39 // -------------- Bits de statut pour Header_Read et Header_Write ------
40 #define MOT_CLEF_INV 2
41 #define GRANDEUR_INV 4
42 
43 /*
44  * Types de trames
45  */
46 #define MIN_TRAME 0
47 #define C8 0
48 #define C4 1
49 #define HEX 2
50 #define HEXEDGES 3
51 #define HEXBRIDGES 4
52 #define HEXLINES 5
53 #define HEX2 6
54 #define MAX_TRAME 6
55 #define NTRAMES 7
56 
57 #define LF 0x0A
58 #define FF 0x0C
59 #define CR 0x0D
60 
61 /*---------------------------------------------------------------------------*\
62 
63  y x-> 1 2 3 4 5
64  |
65  v
66  X|X|X|X|X|X
67  1 -o-o-o-o-o-
68  X|X|X|X|X|X
69  2 -o-o-o-o-o-
70  \|/ X|X|X|X|X|X
71 C8: -o- 3 -o-o-o-o-o-
72  /|\ X|X|X|X|X|X
73  4 -o-o-o-o-o-
74  X|X|X|X|X|X
75  5 -o-o-o-o-o-
76  X|X|X|X|X|X
77 
78 \*---------------------------------------------------------------------------*/
79 /*---------------------------------------------------------------------------*\
80 
81  y x-> 1 2 3 4 5
82  |
83  v
84  | | | | |
85  1 -o-o-o-o-o-
86  | | | | |
87  2 -o-o-o-o-o-
88  | | | | | |
89 C4: -o- 3 -o-o-o-o-o-
90  | | | | | |
91  4 -o-o-o-o-o-
92  | | | | |
93  5 -o-o-o-o-o-
94  | | | | |
95 
96 \*---------------------------------------------------------------------------*/
97 /*---------------------------------------------------------------------------*\
98 
99  y x-> 1 2 3 4 5 1 2 3 4 5
100  |
101  v 1 2 3 4 5
102 
103  \|\|\|\|\| \ / \ / \ / \ / \ /
104  1 -o-o-o-o-o- -o---o---o---o---o---
105  \| /|/|/|/|/|/ / \ / \ / \ / \ / \ /
106  y impair -o- 2 -o-o-o-o-o- ---o---o---o---o---o-
107  /| \|\|\|\|\|\ \ / \ / \ / \ / \ / \
108 HEX: 3 -o-o-o-o-o- -o---o---o---o---o---
109  |/ /|/|/|/|/|/ / \ / \ / \ / \ / \ /
110  y pair -o- 4 -o-o-o-o-o- ---o---o---o---o---o-
111  |\ \|\|\|\|\|\ \ / \ / \ / \ / \ / \
112  5 -o-o-o-o-o- -o---o---o---o---o---
113  /|/|/|/|/| / \ / \ / \ / \ / \
114 
115 \*---------------------------------------------------------------------------*/
116 /*---------------------------------------------------------------------------*\
117 
118  x x-> 1 2 3 4 5 1 2 3 4 5 6
119 
120  impair pair 1 2 3 4 5
121 
122  y 1 2 3 4 5
123  |
124  \| v 1 2 3 4 5
125  y%4==1 -o- -o-
126  /| \| \| \| \ / . \ / . \ / .
127  1 -o-o-o-o-o- -o---o---o---o---o---o
128  | / /| /| /| / \ . / \ . / \ .
129  y%4==2 o o 2 o o o o o o o o o o
130  | / |/ |/ |/ . \ / . \ / . \
131 HEXEDGES: 3 -o-o-o-o-o- -o---o---o---o---o---o
132  |/ |\ |\ |\ . / \ . / \ . /
133  y%4==3 -o- -o- 4 o o o o o o o o o o
134  |\ \| \| \| \ / . \ / . \ / .
135  5 -o-o-o-o-o- -o---o---o---o---o---o
136  | \ /| /| /| / \ . / \ . / \ .
137  y%4==0 o o 6 o o o o o o o o o o
138  | \ |/ |/ | . \ / . \ / . \
139 
140 \*---------------------------------------------------------------------------*/
141 /*---------------------------------------------------------------------------*\
142 
143  x x-> 1 2 3 4 5 1 2 3 4 5 6
144 
145 
146  impair pair 1 2 3 4 5
147 
148 
149  y 1 2 3 4 5
150  |
151  v
152  y%4==1 o -o- 1 2 3 4 5
153  . . .
154  1 o -o- o -o- o o -o- o -o- o -o
155  . . .
156  | / | / | / | \ . . / \ . . / \ .
157  y%4==2 o o 2 o o o o o o o o o o
158  | / | / | / | . \ / . . \ / . . \
159  . . .
160 HEXBRIDGES: 3 o -o- o -o- o -o- o -o- o -o- o
161  . . .
162  | \ | \ | . / \ . . / \ . . /
163  y%4==3 o -o- 4 o o o o o o o o o o
164  | \ | \ | / . . \ / . . \ / .
165  . . .
166  5 o -o- o -o- o o -o- o -o- o -o
167  . . .
168  | \ | / | / | \ . . / \ . . / \ .
169  y%4==0 o o 6 o o o o o o o o o o
170  | \ | / | / | . \ / . . \ / . . \
171 
172 \*---------------------------------------------------------------------------*/
173 /*---------------------------------------------------------------------------*\
174 
175  x x-> 1 2 3 4 5 1 2 3 4 5 6
176 
177  impair pair 1 2 3 4 5
178 
179  y 1 2 3 4 5
180  |
181  \| v 1 2 3 4 5
182  y%4==1 o o
183  /| \| \| \./ \./ \./
184  1 o o o o o o o o o o o
185  / | /| /| / /.\ /.\ /.\
186  y%4==2 -o- -o- 2 -o-o-o-o-o- ---o---o---o---o---o---
187  / | / |/ |/ \./ \./ \./
188 HEXLINES: 3 o o o o o o o o o o o
189  |/ |\ |\ /.\ /.\ /.\
190  y%4==3 o o 4 -o-o-o-o-o- ---o---o---o---o---o---
191  |\ \| \| \./ \./ \./
192  5 o o o o oo o o o o o o
193  \ | /| /| / /.\ /.\ /.\
194  y%4==0 -o- -o- 6 -o-o-o-o-o- ---o---o---o---o---o---
195  \ | / |/ |/ / \./ \./
196 
197 \*---------------------------------------------------------------------------*/
198 /*---------------------------------------------------------------------------*\
199 
200  x-> 1 2 3 4 5 1 2 3 4 5 6
201 
202  y 1 2 3 4 5
203  |
204  v 1 2 3 4 5
205 
206  \| 1 2 3 4 5
207  y%4==1 -o-
208  /| \|\|\|\|\| \ / \ / \ / \ / \ /
209  1 -o-o-o-o-o- -o---o---o---o---o---o
210  |/ /|/|/|/|/| / \ / \ / \ / \ / \ /
211  y%4==2 -o- 2 -o-o-o-o-o- ---o---o---o---o---o-
212  /| /|/|/|/|/|/ \ / \ / \ / \ / \ / \
213 HEX2: 3 -o-o-o-o-o- -o---o---o---o---o---o
214  |/ \|\|\|\|\| / \ / \ / \ / \ / \ /
215  y%4==3 -o- 4 -o-o-o-o-o- ---o---o---o---o---o-
216  |\ \|\|\|\|\| \ / \ / \ / \ / \ / \
217  5 -o-o-o-o-o- -o---o---o---o---o---o
218  \| /|/|/|/|/| / \ / \ / \ / \ / \ /
219  y%4==0 -o- 6 -o-o-o-o-o- ---o---o---o---o---o-
220  |\ /|/|/|/|/| / \ / \ / \ / \ / \
221 
222 \*---------------------------------------------------------------------------*/
223 
224 // -------------- Definitions pour conv_date --------------------------- */
225 
226 #define FRANCAIS 0
227 #define ANGLAIS 1
228 #define NUMERIQUE 2
229 #define IGB_DATE 0
230 
231 // -------------- Constantes diverses ---------------------------------- */
232 
233 #define MAXL 80 // Longueur maximale d'une ligne d'entete */
234 #define N_MAX_ITEMS 30 // Nombre maximal d'items optionnels */
235 #define L_MAX_ITEM 49 // Longueur maximale pour un item
236 #ifndef VRAI
237 #define VRAI 1
238 #endif
239 #ifndef FAUX
240 #define FAUX 0
241 #endif
242 
243 
244 int my_fputs( FILE *, char * );
245 
246 bool Header_Quiet = false;
247 char Header_Message[256];
248 
249 const char *Header_Type[] =
250  {
251  "", "byte", "char", "short", "long", "float", "double", "complex",
252  "double_complex", "rgba", "structure", "pointer", "list","int","uint",
253  "ushort",
254  "vec3f","vec3d","vec4f","vec4d","hfloat","vec9f","vec9d"
255  };
256 
257 
259 const char *deprecated[] = {
260  "fac_x", "fac_y", "fac_z", "fac_t"
261  };
262 
263 //* size of the stored data, not the variable type
264 unsigned short Data_Size[] =
265  {
266  0, sizeof(Byte), sizeof(char), sizeof(short), sizeof(long), sizeof(float),
267  sizeof(double), 0, 0, 0, 0, sizeof(void *), 0, sizeof(int), sizeof(UInt),
268  sizeof(unsigned short),
269  3*sizeof(float), 3*sizeof(double), 4*sizeof(float), 4*sizeof(double),
270  sizeof(short_float), 9*sizeof(float), 9*sizeof(double)
271  };
272 
273 
276  {
277  0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 3, 3, 4, 4, 1, 9, 9
278  };
279 
280 
281 long unsigned
283  {
286  };
287 
288 const char
290  {
291  "big_endian",
292  "little_endian"
293  };
294 
295 
296 static bool
297 is_deprecated( char *s ) {
298  for( unsigned int i=0; i<sizeof(deprecated)/sizeof(deprecated[0]); i++ )
299  if( !strcmp( s, deprecated[i] ) )
300  return true;
301  return false;
302 }
303 
304 
305 #define MAKE_CONSISTENT( D ) \
306  if( bool_dim_##D && bool_inc_##D && bool_##D ) \
307  if( v_dim_##D != v_inc_##D * (v_##D-1) ) { \
308  fprintf( stderr, "Adjusting dim_%s to make dimensions consistent\n", #D ); \
309  v_dim_##D = v_inc_##D * (v_##D-1);\
310  }
311 
312 
313 IGBheader::IGBheader( FILE *f, bool _read, bool quiet )
314 {
315  fileptr(f);
316  if( _read )
317  if( read( quiet ) )
318  throw 1;
319 }
320 
321 
323 {}
324 
325 
326 // set input/output file
327 void IGBheader::fileptr( FILE* fp )
328 {
329  file = fp;
330 }
331 
332 
334 {
335  int statut = 1;
336 
337  if (getenv("HEADER_QUIET")) Header_Quiet = VRAI;
338 
339  if (file==NULL) {
340  if (!Header_Quiet)
341  cerr<< "\nERREUR: descripteur de fichier nul\n";
342  snprintf(Header_Message, sizeof Header_Message, "\nERREUR: descripteur de fichier nul\n");
343  return(0) ;
344  }
345 
346  if (v_type<IGB_MIN_TYPE || v_type>IGB_MAX_TYPE) {
347  if (!Header_Quiet)
348  cerr<< "\nHeader_Write: type inconnu: "<< v_type;
349  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: type inconnu: %d\n",
350  v_type);
351  return (0);
352  }
353  const char *type = Header_Type[v_type];
354 
355  if (v_type==IGB_STRUCTURE && v_taille<1) {
356  if (!Header_Quiet)
357  cerr << "\nHeader_Write: taille invalide:" << v_taille << endl;
358  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: taille invalide: %d\n",
359  v_taille);
360  return (0);
361  }
362 
363  if (v_trame<MIN_TRAME || v_trame>MAX_TRAME) {
364  if (!Header_Quiet)
365  fprintf(stderr, "\nHeader_Write: trame inconnue: %d\n", v_trame);
366  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: trame inconnue: %d\n",
367  v_trame);
368  return (0);
369  }
370 
371  MAKE_CONSISTENT( x );
372  MAKE_CONSISTENT( y );
373  MAKE_CONSISTENT( z );
374  MAKE_CONSISTENT( t );
375 
376  char ligne[1024];
377  if (bool_t) {
378  if (bool_z) {
379  snprintf(ligne, sizeof ligne, "x:%d y:%d z:%d t:%d type:%s systeme:%s ",
380  v_x, v_y, v_z, v_t, type, systemestr());
381  } else {
382  snprintf(ligne, sizeof ligne, "x:%d y:%d t:%d type:%s systeme:%s ",
383  v_x, v_y, v_t, type, systemestr());
384  }
385  } else {
386  if (bool_z) {
387  snprintf(ligne, sizeof ligne, "x:%d y:%d z:%d type:%s systeme:%s ",
388  v_x, v_y, v_z, type, systemestr());
389  } else {
390  snprintf(ligne, sizeof ligne, "x:%d y:%d type:%s systeme:%s ",
391  v_x, v_y, type, systemestr());
392  }
393  }
394  int n_car = strlen(ligne);
395 
396  int n_lignes = 1;
397  int n_items = 0;
398  int l_item[N_MAX_ITEMS+1];
399  char items[N_MAX_ITEMS+1][L_MAX_ITEM];
400  /*
401  Le mot-clef "taille" n'est ecrit que pour le type STRUCTURE mais il est
402  obligatoire pour ce cas.
403  */
404  if (v_type==IGB_STRUCTURE) {
405  snprintf(&items[n_items][0], L_MAX_ITEM, "taille:%d ", v_taille);
406  l_item[n_items] = strlen(&items[n_items][0]);
407  n_items++;
408  }
409  if (bool_org_x) {
410  snprintf(&items[n_items][0], L_MAX_ITEM, "org_x:%g ", v_org_x);
411  l_item[n_items] = strlen(&items[n_items][0]);
412  n_items++;
413  }
414  if (bool_org_y) {
415  snprintf(&items[n_items][0], L_MAX_ITEM, "org_y:%g ", v_org_y);
416  l_item[n_items] = strlen(&items[n_items][0]);
417  n_items++;
418  }
419  if (bool_org_z) {
420  snprintf(&items[n_items][0], L_MAX_ITEM, "org_z:%g ", v_org_z);
421  l_item[n_items] = strlen(&items[n_items][0]);
422  n_items++;
423  }
424  if (bool_org_t) {
425  snprintf(&items[n_items][0], L_MAX_ITEM, "org_t:%g ", v_org_t);
426  l_item[n_items] = strlen(&items[n_items][0]);
427  n_items++;
428  }
429  if (bool_dim_x) {
430  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_x:%g ", v_dim_x);
431  l_item[n_items] = strlen(&items[n_items][0]);
432  n_items++;
433  }
434  if (bool_inc_x) {
435  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_x:%g ", v_inc_x);
436  l_item[n_items] = strlen(&items[n_items][0]);
437  n_items++;
438  }
439  if (bool_dim_y) {
440  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_y:%g ", v_dim_y);
441  l_item[n_items] = strlen(&items[n_items][0]);
442  n_items++;
443  }
444  if (bool_inc_y) {
445  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_y:%g ", v_inc_y);
446  l_item[n_items] = strlen(&items[n_items][0]);
447  n_items++;
448  }
449  if (bool_dim_z) {
450  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_z:%g ", v_dim_z);
451  l_item[n_items] = strlen(&items[n_items][0]);
452  n_items++;
453  }
454  if (bool_inc_z) {
455  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_z:%g ", v_inc_z);
456  l_item[n_items] = strlen(&items[n_items][0]);
457  n_items++;
458  }
459  if (bool_dim_t) {
460  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_t:%g ", v_dim_t);
461  l_item[n_items] = strlen(&items[n_items][0]);
462  n_items++;
463  }
464  if (bool_inc_t) {
465  snprintf(&items[n_items][0], L_MAX_ITEM, "inc_t:%g ", v_inc_t);
466  l_item[n_items] = strlen(&items[n_items][0]);
467  n_items++;
468  }
469  if (bool_vect_z) {
470  snprintf(&items[n_items][0], L_MAX_ITEM, "vect_z:1 ");
471  l_item[n_items] = strlen(&items[n_items][0]);
472  n_items++;
473  }
474  if (bool_unites_x) {
475  snprintf(&items[n_items][0], L_MAX_ITEM, "unites_x:%.40s ", v_unites_x);
476  l_item[n_items] = strlen(&items[n_items][0]);
477  n_items++;
478  }
479  if (bool_unites_y) {
480  snprintf(&items[n_items][0], L_MAX_ITEM, "unites_y:%.40s ", v_unites_y);
481  l_item[n_items] = strlen(&items[n_items][0]);
482  n_items++;
483  }
484  if (bool_unites_z) {
485  snprintf(&items[n_items][0], L_MAX_ITEM, "unites_z:%.40s ", v_unites_z);
486  l_item[n_items] = strlen(&items[n_items][0]);
487  n_items++;
488  }
489  if (bool_unites_t) {
490  snprintf(&items[n_items][0], L_MAX_ITEM, "unites_t:%.40s ", v_unites_t);
491  l_item[n_items] = strlen(&items[n_items][0]);
492  n_items++;
493  }
494  if (bool_num) {
495  snprintf(&items[n_items][0], L_MAX_ITEM, "num:%d ", v_num);
496  l_item[n_items] = strlen(&items[n_items][0]);
497  n_items++;
498  }
499  if (bool_bin) {
500  snprintf(&items[n_items][0], L_MAX_ITEM, "bin:%d ", v_bin);
501  l_item[n_items] = strlen(&items[n_items][0]);
502  n_items++;
503  }
504  if (bool_trame) {
505  switch (v_trame) {
506  case C8:
507  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:c8 ");
508  break;
509  case C4:
510  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:c4 ");
511  break;
512  case HEX:
513  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hex ");
514  break;
515  case HEXEDGES:
516  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hexedges ");
517  break;
518  case HEXBRIDGES:
519  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hexbridges ");
520  break;
521  case HEXLINES:
522  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hexlines ");
523  break;
524  case HEX2:
525  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hex2 ");
526  break;
527  }
528  l_item[n_items] = strlen(&items[n_items][0]);
529  n_items++;
530  }
531  if (bool_lut) {
532  snprintf(&items[n_items][0], L_MAX_ITEM, "lut:%d ", v_lut);
533  l_item[n_items] = strlen(&items[n_items][0]);
534  n_items++;
535  }
536  if (bool_comp) {
537  snprintf(&items[n_items][0], L_MAX_ITEM, "comp:%d ", v_comp);
538  l_item[n_items] = strlen(&items[n_items][0]);
539  n_items++;
540  }
541  if (bool_epais) {
542  snprintf(&items[n_items][0], L_MAX_ITEM, "epais:%g ", v_epais);
543  l_item[n_items] = strlen(&items[n_items][0]);
544  n_items++;
545  }
546  if (bool_unites) {
547  snprintf(&items[n_items][0], L_MAX_ITEM, "unites:%.40s ", v_unites);
548  l_item[n_items] = strlen(&items[n_items][0]);
549  n_items++;
550  }
551  if (bool_facteur) {
552  snprintf(&items[n_items][0], L_MAX_ITEM, "facteur:%g ", v_facteur);
553  l_item[n_items] = strlen(&items[n_items][0]);
554  n_items++;
555  }
556  if (bool_zero) {
557  snprintf(&items[n_items][0], L_MAX_ITEM, "zero:%g ", v_zero);
558  l_item[n_items] = strlen(&items[n_items][0]);
559  n_items++;
560  }
561  if ( v_transparent != NULL ) {
562  char *p=(char *)v_transparent, value[MAXL];
563  int a;
564  for ( a=0; a<Data_Size[v_type]; a++ )
565  snprintf( value+a*2, L_MAX_ITEM, "%0.2x", *(p++) );
566  value[2*Data_Size[v_type]] = '\0';
567  snprintf(&items[n_items][0], L_MAX_ITEM, "transparent:%s ", value );
568  l_item[n_items] = strlen(&items[n_items][0]);
569  n_items++;
570  }
571  if (bool_struct_desc) {
572  snprintf(&items[n_items][0], L_MAX_ITEM, "struct:%.40s ", v_struct_desc);
573  l_item[n_items] = strlen(&items[n_items][0]);
574  n_items++;
575  }
576  if (bool_aut_name) {
577  snprintf(&items[n_items][0], L_MAX_ITEM, "aut:%.40s ", v_aut_name);
578  l_item[n_items] = strlen(&items[n_items][0]);
579  n_items++;
580  }
581  int n_car_total = 0;
582  int n_comment = 0;
583  char **comment;
584  if (bool_comment) {
585  comment = v_comment;
586  while (*comment != NULL) {
587  n_comment++;
588  n_car_total += strlen(*comment++) + 3;
589  }
590  }
591 
592  /*
593  Ecrit tous les items, sauf les commentaires
594  */
595  for (int i=0;i<n_items;i++) {
596  if (n_car+l_item[i]<71) { /* Ajoute a la ligne courante s'il reste de la place */
597  strcat(ligne, &items[i][0]);
598  n_car += l_item[i];
599  } else { /* Sinon, ecrit cette ligne et commence-en une autre */
600  ligne[n_car++] = '\r';
601  ligne[n_car++] = '\n';
602  ligne[n_car] = '\000';
603  n_car_total += n_car;
604  if (puts_fcn(file, ligne)==-1) {
605  if (!Header_Quiet) {
606  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
607  perror("\n *** ");
608  fprintf(stderr, "\n");
609  }
610  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
611  return (0);
612  }
613  strcpy(ligne, &items[i][0]);
614  n_car = l_item[i];
615  n_lignes++;
616  }
617  }
618 
619  /*
620  Termine la derniere ligne
621  */
622  ligne[n_car++] = '\r';
623  ligne[n_car++] = '\n';
624  ligne[n_car] = '\000';
625  n_car_total += n_car;
626  if (puts_fcn(file, ligne )==-1) {
627  if (!Header_Quiet) {
628  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
629  perror("\n *** ");
630  fprintf(stderr, "\n");
631  }
632  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
633  return (0);
634  }
635  n_lignes++;
636 
637  /*
638  Determine le nombre de caracteres et de lignes supplementaires
639  necessaires
640  */
641  int n_blocs = 1 + (n_car_total-1)/1024;
642  int n_car_sup = n_blocs*1024 - n_car_total;
643  int n_lig_sup;
644  if (n_car_sup>0) {
645  n_lig_sup = 1 + (n_car_sup-1)/72;
646  } else {
647  n_lig_sup = 0;
648  }
649  int n_car_dl = 1 + (n_car_sup-1)%72;
650 
651  /*
652  Ecrit les commentaires
653  */
654  if (bool_comment) {
655  comment = v_comment;
656  while (*comment != NULL) {
657  n_car = fprintf(file, "#%.80s\r\n", *comment++);
658  if (n_car==0) {
659  if (!Header_Quiet) {
660  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
661  perror("\n *** ");
662  fprintf(stderr, "\n");
663  }
664  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
665  return (0);
666  }
667  }
668  }
669 
670  /*
671  Complete l'entete a un multiple de 1024 caracteres
672  */
673  for (int i=0;i<70;i++) ligne[i] = ' ';
674  ligne[70] = '\r';
675  ligne[71] = '\n';
676  ligne[72] = '\000';
677  for (int i=0;i<n_lig_sup-1;i++) {
678  if (puts_fcn(file, ligne)==-1) {
679  if (!Header_Quiet) {
680  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
681  perror("\n *** ");
682  fprintf(stderr, "\n");
683  }
684  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
685  return 0;
686  }
687  }
688 
689  /*
690  La derniere ligne se termine par un saut de page (FF)
691  */
692  for (int i=0;i<n_car_dl-2;i++) ligne[i] = ' ';
693  if (n_car_dl>2) ligne[n_car_dl-3] = '\r';
694  if (n_car_dl>1) ligne[n_car_dl-2] = '\n';
695  ligne[n_car_dl-1] = FF;
696  ligne[n_car_dl] = '\000';
697  if (puts_fcn(file, ligne )==-1) {
698  if (!Header_Quiet) {
699  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
700  perror("\n *** ");
701  fprintf(stderr, "\n");
702  }
703  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
704  return (0);
705  }
706 
707  if (n_car_total>1024) {
708  if (!Header_Quiet)
709  fprintf(stderr,
710  "\nHeader_Write ATTENTION: etiquette de grandeur non-standard \n");
711  snprintf(Header_Message, sizeof Header_Message,
712  "\nHeader_Write ATTENTION: etiquette de grandeur non-standard \n");
713  statut |= GRANDEUR_INV;
714  } else {
715  snprintf(Header_Message, sizeof Header_Message,
716  "\nHeader_Write: Entete transcrite sans problemes\n");
717  }
718 
719  return (statut);
720 
721 }
722 
723 
732 int IGBheader::read( bool quiet )
733 {
734  int go=VRAI, nosup=VRAI;
735  int in, com=0;
736  char str[MAXL+1];
737  int statut=0;
738  float v_fac_t;
739  bool bool_fac_t = false;
740 
741  Header_Quiet = quiet;
742 
743  /* --- pour toutes les lignes de l'entete (def=8) ou jusqu'a un <FF> -- */
744  for ( int s=8; (s>0 || nosup) && go; s-- ) {
745 
746  /* --- lit la ligne dans le fichier --- */
747  int i = 0 ;
748  int bytes_read = tell(file);
749  if( bytes_read<0 )
750  return 2;
751 
752  while (1) {
753 
754  /* --- (ligne trop longue) --> erreur --- */
755  if ( i >= 80 ) {
756  if (!Header_Quiet)
757  fprintf(stderr,
758  "\nERREUR ligne trop longue (>80) dans l'entete !\n" ) ;
759  snprintf(Header_Message, sizeof Header_Message,
760  "\nERREUR ligne trop longue (>80) dans l'entete !\n" ) ;
761  return ERR_LINE_TOO_LONG;
762  }
763 
764  /* --- lit le caractere suivant --- */
765  in = getc(file);
766 
767  /* --- (EOF dans l'entete) --> erreur --- */
768  if ( in == -1 ) {
769  if (!Header_Quiet)
770  fprintf(stderr,
771  "\nERREUR Fin de fichier dans l'entete !\n" ) ;
772  snprintf(Header_Message, sizeof Header_Message,
773  "\nERREUR Fin de fichier dans l'entete !\n" ) ;
774  return ERR_EOF_IN_HEADER;
775  }
776 
777  /* --- FF --> termine le header --- */
778  else if ( in == FF ) {
779  str[i] = '\000' ;
780  go = FAUX ;
781  break ;
782  }
783 
784  /* --- CR et LF --> termine la ligne --- */
785  else if ( in == CR ) {
786  str[i] = '\000' ;
787  } else if ( in == LF ) {
788  str[i] = '\000' ;
789  break ;
790  }
791 
792  /* --- (0x20 < caractere > 0x7E) si non --> erreur --- */
793  else if ( in && ! isprint(in) ) {
794  if (!Header_Quiet)
795  fprintf(stderr,
796  "\nERREUR caract. non imprim. 0x%.2X dans "
797  "l'entete at byte number %ld\n", in, tell(file) );
798  snprintf(Header_Message, sizeof Header_Message,
799  "\nERREUR caract. non imprim. 0x%.2X dans l'entete !\n", in );
800  return ERR_UNPRINTABLE_CHAR;
801  } else {
802  str[i++] = (char) in ;
803  }
804 
805  }
806 
807  /* --- ----- ----- ----- sauve les commentaires ----- ----- --- */
808  for ( char* pt_1=str; *pt_1; pt_1++ ) {
809 
810  /* --- '*' ou '#' = commentaires ------> dans comment --- */
811  if ( *pt_1 == '*' || *pt_1 == '#') {
812  if ( *(pt_1+1) ) {
813  v_comment[com] = (char *)malloc( strlen(pt_1+1) + 1 ) ;
814  strcpy( v_comment[com++], pt_1+1 ) ;
815  if (com%NALLOC == 0)
816  v_comment = (char **)
817  realloc(v_comment, (com+NALLOC)*sizeof(char *));
818  v_comment[com] = NULL ;
819  bool_comment = VRAI;
820  }
821  *pt_1 = '\000' ;
822  break ;
823  }
824 
825  /* --- convertit majuscule un minuscules --- */
826  if (isupper(*pt_1)) *pt_1 = tolower( *pt_1 ) ;
827 
828  }
829 
830 
831  /* --- ----- ----- ----- analyse la ligne ----- ----- ----- --- */
832  for ( char *pt_1=strtok(str," ,;\t"); pt_1; pt_1=strtok(NULL," ,;\t") ) {
833 
834  /* --- separe la chaine de caract. --- */
835  /* --- pt_1 pointe au key_word ---- */
836  /* --- pt_2 pointe a la donne ---- */
837  char *pt_2;
838  for ( pt_2 = pt_1; *pt_2 != ':'; pt_2 ++ )
839  if ( ! *pt_2 ) {
840  if (!Header_Quiet)
841  fprintf(stderr,
842  "\nERREUR de syntaxe dans l'entete (%s)\n", pt_1);
843  snprintf(Header_Message, sizeof Header_Message,
844  "\nERREUR de syntaxe dans l'entete (%s)\n", pt_1);
845  return ERR_IGB_SYNTAX;
846  }
847 
848  *pt_2++ = '\000' ;
849 
850  /* --- recherche le mot-clef --- */
851  if ( ! strcmp( pt_1, "x" ) ) {
852  v_x = atoi( pt_2 ) ;
853  bool_x = VRAI;
854 
855  } else if ( ! strcmp( pt_1, "y" ) ) {
856  v_y = atoi( pt_2 ) ;
857  bool_y = VRAI;
858 
859  } else if ( ! strcmp( pt_1, "z" ) ) {
860  v_z = atoi( pt_2 ) ;
861  bool_z = VRAI;
862 
863  } else if ( ! strcmp( pt_1, "t" ) ) {
864  v_t = atoi( pt_2 ) ;
865  bool_t = VRAI;
866 
867  /* Pour compatibilite avec les vielles images */
868  } else if ( ! strcmp( pt_1, "sup" ) ) {
869  s += atoi( pt_2 ) ;
870  nosup = FAUX;
871 
872  } else if ( ! strcmp( pt_1, "type" ) ) {
873 
874  for( int htype=IGB_MIN_TYPE; htype<=IGB_MAX_TYPE; htype++ )
875  if ( !strcmp( pt_2, Header_Type[htype] ) ) {
876  v_type = htype;
877  }
878  bool_type = VRAI;
879 
880  } else if ( ! strcmp( pt_1, "taille" ) ) {
881  v_taille = atoi( pt_2 ) ;
882  bool_taille = VRAI;
883 
884  } else if ( ! strcmp( pt_1, "systeme" ) ) {
885  v_systeme = INCONNU;
886  for (i=0;i<N_SYSTEMES;i++) {
887  if ( ! strcmp( pt_2, Header_Systeme[i] ) ) {
888  v_systeme = Header_Systeme_No[i] ;
889  break;
890  }
891  }
892 
893  } else if ( ! strcmp( pt_1, "bin" ) ) {
894  v_bin = atoi( pt_2 ) ;
895  bool_bin = VRAI;
896 
897  } else if ( ! strcmp( pt_1, "trame" ) ) {
898 
899  if ( ! strcmp( pt_2, "c8" ) ) {
900  v_trame = C8 ;
901  } else if ( ! strcmp( pt_2, "c4" ) ) {
902  v_trame = C4 ;
903  } else if ( ! strcmp( pt_2, "hex" ) ) {
904  v_trame = HEX ;
905  } else if ( ! strcmp( pt_2, "hexedges" ) ) {
906  v_trame = HEXEDGES ;
907  } else if ( ! strcmp( pt_2, "hexbridges" ) ) {
908  v_trame = HEXBRIDGES ;
909  } else if ( ! strcmp( pt_2, "hexlines" ) ) {
910  v_trame = HEXLINES ;
911  } else if ( ! strcmp( pt_2, "hex2" ) ) {
912  v_trame = HEX2 ;
913  }
914  bool_trame = VRAI;
915 
916  } else if ( ! strcmp( pt_1, "num" ) ) {
917  v_num = atoi( pt_2 ) ;
918  bool_num = VRAI;
919 
920  } else if ( ! strcmp( pt_1, "comp" ) ) {
921  v_comp = atoi( pt_2 ) ;
922  bool_comp = VRAI;
923 
924  } else if ( ! strcmp( pt_1, "lut" ) ) {
925  v_lut = atoi( pt_2 ) ;
926  bool_lut = VRAI;
927 
928  } else if ( ! strcmp( pt_1, "dim_x" ) ) {
929  v_dim_x = atof( pt_2 ) ;
930  bool_dim_x = VRAI;
931 
932  } else if ( ! strcmp( pt_1, "dim_y" ) ) {
933  v_dim_y = atof( pt_2 ) ;
934  bool_dim_y = VRAI;
935 
936  } else if ( ! strcmp( pt_1, "dim_z" ) ) {
937  v_dim_z = atof( pt_2 ) ;
938  bool_dim_z = VRAI;
939 
940  } else if ( ! strcmp( pt_1, "dim_t" ) ) {
941  v_dim_t = atof( pt_2 ) ;
942  bool_dim_t = VRAI;
943 
944  } else if ( ! strcmp( pt_1, "inc_x" ) ) {
945  v_inc_x = atof( pt_2 ) ;
946  bool_inc_x = VRAI;
947 
948  } else if ( ! strcmp( pt_1, "inc_y" ) ) {
949  v_inc_y = atof( pt_2 ) ;
950  bool_inc_y = VRAI;
951 
952  } else if ( ! strcmp( pt_1, "inc_z" ) ) {
953  v_inc_z = atof( pt_2 ) ;
954  bool_inc_z = VRAI;
955 
956  } else if ( ! strcmp( pt_1, "inc_t" ) ) {
957  v_inc_t = atof( pt_2 ) ;
958  bool_inc_t = VRAI;
959 
960  } else if ( ! strcmp( pt_1, "org_x" ) ) {
961  v_org_x = atof( pt_2 ) ;
962  bool_org_x = VRAI;
963 
964  } else if ( ! strcmp( pt_1, "org_y" ) ) {
965  v_org_y = atof( pt_2 ) ;
966  bool_org_y = VRAI;
967 
968  } else if ( ! strcmp( pt_1, "org_z" ) ) {
969  v_org_z = atof( pt_2 ) ;
970  bool_org_z = VRAI;
971 
972  } else if ( ! strcmp( pt_1, "org_t" ) ) {
973  v_org_t = atof( pt_2 ) ;
974  bool_org_t = VRAI;
975 
976  } else if ( ! strcmp( pt_1, "vect_z" ) ) {
977  bool_vect_z = VRAI;
978 
979  } else if ( ! strcmp( pt_1, "unites_x" ) ) {
980  strncpy( v_unites_x, pt_2, 40 ) ;
981  bool_unites_x = VRAI;
982 
983  } else if ( ! strcmp( pt_1, "unites_y" ) ) {
984  strncpy( v_unites_y, pt_2, 40 ) ;
985  bool_unites_y = VRAI;
986 
987  } else if ( ! strcmp( pt_1, "unites_z" ) ) {
988  strncpy( v_unites_z, pt_2, 40 ) ;
989  bool_unites_z = VRAI;
990 
991  } else if ( ! strcmp( pt_1, "unites_t" ) ) {
992  strncpy( v_unites_t, pt_2, 40 ) ;
993  bool_unites_t = VRAI;
994 
995  } else if ( ! strcmp( pt_1, "epais" ) ) {
996  v_epais = atof( pt_2 ) ;
997  bool_epais = VRAI;
998 
999  } else if ( ! strcmp( pt_1, "unites" ) ) {
1000  strncpy( v_unites, pt_2, 40 ) ;
1001  bool_unites = VRAI;
1002 
1003  } else if ( ! strcmp( pt_1, "facteur" ) ) {
1004  v_facteur = atof( pt_2 ) ;
1005  bool_facteur = VRAI;
1006 
1007  } else if ( ! strcmp( pt_1, "zero" ) ) {
1008  v_zero = atof( pt_2 ) ;
1009  bool_zero = VRAI;
1010 
1011  } else if ( ! strcmp( pt_1, "struct" ) ) {
1012  strncpy( v_struct_desc, pt_2, 40 ) ;
1013  bool_struct_desc = VRAI;
1014 
1015  } else if ( ! strcmp( pt_1, "aut" ) ) {
1016  strncpy( v_aut_name, pt_2, 40 ) ;
1017  bool_aut_name = VRAI;
1018 
1019  } else if ( !strcmp( pt_1, "transparent" ) ) {
1020  strcpy( transstr, pt_2 );
1021  bool_transparent = VRAI;
1022 
1023  } else {
1024  if( is_deprecated( pt_1 ) ){
1025  if (!Header_Quiet)
1026  fprintf(stderr,"\nATTENTION: mot-clef %s obsolete !\n", pt_1 ) ;
1027  if( !strcmp( pt_1, "fac_t" ) ){
1028  v_fac_t = atof( pt_2 );
1029  bool_fac_t = VRAI;
1030  }
1031  } else {
1032  if (!Header_Quiet)
1033  fprintf(stderr,"\nATTENTION: mot-clef %s non reconnu !\n", pt_1 );
1034  snprintf(Header_Message, sizeof Header_Message,
1035  "\nATTENTION: mot-clef %s non reconnu !\n", pt_1 ) ;
1036  statut |= MOT_CLEF_INV;
1037  }
1038  }
1039  }
1040  }
1041  /* determine the transparent value if one was chosen */
1042  if ( bool_transparent ) {
1043  if ( strlen(transstr) != 2*Data_Size[v_type] ) {
1044  fprintf(stderr,"ATTENTION: ignoring invalid transparent value !\n");
1045  } else {
1046  char s[3], *p, *v;
1047  s[2] = '\0';
1048  v = (char *)(v_transparent = calloc( Data_Size[v_type], 1 ));
1049  for ( int i=0; i<Data_Size[v_type]; i++ ) {
1050  s[0] = transstr[i*2];
1051  s[1] = transstr[i*2+1];
1052  v[i] = strtol( s, &p, 16 );
1053  }
1054  }
1055  }
1056 
1057  /* --- l'info x y et type est obligatoire --- */
1058  if ( !bool_x || !bool_y || !bool_type ) {
1059  if (!Header_Quiet)
1060  fprintf(stderr, "\nERREUR x, y ou type non definis\n") ;
1061  snprintf(Header_Message, sizeof Header_Message, "\nERREUR x, y ou type non definis\n") ;
1062  return ERR_UNDEFINED_X_Y_TYPE ;
1063  }
1064 
1065  /* --- calcul des inc et dim --- */
1066  if ( bool_dim_x )
1067  if ( bool_inc_x ) {
1068  float dim_x = v_inc_x * v_x ;
1069  if ( dim_x != v_dim_x ) {
1070  if (!Header_Quiet) {
1071  fprintf(stderr, "\nATTENTION:\n") ;
1072  fprintf(stderr,
1073  "conflit entre x (%d) * inc_x (%.12g) = %.12g et dim_x (%.12g)\n",
1074  v_x, v_inc_x, dim_x, v_dim_x) ;
1075  }
1076  snprintf(Header_Message, sizeof Header_Message,
1077  "conflit entre x (%d) * inc_x (%.12g) = %.12g et dim_x (%.12g)\n",
1078  v_x, v_inc_x, dim_x, v_dim_x) ;
1079  statut = WARN_DIM_INCONSISTENT ;
1080  }
1081  } else {
1082  v_inc_x = v_dim_x / v_x ;
1083  bool_inc_x = VRAI;
1084  }
1085  else {
1086  v_dim_x = v_x * v_inc_x ;
1087  if ( bool_inc_x ) bool_dim_x = VRAI;
1088  }
1089 
1090  if ( bool_dim_y )
1091  if ( bool_inc_y ) {
1092  float dim_y = v_inc_y * v_y ;
1093  if ( dim_y != v_dim_y ) {
1094  if (!Header_Quiet) {
1095  fprintf(stderr, "\nATTENTION:\n") ;
1096  fprintf(stderr,
1097  "conflit entre y (%d) * inc_y (%.12g) = %.12g et dim_y (%.12g)\n",
1098  v_y, v_inc_y, dim_y, v_dim_y) ;
1099  }
1100  snprintf(Header_Message, sizeof Header_Message,
1101  "conflit entre y (%d) * inc_y (%.12g) = %.12g et dim_y (%.12g)\n",
1102  v_y, v_inc_y, dim_y, v_dim_y) ;
1103  statut = WARN_DIM_INCONSISTENT ;
1104  }
1105  } else {
1106  v_inc_y = v_dim_y / v_y ;
1107  bool_inc_y = VRAI;
1108  }
1109  else {
1110  v_dim_y = v_y * v_inc_y ;
1111  if ( bool_inc_y ) bool_dim_y = VRAI;
1112  }
1113 
1114  if ( bool_dim_z )
1115  if ( bool_inc_z ) {
1116  float dim_z = v_inc_z * v_z ;
1117  if ( dim_z != v_dim_z ) {
1118  if (!Header_Quiet) {
1119  fprintf(stderr, "\nATTENTION:\n") ;
1120  fprintf(stderr,
1121  "conflit entre z (%d) * inc_z (%.12g) = %.12g et dim_z (%.12g)\n",
1122  v_z, v_inc_z, dim_z, v_dim_z) ;
1123  }
1124  snprintf(Header_Message, sizeof Header_Message,
1125  "conflit entre z (%d) * inc_z (%.12g) = %.12g et dim_z (%.12g)\n",
1126  v_z, v_inc_z, dim_z, v_dim_z) ;
1127  statut = WARN_DIM_INCONSISTENT ;
1128  }
1129  } else {
1130  v_inc_z = v_dim_z / v_z ;
1131  bool_inc_z = VRAI;
1132  }
1133  else {
1134  v_dim_z = v_z * v_inc_z ;
1135  if ( bool_inc_z ) bool_dim_z = VRAI;
1136  }
1137 
1138  if( !bool_inc_t && bool_fac_t ) {
1139  bool_inc_t = VRAI;
1140  v_inc_t = v_fac_t;
1141  }
1142  if ( bool_dim_t )
1143  if ( bool_inc_t ) {
1144  float dim_t = v_inc_t * (v_t-1) ;
1145  if ( fabs(dim_t - v_dim_t) > v_inc_t ) {
1146  if (!Header_Quiet) {
1147  fprintf(stderr, "\nATTENTION:\n") ;
1148  fprintf(stderr,
1149  "conflit entre t (%d) * inc_t (%.12g) = %.12g et dim_t (%.12g)\n",
1150  v_t, v_inc_t, dim_t, v_dim_t) ;
1151  }
1152  snprintf(Header_Message, sizeof Header_Message,
1153  "conflit entre t (%d) * inc_t (%.12g) = %.12g et dim_t (%.12g)\n",
1154  v_t, v_inc_t, dim_t, v_dim_t) ;
1155  statut = WARN_DIM_INCONSISTENT ;
1156  }
1157  } else {
1158  v_inc_t = v_dim_t / (v_t - 1) ;
1159  bool_inc_t = VRAI;
1160  }
1161  else {
1162  v_dim_t = (v_t-1) * v_inc_t ;
1163  if ( bool_inc_t )
1164  bool_dim_t = VRAI;
1165  }
1166 
1167  if ( bool_taille ) {
1168  if (v_type!=IGB_STRUCTURE) {
1169  if (!Header_Quiet)
1170  fprintf(stderr,
1171  "\nERREUR taille redefinie pour type autre que structure\n") ;
1172  snprintf(Header_Message, sizeof Header_Message,
1173  "\nERREUR taille redefinie pour type autre que structure\n") ;
1174  return ERR_SIZE_REDEFINED;
1175  }
1176  } else {
1177  if (v_type==IGB_STRUCTURE) {
1178  if (!Header_Quiet)
1179  fprintf(stderr,
1180  "\nERREUR taille non definie pour type structure\n") ;
1181  snprintf(Header_Message, sizeof Header_Message,
1182  "\nERREUR taille non definie pour type structure\n") ;
1183  return ERR_SIZE_NOT_DEFINED;
1184  } else {
1185  v_taille = Data_Size[v_type];
1186  }
1187  }
1188 
1189  if (tell(file)!=1024) {
1190  fseek(file, 1024, SEEK_SET);
1191  if (!Header_Quiet) {
1192  fprintf(stderr,
1193  "\nATTENTION: etiquette de grandeur non-standard: %ld \n", tell(file));
1194  }
1195  snprintf(Header_Message, sizeof Header_Message,
1196  "\nATTENTION: etiquette de grandeur non-standard \n");
1197  statut |= GRANDEUR_INV;
1198  } else {
1199  snprintf(Header_Message, sizeof Header_Message,
1200  "\nHeader_Read: Etiquette lue sans problemes\n");
1201  }
1202 
1203  if (bool_vect_z) {
1204  v_vect_z = (float *)malloc(v_z*sizeof(float));
1205  fread(v_vect_z, sizeof(float), v_z, file);
1206  }
1207 
1208  return statut;
1209 }
1210 
1211 /*
1212  * add a comment line
1213  * clear comments is NULL is passed
1214  */
1215 void IGBheader::comment(char *ligne)
1216 {
1217  int i = 0, n_lignes = 0;
1218 
1219  if ( ligne == NULL ) {
1220  while ( v_comment[n_lignes] != NULL )
1221  free( v_comment[n_lignes++] );
1222  return;
1223  }
1224 
1225  while (v_comment[i++]!=NULL) n_lignes++;
1226  v_comment = (char **)realloc(v_comment, (n_lignes+2)*sizeof(char *));
1227 
1228  v_comment[n_lignes] = (char *)malloc(strlen(ligne)+1) ;
1229  strcpy(v_comment[n_lignes++], ligne) ;
1230  v_comment[n_lignes] = NULL;
1231  bool_comment = true;
1232 }
1233 
1234 const char *IGBheader::systemestr( void )
1235 {
1236  int i;
1237 
1238  for ( i=0; i<N_SYSTEMES; i++ )
1239  if ( Header_Systeme_No[i] == v_systeme )
1240  break;
1241 
1242  if ( i<N_SYSTEMES )
1243  return Header_Systeme[i];
1244  else
1245  return NULL;
1246 }
1247 
1248 
1249 void IGBheader::systeme( const char* s )
1250 {
1251  int i;
1252  for ( i=0; i<N_SYSTEMES; i++ )
1253  if ( !strncmp( Header_Systeme[i], s, strlen(s) ) )
1254  break;
1255 
1256  if ( i<N_SYSTEMES )
1257  v_systeme = Header_Systeme_No[i];
1258 }
1259 
1260 
1261 void IGBheader::type( char *datatype )
1262 {
1263  int tn=IGB_MIN_TYPE;
1264  while ( tn<=IGB_MAX_TYPE && strcmp(Header_Type[tn],datatype) )
1265  tn++;
1266  if ( tn<=IGB_MAX_TYPE )
1267  v_type = tn;
1268  else {
1269  cerr << "illegal data type specified for IGB header" << endl;
1270  exit(1);
1271  }
1272  bool_type = true;
1273 }
1274 
1275 
1276 long IGBheader::tell( FILE *f )
1277 {
1278  return ftell(f);
1279 }
1280 
1281 
1282 int IGBheader::puts_fcn( FILE* f, char* s )
1283 {
1284  return fputs(s, f);
1285 }
1286 
1287 
1293 void IGBheader :: swab( void *data, int nd )
1294 {
1295  if ( data_size()==1 || v_type==IGB_RGBA )
1296  return;
1297 
1298  unsigned char *bp = (unsigned char *)data;
1299  int ds = data_size()/num_components();
1300 
1301  if ( nd<0 ) nd = v_x*v_y*v_z*v_t;
1302 
1303  nd *= num_components();
1304 
1305  switch ( ds ) {
1306  case 2:
1307  for ( int i=0; i<nd; i++ ) {
1308  unsigned char tmpb = bp[0];
1309  bp[0] = bp[1];
1310  bp[1] = tmpb;
1311  bp += data_size();
1312  }
1313  break;
1314  case 4:
1315  for ( int i=0; i<nd; i++ ) {
1316  unsigned char tmpb = bp[0];
1317  bp[0] = bp[3];
1318  bp[3] = tmpb;
1319  tmpb = bp[1];
1320  bp[1] = bp[2];
1321  bp[2] = tmpb;
1322  bp += data_size();
1323  }
1324  break;
1325  case 8:
1326  for ( int i=0; i<nd; i++ ) {
1327  unsigned char tmpb = bp[0];
1328  bp[0] = bp[7];
1329  bp[7] = tmpb;
1330  tmpb = bp[1];
1331  bp[1] = bp[6];
1332  bp[6] = tmpb;
1333  tmpb = bp[2];
1334  bp[2] = bp[5];
1335  bp[5] = tmpb;
1336  tmpb = bp[3];
1337  bp[3] = bp[4];
1338  bp[4] = tmpb;
1339  bp += data_size();
1340  }
1341  break;
1342  }
1343 }
1344 
1345 
1348 {
1349  float val=IGB_ENDIAN_VAL;
1350  unsigned char le_val[] = { IGB_LITTLE_END_REP },
1351  *pval = (unsigned char *)(&val);
1352 
1353  assert( sizeof(float) == 4 );
1354 
1355  if ( *pval == le_val[0] )
1356  return IGB_LITTLE_ENDIAN;
1357  else
1358  return IGB_BIG_ENDIAN;
1359 }
1360 
1361 } // namespace opencarp
int read(bool quiet=false)
Definition: IGBheader.cc:732
const char * systemestr(void)
Definition: IGBheader.cc:1234
int systeme(void)
Definition: IGBheader.h:285
IGBheader(FILE *f=NULL, bool read=false, bool quiet=false)
Definition: IGBheader.cc:313
char ** comment(void)
Definition: IGBheader.h:254
FILE * fileptr(void)
Definition: IGBheader.h:247
void swab(void *, int nd=-1)
Definition: IGBheader.cc:1293
unsigned int UInt
Definition: IGBheader.h:133
const char * Header_Type[]
Definition: IGBheader.cc:249
unsigned short Data_Size[]
Definition: IGBheader.cc:264
char Header_Message[256]
Definition: IGBheader.cc:247
const char * deprecated[]
Definition: IGBheader.cc:259
int my_fputs(FILE *, char *)
uint16_t short_float
Definition: short_float.h:48
long unsigned Header_Systeme_No[]
Definition: IGBheader.cc:282
int Num_Components[]
Definition: IGBheader.cc:275
unsigned char Byte
Definition: IGBheader.h:123
const char * Header_Systeme[]
Definition: IGBheader.cc:289
bool Header_Quiet
Definition: IGBheader.cc:246
#define GRANDEUR_INV
Definition: IGBheader.cc:41
#define FF
Definition: IGBheader.cc:58
#define LF
Definition: IGBheader.cc:57
#define N_MAX_ITEMS
Definition: IGBheader.cc:234
#define MAX_TRAME
Definition: IGBheader.cc:54
#define C8
Definition: IGBheader.cc:47
#define HEXLINES
Definition: IGBheader.cc:52
#define CR
Definition: IGBheader.cc:59
#define HEX
Definition: IGBheader.cc:49
#define L_MAX_ITEM
Definition: IGBheader.cc:235
#define HEXEDGES
Definition: IGBheader.cc:50
#define HEXBRIDGES
Definition: IGBheader.cc:51
#define MAXL
Definition: IGBheader.cc:233
#define C4
Definition: IGBheader.cc:48
#define MAKE_CONSISTENT(D)
Definition: IGBheader.cc:305
#define HEX2
Definition: IGBheader.cc:53
#define VRAI
Definition: IGBheader.cc:237
#define FAUX
Definition: IGBheader.cc:240
#define MOT_CLEF_INV
Definition: IGBheader.cc:40
#define ERR_IGB_SYNTAX
Definition: IGBheader.h:103
#define IGB_LITTLE_ENDIAN
Definition: IGBheader.h:50
#define IGB_ENDIAN_VAL
Definition: IGBheader.h:96
#define N_SYSTEMES
Definition: IGBheader.h:51
#define ERR_UNDEFINED_X_Y_TYPE
Definition: IGBheader.h:104
#define IGB_RGBA
Definition: IGBheader.h:63
#define IGB_MAX_TYPE
Definition: IGBheader.h:78
#define IGB_STRUCTURE
Definition: IGBheader.h:64
#define WARN_DIM_INCONSISTENT
Definition: IGBheader.h:107
#define INCONNU
Definition: IGBheader.h:47
#define IGB_BIG_ENDIAN
Definition: IGBheader.h:49
#define ERR_EOF_IN_HEADER
Definition: IGBheader.h:100
#define IGB_MIN_TYPE
Definition: IGBheader.h:77
#define ERR_SIZE_NOT_DEFINED
Definition: IGBheader.h:106
#define ERR_LINE_TOO_LONG
Definition: IGBheader.h:101
#define NALLOC
Definition: IGBheader.h:44
#define ERR_UNPRINTABLE_CHAR
Definition: IGBheader.h:102
#define ERR_SIZE_REDEFINED
Definition: IGBheader.h:105
#define IGB_LITTLE_END_REP
Definition: IGBheader.h:97