PLplot  5.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pdfutils.c
Go to the documentation of this file.
1 // xId: pdfutils.c 11966 2011-10-14 07:10:05Z andrewross $
2 //
3 // pdf_utils.c
4 //
5 // Copyright (C) 1992, 1993, 1994, 1995
6 // Maurice LeBrun mjl@dino.ph.utexas.edu
7 // Institute for Fusion Studies University of Texas at Austin
8 //
9 // Copyright (C) 2004 Joao Cardoso
10 // Copyright (C) 2004 Alan W. Irwin
11 // Copyright (C) 2004 Andrew Ross
12 //
13 // This file is part of PLplot.
14 //
15 // PLplot is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU Library General Public License as published
17 // by the Free Software Foundation; either version 2 of the License, or
18 // (at your option) any later version.
19 //
20 // PLplot is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU Library General Public License for more details.
24 //
25 // You should have received a copy of the GNU Library General Public License
26 // along with PLplot; if not, write to the Free Software
27 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 //
29 //--------------------------------------------------------------------------
30 //
31 
36 
37 #define NEED_PLDEBUG
38 #include "plplotP.h"
39 
40 static void print_ieeef( float *, U_LONG * );
41 static int pdf_wrx( const U_CHAR *x, long nitems, PDFstrm *pdfs );
42 
43 static int debug = 0;
44 
45 //--------------------------------------------------------------------------
46 // void pdf_set (string, value)
47 //
53 //--------------------------------------------------------------------------
54 
55 void
56 pdf_set( char *option, int value )
57 {
58  if ( !strcmp( option, "debug" ) )
59  debug = value;
60 }
61 
62 //--------------------------------------------------------------------------
63 // pdf_fopen()
64 //
71 //--------------------------------------------------------------------------
72 
73 PDFstrm *
74 pdf_fopen( const char *filename, const char *mode )
75 {
76  PDFstrm *pdfs;
77 
78  dbug_enter( "pdf_fopen" );
79 
80  pdfs = (PDFstrm *) malloc( sizeof ( PDFstrm ) );
81 
82  if ( pdfs != NULL )
83  {
84  pdfs->buffer = NULL;
85  pdfs->file = NULL;
86  pdfs->bp = 0;
87 #ifdef PLPLOT_USE_TCL_CHANNELS
88  pdfs->tclChan = NULL;
89  if ( 1 )
90  {
91  char new_mode[3];
92  int binary = 0;
93  char *m, *p;
94 
95  // Copy over the mode, removing 'b' if needed
96  for ( m = mode, p = new_mode; *m != 0; m++ )
97  {
98  if ( *m == 'b' )
99  {
100  binary = 1;
101  }
102  else
103  {
104  *p = *m;
105  p++;
106  }
107  }
108  *p = 0;
109 
110  pdfs->tclChan = Tcl_OpenFileChannel( NULL, filename, new_mode, 0 );
111  if ( pdfs->tclChan == NULL )
112  {
113  pdf_close( pdfs );
114  pdfs = NULL;
115  }
116  else
117  {
118  if ( binary )
119  {
120  Tcl_SetChannelOption( NULL, pdfs->tclChan, "-translation",
121  "binary" );
122  }
123  }
124  }
125 #else
126  pdfs->file = fopen( filename, mode );
127  if ( pdfs->file == NULL )
128  {
129  pdf_close( pdfs );
130  pdfs = NULL;
131  }
132 #endif
133  }
134 
135  return pdfs;
136 }
137 
138 //--------------------------------------------------------------------------
139 // pdf_bopen()
140 //
149 //--------------------------------------------------------------------------
150 
151 PDFstrm *
152 pdf_bopen( U_CHAR *buffer, size_t bufmax )
153 {
154  PDFstrm *pdfs;
155 
156  dbug_enter( "pdf_bopen" );
157 
158  pdfs = (PDFstrm *) malloc( sizeof ( PDFstrm ) );
159 
160  if ( pdfs != NULL )
161  {
162  pdfs->file = NULL;
163 #ifdef PLPLOT_USE_TCL_CHANNELS
164  pdfs->tclChan = NULL;
165 #endif
166  pdfs->bp = 0;
167 
168  if ( buffer == NULL )
169  {
170  if ( bufmax > 0 )
171  pdfs->bufmax = bufmax;
172  else
173  pdfs->bufmax = 2048;
174 
175  pdfs->buffer = (U_CHAR *) malloc( pdfs->bufmax );
176  if ( pdfs->buffer == NULL )
177  {
178  pdf_close( pdfs );
179  pdfs = NULL;
180  }
181  }
182  else
183  {
184  pdfs->bufmax = bufmax;
185  pdfs->buffer = buffer;
186  }
187  }
188 
189  return pdfs;
190 }
191 
192 //--------------------------------------------------------------------------
193 // pdf_finit()
194 //
201 //--------------------------------------------------------------------------
202 
203 PDFstrm *
204 pdf_finit( FILE *file )
205 {
206  PDFstrm *pdfs;
207 
208  dbug_enter( "pdf_finit" );
209 
210  pdfs = (PDFstrm *) malloc( sizeof ( PDFstrm ) );
211 
212  if ( pdfs != NULL )
213  {
214  pdfs->buffer = NULL;
215  pdfs->file = file;
216 #ifdef PLPLOT_USE_TCL_CHANNELS
217  pdfs->tclChan = NULL;
218 #endif
219  pdfs->bp = 0;
220  }
221 
222  return pdfs;
223 }
224 
225 //--------------------------------------------------------------------------
226 // pdf_close()
227 //
235 //--------------------------------------------------------------------------
236 
237 int
239 {
240  dbug_enter( "pdf_close" );
241 
242  if ( pdfs != NULL )
243  {
244  if ( pdfs->file != NULL )
245  {
246  fclose( pdfs->file );
247 #ifdef PLPLOT_USE_TCL_CHANNELS
248  }
249  else if ( pdfs->tclChan != NULL )
250  {
251  Tcl_Close( NULL, pdfs->tclChan );
252 #endif
253  }
254  else if ( pdfs->buffer != NULL )
255  {
256  free( (void *) pdfs->buffer );
257  }
258  free( (void *) pdfs );
259  }
260  return 0;
261 }
262 
263 //--------------------------------------------------------------------------
264 // int pdf_putc()
265 //
273 //--------------------------------------------------------------------------
274 
275 int
276 pdf_putc( int c, PDFstrm *pdfs )
277 {
278  int result = EOF;
279 
280  if ( pdfs->file != NULL )
281  {
282  result = putc( c, pdfs->file );
283  pdfs->bp++;
284 #ifdef PLPLOT_USE_TCL_CHANNELS
285  }
286  else if ( pdfs->tclChan != NULL )
287  {
288  result = Tcl_WriteChars( pdfs->tclChan, &c, 1 );
289  pdfs->bp++;
290 #endif
291  }
292  else if ( pdfs->buffer != NULL )
293  {
294  if ( pdfs->bp >= pdfs->bufmax )
295  {
296  pldebug( "pdf_putc",
297  "Increasing buffer to %d bytes\n", pdfs->bufmax );
298  pdfs->bufmax += 512;
299  if ( ( pdfs->buffer = (U_CHAR *) realloc( (void *) pdfs->buffer, pdfs->bufmax ) ) == NULL )
300  {
301  plexit( "pdf_putc: Insufficient memory" );
302  }
303  }
304  pdfs->buffer[pdfs->bp++] = (unsigned char) c;
305  result = c;
306  }
307  else
308  plexit( "pdf_putc: Illegal operation" );
309 
310  return result;
311 }
312 
313 //--------------------------------------------------------------------------
314 // int pdf_getc()
315 //
322 //--------------------------------------------------------------------------
323 
324 int
326 {
327  int result = EOF;
328 
329  if ( pdfs->file != NULL )
330  {
331  result = getc( pdfs->file );
332  pdfs->bp++;
333 #ifdef PLPLOT_USE_TCL_CHANNELS
334  }
335  else if ( pdfs->tclChan != NULL )
336  {
337  result = Tcl_Read( pdfs->tclChan, &result, 1 );
338  pdfs->bp++;
339 #endif
340  }
341  else if ( pdfs->buffer != NULL )
342  {
343  if ( pdfs->bp < pdfs->bufmax )
344  result = pdfs->buffer[pdfs->bp++];
345  }
346  else
347  plexit( "pdf_getc: Illegal operation" );
348 
349  return result;
350 }
351 
352 //--------------------------------------------------------------------------
353 // int pdf_ungetc()
354 //
362 //--------------------------------------------------------------------------
363 
364 int
365 pdf_ungetc( int c, PDFstrm *pdfs )
366 {
367  int result = EOF;
368 
369  if ( pdfs->file != NULL )
370  {
371  result = ungetc( c, pdfs->file );
372  if ( pdfs->bp > 0 )
373  pdfs->bp--;
374 #ifdef PLPLOT_USE_TCL_CHANNELS
375  }
376  else if ( pdfs->tclChan != NULL )
377  {
378  result = Tcl_Ungets( pdfs->tclChan, &c, 1, 0 );
379  if ( pdfs->bp > 0 )
380  pdfs->bp--;
381 #endif
382  }
383  else if ( pdfs->buffer != NULL )
384  {
385  if ( pdfs->bp > 0 )
386  {
387  pdfs->buffer[--pdfs->bp] = (unsigned char) c;
388  result = c;
389  }
390  }
391  else
392  plexit( "pdf_ungetc: Illegal operation" );
393 
394  return result;
395 }
396 
397 //--------------------------------------------------------------------------
398 // int pdf_wrx()
399 //
408 //--------------------------------------------------------------------------
409 
410 static int
411 pdf_wrx( const U_CHAR *x, long nitems, PDFstrm *pdfs )
412 {
413  int i, result = 0;
414 
415  if ( pdfs->file != NULL )
416  {
417  result = (int) fwrite( x, 1, (size_t) nitems, pdfs->file );
418  pdfs->bp += (size_t) nitems;
419 #ifdef PLPLOT_USE_TCL_CHANNELS
420  }
421  else if ( pdfs->tclChan != NULL )
422  {
423  result = Tcl_Write( pdfs->tclChan, x, nitems );
424  pdfs->bp += nitems;
425 #endif
426  }
427  else if ( pdfs->buffer != NULL )
428  {
429  for ( i = 0; i < nitems; i++ )
430  {
431  if ( pdfs->bp >= pdfs->bufmax )
432  {
433  pldebug( "pdf_wrx",
434  "Increasing buffer to %d bytes\n", pdfs->bufmax );
435  pdfs->bufmax += 512;
436  if ( ( pdfs->buffer = (U_CHAR *)
437  realloc( (void *) ( pdfs->buffer ), pdfs->bufmax ) ) == NULL )
438  {
439  plexit( "pdf_wrx: Insufficient memory" );
440  }
441  }
442  pdfs->buffer[pdfs->bp++] = x[i];
443  }
444  result = i;
445  }
446 
447  return result;
448 }
449 
450 //--------------------------------------------------------------------------
451 // int pdf_rdx()
452 //
461 //--------------------------------------------------------------------------
462 
463 int
464 pdf_rdx( U_CHAR *x, long nitems, PDFstrm *pdfs )
465 {
466  int i, result = 0;
467 
468  if ( pdfs->file != NULL )
469  {
470  result = (int) fread( x, 1, (size_t) nitems, pdfs->file );
471  pdfs->bp += (size_t) nitems;
472 #ifdef PLPLOT_USE_TCL_CHANNELS
473  }
474  else if ( pdfs->tclChan != NULL )
475  {
476  result = Tcl_ReadRaw( pdfs->tclChan, x, nitems );
477  pdfs->bp += nitems;
478 #endif
479  }
480  else if ( pdfs->buffer != NULL )
481  {
482  for ( i = 0; i < nitems; i++ )
483  {
484  if ( pdfs->bp > pdfs->bufmax )
485  break;
486  x[i] = pdfs->buffer[pdfs->bp++];
487  }
488  result = i;
489  }
490 
491  return result;
492 }
493 
494 //--------------------------------------------------------------------------
495 // pdf_wr_header()
496 //
506 //--------------------------------------------------------------------------
507 
508 int
509 pdf_wr_header( PDFstrm *pdfs, const char *header )
510 {
511  int i;
512 
513  dbug_enter( "pdf_wr_header" );
514 
515  for ( i = 0; i < 79; i++ )
516  {
517  if ( header[i] == '\0' )
518  break;
519  if ( pdf_putc( header[i], pdfs ) == EOF )
520  return PDF_WRERR;
521  }
522  if ( pdf_putc( '\n', pdfs ) == EOF )
523  return PDF_WRERR;
524 
525  return 0;
526 }
527 
528 //--------------------------------------------------------------------------
529 // int pdf_rd_header
530 //
539 //--------------------------------------------------------------------------
540 
541 int
542 pdf_rd_header( PDFstrm *pdfs, char *header )
543 {
544  int i, c;
545 
546  dbug_enter( "pdf_rd_header" );
547 
548  for ( i = 0; i < 79; i++ )
549  {
550  if ( ( c = pdf_getc( pdfs ) ) == EOF )
551  return PDF_RDERR;
552 
553  header[i] = (char) c;
554  if ( header[i] == '\n' )
555  break;
556  }
557  header[i] = '\0'; // NULL terminate
558  return 0;
559 }
560 
561 //--------------------------------------------------------------------------
562 // pdf_wr_string()
563 //
571 //--------------------------------------------------------------------------
572 
573 int
574 pdf_wr_string( PDFstrm *pdfs, const char *string )
575 {
576  int i;
577 
578  dbug_enter( "pdf_wr_string" );
579 
580  for ( i = 0; i <= (int) strlen( string ); i++ )
581  {
582  if ( pdf_putc( string[i], pdfs ) == EOF )
583  return PDF_WRERR;
584  }
585 
586  return 0;
587 }
588 
589 //--------------------------------------------------------------------------
590 // int pdf_rd_string
591 //
601 //--------------------------------------------------------------------------
602 
603 int
604 pdf_rd_string( PDFstrm *pdfs, char *string, int nmax )
605 {
606  int i, c;
607 
608  dbug_enter( "pdf_rd_string" );
609 
610  for ( i = 0; i < nmax; i++ )
611  {
612  if ( ( c = pdf_getc( pdfs ) ) == EOF )
613  return PDF_RDERR;
614 
615  string[i] = (char) c;
616  if ( c == '\0' )
617  break;
618  }
619  string[i] = '\0'; // handle boundary case
620  return 0;
621 }
622 
623 //--------------------------------------------------------------------------
624 // int pdf_wr_1byte()
625 //
633 //--------------------------------------------------------------------------
634 
635 int
637 {
638  U_CHAR x[1];
639 
640  x[0] = s;
641  if ( pdf_wrx( x, 1, pdfs ) != 1 )
642  return PDF_WRERR;
643 
644  return 0;
645 }
646 
647 //--------------------------------------------------------------------------
648 // int pdf_rd_1byte()
649 //
657 //--------------------------------------------------------------------------
658 
659 int
661 {
662  U_CHAR x[1];
663 
664  if ( !pdf_rdx( x, 1, pdfs ) )
665  return PDF_RDERR;
666 
667  *ps = ( (U_CHAR) x[0] );
668  return 0;
669 }
670 
671 //--------------------------------------------------------------------------
672 // pdf_wr_2bytes()
673 //
681 //--------------------------------------------------------------------------
682 
683 int
685 {
686  U_CHAR x[2];
687 
688  x[0] = (U_CHAR) ( (U_LONG) ( s & (U_LONG) 0x00FF ) );
689  x[1] = (U_CHAR) ( (U_LONG) ( s & (U_LONG) 0xFF00 ) >> 8 );
690 
691  if ( pdf_wrx( x, 2, pdfs ) != 2 )
692  return PDF_WRERR;
693 
694  return 0;
695 }
696 
697 //--------------------------------------------------------------------------
698 // pdf_rd_2bytes()
699 //
707 //--------------------------------------------------------------------------
708 
709 int
711 {
712  U_CHAR x[2];
713  U_SHORT xs;
714 
715  if ( !pdf_rdx( x, 2, pdfs ) )
716  return PDF_RDERR;
717 
718  *ps = 0;
719  xs = (U_SHORT) x[0];
720  *ps |= xs;
721  xs = (U_SHORT) ( (U_SHORT) x[1] << 8 );
722  *ps |= xs;
723 
724  return 0;
725 }
726 
727 //--------------------------------------------------------------------------
728 // pdf_wr_2nbytes()
729 //
738 //--------------------------------------------------------------------------
739 
740 int
742 {
743  PLINT i;
744  U_CHAR x[2];
745 
746  for ( i = 0; i < n; i++ )
747  {
748  x[0] = (U_CHAR) ( (U_LONG) ( s[i] & (U_LONG) 0x00FF ) );
749  x[1] = (U_CHAR) ( (U_LONG) ( s[i] & (U_LONG) 0xFF00 ) >> 8 );
750 
751  if ( pdf_wrx( x, 2, pdfs ) != 2 )
752  return PDF_WRERR;
753  }
754  return 0;
755 }
756 
757 //--------------------------------------------------------------------------
758 // pdf_rd_2nbytes()
759 //
768 //--------------------------------------------------------------------------
769 
770 int
772 {
773  PLINT i;
774  U_CHAR x[2];
775  U_SHORT xs;
776 
777  for ( i = 0; i < n; i++ )
778  {
779  if ( !pdf_rdx( x, 2, pdfs ) )
780  return PDF_RDERR;
781 
782  s[i] = 0;
783  xs = (U_SHORT) x[0];
784  s[i] |= xs;
785  xs = (U_SHORT) ( (U_SHORT) x[1] << 8 );
786  s[i] |= xs;
787  }
788  return 0;
789 }
790 
791 //--------------------------------------------------------------------------
792 // pdf_wr_4bytes()
793 //
801 //--------------------------------------------------------------------------
802 
803 int
805 {
806  U_CHAR x[4];
807 
808  x[0] = (U_CHAR) ( ( s & (U_LONG) 0x000000FF ) );
809  x[1] = (U_CHAR) ( ( s & (U_LONG) 0x0000FF00 ) >> 8 );
810  x[2] = (U_CHAR) ( ( s & (U_LONG) 0x00FF0000 ) >> 16 );
811  x[3] = (U_CHAR) ( ( s & (U_LONG) 0xFF000000 ) >> 24 );
812 
813  if ( pdf_wrx( x, 4, pdfs ) != 4 )
814  return PDF_WRERR;
815 
816  return 0;
817 }
818 
819 //--------------------------------------------------------------------------
820 // pdf_rd_4bytes()
821 //
829 //--------------------------------------------------------------------------
830 
831 int
833 {
834  U_CHAR x[4];
835 
836  if ( !pdf_rdx( x, 4, pdfs ) )
837  return PDF_RDERR;
838 
839  *ps = 0;
840  *ps |= (U_LONG) x[0];
841  *ps |= (U_LONG) x[1] << 8;
842  *ps |= (U_LONG) x[2] << 16;
843  *ps |= (U_LONG) x[3] << 24;
844 
845  return 0;
846 }
847 
848 //--------------------------------------------------------------------------
849 // Here is the IEEE floating point specification in both 32 bit and 64 bit
850 // precisions, from page 9 of "IEEE Standard for Binary Floating-Point
851 // Arithmetic", copyright 1985, IEEE Std 754-1985:
852 //
853 //
854 // Single Format
855 //
856 // msb means most significant bit
857 // lsb means least significant bit
858 //
859 // 1 8 23
860 //--------------------------------------------------------------------------
861 // | | | |
862 // | s | e | f |
863 // |___|________________|______________________________________________|
864 // msb lsb msb lsb
865 //
866 //
867 //
868 // Double Format
869 //
870 // msb means most significant bit
871 // lsb means least significant bit
872 //
873 // 1 11 52
874 //--------------------------------------------------------------------------
875 // | | | |
876 // | s | e | f |
877 // |___|________________|______________________________________________|
878 // msb lsb msb lsb
879 //
880 //
881 // (Thanks to: Andy Mai (mai@ncar.ucar.edu))
882 //
883 //
884 // According to "inmos: Transputer instruction set" the IEEE standard
885 // specifies the floating format as:
886 //
887 // s exp frac
888 //
889 // Where: s = sign bit (1 bit)
890 // exp = exponent (8 bits for 32 bit float / 11 bits for 64 bit float)
891 // frac = fraction (23 bits for 32 bit float / 52 bits for 64 bit float)
892 //
893 // value of (s exp frac) = (-1)^s * 1.frac * 2^(exp-bias) ; if exp not 0
894 // (-1)^s * 0.frac * 2^(1-bias) ; if exp = 0
895 //
896 // where bias = 127 for 32 bit float
897 // bias = 1023 for 64 bit float
898 //
899 // (Thanks to: Tom Bjorkholm(TBJORKHOLM@abo.fi))
900 //
901 //--------------------------------------------------------------------------
902 
903 //--------------------------------------------------------------------------
904 // int pdf_wr_ieeef()
905 //
913 //--------------------------------------------------------------------------
914 
915 int
916 pdf_wr_ieeef( PDFstrm *pdfs, float f )
917 {
918  double fdbl, fmant, f_new;
919  float fsgl, f_tmp;
920  int istat, ex, e_new, e_off;
921  const int bias = 127;
922  U_LONG value, s_ieee, e_ieee, f_ieee;
923 
924  if ( f == 0.0 )
925  {
926  value = 0;
927  return ( pdf_wr_4bytes( pdfs, value ) );
928  }
929  fdbl = f;
930  fsgl = (float) fdbl;
931  fmant = frexp( fdbl, &ex );
932 
933  if ( fmant < 0 )
934  s_ieee = 1;
935  else
936  s_ieee = 0;
937 
938  fmant = fabs( fmant );
939  f_new = 2 * fmant;
940  e_new = ex - 1;
941 
942  if ( e_new < 1 - bias )
943  {
944  e_off = e_new - ( 1 - bias );
945  e_ieee = 0;
946  f_tmp = (float) ( f_new * pow( (double) 2.0, (double) e_off ) );
947  }
948  else
949  {
950  e_ieee = (U_LONG) ( e_new + bias );
951  f_tmp = (float) ( f_new - 1 );
952  }
953  f_ieee = (U_LONG) ( f_tmp * 8388608 ); // multiply by 2^23
954 
955  if ( e_ieee > 255 )
956  {
957  if ( debug )
958  fprintf( stderr, "pdf_wr_ieeef: Warning -- overflow\n" );
959  e_ieee = 255;
960  }
961 
962  s_ieee = s_ieee << 31;
963  e_ieee = e_ieee << 23;
964 
965  value = s_ieee | e_ieee | f_ieee;
966 
967  if ( ( istat = pdf_wr_4bytes( pdfs, value ) ) )
968  return ( istat );
969 
970  if ( debug )
971  {
972  fprintf( stderr, "Float value (written): %g\n", fsgl );
973  print_ieeef( &fsgl, &value );
974  }
975 
976  return 0;
977 }
978 
979 //--------------------------------------------------------------------------
980 // int pdf_rd_ieeef()
981 //
989 //--------------------------------------------------------------------------
990 
991 int
992 pdf_rd_ieeef( PDFstrm *pdfs, float *pf )
993 {
994  double f_new, f_tmp;
995  float fsgl;
996  int istat, ex, bias = 127;
997  U_LONG value, s_ieee, e_ieee, f_ieee;
998 
999  if ( ( istat = pdf_rd_4bytes( pdfs, &value ) ) )
1000  return ( istat );
1001 
1002  s_ieee = ( value & (U_LONG) 0x80000000 ) >> 31;
1003  e_ieee = ( value & (U_LONG) 0x7F800000 ) >> 23;
1004  f_ieee = ( value & (U_LONG) 0x007FFFFF );
1005 
1006  f_tmp = (double) f_ieee / 8388608.0; // divide by 2^23
1007 
1008  if ( e_ieee == 0 )
1009  {
1010  ex = 1 - bias;
1011  f_new = f_tmp;
1012  }
1013  else
1014  {
1015  ex = (int) e_ieee - bias;
1016  f_new = 1.0 + f_tmp;
1017  }
1018 
1019  fsgl = (float) ( f_new * pow( 2.0, (double) ex ) );
1020  if ( s_ieee == 1 )
1021  fsgl = -fsgl;
1022 
1023  *pf = fsgl;
1024 
1025  if ( debug )
1026  {
1027  fprintf( stderr, "Float value (read): %g\n", fsgl );
1028  print_ieeef( &fsgl, &value );
1029  }
1030 
1031  return 0;
1032 }
1033 
1034 //--------------------------------------------------------------------------
1035 // print_ieeef()
1036 //
1045 //--------------------------------------------------------------------------
1046 
1047 static void
1048 print_ieeef( float *vx, U_LONG *vy )
1049 {
1050  int i;
1051  U_LONG f, *x = (U_LONG *) vx, *y = vy;
1052  char bitrep[33];
1053 
1054  bitrep[32] = '\0';
1055 
1056  f = *x;
1057  for ( i = 0; i < 32; i++ )
1058  {
1059  if ( f & 1 )
1060  bitrep[32 - i - 1] = '1';
1061  else
1062  bitrep[32 - i - 1] = '0';
1063  f = f >> 1;
1064  }
1065  fprintf( stderr, "Binary representation: " );
1066  fprintf( stderr, "%s\n", bitrep );
1067 
1068  f = *y;
1069  for ( i = 0; i < 32; i++ )
1070  {
1071  if ( f & 1 )
1072  bitrep[32 - i - 1] = '1';
1073  else
1074  bitrep[32 - i - 1] = '0';
1075  f = f >> 1;
1076  }
1077  fprintf( stderr, "Converted representation: " );
1078  fprintf( stderr, "%s\n\n", bitrep );
1079 
1080  return;
1081 }
1082 
1083 //--------------------------------------------------------------------------
1084 // plAlloc2dGrid()
1085 //
1101 //--------------------------------------------------------------------------
1102 
1103 void
1105 {
1106  PLINT i;
1107 
1108  if ( ( *f = (PLFLT **) calloc( (size_t) nx, sizeof ( PLFLT * ) ) ) == NULL )
1109  plexit( "Memory allocation error in \"plAlloc2dGrid\"" );
1110 
1111  for ( i = 0; i < nx; i++ )
1112  {
1113  if ( ( ( *f )[i] = (PLFLT *) calloc( (size_t) ny, sizeof ( PLFLT ) ) ) == NULL )
1114  plexit( "Memory allocation error in \"plAlloc2dGrid\"" );
1115  }
1116 }
1117 
1118 //--------------------------------------------------------------------------
1119 // Free2dGrid()
1120 //
1126 //--------------------------------------------------------------------------
1127 
1128 void
1130 {
1131  PLINT i;
1132 
1133  for ( i = 0; i < nx; i++ )
1134  free( (void *) f[i] );
1135 
1136  free( (void *) f );
1137 }
1138 
1139 //--------------------------------------------------------------------------
1140 // MinMax2dGrid()
1141 //
1151 //--------------------------------------------------------------------------
1152 
1153 void
1154 plMinMax2dGrid( const PLFLT * const*f, PLINT nx, PLINT ny, PLFLT *fnmax, PLFLT *fnmin )
1155 {
1156  int i, j;
1157  PLFLT m, M;
1158 
1159  if ( !isfinite( f[0][0] ) )
1160  {
1161  M = -HUGE_VAL;
1162  m = HUGE_VAL;
1163  }
1164  else
1165  M = m = f[0][0];
1166 
1167  for ( i = 0; i < nx; i++ )
1168  {
1169  for ( j = 0; j < ny; j++ )
1170  {
1171  if ( !isfinite( f[i][j] ) )
1172  continue;
1173  if ( f[i][j] > M )
1174  M = f[i][j];
1175  if ( f[i][j] < m )
1176  m = f[i][j];
1177  }
1178  }
1179  *fnmax = M;
1180  *fnmin = m;
1181 }