| 1 |
/* This is ppmntsc.c, a program to adjust saturation values in an image |
| 2 |
so they are legal for NTSC or PAL. |
| 3 |
|
| 4 |
It is derived from the program rlelegal.c, dated June 5, 1995, |
| 5 |
which is described below and propagates that program's copyright. |
| 6 |
The derivation was done by Bryan Henderson on 2000.04.21 to convert |
| 7 |
it from operating on the RLE format to operating on the PPM format |
| 8 |
and to rewrite it in a cleaner style, taking advantage of modern C |
| 9 |
compiler technology. |
| 10 |
*/ |
| 11 |
|
| 12 |
|
| 13 |
/* |
| 14 |
* This software is copyrighted as noted below. It may be freely copied, |
| 15 |
* modified, and redistributed, provided that the copyright notice is |
| 16 |
* preserved on all copies. |
| 17 |
* |
| 18 |
* There is no warranty or other guarantee of fitness for this software, |
| 19 |
* it is provided solely "as is". Bug reports or fixes may be sent |
| 20 |
* to the author, who may or may not act on them as he desires. |
| 21 |
* |
| 22 |
* You may not include this software in a program or other software product |
| 23 |
* without supplying the source, or without informing the end-user that the |
| 24 |
* source is available for no extra charge. |
| 25 |
* |
| 26 |
* If you modify this software, you should include a notice giving the |
| 27 |
* name of the person performing the modification, the date of modification, |
| 28 |
* and the reason for such modification. |
| 29 |
*/ |
| 30 |
|
| 31 |
/* |
| 32 |
* rlelegal.c - Make RGB colors legal in the YIQ or YUV color systems. |
| 33 |
* |
| 34 |
* Author: Wes Barris |
| 35 |
* Minnesota Supercomputer Center, Inc. |
| 36 |
* Date: Fri Oct 15, 1993 |
| 37 |
* @Copyright, Research Equipment Inc., d/b/a Minnesota Supercomputer |
| 38 |
* Center, Inc., 1993 |
| 39 |
|
| 40 |
*/ |
| 41 |
|
| 42 |
#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */ |
| 43 |
#define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */ |
| 44 |
#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ |
| 45 |
|
| 46 |
#include <stdio.h> |
| 47 |
#include <math.h> |
| 48 |
#include <string.h> |
| 49 |
|
| 50 |
#include "pm_c_util.h" |
| 51 |
#include "ppm.h" |
| 52 |
#include "mallocvar.h" |
| 53 |
#include "shhopt.h" |
| 54 |
|
| 55 |
enum legalize {RAISE_SAT, LOWER_SAT, ALREADY_LEGAL}; |
| 56 |
/* The actions that make a legal pixel */ |
| 57 |
|
| 58 |
struct cmdlineInfo { |
| 59 |
/* All the information the user supplied in the command line, |
| 60 |
in a form easy for the program to use. |
| 61 |
*/ |
| 62 |
const char * inputFilename; |
| 63 |
unsigned int verbose; |
| 64 |
unsigned int debug; |
| 65 |
unsigned int pal; |
| 66 |
enum {ALL, LEGAL_ONLY, ILLEGAL_ONLY, CORRECTED_ONLY} output; |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
static void |
| 72 |
parseCommandLine(int argc, const char ** argv, |
| 73 |
struct cmdlineInfo * const cmdlineP) { |
| 74 |
/*---------------------------------------------------------------------------- |
| 75 |
Note that many of the strings that this function returns in the |
| 76 |
*cmdlineP structure are actually in the supplied argv array. And |
| 77 |
sometimes, one of these strings is actually just a suffix of an entry |
| 78 |
in argv! |
| 79 |
-----------------------------------------------------------------------------*/ |
| 80 |
optStruct3 opt; |
| 81 |
optEntry *option_def; |
| 82 |
/* Instructions to OptParseOptions on how to parse our options. |
| 83 |
*/ |
| 84 |
unsigned int option_def_index; |
| 85 |
unsigned int legalonly, illegalonly, correctedonly; |
| 86 |
|
| 87 |
MALLOCARRAY(option_def, 100); |
| 88 |
|
| 89 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 90 |
OPTENT3('v', "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); |
| 91 |
OPTENT3('V', "debug", OPT_FLAG, NULL, &cmdlineP->debug, 0); |
| 92 |
OPTENT3('p', "pal", OPT_FLAG, NULL, &cmdlineP->pal, 0); |
| 93 |
OPTENT3('l', "legalonly", OPT_FLAG, NULL, &legalonly, 0); |
| 94 |
OPTENT3('i', "illegalonly", OPT_FLAG, NULL, &illegalonly, 0); |
| 95 |
OPTENT3('c', "correctedonly", OPT_FLAG, NULL, &correctedonly, 0); |
| 96 |
|
| 97 |
opt.opt_table = option_def; |
| 98 |
opt.short_allowed = true; |
| 99 |
opt.allowNegNum = false; |
| 100 |
|
| 101 |
pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); |
| 102 |
|
| 103 |
if (argc - 1 == 0) |
| 104 |
cmdlineP->inputFilename = "-"; /* he wants stdin */ |
| 105 |
else if (argc - 1 == 1) |
| 106 |
cmdlineP->inputFilename = argv[1]; |
| 107 |
else |
| 108 |
pm_error("Too many arguments. The only arguments accepted " |
| 109 |
"are the mask color and optional input file specification"); |
| 110 |
|
| 111 |
if (legalonly + illegalonly + correctedonly > 1) |
| 112 |
pm_error("--legalonly, --illegalonly, and --correctedonly are " |
| 113 |
"conflicting options. Specify at most one of these."); |
| 114 |
|
| 115 |
if (legalonly) |
| 116 |
cmdlineP->output = LEGAL_ONLY; |
| 117 |
else if (illegalonly) |
| 118 |
cmdlineP->output = ILLEGAL_ONLY; |
| 119 |
else if (correctedonly) |
| 120 |
cmdlineP->output = CORRECTED_ONLY; |
| 121 |
else |
| 122 |
cmdlineP->output = ALL; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
static void |
| 128 |
rgbtoyiq(const int r, const int g, const int b, |
| 129 |
double * const y_p, |
| 130 |
double * const i_p, |
| 131 |
double * const q_p) { |
| 132 |
|
| 133 |
*y_p = .299*(r/255.0) + .587*(g/255.0) + .114*(b/255.0); |
| 134 |
*i_p = .596*(r/255.0) - .274*(g/255.0) - .322*(b/255.0); |
| 135 |
*q_p = .211*(r/255.0) - .523*(g/255.0) + .312*(b/255.0); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
static void |
| 141 |
yiqtorgb(const double y, const double i, const double q, |
| 142 |
int * const r_p, int * const g_p, int * const b_p) { |
| 143 |
*r_p = 255.0*(1.00*y + .9562*i + .6214*q); |
| 144 |
*g_p = 255.0*(1.00*y - .2727*i - .6468*q); |
| 145 |
*b_p = 255.0*(1.00*y -1.1037*i +1.7006*q); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static void |
| 151 |
rgbtoyuv(const int r, const int g, const int b, |
| 152 |
double * const y_p, |
| 153 |
double * const u_p, |
| 154 |
double * const v_p) { |
| 155 |
*y_p = .299*(r/255.0) + .587*(g/255.0) + .114*(b/255.0); |
| 156 |
*u_p = -.147*(r/255.0) - .289*(g/255.0) + .437*(b/255.0); |
| 157 |
*v_p = .615*(r/255.0) - .515*(g/255.0) - .100*(b/255.0); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static void |
| 163 |
yuvtorgb(const double y, const double u, const double v, |
| 164 |
int * const r_p, int * const g_p, int * const b_p) { |
| 165 |
|
| 166 |
*r_p = 255.0*(1.00*y + .0000*u +1.1398*v); |
| 167 |
*g_p = 255.0*(1.00*y - .3938*u - .5805*v); |
| 168 |
*b_p = 255.0*(1.00*y +2.0279*u + .0000*v); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
static void |
| 174 |
makeLegalYiq(double const y, |
| 175 |
double const i, |
| 176 |
double const q, |
| 177 |
double * const yNewP, |
| 178 |
double * const iNewP, |
| 179 |
double * const qNewP, |
| 180 |
enum legalize * const actionP) { |
| 181 |
|
| 182 |
double satOld, satNew; |
| 183 |
/* |
| 184 |
* I and Q are legs of a right triangle. Saturation is the hypotenuse. |
| 185 |
*/ |
| 186 |
satOld = sqrt(SQR(i) + SQR(q)); |
| 187 |
if (y+satOld > 1.0) { |
| 188 |
const double diff = 0.5*((y + satOld) - 1.0); |
| 189 |
*yNewP = y - diff; |
| 190 |
satNew = 1.0 - *yNewP; |
| 191 |
*iNewP = i * (satNew/satOld); |
| 192 |
*qNewP = q * (satNew/satOld); |
| 193 |
*actionP = LOWER_SAT; |
| 194 |
} else if (y - satOld <= -0.251) { |
| 195 |
const double diff = 0.5*((satOld - y) - 0.251); |
| 196 |
*yNewP = y + diff; |
| 197 |
satNew = 0.250 + *yNewP; |
| 198 |
*iNewP = i * (satNew/satOld); |
| 199 |
*qNewP = q * (satNew/satOld); |
| 200 |
*actionP = RAISE_SAT; |
| 201 |
} else { |
| 202 |
*yNewP = y; |
| 203 |
*iNewP = i; |
| 204 |
*qNewP = q; |
| 205 |
*actionP = ALREADY_LEGAL; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static void |
| 212 |
make_legal_yuv(const double y, const double u, const double v, |
| 213 |
double * const y_new_p, |
| 214 |
double * const u_new_p, |
| 215 |
double * const v_new_p, |
| 216 |
enum legalize * const action_p |
| 217 |
) { |
| 218 |
|
| 219 |
double sat_old, sat_new; |
| 220 |
/* |
| 221 |
* U and V are legs of a right triangle. Saturation is the hypotenuse. |
| 222 |
*/ |
| 223 |
sat_old = sqrt(u*u + v*v); |
| 224 |
if (y+sat_old >= 1.334) { |
| 225 |
const double diff = 0.5*((y+sat_old) - 1.334); |
| 226 |
*y_new_p = y - diff; |
| 227 |
sat_new = 1.333 - *y_new_p; |
| 228 |
*u_new_p = u*(sat_new/sat_old); |
| 229 |
*v_new_p = v*(sat_new/sat_old); |
| 230 |
*action_p = LOWER_SAT; |
| 231 |
} else if (y-sat_old <= -0.339) { |
| 232 |
const double diff = 0.5*((sat_old-y) - 0.339); |
| 233 |
*y_new_p = y + diff; |
| 234 |
sat_new = 0.338 + *y_new_p; |
| 235 |
*u_new_p = u*(sat_new/sat_old); |
| 236 |
*v_new_p = v*(sat_new/sat_old); |
| 237 |
*action_p = RAISE_SAT; |
| 238 |
} else { |
| 239 |
*u_new_p = u; |
| 240 |
*v_new_p = v; |
| 241 |
*action_p = ALREADY_LEGAL; |
| 242 |
} |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
static void |
| 249 |
make_legal_yiq_i(const int r_in, const int g_in, const int b_in, |
| 250 |
int * const r_out_p, |
| 251 |
int * const g_out_p, |
| 252 |
int * const b_out_p, |
| 253 |
enum legalize * const action_p |
| 254 |
) { |
| 255 |
|
| 256 |
double y, i, q; |
| 257 |
double y_new, i_new, q_new; |
| 258 |
/* |
| 259 |
* Convert to YIQ and compute the new saturation. |
| 260 |
*/ |
| 261 |
rgbtoyiq(r_in, g_in, b_in, &y, &i, &q); |
| 262 |
makeLegalYiq(y, i, q, &y_new, &i_new, &q_new, action_p); |
| 263 |
if (*action_p != ALREADY_LEGAL) |
| 264 |
/* |
| 265 |
* Given the new I and Q, compute new RGB values. |
| 266 |
*/ |
| 267 |
yiqtorgb(y_new, i_new, q_new, r_out_p, g_out_p, b_out_p); |
| 268 |
else { |
| 269 |
*r_out_p = r_in; |
| 270 |
*g_out_p = g_in; |
| 271 |
*b_out_p = b_in; |
| 272 |
} |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static void |
| 279 |
make_legal_yuv_i(const int r_in, const int g_in, const int b_in, |
| 280 |
int * const r_out_p, |
| 281 |
int * const g_out_p, |
| 282 |
int * const b_out_p, |
| 283 |
enum legalize * const action_p |
| 284 |
){ |
| 285 |
|
| 286 |
double y, u, v; |
| 287 |
double y_new, u_new, v_new; |
| 288 |
/* |
| 289 |
* Convert to YUV and compute the new saturation. |
| 290 |
*/ |
| 291 |
rgbtoyuv(r_in, g_in, b_in, &y, &u, &v); |
| 292 |
make_legal_yuv(y, u, v, &y_new, &u_new, &v_new, action_p); |
| 293 |
if (*action_p != ALREADY_LEGAL) |
| 294 |
/* |
| 295 |
* Given the new U and V, compute new RGB values. |
| 296 |
*/ |
| 297 |
yuvtorgb(y_new, u_new, v_new, r_out_p, g_out_p, b_out_p); |
| 298 |
else { |
| 299 |
*r_out_p = r_in; |
| 300 |
*g_out_p = g_in; |
| 301 |
*b_out_p = b_in; |
| 302 |
} |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
static void |
| 309 |
make_legal_yiq_b(const pixel input, |
| 310 |
pixel * const output_p, |
| 311 |
enum legalize * const action_p) { |
| 312 |
|
| 313 |
|
| 314 |
int ir_in, ig_in, ib_in; |
| 315 |
int ir_out, ig_out, ib_out; |
| 316 |
|
| 317 |
ir_in = (int)PPM_GETR(input); |
| 318 |
ig_in = (int)PPM_GETG(input); |
| 319 |
ib_in = (int)PPM_GETB(input); |
| 320 |
|
| 321 |
make_legal_yiq_i(ir_in, ig_in, ib_in, &ir_out, &ig_out, &ib_out, action_p); |
| 322 |
|
| 323 |
PPM_ASSIGN(*output_p, ir_out, ig_out, ib_out); |
| 324 |
|
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
static void |
| 331 |
make_legal_yuv_b(const pixel input, |
| 332 |
pixel * const output_p, |
| 333 |
enum legalize * const action_p) { |
| 334 |
|
| 335 |
int ir_in, ig_in, ib_in; |
| 336 |
int ir_out, ig_out, ib_out; |
| 337 |
|
| 338 |
ir_in = (int)PPM_GETR(input); |
| 339 |
ig_in = (int)PPM_GETG(input); |
| 340 |
ib_in = (int)PPM_GETB(input); |
| 341 |
make_legal_yuv_i(ir_in, ig_in, ib_in, &ir_out, &ig_out, &ib_out, action_p); |
| 342 |
|
| 343 |
PPM_ASSIGN(*output_p, ir_out, ig_out, ib_out); |
| 344 |
|
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
static void |
| 351 |
reportMapping(pixel const oldPixel, |
| 352 |
pixel const newPixel) { |
| 353 |
/*---------------------------------------------------------------------------- |
| 354 |
Assuming oldPixel and newPixel are input and output pixels, |
| 355 |
tell the user that we changed a pixel to make it legal, if in fact we |
| 356 |
did and it isn't the same change that we just reported. |
| 357 |
-----------------------------------------------------------------------------*/ |
| 358 |
static pixel lastChangedPixel; |
| 359 |
static bool firstTime = true; |
| 360 |
|
| 361 |
if (!PPM_EQUAL(oldPixel, newPixel) && |
| 362 |
(firstTime || PPM_EQUAL(oldPixel, lastChangedPixel))) { |
| 363 |
pm_message("Mapping %u %u %u -> %u %u %u\n", |
| 364 |
PPM_GETR(oldPixel), |
| 365 |
PPM_GETG(oldPixel), |
| 366 |
PPM_GETB(oldPixel), |
| 367 |
PPM_GETR(newPixel), |
| 368 |
PPM_GETG(newPixel), |
| 369 |
PPM_GETB(newPixel) |
| 370 |
); |
| 371 |
|
| 372 |
lastChangedPixel = oldPixel; |
| 373 |
firstTime = false; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
static void |
| 380 |
convertOneImage(FILE * const ifP, |
| 381 |
struct cmdlineInfo const cmdline, |
| 382 |
unsigned int * const hiCountP, |
| 383 |
unsigned int * const loCountP) { |
| 384 |
|
| 385 |
/* Parameters of input image: */ |
| 386 |
int rows, cols; |
| 387 |
pixval maxval; |
| 388 |
int format; |
| 389 |
|
| 390 |
ppm_readppminit(ifP, &cols, &rows, &maxval, &format); |
| 391 |
ppm_writeppminit(stdout, cols, rows, maxval, 0); |
| 392 |
{ |
| 393 |
pixel * const inputRow = ppm_allocrow(cols); |
| 394 |
pixel * const outputRow = ppm_allocrow(cols); |
| 395 |
|
| 396 |
pixel black; |
| 397 |
/* A constant - black pixel */ |
| 398 |
|
| 399 |
PPM_ASSIGN(black, 0, 0, 0); |
| 400 |
|
| 401 |
{ |
| 402 |
unsigned int row; |
| 403 |
|
| 404 |
*hiCountP = 0; *loCountP = 0; /* initial values */ |
| 405 |
|
| 406 |
for (row = 0; row < rows; ++row) { |
| 407 |
unsigned int col; |
| 408 |
ppm_readppmrow(ifP, inputRow, cols, maxval, format); |
| 409 |
for (col = 0; col < cols; ++col) { |
| 410 |
pixel corrected; |
| 411 |
/* Corrected or would-be corrected value for pixel */ |
| 412 |
enum legalize action; |
| 413 |
/* What action was used to make pixel legal */ |
| 414 |
if (cmdline.pal) |
| 415 |
make_legal_yuv_b(inputRow[col], |
| 416 |
&corrected, |
| 417 |
&action); |
| 418 |
else |
| 419 |
make_legal_yiq_b(inputRow[col], |
| 420 |
&corrected, |
| 421 |
&action); |
| 422 |
|
| 423 |
if (action == LOWER_SAT) |
| 424 |
++*hiCountP; |
| 425 |
if (action == RAISE_SAT) |
| 426 |
++*loCountP; |
| 427 |
if (cmdline.debug) |
| 428 |
reportMapping(inputRow[col], corrected); |
| 429 |
switch (cmdline.output) { |
| 430 |
case ALL: |
| 431 |
outputRow[col] = corrected; |
| 432 |
break; |
| 433 |
case LEGAL_ONLY: |
| 434 |
outputRow[col] = (action == ALREADY_LEGAL) ? |
| 435 |
inputRow[col] : black; |
| 436 |
break; |
| 437 |
case ILLEGAL_ONLY: |
| 438 |
outputRow[col] = (action != ALREADY_LEGAL) ? |
| 439 |
inputRow[col] : black; |
| 440 |
break; |
| 441 |
case CORRECTED_ONLY: |
| 442 |
outputRow[col] = (action != ALREADY_LEGAL) ? |
| 443 |
corrected : black; |
| 444 |
break; |
| 445 |
} |
| 446 |
} |
| 447 |
ppm_writeppmrow(stdout, outputRow, cols, maxval, 0); |
| 448 |
} |
| 449 |
} |
| 450 |
ppm_freerow(outputRow); |
| 451 |
ppm_freerow(inputRow); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
int |
| 458 |
main(int argc, const char **argv) { |
| 459 |
|
| 460 |
struct cmdlineInfo cmdline; |
| 461 |
FILE * ifP; |
| 462 |
unsigned int totalHiCount, totalLoCount; |
| 463 |
unsigned int imageCount; |
| 464 |
|
| 465 |
int eof; |
| 466 |
|
| 467 |
pm_proginit(&argc, argv); |
| 468 |
|
| 469 |
parseCommandLine(argc, argv, &cmdline); |
| 470 |
|
| 471 |
ifP = pm_openr(cmdline.inputFilename); |
| 472 |
|
| 473 |
imageCount = 0; /* initial value */ |
| 474 |
totalHiCount = 0; /* initial value */ |
| 475 |
totalLoCount = 0; /* initial value */ |
| 476 |
|
| 477 |
eof = false; |
| 478 |
while (!eof) { |
| 479 |
unsigned int hiCount, loCount; |
| 480 |
|
| 481 |
convertOneImage(ifP, cmdline, &hiCount, &loCount); |
| 482 |
|
| 483 |
++imageCount; |
| 484 |
totalHiCount += hiCount; |
| 485 |
totalLoCount += loCount; |
| 486 |
|
| 487 |
ppm_nextimage(ifP, &eof); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
if (cmdline.verbose) { |
| 492 |
pm_message("%u images processed.", imageCount); |
| 493 |
pm_message("%u pixels were above the saturation limit.", |
| 494 |
totalHiCount); |
| 495 |
pm_message("%u pixels were below the saturation limit.", |
| 496 |
totalLoCount); |
| 497 |
} |
| 498 |
|
| 499 |
pm_close(ifP); |
| 500 |
|
| 501 |
return 0; |
| 502 |
} |