about summary refs log tree commit diff
path: root/other/pamfixtrunc.c
blob: 6d71406f5c047a5a8db52e8725fd9e8548e1bae5 (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
/*============================================================================
                             pamfixtrunc
==============================================================================
  Fix a Netpbm image that has been truncated, e.g. by I/O error.

  By Bryan Henderson, January 2007.

  Contributed to the public domain by its author.

============================================================================*/

#include <setjmp.h>

#include "pm_c_util.h"
#include "pam.h"
#include "shhopt.h"
#include "mallocvar.h"

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFilespec;  /* Filespec of input file */
    unsigned int verbose;
};



static void
parseCommandLine(int argc, char ** const argv,
                 struct cmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
   Note that the file spec array we return is stored in the storage that
   was passed to us as the argv array.
-----------------------------------------------------------------------------*/
    optEntry * option_def;

    optStruct3 opt;

    unsigned int option_def_index;

    MALLOCARRAY(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENTRY */

    OPTENT3(0, "verbose",   OPT_FLAG,    NULL, &cmdlineP->verbose,       0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;  /* We don't parms that are negative numbers */

    optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (argc-1 == 0) 
        cmdlineP->inputFilespec = "-";
    else if (argc-1 != 1)
        pm_error("Program takes zero or one argument (filename).  You "
                 "specified %d", argc-1);
    else
        cmdlineP->inputFilespec = argv[1];
}



static unsigned int readErrRow;
static bool readErrVerbose;

static pm_usererrormsgfn discardMsg;

static void
discardMsg(const char * const msg) {
    if (readErrVerbose)
        pm_message("Error reading row %u: %s", readErrRow, msg);
}



static void
countRows(const struct pam * const inpamP,
          bool               const verbose,
          unsigned int *     const goodRowCountP) {

    tuple * tuplerow;
    unsigned int row;
    jmp_buf jmpbuf;
    int rc;
    unsigned int goodRowCount;
    
    tuplerow = pnm_allocpamrow(inpamP);

    pm_setusererrormsgfn(discardMsg);

    rc = setjmp(jmpbuf);
    if (rc == 0) {
        pm_setjmpbuf(&jmpbuf);

        readErrVerbose = verbose;
        goodRowCount = 0;  /* initial value */
        for (row = 0; row < inpamP->height; ++row) {
            readErrRow = row;
            pnm_readpamrow(inpamP, tuplerow);
            /* The above does not return if it can't read the next row from
               the file.  Instead, it longjmps out of this loop.
            */
            ++goodRowCount;
        }
    }
    *goodRowCountP = goodRowCount;

    pnm_freepamrow(tuplerow);
}



static void
copyGoodRows(const struct pam * const inpamP,
             FILE *             const ofP,
             unsigned int       const goodRowCount) {

    struct pam outpam;
    tuple * tuplerow;
    unsigned int row;

    outpam = *inpamP;  /* initial value */

    outpam.file = ofP;
    outpam.height = goodRowCount;

    tuplerow = pnm_allocpamrow(inpamP);

    pnm_writepaminit(&outpam);

    for (row = 0; row < outpam.height; ++row) {
        pnm_readpamrow(inpamP, tuplerow);
        pnm_writepamrow(&outpam, tuplerow);
    }
    
    pnm_freepamrow(tuplerow);
}



int
main(int argc, char * argv[]) {
    struct cmdlineInfo cmdline;
    struct pam inpam;
    FILE * ifP;
    pm_filepos rasterPos;
    unsigned int goodRowCount;

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr_seekable(cmdline.inputFilespec);

    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));

    pm_tell2(ifP, &rasterPos, sizeof(rasterPos));

    countRows(&inpam, cmdline.verbose, &goodRowCount);

    pm_message("Copying %u good rows; %u bottom rows missing",
               goodRowCount, inpam.height - goodRowCount);
    
    pm_seek2(ifP, &rasterPos, sizeof(rasterPos));

    copyGoodRows(&inpam, stdout, goodRowCount);

    pm_close(inpam.file);
    
    return 0;
}