| 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 |
#include <stdio.h> |
| 59 |
|
| 60 |
#include "pm_c_util.h" |
| 61 |
#include "mallocvar.h" |
| 62 |
#include "shhopt.h" |
| 63 |
#include "ppm.h" |
| 64 |
|
| 65 |
typedef unsigned char uint8; |
| 66 |
|
| 67 |
#define CHOP(x) ((x < 0) ? 0 : ((x > 255) ? 255 : x)) |
| 68 |
|
| 69 |
struct CmdlineInfo { |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
const char * inputFileName; |
| 74 |
int width; |
| 75 |
int height; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static void |
| 81 |
parseCommandLine(int argc, const char ** argv, |
| 82 |
struct CmdlineInfo *cmdlineP) { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
optEntry * option_def; |
| 89 |
optStruct3 opt; |
| 90 |
|
| 91 |
unsigned int option_def_index; |
| 92 |
|
| 93 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 94 |
|
| 95 |
option_def_index = 0; |
| 96 |
OPTENT3(0, "width", OPT_INT, &cmdlineP->width, NULL, 0); |
| 97 |
OPTENT3(0, "height", OPT_INT, &cmdlineP->height, NULL, 0); |
| 98 |
|
| 99 |
|
| 100 |
cmdlineP->width = 64; |
| 101 |
cmdlineP->height = 48; |
| 102 |
|
| 103 |
opt.opt_table = option_def; |
| 104 |
opt.short_allowed = FALSE; |
| 105 |
opt.allowNegNum = FALSE; |
| 106 |
|
| 107 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 108 |
|
| 109 |
|
| 110 |
if (cmdlineP->width <= 0) |
| 111 |
pm_error("-width must be positive."); |
| 112 |
if (cmdlineP->width %4 != 0) |
| 113 |
pm_error("-width must be a multiple of 4."); |
| 114 |
if (cmdlineP->height <= 0) |
| 115 |
pm_error("-height must be positive."); |
| 116 |
|
| 117 |
if (argc > 2) |
| 118 |
pm_error("There is at most 1 argument: the input file spec. " |
| 119 |
"You supplied %d", argc-1); |
| 120 |
else { |
| 121 |
if (argc > 1) |
| 122 |
cmdlineP->inputFileName = argv[1]; |
| 123 |
else |
| 124 |
cmdlineP->inputFileName = "-"; |
| 125 |
} |
| 126 |
free(option_def); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static void |
| 132 |
ReadYUV(FILE * const ifP, |
| 133 |
uint8 * const inbuff) { |
| 134 |
|
| 135 |
size_t bytesRead; |
| 136 |
|
| 137 |
bytesRead = fread(inbuff, 1, 6, ifP); |
| 138 |
|
| 139 |
if (bytesRead != 6 ) { |
| 140 |
if (feof(ifP)) |
| 141 |
pm_error("Premature end of input."); |
| 142 |
else |
| 143 |
pm_error("Error reading input."); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
static void |
| 150 |
YUVtoPPM(FILE * const ifP, |
| 151 |
int const width, |
| 152 |
int const height, |
| 153 |
pixel * const pixrow ) { |
| 154 |
|
| 155 |
unsigned int col; |
| 156 |
|
| 157 |
for (col = 0; col < width; ++col) { |
| 158 |
|
| 159 |
uint8 inbuff[6]; |
| 160 |
|
| 161 |
uint8 * const origY = &inbuff[0]; |
| 162 |
uint8 * const origCb = &inbuff[4]; |
| 163 |
uint8 * const origCr = &inbuff[5]; |
| 164 |
int y, u, v; |
| 165 |
int32_t tempR, tempG, tempB; |
| 166 |
pixval r, g, b; |
| 167 |
|
| 168 |
if (col % 4 == 0) { |
| 169 |
ReadYUV(ifP, inbuff); |
| 170 |
u = origCb[0] - 128; |
| 171 |
v = origCr[0] - 128; |
| 172 |
} |
| 173 |
|
| 174 |
y = origY[col % 4] - 16; |
| 175 |
|
| 176 |
tempR = 104635 * v + y * 76310; |
| 177 |
tempG = -25690 * u + -53294 * v + y * 76310; |
| 178 |
tempB = 132278 * u + y * 76310; |
| 179 |
|
| 180 |
r = CHOP((int)(tempR >> 16)); |
| 181 |
g = CHOP((int)(tempG >> 16)); |
| 182 |
b = CHOP((int)(tempB >> 16)); |
| 183 |
|
| 184 |
PPM_ASSIGN(pixrow[col], r, g, b); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
int |
| 191 |
main(int argc, const char **argv) { |
| 192 |
|
| 193 |
pixval const maxval = 255; |
| 194 |
struct CmdlineInfo cmdline; |
| 195 |
FILE * ifP; |
| 196 |
pixel * pixrow; |
| 197 |
unsigned int row; |
| 198 |
|
| 199 |
pm_proginit(&argc, argv); |
| 200 |
|
| 201 |
parseCommandLine(argc, argv, &cmdline); |
| 202 |
|
| 203 |
pixrow = ppm_allocrow(cmdline.width); |
| 204 |
|
| 205 |
pm_message("Reading (%ux%u): '%s'", cmdline.width, cmdline.height, |
| 206 |
cmdline.inputFileName); |
| 207 |
|
| 208 |
ifP = pm_openr(cmdline.inputFileName); |
| 209 |
|
| 210 |
ppm_writeppminit(stdout, cmdline.width, cmdline.height, maxval, 0); |
| 211 |
|
| 212 |
for (row = 0; row < cmdline.height; ++row) { |
| 213 |
YUVtoPPM(ifP, cmdline.width, cmdline.height, pixrow); |
| 214 |
ppm_writeppmrow(stdout, pixrow, cmdline.width, maxval, 0); |
| 215 |
} |
| 216 |
|
| 217 |
if (fgetc(ifP) != EOF) |
| 218 |
pm_message("Extraneous data at end of image."); |
| 219 |
|
| 220 |
pm_close(ifP); |
| 221 |
ppm_freerow(pixrow); |
| 222 |
|
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|