about summary refs log tree commit diff
path: root/converter/other/hdifftopam.c
blob: 7464bb1a2872c3670ec6f202df13b712d764cbe5 (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
/******************************************************************************
                                hdifftopam
*******************************************************************************
  This program recovers a PAM image from a horizontal difference images
  such as created by Pamtohdiff.

  By Bryan Henderson, San Jose, CA 2002.04.15.
******************************************************************************/
#include <string.h>
#include <stdio.h>

#include "pm_c_util.h"
#include "pam.h"
#include "shhopt.h"
#include "nstring.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;  /* Filespecs of input files */
    unsigned int pnm;
    unsigned int verbose;
};



static void
parseCommandLine(int argc, char ** 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 = malloc( 100*sizeof( optEntry ) );
        /* Instructions to pm_optParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "pnm",       OPT_FLAG,    NULL, &cmdlineP->pnm,      0);
    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 have no parms that are negative numbers */

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

    if (argc-1 < 1)
        cmdlineP->inputFilespec = "-";
    else if (argc-1 == 1)
        cmdlineP->inputFilespec = argv[1];
    else
        pm_error("Too many arguments.");
}



static void
makePnm(struct pam * const pamP) {

    switch (pamP->depth) {
    case 1:
        pamP->format = PGM_FORMAT;
        break;
    case 3:
        pamP->format = PPM_FORMAT;
        break;
    default:
        pm_error("Input depth (%d) does not correspond to a PNM format.",
                 pamP->depth);
    }
}



static void
    describeOutput(struct pam const pam) {

    pm_message("Output is %d x %d x %d, maxval %u",
               pam.width, pam.height, pam.depth, (unsigned int) pam.maxval);
}



int
main(int argc, char *argv[]) {
    FILE *ifP;
    struct cmdlineInfo cmdline;
    struct pam diffpam, outpam;
    unsigned int row;
    tuple * diffrow;
    tuple * outrow;
    tuple * prevrow;

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

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

    if (diffpam.format != PAM_FORMAT)
        pm_error("Input must be a PAM file, not PNM");
    else if (!streq(diffpam.tuple_type, "hdiff"))
        pm_error("Input tuple type is '%s'.  Must be 'hdiff'",
                 diffpam.tuple_type);

    outpam = diffpam;
    outpam.file = stdout;
    strcpy(outpam.tuple_type, "unhdiff");

    if (cmdline.verbose)
        describeOutput(outpam);
    if (cmdline.pnm)
        makePnm(&outpam);

    pnm_writepaminit(&outpam);

    diffrow = pnm_allocpamrow(&diffpam);
    outrow =  pnm_allocpamrow(&outpam);
    prevrow = pnm_allocpamrow(&diffpam);

    pnm_setpamrow(&diffpam, prevrow, 0);

    {
        unsigned int const bias = diffpam.maxval/2;

        for (row = 0; row < diffpam.height; ++row) {
            unsigned int col;
            pnm_readpamrow(&diffpam, diffrow);
            for (col = 0; col < diffpam.width; ++col) {
                unsigned int plane;
                for (plane = 0; plane < diffpam.depth; ++plane) {
                    sample const prevSample = prevrow[col][plane];
                    sample const diffSample = diffrow[col][plane];

                    outrow[col][plane] =
                        (-bias + prevSample + diffSample) % (outpam.maxval+1);
                    prevrow[col][plane] = outrow[col][plane];
                }
            }
            pnm_writepamrow(&outpam, outrow);
        }
    }
    pnm_freepamrow(prevrow);
    pnm_freepamrow(outrow);
    pnm_freepamrow(diffrow);

    exit(0);
}