blob: b09cdaaf405b19d4e04ce5789a68304ee79c819b [file] [log] [blame]
DRCb8b359a2011-05-25 03:54:56 +00001/*
DRC95e4cb22014-03-12 06:51:39 +00002 * Copyright (C)2009-2014 D. R. Commander. All Rights Reserved.
DRCb8b359a2011-05-25 03:54:56 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
DRCc6501f72013-11-04 23:07:54 +000032#include <ctype.h>
DRCb8b359a2011-05-25 03:54:56 +000033#include <math.h>
34#include <errno.h>
35#include <cdjpeg.h>
36#include "./bmp.h"
37#include "./tjutil.h"
38#include "./turbojpeg.h"
39
40
41#define _throw(op, err) { \
42 printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
43 retval=-1; goto bailout;}
44#define _throwunix(m) _throw(m, strerror(errno))
45#define _throwtj(m) _throw(m, tjGetErrorStr())
46#define _throwbmp(m) _throw(m, bmpgeterr())
47
DRC95e4cb22014-03-12 06:51:39 +000048int flags=TJFLAG_NOREALLOC, componly=0, decomponly=0, doyuv=0, quiet=0,
49 dotile=0, pf=TJPF_BGR, yuvpad=1, warmup=1;
DRCb8b359a2011-05-25 03:54:56 +000050char *ext="ppm";
51const char *pixFormatStr[TJ_NUMPF]=
52{
53 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY"
54};
55const char *subNameLong[TJ_NUMSAMP]=
56{
DRC1f3635c2013-08-18 10:19:00 +000057 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
DRCb8b359a2011-05-25 03:54:56 +000058};
DRCcd7c3e62013-08-23 02:49:25 +000059const char *csName[TJ_NUMCS]=
60{
61 "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
62};
DRC1f3635c2013-08-18 10:19:00 +000063const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440", "411"};
DRCb8b359a2011-05-25 03:54:56 +000064tjscalingfactor *scalingfactors=NULL, sf={1, 1}; int nsf=0;
65int xformop=TJXOP_NONE, xformopt=0;
DRCf5467112011-09-20 05:02:19 +000066int (*customFilter)(short *, tjregion, tjregion, int, int, tjtransform *);
DRCb8b359a2011-05-25 03:54:56 +000067double benchtime=5.0;
68
69
DRCcd7c3e62013-08-23 02:49:25 +000070char *formatName(int subsamp, int cs, char *buf)
71{
DRC38cb1ec2013-08-23 04:45:43 +000072 if(cs==TJCS_YCbCr) return (char *)subNameLong[subsamp];
DRCcd7c3e62013-08-23 02:49:25 +000073 else if(cs==TJCS_YCCK)
74 {
75 snprintf(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
76 return buf;
77 }
DRC38cb1ec2013-08-23 04:45:43 +000078 else return (char *)csName[cs];
DRCcd7c3e62013-08-23 02:49:25 +000079}
80
81
DRCb8b359a2011-05-25 03:54:56 +000082char *sigfig(double val, int figs, char *buf, int len)
83{
84 char format[80];
85 int digitsafterdecimal=figs-(int)ceil(log10(fabs(val)));
86 if(digitsafterdecimal<1) snprintf(format, 80, "%%.0f");
87 else snprintf(format, 80, "%%.%df", digitsafterdecimal);
88 snprintf(buf, len, format, val);
89 return buf;
90}
91
92
DRC57a37362011-09-17 00:41:14 +000093/* Custom DCT filter which produces a negative of the image */
DRC7bf04d32011-09-17 00:18:31 +000094int dummyDCTFilter(short *coeffs, tjregion arrayRegion, tjregion planeRegion,
DRCf5467112011-09-20 05:02:19 +000095 int componentIndex, int transformIndex, tjtransform *transform)
DRC7bf04d32011-09-17 00:18:31 +000096{
97 int i;
98 for(i=0; i<arrayRegion.w*arrayRegion.h; i++) coeffs[i]=-coeffs[i];
99 return 0;
100}
101
DRC57a37362011-09-17 00:41:14 +0000102
DRCb8b359a2011-05-25 03:54:56 +0000103/* Decompression test */
DRC95e4cb22014-03-12 06:51:39 +0000104int decomp(unsigned char *srcbuf, unsigned char **jpegbuf,
DRCb8b359a2011-05-25 03:54:56 +0000105 unsigned long *jpegsize, unsigned char *dstbuf, int w, int h,
106 int subsamp, int jpegqual, char *filename, int tilew, int tileh)
107{
108 char tempstr[1024], sizestr[20]="\0", qualstr[6]="\0", *ptr;
109 FILE *file=NULL; tjhandle handle=NULL;
DRC95e4cb22014-03-12 06:51:39 +0000110 int row, col, iter=0, dstbufalloc=0, retval=0;
111 double elapsed, elapsedDecode;
DRCb8b359a2011-05-25 03:54:56 +0000112 int ps=tjPixelSize[pf];
DRCc6501f72013-11-04 23:07:54 +0000113 int scaledw=TJSCALED(w, sf);
114 int scaledh=TJSCALED(h, sf);
DRCb8b359a2011-05-25 03:54:56 +0000115 int pitch=scaledw*ps;
116 int ntilesw=(w+tilew-1)/tilew, ntilesh=(h+tileh-1)/tileh;
DRC95e4cb22014-03-12 06:51:39 +0000117 unsigned char *dstptr, *dstptr2, *yuvbuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000118
119 if(jpegqual>0)
120 {
121 snprintf(qualstr, 6, "_Q%d", jpegqual);
122 qualstr[5]=0;
123 }
124
125 if((handle=tjInitDecompress())==NULL)
126 _throwtj("executing tjInitDecompress()");
127
DRCb8b359a2011-05-25 03:54:56 +0000128 if(dstbuf==NULL)
129 {
DRC95e4cb22014-03-12 06:51:39 +0000130 if((dstbuf=(unsigned char *)malloc(pitch*scaledh))==NULL)
131 _throwunix("allocating destination buffer");
DRCb8b359a2011-05-25 03:54:56 +0000132 dstbufalloc=1;
133 }
134 /* Set the destination buffer to gray so we know whether the decompressor
135 attempted to write to it */
DRC95e4cb22014-03-12 06:51:39 +0000136 memset(dstbuf, 127, pitch*scaledh);
DRCb8b359a2011-05-25 03:54:56 +0000137
DRC95e4cb22014-03-12 06:51:39 +0000138 if(doyuv)
DRCb8b359a2011-05-25 03:54:56 +0000139 {
DRC95e4cb22014-03-12 06:51:39 +0000140 int width=dotile? tilew:scaledw;
141 int height=dotile? tileh:scaledh;
142 int yuvsize=tjBufSizeYUV2(width, yuvpad, height, subsamp);
143 if((yuvbuf=(unsigned char *)malloc(yuvsize))==NULL)
144 _throwunix("allocating YUV buffer");
145 memset(yuvbuf, 127, yuvsize);
DRCb8b359a2011-05-25 03:54:56 +0000146 }
DRCb8b359a2011-05-25 03:54:56 +0000147
148 /* Benchmark */
DRC95e4cb22014-03-12 06:51:39 +0000149 iter=-warmup;
150 elapsed=elapsedDecode=0.;
151 while(1)
DRCb8b359a2011-05-25 03:54:56 +0000152 {
153 int tile=0;
DRC95e4cb22014-03-12 06:51:39 +0000154 double start=gettime();
155 for(row=0, dstptr=dstbuf; row<ntilesh; row++, dstptr+=pitch*tileh)
DRCb8b359a2011-05-25 03:54:56 +0000156 {
157 for(col=0, dstptr2=dstptr; col<ntilesw; col++, tile++, dstptr2+=ps*tilew)
158 {
159 int width=dotile? min(tilew, w-col*tilew):scaledw;
160 int height=dotile? min(tileh, h-row*tileh):scaledh;
DRC95e4cb22014-03-12 06:51:39 +0000161 if(doyuv)
162 {
163 double startDecode;
164 if(tjDecompressToYUV2(handle, jpegbuf[tile], jpegsize[tile], yuvbuf,
165 width, yuvpad, height, flags)==-1)
166 _throwtj("executing tjDecompressToYUV2()");
167 startDecode=gettime();
168 if(tjDecodeYUV(handle, yuvbuf, yuvpad, subsamp, dstptr2, width,
169 pitch, height, pf, flags)==-1)
170 _throwtj("executing tjDecodeYUV()");
171 if(iter>=0) elapsedDecode+=gettime()-startDecode;
172 }
173 else
174 if(tjDecompress2(handle, jpegbuf[tile], jpegsize[tile], dstptr2,
175 width, pitch, height, pf, flags)==-1)
176 _throwtj("executing tjDecompress2()");
DRCb8b359a2011-05-25 03:54:56 +0000177 }
178 }
DRC95e4cb22014-03-12 06:51:39 +0000179 iter++;
180 if(iter>=1)
181 {
182 elapsed+=gettime()-start;
183 if(elapsed>=benchtime) break;
184 }
DRCb8b359a2011-05-25 03:54:56 +0000185 }
DRC95e4cb22014-03-12 06:51:39 +0000186 if(doyuv) elapsed-=elapsedDecode;
DRCb8b359a2011-05-25 03:54:56 +0000187
188 if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
189 handle=NULL;
190
191 if(quiet)
192 {
DRCe1efc8a2014-03-12 07:16:17 +0000193 printf("%-6s%s",
194 sigfig((double)(w*h)/1000000.*(double)iter/elapsed, 4, tempstr, 1024),
195 quiet==2? "\n":" ");
DRC95e4cb22014-03-12 06:51:39 +0000196 if(doyuv)
DRCe1efc8a2014-03-12 07:16:17 +0000197 printf("%s\n",
DRC95e4cb22014-03-12 06:51:39 +0000198 sigfig((double)(w*h)/1000000.*(double)iter/elapsedDecode, 4, tempstr,
199 1024));
DRCe1efc8a2014-03-12 07:16:17 +0000200 else if(quiet!=2) printf("\n");
DRCb8b359a2011-05-25 03:54:56 +0000201 }
202 else
203 {
DRC95e4cb22014-03-12 06:51:39 +0000204 printf("%s --> Frame rate: %f fps\n",
205 doyuv? "Decomp to YUV":"Decompress ", (double)iter/elapsed);
206 printf(" Throughput: %f Megapixels/sec\n",
207 (double)(w*h)/1000000.*(double)iter/elapsed);
208 if(doyuv)
209 {
210 printf("YUV Decode --> Frame rate:  %f fps\n",
211 (double)iter/elapsedDecode);
212 printf(" Throughput: %f Megapixels/sec\n",
213 (double)(w*h)/1000000.*(double)iter/elapsedDecode);
214 }
DRCb8b359a2011-05-25 03:54:56 +0000215 }
DRCc6501f72013-11-04 23:07:54 +0000216 if(sf.num!=1 || sf.denom!=1)
217 snprintf(sizestr, 20, "%d_%d", sf.num, sf.denom);
218 else if(tilew!=w || tileh!=h)
219 snprintf(sizestr, 20, "%dx%d", tilew, tileh);
220 else snprintf(sizestr, 20, "full");
221 if(decomponly)
222 snprintf(tempstr, 1024, "%s_%s.%s", filename, sizestr, ext);
223 else
224 snprintf(tempstr, 1024, "%s_%s%s_%s.%s", filename, subName[subsamp],
225 qualstr, sizestr, ext);
226
DRC95e4cb22014-03-12 06:51:39 +0000227 if(savebmp(tempstr, dstbuf, scaledw, scaledh, pf,
228 (flags&TJFLAG_BOTTOMUP)!=0)==-1)
229 _throwbmp("saving bitmap");
230 ptr=strrchr(tempstr, '.');
231 snprintf(ptr, 1024-(ptr-tempstr), "-err.%s", ext);
232 if(srcbuf && sf.num==1 && sf.denom==1)
DRCb8b359a2011-05-25 03:54:56 +0000233 {
DRC95e4cb22014-03-12 06:51:39 +0000234 if(!quiet) printf("Compression error written to %s.\n", tempstr);
235 if(subsamp==TJ_GRAYSCALE)
236 {
237 int index, index2;
238 for(row=0, index=0; row<h; row++, index+=pitch)
239 {
240 for(col=0, index2=index; col<w; col++, index2+=ps)
241 {
242 int rindex=index2+tjRedOffset[pf];
243 int gindex=index2+tjGreenOffset[pf];
244 int bindex=index2+tjBlueOffset[pf];
245 int y=(int)((double)srcbuf[rindex]*0.299
246 + (double)srcbuf[gindex]*0.587
247 + (double)srcbuf[bindex]*0.114 + 0.5);
248 if(y>255) y=255; if(y<0) y=0;
249 dstbuf[rindex]=abs(dstbuf[rindex]-y);
250 dstbuf[gindex]=abs(dstbuf[gindex]-y);
251 dstbuf[bindex]=abs(dstbuf[bindex]-y);
252 }
253 }
254 }
255 else
256 {
257 for(row=0; row<h; row++)
258 for(col=0; col<w*ps; col++)
259 dstbuf[pitch*row+col]
260 =abs(dstbuf[pitch*row+col]-srcbuf[pitch*row+col]);
261 }
262 if(savebmp(tempstr, dstbuf, w, h, pf,
DRCb8b359a2011-05-25 03:54:56 +0000263 (flags&TJFLAG_BOTTOMUP)!=0)==-1)
264 _throwbmp("saving bitmap");
DRCb8b359a2011-05-25 03:54:56 +0000265 }
266
267 bailout:
DRC95e4cb22014-03-12 06:51:39 +0000268 if(file) fclose(file);
269 if(handle) tjDestroy(handle);
270 if(dstbuf && dstbufalloc) free(dstbuf);
271 if(yuvbuf) free(yuvbuf);
DRCb8b359a2011-05-25 03:54:56 +0000272 return retval;
273}
274
275
DRC95e4cb22014-03-12 06:51:39 +0000276void fullTest(unsigned char *srcbuf, int w, int h, int subsamp, int jpegqual,
DRCb8b359a2011-05-25 03:54:56 +0000277 char *filename)
278{
279 char tempstr[1024], tempstr2[80];
280 FILE *file=NULL; tjhandle handle=NULL;
DRC95e4cb22014-03-12 06:51:39 +0000281 unsigned char **jpegbuf=NULL, *yuvbuf=NULL, *tmpbuf=NULL, *srcptr, *srcptr2;
282 double start, elapsed, elapsedEncode;
DRCb8b359a2011-05-25 03:54:56 +0000283 int totaljpegsize=0, row, col, i, tilew=w, tileh=h, retval=0;
DRC95e4cb22014-03-12 06:51:39 +0000284 int iter, yuvsize=0;
DRCb8b359a2011-05-25 03:54:56 +0000285 unsigned long *jpegsize=NULL;
DRC95e4cb22014-03-12 06:51:39 +0000286 int ps=tjPixelSize[pf];
DRCc6501f72013-11-04 23:07:54 +0000287 int ntilesw=1, ntilesh=1, pitch=w*ps;
DRC95e4cb22014-03-12 06:51:39 +0000288 const char *pfStr=pixFormatStr[pf];
DRCb8b359a2011-05-25 03:54:56 +0000289
290 if((tmpbuf=(unsigned char *)malloc(pitch*h)) == NULL)
291 _throwunix("allocating temporary image buffer");
292
293 if(!quiet)
DRCc6501f72013-11-04 23:07:54 +0000294 printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pfStr,
DRCb8b359a2011-05-25 03:54:56 +0000295 (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp],
296 jpegqual);
297
298 for(tilew=dotile? 8:w, tileh=dotile? 8:h; ; tilew*=2, tileh*=2)
299 {
300 if(tilew>w) tilew=w; if(tileh>h) tileh=h;
301 ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
302
303 if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
304 *ntilesw*ntilesh))==NULL)
305 _throwunix("allocating JPEG tile array");
306 memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
307 if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
308 *ntilesw*ntilesh))==NULL)
309 _throwunix("allocating JPEG size array");
310 memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
311
312 if((flags&TJFLAG_NOREALLOC)!=0)
313 for(i=0; i<ntilesw*ntilesh; i++)
314 {
DRC9b49f0e2011-07-12 03:17:23 +0000315 if((jpegbuf[i]=(unsigned char *)malloc(tjBufSize(tilew, tileh,
316 subsamp)))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000317 _throwunix("allocating JPEG tiles");
318 }
319
320 /* Compression test */
321 if(quiet==1)
DRC95e4cb22014-03-12 06:51:39 +0000322 printf("%-4s (%s) %-5s %-3d ", pfStr,
323 (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp], jpegqual);
DRC78b227e2014-03-14 04:15:43 +0000324 for(i=0; i<h; i++)
325 memcpy(&tmpbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
DRCb8b359a2011-05-25 03:54:56 +0000326 if((handle=tjInitCompress())==NULL)
327 _throwtj("executing tjInitCompress()");
328
DRC95e4cb22014-03-12 06:51:39 +0000329 if(doyuv)
DRCc6501f72013-11-04 23:07:54 +0000330 {
DRC95e4cb22014-03-12 06:51:39 +0000331 yuvsize=tjBufSizeYUV2(tilew, yuvpad, tileh, subsamp);
332 if((yuvbuf=(unsigned char *)malloc(yuvsize))==NULL)
333 _throwunix("allocating YUV buffer");
334 memset(yuvbuf, 127, yuvsize);
DRCc6501f72013-11-04 23:07:54 +0000335 }
DRCb8b359a2011-05-25 03:54:56 +0000336
337 /* Benchmark */
DRC95e4cb22014-03-12 06:51:39 +0000338 iter=-warmup;
339 elapsed=elapsedEncode=0.;
340 while(1)
DRCb8b359a2011-05-25 03:54:56 +0000341 {
342 int tile=0;
343 totaljpegsize=0;
DRC95e4cb22014-03-12 06:51:39 +0000344 start=gettime();
DRCb8b359a2011-05-25 03:54:56 +0000345 for(row=0, srcptr=srcbuf; row<ntilesh; row++, srcptr+=pitch*tileh)
346 {
347 for(col=0, srcptr2=srcptr; col<ntilesw; col++, tile++,
348 srcptr2+=ps*tilew)
349 {
350 int width=min(tilew, w-col*tilew);
351 int height=min(tileh, h-row*tileh);
DRC95e4cb22014-03-12 06:51:39 +0000352 if(doyuv)
DRCc6501f72013-11-04 23:07:54 +0000353 {
DRC95e4cb22014-03-12 06:51:39 +0000354 double startEncode=gettime();
355 if(tjEncodeYUV3(handle, srcptr2, width, pitch, height, pf, yuvbuf,
356 yuvpad, subsamp, flags)==-1)
357 _throwtj("executing tjEncodeYUV3()");
358 if(iter>=0) elapsedEncode+=gettime()-startEncode;
359 if(tjCompressFromYUV(handle, yuvbuf, width, yuvpad, height,
DRCc6501f72013-11-04 23:07:54 +0000360 subsamp, &jpegbuf[tile], &jpegsize[tile], jpegqual, flags)==-1)
361 _throwtj("executing tjCompressFromYUV()");
362 }
363 else
364 {
365 if(tjCompress2(handle, srcptr2, width, pitch, height, pf,
366 &jpegbuf[tile], &jpegsize[tile], subsamp, jpegqual, flags)==-1)
367 _throwtj("executing tjCompress2()");
368 }
DRCb8b359a2011-05-25 03:54:56 +0000369 totaljpegsize+=jpegsize[tile];
370 }
371 }
DRC95e4cb22014-03-12 06:51:39 +0000372 iter++;
373 if(iter>=1)
374 {
375 elapsed+=gettime()-start;
376 if(elapsed>=benchtime) break;
377 }
DRCb8b359a2011-05-25 03:54:56 +0000378 }
DRC95e4cb22014-03-12 06:51:39 +0000379 if(doyuv) elapsed-=elapsedEncode;
DRCb8b359a2011-05-25 03:54:56 +0000380
381 if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
382 handle=NULL;
383
DRC95e4cb22014-03-12 06:51:39 +0000384 if(quiet==1) printf("%-5d %-5d ", tilew, tileh);
DRCb8b359a2011-05-25 03:54:56 +0000385 if(quiet)
386 {
DRC95e4cb22014-03-12 06:51:39 +0000387 if(doyuv)
388 printf("%-6s%s",
389 sigfig((double)(w*h)/1000000.*(double)iter/elapsedEncode, 4, tempstr,
390 1024), quiet==2? "\n":" ");
391 printf("%-6s%s",
392 sigfig((double)(w*h)/1000000.*(double)iter/elapsed, 4, tempstr, 1024),
393 quiet==2? "\n":" ");
394 printf("%-6s%s",
DRCb8b359a2011-05-25 03:54:56 +0000395 sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
DRC95e4cb22014-03-12 06:51:39 +0000396 quiet==2? "\n":" ");
DRCb8b359a2011-05-25 03:54:56 +0000397 }
398 else
399 {
400 printf("\n%s size: %d x %d\n", dotile? "Tile":"Image", tilew,
401 tileh);
DRC95e4cb22014-03-12 06:51:39 +0000402 if(doyuv)
403 {
404 printf("Encode YUV --> Frame rate: %f fps\n",
405 (double)iter/elapsedEncode);
406 printf(" Output image size: %d bytes\n", yuvsize);
407 printf(" Compression ratio: %f:1\n",
408 (double)(w*h*ps)/(double)yuvsize);
409 printf(" Throughput: %f Megapixels/sec\n",
410 (double)(w*h)/1000000.*(double)iter/elapsedEncode);
411 printf(" Output bit stream: %f Megabits/sec\n",
412 (double)yuvsize*8./1000000.*(double)iter/elapsedEncode);
413 }
414 printf("%s --> Frame rate: %f fps\n",
415 doyuv? "Comp from YUV":"Compress ", (double)iter/elapsed);
416 printf(" Output image size: %d bytes\n",
417 totaljpegsize);
418 printf(" Compression ratio: %f:1\n",
DRCb8b359a2011-05-25 03:54:56 +0000419 (double)(w*h*ps)/(double)totaljpegsize);
DRC95e4cb22014-03-12 06:51:39 +0000420 printf(" Throughput: %f Megapixels/sec\n",
421 (double)(w*h)/1000000.*(double)iter/elapsed);
422 printf(" Output bit stream: %f Megabits/sec\n",
423 (double)totaljpegsize*8./1000000.*(double)iter/elapsed);
DRCb8b359a2011-05-25 03:54:56 +0000424 }
425 if(tilew==w && tileh==h)
426 {
427 snprintf(tempstr, 1024, "%s_%s_Q%d.jpg", filename, subName[subsamp],
428 jpegqual);
429 if((file=fopen(tempstr, "wb"))==NULL)
430 _throwunix("opening reference image");
431 if(fwrite(jpegbuf[0], jpegsize[0], 1, file)!=1)
432 _throwunix("writing reference image");
433 fclose(file); file=NULL;
434 if(!quiet) printf("Reference image written to %s\n", tempstr);
435 }
436
437 /* Decompression test */
DRCc6501f72013-11-04 23:07:54 +0000438 if(!componly)
439 {
DRC95e4cb22014-03-12 06:51:39 +0000440 if(decomp(srcbuf, jpegbuf, jpegsize, tmpbuf, w, h, subsamp, jpegqual,
DRCc6501f72013-11-04 23:07:54 +0000441 filename, tilew, tileh)==-1)
442 goto bailout;
443 }
DRCb8b359a2011-05-25 03:54:56 +0000444
445 for(i=0; i<ntilesw*ntilesh; i++)
446 {
447 if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;
448 }
449 free(jpegbuf); jpegbuf=NULL;
450 free(jpegsize); jpegsize=NULL;
DRC95e4cb22014-03-12 06:51:39 +0000451 if(doyuv)
452 {
453 free(yuvbuf); yuvbuf=NULL;
454 }
DRCb8b359a2011-05-25 03:54:56 +0000455
456 if(tilew==w && tileh==h) break;
457 }
458
459 bailout:
460 if(file) {fclose(file); file=NULL;}
461 if(jpegbuf)
462 {
463 for(i=0; i<ntilesw*ntilesh; i++)
464 {
465 if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;
466 }
467 free(jpegbuf); jpegbuf=NULL;
468 }
DRC95e4cb22014-03-12 06:51:39 +0000469 if(yuvbuf) {free(yuvbuf); yuvbuf=NULL;}
DRCb8b359a2011-05-25 03:54:56 +0000470 if(jpegsize) {free(jpegsize); jpegsize=NULL;}
471 if(tmpbuf) {free(tmpbuf); tmpbuf=NULL;}
472 if(handle) {tjDestroy(handle); handle=NULL;}
473 return;
474}
475
476
DRC95e4cb22014-03-12 06:51:39 +0000477void decompTest(char *filename)
DRCb8b359a2011-05-25 03:54:56 +0000478{
479 FILE *file=NULL; tjhandle handle=NULL;
480 unsigned char **jpegbuf=NULL, *srcbuf=NULL;
481 unsigned long *jpegsize=NULL, srcsize, totaljpegsize;
482 tjtransform *t=NULL;
DRCcd7c3e62013-08-23 02:49:25 +0000483 int w=0, h=0, subsamp=-1, cs=-1, _w, _h, _tilew, _tileh,
DRCb8b359a2011-05-25 03:54:56 +0000484 _ntilesw, _ntilesh, _subsamp;
485 char *temp=NULL, tempstr[80], tempstr2[80];
DRCadf55512014-03-12 07:17:23 +0000486 int row, col, i, iter, tilew, tileh, ntilesw=1, ntilesh=1, retval=0;
DRCb8b359a2011-05-25 03:54:56 +0000487 double start, elapsed;
488 int ps=tjPixelSize[pf], tile;
489
490 if((file=fopen(filename, "rb"))==NULL)
491 _throwunix("opening file");
DRCc45653e2013-10-12 21:52:48 +0000492 if(fseek(file, 0, SEEK_END)<0 || (srcsize=ftell(file))==(unsigned long)-1)
DRCb8b359a2011-05-25 03:54:56 +0000493 _throwunix("determining file size");
494 if((srcbuf=(unsigned char *)malloc(srcsize))==NULL)
495 _throwunix("allocating memory");
496 if(fseek(file, 0, SEEK_SET)<0)
497 _throwunix("setting file position");
498 if(fread(srcbuf, srcsize, 1, file)<1)
499 _throwunix("reading JPEG data");
500 fclose(file); file=NULL;
501
502 temp=strrchr(filename, '.');
503 if(temp!=NULL) *temp='\0';
504
505 if((handle=tjInitTransform())==NULL)
506 _throwtj("executing tjInitTransform()");
DRCcd7c3e62013-08-23 02:49:25 +0000507 if(tjDecompressHeader3(handle, srcbuf, srcsize, &w, &h, &subsamp, &cs)==-1)
508 _throwtj("executing tjDecompressHeader3()");
DRCb8b359a2011-05-25 03:54:56 +0000509
510 if(quiet==1)
511 {
512 printf("All performance values in Mpixels/sec\n\n");
DRC95e4cb22014-03-12 06:51:39 +0000513 printf("Bitmap JPEG JPEG %s %s Xform Comp Decomp ",
DRCb8b359a2011-05-25 03:54:56 +0000514 dotile? "Tile ":"Image", dotile? "Tile ":"Image");
DRC95e4cb22014-03-12 06:51:39 +0000515 if(doyuv) printf("Decode");
516 printf("\n");
517 printf("Format CS Subsamp Width Height Perf Ratio Perf ");
518 if(doyuv) printf("Perf");
519 printf("\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000520 }
521 else if(!quiet)
DRC95e4cb22014-03-12 06:51:39 +0000522 printf(">>>>> JPEG %s --> %s (%s) <<<<<\n",
523 formatName(subsamp, cs, tempstr), pixFormatStr[pf],
524 (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down");
DRCb8b359a2011-05-25 03:54:56 +0000525
526 for(tilew=dotile? 16:w, tileh=dotile? 16:h; ; tilew*=2, tileh*=2)
527 {
528 if(tilew>w) tilew=w; if(tileh>h) tileh=h;
529 ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
530
531 if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
532 *ntilesw*ntilesh))==NULL)
533 _throwunix("allocating JPEG tile array");
534 memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
535 if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
536 *ntilesw*ntilesh))==NULL)
537 _throwunix("allocating JPEG size array");
538 memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
539
DRCdd592332014-03-12 06:15:51 +0000540 if((flags&TJFLAG_NOREALLOC)!=0 || !dotile)
DRCb8b359a2011-05-25 03:54:56 +0000541 for(i=0; i<ntilesw*ntilesh; i++)
542 {
DRC9b49f0e2011-07-12 03:17:23 +0000543 if((jpegbuf[i]=(unsigned char *)malloc(tjBufSize(tilew, tileh,
544 subsamp)))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000545 _throwunix("allocating JPEG tiles");
546 }
547
548 _w=w; _h=h; _tilew=tilew; _tileh=tileh;
549 if(!quiet)
550 {
551 printf("\n%s size: %d x %d", dotile? "Tile":"Image", _tilew,
552 _tileh);
553 if(sf.num!=1 || sf.denom!=1)
554 printf(" --> %d x %d", TJSCALED(_w, sf), TJSCALED(_h, sf));
555 printf("\n");
556 }
557 else if(quiet==1)
558 {
DRC95e4cb22014-03-12 06:51:39 +0000559 printf("%-4s (%s) %-5s %-5s ", pixFormatStr[pf],
DRCcd7c3e62013-08-23 02:49:25 +0000560 (flags&TJFLAG_BOTTOMUP)? "BU":"TD", csName[cs], subNameLong[subsamp]);
DRC95e4cb22014-03-12 06:51:39 +0000561 printf("%-5d %-5d ", tilew, tileh);
DRCb8b359a2011-05-25 03:54:56 +0000562 }
563
564 _subsamp=subsamp;
DRC7bf04d32011-09-17 00:18:31 +0000565 if(dotile || xformop!=TJXOP_NONE || xformopt!=0 || customFilter)
DRCb8b359a2011-05-25 03:54:56 +0000566 {
567 if((t=(tjtransform *)malloc(sizeof(tjtransform)*ntilesw*ntilesh))
568 ==NULL)
569 _throwunix("allocating image transform array");
570
571 if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE
572 || xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270)
573 {
574 _w=h; _h=w; _tilew=tileh; _tileh=tilew;
575 }
576
577 if(xformopt&TJXOPT_GRAY) _subsamp=TJ_GRAYSCALE;
578 if(xformop==TJXOP_HFLIP || xformop==TJXOP_ROT180)
579 _w=_w-(_w%tjMCUWidth[_subsamp]);
580 if(xformop==TJXOP_VFLIP || xformop==TJXOP_ROT180)
581 _h=_h-(_h%tjMCUHeight[_subsamp]);
582 if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT90)
583 _w=_w-(_w%tjMCUHeight[_subsamp]);
584 if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT270)
585 _h=_h-(_h%tjMCUWidth[_subsamp]);
586 _ntilesw=(_w+_tilew-1)/_tilew;
587 _ntilesh=(_h+_tileh-1)/_tileh;
588
DRCce6891f2014-03-13 20:33:43 +0000589 if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE
590 || xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270)
591 {
592 if(_subsamp==TJSAMP_422) _subsamp=TJSAMP_440;
593 else if(_subsamp==TJSAMP_440) _subsamp=TJSAMP_422;
594 }
595
DRCb8b359a2011-05-25 03:54:56 +0000596 for(row=0, tile=0; row<_ntilesh; row++)
597 {
598 for(col=0; col<_ntilesw; col++, tile++)
599 {
600 t[tile].r.w=min(_tilew, _w-col*_tilew);
601 t[tile].r.h=min(_tileh, _h-row*_tileh);
602 t[tile].r.x=col*_tilew;
603 t[tile].r.y=row*_tileh;
604 t[tile].op=xformop;
605 t[tile].options=xformopt|TJXOPT_TRIM;
DRC7bf04d32011-09-17 00:18:31 +0000606 t[tile].customFilter=customFilter;
607 if(t[tile].options&TJXOPT_NOOUTPUT && jpegbuf[tile])
608 {
609 free(jpegbuf[tile]); jpegbuf[tile]=NULL;
610 }
DRCb8b359a2011-05-25 03:54:56 +0000611 }
612 }
613
DRCadf55512014-03-12 07:17:23 +0000614 iter=-warmup;
615 elapsed=0.;
616 while(1)
617 {
618 start=gettime();
619 if(tjTransform(handle, srcbuf, srcsize, _ntilesw*_ntilesh, jpegbuf,
620 jpegsize, t, flags)==-1)
621 _throwtj("executing tjTransform()");
622 iter++;
623 if(iter>=1)
624 {
625 elapsed+=gettime()-start;
626 if(elapsed>=benchtime) break;
627 }
628 }
DRCb8b359a2011-05-25 03:54:56 +0000629
630 free(t); t=NULL;
631
632 for(tile=0, totaljpegsize=0; tile<_ntilesw*_ntilesh; tile++)
633 totaljpegsize+=jpegsize[tile];
634
635 if(quiet)
636 {
DRC95e4cb22014-03-12 06:51:39 +0000637 printf("%-6s%s%-6s%s",
DRCb8b359a2011-05-25 03:54:56 +0000638 sigfig((double)(w*h)/1000000./elapsed, 4, tempstr, 80),
DRC95e4cb22014-03-12 06:51:39 +0000639 quiet==2? "\n":" ",
DRCb8b359a2011-05-25 03:54:56 +0000640 sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
DRC95e4cb22014-03-12 06:51:39 +0000641 quiet==2? "\n":" ");
DRCb8b359a2011-05-25 03:54:56 +0000642 }
643 else if(!quiet)
644 {
DRC95e4cb22014-03-12 06:51:39 +0000645 printf("Transform --> Frame rate: %f fps\n", 1.0/elapsed);
646 printf(" Output image size: %lu bytes\n", totaljpegsize);
647 printf(" Compression ratio: %f:1\n",
DRCb8b359a2011-05-25 03:54:56 +0000648 (double)(w*h*ps)/(double)totaljpegsize);
DRC95e4cb22014-03-12 06:51:39 +0000649 printf(" Throughput: %f Megapixels/sec\n",
DRCb8b359a2011-05-25 03:54:56 +0000650 (double)(w*h)/1000000./elapsed);
DRC95e4cb22014-03-12 06:51:39 +0000651 printf(" Output bit stream: %f Megabits/sec\n",
DRCb8b359a2011-05-25 03:54:56 +0000652 (double)totaljpegsize*8./1000000./elapsed);
653 }
654 }
655 else
656 {
DRC95e4cb22014-03-12 06:51:39 +0000657 if(quiet==1) printf("N/A N/A ");
DRCb8b359a2011-05-25 03:54:56 +0000658 jpegsize[0]=srcsize;
659 memcpy(jpegbuf[0], srcbuf, srcsize);
660 }
661
662 if(w==tilew) _tilew=_w;
663 if(h==tileh) _tileh=_h;
DRC7bf04d32011-09-17 00:18:31 +0000664 if(!(xformopt&TJXOPT_NOOUTPUT))
665 {
DRC95e4cb22014-03-12 06:51:39 +0000666 if(decomp(NULL, jpegbuf, jpegsize, NULL, _w, _h, _subsamp, 0,
DRC7bf04d32011-09-17 00:18:31 +0000667 filename, _tilew, _tileh)==-1)
668 goto bailout;
669 }
670 else if(quiet==1) printf("N/A\n");
DRCb8b359a2011-05-25 03:54:56 +0000671
672 for(i=0; i<ntilesw*ntilesh; i++)
673 {
674 free(jpegbuf[i]); jpegbuf[i]=NULL;
675 }
676 free(jpegbuf); jpegbuf=NULL;
677 if(jpegsize) {free(jpegsize); jpegsize=NULL;}
678
679 if(tilew==w && tileh==h) break;
680 }
681
682 bailout:
683 if(file) {fclose(file); file=NULL;}
684 if(jpegbuf)
685 {
686 for(i=0; i<ntilesw*ntilesh; i++)
687 {
688 if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;
689 }
690 free(jpegbuf); jpegbuf=NULL;
691 }
692 if(jpegsize) {free(jpegsize); jpegsize=NULL;}
693 if(srcbuf) {free(srcbuf); srcbuf=NULL;}
694 if(t) {free(t); t=NULL;}
695 if(handle) {tjDestroy(handle); handle=NULL;}
696 return;
697}
698
699
700void usage(char *progname)
701{
702 int i;
703 printf("USAGE: %s\n", progname);
DRC95e4cb22014-03-12 06:51:39 +0000704 printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000705 printf(" %s\n", progname);
706 printf(" <Inputfile (JPG)> [options]\n\n");
707 printf("Options:\n\n");
708 printf("-alloc = Dynamically allocate JPEG image buffers\n");
DRCdd5fcdd2014-03-17 10:40:10 +0000709 printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
DRCb8b359a2011-05-25 03:54:56 +0000710 printf("-bottomup = Test bottom-up compression/decompression\n");
711 printf("-tile = Test performance of the codec when the image is encoded as separate\n");
712 printf(" tiles of varying sizes.\n");
DRCb8b359a2011-05-25 03:54:56 +0000713 printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
DRCdd5fcdd2014-03-17 10:40:10 +0000714 printf(" Test the specified color conversion path in the codec (default = BGR)\n");
DRC73d74c12012-06-29 23:46:38 +0000715 printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
716 printf(" the underlying codec\n");
717 printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
718 printf(" codec\n");
719 printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
720 printf(" underlying codec\n");
DRC95e4cb22014-03-12 06:51:39 +0000721 printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
722 printf(" of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");
723 printf(" GRAY). The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
724 printf(" sequence.\n");
DRCb8b359a2011-05-25 03:54:56 +0000725 printf("-quiet = Output results in tabular rather than verbose format\n");
DRC95e4cb22014-03-12 06:51:39 +0000726 printf("-yuv = Test YUV encoding/decoding functions\n");
727 printf("-yuvpad <p> = If testing YUV encoding/decoding, this specifies the number of\n");
728 printf(" bytes to which each row of each plane in the intermediate YUV image is\n");
DRCdd5fcdd2014-03-17 10:40:10 +0000729 printf(" padded (default = 1)\n");
DRC95e4cb22014-03-12 06:51:39 +0000730 printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
DRCb8b359a2011-05-25 03:54:56 +0000731 printf(" factor of M/N (M/N = ");
732 for(i=0; i<nsf; i++)
733 {
734 printf("%d/%d", scalingfactors[i].num, scalingfactors[i].denom);
735 if(nsf==2 && i!=nsf-1) printf(" or ");
736 else if(nsf>2)
737 {
738 if(i!=nsf-1) printf(", ");
739 if(i==nsf-2) printf("or ");
740 }
DRCab2df6e2012-01-28 06:49:56 +0000741 if(i%8==0 && i!=0) printf("\n ");
DRCb8b359a2011-05-25 03:54:56 +0000742 }
743 printf(")\n");
744 printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
745 printf(" Perform the corresponding lossless transform prior to\n");
746 printf(" decompression (these options are mutually exclusive)\n");
747 printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
748 printf(" test (can be combined with the other transforms above)\n");
DRCc6501f72013-11-04 23:07:54 +0000749 printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");
DRC95e4cb22014-03-12 06:51:39 +0000750 printf("-warmup <w> = Execute each benchmark <w> times to prime the cache before\n");
DRCdd5fcdd2014-03-17 10:40:10 +0000751 printf(" taking performance measurements (default = 1)\n");
DRCc6501f72013-11-04 23:07:54 +0000752 printf("-componly = Stop after running compression tests. Do not test decompression.\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000753 printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
754 printf("test will be performed for all quality values in the range.\n\n");
755 exit(1);
756}
757
758
759int main(int argc, char *argv[])
760{
DRCc6501f72013-11-04 23:07:54 +0000761 unsigned char *srcbuf=NULL; int w=0, h=0, i, j;
DRCb8b359a2011-05-25 03:54:56 +0000762 int minqual=-1, maxqual=-1; char *temp;
DRCc6501f72013-11-04 23:07:54 +0000763 int minarg=2, retval=0, subsamp=-1;
DRCb8b359a2011-05-25 03:54:56 +0000764
765 if((scalingfactors=tjGetScalingFactors(&nsf))==NULL || nsf==0)
766 _throwtj("executing tjGetScalingFactors()");
767
768 if(argc<minarg) usage(argv[0]);
769
770 temp=strrchr(argv[1], '.');
771 if(temp!=NULL)
772 {
773 if(!strcasecmp(temp, ".bmp")) ext="bmp";
774 if(!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg")) decomponly=1;
775 }
776
777 printf("\n");
778
DRC95e4cb22014-03-12 06:51:39 +0000779 if(!decomponly)
DRCb8b359a2011-05-25 03:54:56 +0000780 {
781 minarg=3;
782 if(argc<minarg) usage(argv[0]);
783 if((minqual=atoi(argv[2]))<1 || minqual>100)
784 {
785 puts("ERROR: Quality must be between 1 and 100.");
786 exit(1);
787 }
788 if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
789 && sscanf(&temp[1], "%d", &maxqual)==1 && maxqual>minqual && maxqual>=1
790 && maxqual<=100) {}
791 else maxqual=minqual;
792 }
793
794 if(argc>minarg)
795 {
796 for(i=minarg; i<argc; i++)
797 {
798 if(!strcasecmp(argv[i], "-tile"))
799 {
800 dotile=1; xformopt|=TJXOPT_CROP;
801 }
DRCb8b359a2011-05-25 03:54:56 +0000802 if(!strcasecmp(argv[i], "-fastupsample"))
803 {
804 printf("Using fast upsampling code\n\n");
805 flags|=TJFLAG_FASTUPSAMPLE;
806 }
DRC73d74c12012-06-29 23:46:38 +0000807 if(!strcasecmp(argv[i], "-fastdct"))
808 {
809 printf("Using fastest DCT/IDCT algorithm\n\n");
810 flags|=TJFLAG_FASTDCT;
811 }
812 if(!strcasecmp(argv[i], "-accuratedct"))
813 {
814 printf("Using most accurate DCT/IDCT algorithm\n\n");
815 flags|=TJFLAG_ACCURATEDCT;
816 }
DRCb8b359a2011-05-25 03:54:56 +0000817 if(!strcasecmp(argv[i], "-rgb")) pf=TJPF_RGB;
818 if(!strcasecmp(argv[i], "-rgbx")) pf=TJPF_RGBX;
819 if(!strcasecmp(argv[i], "-bgr")) pf=TJPF_BGR;
820 if(!strcasecmp(argv[i], "-bgrx")) pf=TJPF_BGRX;
821 if(!strcasecmp(argv[i], "-xbgr")) pf=TJPF_XBGR;
822 if(!strcasecmp(argv[i], "-xrgb")) pf=TJPF_XRGB;
823 if(!strcasecmp(argv[i], "-bottomup")) flags|=TJFLAG_BOTTOMUP;
824 if(!strcasecmp(argv[i], "-quiet")) quiet=1;
825 if(!strcasecmp(argv[i], "-qq")) quiet=2;
826 if(!strcasecmp(argv[i], "-scale") && i<argc-1)
827 {
828 int temp1=0, temp2=0, match=0;
829 if(sscanf(argv[++i], "%d/%d", &temp1, &temp2)==2)
830 {
831 for(j=0; j<nsf; j++)
832 {
DRCab2df6e2012-01-28 06:49:56 +0000833 if((double)temp1/(double)temp2
834 == (double)scalingfactors[j].num/(double)scalingfactors[j].denom)
DRCb8b359a2011-05-25 03:54:56 +0000835 {
836 sf=scalingfactors[j];
837 match=1; break;
838 }
839 }
840 if(!match) usage(argv[0]);
841 }
842 else usage(argv[0]);
843 }
844 if(!strcasecmp(argv[i], "-hflip")) xformop=TJXOP_HFLIP;
845 if(!strcasecmp(argv[i], "-vflip")) xformop=TJXOP_VFLIP;
846 if(!strcasecmp(argv[i], "-transpose")) xformop=TJXOP_TRANSPOSE;
847 if(!strcasecmp(argv[i], "-transverse")) xformop=TJXOP_TRANSVERSE;
848 if(!strcasecmp(argv[i], "-rot90")) xformop=TJXOP_ROT90;
849 if(!strcasecmp(argv[i], "-rot180")) xformop=TJXOP_ROT180;
850 if(!strcasecmp(argv[i], "-rot270")) xformop=TJXOP_ROT270;
851 if(!strcasecmp(argv[i], "-grayscale")) xformopt|=TJXOPT_GRAY;
DRC7bf04d32011-09-17 00:18:31 +0000852 if(!strcasecmp(argv[i], "-custom")) customFilter=dummyDCTFilter;
853 if(!strcasecmp(argv[i], "-nooutput")) xformopt|=TJXOPT_NOOUTPUT;
DRCb8b359a2011-05-25 03:54:56 +0000854 if(!strcasecmp(argv[i], "-benchtime") && i<argc-1)
855 {
856 double temp=atof(argv[++i]);
857 if(temp>0.0) benchtime=temp;
858 else usage(argv[0]);
859 }
DRC95e4cb22014-03-12 06:51:39 +0000860 if(!strcasecmp(argv[i], "-warmup") && i<argc-1)
861 {
862 int temp=atoi(argv[++i]);
863 if(temp>=0)
864 {
865 warmup=temp;
866 printf("Warmup runs = %d\n\n", warmup);
867 }
868 else usage(argv[0]);
869 }
DRCb8b359a2011-05-25 03:54:56 +0000870 if(!strcmp(argv[i], "-?")) usage(argv[0]);
871 if(!strcasecmp(argv[i], "-alloc")) flags&=(~TJFLAG_NOREALLOC);
DRC94f0e032011-05-25 04:35:53 +0000872 if(!strcasecmp(argv[i], "-bmp")) ext="bmp";
DRC95e4cb22014-03-12 06:51:39 +0000873 if(!strcasecmp(argv[i], "-yuv"))
DRCc6501f72013-11-04 23:07:54 +0000874 {
DRC95e4cb22014-03-12 06:51:39 +0000875 printf("Testing YUV planar encoding/decoding\n\n");
876 doyuv=1;
DRCc6501f72013-11-04 23:07:54 +0000877 }
878 if(!strcasecmp(argv[i], "-yuvpad") && i<argc-1)
879 {
880 int temp=atoi(argv[++i]);
881 if(temp>=1) yuvpad=temp;
882 }
883 if(!strcasecmp(argv[i], "-subsamp") && i<argc-1)
884 {
885 i++;
886 if(toupper(argv[i][0])=='G') subsamp=TJSAMP_GRAY;
887 else
888 {
889 int temp=atoi(argv[i]);
890 switch(temp)
891 {
892 case 444: subsamp=TJSAMP_444; break;
893 case 422: subsamp=TJSAMP_422; break;
894 case 440: subsamp=TJSAMP_440; break;
895 case 420: subsamp=TJSAMP_420; break;
896 case 411: subsamp=TJSAMP_411; break;
897 }
898 }
899 }
900 if(!strcasecmp(argv[i], "-componly")) componly=1;
DRCb8b359a2011-05-25 03:54:56 +0000901 }
902 }
903
904 if((sf.num!=1 || sf.denom!=1) && dotile)
905 {
906 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
907 printf("work when scaled decompression is enabled.\n");
908 dotile=0;
909 }
910
DRCb8b359a2011-05-25 03:54:56 +0000911 if(!decomponly)
912 {
DRC95e4cb22014-03-12 06:51:39 +0000913 if(loadbmp(argv[1], &srcbuf, &w, &h, pf, (flags&TJFLAG_BOTTOMUP)!=0)==-1)
914 _throwbmp("loading bitmap");
DRCb8b359a2011-05-25 03:54:56 +0000915 temp=strrchr(argv[1], '.');
916 if(temp!=NULL) *temp='\0';
917 }
918
919 if(quiet==1 && !decomponly)
920 {
921 printf("All performance values in Mpixels/sec\n\n");
DRC95e4cb22014-03-12 06:51:39 +0000922 printf("Bitmap JPEG JPEG %s %s ",
DRCb8b359a2011-05-25 03:54:56 +0000923 dotile? "Tile ":"Image", dotile? "Tile ":"Image");
DRC95e4cb22014-03-12 06:51:39 +0000924 if(doyuv) printf("Encode ");
925 printf("Comp Comp Decomp ");
926 if(doyuv) printf("Decode");
927 printf("\n");
928 printf("Format Subsamp Qual Width Height ");
929 if(doyuv) printf("Perf ");
930 printf("Perf Ratio Perf ");
931 if(doyuv) printf("Perf");
932 printf("\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000933 }
934
935 if(decomponly)
936 {
DRC95e4cb22014-03-12 06:51:39 +0000937 decompTest(argv[1]);
DRCb8b359a2011-05-25 03:54:56 +0000938 printf("\n");
939 goto bailout;
940 }
DRC95e4cb22014-03-12 06:51:39 +0000941 if(subsamp>=0 && subsamp<TJ_NUMSAMP)
DRCc6501f72013-11-04 23:07:54 +0000942 {
943 for(i=maxqual; i>=minqual; i--)
DRC95e4cb22014-03-12 06:51:39 +0000944 fullTest(srcbuf, w, h, subsamp, i, argv[1]);
DRCc6501f72013-11-04 23:07:54 +0000945 printf("\n");
946 }
947 else
948 {
949 for(i=maxqual; i>=minqual; i--)
DRC95e4cb22014-03-12 06:51:39 +0000950 fullTest(srcbuf, w, h, TJSAMP_GRAY, i, argv[1]);
DRCc6501f72013-11-04 23:07:54 +0000951 printf("\n");
952 for(i=maxqual; i>=minqual; i--)
DRC95e4cb22014-03-12 06:51:39 +0000953 fullTest(srcbuf, w, h, TJSAMP_420, i, argv[1]);
DRCc6501f72013-11-04 23:07:54 +0000954 printf("\n");
955 for(i=maxqual; i>=minqual; i--)
DRC95e4cb22014-03-12 06:51:39 +0000956 fullTest(srcbuf, w, h, TJSAMP_422, i, argv[1]);
DRCc6501f72013-11-04 23:07:54 +0000957 printf("\n");
958 for(i=maxqual; i>=minqual; i--)
DRC95e4cb22014-03-12 06:51:39 +0000959 fullTest(srcbuf, w, h, TJSAMP_444, i, argv[1]);
DRCc6501f72013-11-04 23:07:54 +0000960 printf("\n");
961 }
DRCb8b359a2011-05-25 03:54:56 +0000962
963 bailout:
964 if(srcbuf) free(srcbuf);
965 return retval;
966}