Debian Codegraph @

converter/pgm/st4topgm.c

raw
1 /*=============================================================================
2 st4topgm
3 ===============================================================================
4
5 Convert an SBIG ST-4 image (not to be confused with the more sophisticated
6 SBIG format that every other SBIG camera produces) to PGM.
7
8 By Bryan Henderson January 2015.
9
10 Contributed to the public domain by its author.
11
12 This program was intended to substitute for the program of the same name in
13 the Debian version of Netpbm, by Justin Pryzby <[email protected]>
14 in December 2003.
15
16 =============================================================================*/
17 #include <string.h>
18
19 #include "pm_config.h"
20 #include "pm_c_util.h"
21 #include "pm.h"
22 #include "pam.h"
23
24
25
26 static unsigned int const st4Height = 165;
27 static unsigned int const st4Width = 192;
28 static unsigned int const st4Maxval = 255;
29
30
31
32 static void
33 validateFileSize(FILE * const ifP) {
34 /*----------------------------------------------------------------------------
35 Abort program if *ifP is not the proper size for an ST-4 SBIG file.
36
37 Don't change file position.
38 -----------------------------------------------------------------------------*/
39 pm_filepos const st4FileSize = (st4Height+1) * st4Width;
40
41 pm_filepos oldFilePos;
42 pm_filepos endFilePos;
43
44 pm_tell2(ifP, &oldFilePos, sizeof(endFilePos));
45
46 fseek(ifP, 0, SEEK_END);
47
48 pm_tell2(ifP, &endFilePos, sizeof(endFilePos));
49
50 pm_seek2(ifP, &oldFilePos, sizeof(oldFilePos));
51
52 if (endFilePos != st4FileSize)
53 pm_error("File is the wrong size for an ST-4 SBIG file. "
54 "It is %u bytes; it should be %u bytes",
55 (unsigned)endFilePos, (unsigned)st4FileSize);
56 }
57
58
59
60 static void
61 writeRaster(FILE * const ifP,
62 struct pam * const pamP) {
63
64 tuple * tuplerow;
65 unsigned int row;
66
67 tuplerow = pnm_allocpamrow(pamP);
68
69 for (row = 0; row < st4Height; ++row) {
70 unsigned int col;
71
72 for (col = 0; col < st4Width; ++col) {
73 unsigned char c;
74
75 pm_readcharu(ifP, &c);
76
77 tuplerow[col][0] = c;
78 }
79 pnm_writepamrow(pamP, tuplerow);
80 }
81
82 pnm_freepamrow(tuplerow);
83 }
84
85
86
87 struct St4Footer {
88 /*----------------------------------------------------------------------------
89 The information in an ST-4 SBIG footer.
90 -----------------------------------------------------------------------------*/
91 /* Note that numerical information is in decimal text, because we're lazy.
92 */
93
94 char comment[78+1];
95 char exposureTime[10+1];
96 char focalLength[10+1];
97 char apertureArea[10+1];
98 char calibrationFactor[10+1];
99 };
100
101
102
103 static void
104 stripTrailing(char * const arg) {
105
106 if (strlen(arg) > 0) {
107 char * p;
108 for (p = arg + strlen(arg); p > arg && *(p-1) == ' '; --p);
109
110 *p = '\0';
111 }
112 }
113
114
115
116 static void
117 stripLeading(char * const arg) {
118
119 const char * p;
120
121 for (p = &arg[0]; *p == ' '; ++p);
122
123 if (p > arg)
124 memmove(arg, p, strlen(p) + 1);
125 }
126
127
128
129 static void
130 readFooter(FILE * const ifP,
131 struct St4Footer * const footerP) {
132 /*----------------------------------------------------------------------------
133 Read the footer of the ST-4 image from *ifP, assuming *ifP is positioned
134 to the footer.
135
136 Return its contents as *footerP.
137 -----------------------------------------------------------------------------*/
138 char buffer[192];
139 size_t bytesReadCt;
140
141 /* The footer is laid out as follows.
142
143 off len description
144 --- --- -----------
145 000 1 Signature: 'v'
146 001 78 Freeform comment
147 079 10 Exposure time in 1/100s of a second
148 089 10 Focal length in inches
149 099 10 Aperture area in square inches
150 109 10 Calibration factor
151 119 73 Reserved
152
153 Note that the footer is the same length as a raster row.
154 */
155
156 bytesReadCt = fread(buffer, 1, sizeof(buffer), ifP);
157
158 if (bytesReadCt != 192)
159 pm_error("Failed to read footer of image");
160
161 if (buffer[0] != 'v')
162 pm_error("Input is not an ST-4 file. We know because the "
163 "signature byte (first byte of the footer) is not 'v'");
164
165 buffer[191] = '\0';
166 memmove(footerP->comment, &buffer[1], 78);
167 footerP->comment[78] = '\0';
168 stripTrailing(footerP->comment);
169
170 memmove(footerP->exposureTime, &buffer[79], 10);
171 footerP->exposureTime[10] = '\0';
172 stripLeading(footerP->exposureTime);
173
174 memmove(footerP->focalLength, &buffer[89], 10);
175 footerP->focalLength[10] = '\0';
176 stripLeading(footerP->focalLength);
177
178 memmove(footerP->apertureArea, &buffer[99], 10);
179 footerP->apertureArea[10] = '\0';
180 stripLeading(footerP->apertureArea);
181
182 memmove(footerP->calibrationFactor, &buffer[109], 10);
183 footerP->calibrationFactor[10] = '\0';
184 stripLeading(footerP->calibrationFactor);
185 }
186
187
188
189 static void
190 reportFooter(struct St4Footer const footer) {
191
192 pm_message("Comment: %s", footer.comment);
193
194 pm_message("Exposure time (1/100 s): %s", footer.exposureTime);
195
196 pm_message("Focal length (in): %s", footer.focalLength);
197
198 pm_message("Aperture area (sq in): %s", footer.apertureArea);
199
200 pm_message("Calibration factor: %s", footer.calibrationFactor);
201 }
202
203
204
205 int
206 main(int argc, const char **argv) {
207
208 FILE * ifP;
209 const char * inputFileName;
210 struct pam outpam;
211 struct St4Footer footer;
212
213 pm_proginit(&argc, argv);
214
215 if (argc-1 < 1)
216 inputFileName = "-";
217 else {
218 inputFileName = argv[1];
219 if (argc-1 > 1)
220 pm_error("Too many arguments: %u. "
221 "The only possible argument is the "
222 "optional input file name", argc-1);
223 }
224
225 /* We check the file size to catch the common problem of the input not
226 being valid ST-4 SBIG input. Unlike most formats, this one does not
227 have any signature at the head of the file.
228
229 More checks on the validity of the format happens when we process
230 the image footer.
231 */
232
233 ifP = pm_openr_seekable(inputFileName);
234
235 validateFileSize(ifP);
236
237 outpam.size = sizeof(outpam);
238 outpam.len = PAM_STRUCT_SIZE(maxval);
239 outpam.file = stdout;
240 outpam.format = PGM_FORMAT;
241 outpam.plainformat = false;
242 outpam.height = st4Height;
243 outpam.width = st4Width;
244 outpam.depth = 1;
245 outpam.maxval = st4Maxval;
246
247 pnm_writepaminit(&outpam);
248
249 writeRaster(ifP, &outpam);
250
251 readFooter(ifP, &footer);
252
253 reportFooter(footer);
254
255 pm_close(ifP);
256 pm_close(stdout);
257
258 return 0;
259 }
260
261
262