| 1 |
/****************************************************************************** |
| 2 |
pammixinterlace |
| 3 |
******************************************************************************* |
| 4 |
De-interlace an image by merging adjacent rows. |
| 5 |
|
| 6 |
Copyright (C) 2007 Bruce Guenter, FutureQuest, Inc. |
| 7 |
|
| 8 |
Permission to use, copy, modify, and distribute this software and its |
| 9 |
documentation for any purpose and without fee is hereby granted, |
| 10 |
provided that the above copyright notice appear in all copies and that |
| 11 |
both that copyright notice and this permission notice appear in |
| 12 |
supporting documentation. This software is provided "as is" without |
| 13 |
express or implied warranty. |
| 14 |
|
| 15 |
******************************************************************************/ |
| 16 |
|
| 17 |
#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */ |
| 18 |
#define _BSD_SOURCE /* Make sure strcaseeq() is in nstring.h */ |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "pm_c_util.h" |
| 23 |
#include "mallocvar.h" |
| 24 |
#include "nstring.h" |
| 25 |
#include "shhopt.h" |
| 26 |
#include "pam.h" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
static sample |
| 31 |
clamp(sample const val, |
| 32 |
sample const maxval) { |
| 33 |
|
| 34 |
return val < 0 ? 0 : val > maxval ? maxval : val; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
static bool |
| 40 |
distant(int const above, |
| 41 |
int const mid, |
| 42 |
int const below) { |
| 43 |
|
| 44 |
return abs(mid - (above + below) / 2) > abs(above - below); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static void |
| 50 |
filterLinearBlend(tuple * const outputrow, |
| 51 |
tuple ** const tuplerowWindow, |
| 52 |
unsigned int const width, |
| 53 |
unsigned int const depth, |
| 54 |
bool const adaptive, |
| 55 |
sample const maxval) { |
| 56 |
|
| 57 |
unsigned int col; |
| 58 |
|
| 59 |
for (col = 0; col < width; ++col) { |
| 60 |
unsigned int plane; |
| 61 |
|
| 62 |
for (plane = 0; plane < depth; ++plane) { |
| 63 |
int const above = tuplerowWindow[0][col][plane]; |
| 64 |
int const mid = tuplerowWindow[1][col][plane]; |
| 65 |
int const below = tuplerowWindow[2][col][plane]; |
| 66 |
|
| 67 |
sample out; |
| 68 |
|
| 69 |
if (!adaptive || distant(above, mid, below)) |
| 70 |
out = (above + mid * 2 + below) / 4; |
| 71 |
else |
| 72 |
out = mid; |
| 73 |
|
| 74 |
outputrow[col][plane] = out; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static void |
| 82 |
filterFfmpeg(tuple * const outputrow, |
| 83 |
tuple ** const tuplerowWindow, |
| 84 |
unsigned int const width, |
| 85 |
unsigned int const depth, |
| 86 |
bool const adaptive, |
| 87 |
sample const maxval) { |
| 88 |
|
| 89 |
unsigned int col; |
| 90 |
|
| 91 |
for (col = 0; col < width; ++col) { |
| 92 |
unsigned int plane; |
| 93 |
|
| 94 |
for (plane = 0; plane < depth; ++plane) { |
| 95 |
int const above = tuplerowWindow[1][col][plane]; |
| 96 |
int const mid = tuplerowWindow[2][col][plane]; |
| 97 |
int const below = tuplerowWindow[3][col][plane]; |
| 98 |
|
| 99 |
sample out; |
| 100 |
|
| 101 |
if (!adaptive || distant(above, mid, below)) { |
| 102 |
int const a = (- (int)tuplerowWindow[0][col][plane] |
| 103 |
+ above * 4 |
| 104 |
+ mid * 2 |
| 105 |
+ below * 4 |
| 106 |
- (int)tuplerowWindow[4][col][plane]) / 8; |
| 107 |
out = clamp(a, maxval); |
| 108 |
} else |
| 109 |
out = mid; |
| 110 |
|
| 111 |
outputrow[col][plane] = out; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static void |
| 119 |
filterFIR(tuple * const outputrow, |
| 120 |
tuple ** const tuplerowWindow, |
| 121 |
unsigned int const width, |
| 122 |
unsigned int const depth, |
| 123 |
bool const adaptive, |
| 124 |
sample const maxval) { |
| 125 |
|
| 126 |
unsigned int col; |
| 127 |
|
| 128 |
for (col = 0; col < width; ++col) { |
| 129 |
unsigned int plane; |
| 130 |
|
| 131 |
for (plane = 0; plane < depth; ++plane) { |
| 132 |
|
| 133 |
int const above = tuplerowWindow[1][col][plane]; |
| 134 |
int const mid = tuplerowWindow[2][col][plane]; |
| 135 |
int const below = tuplerowWindow[3][col][plane]; |
| 136 |
|
| 137 |
sample out; |
| 138 |
|
| 139 |
if (!adaptive || distant(above, mid, below)) { |
| 140 |
int const a = (- (int)tuplerowWindow[0][col][plane] |
| 141 |
+ above * 2 |
| 142 |
+ mid * 6 |
| 143 |
+ below * 2 |
| 144 |
- (int)tuplerowWindow[4][col][plane]) / 8; |
| 145 |
out = clamp(a, maxval); |
| 146 |
} else |
| 147 |
out = mid; |
| 148 |
|
| 149 |
outputrow[col][plane] = out; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
struct filter { |
| 157 |
const char * name; /* The command-line name of the filter */ |
| 158 |
unsigned int rows; /* The number of rows the filter operates on */ |
| 159 |
void (*filter)(tuple *, tuple **, |
| 160 |
unsigned int width, unsigned int depth, |
| 161 |
bool adaptive, sample maxval); |
| 162 |
}; |
| 163 |
|
| 164 |
static struct filter filters[] = { |
| 165 |
{ "fir", 5, filterFIR }, /* FIR is cleanest and default filter */ |
| 166 |
{ "ffmpeg", 5, filterFfmpeg }, |
| 167 |
{ "linear", 3, filterLinearBlend }, |
| 168 |
}; |
| 169 |
|
| 170 |
struct cmdlineInfo { |
| 171 |
/* All the information the user supplied in the command line, |
| 172 |
in a form easy for the program to use. |
| 173 |
*/ |
| 174 |
const char * inputFileName; /* Names of input files */ |
| 175 |
struct filter * filterP; |
| 176 |
unsigned int adaptive; |
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static void |
| 182 |
parseCommandLine(int argc, char ** argv, |
| 183 |
struct cmdlineInfo * const cmdlineP) { |
| 184 |
/*---------------------------------------------------------------------------- |
| 185 |
Note that the file spec array we return is stored in the storage that |
| 186 |
was passed to us as the argv array. |
| 187 |
-----------------------------------------------------------------------------*/ |
| 188 |
optStruct3 opt; /* set by OPTENT3 */ |
| 189 |
optEntry * option_def; |
| 190 |
unsigned int option_def_index; |
| 191 |
const char * filterName; |
| 192 |
unsigned int filterSpec; |
| 193 |
|
| 194 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 195 |
|
| 196 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 197 |
OPTENT3(0, "filter", OPT_STRING, &filterName, &filterSpec, 0); |
| 198 |
OPTENT3(0, "adaptive", OPT_FLAG, NULL, &cmdlineP->adaptive, 0); |
| 199 |
|
| 200 |
opt.opt_table = option_def; |
| 201 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 202 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 203 |
|
| 204 |
pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); |
| 205 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 206 |
|
| 207 |
if (!filterSpec) |
| 208 |
cmdlineP->filterP = &filters[0]; |
| 209 |
else { |
| 210 |
unsigned int i; |
| 211 |
cmdlineP->filterP = NULL; |
| 212 |
for (i = 0; i < sizeof filters / sizeof(struct filter); ++i) { |
| 213 |
if (strcaseeq(filterName, filters[i].name)) { |
| 214 |
cmdlineP->filterP = &filters[i]; |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
if (!cmdlineP->filterP) |
| 219 |
pm_error("The filter name '%s' is not known.", filterName); |
| 220 |
} |
| 221 |
|
| 222 |
if (argc-1 < 1) |
| 223 |
cmdlineP->inputFileName = "-"; |
| 224 |
else if (argc-1 == 1) |
| 225 |
cmdlineP->inputFileName = argv[1]; |
| 226 |
else |
| 227 |
pm_error("You specified too many arguments (%d). The only " |
| 228 |
"argument is the optional input file specification.", |
| 229 |
argc-1); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
static void |
| 235 |
allocateRowWindowBuffer(struct pam * const pamP, |
| 236 |
tuple ** const tuplerowWindow, |
| 237 |
unsigned int const rows) { |
| 238 |
|
| 239 |
unsigned int row; |
| 240 |
|
| 241 |
for (row = 0; row < rows; ++row) |
| 242 |
tuplerowWindow[row] = pnm_allocpamrow(pamP); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static void |
| 248 |
freeRowWindowBuffer(tuple ** const tuplerowWindow, |
| 249 |
unsigned int const rows) { |
| 250 |
|
| 251 |
unsigned int row; |
| 252 |
|
| 253 |
for (row = 0; row < rows; ++row) |
| 254 |
pnm_freepamrow(tuplerowWindow[row]); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
static void |
| 260 |
slideWindowDown(tuple ** const tuplerowWindow, |
| 261 |
unsigned int const rows) { |
| 262 |
/*---------------------------------------------------------------------------- |
| 263 |
Slide the rows-line tuple row window tuplerowWindow[] down one by moving |
| 264 |
pointers. |
| 265 |
-----------------------------------------------------------------------------*/ |
| 266 |
tuple * const oldrow0 = tuplerowWindow[0]; |
| 267 |
|
| 268 |
unsigned int i; |
| 269 |
|
| 270 |
for (i = 0; i < rows-1; ++i) |
| 271 |
tuplerowWindow[i] = tuplerowWindow[i+1]; |
| 272 |
|
| 273 |
tuplerowWindow[i] = oldrow0; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
int |
| 279 |
main(int argc, char *argv[]) { |
| 280 |
|
| 281 |
FILE * ifP; |
| 282 |
struct cmdlineInfo cmdline; |
| 283 |
struct pam inpam; |
| 284 |
struct pam outpam; |
| 285 |
tuple * tuplerowWindow[5]; |
| 286 |
tuple * outputrow; |
| 287 |
unsigned int rows; |
| 288 |
|
| 289 |
pnm_init(&argc, argv); |
| 290 |
|
| 291 |
parseCommandLine(argc, argv, &cmdline); |
| 292 |
|
| 293 |
rows = cmdline.filterP->rows; |
| 294 |
|
| 295 |
ifP = pm_openr(cmdline.inputFileName); |
| 296 |
|
| 297 |
pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); |
| 298 |
|
| 299 |
outpam = inpam; /* Initial value -- most fields should be same */ |
| 300 |
outpam.file = stdout; |
| 301 |
|
| 302 |
pnm_writepaminit(&outpam); |
| 303 |
|
| 304 |
allocateRowWindowBuffer(&inpam, tuplerowWindow, rows); |
| 305 |
outputrow = pnm_allocpamrow(&outpam); |
| 306 |
|
| 307 |
if (inpam.height < rows) { |
| 308 |
unsigned int row; |
| 309 |
pm_message("WARNING: Image height less than %d. No mixing done.", |
| 310 |
rows); |
| 311 |
for (row = 0; row < inpam.height; ++row) { |
| 312 |
pnm_readpamrow(&inpam, tuplerowWindow[0]); |
| 313 |
pnm_writepamrow(&outpam, tuplerowWindow[0]); |
| 314 |
} |
| 315 |
} else { |
| 316 |
unsigned int row; |
| 317 |
|
| 318 |
for (row = 0; row < rows-1; ++row) |
| 319 |
pnm_readpamrow(&inpam, tuplerowWindow[row]); |
| 320 |
|
| 321 |
/* Pass through first unfilterable rows */ |
| 322 |
for (row = 0; row < rows/2; ++row) |
| 323 |
pnm_writepamrow(&outpam, tuplerowWindow[row]); |
| 324 |
|
| 325 |
for (row = rows / 2 + 1; row < inpam.height - rows / 2 + 1; ++row) { |
| 326 |
pnm_readpamrow(&inpam, tuplerowWindow[rows-1]); |
| 327 |
cmdline.filterP->filter(outputrow, tuplerowWindow, |
| 328 |
inpam.width, inpam.depth, |
| 329 |
cmdline.adaptive, inpam.maxval); |
| 330 |
pnm_writepamrow(&outpam, outputrow); |
| 331 |
|
| 332 |
slideWindowDown(tuplerowWindow, rows); |
| 333 |
} |
| 334 |
|
| 335 |
/* Pass through last rows */ |
| 336 |
for (row = rows/2; row < rows-1; ++row) |
| 337 |
pnm_writepamrow(&outpam, tuplerowWindow[row]); |
| 338 |
} |
| 339 |
|
| 340 |
freeRowWindowBuffer(tuplerowWindow, rows); |
| 341 |
pnm_freepamrow(outputrow); |
| 342 |
pm_close(inpam.file); |
| 343 |
pm_close(outpam.file); |
| 344 |
|
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|