~ubuntu-branches/ubuntu/hardy/netpbm-free/hardy-updates

« back to all changes in this revision

Viewing changes to pgm/st4topgm.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2004-07-29 20:25:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040729202546-9o69r65403wvtla5
Tags: 2:10.0-5
* build-depends against libtiff4.
* fix typo in ppmtowinicon. Closes: #261999.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * st4topgm.c
 
3
 * Justin Pryzby <justinpryzby@users.sf.net>
 
4
 * Mon Dec 15 17:51:29 EST 2003
 
5
 *
 
6
 * Convert an image from an SBIG ST-4 astronomical CCD camera to pgm.
 
7
 * See [http://www.sbig.com/].  Use sbigtopgm for all other SBIG
 
8
 * cameras.
 
9
 *
 
10
 * Copyright (C) 2003 by Justin Pryzby <justinpryzby@users.sf.net>
 
11
 *
 
12
 * Permission to use, copy, modify, and distribute this software and its
 
13
 * documentation for any purpose and without fee is hereby granted, provided
 
14
 * that the above copyright notice appear in all copies and that both that
 
15
 * copyright notice and this permission notice appear in supporting
 
16
 * documentation.  This software is provided "as is" without express or
 
17
 * implied warranty.
 
18
 */
 
19
 
 
20
#include <string.h>
 
21
#include "pgm.h"
 
22
 
 
23
/* 192x166, a 192*165 image plus a "line" of metadata. */
 
24
#define FSIZE   31872
 
25
#define ROWS    165
 
26
#define COLS    192
 
27
#define MAXVAL  255
 
28
 
 
29
int main(int argc, char **argv);
 
30
void read_foot(FILE *fp);
 
31
void trim_comment0(char *comment);
 
32
void trim_comment1(char *comment);
 
33
 
 
34
int main(int argc, char **argv)
 
35
{
 
36
        FILE *fp;
 
37
        int row,col;
 
38
        gray *grayrow;
 
39
 
 
40
        pgm_init(&argc, argv);
 
41
 
 
42
        if (argc>2) pm_usage("[st4file]");
 
43
        else if (argc==1) fp=pm_openr_seekable("-");
 
44
        else fp=pm_openr_seekable(argv[1]);
 
45
 
 
46
        fseek(fp, 0, SEEK_END);
 
47
        if (ftell(fp)!=FSIZE) pm_error("Incorrect file size.");
 
48
 
 
49
        pgm_writepgminit(stdout, COLS, ROWS, MAXVAL, 0);
 
50
        grayrow = pgm_allocrow(COLS);
 
51
 
 
52
        fseek(fp, 31680, SEEK_SET);
 
53
        read_foot(fp);
 
54
 
 
55
        /*
 
56
         * Okay, the file is of the correct size, and read_foot has
 
57
         * confirmed the constant-position, constant value byte.  We
 
58
         * accept the file as valid.
 
59
         */
 
60
        fseek(fp, 0, SEEK_SET);
 
61
 
 
62
        /* Data. */
 
63
        for (row=0; row<ROWS; row++) {
 
64
                for (col=0; col<COLS; col++) {
 
65
                        grayrow[col]=fgetc(fp);
 
66
                }
 
67
 
 
68
                pgm_writepgmrow(stdout, grayrow, COLS, MAXVAL, 0);
 
69
        }
 
70
 
 
71
        pgm_freerow(grayrow);
 
72
        
 
73
        pm_close(fp);
 
74
        pm_close(stdout);
 
75
 
 
76
        return 0;
 
77
}
 
78
 
 
79
 
 
80
/* 
 
81
 * Read the footer, which is stored in the following form.
 
82
 * 1:       'v'.
 
83
 * 2-79:    Freeform comment.
 
84
 * 80-89:   Exposure time in 1/100s of a second.
 
85
 * 90-99:   Focal length in inches.
 
86
 * 100-109: Aperture area in square inches.
 
87
 * 110-119: Calibration factor.
 
88
 * 120-192: Reserved.
 
89
 */
 
90
void read_foot(FILE *fp)
 
91
{
 
92
        char buf[192];
 
93
 
 
94
        fread(buf, 192, 1, fp);
 
95
 
 
96
        /*
 
97
         * The only constant byte at a known constant offset, and thus
 
98
         * serves as one of our two checks of the file type.  (The other
 
99
         * check being the file size).
 
100
         */
 
101
        if (buf[0]!='v') pm_error("Invalid file format.");
 
102
 
 
103
        memmove(buf, buf+1, 78);
 
104
        buf[78]=0;
 
105
        trim_comment0(buf);
 
106
        fprintf(stderr, "Comment:                 %80s\n", buf);
 
107
 
 
108
        memcpy(buf, buf+79, 10);
 
109
        buf[10]=0;
 
110
        trim_comment1(buf);
 
111
        fprintf(stderr, "Exposure time (1/100 s): %80s\n", buf);
 
112
 
 
113
        memcpy(buf, buf+89, 10);
 
114
        trim_comment1(buf);
 
115
        fprintf(stderr, "Focal length (in):       %80s\n", buf);
 
116
 
 
117
        memcpy(buf, buf+99, 10);
 
118
        trim_comment1(buf);
 
119
        fprintf(stderr, "Aperture area (sq in):   %80s\n", buf);
 
120
 
 
121
        memcpy(buf, buf+109, 10);
 
122
        trim_comment1(buf);
 
123
        fprintf(stderr, "Calibration factor:      %80s\n", buf);
 
124
}
 
125
 
 
126
/*
 
127
 * Trim up to 79 trailing spaces from the given string.
 
128
 */
 
129
void trim_comment0(char *comment)
 
130
{
 
131
        for ( ; comment[strlen(comment)-1]==' '; comment[strlen(comment)-1]=0);
 
132
}
 
133
 
 
134
/*
 
135
 * Trim up to 79 leading spaces from the given string.
 
136
 */
 
137
void trim_comment1(char *comment)
 
138
{
 
139
        for ( ; 32==*comment; memmove(comment, comment+1, strlen(comment)));
 
140
}