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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* pnmshear.c - read a portable anymap and shear it by some angle
**
** Copyright (C) 1989, 1991 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
#define _XOPEN_SOURCE 500 /* get M_PI in math.h */
#include <assert.h>
#include <math.h>
#include <string.h>
#include "pm_c_util.h"
#include "mallocvar.h"
#include "ppm.h"
#include "pnm.h"
#include "shhopt.h"
#define SCALE 4096
#define HALFSCALE 2048
struct CmdlineInfo {
/* All the information the user supplied in the command line,
in a form easy for the program to use.
*/
const char * inputFileName; /* Name of input file */
double angle; /* requested shear angle, in radians */
unsigned int noantialias; /* -noantialias option */
const char * background; /* NULL if none */
};
static void
parseCommandLine(int argc, const char ** argv,
struct CmdlineInfo *cmdlineP) {
optStruct3 opt;
unsigned int option_def_index = 0;
optEntry * option_def;
unsigned int backgroundSpec;
MALLOCARRAY(option_def, 100);
OPTENT3(0, "noantialias", OPT_FLAG, NULL, &cmdlineP->noantialias, 0);
OPTENT3(0, "background", OPT_STRING, &cmdlineP->background,
&backgroundSpec, 0);
opt.opt_table = option_def;
opt.short_allowed = FALSE;
opt.allowNegNum = TRUE;
pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
if (!backgroundSpec)
cmdlineP->background = NULL;
if (argc-1 < 1)
pm_error("Need an argument: the shear angle.\n");
else {
char *endptr;
cmdlineP->angle = strtod(argv[1], &endptr) * M_PI / 180;
if (*endptr != '\0' || strlen(argv[1]) == 0)
pm_error("Angle argument is not a valid floating point number: "
"'%s'", argv[1]);
if (argc-1 < 2)
cmdlineP->inputFileName = "-";
else {
cmdlineP->inputFileName = argv[2];
if (argc-1 > 2)
pm_error("too many arguments (%d). "
"The only arguments are shear angle and filespec.",
argc-1);
}
}
free(option_def);
}
static void
makeNewXel(xel * const outputXelP,
xel const curXel,
xel const prevXel,
double const fracnew0,
double const omfracnew0,
int const format) {
/*----------------------------------------------------------------------------
Create an output xel as *outputXel, which is part curXel and part
prevXel, the part given by the fractions omfracnew0 and fracnew0,
respectively. These fraction values are the numerator of a fraction
whose denominator is SCALE.
The format of the pixel is 'format'.
-----------------------------------------------------------------------------*/
switch (PNM_FORMAT_TYPE(format)) {
case PPM_TYPE:
PPM_ASSIGN(*outputXelP,
(fracnew0 * PPM_GETR(prevXel)
+ omfracnew0 * PPM_GETR(curXel)
+ HALFSCALE) / SCALE,
(fracnew0 * PPM_GETG(prevXel)
+ omfracnew0 * PPM_GETG(curXel)
+ HALFSCALE) / SCALE,
(fracnew0 * PPM_GETB(prevXel)
+ omfracnew0 * PPM_GETB(curXel)
+ HALFSCALE) / SCALE );
break;
default:
PNM_ASSIGN1(*outputXelP,
(fracnew0 * PNM_GET1(prevXel)
+ omfracnew0 * PNM_GET1(curXel)
+ HALFSCALE) / SCALE );
break;
}
}
static void
shearRow(xel * const xelrow,
unsigned int const cols,
xel * const newxelrow,
unsigned int const newcols,
double const shearCols,
int const format,
xel const bgxel,
bool const antialias) {
/*----------------------------------------------------------------------------
Shear the row 'xelrow' by 'shearCols' columns, and return the result as
'newxelrow'. They are 'cols' and 'newcols' columns wide, respectively.
Fill in the part of the output row that doesn't contain image data with
'bgxel'.
Use antialiasing iff 'antialias'.
The format of the input xels (which implies something about the
output xels too) is 'format'.
-----------------------------------------------------------------------------*/
unsigned int const intShearCols = (unsigned int) shearCols;
assert(shearCols >= 0.0);
if (antialias) {
const long fracnew0 = (shearCols - intShearCols) * SCALE;
const long omfracnew0 = SCALE - fracnew0;
unsigned int col;
xel prevXel;
for (col = 0; col < newcols; ++col)
newxelrow[col] = bgxel;
prevXel = bgxel;
for (col = 0; col < cols; ++col) {
makeNewXel(&newxelrow[intShearCols + col],
xelrow[col], prevXel, fracnew0, omfracnew0,
format);
prevXel = xelrow[col];
}
if (fracnew0 > 0)
/* Need to add a column for what's left over */
makeNewXel(&newxelrow[intShearCols + cols],
bgxel, prevXel, fracnew0, omfracnew0, format);
} else {
unsigned int col;
for (col = 0; col < intShearCols; ++col)
newxelrow[col] = bgxel;
for (col = 0; col < cols; ++col)
newxelrow[intShearCols+col] = xelrow[col];
for (col = intShearCols + cols; col < newcols; ++col)
newxelrow[col] = bgxel;
}
}
static xel
backgroundColor(const char * const backgroundColorName,
xel * const topRow,
int const cols,
xelval const maxval,
int const format) {
xel retval;
if (backgroundColorName) {
retval = pnm_parsecolorxel(backgroundColorName, maxval, format);
} else
retval = pnm_backgroundxelrow(topRow, cols, maxval, format);
return retval;
}
int
main(int argc, const char * argv[]) {
FILE * ifP;
xel * xelrow;
xel * newxelrow;
xel bgxel;
int rows, cols, format;
int newformat, newcols;
int row;
xelval maxval, newmaxval;
double shearfac;
struct CmdlineInfo cmdline;
pm_proginit(&argc, argv);
parseCommandLine(argc, argv, &cmdline);
ifP = pm_openr(cmdline.inputFileName);
pnm_readpnminit(ifP, &cols, &rows, &maxval, &format);
xelrow = pnm_allocrow(cols);
/* Promote PBM files to PGM. */
if (!cmdline.noantialias && PNM_FORMAT_TYPE(format) == PBM_TYPE) {
newformat = PGM_TYPE;
newmaxval = PGM_MAXMAXVAL;
pm_message("promoting from PBM to PGM - "
"use -noantialias to avoid this");
} else {
newformat = format;
newmaxval = maxval;
}
shearfac = fabs(tan(cmdline.angle));
newcols = rows * shearfac + cols + 0.999999;
pnm_writepnminit(stdout, newcols, rows, newmaxval, newformat, 0);
newxelrow = pnm_allocrow(newcols);
for (row = 0; row < rows; ++row) {
double shearCols;
pnm_readpnmrow(ifP, xelrow, cols, newmaxval, format);
if (row == 0)
bgxel = backgroundColor(cmdline.background,
xelrow, cols, newmaxval, format);
if (cmdline.angle > 0.0)
shearCols = row * shearfac;
else
shearCols = (rows - row) * shearfac;
shearRow(xelrow, cols, newxelrow, newcols,
shearCols, format, bgxel, !cmdline.noantialias);
pnm_writepnmrow(stdout, newxelrow, newcols, newmaxval, newformat, 0);
}
pm_close(ifP);
pm_close(stdout);
return 0;
}
|