aboutsummaryrefslogtreecommitdiff
path: root/jpgtest.c
blob: 0ec2a1534df12c13168b9006667a4d212e696e19 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/* Copyright (C)2004 Landmark Graphics Corporation
 * Copyright (C)2005, 2006 Sun Microsystems, Inc.
 * Copyright (C)2009-2011 D. R. Commander
 *
 * This library is free software and may be redistributed and/or modified under
 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
 * any later version.  The full license is in the LICENSE.txt file included
 * with this distribution.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * wxWindows Library License for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include "./bmp.h"
#include "./rrutil.h"
#include "./rrtimer.h"
#include "./turbojpeg.h"

#define _throw(op, err) {  \
	printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err);  goto bailout;}
#define _throwunix(m) _throw(m, strerror(errno))
#define _throwtj(m) _throw(m, tjGetErrorStr())
#define _throwbmp(m) _throw(m, bmpgeterr())

#define PAD(v, p) ((v+(p)-1)&(~((p)-1)))

enum {YUVENCODE=1, YUVDECODE};
int forcemmx=0, forcesse=0, forcesse2=0, forcesse3=0, fastupsample=0,
	decomponly=0, yuv=0, quiet=0, dotile=0, pf=BMP_BGR, bu=0, useppm=0,
	scalefactor=1;
const int _ps[BMPPIXELFORMATS]={3, 4, 3, 4, 4, 4};
const int _flags[BMPPIXELFORMATS]={0, 0, TJ_BGR, TJ_BGR,
	TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST};
const int _rindex[BMPPIXELFORMATS]={0, 0, 2, 2, 3, 1};
const int _gindex[BMPPIXELFORMATS]={1, 1, 1, 1, 2, 2};
const int _bindex[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3};
const char *_pfname[]={"RGB", "RGBX", "BGR", "BGRX", "XBGR", "XRGB"};
const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY"};
const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY"};

void printsigfig(double val, int figs)
{
	char format[80];
	double _l=log10(val);  int l;
	if(_l<0.)
	{
		l=(int)fabs(_l);
		sprintf(format, "%%%d.%df", figs+l+2, figs+l);
	}
	else
	{
		l=(int)_l+1;
		if(figs<=l) sprintf(format, "%%.0f");
		else sprintf(format, "%%%d.%df", figs+1, figs-l);
	}	
	printf(format, val);
}

// Decompression test
int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf,
	unsigned long *comptilesize, unsigned char *rgbbuf, int w, int h,
	int jpegsub, int qual, char *filename, int tilesizex, int tilesizey)
{
	char tempstr[1024], qualstr[5]="\0";
	FILE *outfile=NULL;  tjhandle hnd=NULL;
	int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0)
		|(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0)
		|(fastupsample?TJ_FASTUPSAMPLE:0);
	int i, j, ITER, rgbbufalloc=0;
	double start, elapsed;
	int ps=_ps[pf];
	int yuvsize=TJBUFSIZEYUV(w, h, jpegsub), bufsize;
	int scaledw=(yuv==YUVDECODE)? w : (w+scalefactor-1)/scalefactor;
	int scaledh=(yuv==YUVDECODE)? h : (h+scalefactor-1)/scalefactor;
	int pitch=scaledw*ps;

	if(qual>0)
	{
		snprintf(qualstr, 5, "Q%d", qual);
		qualstr[4]=0;
	}

	flags |= _flags[pf];
	if(bu) flags |= TJ_BOTTOMUP;
	if((hnd=tjInitDecompress())==NULL)
		_throwtj("executing tjInitDecompress()");

	bufsize=(yuv==YUVDECODE? yuvsize:pitch*h);
	if(rgbbuf==NULL)
	{
		if((rgbbuf=(unsigned char *)malloc(bufsize)) == NULL)
			_throwunix("allocating image buffer");
		rgbbufalloc=1;
	}
	// Grey image means decompressor did nothing
	memset(rgbbuf, 127, bufsize);

	if(yuv==YUVDECODE)
	{
		if(tjDecompressToYUV(hnd, jpegbuf[0], comptilesize[0], rgbbuf, flags)==-1)
			_throwtj("executing tjDecompressToYUV()");
	}
	else if(tjDecompress(hnd, jpegbuf[0], comptilesize[0], rgbbuf, scaledw,
		pitch, scaledh, ps, flags)==-1)
		_throwtj("executing tjDecompress()");
	ITER=0;
	start=rrtime();
	do
	{
		int tilen=0;
		for(i=0; i<h; i+=tilesizey)
		{
			for(j=0; j<w; j+=tilesizex)
			{
				int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
				if(yuv==YUVDECODE)
				{
					if(tjDecompressToYUV(hnd, jpegbuf[tilen], comptilesize[tilen],
						&rgbbuf[pitch*i+ps*j], flags)==-1)
						_throwtj("executing tjDecompressToYUV()");
				}
				else if(tjDecompress(hnd, jpegbuf[tilen], comptilesize[tilen],
					&rgbbuf[pitch*i+ps*j], scaledw, pitch, scaledh, ps, flags)==-1)
					_throwtj("executing tjDecompress()");
				tilen++;
			}
		}
		ITER++;
	}	while((elapsed=rrtime()-start)<5.);
	if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()");
	hnd=NULL;
	if(quiet)
	{
		printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
		printf("\n");
	}
	else
	{
		printf("D--> Frame rate:           %f fps\n", (double)ITER/elapsed);
		printf("     Dest. throughput:     %f Megapixels/sec\n",
			(double)(w*h)/1000000.*(double)ITER/elapsed);
	}
	if(yuv==YUVDECODE)
	{
		sprintf(tempstr, "%s_%s%s.yuv", filename, _subnames[jpegsub], qualstr);
		if((outfile=fopen(tempstr, "wb"))==NULL)
			_throwunix("opening YUV image for output");
		if(fwrite(rgbbuf, yuvsize, 1, outfile)!=1)
			_throwunix("writing YUV image");
		fclose(outfile);  outfile=NULL;
	}
	else
	{
		if(tilesizex==w && tilesizey==h)
		{
			if(decomponly)
				sprintf(tempstr, "%s_full.%s", filename, useppm?"ppm":"bmp");
			else
				sprintf(tempstr, "%s_%s%s_full.%s", filename, _subnames[jpegsub],
					qualstr, useppm?"ppm":"bmp");
		}
		else sprintf(tempstr, "%s_%s%s_%dx%d.%s", filename, _subnames[jpegsub],
			qualstr, tilesizex, tilesizey, useppm?"ppm":"bmp");
		if(savebmp(tempstr, rgbbuf, scaledw, scaledh, pf, pitch, bu)==-1)
			_throwbmp("saving bitmap");
		sprintf(strrchr(tempstr, '.'), "-err.%s", useppm?"ppm":"bmp");
		if(srcbuf && scalefactor==1)
		{
			if(!quiet)
				printf("Computing compression error and saving to %s.\n", tempstr);
			if(jpegsub==TJ_GRAYSCALE)
			{
				for(j=0; j<h; j++)
				{
					for(i=0; i<w*ps; i+=ps)
					{
						int y=(int)((double)srcbuf[w*ps*j+i+_rindex[pf]]*0.299
							+ (double)srcbuf[w*ps*j+i+_gindex[pf]]*0.587
							+ (double)srcbuf[w*ps*j+i+_bindex[pf]]*0.114 + 0.5);
						if(y>255) y=255;  if(y<0) y=0;
						rgbbuf[pitch*j+i+_rindex[pf]]=abs(rgbbuf[pitch*j+i+_rindex[pf]]-y);
						rgbbuf[pitch*j+i+_gindex[pf]]=abs(rgbbuf[pitch*j+i+_gindex[pf]]-y);
						rgbbuf[pitch*j+i+_bindex[pf]]=abs(rgbbuf[pitch*j+i+_bindex[pf]]-y);
					}
				}
			}		
			else
			{
				for(j=0; j<h; j++) for(i=0; i<w*ps; i++)
					rgbbuf[pitch*j+i]=abs(rgbbuf[pitch*j+i]-srcbuf[w*ps*j+i]);
			}
			if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
				_throwbmp("saving bitmap");
		}
	}

	if(hnd) {tjDestroy(hnd);  hnd=NULL;}
	if(rgbbuf && rgbbufalloc) {free(rgbbuf);  rgbbuf=NULL;}
	return 0;

	bailout:
	if(outfile) {fclose(outfile);  outfile=NULL;}
	if(hnd) {tjDestroy(hnd);  hnd=NULL;}
	if(rgbbuf && rgbbufalloc) {free(rgbbuf);  rgbbuf=NULL;}
	return -1;
}

void dotest(unsigned char *srcbuf, int w, int h, int jpegsub, int qual,
	char *filename)
{
	char tempstr[1024];
	FILE *outfile=NULL;  tjhandle hnd=NULL;
	unsigned char **jpegbuf=NULL, *rgbbuf=NULL;
	double start, elapsed;
	int jpgbufsize=0, i, j, tilesizex=w, tilesizey=h, numtilesx=1, numtilesy=1,
		ITER;
	unsigned long *comptilesize=NULL;
	int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0)
		|(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0)
		|(fastupsample?TJ_FASTUPSAMPLE:0);
	int ps=_ps[pf], tilen;
	int pitch=w*ps, yuvsize=0;

	flags |= _flags[pf];
	if(bu) flags |= TJ_BOTTOMUP;

	if(yuv==YUVENCODE) yuvsize=TJBUFSIZEYUV(w, h, jpegsub);
	if((rgbbuf=(unsigned char *)malloc(max(yuvsize, pitch*h+1))) == NULL)
		_throwunix("allocating image buffer");

	if(!quiet)
	{
		if(yuv==YUVENCODE)
			printf(">>>>>  %s (%s) <--> YUV %s  <<<<<\n", _pfname[pf],
				bu?"Bottom-up":"Top-down", _subnamel[jpegsub]);
		else
			printf(">>>>>  %s (%s) <--> JPEG %s Q%d  <<<<<\n", _pfname[pf],
				bu?"Bottom-up":"Top-down", _subnamel[jpegsub], qual);
	}
	if(yuv) dotile=0;
	if(dotile) {tilesizex=tilesizey=4;}  else {tilesizex=w;  tilesizey=h;}

	do
	{
		tilesizex*=2;  if(tilesizex>w) tilesizex=w;
		tilesizey*=2;  if(tilesizey>h) tilesizey=h;
		numtilesx=(w+tilesizex-1)/tilesizex;
		numtilesy=(h+tilesizey-1)/tilesizey;
		if((comptilesize=(unsigned long *)malloc(sizeof(unsigned long)*numtilesx*numtilesy)) == NULL
		|| (jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)*numtilesx*numtilesy)) == NULL)
			_throwunix("allocating image buffers");
		memset(jpegbuf, 0, sizeof(unsigned char *)*numtilesx*numtilesy);
		for(i=0; i<numtilesx*numtilesy; i++)
		{
			if((jpegbuf[i]=(unsigned char *)malloc(
				yuv==YUVENCODE? TJBUFSIZEYUV(tilesizex, tilesizey, jpegsub)
					: TJBUFSIZE(tilesizex, tilesizey))) == NULL)
				_throwunix("allocating image buffers");
		}

		// Compression test
		if(quiet==1) printf("%s\t%s\t%s\t%d\t",  _pfname[pf], bu?"BU":"TD",
			_subnamel[jpegsub], qual);
		for(i=0; i<h; i++) memcpy(&rgbbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
		if((hnd=tjInitCompress())==NULL)
			_throwtj("executing tjInitCompress()");
		if(yuv==YUVENCODE)
		{
			if(tjEncodeYUV(hnd, rgbbuf, tilesizex, pitch, tilesizey, ps,
				jpegbuf[0], jpegsub, flags)==-1)
				_throwtj("executing tjEncodeYUV()");
			comptilesize[0]=TJBUFSIZEYUV(tilesizex, tilesizey, jpegsub);
		}
		else if(tjCompress(hnd, rgbbuf, tilesizex, pitch, tilesizey, ps,
			jpegbuf[0], &comptilesize[0], jpegsub, qual, flags)==-1)
			_throwtj("executing tjCompress()");
		ITER=0;
		start=rrtime();
		do
		{
			jpgbufsize=0;  tilen=0;
			for(i=0; i<h; i+=tilesizey)
			{
				for(j=0; j<w; j+=tilesizex)
				{
					int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
					if(yuv==YUVENCODE)
					{
						if(tjEncodeYUV(hnd, &rgbbuf[pitch*i+j*ps], tempw, pitch,
							temph, ps, jpegbuf[tilen], jpegsub, flags)==-1)
							_throwtj("executing tjEncodeYUV()");
						comptilesize[tilen]=TJBUFSIZEYUV(tempw, temph, jpegsub);
					}
					else if(tjCompress(hnd, &rgbbuf[pitch*i+j*ps], tempw, pitch,
						temph, ps, jpegbuf[tilen], &comptilesize[tilen], jpegsub, qual,
						flags)==-1)
						_throwtj("executing tjCompress()");
					jpgbufsize+=comptilesize[tilen];
					tilen++;
				}
			}
			ITER++;
		} while((elapsed=rrtime()-start)<5.);
		if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()");
		hnd=NULL;
		if(quiet==1)
		{
			if(tilesizex==w && tilesizey==h) printf("Full     \t");
			else printf("%-4d %-4d\t", tilesizex, tilesizey);
		}
		if(quiet)
		{
			printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
			printf("%c", quiet==2? '\n':'\t');
			printsigfig((double)(w*h*ps)/(double)jpgbufsize, 4);
			printf("%c", quiet==2? '\n':'\t');
		}
		else
		{
			if(tilesizex==w && tilesizey==h) printf("\nFull image\n");
			else printf("\nTile size: %d x %d\n", tilesizex, tilesizey);
			printf("C--> Frame rate:           %f fps\n", (double)ITER/elapsed);
			printf("     Output image size:    %d bytes\n", jpgbufsize);
			printf("     Compression ratio:    %f:1\n",
				(double)(w*h*ps)/(double)jpgbufsize);
			printf("     Source throughput:    %f Megapixels/sec\n",
				(double)(w*h)/1000000.*(double)ITER/elapsed);
			printf("     Output bit stream:    %f Megabits/sec\n",
				(double)jpgbufsize*8./1000000.*(double)ITER/elapsed);
		}
		if(tilesizex==w && tilesizey==h)
		{
			if(yuv==YUVENCODE)
				sprintf(tempstr, "%s_%s.yuv", filename, _subnames[jpegsub]);
			else
				sprintf(tempstr, "%s_%sQ%d.jpg", filename, _subnames[jpegsub], qual);
			if((outfile=fopen(tempstr, "wb"))==NULL)
				_throwunix("opening reference image");
			if(fwrite(jpegbuf[0], jpgbufsize, 1, outfile)!=1)
				_throwunix("writing reference image");
			fclose(outfile);  outfile=NULL;
			if(!quiet) printf("Reference image written to %s\n", tempstr);
		}
		if(yuv==YUVENCODE)
		{
			if(quiet==1) printf("\n");  goto bailout;
		}

		// Decompression test
		if(decomptest(srcbuf, jpegbuf, comptilesize, rgbbuf, w, h, jpegsub, qual,
			filename, tilesizex, tilesizey)==-1)
			goto bailout;

		// Cleanup
		if(outfile) {fclose(outfile);  outfile=NULL;}
		if(jpegbuf)
		{
			for(i=0; i<numtilesx*numtilesy; i++)
				{if(jpegbuf[i]) free(jpegbuf[i]);  jpegbuf[i]=NULL;}
			free(jpegbuf);  jpegbuf=NULL;
		}
		if(comptilesize) {free(comptilesize);  comptilesize=NULL;}
	} while(tilesizex<w || tilesizey<h);

	if(rgbbuf) {free(rgbbuf);  rgbbuf=NULL;}
	return;

	bailout:
	if(outfile) {fclose(outfile);  outfile=NULL;}
	if(jpegbuf)
	{
		for(i=0; i<numtilesx*numtilesy; i++)
			{if(jpegbuf[i]) free(jpegbuf[i]);  jpegbuf[i]=NULL;}
		free(jpegbuf);  jpegbuf=NULL;
	}
	if(comptilesize) {free(comptilesize);  comptilesize=NULL;}
	if(rgbbuf) {free(rgbbuf);  rgbbuf=NULL;}
	if(hnd) {tjDestroy(hnd);  hnd=NULL;}
	return;
}


void dodecomptest(char *filename)
{
	FILE *file=NULL;  tjhandle hnd=NULL;
	unsigned char *jpegbuf=NULL;
	int w=0, h=0, jpegsub=-1;
	unsigned long jpgbufsize=0;
	char *temp=NULL;

	useppm=1;

	if((file=fopen(filename, "rb"))==NULL)
		_throwunix("opening file");
	if(fseek(file, 0, SEEK_END)<0 || (jpgbufsize=ftell(file))<0)
		_throwunix("determining file size");
	if((jpegbuf=(unsigned char *)malloc(jpgbufsize))==NULL)
		_throwunix("allocating memory");
	if(fseek(file, 0, SEEK_SET)<0)
		_throwunix("setting file position");
	if(fread(jpegbuf, jpgbufsize, 1, file)<1)
		_throwunix("reading JPEG data");
	fclose(file);  file=NULL;

	temp=strrchr(filename, '.');
	if(temp!=NULL) *temp='\0';

	if((hnd=tjInitDecompress())==NULL) _throwtj("executing tjInitDecompress()");
	if(tjDecompressHeader2(hnd, jpegbuf, jpgbufsize, &w, &h, &jpegsub)==-1)
		_throwtj("executing tjDecompressHeader2()");
	if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()");
	hnd=NULL;

	if(quiet==1)
	{
		printf("All performance values in Mpixels/sec\n\n");
		printf("Bitmap\tBitmap\tJPEG\tImage Size\tDecomp\n"),
		printf("Format\tOrder\tFormat\t  X    Y  \tPerf\n\n");
		printf("%s\t%s\t%s\t%-4d  %-4d\t", _pfname[pf], bu?"BU":"TD",
			_subnamel[jpegsub], w, h);
	}
	else
	{
		if(yuv==YUVDECODE)
			printf(">>>>>  JPEG --> YUV %s  <<<<<\n", _subnamel[jpegsub]);
		else
			printf(">>>>>  JPEG --> %s (%s)  <<<<<\n", _pfname[pf],
				bu?"Bottom-up":"Top-down");
		printf("\nImage size: %d x %d", w, h);
		if(scalefactor!=1) printf(" --> %d x %d", (w+scalefactor-1)/scalefactor,
			(h+scalefactor-1)/scalefactor);
		printf("\n");
	}

	decomptest(NULL, &jpegbuf, &jpgbufsize, NULL, w, h, jpegsub, 0, filename, w,
		h);

	bailout:
	if(file) {fclose(file);  file=NULL;}
	if(jpegbuf) {free(jpegbuf);  jpegbuf=NULL;}
	if(hnd) {tjDestroy(hnd);  hnd=NULL;}
	return;
}


void usage(char *progname)
{
	printf("USAGE: %s\n", progname);
	printf("       <Inputfile (BMP|PPM)> <%% Quality> [options]\n\n");
	printf("       %s\n", progname);
	printf("       <Inputfile (JPG)> [options]\n\n");
	printf("Options:\n\n");
	printf("-tile = Test performance of the codec when the image is encoded as separate\n");
	printf("     tiles of varying sizes.\n");
	printf("-forcemmx, -forcesse, -forcesse2, -forcesse3 =\n");
	printf("     Force MMX, SSE, SSE2, or SSE3 code paths in the underlying codec\n");
	printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
	printf("     Test the specified color conversion path in the codec (default: BGR)\n");
	printf("-fastupsample = Use fast, inaccurate upsampling code to perform 4:2:2 and 4:2:0\n");
	printf("     YUV decoding in libjpeg decompressor\n");
	printf("-quiet = Output results in tabular rather than verbose format\n");
	printf("-yuvencode = Encode RGB input as planar YUV rather than compressing as JPEG\n");
	printf("-yuvdecode = Decode JPEG image to planar YUV rather than RGB\n");
	printf("-scale 1/N = scale down the width/height of the decompressed JPEG image by a\n");
	printf("     factor of N (N = 1, 2, 4, or 8}\n\n");
	printf("NOTE:  If the quality is specified as a range (e.g. 90-100), a separate\n");
	printf("test will be performed for all quality values in the range.\n\n");
	exit(1);
}


int main(int argc, char *argv[])
{
	unsigned char *bmpbuf=NULL;  int w, h, i;
	int qual=-1, hiqual=-1;  char *temp;
	int minarg=2;

	if(argc<minarg) usage(argv[0]);

	temp=strrchr(argv[1], '.');
	if(temp!=NULL)
	{
		if(!stricmp(temp, ".ppm")) useppm=1;
		if(!stricmp(temp, ".jpg") || !stricmp(temp, ".jpeg")) decomponly=1;
	}

	printf("\n");

	if(argc>minarg)
	{
		for(i=minarg; i<argc; i++)
		{
			if(!stricmp(argv[i], "-yuvencode"))
			{
				printf("Testing YUV planar encoding\n\n");
				yuv=YUVENCODE;  hiqual=qual=100;
			}
			if(!stricmp(argv[i], "-yuvdecode"))
			{
				printf("Testing YUV planar decoding\n\n");
				yuv=YUVDECODE;
			}
		}
	}

	if(!decomponly && yuv!=YUVENCODE)
	{
		minarg=3;
		if(argc<minarg) usage(argv[0]);
		if((qual=atoi(argv[2]))<1 || qual>100)
		{
			puts("ERROR: Quality must be between 1 and 100.");
			exit(1);
		}
		if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
			&& sscanf(&temp[1], "%d", &hiqual)==1 && hiqual>qual && hiqual>=1
			&& hiqual<=100) {}
		else hiqual=qual;
	}

	if(argc>minarg)
	{
		for(i=minarg; i<argc; i++)
		{
			if(!stricmp(argv[i], "-tile")) dotile=1;
			if(!stricmp(argv[i], "-forcesse3"))
			{
				printf("Using SSE3 code\n\n");
				forcesse3=1;
			}
			if(!stricmp(argv[i], "-forcesse2"))
			{
				printf("Using SSE2 code\n\n");
				forcesse2=1;
			}
			if(!stricmp(argv[i], "-forcesse"))
			{
				printf("Using SSE code\n\n");
				forcesse=1;
			}
			if(!stricmp(argv[i], "-forcemmx"))
			{
				printf("Using MMX code\n\n");
				forcemmx=1;
			}
			if(!stricmp(argv[i], "-fastupsample"))
			{
				printf("Using fast upsampling code\n\n");
				fastupsample=1;
			}
			if(!stricmp(argv[i], "-rgb")) pf=BMP_RGB;
			if(!stricmp(argv[i], "-rgbx")) pf=BMP_RGBX;
			if(!stricmp(argv[i], "-bgr")) pf=BMP_BGR;
			if(!stricmp(argv[i], "-bgrx")) pf=BMP_BGRX;
			if(!stricmp(argv[i], "-xbgr")) pf=BMP_XBGR;
			if(!stricmp(argv[i], "-xrgb")) pf=BMP_XRGB;
			if(!stricmp(argv[i], "-bottomup")) bu=1;
			if(!stricmp(argv[i], "-quiet")) quiet=1;
			if(!stricmp(argv[i], "-qq")) quiet=2;
			if(!stricmp(argv[i], "-scale") && i<argc-1)
			{
				int temp1=0, temp2=0;
				if(sscanf(argv[++i], "%d/%d", &temp1, &temp2)!=2
					|| temp1!=1 || temp2<1 || temp2>8 || (temp2&(temp2-1))!=0)
					usage(argv[0]);
				scalefactor=temp2;
			}
		}
	}

	if(scalefactor!=1 && dotile)
	{
		printf("Disabling tiled compression/decompression tests, because these tests do not\n");
		printf("work when scaled decompression is enabled.\n");
		dotile=0;
	}

	if(!decomponly)
	{
		if(loadbmp(argv[1], &bmpbuf, &w, &h, pf, 1, bu)==-1)
			_throwbmp("loading bitmap");
		temp=strrchr(argv[1], '.');
		if(temp!=NULL) *temp='\0';
	}

	if(quiet==1 && !decomponly)
	{
		printf("All performance values in Mpixels/sec\n\n");
		printf("Bitmap\tBitmap\tJPEG\tJPEG\tTile Size\tCompr\tCompr\tDecomp\n");
		printf("Format\tOrder\tFormat\tQual\t X    Y  \tPerf \tRatio\tPerf\n\n");
	}

	if(decomponly)
	{
		dodecomptest(argv[1]);
		printf("\n");
		goto bailout;
	}
	for(i=hiqual; i>=qual; i--)
		dotest(bmpbuf, w, h, TJ_GRAYSCALE, i, argv[1]);
	printf("\n");
	for(i=hiqual; i>=qual; i--)
		dotest(bmpbuf, w, h, TJ_420, i, argv[1]);
	printf("\n");
	for(i=hiqual; i>=qual; i--)
		dotest(bmpbuf, w, h, TJ_422, i, argv[1]);
	printf("\n");
	for(i=hiqual; i>=qual; i--)
		dotest(bmpbuf, w, h, TJ_444, i, argv[1]);
	printf("\n");

	bailout:
	if(bmpbuf) free(bmpbuf);
	return 0;
}