| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#define JPEG_CJPEG_DJPEG |
| 15 |
#include "jinclude.h" |
| 16 |
|
| 17 |
#include <ctype.h> |
| 18 |
#ifdef USE_SETMODE |
| 19 |
#include <fcntl.h> |
| 20 |
|
| 21 |
#include <io.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifdef USE_CCOMMAND |
| 25 |
#ifdef __MWERKS__ |
| 26 |
#include <SIOUX.h> |
| 27 |
#include <console.h> |
| 28 |
#endif |
| 29 |
#ifdef THINK_C |
| 30 |
#include <console.h> |
| 31 |
#endif |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef DONT_USE_B_MODE |
| 35 |
#define READ_BINARY "r" |
| 36 |
#else |
| 37 |
#ifdef VMS |
| 38 |
#define READ_BINARY "rb", "ctx=stm" |
| 39 |
#else |
| 40 |
#define READ_BINARY "rb" |
| 41 |
#endif |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifndef EXIT_FAILURE |
| 45 |
#define EXIT_FAILURE 1 |
| 46 |
#endif |
| 47 |
#ifndef EXIT_SUCCESS |
| 48 |
#ifdef VMS |
| 49 |
#define EXIT_SUCCESS 1 |
| 50 |
#else |
| 51 |
#define EXIT_SUCCESS 0 |
| 52 |
#endif |
| 53 |
#endif |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static FILE * infile; |
| 62 |
|
| 63 |
|
| 64 |
#define NEXTBYTE() getc(infile) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
#define ERREXIT(msg) (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE)) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
static int |
| 73 |
read_1_byte (void) |
| 74 |
{ |
| 75 |
int c; |
| 76 |
|
| 77 |
c = NEXTBYTE(); |
| 78 |
if (c == EOF) |
| 79 |
ERREXIT("Premature EOF in JPEG file"); |
| 80 |
return c; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static unsigned int |
| 86 |
read_2_bytes (void) |
| 87 |
{ |
| 88 |
int c1, c2; |
| 89 |
|
| 90 |
c1 = NEXTBYTE(); |
| 91 |
if (c1 == EOF) |
| 92 |
ERREXIT("Premature EOF in JPEG file"); |
| 93 |
c2 = NEXTBYTE(); |
| 94 |
if (c2 == EOF) |
| 95 |
ERREXIT("Premature EOF in JPEG file"); |
| 96 |
return (((unsigned int) c1) << 8) + ((unsigned int) c2); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#define M_SOF0 0xC0 |
| 107 |
#define M_SOF1 0xC1 |
| 108 |
#define M_SOF2 0xC2 |
| 109 |
#define M_SOF3 0xC3 |
| 110 |
#define M_SOF5 0xC5 |
| 111 |
#define M_SOF6 0xC6 |
| 112 |
#define M_SOF7 0xC7 |
| 113 |
#define M_SOF9 0xC9 |
| 114 |
#define M_SOF10 0xCA |
| 115 |
#define M_SOF11 0xCB |
| 116 |
#define M_SOF13 0xCD |
| 117 |
#define M_SOF14 0xCE |
| 118 |
#define M_SOF15 0xCF |
| 119 |
#define M_SOI 0xD8 |
| 120 |
#define M_EOI 0xD9 |
| 121 |
#define M_SOS 0xDA |
| 122 |
#define M_APP0 0xE0 |
| 123 |
#define M_APP12 0xEC |
| 124 |
#define M_COM 0xFE |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static int |
| 138 |
next_marker (void) |
| 139 |
{ |
| 140 |
int c; |
| 141 |
int discarded_bytes = 0; |
| 142 |
|
| 143 |
|
| 144 |
c = read_1_byte(); |
| 145 |
while (c != 0xFF) { |
| 146 |
discarded_bytes++; |
| 147 |
c = read_1_byte(); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
do { |
| 153 |
c = read_1_byte(); |
| 154 |
} while (c == 0xFF); |
| 155 |
|
| 156 |
if (discarded_bytes != 0) { |
| 157 |
fprintf(stderr, "Warning: garbage data found in JPEG file\n"); |
| 158 |
} |
| 159 |
|
| 160 |
return c; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
static int |
| 173 |
first_marker (void) |
| 174 |
{ |
| 175 |
int c1, c2; |
| 176 |
|
| 177 |
c1 = NEXTBYTE(); |
| 178 |
c2 = NEXTBYTE(); |
| 179 |
if (c1 != 0xFF || c2 != M_SOI) |
| 180 |
ERREXIT("Not a JPEG file"); |
| 181 |
return c2; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
static void |
| 195 |
skip_variable (void) |
| 196 |
|
| 197 |
{ |
| 198 |
unsigned int length; |
| 199 |
|
| 200 |
|
| 201 |
length = read_2_bytes(); |
| 202 |
|
| 203 |
if (length < 2) |
| 204 |
ERREXIT("Erroneous JPEG marker length"); |
| 205 |
length -= 2; |
| 206 |
|
| 207 |
while (length > 0) { |
| 208 |
(void) read_1_byte(); |
| 209 |
length--; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
static void |
| 221 |
process_COM (void) |
| 222 |
{ |
| 223 |
unsigned int length; |
| 224 |
int ch; |
| 225 |
int lastch = 0; |
| 226 |
|
| 227 |
|
| 228 |
length = read_2_bytes(); |
| 229 |
|
| 230 |
if (length < 2) |
| 231 |
ERREXIT("Erroneous JPEG marker length"); |
| 232 |
length -= 2; |
| 233 |
|
| 234 |
while (length > 0) { |
| 235 |
ch = read_1_byte(); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
if (ch == '\r') { |
| 242 |
printf("\n"); |
| 243 |
} else if (ch == '\n') { |
| 244 |
if (lastch != '\r') |
| 245 |
printf("\n"); |
| 246 |
} else if (ch == '\\') { |
| 247 |
printf("\\\\"); |
| 248 |
} else if (isprint(ch)) { |
| 249 |
putc(ch, stdout); |
| 250 |
} else { |
| 251 |
printf("\\%03o", ch); |
| 252 |
} |
| 253 |
lastch = ch; |
| 254 |
length--; |
| 255 |
} |
| 256 |
printf("\n"); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static void |
| 266 |
process_SOFn (int marker) |
| 267 |
{ |
| 268 |
unsigned int length; |
| 269 |
unsigned int image_height, image_width; |
| 270 |
int data_precision, num_components; |
| 271 |
const char * process; |
| 272 |
int ci; |
| 273 |
|
| 274 |
length = read_2_bytes(); |
| 275 |
|
| 276 |
data_precision = read_1_byte(); |
| 277 |
image_height = read_2_bytes(); |
| 278 |
image_width = read_2_bytes(); |
| 279 |
num_components = read_1_byte(); |
| 280 |
|
| 281 |
switch (marker) { |
| 282 |
case M_SOF0: process = "Baseline"; break; |
| 283 |
case M_SOF1: process = "Extended sequential"; break; |
| 284 |
case M_SOF2: process = "Progressive"; break; |
| 285 |
case M_SOF3: process = "Lossless"; break; |
| 286 |
case M_SOF5: process = "Differential sequential"; break; |
| 287 |
case M_SOF6: process = "Differential progressive"; break; |
| 288 |
case M_SOF7: process = "Differential lossless"; break; |
| 289 |
case M_SOF9: process = "Extended sequential, arithmetic coding"; break; |
| 290 |
case M_SOF10: process = "Progressive, arithmetic coding"; break; |
| 291 |
case M_SOF11: process = "Lossless, arithmetic coding"; break; |
| 292 |
case M_SOF13: process = "Differential sequential, arithmetic coding"; break; |
| 293 |
case M_SOF14: process = "Differential progressive, arithmetic coding"; break; |
| 294 |
case M_SOF15: process = "Differential lossless, arithmetic coding"; break; |
| 295 |
default: process = "Unknown"; break; |
| 296 |
} |
| 297 |
|
| 298 |
printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n", |
| 299 |
image_width, image_height, num_components, data_precision); |
| 300 |
printf("JPEG process: %s\n", process); |
| 301 |
|
| 302 |
if (length != (unsigned int) (8 + num_components * 3)) |
| 303 |
ERREXIT("Bogus SOF marker length"); |
| 304 |
|
| 305 |
for (ci = 0; ci < num_components; ci++) { |
| 306 |
(void) read_1_byte(); |
| 307 |
(void) read_1_byte(); |
| 308 |
(void) read_1_byte(); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
static int |
| 324 |
scan_JPEG_header (int verbose) |
| 325 |
{ |
| 326 |
int marker; |
| 327 |
|
| 328 |
|
| 329 |
if (first_marker() != M_SOI) |
| 330 |
ERREXIT("Expected SOI marker first"); |
| 331 |
|
| 332 |
|
| 333 |
for (;;) { |
| 334 |
marker = next_marker(); |
| 335 |
switch (marker) { |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
case M_SOF0: |
| 340 |
case M_SOF1: |
| 341 |
case M_SOF2: |
| 342 |
case M_SOF3: |
| 343 |
case M_SOF5: |
| 344 |
case M_SOF6: |
| 345 |
case M_SOF7: |
| 346 |
case M_SOF9: |
| 347 |
case M_SOF10: |
| 348 |
case M_SOF11: |
| 349 |
case M_SOF13: |
| 350 |
case M_SOF14: |
| 351 |
case M_SOF15: |
| 352 |
if (verbose) |
| 353 |
process_SOFn(marker); |
| 354 |
else |
| 355 |
skip_variable(); |
| 356 |
break; |
| 357 |
|
| 358 |
case M_SOS: |
| 359 |
return marker; |
| 360 |
|
| 361 |
case M_EOI: |
| 362 |
return marker; |
| 363 |
|
| 364 |
case M_COM: |
| 365 |
process_COM(); |
| 366 |
break; |
| 367 |
|
| 368 |
case M_APP12: |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
if (verbose) { |
| 373 |
printf("APP12 contains:\n"); |
| 374 |
process_COM(); |
| 375 |
} else |
| 376 |
skip_variable(); |
| 377 |
break; |
| 378 |
|
| 379 |
default: |
| 380 |
skip_variable(); |
| 381 |
break; |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
static const char * progname; |
| 390 |
|
| 391 |
|
| 392 |
static void |
| 393 |
usage (void) |
| 394 |
|
| 395 |
{ |
| 396 |
fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n"); |
| 397 |
|
| 398 |
fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname); |
| 399 |
|
| 400 |
fprintf(stderr, "Switches (names may be abbreviated):\n"); |
| 401 |
fprintf(stderr, " -verbose Also display dimensions of JPEG image\n"); |
| 402 |
|
| 403 |
exit(EXIT_FAILURE); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
static int |
| 408 |
keymatch (char * arg, const char * keyword, int minchars) |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
{ |
| 413 |
register int ca, ck; |
| 414 |
register int nmatched = 0; |
| 415 |
|
| 416 |
while ((ca = *arg++) != '\0') { |
| 417 |
if ((ck = *keyword++) == '\0') |
| 418 |
return 0; |
| 419 |
if (isupper(ca)) |
| 420 |
ca = tolower(ca); |
| 421 |
if (ca != ck) |
| 422 |
return 0; |
| 423 |
nmatched++; |
| 424 |
} |
| 425 |
|
| 426 |
if (nmatched < minchars) |
| 427 |
return 0; |
| 428 |
return 1; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
int |
| 437 |
main (int argc, char **argv) |
| 438 |
{ |
| 439 |
int argn; |
| 440 |
char * arg; |
| 441 |
int verbose = 0; |
| 442 |
|
| 443 |
|
| 444 |
#ifdef USE_CCOMMAND |
| 445 |
argc = ccommand(&argv); |
| 446 |
#endif |
| 447 |
|
| 448 |
progname = argv[0]; |
| 449 |
if (progname == NULL || progname[0] == 0) |
| 450 |
progname = "rdjpgcom"; |
| 451 |
|
| 452 |
|
| 453 |
for (argn = 1; argn < argc; argn++) { |
| 454 |
arg = argv[argn]; |
| 455 |
if (arg[0] != '-') |
| 456 |
break; |
| 457 |
arg++; |
| 458 |
if (keymatch(arg, "verbose", 1)) { |
| 459 |
verbose++; |
| 460 |
} else |
| 461 |
usage(); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
if (argn < argc-1) { |
| 467 |
fprintf(stderr, "%s: only one input file\n", progname); |
| 468 |
usage(); |
| 469 |
} |
| 470 |
if (argn < argc) { |
| 471 |
if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) { |
| 472 |
fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]); |
| 473 |
exit(EXIT_FAILURE); |
| 474 |
} |
| 475 |
} else { |
| 476 |
|
| 477 |
#ifdef USE_SETMODE |
| 478 |
setmode(fileno(stdin), O_BINARY); |
| 479 |
#endif |
| 480 |
#ifdef USE_FDOPEN |
| 481 |
if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) { |
| 482 |
fprintf(stderr, "%s: can't open stdin\n", progname); |
| 483 |
exit(EXIT_FAILURE); |
| 484 |
} |
| 485 |
#else |
| 486 |
infile = stdin; |
| 487 |
#endif |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
(void) scan_JPEG_header(verbose); |
| 492 |
|
| 493 |
|
| 494 |
exit(EXIT_SUCCESS); |
| 495 |
return 0; |
| 496 |
} |