Debian Codegraph @

converter/pgm/sbigtopgm.c

raw
1 /*
2 sbigtopgm.c - read a Santa Barbara Instruments Group CCDOPS file
3
4 Note: All SBIG CCD astronomical cameras produce 14 bits
5 (the ST-4 and ST-5) or 16 bits (ST-6 and later) per pixel.
6
7 If you find yourself having to add functionality included subsequent
8 to the implementation of this program, you can probably find
9 documentation of any changes to the SBIG file format on their
10 Web site: http://www.sbig.com/
11
12 Copyright (C) 1998 by John Walker
13 http://www.fourmilab.ch/
14
15 Permission to use, copy, modify, and distribute this software and
16 its documentation for any purpose and without fee is hereby
17 granted, provided that the above copyright notice appear in all
18 copies and that both that copyright notice and this permission
19 notice appear in supporting documentation. This software is
20 provided "as is" without express or implied warranty.
21 */
22
23 #include <string.h>
24
25 #include "pm_c_util.h"
26 #include "mallocvar.h"
27 #include "nstring.h"
28 #include "shhopt.h"
29 #include "pm.h"
30 #include "pgm.h"
31
32 struct CmdlineInfo {
33 /* All the information the user supplied in the command line,
34 in a form easy for the program to use.
35 */
36 const char * inputFileName;
37 };
38
39
40
41 static void
42 parseCommandLine(int argc, const char ** argv,
43 struct CmdlineInfo * const cmdlineP) {
44 /*----------------------------------------------------------------------------
45 Note that the file spec array we return is stored in the storage that
46 was passed to as as the argv array.
47 -----------------------------------------------------------------------------*/
48 optEntry * option_def;
49 optStruct3 opt;
50
51 unsigned int option_def_index;
52
53 MALLOCARRAY_NOFAIL(option_def, 100);
54
55 OPTENTINIT;
56
57 opt.opt_table = option_def;
58 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
59 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
60
61 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
62 /* Uses and sets argc, argv, and some of *cmdlineP and others */
63
64 if (argc-1 < 1)
65 cmdlineP->inputFileName = "-";
66 else {
67 cmdlineP->inputFileName = argv[1];
68
69 if (argc-1 > 1)
70 pm_error("Too many arguments. The only possible argument is the "
71 "optional input file name");
72 }
73 }
74
75
76
77 #define SBIG_HEADER_LENGTH 2048 /* File header length */
78
79
80
81 static void
82 looseCanon(char * const cpArg) {
83 /*----------------------------------------------------------------------------
84 Canonicalize a line from the file header so items more sloppily formatted
85 than those written by CCDOPS are still accepted.
86
87 Remove all whitespace and make all letters lowercase.
88
89 Note that the SBIG Type 3 format specification at www.sbig.com in January
90 2015 says header parameter names are capitalized like 'Height'; we change
91 that to "height".
92
93 The spec also says the line ends with LF, then CR (yes, really). Assuming
94 Caller separates lines at LF, that means we see CR at the beginning of all
95 lines but the first. We remove that.
96 -----------------------------------------------------------------------------*/
97 char * cp;
98 char * op;
99 char c;
100
101 cp = cpArg; /* initial value */
102 op = cpArg; /* initial value */
103
104 while ((c = *cp++) != 0) {
105 if (!ISSPACE(c)) {
106 if (ISUPPER(c))
107 c = tolower(c);
108 *op++ = c;
109 }
110 }
111 *op++ = '\0';
112 }
113
114
115
116 struct SbigHeader {
117 /*----------------------------------------------------------------------------
118 The information in an SBIG file header.
119
120 This is only the information this program cares about; the header
121 may have much more information in it.
122 -----------------------------------------------------------------------------*/
123 unsigned int rows;
124 unsigned int cols;
125 unsigned int maxval;
126 bool isCompressed;
127 const char * cameraType;
128 /* Null means information not in header */
129 };
130
131
132
133 static void
134 readSbigHeader(FILE * const ifP,
135 struct SbigHeader * const sbigHeaderP) {
136
137 size_t rc;
138 bool gotCompression;
139 bool gotWidth;
140 bool gotHeight;
141 char * buffer; /* malloced */
142 char * cursor;
143 bool endOfHeader;
144
145 MALLOCARRAY_NOFAIL(buffer, SBIG_HEADER_LENGTH + 1);
146
147 rc = fread(buffer, SBIG_HEADER_LENGTH, 1, ifP);
148
149 if (rc < 1)
150 pm_error("error reading SBIG file header");
151
152 buffer[SBIG_HEADER_LENGTH] = '\0';
153
154 /* The SBIG header specification equivalent to maxval is
155 "Sat_level", the saturation level of the image. This
156 specification is optional, and was not included in files
157 written by early versions of CCDOPS. It was introduced when it
158 became necessary to distinguish 14-bit images with a Sat_level
159 of 16383 from 16-bit images which saturate at 65535. In
160 addition, co-adding images or capturing with Track and
161 Accumulate can increase the saturation level. Since files
162 which don't have a Sat_level line in the header were most
163 probably written by early drivers for the ST-4 or ST-5, it
164 might seem reasonable to make the default for maxval 16383,
165 the correct value for those cameras. I chose instead to use
166 65535 as the default because the overwhelming majority of
167 cameras in use today are 16 bit, and it's possible some
168 non-SBIG software may omit the "optional" Sat_level
169 specification. Also, no harm is done if a larger maxval is
170 specified than appears in the image--a simple contrast stretch
171 will adjust pixels to use the full 0 to maxval range. The
172 converse, pixels having values greater than maxval, results in
173 an invalid file which may cause problems in programs which
174 attempt to process it.
175
176 According to the official specification, the camera type name is the
177 first item in the header, and may or may not start with "ST-". But
178 this program has historically had an odd method of detecting camera
179 type, which allows any string starting with "ST-" anywhere in the
180 header, and for now we leave that undisturbed. 2015.05.27.
181 */
182
183 gotCompression = false; /* initial value */
184 gotWidth = false; /* initial value */
185 gotHeight = false; /* initial value */
186
187 sbigHeaderP->maxval = 65535; /* initial assumption */
188 sbigHeaderP->cameraType = NULL; /* initial assumption */
189
190 for (cursor = &buffer[0], endOfHeader = false; !endOfHeader;) {
191 char * const cp = strchr(cursor, '\n');
192
193 if (cp == NULL) {
194 pm_error("malformed SBIG file header at character %u",
195 (unsigned)(cursor - &buffer[0]));
196 }
197 *cp = '\0';
198 if (strneq(cursor, "ST-", 3) ||
199 (cursor == &buffer[0] && strstr(cursor,"Image") != NULL)) {
200
201 char * const ep = strchr(cursor + 3, ' ');
202
203 if (ep != NULL) {
204 *ep = '\0';
205 sbigHeaderP->cameraType = pm_strdup(cursor);
206 *ep = ' ';
207 }
208 }
209
210 looseCanon(cursor);
211 /* Convert from standard SBIG to an internal format */
212
213 if (strneq(cursor, "st-", 3) || cursor == &buffer[0]) {
214 sbigHeaderP->isCompressed =
215 (strstr(cursor, "compressedimage") != NULL);
216 gotCompression = true;
217 } else if (strneq(cursor, "height=", 7)) {
218 sbigHeaderP->rows = atoi(cursor + 7);
219 gotHeight = true;
220 } else if (strneq(cursor, "width=", 6)) {
221 sbigHeaderP->cols = atoi(cursor + 6);
222 gotWidth = true;
223 } else if (strneq(cursor, "sat_level=", 10)) {
224 sbigHeaderP->maxval = atoi(cursor + 10);
225 } else if (streq("end", cursor)) {
226 endOfHeader = true;
227 }
228 cursor = cp + 1;
229 }
230
231 if (!gotCompression)
232 pm_error("Required 'ST-*' specification missing "
233 "from SBIG file header");
234 if (!gotHeight)
235 pm_error("required 'height=' specification missing"
236 "from SBIG file header");
237 if (!gotWidth)
238 pm_error("required 'width=' specification missing "
239 "from SBIG file header");
240 }
241
242
243
244 static void
245 termSbigHeader(struct SbigHeader const sbigHeader) {
246
247 if (sbigHeader.cameraType)
248 pm_strfree(sbigHeader.cameraType);
249 }
250
251
252
253 static void
254 writeRaster(FILE * const ifP,
255 struct SbigHeader const hdr,
256 FILE * const ofP) {
257
258 gray * grayrow;
259 unsigned int row;
260
261 grayrow = pgm_allocrow(hdr.cols);
262
263 for (row = 0; row < hdr.rows; ++row) {
264 bool compthis;
265 unsigned int col;
266
267 if (hdr.isCompressed) {
268 unsigned short rowlen; /* Compressed row length */
269
270 pm_readlittleshortu(ifP, &rowlen);
271
272 /* If compression results in a row length >= the uncompressed
273 row length, that row is output uncompressed. We detect this
274 by observing that the compressed row length is equal to
275 that of an uncompressed row.
276 */
277
278 if (rowlen == hdr.cols * 2)
279 compthis = false;
280 else
281 compthis = hdr.isCompressed;
282 } else
283 compthis = hdr.isCompressed;
284
285 for (col = 0; col < hdr.cols; ++col) {
286 unsigned short g;
287
288 if (compthis) {
289 if (col == 0) {
290 pm_readlittleshortu(ifP, &g);
291 } else {
292 int const delta = getc(ifP);
293
294 if (delta == 0x80)
295 pm_readlittleshortu(ifP, &g);
296 else
297 g += ((signed char) delta);
298 }
299 } else
300 pm_readlittleshortu(ifP, &g);
301 grayrow[col] = g;
302 }
303 pgm_writepgmrow(ofP, grayrow, hdr.cols, hdr.maxval, 0);
304 }
305
306 pgm_freerow(grayrow);
307 }
308
309
310
311 int
312 main(int argc, const char ** argv) {
313
314 FILE * ifP;
315 struct CmdlineInfo cmdline;
316 struct SbigHeader hdr;
317
318 pm_proginit(&argc, argv);
319
320 parseCommandLine(argc, argv, &cmdline);
321
322 ifP = pm_openr(cmdline.inputFileName);
323
324 readSbigHeader(ifP, &hdr);
325
326 pm_message("SBIG '%s' %ux%u %s image, saturation level = %u",
327 (hdr.cameraType ? hdr.cameraType : "ST-?"),
328 hdr.cols, hdr.rows,
329 hdr.isCompressed ? "compressed" : "uncompressed",
330 hdr.maxval);
331
332 if (hdr.maxval > PGM_OVERALLMAXVAL) {
333 pm_error("Saturation level (%u levels) is too large"
334 "This program's limit is %u.", hdr.maxval, PGM_OVERALLMAXVAL);
335 }
336
337 pgm_writepgminit(stdout, hdr.cols, hdr.rows, hdr.maxval, 0);
338
339 writeRaster(ifP, hdr, stdout);
340
341 termSbigHeader(hdr);
342
343 pm_close(ifP);
344 pm_close(stdout);
345
346 return 0;
347 }
348
349
350