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