| 1 | #ifndef lint | 
| 2 | static const char       RCSid[] = "$Id: resolu.c,v 2.8 2023/06/21 15:43:16 greg Exp $"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | * Read and write image resolutions. | 
| 6 | * | 
| 7 | * Externals declared in resolu.h | 
| 8 | */ | 
| 9 |  | 
| 10 | #include "copyright.h" | 
| 11 |  | 
| 12 | #include <stdlib.h> | 
| 13 | #include <stdio.h> | 
| 14 |  | 
| 15 | #include "resolu.h" | 
| 16 |  | 
| 17 |  | 
| 18 | char  resolu_buf[RESOLU_BUFLEN];        /* resolution line buffer */ | 
| 19 |  | 
| 20 |  | 
| 21 | void | 
| 22 | fputresolu(                             /* put out picture dimensions */ | 
| 23 | int  ord,               /* scanline ordering */ | 
| 24 | int  sl, int ns,        /* scanline length and number */ | 
| 25 | FILE  *fp | 
| 26 | ) | 
| 27 | { | 
| 28 | RESOLU  rs; | 
| 29 |  | 
| 30 | if ((rs.rt = ord) & YMAJOR) { | 
| 31 | rs.xr = sl; | 
| 32 | rs.yr = ns; | 
| 33 | } else { | 
| 34 | rs.xr = ns; | 
| 35 | rs.yr = sl; | 
| 36 | } | 
| 37 | fputsresolu(&rs, fp); | 
| 38 | } | 
| 39 |  | 
| 40 |  | 
| 41 | int | 
| 42 | fgetresolu(                             /* get picture dimensions */ | 
| 43 | int  *sl, int *ns,              /* scanline length and number */ | 
| 44 | FILE  *fp | 
| 45 | ) | 
| 46 | { | 
| 47 | RESOLU  rs; | 
| 48 |  | 
| 49 | if (!fgetsresolu(&rs, fp)) | 
| 50 | return(-1); | 
| 51 | if (rs.rt & YMAJOR) { | 
| 52 | *sl = rs.xr; | 
| 53 | *ns = rs.yr; | 
| 54 | } else { | 
| 55 | *sl = rs.yr; | 
| 56 | *ns = rs.xr; | 
| 57 | } | 
| 58 | return(rs.rt); | 
| 59 | } | 
| 60 |  | 
| 61 |  | 
| 62 | char * | 
| 63 | resolu2str(                     /* convert resolution struct to line */ | 
| 64 | char  *buf, | 
| 65 | RESOLU  *rp | 
| 66 | ) | 
| 67 | { | 
| 68 | if (rp->rt&YMAJOR) | 
| 69 | sprintf(buf, "%cY %8d %cX %8d\n", | 
| 70 | rp->rt&YDECR ? '-' : '+', rp->yr, | 
| 71 | rp->rt&XDECR ? '-' : '+', rp->xr); | 
| 72 | else | 
| 73 | sprintf(buf, "%cX %8d %cY %8d\n", | 
| 74 | rp->rt&XDECR ? '-' : '+', rp->xr, | 
| 75 | rp->rt&YDECR ? '-' : '+', rp->yr); | 
| 76 | return(buf); | 
| 77 | } | 
| 78 |  | 
| 79 |  | 
| 80 | int | 
| 81 | str2resolu(                     /* convert resolution line to struct */ | 
| 82 | RESOLU  *rp, | 
| 83 | char  *buf | 
| 84 | ) | 
| 85 | { | 
| 86 | char  *xndx, *yndx; | 
| 87 | char  *cp; | 
| 88 |  | 
| 89 | if (buf == NULL) | 
| 90 | return(0); | 
| 91 | xndx = yndx = NULL; | 
| 92 | for (cp = buf; *cp; cp++) | 
| 93 | if (*cp == 'X') | 
| 94 | xndx = cp; | 
| 95 | else if (*cp == 'Y') | 
| 96 | yndx = cp; | 
| 97 | if (xndx == NULL || yndx == NULL) | 
| 98 | return(0); | 
| 99 | rp->rt = 0; | 
| 100 | if (xndx > yndx) rp->rt |= YMAJOR; | 
| 101 | if (xndx[-1] == '-') rp->rt |= XDECR; | 
| 102 | if (yndx[-1] == '-') rp->rt |= YDECR; | 
| 103 | if ((rp->xr = atoi(xndx+1)) <= 0) | 
| 104 | return(0); | 
| 105 | if ((rp->yr = atoi(yndx+1)) <= 0) | 
| 106 | return(0); | 
| 107 | return(1); | 
| 108 | } |