Debian Codegraph @

converter/pgm/spottopgm.c

raw
1 /* Spottopgm: Convert a SPOT satellite image to PGM format.
2 *
3 * Usage: spottopgm [-1|2|3] [Firstcol Firstline Lastcol Lastline] inputfile
4 *
5 * Author: Warren Toomey, 1992.
6 */
7
8 #include <stdio.h>
9 #include "pgm.h"
10
11 /* Global variables */
12
13 static FILE *spotfile; /* The input file */
14 static char linebuf[12000]; /* The line buffer */
15
16 static long Firstline = 0, /* The rectangle the user wants */
17 Lastline = 3000, /* cut out of the image */
18 Firstcol = 0,
19 Lastcol = 0;
20 static long Diff = 0; /* Firstcol - Lastcol */
21 static char *Bufptr; /* Pointer into the input image */
22 static int Color = 1; /* Either 1, 2 or 3 */
23 static int Colbool = 0; /* 1 if color */
24
25
26 /* Get_image extracts the pixel data from one line
27 * (i.e one record) in the SPOT input file. A SPOT image
28 * record has a header, data and trailer. The data lengths
29 * are fixed at 3960, 5400, 8640 or 10980 bytes.
30 *
31 * When we arrive here we have read in 12 bytes of the record.
32 * We then read in the rest of the record. We find the trailer
33 * and from that determine the number of pixels on the line.
34 *
35 * If the image is really color i.e interleaved 3 colors, we
36 * convert a line if its spectral sequence is the same as the one
37 * requested by the user (i.e 1, 2 or 3). I could create a ppm file
38 * but I couldn't be bothered with the rearranging of the data.
39 */
40 static int
41 get_image(long length)
42 {
43 int cnt;
44 struct Linehdr /* Each line begins with the 12 bytes */
45 { /* we have already, plus these 20 bytes */
46 long linenum; /* The line number of the record */
47 short recseq; /* The record sequence number */
48 short spectseq; /* The spectral number of the line */
49 long linetime; /* Time it was recorded (in ms). */
50 long leftpixmar; /* The pixel number of the 1st pixel */
51 long rightpixmar; /* The pixel number of the last pixel */
52 } linehdr;
53 long numpixels; /* Number of pixels on the line */
54
55 /* Get the details of this line */
56 if (pm_readbiglong (spotfile, &linehdr.linenum) == -1
57 || pm_readbigshort (spotfile, &linehdr.recseq) == -1
58 || pm_readbigshort (spotfile, &linehdr.spectseq) == -1
59 || pm_readbiglong (spotfile, &linehdr.linetime) == -1
60 || pm_readbiglong (spotfile, &linehdr.leftpixmar) == -1
61 || pm_readbiglong (spotfile, &linehdr.rightpixmar) == -1)
62 pm_error ("EOF / read error reading line header");
63
64 /* Now read in the line data */
65 cnt = length - 20 - 88;
66 cnt = fread(linebuf, 1, cnt, spotfile);
67
68 if (!Diff)
69 {
70 cnt += 28;
71 if (fseek (spotfile, 24, 1) == EOF)
72 pm_error ("seek error");
73 if (pm_readbiglong (spotfile, &numpixels) == -1)
74 pm_error ("EOF / read error reading line ender");
75
76 /* Determine the picture size */
77 Bufptr = &linebuf[Firstcol];
78 if (Lastcol == 0 || Lastcol > numpixels)
79 Lastcol = numpixels;
80 Diff = Lastcol - Firstcol;
81 /* Print out the header */
82 printf("P5\n%ld %ld\n255\n", Diff, Lastline - Firstline);
83 /* Inform about the image size */
84 if (Colbool) fprintf(stderr, "Color image, ");
85 fprintf(stderr, "%ld pixels wide\n", numpixels);
86 }
87
88 /* Output the line */
89 if (linehdr.linenum >= Firstline && linehdr.linenum <= Lastline
90 && linehdr.spectseq == Color)
91 fwrite(Bufptr, 1, Diff, stdout);
92 if (linehdr.linenum > Lastline) exit(0);
93
94 #ifdef DEBUG
95 fprintf(stderr,
96 "Line %4d, %3d, %3d, time %4d, l/r "
97 "pixmar %4d %4d len %d pixnum %d\n",
98 linehdr.linenum, linehdr.recseq,
99 linehdr.spectseq, linehdr.linetime,
100 linehdr.leftpixmar, linehdr.rightpixmar, length, numpixels);
101 #endif
102 /* And return the amount to seek */
103 return (length - 20 - cnt);
104 }
105
106
107
108 /* The image header tells us if the image is in monochrome or color, and
109 * if the latter, if the input colors are interleaved. If interleaved
110 * color, lines are interleaved R, G, B, R, G, B etc. Technically, some
111 * interleaving of infra-red, visible and ultra-violet.
112 *
113 * In the description field below,
114 * element 0 == P --> monochrome
115 * element 0 == X --> color
116 * element 9 == S --> sequential (i.e only one color here)
117 * element 9 == I --> interleaved (1 or more colors)
118 */
119 static int
120 get_imghdr(int length)
121 {
122 struct Imghdr
123 {
124 long linewidth;
125 char dummy1[36];
126 char description[16]; /* Type of image */
127 } header;
128
129 if (pm_readbiglong (spotfile, &header.linewidth) == -1)
130 pm_error ("EOF / read error reading header");
131 #ifdef DEBUG
132 if (fread (header.dummy1, 1, 36, spotfile) != 36)
133 pm_error ("EOF / read error reading header");
134 #else
135 if (fseek (spotfile, 36, 1) == EOF)
136 pm_error ("seek error");
137 #endif
138 if (fread (header.description, 1, 16, spotfile) != 16)
139 pm_error ("EOF / read error reading header");
140
141 /* Determine mono or color */
142 if (header.description[0] == 'X' && header.description[9] == 'S')
143 Colbool = 1;
144 else Colbool = 0;
145
146 #ifdef DEBUG
147 fprintf(stderr, "Dummy str is >%s<\n", header.dummy1);
148 fprintf(stderr, "Imghdr str is >%s<, col %d\n",
149 header.description, Colbool);
150 #endif
151 /* Return the amount to fseek */
152 return (length - 56);
153 }
154
155
156
157 static void
158 usage()
159 {
160 fprintf(stderr,
161 "Usage: spottopgm [-1|2|3] [Firstcol Firstline Lastcol Lastline] "
162 "input_file\n");
163 exit(1);
164 }
165
166
167
168 int
169 main(int argc, char *argv[])
170 {
171 struct Record /* A SPOT image is broken up into */
172 { /* records with the following fields */
173 long record; /* The record number (1, 2, 3...) */
174 unsigned char sub1; /* Record sub type 1 */
175 unsigned char type; /* The record type */
176 unsigned char sub2; /* Record sub type 2 */
177 unsigned char sub3; /* Record sub type 3 */
178 long length; /* Record length in bytes */
179 } arecord;
180
181 pgm_init( &argc, argv );
182
183 switch (argc)
184 {
185 case 7:
186 Color= -(atoi(argv[1])); /* Get the color to extract */
187 argv++;
188 case 6:
189 Firstcol = atoi(argv[1]); /* Get the rectangle to extract */
190 Firstline = atoi(argv[2]);
191 Lastcol = atoi(argv[3]);
192 Lastline = atoi(argv[4]);
193 argv += 4;
194 goto openfile; /* Yuk, a goto! */
195 case 3:
196 Color= -(atoi(argv[1])); /* Get the color to extract */
197 argv++;
198 case 2:
199 openfile:
200 spotfile = fopen(argv[1], "rb"); /* Open the input file */
201 if (spotfile == NULL) { perror("fopen"); exit(1); }
202 break;
203 default:
204 usage();
205 }
206
207 while (1) /* Get a record */
208 {
209 if (pm_readbiglong (spotfile, &arecord.record) == -1)
210 break;
211 arecord.sub1 = fgetc (spotfile);
212 arecord.type = fgetc (spotfile);
213 arecord.sub2 = fgetc (spotfile);
214 arecord.sub3 = fgetc (spotfile);
215 if (pm_readbiglong (spotfile, &arecord.length) == -1)
216 pm_error ("EOF / read error reading a record");
217
218 arecord.length -= 12; /* Subtract header size as well */
219 if (arecord.type == 0355 && arecord.sub1 == 0355)
220 arecord.length = get_image(arecord.length);
221 else if (arecord.type == 0300 && arecord.sub1 == 077)
222 arecord.length = get_imghdr(arecord.length);
223 #ifdef DEBUG
224 else
225 fprintf(stderr,
226 "Rcrd %3d, type %03o, stype %03o %03o %03o, length %d\n",
227 arecord.record, arecord.type, arecord.sub1, arecord.sub2,
228 (int) arecord.sub3 & 0xff, arecord.length);
229 #endif
230 /* Seek to next record */
231 fseek(spotfile, arecord.length, 1);
232 }
233 exit (0);
234 }
235
236
237