| 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 |
#include <stdbool.h> |
| 60 |
#include <stdio.h> |
| 61 |
#include <ctype.h> |
| 62 |
#include <string.h> |
| 63 |
#include <stdlib.h> |
| 64 |
|
| 65 |
#include "pm_c_util.h" |
| 66 |
#include "ppm.h" |
| 67 |
#include "mallocvar.h" |
| 68 |
|
| 69 |
typedef unsigned char uint8; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
#define YUVMAXVAL 255 |
| 74 |
#define HALFYUVMAXVAL 128 |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static float *mult299, *mult587, *mult114; |
| 84 |
static float *mult16874, *mult33126, *mult5; |
| 85 |
static float *mult41869, *mult08131; |
| 86 |
|
| 87 |
static __inline__ float |
| 88 |
luminance(pixel const p) { |
| 89 |
return mult299[PPM_GETR(p)] |
| 90 |
+ mult587[PPM_GETG(p)] |
| 91 |
+ mult114[PPM_GETB(p)] |
| 92 |
; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static __inline__ float |
| 98 |
chrominanceRed(pixel const p) { |
| 99 |
return mult5[PPM_GETR(p)] |
| 100 |
+ mult41869[PPM_GETG(p)] |
| 101 |
+ mult08131[PPM_GETB(p)] |
| 102 |
; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static __inline__ float |
| 108 |
chrominanceBlue(pixel const p) { |
| 109 |
return mult16874[PPM_GETR(p)] |
| 110 |
+ mult33126[PPM_GETG(p)] |
| 111 |
+ mult5[PPM_GETB(p)] |
| 112 |
; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static void |
| 118 |
createMultiplicationTables(pixval const maxval) { |
| 119 |
|
| •120 |
overflow_add(maxval, 1); |
| 121 |
MALLOCARRAY_NOFAIL(mult299 , maxval+1); |
| 122 |
MALLOCARRAY_NOFAIL(mult587 , maxval+1); |
| 123 |
MALLOCARRAY_NOFAIL(mult114 , maxval+1); |
| 124 |
MALLOCARRAY_NOFAIL(mult16874 , maxval+1); |
| 125 |
MALLOCARRAY_NOFAIL(mult33126 , maxval+1); |
| 126 |
MALLOCARRAY_NOFAIL(mult5 , maxval+1); |
| 127 |
MALLOCARRAY_NOFAIL(mult41869 , maxval+1); |
| 128 |
MALLOCARRAY_NOFAIL(mult08131 , maxval+1); |
| 129 |
|
| 130 |
if (maxval == YUVMAXVAL) { |
| 131 |
|
| 132 |
unsigned int index; |
| 133 |
|
| 134 |
for (index = 0; index <= maxval; ++index) { |
| 135 |
mult299[index] = 0.29900*index; |
| 136 |
mult587[index] = 0.58700*index; |
| 137 |
mult114[index] = 0.11400*index; |
| 138 |
mult5[index] = 0.50000*index; |
| 139 |
mult41869[index] = -0.41869*index; |
| 140 |
mult08131[index] = -0.08131*index; |
| 141 |
mult16874[index] = -0.16874*index; |
| 142 |
mult33126[index] = -0.33126*index; |
| 143 |
} |
| 144 |
} else { |
| 145 |
unsigned int index; |
| 146 |
for (index = 0; index <= maxval; ++index) { |
| 147 |
mult299[index] = 0.29900*index*(maxval/YUVMAXVAL); |
| 148 |
mult587[index] = 0.58700*index*(maxval/YUVMAXVAL); |
| 149 |
mult114[index] = 0.11400*index*(maxval/YUVMAXVAL); |
| 150 |
mult5[index] = 0.50000*index*(maxval/YUVMAXVAL); |
| 151 |
mult41869[index] = -0.41869*index*(maxval/YUVMAXVAL); |
| 152 |
mult08131[index] = -0.08131*index*(maxval/YUVMAXVAL); |
| 153 |
mult16874[index] = -0.16874*index*(maxval/YUVMAXVAL); |
| 154 |
mult33126[index] = -0.33126*index*(maxval/YUVMAXVAL); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static void |
| 162 |
freeMultiplicationTables(void) { |
| 163 |
free(mult299 ); |
| 164 |
free(mult587 ); |
| 165 |
free(mult114 ); |
| 166 |
free(mult16874 ); |
| 167 |
free(mult33126 ); |
| 168 |
free(mult5 ); |
| 169 |
free(mult41869 ); |
| 170 |
free(mult08131 ); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
static void |
| 193 |
ppmToYuv(pixel ** const ppmImage, |
| 194 |
unsigned int const width, |
| 195 |
unsigned int const height, |
| 196 |
uint8 *** const orig_yP, |
| 197 |
uint8 *** const orig_crP, |
| 198 |
uint8 *** const orig_cbP) { |
| 199 |
|
| 200 |
int y; |
| 201 |
uint8 ** orig_y; |
| 202 |
uint8 ** orig_cr; |
| 203 |
uint8 ** orig_cb; |
| 204 |
|
| 205 |
orig_y = *orig_yP; |
| 206 |
orig_cr = *orig_crP; |
| 207 |
orig_cb = *orig_cbP; |
| 208 |
|
| 209 |
for (y = 0; y + 1 < height; y += 2) { |
| 210 |
uint8 *dy0, *dy1; |
| 211 |
uint8 *dcr, *dcb; |
| 212 |
const pixel *src0, *src1; |
| 213 |
|
| 214 |
|
| 215 |
unsigned int x; |
| 216 |
|
| 217 |
src0 = ppmImage[y]; |
| 218 |
src1 = ppmImage[y + 1]; |
| 219 |
|
| 220 |
dy0 = orig_y[y]; |
| 221 |
dy1 = orig_y[y + 1]; |
| 222 |
dcr = orig_cr[y / 2]; |
| 223 |
dcb = orig_cb[y / 2]; |
| 224 |
|
| 225 |
for (x = 0; x + 1 < width; x += 2) { |
| 226 |
dy0[x] = luminance(src0[x]); |
| 227 |
dy1[x] = luminance(src1[x]); |
| 228 |
|
| 229 |
dy0[x+1] = luminance(src0[x+1]); |
| 230 |
dy1[x+1] = luminance(src1[x+1]); |
| 231 |
|
| 232 |
dcr[x/2] = (( |
| 233 |
chrominanceRed(src0[x]) + |
| 234 |
chrominanceRed(src1[x]) + |
| 235 |
chrominanceRed(src0[x+1]) + |
| 236 |
chrominanceRed(src1[x+1]) |
| 237 |
) / 4) + HALFYUVMAXVAL; |
| 238 |
|
| 239 |
dcb[x/2] = (( |
| 240 |
chrominanceBlue(src0[x]) + |
| 241 |
chrominanceBlue(src1[x]) + |
| 242 |
chrominanceBlue(src0[x+1]) + |
| 243 |
chrominanceBlue(src1[x+1]) |
| 244 |
) / 4) + HALFYUVMAXVAL; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static void |
| 252 |
writeYUV(FILE * const ofP, |
| 253 |
unsigned int const width, |
| 254 |
unsigned int const height, |
| 255 |
uint8 ** const orig_y, |
| 256 |
uint8 ** const orig_cr, |
| 257 |
uint8 ** const orig_cb) { |
| 258 |
|
| 259 |
unsigned int y; |
| 260 |
|
| 261 |
for (y = 0; y < height; ++y) |
| 262 |
fwrite(orig_y[y], 1, width, ofP); |
| 263 |
|
| 264 |
for (y = 0; y < height / 2; ++y) |
| 265 |
fwrite(orig_cb[y], 1, width / 2, ofP); |
| 266 |
|
| 267 |
for (y = 0; y < height / 2; ++y) |
| 268 |
fwrite(orig_cr[y], 1, width / 2, ofP); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static void |
| 274 |
allocYUV(int const width, |
| 275 |
int const height, |
| 276 |
uint8 *** const orig_yP, |
| 277 |
uint8 *** const orig_crP, |
| 278 |
uint8 *** const orig_cbP) { |
| 279 |
|
| 280 |
unsigned int y; |
| 281 |
uint8 ** orig_y; |
| 282 |
uint8 ** orig_cr; |
| 283 |
uint8 ** orig_cb; |
| 284 |
|
| 285 |
MALLOCARRAY_NOFAIL(*orig_yP, height); |
| 286 |
orig_y = *orig_yP; |
| 287 |
for (y = 0; y < height; ++y) |
| 288 |
MALLOCARRAY_NOFAIL(orig_y[y], width); |
| 289 |
|
| 290 |
MALLOCARRAY_NOFAIL(*orig_crP, height / 2); |
| 291 |
orig_cr = *orig_crP; |
| 292 |
for (y = 0; y < height / 2; ++y) |
| 293 |
MALLOCARRAY_NOFAIL(orig_cr[y], width / 2); |
| 294 |
|
| 295 |
MALLOCARRAY_NOFAIL(*orig_cbP, height / 2); |
| 296 |
orig_cb = *orig_cbP; |
| 297 |
for (y = 0; y < height / 2; ++y) |
| 298 |
MALLOCARRAY_NOFAIL(orig_cb[y], width / 2); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
static void |
| 304 |
freeYUV(unsigned int const width, |
| 305 |
unsigned int const height, |
| 306 |
uint8 ** const orig_y, |
| 307 |
uint8 ** const orig_cr, |
| 308 |
uint8 ** const orig_cb){ |
| 309 |
|
| 310 |
unsigned int y; |
| 311 |
|
| 312 |
if (orig_y) { |
| 313 |
for (y = 0; y < height; ++y) |
| 314 |
free(orig_y[y]); |
| 315 |
free(orig_y); |
| 316 |
} |
| 317 |
|
| 318 |
if (orig_cr) { |
| 319 |
for (y = 0; y < height / 2; ++y) |
| 320 |
free(orig_cr[y]); |
| 321 |
free(orig_cr); |
| 322 |
} |
| 323 |
|
| 324 |
if (orig_cb) { |
| 325 |
for (y = 0; y < height / 2; ++y) |
| 326 |
free(orig_cb[y]); |
| 327 |
free(orig_cb); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
int |
| 334 |
main(int argc, const char **argv) { |
| 335 |
|
| 336 |
const char *inputFilename; |
| 337 |
FILE * ifP; |
| 338 |
int width, height; |
| 339 |
pixval maxval; |
| 340 |
pixel ** ppmImage; |
| 341 |
uint8 **orig_y, **orig_cr, **orig_cb; |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
int eof; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
int lastWidth, lastHeight; |
| 353 |
pixval lastMaxval; |
| 354 |
|
| 355 |
pm_proginit(&argc, argv); |
| 356 |
|
| 357 |
if (argc-1 > 1) { |
| 358 |
pm_error("Program takes either one argument -- " |
| 359 |
"the input filename -- or no arguments (input is stdin)"); |
| 360 |
exit(1); |
| 361 |
} else if (argc-1 > 0) |
| 362 |
inputFilename = argv[1]; |
| 363 |
else |
| 364 |
inputFilename = "-"; |
| 365 |
|
| 366 |
ifP = pm_openr(inputFilename); |
| 367 |
|
| 368 |
eof = false; |
| 369 |
lastMaxval = 0; |
| 370 |
lastWidth = 0; |
| 371 |
lastHeight = 0; |
| 372 |
orig_y = orig_cr = orig_cb = 0; |
| 373 |
|
| 374 |
while (!eof) { |
| 375 |
ppmImage = ppm_readppm(ifP, &width, &height, &maxval); |
| 376 |
|
| 377 |
if (width % 2 != 0) |
| 378 |
pm_message("Input image has odd number of columns. The rightmost " |
| 379 |
"column will be omitted from the output."); |
| 380 |
if (height % 2 != 0) |
| 381 |
pm_message("Input image has odd number of rows. The bottom " |
| 382 |
"row will be omitted from the output."); |
| 383 |
|
| 384 |
if (maxval != lastMaxval) { |
| 385 |
|
| 386 |
freeMultiplicationTables(); |
| 387 |
createMultiplicationTables(maxval); |
| 388 |
} |
| 389 |
lastMaxval = maxval; |
| 390 |
|
| 391 |
if (height != lastHeight || width != lastWidth) { |
| 392 |
freeYUV(width, height, orig_y, orig_cr, orig_cb); |
| 393 |
|
| 394 |
allocYUV(width, height, &orig_y, &orig_cr, &orig_cb); |
| 395 |
} |
| 396 |
lastHeight = height; |
| 397 |
lastWidth = width; |
| 398 |
|
| 399 |
ppmToYuv(ppmImage, width, height, &orig_y, &orig_cr, &orig_cb); |
| 400 |
|
| 401 |
writeYUV(stdout, (width/2)*2, (height/2)*2, orig_y, orig_cr, orig_cb); |
| 402 |
|
| 403 |
ppm_freearray(ppmImage, height); |
| 404 |
ppm_nextimage(ifP, &eof); |
| 405 |
} |
| 406 |
freeYUV(width, height, orig_y, orig_cr, orig_cb); |
| 407 |
freeMultiplicationTables(); |
| 408 |
pm_close(ifP); |
| 409 |
|
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|