Debian Codegraph @

converter/pgm/fstopgm.c

raw
1 /* fstopgm.c - read a Usenix FaceSaver(tm) file and produce a portable graymap
2 **
3 ** Copyright (C) 1989 by Jef Poskanzer.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12
13 #include <string.h>
14
15 #include "pm.h"
16 #include "pgm.h"
17
18
19
20 static int
21 gethexit(FILE * const ifP) {
22
23 for ( ; ; ) {
24 unsigned int const i = getc(ifP);
25
26 if (i == EOF)
27 pm_error("EOF / read error");
28 else {
29 char const c = (char) i;
30 if (c >= '0' && c <= '9')
31 return c - '0';
32 else if (c >= 'A' && c <= 'F')
33 return c - 'A' + 10;
34 else if (c >= 'a' && c <= 'f')
35 return c - 'a' + 10;
36 else {
37 /* Ignore - whitespace. */
38 }
39 }
40 }
41 }
42
43
44
45 static void
46 warnNonsquarePixels(unsigned int const cols,
47 unsigned int const xcols,
48 unsigned int const rows,
49 unsigned int const xrows) {
50
51 const char * const baseMsg = "warning, non-square pixels";
52
53 if (pm_have_float_format()) {
54 float const rowratio = (float) xrows / (float) rows;
55 float const colratio = (float) xcols / (float) cols;
56
57 pm_message("%s; to fix do a 'pamscale -%cscale %g'",
58 baseMsg,
59 rowratio > colratio ? 'y' : 'x',
60 rowratio > colratio ?
61 rowratio / colratio : colratio / rowratio);
62 } else
63 pm_message("%s", baseMsg);
64 }
65
66
67
68 int
69 main(int argc, const char ** argv) {
70
71 FILE * ifP;
72 gray ** grays;
73 int argn;
74 int row;
75 gray maxval;
76 int rows, cols, depth;
77 int xrows, xcols, xdepth;
78 #define STRSIZE 1000
79
80 pm_proginit(&argc, argv);
81
82 rows = 0;
83 cols = 0;
84 depth = 0;
85
86 xrows = 0;
87 xcols = 0;
88 xdepth = 0;
89
90 argn = 1;
91
92 if (argn < argc) {
93 ifP = pm_openr(argv[argn]);
94 argn++;
95 } else
96 ifP = stdin;
97
98 if (argn != argc)
99 pm_error("Too many arguments. The only argument is the file name");
100
101 /* Read the FaceSaver(tm) header. */
102 for ( ; ; ) {
103 char buf[STRSIZE];
104 char firstname[STRSIZE];
105 char lastname[STRSIZE];
106 char email[STRSIZE];
107
108 char * const rc = fgets(buf, STRSIZE, ifP);
109
110 if (rc == NULL)
111 pm_error("error reading header");
112
113 /* Blank line ends header. */
114 if (strlen(buf) == 1)
115 break;
116
117 if (sscanf(buf, "FirstName: %[^\n]", firstname) == 1);
118 else if (sscanf(buf, "LastName: %[^\n]", lastname) == 1);
119 else if (sscanf(buf, "E-mail: %[^\n]", email ) == 1);
120 else if (sscanf(buf, "PicData: %d %d %d\n",
121 &cols, &rows, &depth ) == 3) {
122 if (depth != 8)
123 pm_error("can't handle 'PicData' depth other than 8");
124 } else if (sscanf(buf, "Image: %d %d %d\n",
125 &xcols, &xrows, &xdepth ) == 3) {
126 if (xdepth != 8)
127 pm_error("can't handle 'Image' depth other than 8");
128 }
129 }
130 if (cols <= 0 || rows <= 0)
131 pm_error("invalid header");
132 maxval = pm_bitstomaxval(depth);
133 if (maxval > PGM_OVERALLMAXVAL)
134 pm_error("depth %d is too large. Our maximum is %d",
135 maxval, PGM_OVERALLMAXVAL);
136 if (xcols != 0 && xrows != 0 && (xcols != cols || xrows != rows))
137 warnNonsquarePixels(cols, xcols, rows, xrows);
138
139 /* Read the hex bits. */
140 grays = pgm_allocarray(cols, rows);
141 for (row = rows - 1; row >= 0; --row) {
142 unsigned int col;
143 for (col = 0; col < cols; ++col) {
144 grays[row][col] = gethexit(ifP) << 4;
145 grays[row][col] += gethexit(ifP);
146 }
147 }
148 pm_close(ifP);
149
150 pgm_writepgm(stdout, grays, cols, rows, maxval, 0);
151 pm_close(stdout);
152
153 return 0;
154 }
155
156
157