Debian Codegraph @

converter/other/hdifftopam.c

raw
1 /******************************************************************************
2 hdifftopam
3 *******************************************************************************
4 This program recovers a PAM image from a horizontal difference images
5 such as created by Pamtohdiff.
6
7 By Bryan Henderson, San Jose, CA 2002.04.15.
8 ******************************************************************************/
9 #include <string.h>
10 #include <stdio.h>
11
12 #include "pm_c_util.h"
13 #include "mallocvar.h"
14 #include "nstring.h"
15 #include "shhopt.h"
16 #include "pam.h"
17
18 struct CmdlineInfo {
19 /* All the information the user supplied in the command line,
20 in a form easy for the program to use.
21 */
22 const char * inputFileNm; /* Names of input files */
23 unsigned int pnm;
24 unsigned int verbose;
25 };
26
27
28
29 static void
30 parseCommandLine(int argc, const char ** const argv,
31 struct CmdlineInfo * const cmdlineP) {
32 /*----------------------------------------------------------------------------
33 Note that the file spec array we return is stored in the storage that
34 was passed to us as the argv array.
35 -----------------------------------------------------------------------------*/
36 optEntry * option_def; /* Used by OPTENT3 */
37 optStruct3 opt;
38
39 unsigned int option_def_index;
40
41 MALLOCARRAY_NOFAIL(option_def, 100);
42
43 option_def_index = 0; /* incremented by OPTENTRY */
44 OPTENT3(0, "pnm", OPT_FLAG, NULL, &cmdlineP->pnm, 0);
45 OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0);
46
47 opt.opt_table = option_def;
48 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
49 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
50
51 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
52 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
53
54 if (argc-1 < 1)
55 cmdlineP->inputFileNm = "-";
56 else if (argc-1 == 1)
57 cmdlineP->inputFileNm = argv[1];
58 else
59 pm_error("Too many arguments.");
60 }
61
62
63
64 static void
65 makePnm(struct pam * const pamP) {
66
67 switch (pamP->depth) {
68 case 1:
69 pamP->format = PGM_FORMAT;
70 break;
71 case 3:
72 pamP->format = PPM_FORMAT;
73 break;
74 default:
75 pm_error("Input depth (%d) does not correspond to a PNM format.",
76 pamP->depth);
77 }
78 }
79
80
81
82 static void
83 describeOutput(struct pam const pam) {
84
85 pm_message("Output is %d x %d x %d, maxval %u",
86 pam.width, pam.height, pam.depth, (unsigned int) pam.maxval);
87 }
88
89
90
91 int
92 main(int argc, const char ** argv) {
93
94 FILE * ifP;
95 struct CmdlineInfo cmdline;
96 struct pam diffpam, outpam;
97 unsigned int row;
98 tuple * diffrow;
99 tuple * outrow;
100 tuple * prevrow;
101
102 pm_proginit(&argc, argv);
103
104 parseCommandLine(argc, argv, &cmdline);
105
106 ifP = pm_openr(cmdline.inputFileNm);
107
108 pnm_readpaminit(ifP, &diffpam, PAM_STRUCT_SIZE(tuple_type));
109
110 if (diffpam.format != PAM_FORMAT)
111 pm_error("Input must be a PAM file, not PNM");
112 else if (!streq(diffpam.tuple_type, "hdiff"))
113 pm_error("Input tuple type is '%s'. Must be 'hdiff'",
114 diffpam.tuple_type);
115
116 outpam = diffpam;
117 outpam.file = stdout;
118 strcpy(outpam.tuple_type, "unhdiff");
119
120 if (cmdline.verbose)
121 describeOutput(outpam);
122 if (cmdline.pnm)
123 makePnm(&outpam);
124
125 pnm_writepaminit(&outpam);
126
127 diffrow = pnm_allocpamrow(&diffpam);
128 outrow = pnm_allocpamrow(&outpam);
129 prevrow = pnm_allocpamrow(&diffpam);
130
131 pnm_setpamrow(&diffpam, prevrow, 0);
132
133 {
134 unsigned int const bias = diffpam.maxval/2;
135
136 for (row = 0; row < diffpam.height; ++row) {
137 unsigned int col;
138 pnm_readpamrow(&diffpam, diffrow);
139 for (col = 0; col < diffpam.width; ++col) {
140 unsigned int plane;
141 for (plane = 0; plane < diffpam.depth; ++plane) {
142 sample const prevSample = prevrow[col][plane];
143 sample const diffSample = diffrow[col][plane];
144
145 outrow[col][plane] =
146 (-bias + prevSample + diffSample) % (outpam.maxval+1);
147 prevrow[col][plane] = outrow[col][plane];
148 }
149 }
150 pnm_writepamrow(&outpam, outrow);
151 }
152 }
153 pnm_freepamrow(prevrow);
154 pnm_freepamrow(outrow);
155 pnm_freepamrow(diffrow);
156
157 exit(0);
158 }
159
160
161