ReactOS 0.4.15-dev-7788-g1ad9096
rdjpgcom.c File Reference
#include "jinclude.h"
#include <ctype.h>
Include dependency graph for rdjpgcom.c:

Go to the source code of this file.

Macros

#define JPEG_CJPEG_DJPEG   /* to get the command-line config symbols */
 
#define READ_BINARY   "rb"
 
#define EXIT_FAILURE   1
 
#define EXIT_SUCCESS   0
 
#define NEXTBYTE()   getc(infile)
 
#define ERREXIT(msg)   (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
 
#define M_SOF0   0xC0 /* Start Of Frame N */
 
#define M_SOF1   0xC1 /* N indicates which compression process */
 
#define M_SOF2   0xC2 /* Only SOF0-SOF2 are now in common use */
 
#define M_SOF3   0xC3
 
#define M_SOF5   0xC5 /* NB: codes C4 and CC are NOT SOF markers */
 
#define M_SOF6   0xC6
 
#define M_SOF7   0xC7
 
#define M_SOF9   0xC9
 
#define M_SOF10   0xCA
 
#define M_SOF11   0xCB
 
#define M_SOF13   0xCD
 
#define M_SOF14   0xCE
 
#define M_SOF15   0xCF
 
#define M_SOI   0xD8 /* Start Of Image (beginning of datastream) */
 
#define M_EOI   0xD9 /* End Of Image (end of datastream) */
 
#define M_SOS   0xDA /* Start Of Scan (begins compressed data) */
 
#define M_APP0   0xE0 /* Application-specific marker, type N */
 
#define M_APP12   0xEC /* (we don't bother to list all 16 APPn's) */
 
#define M_COM   0xFE /* COMment */
 

Functions

static int read_1_byte (void)
 
static unsigned int read_2_bytes (void)
 
static int next_marker (void)
 
static int first_marker (void)
 
static void skip_variable (void)
 
static void process_COM (int raw)
 
static void process_SOFn (int marker)
 
static int scan_JPEG_header (int verbose, int raw)
 
static void usage (void)
 
static int keymatch (char *arg, const char *keyword, int minchars)
 
int main (int argc, char **argv)
 

Variables

static FILEinfile
 
static const charprogname
 

Macro Definition Documentation

◆ ERREXIT

#define ERREXIT (   msg)    (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))

Definition at line 72 of file rdjpgcom.c.

◆ EXIT_FAILURE

#define EXIT_FAILURE   1

Definition at line 49 of file rdjpgcom.c.

◆ EXIT_SUCCESS

#define EXIT_SUCCESS   0

Definition at line 55 of file rdjpgcom.c.

◆ JPEG_CJPEG_DJPEG

#define JPEG_CJPEG_DJPEG   /* to get the command-line config symbols */

Definition at line 15 of file rdjpgcom.c.

◆ M_APP0

#define M_APP0   0xE0 /* Application-specific marker, type N */

Definition at line 126 of file rdjpgcom.c.

◆ M_APP12

#define M_APP12   0xEC /* (we don't bother to list all 16 APPn's) */

Definition at line 127 of file rdjpgcom.c.

◆ M_COM

#define M_COM   0xFE /* COMment */

Definition at line 128 of file rdjpgcom.c.

◆ M_EOI

#define M_EOI   0xD9 /* End Of Image (end of datastream) */

Definition at line 124 of file rdjpgcom.c.

◆ M_SOF0

#define M_SOF0   0xC0 /* Start Of Frame N */

Definition at line 110 of file rdjpgcom.c.

◆ M_SOF1

#define M_SOF1   0xC1 /* N indicates which compression process */

Definition at line 111 of file rdjpgcom.c.

◆ M_SOF10

#define M_SOF10   0xCA

Definition at line 118 of file rdjpgcom.c.

◆ M_SOF11

#define M_SOF11   0xCB

Definition at line 119 of file rdjpgcom.c.

◆ M_SOF13

#define M_SOF13   0xCD

Definition at line 120 of file rdjpgcom.c.

◆ M_SOF14

#define M_SOF14   0xCE

Definition at line 121 of file rdjpgcom.c.

◆ M_SOF15

#define M_SOF15   0xCF

Definition at line 122 of file rdjpgcom.c.

◆ M_SOF2

#define M_SOF2   0xC2 /* Only SOF0-SOF2 are now in common use */

Definition at line 112 of file rdjpgcom.c.

◆ M_SOF3

#define M_SOF3   0xC3

Definition at line 113 of file rdjpgcom.c.

◆ M_SOF5

#define M_SOF5   0xC5 /* NB: codes C4 and CC are NOT SOF markers */

Definition at line 114 of file rdjpgcom.c.

◆ M_SOF6

#define M_SOF6   0xC6

Definition at line 115 of file rdjpgcom.c.

◆ M_SOF7

#define M_SOF7   0xC7

Definition at line 116 of file rdjpgcom.c.

◆ M_SOF9

#define M_SOF9   0xC9

Definition at line 117 of file rdjpgcom.c.

◆ M_SOI

#define M_SOI   0xD8 /* Start Of Image (beginning of datastream) */

Definition at line 123 of file rdjpgcom.c.

◆ M_SOS

#define M_SOS   0xDA /* Start Of Scan (begins compressed data) */

Definition at line 125 of file rdjpgcom.c.

◆ NEXTBYTE

#define NEXTBYTE ( )    getc(infile)

Definition at line 68 of file rdjpgcom.c.

◆ READ_BINARY

#define READ_BINARY   "rb"

Definition at line 44 of file rdjpgcom.c.

Function Documentation

◆ first_marker()

static int first_marker ( void  )
static

Definition at line 177 of file rdjpgcom.c.

178{
179 int c1, c2;
180
181 c1 = NEXTBYTE();
182 c2 = NEXTBYTE();
183 if (c1 != 0xFF || c2 != M_SOI)
184 ERREXIT("Not a JPEG file");
185 return c2;
186}
#define ERREXIT(msg)
Definition: rdjpgcom.c:72
#define NEXTBYTE()
Definition: rdjpgcom.c:68
#define M_SOI
Definition: rdjpgcom.c:123

Referenced by read_markers(), and scan_JPEG_header().

◆ keymatch()

static int keymatch ( char arg,
const char keyword,
int  minchars 
)
static

Definition at line 425 of file rdjpgcom.c.

429{
430 register int ca, ck;
431 register int nmatched = 0;
432
433 while ((ca = *arg++) != '\0') {
434 if ((ck = *keyword++) == '\0')
435 return 0; /* arg longer than keyword, no good */
436 if (isupper(ca)) /* force arg to lcase (assume ck is already) */
437 ca = tolower(ca);
438 if (ca != ck)
439 return 0; /* no good */
440 nmatched++; /* count matched characters */
441 }
442 /* reached end of argument; fail if it's too short for unique abbrev */
443 if (nmatched < minchars)
444 return 0;
445 return 1; /* A-OK */
446}
#define isupper(c)
Definition: acclib.h:71
int tolower(int c)
Definition: utclib.c:902
const char int minchars
Definition: cdjpeg.h:154
static const WCHAR ca[]
Definition: main.c:455

Referenced by main().

◆ main()

int main ( int argc  ,
char **  argv 
)

Definition at line 454 of file rdjpgcom.c.

455{
456 int argn;
457 char * arg;
458 int verbose = 0, raw = 0;
459
460 /* On Mac, fetch a command line. */
461#ifdef USE_CCOMMAND
462 argc = ccommand(&argv);
463#endif
464
465 progname = argv[0];
466 if (progname == NULL || progname[0] == 0)
467 progname = "rdjpgcom"; /* in case C library doesn't provide it */
468
469 /* Parse switches, if any */
470 for (argn = 1; argn < argc; argn++) {
471 arg = argv[argn];
472 if (arg[0] != '-')
473 break; /* not switch, must be file name */
474 arg++; /* advance over '-' */
475 if (keymatch(arg, "verbose", 1)) {
476 verbose++;
477 } else if (keymatch(arg, "raw", 1)) {
478 raw = 1;
479 } else
480 usage();
481 }
482
483 /* Open the input file. */
484 /* Unix style: expect zero or one file name */
485 if (argn < argc-1) {
486 fprintf(stderr, "%s: only one input file\n", progname);
487 usage();
488 }
489 if (argn < argc) {
490 if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
491 fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
493 }
494 } else {
495 /* default input file is stdin */
496#ifdef USE_SETMODE /* need to hack file mode? */
498#endif
499#ifdef USE_FDOPEN /* need to re-open in binary mode? */
500 if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
501 fprintf(stderr, "%s: can't open stdin\n", progname);
503 }
504#else
505 infile = stdin;
506#endif
507 }
508
509 /* Scan the JPEG headers. */
511
512 /* All done. */
514 return 0; /* suppress no-return-value warnings */
515}
static int argc
Definition: ServiceArgs.c:12
#define fileno
Definition: acwin.h:102
#define O_BINARY
Definition: acwin.h:109
#define NULL
Definition: types.h:112
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_ _CRTIMP FILE *__cdecl fdopen(_In_ int _FileHandle, _In_z_ const char *_Format)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
#define stdin
Definition: stdio.h:98
#define argv
Definition: mplay32.c:18
static int scan_JPEG_header(int verbose, int raw)
Definition: rdjpgcom.c:340
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
#define READ_BINARY
Definition: rdjpgcom.c:44
#define EXIT_FAILURE
Definition: rdjpgcom.c:49
static int keymatch(char *arg, const char *keyword, int minchars)
Definition: rdjpgcom.c:425
static const char * progname
Definition: rdjpgcom.c:405
static void usage(void)
Definition: rdjpgcom.c:409
static FILE * infile
Definition: rdjpgcom.c:65
#define verbose
Definition: rosglue.h:36
#define exit(n)
Definition: config.h:202
#define setmode(f, m)
Definition: io.h:42
void * arg
Definition: msvc.h:10

◆ next_marker()

static int next_marker ( void  )
static

Definition at line 142 of file rdjpgcom.c.

143{
144 int c;
145 int discarded_bytes = 0;
146
147 /* Find 0xFF byte; count and skip any non-FFs. */
148 c = read_1_byte();
149 while (c != 0xFF) {
150 discarded_bytes++;
151 c = read_1_byte();
152 }
153 /* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
154 * are legal as pad bytes, so don't count them in discarded_bytes.
155 */
156 do {
157 c = read_1_byte();
158 } while (c == 0xFF);
159
160 if (discarded_bytes != 0) {
161 fprintf(stderr, "Warning: garbage data found in JPEG file\n");
162 }
163
164 return c;
165}
const GLubyte * c
Definition: glext.h:8905
#define c
Definition: ke_i.h:80
static int read_1_byte(void)
Definition: rdjpgcom.c:77

Referenced by jpeg_resync_to_restart(), read_markers(), read_restart_marker(), and scan_JPEG_header().

◆ process_COM()

static void process_COM ( int  raw)
static

Definition at line 225 of file rdjpgcom.c.

226{
227 unsigned int length;
228 int ch;
229 int lastch = 0;
230
231 /* Bill Allombert: set locale properly for isprint */
232#ifdef HAVE_LOCALE_H
233 setlocale(LC_CTYPE, "");
234#endif
235
236 /* Get the marker parameter length count */
238 /* Length includes itself, so must be at least 2 */
239 if (length < 2)
240 ERREXIT("Erroneous JPEG marker length");
241 length -= 2;
242
243 while (length > 0) {
244 ch = read_1_byte();
245 if (raw) {
246 putc(ch, stdout);
247 /* Emit the character in a readable form.
248 * Nonprintables are converted to \nnn form,
249 * while \ is converted to \\.
250 * Newlines in CR, CR/LF, or LF form will be printed as one newline.
251 */
252 } else if (ch == '\r') {
253 printf("\n");
254 } else if (ch == '\n') {
255 if (lastch != '\r')
256 printf("\n");
257 } else if (ch == '\\') {
258 printf("\\\\");
259 } else if (isprint(ch)) {
260 putc(ch, stdout);
261 } else {
262 printf("\\%03o", ch);
263 }
264 lastch = ch;
265 length--;
266 }
267 printf("\n");
268
269 /* Bill Allombert: revert to C locale */
270#ifdef HAVE_LOCALE_H
271 setlocale(LC_CTYPE, "C");
272#endif
273}
#define isprint(c)
Definition: acclib.h:73
#define printf
Definition: freeldr.h:93
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
#define LC_CTYPE
Definition: locale.h:19
#define stdout
Definition: stdio.h:99
_Check_return_opt_ _CRTIMP int __cdecl putc(_In_ int _Ch, _Inout_ FILE *_File)
static unsigned int read_2_bytes(void)
Definition: rdjpgcom.c:90
#define setlocale(n, s)
Definition: locale.h:46

Referenced by scan_JPEG_header().

◆ process_SOFn()

static void process_SOFn ( int  marker)
static

Definition at line 282 of file rdjpgcom.c.

283{
284 unsigned int length;
285 unsigned int image_height, image_width;
286 int data_precision, num_components;
287 const char * process;
288 int ci;
289
290 length = read_2_bytes(); /* usual parameter length count */
291
292 data_precision = read_1_byte();
295 num_components = read_1_byte();
296
297 switch (marker) {
298 case M_SOF0: process = "Baseline"; break;
299 case M_SOF1: process = "Extended sequential"; break;
300 case M_SOF2: process = "Progressive"; break;
301 case M_SOF3: process = "Lossless"; break;
302 case M_SOF5: process = "Differential sequential"; break;
303 case M_SOF6: process = "Differential progressive"; break;
304 case M_SOF7: process = "Differential lossless"; break;
305 case M_SOF9: process = "Extended sequential, arithmetic coding"; break;
306 case M_SOF10: process = "Progressive, arithmetic coding"; break;
307 case M_SOF11: process = "Lossless, arithmetic coding"; break;
308 case M_SOF13: process = "Differential sequential, arithmetic coding"; break;
309 case M_SOF14: process = "Differential progressive, arithmetic coding"; break;
310 case M_SOF15: process = "Differential lossless, arithmetic coding"; break;
311 default: process = "Unknown"; break;
312 }
313
314 printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
315 image_width, image_height, num_components, data_precision);
316 printf("JPEG process: %s\n", process);
317
318 if (length != (unsigned int) (8 + num_components * 3))
319 ERREXIT("Bogus SOF marker length");
320
321 for (ci = 0; ci < num_components; ci++) {
322 (void) read_1_byte(); /* Component ID code */
323 (void) read_1_byte(); /* H, V sampling factors */
324 (void) read_1_byte(); /* Quantization table number */
325 }
326}
int marker
Definition: jpeglib.h:1030
int image_height
int image_width
#define M_SOF7
Definition: rdjpgcom.c:116
#define M_SOF6
Definition: rdjpgcom.c:115
#define M_SOF11
Definition: rdjpgcom.c:119
#define M_SOF9
Definition: rdjpgcom.c:117
#define M_SOF15
Definition: rdjpgcom.c:122
#define M_SOF3
Definition: rdjpgcom.c:113
#define M_SOF13
Definition: rdjpgcom.c:120
#define M_SOF10
Definition: rdjpgcom.c:118
#define M_SOF1
Definition: rdjpgcom.c:111
#define M_SOF5
Definition: rdjpgcom.c:114
#define M_SOF14
Definition: rdjpgcom.c:121
#define M_SOF0
Definition: rdjpgcom.c:110
#define M_SOF2
Definition: rdjpgcom.c:112

Referenced by scan_JPEG_header().

◆ read_1_byte()

static int read_1_byte ( void  )
static

Definition at line 77 of file rdjpgcom.c.

78{
79 int c;
80
81 c = NEXTBYTE();
82 if (c == EOF)
83 ERREXIT("Premature EOF in JPEG file");
84 return c;
85}
#define EOF
Definition: stdio.h:24

Referenced by next_marker(), process_COM(), process_SOFn(), and skip_variable().

◆ read_2_bytes()

static unsigned int read_2_bytes ( void  )
static

Definition at line 90 of file rdjpgcom.c.

91{
92 int c1, c2;
93
94 c1 = NEXTBYTE();
95 if (c1 == EOF)
96 ERREXIT("Premature EOF in JPEG file");
97 c2 = NEXTBYTE();
98 if (c2 == EOF)
99 ERREXIT("Premature EOF in JPEG file");
100 return (((unsigned int) c1) << 8) + ((unsigned int) c2);
101}
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31

Referenced by process_COM(), process_SOFn(), and skip_variable().

◆ scan_JPEG_header()

static int scan_JPEG_header ( int  verbose,
int  raw 
)
static

Definition at line 340 of file rdjpgcom.c.

341{
342 int marker;
343
344 /* Expect SOI at start of file */
345 if (first_marker() != M_SOI)
346 ERREXIT("Expected SOI marker first");
347
348 /* Scan miscellaneous markers until we reach SOS. */
349 for (;;) {
351 switch (marker) {
352 /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
353 * treated as SOFn. C4 in particular is actually DHT.
354 */
355 case M_SOF0: /* Baseline */
356 case M_SOF1: /* Extended sequential, Huffman */
357 case M_SOF2: /* Progressive, Huffman */
358 case M_SOF3: /* Lossless, Huffman */
359 case M_SOF5: /* Differential sequential, Huffman */
360 case M_SOF6: /* Differential progressive, Huffman */
361 case M_SOF7: /* Differential lossless, Huffman */
362 case M_SOF9: /* Extended sequential, arithmetic */
363 case M_SOF10: /* Progressive, arithmetic */
364 case M_SOF11: /* Lossless, arithmetic */
365 case M_SOF13: /* Differential sequential, arithmetic */
366 case M_SOF14: /* Differential progressive, arithmetic */
367 case M_SOF15: /* Differential lossless, arithmetic */
368 if (verbose)
370 else
372 break;
373
374 case M_SOS: /* stop before hitting compressed data */
375 return marker;
376
377 case M_EOI: /* in case it's a tables-only JPEG stream */
378 return marker;
379
380 case M_COM:
381 process_COM(raw);
382 break;
383
384 case M_APP12:
385 /* Some digital camera makers put useful textual information into
386 * APP12 markers, so we print those out too when in -verbose mode.
387 */
388 if (verbose) {
389 printf("APP12 contains:\n");
390 process_COM(raw);
391 } else
393 break;
394
395 default: /* Anything else just gets skipped */
396 skip_variable(); /* we assume it has a parameter count... */
397 break;
398 }
399 } /* end loop */
400}
static void process_COM(int raw)
Definition: rdjpgcom.c:225
#define M_EOI
Definition: rdjpgcom.c:124
static void skip_variable(void)
Definition: rdjpgcom.c:199
static void process_SOFn(int marker)
Definition: rdjpgcom.c:282
#define M_APP12
Definition: rdjpgcom.c:127
static int first_marker(void)
Definition: rdjpgcom.c:177
static int next_marker(void)
Definition: rdjpgcom.c:142
#define M_COM
Definition: rdjpgcom.c:128
#define M_SOS
Definition: rdjpgcom.c:125

Referenced by main().

◆ skip_variable()

static void skip_variable ( void  )
static

Definition at line 199 of file rdjpgcom.c.

201{
202 unsigned int length;
203
204 /* Get the marker parameter length count */
206 /* Length includes itself, so must be at least 2 */
207 if (length < 2)
208 ERREXIT("Erroneous JPEG marker length");
209 length -= 2;
210 /* Skip over the remaining bytes */
211 while (length > 0) {
212 (void) read_1_byte();
213 length--;
214 }
215}

Referenced by jinit_marker_reader(), read_markers(), and scan_JPEG_header().

◆ usage()

static void usage ( void  )
static

Definition at line 409 of file rdjpgcom.c.

411{
412 fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n");
413
414 fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname);
415
416 fprintf(stderr, "Switches (names may be abbreviated):\n");
417 fprintf(stderr, " -raw Display non-printable characters in comments (unsafe)\n");
418 fprintf(stderr, " -verbose Also display dimensions of JPEG image\n");
419
421}

Referenced by main().

Variable Documentation

◆ infile

FILE* infile
static

Definition at line 65 of file rdjpgcom.c.

Referenced by jpeg_stdio_src(), main(), read_JPEG_file(), select_file_type(), and write_one_file().

◆ progname

const char* progname
static

Definition at line 405 of file rdjpgcom.c.

Referenced by main(), and usage().