Back to index

plt-scheme  4.2.1
Defines | Functions | Variables
pdfutils.c File Reference
#include "plplotP.h"

Go to the source code of this file.

Defines

#define NEED_PLDEBUG

Functions

static void print_ieeef (void *, void *)
static int pdf_wrx (const U_CHAR *x, long nitems, PDFstrm *pdfs)
void pdf_set (char *option, int value)
PDFstrmpdf_fopen (char *filename, char *mode)
PDFstrmpdf_bopen (U_CHAR *buffer, long bufmax)
PDFstrmpdf_finit (FILE *file)
int pdf_close (PDFstrm *pdfs)
int pdf_putc (int c, PDFstrm *pdfs)
int pdf_getc (PDFstrm *pdfs)
int pdf_ungetc (int c, PDFstrm *pdfs)
int pdf_rdx (U_CHAR *x, long nitems, PDFstrm *pdfs)
int pdf_wr_header (PDFstrm *pdfs, char *header)
int pdf_rd_header (PDFstrm *pdfs, char *header)
int pdf_wr_string (PDFstrm *pdfs, const char *string)
int pdf_rd_string (PDFstrm *pdfs, char *string, int nmax)
int pdf_wr_1byte (PDFstrm *pdfs, U_CHAR s)
int pdf_rd_1byte (PDFstrm *pdfs, U_CHAR *ps)
int pdf_wr_2bytes (PDFstrm *pdfs, U_SHORT s)
int pdf_rd_2bytes (PDFstrm *pdfs, U_SHORT *ps)
int pdf_wr_2nbytes (PDFstrm *pdfs, U_SHORT *s, PLINT n)
int pdf_rd_2nbytes (PDFstrm *pdfs, U_SHORT *s, PLINT n)
int pdf_wr_4bytes (PDFstrm *pdfs, U_LONG s)
int pdf_rd_4bytes (PDFstrm *pdfs, U_LONG *ps)
int pdf_wr_ieeef (PDFstrm *pdfs, float f)
int pdf_rd_ieeef (PDFstrm *pdfs, float *pf)
void plAlloc2dGrid (PLFLT ***f, PLINT nx, PLINT ny)
void plFree2dGrid (PLFLT **f, PLINT nx, PLINT ny)
void plMinMax2dGrid (PLFLT **f, PLINT nx, PLINT ny, PLFLT *fmax, PLFLT *fmin)

Variables

static int debug = 0

Define Documentation

#define NEED_PLDEBUG

Definition at line 29 of file pdfutils.c.


Function Documentation

PDFstrm* pdf_bopen ( U_CHAR buffer,
long  bufmax 
)

Definition at line 118 of file pdfutils.c.

{
    PDFstrm *pdfs;

    dbug_enter("pdf_bopen");

    pdfs = (PDFstrm *) malloc(sizeof(PDFstrm));

    if (pdfs != NULL) {
       pdfs->file = NULL;
#ifdef PLPLOT_USE_TCL_CHANNELS
       pdfs->tclChan = NULL;
#endif
       pdfs->bp = 0;

       if (buffer == NULL) {
           if (bufmax > 0)
              pdfs->bufmax = bufmax;
           else
              pdfs->bufmax = 2048;

           pdfs->buffer = (U_CHAR *) malloc(pdfs->bufmax);
           if (pdfs->buffer == NULL) {
              pdf_close(pdfs);
              pdfs = NULL;
           }
       }
       else {
           pdfs->bufmax = bufmax;
           pdfs->buffer = buffer;
       }
    }

    return pdfs;
}

Here is the call graph for this function:

int pdf_close ( PDFstrm pdfs)

Definition at line 190 of file pdfutils.c.

{
    dbug_enter("pdf_close");

    if (pdfs != NULL) {
       if (pdfs->file != NULL) {
           fclose(pdfs->file);
#ifdef PLPLOT_USE_TCL_CHANNELS
       } else if (pdfs->tclChan != NULL) {
           Tcl_Close(NULL, pdfs->tclChan);
#endif
       } else if (pdfs->buffer != NULL) {
           free ((void *) pdfs->buffer);
       }
       free((void *) pdfs);
    }
    return 0;
}

Here is the caller graph for this function:

PDFstrm* pdf_finit ( FILE *  file)

Definition at line 162 of file pdfutils.c.

{
    PDFstrm *pdfs;

    dbug_enter("pdf_finit");

    pdfs = (PDFstrm *) malloc(sizeof(PDFstrm));

    if (pdfs != NULL) {
       pdfs->buffer = NULL;
       pdfs->file = file;
#ifdef PLPLOT_USE_TCL_CHANNELS
       pdfs->tclChan = NULL;
#endif
       pdfs->bp = 0;
    }

    return pdfs;
}
PDFstrm* pdf_fopen ( char *  filename,
char *  mode 
)

Definition at line 58 of file pdfutils.c.

{
    PDFstrm *pdfs;

    dbug_enter("pdf_fopen");

    pdfs = (PDFstrm *) malloc(sizeof(PDFstrm));

    if (pdfs != NULL) {
       pdfs->buffer = NULL;
       pdfs->file = NULL;
#ifdef PLPLOT_USE_TCL_CHANNELS
       pdfs->tclChan = NULL;
       if (1) {
           char new_mode[3];
           int binary = 0;
           char *m, *p;
           
           /* Copy over the mode, removing 'b' if needed */
           for (m = mode, p = new_mode; *m != 0; m++) {
               if (*m == 'b') {
                   binary = 1;
               } else {
                  *p = *m;
                  p++;
               }
           }
           *p = 0;
           
           pdfs->tclChan = Tcl_OpenFileChannel(NULL, filename, new_mode, 0);
           if (pdfs->tclChan == NULL) {
              pdf_close(pdfs);
              pdfs = NULL;
           } else {
              if (binary) {
                  Tcl_SetChannelOption(NULL, pdfs->tclChan, "-translation", 
                                    "binary");
              }
           }
       }
#else
       pdfs->file = fopen(filename, mode);
       if (pdfs->file == NULL) {
           pdf_close(pdfs);
           pdfs = NULL;
       }
#endif
    }

    return pdfs;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pdf_getc ( PDFstrm pdfs)

Definition at line 252 of file pdfutils.c.

{
    int result = EOF;

    if (pdfs->file != NULL) {
       result = getc(pdfs->file);
       pdfs->bp++;
#ifdef PLPLOT_USE_TCL_CHANNELS
    } else if (pdfs->tclChan != NULL) {
       result = Tcl_Read(pdfs->tclChan, &result, 1);
       pdfs->bp++;
#endif
    } else if (pdfs->buffer != NULL) {
       if (pdfs->bp < pdfs->bufmax)
           result = pdfs->buffer[pdfs->bp++];
    }
    else
       plexit("pdf_getc: Illegal operation");

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pdf_putc ( int  c,
PDFstrm pdfs 
)

Definition at line 216 of file pdfutils.c.

{
    int result = EOF;

    if (pdfs->file != NULL) {
       result = putc(c, pdfs->file);
       pdfs->bp++;
#ifdef PLPLOT_USE_TCL_CHANNELS
    } else if (pdfs->tclChan != NULL) {
       result = Tcl_WriteChars(pdfs->tclChan, &c, 1);
       pdfs->bp++;
#endif
    } else if (pdfs->buffer != NULL) {
       if (pdfs->bp >= pdfs->bufmax) {
           pldebug("pdf_putc",
                  "Increasing buffer to %d bytes\n", pdfs->bufmax);
           pdfs->bufmax += 512;
           pdfs->buffer = (U_CHAR *)
              realloc((void *) pdfs->buffer, pdfs->bufmax);
       }
       pdfs->buffer[pdfs->bp++] = c;
       result = c;
    }
    else
       plexit("pdf_putc: Illegal operation");

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pdf_rd_1byte ( PDFstrm pdfs,
U_CHAR ps 
)

Definition at line 499 of file pdfutils.c.

{
    U_CHAR x[1];

    if ( ! pdf_rdx(x, 1, pdfs))
       return PDF_RDERR;

    *ps = ((U_CHAR) x[0]);
    return 0;
}

Here is the call graph for this function:

int pdf_rd_2bytes ( PDFstrm pdfs,
U_SHORT ps 
)

Definition at line 537 of file pdfutils.c.

{
    U_CHAR x[2];

    if ( ! pdf_rdx(x, 2, pdfs))
       return PDF_RDERR;

    *ps = 0;
    *ps |= (U_LONG) x[0];
    *ps |= (U_LONG) x[1] << 8;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pdf_rd_2nbytes ( PDFstrm pdfs,
U_SHORT s,
PLINT  n 
)

Definition at line 580 of file pdfutils.c.

{
    PLINT i;
    U_CHAR x[2];

    for (i = 0; i < n; i++) {
       if ( ! pdf_rdx(x, 2, pdfs))
           return PDF_RDERR;

       s[i] = 0;
       s[i] |= (U_SHORT) x[0];
       s[i] |= (U_SHORT) x[1] << 8;
    }
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pdf_rd_4bytes ( PDFstrm pdfs,
U_LONG ps 
)

Definition at line 625 of file pdfutils.c.

{
    U_CHAR x[4];

    if ( ! pdf_rdx(x, 4, pdfs))
       return PDF_RDERR;

    *ps = 0;
    *ps |= (U_LONG) x[0];
    *ps |= (U_LONG) x[1] << 8;
    *ps |= (U_LONG) x[2] << 16;
    *ps |= (U_LONG) x[3] << 24;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pdf_rd_header ( PDFstrm pdfs,
char *  header 
)

Definition at line 409 of file pdfutils.c.

{
    int i, c;

    dbug_enter("pdf_rd_header");

    for (i = 0; i < 79; i++) {
       if ((c = pdf_getc(pdfs)) == EOF)
           return PDF_RDERR;

       header[i] = c;
       if (header[i] == '\n')
           break;
    }
    header[i] = '\0';              /* NULL terminate */
    return 0;
}

Here is the call graph for this function:

int pdf_rd_ieeef ( PDFstrm pdfs,
float *  pf 
)

Definition at line 766 of file pdfutils.c.

{
    double f_new, f_tmp;
    float fsgl;
    int istat, exp, bias = 127;
    U_LONG value, s_ieee, e_ieee, f_ieee;

    if ((istat = pdf_rd_4bytes(pdfs, &value)))
       return (istat);

    s_ieee = (value & (U_LONG) 0x80000000) >> 31;
    e_ieee = (value & (U_LONG) 0x7F800000) >> 23;
    f_ieee = (value & (U_LONG) 0x007FFFFF);

    f_tmp = (double) f_ieee / 8388608.0;  /* divide by 2^23 */

    if (e_ieee == 0) {
       exp = 1 - bias;
       f_new = f_tmp;
    }
    else {
       exp = (int) e_ieee - bias;
       f_new = 1.0 + f_tmp;
    }

    fsgl = f_new * pow(2.0, (double) exp);
    if (s_ieee == 1)
       fsgl = -fsgl;

    *pf = fsgl;

    if (debug) {
       fprintf(stderr, "Float value (read):      %g\n", fsgl);
       print_ieeef(&fsgl, &value);
    }

    return 0;
}

Here is the call graph for this function:

int pdf_rd_string ( PDFstrm pdfs,
char *  string,
int  nmax 
)

Definition at line 456 of file pdfutils.c.

{
    int i, c;

    dbug_enter("pdf_rd_string");

    for (i = 0; i < nmax; i++) {
       if ((c = pdf_getc(pdfs)) == EOF)
           return PDF_RDERR;

       string[i] = c;
       if (c == '\0')
           break;
    }
    string[i] = '\0';              /* handle boundary case */
    return 0;
}

Here is the call graph for this function:

int pdf_rdx ( U_CHAR x,
long  nitems,
PDFstrm pdfs 
)

Definition at line 350 of file pdfutils.c.

{
    int i, result = 0;

    if (pdfs->file != NULL) {
       result = fread(x, 1, nitems, pdfs->file);
       pdfs->bp += nitems;
#ifdef PLPLOT_USE_TCL_CHANNELS
    } else if (pdfs->tclChan != NULL) {
       result = Tcl_ReadRaw(pdfs->tclChan, x, nitems);
       pdfs->bp += nitems;
#endif
    } else if (pdfs->buffer != NULL) {
       for (i = 0; i < nitems; i++) {
           if (pdfs->bp > pdfs->bufmax)
              break;
           x[i] = pdfs->buffer[pdfs->bp++];
       }
       result = i;
    }

    return result;
}

Here is the caller graph for this function:

void pdf_set ( char *  option,
int  value 
)

Definition at line 44 of file pdfutils.c.

{
    if ( ! strcmp(option, "debug"))
       debug = value;
}
int pdf_ungetc ( int  c,
PDFstrm pdfs 
)

Definition at line 281 of file pdfutils.c.

{
    int result = EOF;

    if (pdfs->file != NULL) {
       result = ungetc(c, pdfs->file);
       if (pdfs->bp > 0) 
           pdfs->bp--;
#ifdef PLPLOT_USE_TCL_CHANNELS
    } else if (pdfs->tclChan != NULL) {
       result = Tcl_Ungets(pdfs->tclChan, &c, 1, 0);
       if (pdfs->bp > 0) 
           pdfs->bp--;
#endif
    } else if (pdfs->buffer != NULL) {
       if (pdfs->bp > 0) {
           pdfs->buffer[--pdfs->bp] = c;
           result = c;
       }
    }
    else
       plexit("pdf_ungetc: Illegal operation");

    return result;
}

Here is the call graph for this function:

int pdf_wr_1byte ( PDFstrm pdfs,
U_CHAR  s 
)

Definition at line 481 of file pdfutils.c.

{
    U_CHAR x[1];

    x[0] = s;
    if (pdf_wrx(x, 1, pdfs) != 1)
       return PDF_WRERR;

    return 0;
}

Here is the call graph for this function:

int pdf_wr_2bytes ( PDFstrm pdfs,
U_SHORT  s 
)

Definition at line 517 of file pdfutils.c.

{
    U_CHAR x[2];

    x[0] = (U_CHAR) ((U_LONG) (s & (U_LONG) 0x00FF));
    x[1] = (U_CHAR) ((U_LONG) (s & (U_LONG) 0xFF00) >> 8);

    if (pdf_wrx(x, 2, pdfs) != 2)
       return PDF_WRERR;

    return 0;
}

Here is the call graph for this function:

int pdf_wr_2nbytes ( PDFstrm pdfs,
U_SHORT s,
PLINT  n 
)

Definition at line 558 of file pdfutils.c.

{
    PLINT i;
    U_CHAR x[2];

    for (i = 0; i < n; i++) {
       x[0] = (U_CHAR) ((U_LONG) (s[i] & (U_LONG) 0x00FF));
       x[1] = (U_CHAR) ((U_LONG) (s[i] & (U_LONG) 0xFF00) >> 8);

       if (pdf_wrx(x, 2, pdfs) != 2)
           return PDF_WRERR;
    }
    return 0;
}

Here is the call graph for this function:

int pdf_wr_4bytes ( PDFstrm pdfs,
U_LONG  s 
)

Definition at line 603 of file pdfutils.c.

{
    U_CHAR x[4];

    x[0] = (U_CHAR) ((s & (U_LONG) 0x000000FF));
    x[1] = (U_CHAR) ((s & (U_LONG) 0x0000FF00) >> 8);
    x[2] = (U_CHAR) ((s & (U_LONG) 0x00FF0000) >> 16);
    x[3] = (U_CHAR) ((s & (U_LONG) 0xFF000000) >> 24);

    if (pdf_wrx(x, 4, pdfs) != 4)
       return PDF_WRERR;

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pdf_wr_header ( PDFstrm pdfs,
char *  header 
)

Definition at line 383 of file pdfutils.c.

{
    int i;

    dbug_enter("pdf_wr_header");

    for (i = 0; i < 79; i++) {
       if (header[i] == '\0')
           break;
       if (pdf_putc(header[i], pdfs) == EOF)
           return PDF_WRERR;
    }
    if (pdf_putc('\n', pdfs) == EOF)
       return PDF_WRERR;

    return 0;
}

Here is the call graph for this function:

int pdf_wr_ieeef ( PDFstrm pdfs,
float  f 
)

Definition at line 703 of file pdfutils.c.

{
    double fdbl, fmant, f_new;
    float fsgl, f_tmp;
    int istat, exp, e_new, e_off, bias = 127;
    U_LONG value, s_ieee, e_ieee, f_ieee;

    if (f == 0.0) {
       value = 0;
       return (pdf_wr_4bytes(pdfs, value));
    }
    fsgl = fdbl = f;
    fmant = frexp(fdbl, &exp);

    if (fmant < 0)
       s_ieee = 1;
    else
       s_ieee = 0;

    fmant = fabs(fmant);
    f_new = 2 * fmant;
    e_new = exp - 1;

    if (e_new < 1 - bias) {
       e_off = e_new - (1 - bias);
       e_ieee = 0;
       f_tmp = f_new * pow((double) 2.0, (double) e_off);
    }
    else {
       e_ieee = e_new + bias;
       f_tmp = f_new - 1;
    }
    f_ieee = f_tmp * 8388608;             /* multiply by 2^23 */

    if (e_ieee > 255) {
       if (debug)
           fprintf(stderr, "pdf_wr_ieeef: Warning -- overflow\n");
       e_ieee = 255;
    }

    s_ieee = s_ieee << 31;
    e_ieee = e_ieee << 23;

    value = s_ieee | e_ieee | f_ieee;

    if ((istat = pdf_wr_4bytes(pdfs, value)))
       return (istat);

    if (debug) {
       fprintf(stderr, "Float value (written):      %g\n", fsgl);
       print_ieeef(&fsgl, &value);
    }

    return 0;
}

Here is the call graph for this function:

int pdf_wr_string ( PDFstrm pdfs,
const char *  string 
)

Definition at line 434 of file pdfutils.c.

{
    int i;

    dbug_enter("pdf_wr_string");

    for (i = 0; i <= strlen(string); i++) {
       if (pdf_putc(string[i], pdfs) == EOF)
           return PDF_WRERR;
    }

    return 0;
}

Here is the call graph for this function:

static int pdf_wrx ( const U_CHAR x,
long  nitems,
PDFstrm pdfs 
) [static]

Definition at line 314 of file pdfutils.c.

{
    int i, result = 0;

    if (pdfs->file != NULL) {
       result = fwrite(x, 1, nitems, pdfs->file);
       pdfs->bp += nitems;
#ifdef PLPLOT_USE_TCL_CHANNELS
    } else if (pdfs->tclChan != NULL) {
       result = Tcl_Write(pdfs->tclChan, x, nitems);
       pdfs->bp += nitems;
#endif
    } else if (pdfs->buffer != NULL) {
       for (i = 0; i < nitems; i++) {
           if (pdfs->bp >= pdfs->bufmax) {
              pldebug("pdf_wrx",
                     "Increasing buffer to %d bytes\n", pdfs->bufmax);
              pdfs->bufmax += 512;
              pdfs->buffer = (U_CHAR *)
                  realloc((void *) (pdfs->buffer), pdfs->bufmax);
           }
           pdfs->buffer[pdfs->bp++] = x[i];
       }
       result = i;
    }

    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void plAlloc2dGrid ( PLFLT ***  f,
PLINT  nx,
PLINT  ny 
)

Definition at line 864 of file pdfutils.c.

{
    PLINT i;

    if ((*f = (PLFLT **) calloc(nx, sizeof(PLFLT *)))==NULL)
        plexit("Memory allocation error in \"plAlloc2dGrid\"");

    for (i = 0; i < nx; i++) {
       if (((*f)[i] = (PLFLT *) calloc(ny ,sizeof(PLFLT)))==NULL)
          plexit("Memory allocation error in \"plAlloc2dGrid\"");
    }

}

Here is the call graph for this function:

void plFree2dGrid ( PLFLT **  f,
PLINT  nx,
PLINT  ny 
)

Definition at line 885 of file pdfutils.c.

{
    PLINT i;

    for (i = 0; i < nx; i++)
       free((void *) f[i]);

    free((void *) f);
}
void plMinMax2dGrid ( PLFLT **  f,
PLINT  nx,
PLINT  ny,
PLFLT fmax,
PLFLT fmin 
)

Definition at line 902 of file pdfutils.c.

{
    int i, j;
    PLFLT m, M;

    M = m = f[0][0];

    for (i = 0; i < nx; i++) {
       for (j = 0; j < ny; j++) {
           if (f[i][j] > M) M = f[i][j];
           if (f[i][j] < m) m = f[i][j];
       }
    }
    *fmax = M;
    *fmin = m;
}
static void print_ieeef ( void vx,
void vy 
) [static]

Definition at line 815 of file pdfutils.c.

{
    int i;
    U_LONG f, *x = (U_LONG *) vx, *y = (U_LONG *) vy;
    char bitrep[33];

    bitrep[32] = '\0';

    f = *x;
    for (i = 0; i < 32; i++) {
       if (f & 1)
           bitrep[32 - i - 1] = '1';
       else
           bitrep[32 - i - 1] = '0';
       f = f >> 1;
    }
    fprintf(stderr, "Binary representation:      ");
    fprintf(stderr, "%s\n", bitrep);

    f = *y;
    for (i = 0; i < 32; i++) {
       if (f & 1)
           bitrep[32 - i - 1] = '1';
       else
           bitrep[32 - i - 1] = '0';
       f = f >> 1;
    }
    fprintf(stderr, "Converted representation:   ");
    fprintf(stderr, "%s\n\n", bitrep);

    return;
}

Here is the caller graph for this function:


Variable Documentation

int debug = 0 [static]

Definition at line 35 of file pdfutils.c.