Debian Codegraph @

editor/specialty/pamdeinterlace.c

raw
1 /******************************************************************************
2 pamdeinterlace
3 *******************************************************************************
4 De-interlace an image, i.e. select every 2nd row.
5
6 By Bryan Henderson, San Jose, CA 2001.11.11.
7
8 Contributed to the public domain.
9 ******************************************************************************/
10
11 #include <stdbool.h>
12
13 #include "pm_c_util.h"
14 #include "pam.h"
15 #include "shhopt.h"
16 #include "mallocvar.h"
17
18 enum evenodd {EVEN, ODD};
19
20 struct CmdlineInfo {
21 /* All the information the user supplied in the command line,
22 in a form easy for the program to use.
23 */
24 const char *inputFilespec; /* Filespecs of input files */
25 enum evenodd rowsToTake;
26 };
27
28
29
30 static void
31 parseCommandLine(int argc, const char ** argv,
32 struct CmdlineInfo * const cmdlineP) {
33 /*----------------------------------------------------------------------------
34 Note that the file spec array we return is stored in the storage that
35 was passed to us as the argv array.
36 -----------------------------------------------------------------------------*/
37 optStruct3 opt; /* set by OPTENT3 */
38 optEntry * option_def;
39 unsigned int option_def_index;
40
41 unsigned int takeeven, takeodd;
42
43 MALLOCARRAY_NOFAIL(option_def, 100);
44
45 option_def_index = 0; /* incremented by OPTENT3 */
46 OPTENT3(0, "takeeven", OPT_FLAG, NULL, &takeeven, 0);
47 OPTENT3(0, "takeodd", OPT_FLAG, NULL, &takeodd, 0);
48
49 opt.opt_table = option_def;
50 opt.short_allowed = false; /* We have no short (old-fashioned) options */
51 opt.allowNegNum = false; /* We have no parms that are negative numbers */
52
53 pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
54 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
55
56 free(option_def);
57
58 if (takeeven && takeodd)
59 pm_error("You cannot specify both -takeeven and -takeodd options.");
60
61 if (takeodd)
62 cmdlineP->rowsToTake = ODD;
63 else
64 cmdlineP->rowsToTake = EVEN;
65
66 if (argc-1 < 1)
67 cmdlineP->inputFilespec = "-";
68 else if (argc-1 == 1)
69 cmdlineP->inputFilespec = argv[1];
70 else
71 pm_error("You specified too many arguments (%u). The only "
72 "possible argument is the optional input file specification.",
73 argc-1);
74 }
75
76
77
78 int
79 main(int argc, const char ** argv) {
80
81 FILE * ifP;
82 tuple * tuplerow; /* Row from input image */
83 unsigned int row;
84 struct CmdlineInfo cmdline;
85 struct pam inpam;
86 struct pam outpam;
87
88 pm_proginit(&argc, argv);
89
90 parseCommandLine(argc, argv, &cmdline);
91
92 ifP = pm_openr(cmdline.inputFilespec);
93
94 pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
95
96 if (inpam.height < 2 && cmdline.rowsToTake == ODD)
97 pm_error("You requested to take the odd rows, but there aren't "
98 "any odd rows in the image - it has only one row - Row 0");
99
100 tuplerow = pnm_allocpamrow(&inpam);
101
102 outpam = inpam; /* Initial value -- most fields should be same */
103 outpam.file = stdout;
104 if (inpam.height % 2 == 0)
105 outpam.height = inpam.height / 2;
106 else {
107 if (cmdline.rowsToTake == ODD)
108 outpam.height = inpam.height / 2;
109 else
110 outpam.height = inpam.height / 2 + 1;
111 }
112
113 pnm_writepaminit(&outpam);
114
115 {
116 unsigned int modulusToTake;
117 /* The row number mod 2 of the rows that are supposed to go into
118 the output.
119 */
120
121 switch (cmdline.rowsToTake) {
122 case EVEN: modulusToTake = 0; break;
123 case ODD: modulusToTake = 1; break;
124 default: pm_error("INTERNAL ERROR: invalid rowsToTake");
125 }
126
127 /* Read input and write out rows extracted from it */
128 for (row = 0; row < inpam.height; row++) {
129 pnm_readpamrow(&inpam, tuplerow);
130 if (row % 2 == modulusToTake)
131 pnm_writepamrow(&outpam, tuplerow);
132 }
133 }
134 pnm_freepamrow(tuplerow);
135 pm_close(inpam.file);
136 pm_close(outpam.file);
137
138 return 0;
139 }
140