about summary refs log tree commit diff
path: root/converter/pgm/sbigtopgm.c
blob: 4f96e6f3184ad68786f3890b8c33830c4972ff45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*

    sbigtopgm.c - read a Santa Barbara Instruments Group CCDOPS file

    Note: All SBIG CCD astronomical cameras produce 14 bits or
	  (the ST-4 and ST-5) or 16 bits (ST-6 and later) per pixel.

		  Copyright (C) 1998 by John Walker
		       http://www.fourmilab.ch/

    If you find yourself having to add functionality included subsequent
    to the implementation of this program, you can probably find
    documentation of any changes to the SBIG file format on their
    Web site: http://www.sbig.com/

    Permission to use, copy, modify, and distribute this software and
    its documentation for any purpose and without fee is hereby
    granted, provided that the above copyright notice appear in all
    copies and that both that copyright notice and this permission
    notice appear in supporting documentation.	This software is
    provided "as is" without express or implied warranty.

*/

#include <string.h>

#include "pm_c_util.h"
#include "pm.h"
#include "nstring.h"
#include "pgm.h"

#define SBIG_HEADER_LENGTH  2048      /* File header length */



static void
looseCanon(char * const cpArg) {
/*----------------------------------------------------------------------------
  Canonicalize a line from the file header so items more sloppily formatted
  than those written by CCDOPS are still accepted.
-----------------------------------------------------------------------------*/
    char * cp;
    char * op;
    char c;

    cp = cpArg;  /* initial value */
    op = cpArg;  /* initial value */

    while ((c = *cp++) != 0) {
        if (!ISSPACE(c)) {
            if (ISUPPER(c))
                c = tolower(c);
            *op++ = c;
        }
    }
    *op++ = '\0';
}



int
main(int argc, const char ** argv) {

    FILE * ifP;
    gray * grayrow;
    int argn, row;
    int maxval;
    int comp, rows, cols;
    char header[SBIG_HEADER_LENGTH];
    char * hdr;
    char camera[80];

    pm_proginit(&argc, argv);

    argn = 1;

    if (argn < argc) {
        ifP = pm_openr(argv[argn]);
        argn++;
    } else
        ifP = stdin;

    if (argn != argc)
        pm_usage( "[sbigfile]" );

    if (fread(header, SBIG_HEADER_LENGTH, 1, ifP) < 1)
        pm_error("error reading SBIG file header");

    /*	Walk through the header and parse relevant parameters.	*/

    comp = -1;
    cols = -1;
    rows = -1;

    /*	The SBIG header specification equivalent to maxval is
        "Sat_level", the saturation level of the image.  This
        specification is optional, and was not included in files
        written by early versions of CCDOPS. It was introduced when it
        became necessary to distinguish 14-bit images with a Sat_level
        of 16383 from 16-bit images which saturate at 65535.  In
        addition, co-adding images or capturing with Track and
        Accumulate can increase the saturation level.  Since files
        which don't have a Sat_level line in the header were most
        probably written by early drivers for the ST-4 or ST-5, it
        might seem reasonable to make the default for maxval 16383,
        the correct value for those cameras.  I chose instead to use
        65535 as the default because the overwhelming majority of
        cameras in use today are 16 bit, and it's possible some
        non-SBIG software may omit the "optional" Sat_level
        specification.	Also, no harm is done if a larger maxval is
        specified than appears in the image--a simple contrast stretch
        will adjust pixels to use the full 0 to maxval range.  The
        converse, pixels having values greater than maxval, results in
        an invalid file which may cause problems in programs which
        attempt to process it.
	*/

    maxval = 65535;

    hdr = header;

    strcpy(camera, "ST-?");  /* initial value */

    for (;;) {
        char *cp = strchr(hdr, '\n');

        if (cp == NULL) {
            pm_error("malformed SBIG file header at character %u",
                     (unsigned)(hdr - header));
        }
        *cp = '\0';
        if (strncmp(hdr, "ST-", 3) == 0) {
            char * const ep = strchr(hdr + 3, ' ');

            if (ep != NULL) {
                *ep = '\0';
                strcpy(camera, hdr);
                *ep = ' ';
            }
        }
        looseCanon(hdr);
        if (STRSEQ(hdr, "st-")) {
            comp = strstr(hdr, "compressed") != NULL;
        } else if (STRSEQ(hdr, "height=")) {
            rows = atoi(hdr + 7);
        } else if (STRSEQ(hdr, "width=")) {
            cols = atoi(hdr + 6);
        } else if (STRSEQ(hdr, "sat_level=")) {
            maxval = atoi(hdr + 10);
        } else if (streq(hdr, "end")) {
            break;
        }
        hdr = cp + 1;
    }

    if (comp == -1 || rows == -1 || cols == -1)
        pm_error("required specification missing from SBIG file header");

    pm_message("SBIG %s %dx%d %s image, saturation level = %d",
               camera, cols, rows, comp ? "compressed" : "uncompressed",
               maxval);

    if (maxval > PGM_OVERALLMAXVAL) {
        pm_error("Saturation level (%d levels) is too large"
                 "This program's limit is %d.", maxval, PGM_OVERALLMAXVAL);
    }

    pgm_writepgminit(stdout, cols, rows, maxval, 0);
    grayrow = pgm_allocrow(cols);

    for (row = 0; row < rows; ++row) {
        bool compthis;
        unsigned int col;

        if (comp) {
            unsigned short rowlen;        /* Compressed row length */

            pm_readlittleshortu(ifP, &rowlen);
            
            /*	If compression results in a row length >= the uncompressed
                row length, that row is output uncompressed.  We detect this
                by observing that the compressed row length is equal to
                that of an uncompressed row.
            */

            if (rowlen == cols * 2)
                compthis = false;
            else
                compthis = comp;
        } else
            compthis = comp;

        for (col = 0; col < cols; ++col) {
            unsigned short g;

            if (compthis) {
                if (col == 0) {
                    pm_readlittleshortu(ifP, &g);
                } else {
                    int const delta = getc(ifP);

                    if (delta == 0x80)
                        pm_readlittleshortu(ifP, &g);
                    else
                        g += ((signed char) delta);
                }
            } else
                pm_readlittleshortu(ifP, &g);
            grayrow[col] = g;
        }
        pgm_writepgmrow(stdout, grayrow, cols, maxval, 0);
    }
    pm_close(ifP);
    pm_close(stdout);

    return 0;
}