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 
322 IGBheader::IGBheader( gzFile f, bool _read, bool quiet )
323 {
324  fileptr(f);
325  if( _read )
326  if( read( quiet ) )
327  throw 1;
328 }
329 
330 
332 {}
333 
334 
335 // set output for gzipped output
336 void IGBheader::fileptr( gzFile fp )
337 {
338  file = fp;
339  gzipping = true;
340 }
341 
342 // set output for normal unziped output
343 void IGBheader::fileptr( FILE* fp )
344 {
345  file = fp;
346  gzipping = false;
347 }
348 
349 
351 {
352  int statut = 1;
353 
354  if (getenv("HEADER_QUIET")) Header_Quiet = VRAI;
355 
356  if (file==NULL) {
357  if (!Header_Quiet)
358  cerr<< "\nERREUR: descripteur de fichier nul\n";
359  snprintf(Header_Message, sizeof Header_Message, "\nERREUR: descripteur de fichier nul\n");
360  return(0) ;
361  }
362 
363  if (v_type<IGB_MIN_TYPE || v_type>IGB_MAX_TYPE) {
364  if (!Header_Quiet)
365  cerr<< "\nHeader_Write: type inconnu: "<< v_type;
366  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: type inconnu: %d\n",
367  v_type);
368  return (0);
369  }
370  const char *type = Header_Type[v_type];
371 
372  if (v_type==IGB_STRUCTURE && v_taille<1) {
373  if (!Header_Quiet)
374  cerr << "\nHeader_Write: taille invalide:" << v_taille << endl;
375  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: taille invalide: %d\n",
376  v_taille);
377  return (0);
378  }
379 
380  if (v_trame<MIN_TRAME || v_trame>MAX_TRAME) {
381  if (!Header_Quiet)
382  fprintf(stderr, "\nHeader_Write: trame inconnue: %d\n", v_trame);
383  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: trame inconnue: %d\n",
384  v_trame);
385  return (0);
386  }
387 
388  MAKE_CONSISTENT( x );
389  MAKE_CONSISTENT( y );
390  MAKE_CONSISTENT( z );
391  MAKE_CONSISTENT( t );
392 
393  char ligne[1024];
394  if (bool_t) {
395  if (bool_z) {
396  snprintf(ligne, sizeof ligne, "x:%d y:%d z:%d t:%d type:%s systeme:%s ",
397  v_x, v_y, v_z, v_t, type, systemestr());
398  } else {
399  snprintf(ligne, sizeof ligne, "x:%d y:%d t:%d type:%s systeme:%s ",
400  v_x, v_y, v_t, type, systemestr());
401  }
402  } else {
403  if (bool_z) {
404  snprintf(ligne, sizeof ligne, "x:%d y:%d z:%d type:%s systeme:%s ",
405  v_x, v_y, v_z, type, systemestr());
406  } else {
407  snprintf(ligne, sizeof ligne, "x:%d y:%d type:%s systeme:%s ",
408  v_x, v_y, type, systemestr());
409  }
410  }
411  int n_car = strlen(ligne);
412 
413  int n_lignes = 1;
414  int n_items = 0;
415  int l_item[N_MAX_ITEMS+1];
416  char items[N_MAX_ITEMS+1][L_MAX_ITEM];
417  /*
418  Le mot-clef "taille" n'est ecrit que pour le type STRUCTURE mais il est
419  obligatoire pour ce cas.
420  */
421  if (v_type==IGB_STRUCTURE) {
422  snprintf(&items[n_items][0], L_MAX_ITEM, "taille:%d ", v_taille);
423  l_item[n_items] = strlen(&items[n_items][0]);
424  n_items++;
425  }
426  if (bool_org_x) {
427  snprintf(&items[n_items][0], L_MAX_ITEM, "org_x:%g ", v_org_x);
428  l_item[n_items] = strlen(&items[n_items][0]);
429  n_items++;
430  }
431  if (bool_org_y) {
432  snprintf(&items[n_items][0], L_MAX_ITEM, "org_y:%g ", v_org_y);
433  l_item[n_items] = strlen(&items[n_items][0]);
434  n_items++;
435  }
436  if (bool_org_z) {
437  snprintf(&items[n_items][0], L_MAX_ITEM, "org_z:%g ", v_org_z);
438  l_item[n_items] = strlen(&items[n_items][0]);
439  n_items++;
440  }
441  if (bool_org_t) {
442  snprintf(&items[n_items][0], L_MAX_ITEM, "org_t:%g ", v_org_t);
443  l_item[n_items] = strlen(&items[n_items][0]);
444  n_items++;
445  }
446  if (bool_dim_x) {
447  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_x:%g ", v_dim_x);
448  l_item[n_items] = strlen(&items[n_items][0]);
449  n_items++;
450  }
451  if (bool_inc_x) {
452  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_x:%g ", v_inc_x);
453  l_item[n_items] = strlen(&items[n_items][0]);
454  n_items++;
455  }
456  if (bool_dim_y) {
457  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_y:%g ", v_dim_y);
458  l_item[n_items] = strlen(&items[n_items][0]);
459  n_items++;
460  }
461  if (bool_inc_y) {
462  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_y:%g ", v_inc_y);
463  l_item[n_items] = strlen(&items[n_items][0]);
464  n_items++;
465  }
466  if (bool_dim_z) {
467  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_z:%g ", v_dim_z);
468  l_item[n_items] = strlen(&items[n_items][0]);
469  n_items++;
470  }
471  if (bool_inc_z) {
472  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_z:%g ", v_inc_z);
473  l_item[n_items] = strlen(&items[n_items][0]);
474  n_items++;
475  }
476  if (bool_dim_t) {
477  snprintf(&items[n_items][0], L_MAX_ITEM, "dim_t:%g ", v_dim_t);
478  l_item[n_items] = strlen(&items[n_items][0]);
479  n_items++;
480  }
481  if (bool_inc_t) {
482  snprintf(&items[n_items][0], L_MAX_ITEM, "inc_t:%g ", v_inc_t);
483  l_item[n_items] = strlen(&items[n_items][0]);
484  n_items++;
485  }
486  if (bool_vect_z) {
487  snprintf(&items[n_items][0], L_MAX_ITEM, "vect_z:1 ");
488  l_item[n_items] = strlen(&items[n_items][0]);
489  n_items++;
490  }
491  if (bool_unites_x) {
492  snprintf(&items[n_items][0], L_MAX_ITEM, "unites_x:%.40s ", v_unites_x);
493  l_item[n_items] = strlen(&items[n_items][0]);
494  n_items++;
495  }
496  if (bool_unites_y) {
497  snprintf(&items[n_items][0], L_MAX_ITEM, "unites_y:%.40s ", v_unites_y);
498  l_item[n_items] = strlen(&items[n_items][0]);
499  n_items++;
500  }
501  if (bool_unites_z) {
502  snprintf(&items[n_items][0], L_MAX_ITEM, "unites_z:%.40s ", v_unites_z);
503  l_item[n_items] = strlen(&items[n_items][0]);
504  n_items++;
505  }
506  if (bool_unites_t) {
507  snprintf(&items[n_items][0], L_MAX_ITEM, "unites_t:%.40s ", v_unites_t);
508  l_item[n_items] = strlen(&items[n_items][0]);
509  n_items++;
510  }
511  if (bool_num) {
512  snprintf(&items[n_items][0], L_MAX_ITEM, "num:%d ", v_num);
513  l_item[n_items] = strlen(&items[n_items][0]);
514  n_items++;
515  }
516  if (bool_bin) {
517  snprintf(&items[n_items][0], L_MAX_ITEM, "bin:%d ", v_bin);
518  l_item[n_items] = strlen(&items[n_items][0]);
519  n_items++;
520  }
521  if (bool_trame) {
522  switch (v_trame) {
523  case C8:
524  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:c8 ");
525  break;
526  case C4:
527  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:c4 ");
528  break;
529  case HEX:
530  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hex ");
531  break;
532  case HEXEDGES:
533  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hexedges ");
534  break;
535  case HEXBRIDGES:
536  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hexbridges ");
537  break;
538  case HEXLINES:
539  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hexlines ");
540  break;
541  case HEX2:
542  snprintf(&items[n_items][0], L_MAX_ITEM, "trame:hex2 ");
543  break;
544  }
545  l_item[n_items] = strlen(&items[n_items][0]);
546  n_items++;
547  }
548  if (bool_lut) {
549  snprintf(&items[n_items][0], L_MAX_ITEM, "lut:%d ", v_lut);
550  l_item[n_items] = strlen(&items[n_items][0]);
551  n_items++;
552  }
553  if (bool_comp) {
554  snprintf(&items[n_items][0], L_MAX_ITEM, "comp:%d ", v_comp);
555  l_item[n_items] = strlen(&items[n_items][0]);
556  n_items++;
557  }
558  if (bool_epais) {
559  snprintf(&items[n_items][0], L_MAX_ITEM, "epais:%g ", v_epais);
560  l_item[n_items] = strlen(&items[n_items][0]);
561  n_items++;
562  }
563  if (bool_unites) {
564  snprintf(&items[n_items][0], L_MAX_ITEM, "unites:%.40s ", v_unites);
565  l_item[n_items] = strlen(&items[n_items][0]);
566  n_items++;
567  }
568  if (bool_facteur) {
569  snprintf(&items[n_items][0], L_MAX_ITEM, "facteur:%g ", v_facteur);
570  l_item[n_items] = strlen(&items[n_items][0]);
571  n_items++;
572  }
573  if (bool_zero) {
574  snprintf(&items[n_items][0], L_MAX_ITEM, "zero:%g ", v_zero);
575  l_item[n_items] = strlen(&items[n_items][0]);
576  n_items++;
577  }
578  if ( v_transparent != NULL ) {
579  char *p=(char *)v_transparent, value[MAXL];
580  int a;
581  for ( a=0; a<Data_Size[v_type]; a++ )
582  snprintf( value+a*2, L_MAX_ITEM, "%0.2x", *(p++) );
583  value[2*Data_Size[v_type]] = '\0';
584  snprintf(&items[n_items][0], L_MAX_ITEM, "transparent:%s ", value );
585  l_item[n_items] = strlen(&items[n_items][0]);
586  n_items++;
587  }
588  if (bool_struct_desc) {
589  snprintf(&items[n_items][0], L_MAX_ITEM, "struct:%.40s ", v_struct_desc);
590  l_item[n_items] = strlen(&items[n_items][0]);
591  n_items++;
592  }
593  if (bool_aut_name) {
594  snprintf(&items[n_items][0], L_MAX_ITEM, "aut:%.40s ", v_aut_name);
595  l_item[n_items] = strlen(&items[n_items][0]);
596  n_items++;
597  }
598  int n_car_total = 0;
599  int n_comment = 0;
600  char **comment;
601  if (bool_comment) {
602  comment = v_comment;
603  while (*comment != NULL) {
604  n_comment++;
605  n_car_total += strlen(*comment++) + 3;
606  }
607  }
608 
609  /*
610  Ecrit tous les items, sauf les commentaires
611  */
612  for (int i=0;i<n_items;i++) {
613  if (n_car+l_item[i]<71) { /* Ajoute a la ligne courante s'il reste de la place */
614  strcat(ligne, &items[i][0]);
615  n_car += l_item[i];
616  } else { /* Sinon, ecrit cette ligne et commence-en une autre */
617  ligne[n_car++] = '\r';
618  ligne[n_car++] = '\n';
619  ligne[n_car] = '\000';
620  n_car_total += n_car;
621  if (puts_fcn((void *)file, ligne)==-1) {
622  if (!Header_Quiet) {
623  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
624  perror("\n *** ");
625  fprintf(stderr, "\n");
626  }
627  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
628  return (0);
629  }
630  strcpy(ligne, &items[i][0]);
631  n_car = l_item[i];
632  n_lignes++;
633  }
634  }
635 
636  /*
637  Termine la derniere ligne
638  */
639  ligne[n_car++] = '\r';
640  ligne[n_car++] = '\n';
641  ligne[n_car] = '\000';
642  n_car_total += n_car;
643  if (puts_fcn((void *)file, ligne )==-1) {
644  if (!Header_Quiet) {
645  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
646  perror("\n *** ");
647  fprintf(stderr, "\n");
648  }
649  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
650  return (0);
651  }
652  n_lignes++;
653 
654  /*
655  Determine le nombre de caracteres et de lignes supplementaires
656  necessaires
657  */
658  int n_blocs = 1 + (n_car_total-1)/1024;
659  int n_car_sup = n_blocs*1024 - n_car_total;
660  int n_lig_sup;
661  if (n_car_sup>0) {
662  n_lig_sup = 1 + (n_car_sup-1)/72;
663  } else {
664  n_lig_sup = 0;
665  }
666  int n_car_dl = 1 + (n_car_sup-1)%72;
667 
668  /*
669  Ecrit les commentaires
670  */
671  if (bool_comment) {
672  comment = v_comment;
673  while (*comment != NULL) {
674  n_car = gzprintf((gzFile)file, "#%.80s\r\n", *comment++);
675  if (n_car==0) {
676  if (!Header_Quiet) {
677  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
678  perror("\n *** ");
679  fprintf(stderr, "\n");
680  }
681  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
682  return (0);
683  }
684  }
685  }
686 
687  /*
688  Complete l'entete a un multiple de 1024 caracteres
689  */
690  for (int i=0;i<70;i++) ligne[i] = ' ';
691  ligne[70] = '\r';
692  ligne[71] = '\n';
693  ligne[72] = '\000';
694  for (int i=0;i<n_lig_sup-1;i++) {
695  if (puts_fcn((void *)file, ligne)==-1) {
696  if (!Header_Quiet) {
697  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
698  perror("\n *** ");
699  fprintf(stderr, "\n");
700  }
701  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
702  return 0;
703  }
704  }
705 
706  /*
707  La derniere ligne se termine par un saut de page (FF)
708  */
709  for (int i=0;i<n_car_dl-2;i++) ligne[i] = ' ';
710  if (n_car_dl>2) ligne[n_car_dl-3] = '\r';
711  if (n_car_dl>1) ligne[n_car_dl-2] = '\n';
712  ligne[n_car_dl-1] = FF;
713  ligne[n_car_dl] = '\000';
714  if (puts_fcn( (void *)file, ligne )==-1) {
715  if (!Header_Quiet) {
716  fprintf(stderr, "\nHeader_Write: Erreur a l'ecriture \n");
717  perror("\n *** ");
718  fprintf(stderr, "\n");
719  }
720  snprintf(Header_Message, sizeof Header_Message, "\nHeader_Write: Erreur a l'ecriture \n");
721  return (0);
722  }
723 
724  if (n_car_total>1024) {
725  if (!Header_Quiet)
726  fprintf(stderr,
727  "\nHeader_Write ATTENTION: etiquette de grandeur non-standard \n");
728  snprintf(Header_Message, sizeof Header_Message,
729  "\nHeader_Write ATTENTION: etiquette de grandeur non-standard \n");
730  statut |= GRANDEUR_INV;
731  } else {
732  snprintf(Header_Message, sizeof Header_Message,
733  "\nHeader_Write: Entete transcrite sans problemes\n");
734  }
735 
736  /*
737  if (bool_vect_z) {
738  if (v_vect_z==NULL) {
739  if (!Header_Quiet)
740  fprintf(stderr,"\nERREUR: vect_z nul\n");
741  snprintf(Header_Message, sizeof Header_Message, "\nERREUR: vect_z nul\n");
742  return(0) ;
743  }
744  gzwrite(file, v_vect_z, sizeof(float)*v_z);
745  }
746  */
747 
748  return (statut);
749 
750 }
751 
752 
761 int IGBheader::read( bool quiet )
762 {
763  int go=VRAI, nosup=VRAI;
764  int in, com=0;
765  char str[MAXL+1];
766  int statut=0;
767  float v_fac_t;
768  bool bool_fac_t = false;
769 
770  Header_Quiet = quiet;
771 
772  /* --- pour toutes les lignes de l'entete (def=8) ou jusqu'a un <FF> -- */
773  for ( int s=8; (s>0 || nosup) && go; s-- ) {
774 
775  /* --- lit la ligne dans le fichier --- */
776  int i = 0 ;
777  int bytes_read = tell(file);
778  if( bytes_read<0 )
779  return 2;
780 
781  while (1) {
782 
783  /* --- (ligne trop longue) --> erreur --- */
784  if ( i >= 80 ) {
785  if (!Header_Quiet)
786  fprintf(stderr,
787  "\nERREUR ligne trop longue (>80) dans l'entete !\n" ) ;
788  snprintf(Header_Message, sizeof Header_Message,
789  "\nERREUR ligne trop longue (>80) dans l'entete !\n" ) ;
790  return ERR_LINE_TOO_LONG;
791  }
792 
793  /* --- lit le caractere suivant --- */
794  //in = gzgetc( (gzFile)file ) ;
795  in = getc( (FILE*)file ) ;
796 
797  /* --- (EOF dans l'entete) --> erreur --- */
798  if ( in == -1 ) {
799  if (!Header_Quiet)
800  fprintf(stderr,
801  "\nERREUR Fin de fichier dans l'entete !\n" ) ;
802  snprintf(Header_Message, sizeof Header_Message,
803  "\nERREUR Fin de fichier dans l'entete !\n" ) ;
804  return ERR_EOF_IN_HEADER;
805  }
806 
807  /* --- FF --> termine le header --- */
808  else if ( in == FF ) {
809  str[i] = '\000' ;
810  go = FAUX ;
811  break ;
812  }
813 
814  /* --- CR et LF --> termine la ligne --- */
815  else if ( in == CR ) {
816  str[i] = '\000' ;
817  } else if ( in == LF ) {
818  str[i] = '\000' ;
819  break ;
820  }
821 
822  /* --- (0x20 < caractere > 0x7E) si non --> erreur --- */
823  else if ( in && ! isprint(in) ) {
824  if (!Header_Quiet)
825  fprintf(stderr,
826  "\nERREUR caract. non imprim. 0x%.2X dans "
827  "l'entete at byte number %ld\n", in, tell(file) );
828  snprintf(Header_Message, sizeof Header_Message,
829  "\nERREUR caract. non imprim. 0x%.2X dans l'entete !\n", in );
830  return ERR_UNPRINTABLE_CHAR;
831  } else {
832  str[i++] = (char) in ;
833  }
834 
835  }
836 
837  /* --- ----- ----- ----- sauve les commentaires ----- ----- --- */
838  for ( char* pt_1=str; *pt_1; pt_1++ ) {
839 
840  /* --- '*' ou '#' = commentaires ------> dans comment --- */
841  if ( *pt_1 == '*' || *pt_1 == '#') {
842  if ( *(pt_1+1) ) {
843  v_comment[com] = (char *)malloc( strlen(pt_1+1) + 1 ) ;
844  strcpy( v_comment[com++], pt_1+1 ) ;
845  if (com%NALLOC == 0)
846  v_comment = (char **)
847  realloc(v_comment, (com+NALLOC)*sizeof(char *));
848  v_comment[com] = NULL ;
849  bool_comment = VRAI;
850  }
851  *pt_1 = '\000' ;
852  break ;
853  }
854 
855  /* --- convertit majuscule un minuscules --- */
856  if (isupper(*pt_1)) *pt_1 = tolower( *pt_1 ) ;
857 
858  }
859 
860 
861  /* --- ----- ----- ----- analyse la ligne ----- ----- ----- --- */
862  for ( char *pt_1=strtok(str," ,;\t"); pt_1; pt_1=strtok(NULL," ,;\t") ) {
863 
864  /* --- separe la chaine de caract. --- */
865  /* --- pt_1 pointe au key_word ---- */
866  /* --- pt_2 pointe a la donne ---- */
867  char *pt_2;
868  for ( pt_2 = pt_1; *pt_2 != ':'; pt_2 ++ )
869  if ( ! *pt_2 ) {
870  if (!Header_Quiet)
871  fprintf(stderr,
872  "\nERREUR de syntaxe dans l'entete (%s)\n", pt_1);
873  snprintf(Header_Message, sizeof Header_Message,
874  "\nERREUR de syntaxe dans l'entete (%s)\n", pt_1);
875  return ERR_IGB_SYNTAX;
876  }
877 
878  *pt_2++ = '\000' ;
879 
880  /* --- recherche le mot-clef --- */
881  if ( ! strcmp( pt_1, "x" ) ) {
882  v_x = atoi( pt_2 ) ;
883  bool_x = VRAI;
884 
885  } else if ( ! strcmp( pt_1, "y" ) ) {
886  v_y = atoi( pt_2 ) ;
887  bool_y = VRAI;
888 
889  } else if ( ! strcmp( pt_1, "z" ) ) {
890  v_z = atoi( pt_2 ) ;
891  bool_z = VRAI;
892 
893  } else if ( ! strcmp( pt_1, "t" ) ) {
894  v_t = atoi( pt_2 ) ;
895  bool_t = VRAI;
896 
897  /* Pour compatibilite avec les vielles images */
898  } else if ( ! strcmp( pt_1, "sup" ) ) {
899  s += atoi( pt_2 ) ;
900  nosup = FAUX;
901 
902  } else if ( ! strcmp( pt_1, "type" ) ) {
903 
904  for( int htype=IGB_MIN_TYPE; htype<=IGB_MAX_TYPE; htype++ )
905  if ( !strcmp( pt_2, Header_Type[htype] ) ) {
906  v_type = htype;
907  }
908  bool_type = VRAI;
909 
910  } else if ( ! strcmp( pt_1, "taille" ) ) {
911  v_taille = atoi( pt_2 ) ;
912  bool_taille = VRAI;
913 
914  } else if ( ! strcmp( pt_1, "systeme" ) ) {
915  v_systeme = INCONNU;
916  for (i=0;i<N_SYSTEMES;i++) {
917  if ( ! strcmp( pt_2, Header_Systeme[i] ) ) {
918  v_systeme = Header_Systeme_No[i] ;
919  break;
920  }
921  }
922 
923  } else if ( ! strcmp( pt_1, "bin" ) ) {
924  v_bin = atoi( pt_2 ) ;
925  bool_bin = VRAI;
926 
927  } else if ( ! strcmp( pt_1, "trame" ) ) {
928 
929  if ( ! strcmp( pt_2, "c8" ) ) {
930  v_trame = C8 ;
931  } else if ( ! strcmp( pt_2, "c4" ) ) {
932  v_trame = C4 ;
933  } else if ( ! strcmp( pt_2, "hex" ) ) {
934  v_trame = HEX ;
935  } else if ( ! strcmp( pt_2, "hexedges" ) ) {
936  v_trame = HEXEDGES ;
937  } else if ( ! strcmp( pt_2, "hexbridges" ) ) {
938  v_trame = HEXBRIDGES ;
939  } else if ( ! strcmp( pt_2, "hexlines" ) ) {
940  v_trame = HEXLINES ;
941  } else if ( ! strcmp( pt_2, "hex2" ) ) {
942  v_trame = HEX2 ;
943  }
944  bool_trame = VRAI;
945 
946  } else if ( ! strcmp( pt_1, "num" ) ) {
947  v_num = atoi( pt_2 ) ;
948  bool_num = VRAI;
949 
950  } else if ( ! strcmp( pt_1, "comp" ) ) {
951  v_comp = atoi( pt_2 ) ;
952  bool_comp = VRAI;
953 
954  } else if ( ! strcmp( pt_1, "lut" ) ) {
955  v_lut = atoi( pt_2 ) ;
956  bool_lut = VRAI;
957 
958  } else if ( ! strcmp( pt_1, "dim_x" ) ) {
959  v_dim_x = atof( pt_2 ) ;
960  bool_dim_x = VRAI;
961 
962  } else if ( ! strcmp( pt_1, "dim_y" ) ) {
963  v_dim_y = atof( pt_2 ) ;
964  bool_dim_y = VRAI;
965 
966  } else if ( ! strcmp( pt_1, "dim_z" ) ) {
967  v_dim_z = atof( pt_2 ) ;
968  bool_dim_z = VRAI;
969 
970  } else if ( ! strcmp( pt_1, "dim_t" ) ) {
971  v_dim_t = atof( pt_2 ) ;
972  bool_dim_t = VRAI;
973 
974  } else if ( ! strcmp( pt_1, "inc_x" ) ) {
975  v_inc_x = atof( pt_2 ) ;
976  bool_inc_x = VRAI;
977 
978  } else if ( ! strcmp( pt_1, "inc_y" ) ) {
979  v_inc_y = atof( pt_2 ) ;
980  bool_inc_y = VRAI;
981 
982  } else if ( ! strcmp( pt_1, "inc_z" ) ) {
983  v_inc_z = atof( pt_2 ) ;
984  bool_inc_z = VRAI;
985 
986  } else if ( ! strcmp( pt_1, "inc_t" ) ) {
987  v_inc_t = atof( pt_2 ) ;
988  bool_inc_t = VRAI;
989 
990  } else if ( ! strcmp( pt_1, "org_x" ) ) {
991  v_org_x = atof( pt_2 ) ;
992  bool_org_x = VRAI;
993 
994  } else if ( ! strcmp( pt_1, "org_y" ) ) {
995  v_org_y = atof( pt_2 ) ;
996  bool_org_y = VRAI;
997 
998  } else if ( ! strcmp( pt_1, "org_z" ) ) {
999  v_org_z = atof( pt_2 ) ;
1000  bool_org_z = VRAI;
1001 
1002  } else if ( ! strcmp( pt_1, "org_t" ) ) {
1003  v_org_t = atof( pt_2 ) ;
1004  bool_org_t = VRAI;
1005 
1006  } else if ( ! strcmp( pt_1, "vect_z" ) ) {
1007  bool_vect_z = VRAI;
1008 
1009  } else if ( ! strcmp( pt_1, "unites_x" ) ) {
1010  strncpy( v_unites_x, pt_2, 40 ) ;
1011  bool_unites_x = VRAI;
1012 
1013  } else if ( ! strcmp( pt_1, "unites_y" ) ) {
1014  strncpy( v_unites_y, pt_2, 40 ) ;
1015  bool_unites_y = VRAI;
1016 
1017  } else if ( ! strcmp( pt_1, "unites_z" ) ) {
1018  strncpy( v_unites_z, pt_2, 40 ) ;
1019  bool_unites_z = VRAI;
1020 
1021  } else if ( ! strcmp( pt_1, "unites_t" ) ) {
1022  strncpy( v_unites_t, pt_2, 40 ) ;
1023  bool_unites_t = VRAI;
1024 
1025  } else if ( ! strcmp( pt_1, "epais" ) ) {
1026  v_epais = atof( pt_2 ) ;
1027  bool_epais = VRAI;
1028 
1029  } else if ( ! strcmp( pt_1, "unites" ) ) {
1030  strncpy( v_unites, pt_2, 40 ) ;
1031  bool_unites = VRAI;
1032 
1033  } else if ( ! strcmp( pt_1, "facteur" ) ) {
1034  v_facteur = atof( pt_2 ) ;
1035  bool_facteur = VRAI;
1036 
1037  } else if ( ! strcmp( pt_1, "zero" ) ) {
1038  v_zero = atof( pt_2 ) ;
1039  bool_zero = VRAI;
1040 
1041  } else if ( ! strcmp( pt_1, "struct" ) ) {
1042  strncpy( v_struct_desc, pt_2, 40 ) ;
1043  bool_struct_desc = VRAI;
1044 
1045  } else if ( ! strcmp( pt_1, "aut" ) ) {
1046  strncpy( v_aut_name, pt_2, 40 ) ;
1047  bool_aut_name = VRAI;
1048 
1049  } else if ( !strcmp( pt_1, "transparent" ) ) {
1050  strcpy( transstr, pt_2 );
1051  bool_transparent = VRAI;
1052 
1053  } else {
1054  if( is_deprecated( pt_1 ) ){
1055  if (!Header_Quiet)
1056  fprintf(stderr,"\nATTENTION: mot-clef %s obsolete !\n", pt_1 ) ;
1057  if( !strcmp( pt_1, "fac_t" ) ){
1058  v_fac_t = atof( pt_2 );
1059  bool_fac_t = VRAI;
1060  }
1061  } else {
1062  if (!Header_Quiet)
1063  fprintf(stderr,"\nATTENTION: mot-clef %s non reconnu !\n", pt_1 );
1064  snprintf(Header_Message, sizeof Header_Message,
1065  "\nATTENTION: mot-clef %s non reconnu !\n", pt_1 ) ;
1066  statut |= MOT_CLEF_INV;
1067  }
1068  }
1069  }
1070  }
1071  /* determine the transparent value if one was chosen */
1072  if ( bool_transparent ) {
1073  if ( strlen(transstr) != 2*Data_Size[v_type] ) {
1074  fprintf(stderr,"ATTENTION: ignoring invalid transparent value !\n");
1075  } else {
1076  char s[3], *p, *v;
1077  s[2] = '\0';
1078  v = (char *)(v_transparent = calloc( Data_Size[v_type], 1 ));
1079  for ( int i=0; i<Data_Size[v_type]; i++ ) {
1080  s[0] = transstr[i*2];
1081  s[1] = transstr[i*2+1];
1082  v[i] = strtol( s, &p, 16 );
1083  }
1084  }
1085  }
1086 
1087  /* --- l'info x y et type est obligatoire --- */
1088  if ( !bool_x || !bool_y || !bool_type ) {
1089  if (!Header_Quiet)
1090  fprintf(stderr, "\nERREUR x, y ou type non definis\n") ;
1091  snprintf(Header_Message, sizeof Header_Message, "\nERREUR x, y ou type non definis\n") ;
1092  return ERR_UNDEFINED_X_Y_TYPE ;
1093  }
1094 
1095  /* --- calcul des inc et dim --- */
1096  if ( bool_dim_x )
1097  if ( bool_inc_x ) {
1098  float dim_x = v_inc_x * v_x ;
1099  if ( dim_x != v_dim_x ) {
1100  if (!Header_Quiet) {
1101  fprintf(stderr, "\nATTENTION:\n") ;
1102  fprintf(stderr,
1103  "conflit entre x (%d) * inc_x (%.12g) = %.12g et dim_x (%.12g)\n",
1104  v_x, v_inc_x, dim_x, v_dim_x) ;
1105  }
1106  snprintf(Header_Message, sizeof Header_Message,
1107  "conflit entre x (%d) * inc_x (%.12g) = %.12g et dim_x (%.12g)\n",
1108  v_x, v_inc_x, dim_x, v_dim_x) ;
1109  statut = WARN_DIM_INCONSISTENT ;
1110  }
1111  } else {
1112  v_inc_x = v_dim_x / v_x ;
1113  bool_inc_x = VRAI;
1114  }
1115  else {
1116  v_dim_x = v_x * v_inc_x ;
1117  if ( bool_inc_x ) bool_dim_x = VRAI;
1118  }
1119 
1120  if ( bool_dim_y )
1121  if ( bool_inc_y ) {
1122  float dim_y = v_inc_y * v_y ;
1123  if ( dim_y != v_dim_y ) {
1124  if (!Header_Quiet) {
1125  fprintf(stderr, "\nATTENTION:\n") ;
1126  fprintf(stderr,
1127  "conflit entre y (%d) * inc_y (%.12g) = %.12g et dim_y (%.12g)\n",
1128  v_y, v_inc_y, dim_y, v_dim_y) ;
1129  }
1130  snprintf(Header_Message, sizeof Header_Message,
1131  "conflit entre y (%d) * inc_y (%.12g) = %.12g et dim_y (%.12g)\n",
1132  v_y, v_inc_y, dim_y, v_dim_y) ;
1133  statut = WARN_DIM_INCONSISTENT ;
1134  }
1135  } else {
1136  v_inc_y = v_dim_y / v_y ;
1137  bool_inc_y = VRAI;
1138  }
1139  else {
1140  v_dim_y = v_y * v_inc_y ;
1141  if ( bool_inc_y ) bool_dim_y = VRAI;
1142  }
1143 
1144  if ( bool_dim_z )
1145  if ( bool_inc_z ) {
1146  float dim_z = v_inc_z * v_z ;
1147  if ( dim_z != v_dim_z ) {
1148  if (!Header_Quiet) {
1149  fprintf(stderr, "\nATTENTION:\n") ;
1150  fprintf(stderr,
1151  "conflit entre z (%d) * inc_z (%.12g) = %.12g et dim_z (%.12g)\n",
1152  v_z, v_inc_z, dim_z, v_dim_z) ;
1153  }
1154  snprintf(Header_Message, sizeof Header_Message,
1155  "conflit entre z (%d) * inc_z (%.12g) = %.12g et dim_z (%.12g)\n",
1156  v_z, v_inc_z, dim_z, v_dim_z) ;
1157  statut = WARN_DIM_INCONSISTENT ;
1158  }
1159  } else {
1160  v_inc_z = v_dim_z / v_z ;
1161  bool_inc_z = VRAI;
1162  }
1163  else {
1164  v_dim_z = v_z * v_inc_z ;
1165  if ( bool_inc_z ) bool_dim_z = VRAI;
1166  }
1167 
1168  if( !bool_inc_t && bool_fac_t ) {
1169  bool_inc_t = VRAI;
1170  v_inc_t = v_fac_t;
1171  }
1172  if ( bool_dim_t )
1173  if ( bool_inc_t ) {
1174  float dim_t = v_inc_t * (v_t-1) ;
1175  if ( fabs(dim_t - v_dim_t) > v_inc_t ) {
1176  if (!Header_Quiet) {
1177  fprintf(stderr, "\nATTENTION:\n") ;
1178  fprintf(stderr,
1179  "conflit entre t (%d) * inc_t (%.12g) = %.12g et dim_t (%.12g)\n",
1180  v_t, v_inc_t, dim_t, v_dim_t) ;
1181  }
1182  snprintf(Header_Message, sizeof Header_Message,
1183  "conflit entre t (%d) * inc_t (%.12g) = %.12g et dim_t (%.12g)\n",
1184  v_t, v_inc_t, dim_t, v_dim_t) ;
1185  statut = WARN_DIM_INCONSISTENT ;
1186  }
1187  } else {
1188  v_inc_t = v_dim_t / (v_t - 1) ;
1189  bool_inc_t = VRAI;
1190  }
1191  else {
1192  v_dim_t = (v_t-1) * v_inc_t ;
1193  if ( bool_inc_t )
1194  bool_dim_t = VRAI;
1195  }
1196 
1197  if ( bool_taille ) {
1198  if (v_type!=IGB_STRUCTURE) {
1199  if (!Header_Quiet)
1200  fprintf(stderr,
1201  "\nERREUR taille redefinie pour type autre que structure\n") ;
1202  snprintf(Header_Message, sizeof Header_Message,
1203  "\nERREUR taille redefinie pour type autre que structure\n") ;
1204  return ERR_SIZE_REDEFINED;
1205  }
1206  } else {
1207  if (v_type==IGB_STRUCTURE) {
1208  if (!Header_Quiet)
1209  fprintf(stderr,
1210  "\nERREUR taille non definie pour type structure\n") ;
1211  snprintf(Header_Message, sizeof Header_Message,
1212  "\nERREUR taille non definie pour type structure\n") ;
1213  return ERR_SIZE_NOT_DEFINED;
1214  } else {
1215  v_taille = Data_Size[v_type];
1216  }
1217  }
1218 
1219  if (tell(file)!=1024) {
1220  gzseek( (gzFile)file, 1024, SEEK_SET );
1221  if (!Header_Quiet) {
1222  fprintf(stderr,
1223  "\nATTENTION: etiquette de grandeur non-standard: %ld \n", tell(file));
1224  }
1225  snprintf(Header_Message, sizeof Header_Message,
1226  "\nATTENTION: etiquette de grandeur non-standard \n");
1227  statut |= GRANDEUR_INV;
1228  } else {
1229  snprintf(Header_Message, sizeof Header_Message,
1230  "\nHeader_Read: Etiquette lue sans problemes\n");
1231  }
1232 
1233  if (bool_vect_z) {
1234  v_vect_z = (float *)malloc(v_z*sizeof(float));
1235  gzread((gzFile)file, v_vect_z, sizeof(float)*v_z);
1236  }
1237 
1238  return statut;
1239 }
1240 
1241 /*
1242  * add a comment line
1243  * clear comments is NULL is passed
1244  */
1245 void IGBheader::comment(char *ligne)
1246 {
1247  int i = 0, n_lignes = 0;
1248 
1249  if ( ligne == NULL ) {
1250  while ( v_comment[n_lignes] != NULL )
1251  free( v_comment[n_lignes++] );
1252  return;
1253  }
1254 
1255  while (v_comment[i++]!=NULL) n_lignes++;
1256  v_comment = (char **)realloc(v_comment, (n_lignes+2)*sizeof(char *));
1257 
1258  v_comment[n_lignes] = (char *)malloc(strlen(ligne)+1) ;
1259  strcpy(v_comment[n_lignes++], ligne) ;
1260  v_comment[n_lignes] = NULL;
1261  bool_comment = true;
1262 }
1263 
1264 const char *IGBheader::systemestr( void )
1265 {
1266  int i;
1267 
1268  for ( i=0; i<N_SYSTEMES; i++ )
1269  if ( Header_Systeme_No[i] == v_systeme )
1270  break;
1271 
1272  if ( i<N_SYSTEMES )
1273  return Header_Systeme[i];
1274  else
1275  return NULL;
1276 }
1277 
1278 
1279 void IGBheader::systeme( const char* s )
1280 {
1281  int i;
1282  for ( i=0; i<N_SYSTEMES; i++ )
1283  if ( !strncmp( Header_Systeme[i], s, strlen(s) ) )
1284  break;
1285 
1286  if ( i<N_SYSTEMES )
1287  v_systeme = Header_Systeme_No[i];
1288 }
1289 
1290 
1291 void IGBheader::type( char *datatype )
1292 {
1293  int tn=IGB_MIN_TYPE;
1294  while ( tn<=IGB_MAX_TYPE && strcmp(Header_Type[tn],datatype) )
1295  tn++;
1296  if ( tn<=IGB_MAX_TYPE )
1297  v_type = tn;
1298  else {
1299  cerr << "illegal data type specified for IGB header" << endl;
1300  exit(1);
1301  }
1302  bool_type = true;
1303 }
1304 
1305 
1306 // simple wrapper for ftell/gztell
1307 long IGBheader::tell( void *f )
1308 {
1309  if( gzipping )
1310  return gztell( (gzFile)f );
1311  else
1312  return ftell( (FILE*)f );
1313 }
1314 
1315 
1316 // simple wrapper so gzputs and fputs argument order jive
1317 int IGBheader::puts_fcn( void* f, char* s )
1318 {
1319  if ( gzipping ) {
1320  gzFile gzf=(gzFile)f;
1321  return gzputs( gzf, s );
1322  } else {
1323  FILE *fptr=(FILE *)f;
1324  return fputs( s, fptr );
1325  }
1326 }
1327 
1328 
1334 void IGBheader :: swab( void *data, int nd )
1335 {
1336  if ( data_size()==1 || v_type==IGB_RGBA )
1337  return;
1338 
1339  unsigned char *bp = (unsigned char *)data;
1340  int ds = data_size()/num_components();
1341 
1342  if ( nd<0 ) nd = v_x*v_y*v_z*v_t;
1343 
1344  nd *= num_components();
1345 
1346  switch ( ds ) {
1347  case 2:
1348  for ( int i=0; i<nd; i++ ) {
1349  unsigned char tmpb = bp[0];
1350  bp[0] = bp[1];
1351  bp[1] = tmpb;
1352  bp += data_size();
1353  }
1354  break;
1355  case 4:
1356  for ( int i=0; i<nd; i++ ) {
1357  unsigned char tmpb = bp[0];
1358  bp[0] = bp[3];
1359  bp[3] = tmpb;
1360  tmpb = bp[1];
1361  bp[1] = bp[2];
1362  bp[2] = tmpb;
1363  bp += data_size();
1364  }
1365  break;
1366  case 8:
1367  for ( int i=0; i<nd; i++ ) {
1368  unsigned char tmpb = bp[0];
1369  bp[0] = bp[7];
1370  bp[7] = tmpb;
1371  tmpb = bp[1];
1372  bp[1] = bp[6];
1373  bp[6] = tmpb;
1374  tmpb = bp[2];
1375  bp[2] = bp[5];
1376  bp[5] = tmpb;
1377  tmpb = bp[3];
1378  bp[3] = bp[4];
1379  bp[4] = tmpb;
1380  bp += data_size();
1381  }
1382  break;
1383  }
1384 }
1385 
1386 
1389 {
1390  float val=IGB_ENDIAN_VAL;
1391  unsigned char le_val[] = { IGB_LITTLE_END_REP },
1392  *pval = (unsigned char *)(&val);
1393 
1394  assert( sizeof(float) == 4 );
1395 
1396  if ( *pval == le_val[0] )
1397  return IGB_LITTLE_ENDIAN;
1398  else
1399  return IGB_BIG_ENDIAN;
1400 }
1401 
1402 } // namespace opencarp
1403 
#define WARN_DIM_INCONSISTENT
Definition: IGBheader.h:108
int Num_Components[]
Definition: IGBheader.cc:275
#define ERR_LINE_TOO_LONG
Definition: IGBheader.h:102
#define ERR_SIZE_REDEFINED
Definition: IGBheader.h:106
#define LF
Definition: IGBheader.cc:57
const char * Header_Systeme[]
Definition: IGBheader.cc:289
#define HEXLINES
Definition: IGBheader.cc:52
#define IGB_STRUCTURE
Definition: IGBheader.h:65
#define C8
Definition: IGBheader.cc:47
#define IGB_LITTLE_END_REP
Definition: IGBheader.h:98
#define GRANDEUR_INV
Definition: IGBheader.cc:41
bool Header_Quiet
Definition: IGBheader.cc:246
#define N_SYSTEMES
Definition: IGBheader.h:52
int my_fputs(FILE *, char *)
#define ERR_IGB_SYNTAX
Definition: IGBheader.h:104
#define C4
Definition: IGBheader.cc:48
IGBheader(gzFile a=NULL, bool read=false, bool quiet=false)
Definition: IGBheader.cc:322
const char * systemestr(void)
Definition: IGBheader.cc:1264
#define HEX2
Definition: IGBheader.cc:53
#define HEXBRIDGES
Definition: IGBheader.cc:51
void * fileptr(void)
Definition: IGBheader.h:251
#define FAUX
Definition: IGBheader.cc:240
#define CR
Definition: IGBheader.cc:59
#define VRAI
Definition: IGBheader.cc:237
#define MAX_TRAME
Definition: IGBheader.cc:54
void swab(void *, int nd=-1)
Definition: IGBheader.cc:1334
uint16_t short_float
Definition: short_float.h:48
#define FF
Definition: IGBheader.cc:58
#define IGB_BIG_ENDIAN
Definition: IGBheader.h:50
#define IGB_LITTLE_ENDIAN
Definition: IGBheader.h:51
#define L_MAX_ITEM
Definition: IGBheader.cc:235
char ** comment(void)
Definition: IGBheader.h:258
#define MAKE_CONSISTENT(D)
Definition: IGBheader.cc:305
unsigned int UInt
Definition: IGBheader.h:134
int systeme(void)
Definition: IGBheader.h:289
#define IGB_RGBA
Definition: IGBheader.h:64
#define NALLOC
Definition: IGBheader.h:45
long unsigned Header_Systeme_No[]
Definition: IGBheader.cc:282
#define ERR_EOF_IN_HEADER
Definition: IGBheader.h:101
#define HEXEDGES
Definition: IGBheader.cc:50
#define MAXL
Definition: IGBheader.cc:233
#define MOT_CLEF_INV
Definition: IGBheader.cc:40
int read(bool quiet=false)
Definition: IGBheader.cc:761
const char * deprecated[]
Definition: IGBheader.cc:259
#define IGB_ENDIAN_VAL
Definition: IGBheader.h:97
#define INCONNU
Definition: IGBheader.h:48
#define IGB_MAX_TYPE
Definition: IGBheader.h:79
#define IGB_MIN_TYPE
Definition: IGBheader.h:78
unsigned char Byte
Definition: IGBheader.h:124
const char * Header_Type[]
Definition: IGBheader.cc:249
#define ERR_UNDEFINED_X_Y_TYPE
Definition: IGBheader.h:105
#define ERR_UNPRINTABLE_CHAR
Definition: IGBheader.h:103
char Header_Message[256]
Definition: IGBheader.cc:247
#define ERR_SIZE_NOT_DEFINED
Definition: IGBheader.h:107
#define HEX
Definition: IGBheader.cc:49
unsigned short Data_Size[]
Definition: IGBheader.cc:264
#define N_MAX_ITEMS
Definition: IGBheader.cc:234