• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C)2009-2019 D. R. Commander.  All Rights Reserved.
3  *
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>
32 #include <ctype.h>
33 #include <math.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <cdjpeg.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 }
45 #define THROW_UNIX(m)  THROW(m, strerror(errno))
46 
47 char tjErrorStr[JMSG_LENGTH_MAX] = "\0", tjErrorMsg[JMSG_LENGTH_MAX] = "\0";
48 int tjErrorLine = -1, tjErrorCode = -1;
49 
50 #define THROW_TJG(m) { \
51   printf("ERROR in line %d while %s:\n%s\n", __LINE__, m, \
52          tjGetErrorStr2(NULL)); \
53   retval = -1;  goto bailout; \
54 }
55 
56 #define THROW_TJ(m) { \
57   int _tjErrorCode = tjGetErrorCode(handle); \
58   char *_tjErrorStr = tjGetErrorStr2(handle); \
59   \
60   if (!(flags & TJFLAG_STOPONWARNING) && _tjErrorCode == TJERR_WARNING) { \
61     if (strncmp(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX) || \
62         strncmp(tjErrorMsg, m, JMSG_LENGTH_MAX) || \
63         tjErrorCode != _tjErrorCode || tjErrorLine != __LINE__) { \
64       strncpy(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX - 1); \
65       strncpy(tjErrorMsg, m, JMSG_LENGTH_MAX - 1); \
66       tjErrorCode = _tjErrorCode; \
67       tjErrorLine = __LINE__; \
68       printf("WARNING in line %d while %s:\n%s\n", __LINE__, m, _tjErrorStr); \
69     } \
70   } else { \
71     printf("%s in line %d while %s:\n%s\n", \
72            _tjErrorCode == TJERR_WARNING ? "WARNING" : "ERROR", __LINE__, m, \
73            _tjErrorStr); \
74     retval = -1;  goto bailout; \
75   } \
76 }
77 
78 int flags = TJFLAG_NOREALLOC, compOnly = 0, decompOnly = 0, doYUV = 0,
79   quiet = 0, doTile = 0, pf = TJPF_BGR, yuvPad = 1, doWrite = 1;
80 char *ext = "ppm";
81 const char *pixFormatStr[TJ_NUMPF] = {
82   "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
83 };
84 const char *subNameLong[TJ_NUMSAMP] = {
85   "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
86 };
87 const char *csName[TJ_NUMCS] = {
88   "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
89 };
90 const char *subName[TJ_NUMSAMP] = {
91   "444", "422", "420", "GRAY", "440", "411"
92 };
93 tjscalingfactor *scalingFactors = NULL, sf = { 1, 1 };
94 int nsf = 0, xformOp = TJXOP_NONE, xformOpt = 0;
95 int (*customFilter) (short *, tjregion, tjregion, int, int, tjtransform *);
96 double benchTime = 5.0, warmup = 1.0;
97 
98 
formatName(int subsamp,int cs,char * buf)99 char *formatName(int subsamp, int cs, char *buf)
100 {
101   if (cs == TJCS_YCbCr)
102     return (char *)subNameLong[subsamp];
103   else if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
104     snprintf(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
105     return buf;
106   } else
107     return (char *)csName[cs];
108 }
109 
110 
sigfig(double val,int figs,char * buf,int len)111 char *sigfig(double val, int figs, char *buf, int len)
112 {
113   char format[80];
114   int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));
115 
116   if (digitsAfterDecimal < 1)
117     snprintf(format, 80, "%%.0f");
118   else
119     snprintf(format, 80, "%%.%df", digitsAfterDecimal);
120   snprintf(buf, len, format, val);
121   return buf;
122 }
123 
124 
125 /* Custom DCT filter which produces a negative of the image */
dummyDCTFilter(short * coeffs,tjregion arrayRegion,tjregion planeRegion,int componentIndex,int transformIndex,tjtransform * transform)126 int dummyDCTFilter(short *coeffs, tjregion arrayRegion, tjregion planeRegion,
127                    int componentIndex, int transformIndex,
128                    tjtransform *transform)
129 {
130   int i;
131 
132   for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
133     coeffs[i] = -coeffs[i];
134   return 0;
135 }
136 
137 
138 /* Decompression test */
decomp(unsigned char * srcBuf,unsigned char ** jpegBuf,unsigned long * jpegSize,unsigned char * dstBuf,int w,int h,int subsamp,int jpegQual,char * fileName,int tilew,int tileh)139 int decomp(unsigned char *srcBuf, unsigned char **jpegBuf,
140            unsigned long *jpegSize, unsigned char *dstBuf, int w, int h,
141            int subsamp, int jpegQual, char *fileName, int tilew, int tileh)
142 {
143   char tempStr[1024], sizeStr[20] = "\0", qualStr[13] = "\0", *ptr;
144   FILE *file = NULL;
145   tjhandle handle = NULL;
146   int row, col, iter = 0, dstBufAlloc = 0, retval = 0;
147   double elapsed, elapsedDecode;
148   int ps = tjPixelSize[pf];
149   int scaledw = TJSCALED(w, sf);
150   int scaledh = TJSCALED(h, sf);
151   int pitch = scaledw * ps;
152   int ntilesw = (w + tilew - 1) / tilew, ntilesh = (h + tileh - 1) / tileh;
153   unsigned char *dstPtr, *dstPtr2, *yuvBuf = NULL;
154 
155   if (jpegQual > 0) {
156     snprintf(qualStr, 13, "_Q%d", jpegQual);
157     qualStr[12] = 0;
158   }
159 
160   if ((handle = tjInitDecompress()) == NULL)
161     THROW_TJ("executing tjInitDecompress()");
162 
163   if (dstBuf == NULL) {
164     if ((unsigned long long)pitch * (unsigned long long)scaledh >
165         (unsigned long long)((size_t)-1))
166       THROW("allocating destination buffer", "Image is too large");
167     if ((dstBuf = (unsigned char *)malloc((size_t)pitch * scaledh)) == NULL)
168       THROW_UNIX("allocating destination buffer");
169     dstBufAlloc = 1;
170   }
171   /* Set the destination buffer to gray so we know whether the decompressor
172      attempted to write to it */
173   memset(dstBuf, 127, pitch * scaledh);
174 
175   if (doYUV) {
176     int width = doTile ? tilew : scaledw;
177     int height = doTile ? tileh : scaledh;
178     unsigned long yuvSize = tjBufSizeYUV2(width, yuvPad, height, subsamp);
179 
180     if (yuvSize == (unsigned long)-1)
181       THROW_TJ("allocating YUV buffer");
182     if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
183       THROW_UNIX("allocating YUV buffer");
184     memset(yuvBuf, 127, yuvSize);
185   }
186 
187   /* Benchmark */
188   iter = -1;
189   elapsed = elapsedDecode = 0.;
190   while (1) {
191     int tile = 0;
192     double start = getTime();
193 
194     for (row = 0, dstPtr = dstBuf; row < ntilesh;
195          row++, dstPtr += pitch * tileh) {
196       for (col = 0, dstPtr2 = dstPtr; col < ntilesw;
197            col++, tile++, dstPtr2 += ps * tilew) {
198         int width = doTile ? min(tilew, w - col * tilew) : scaledw;
199         int height = doTile ? min(tileh, h - row * tileh) : scaledh;
200 
201         if (doYUV) {
202           double startDecode;
203 
204           if (tjDecompressToYUV2(handle, jpegBuf[tile], jpegSize[tile], yuvBuf,
205                                  width, yuvPad, height, flags) == -1)
206             THROW_TJ("executing tjDecompressToYUV2()");
207           startDecode = getTime();
208           if (tjDecodeYUV(handle, yuvBuf, yuvPad, subsamp, dstPtr2, width,
209                           pitch, height, pf, flags) == -1)
210             THROW_TJ("executing tjDecodeYUV()");
211           if (iter >= 0) elapsedDecode += getTime() - startDecode;
212         } else if (tjDecompress2(handle, jpegBuf[tile], jpegSize[tile],
213                                  dstPtr2, width, pitch, height, pf,
214                                  flags) == -1)
215           THROW_TJ("executing tjDecompress2()");
216       }
217     }
218     elapsed += getTime() - start;
219     if (iter >= 0) {
220       iter++;
221       if (elapsed >= benchTime) break;
222     } else if (elapsed >= warmup) {
223       iter = 0;
224       elapsed = elapsedDecode = 0.;
225     }
226   }
227   if (doYUV) elapsed -= elapsedDecode;
228 
229   if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
230   handle = NULL;
231 
232   if (quiet) {
233     printf("%-6s%s",
234            sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
235                   tempStr, 1024),
236            quiet == 2 ? "\n" : "  ");
237     if (doYUV)
238       printf("%s\n",
239              sigfig((double)(w * h) / 1000000. * (double)iter / elapsedDecode,
240                     4, tempStr, 1024));
241     else if (quiet != 2) printf("\n");
242   } else {
243     printf("%s --> Frame rate:         %f fps\n",
244            doYUV ? "Decomp to YUV" : "Decompress   ", (double)iter / elapsed);
245     printf("                  Throughput:         %f Megapixels/sec\n",
246            (double)(w * h) / 1000000. * (double)iter / elapsed);
247     if (doYUV) {
248       printf("YUV Decode    --> Frame rate:         %f fps\n",
249              (double)iter / elapsedDecode);
250       printf("                  Throughput:         %f Megapixels/sec\n",
251              (double)(w * h) / 1000000. * (double)iter / elapsedDecode);
252     }
253   }
254 
255   if (!doWrite) goto bailout;
256 
257   if (sf.num != 1 || sf.denom != 1)
258     snprintf(sizeStr, 20, "%d_%d", sf.num, sf.denom);
259   else if (tilew != w || tileh != h)
260     snprintf(sizeStr, 20, "%dx%d", tilew, tileh);
261   else snprintf(sizeStr, 20, "full");
262   if (decompOnly)
263     snprintf(tempStr, 1024, "%s_%s.%s", fileName, sizeStr, ext);
264   else
265     snprintf(tempStr, 1024, "%s_%s%s_%s.%s", fileName, subName[subsamp],
266              qualStr, sizeStr, ext);
267 
268   if (tjSaveImage(tempStr, dstBuf, scaledw, 0, scaledh, pf, flags) == -1)
269     THROW_TJG("saving bitmap");
270   ptr = strrchr(tempStr, '.');
271   snprintf(ptr, 1024 - (ptr - tempStr), "-err.%s", ext);
272   if (srcBuf && sf.num == 1 && sf.denom == 1) {
273     if (!quiet) printf("Compression error written to %s.\n", tempStr);
274     if (subsamp == TJ_GRAYSCALE) {
275       unsigned long index, index2;
276 
277       for (row = 0, index = 0; row < h; row++, index += pitch) {
278         for (col = 0, index2 = index; col < w; col++, index2 += ps) {
279           unsigned long rindex = index2 + tjRedOffset[pf];
280           unsigned long gindex = index2 + tjGreenOffset[pf];
281           unsigned long bindex = index2 + tjBlueOffset[pf];
282           int y = (int)((double)srcBuf[rindex] * 0.299 +
283                         (double)srcBuf[gindex] * 0.587 +
284                         (double)srcBuf[bindex] * 0.114 + 0.5);
285 
286           if (y > 255) y = 255;
287           if (y < 0) y = 0;
288           dstBuf[rindex] = abs(dstBuf[rindex] - y);
289           dstBuf[gindex] = abs(dstBuf[gindex] - y);
290           dstBuf[bindex] = abs(dstBuf[bindex] - y);
291         }
292       }
293     } else {
294       for (row = 0; row < h; row++)
295         for (col = 0; col < w * ps; col++)
296           dstBuf[pitch * row + col] =
297             abs(dstBuf[pitch * row + col] - srcBuf[pitch * row + col]);
298     }
299     if (tjSaveImage(tempStr, dstBuf, w, 0, h, pf, flags) == -1)
300       THROW_TJG("saving bitmap");
301   }
302 
303 bailout:
304   if (file) fclose(file);
305   if (handle) tjDestroy(handle);
306   if (dstBuf && dstBufAlloc) free(dstBuf);
307   if (yuvBuf) free(yuvBuf);
308   return retval;
309 }
310 
311 
fullTest(unsigned char * srcBuf,int w,int h,int subsamp,int jpegQual,char * fileName)312 int fullTest(unsigned char *srcBuf, int w, int h, int subsamp, int jpegQual,
313              char *fileName)
314 {
315   char tempStr[1024], tempStr2[80];
316   FILE *file = NULL;
317   tjhandle handle = NULL;
318   unsigned char **jpegBuf = NULL, *yuvBuf = NULL, *tmpBuf = NULL, *srcPtr,
319     *srcPtr2;
320   double start, elapsed, elapsedEncode;
321   int totalJpegSize = 0, row, col, i, tilew = w, tileh = h, retval = 0;
322   int iter;
323   unsigned long *jpegSize = NULL, yuvSize = 0;
324   int ps = tjPixelSize[pf];
325   int ntilesw = 1, ntilesh = 1, pitch = w * ps;
326   const char *pfStr = pixFormatStr[pf];
327 
328   if ((unsigned long long)pitch * (unsigned long long)h >
329       (unsigned long long)((size_t)-1))
330     THROW("allocating temporary image buffer", "Image is too large");
331   if ((tmpBuf = (unsigned char *)malloc((size_t)pitch * h)) == NULL)
332     THROW_UNIX("allocating temporary image buffer");
333 
334   if (!quiet)
335     printf(">>>>>  %s (%s) <--> JPEG %s Q%d  <<<<<\n", pfStr,
336            (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down",
337            subNameLong[subsamp], jpegQual);
338 
339   for (tilew = doTile ? 8 : w, tileh = doTile ? 8 : h; ;
340        tilew *= 2, tileh *= 2) {
341     if (tilew > w) tilew = w;
342     if (tileh > h) tileh = h;
343     ntilesw = (w + tilew - 1) / tilew;
344     ntilesh = (h + tileh - 1) / tileh;
345 
346     if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
347                                             ntilesw * ntilesh)) == NULL)
348       THROW_UNIX("allocating JPEG tile array");
349     memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
350     if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
351                                             ntilesw * ntilesh)) == NULL)
352       THROW_UNIX("allocating JPEG size array");
353     memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
354 
355     if ((flags & TJFLAG_NOREALLOC) != 0)
356       for (i = 0; i < ntilesw * ntilesh; i++) {
357         if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
358           THROW("getting buffer size", "Image is too large");
359         if ((jpegBuf[i] = (unsigned char *)
360                           tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
361           THROW_UNIX("allocating JPEG tiles");
362       }
363 
364     /* Compression test */
365     if (quiet == 1)
366       printf("%-4s (%s)  %-5s    %-3d   ", pfStr,
367              (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", subNameLong[subsamp],
368              jpegQual);
369     for (i = 0; i < h; i++)
370       memcpy(&tmpBuf[pitch * i], &srcBuf[w * ps * i], w * ps);
371     if ((handle = tjInitCompress()) == NULL)
372       THROW_TJ("executing tjInitCompress()");
373 
374     if (doYUV) {
375       yuvSize = tjBufSizeYUV2(tilew, yuvPad, tileh, subsamp);
376       if (yuvSize == (unsigned long)-1)
377         THROW_TJ("allocating YUV buffer");
378       if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
379         THROW_UNIX("allocating YUV buffer");
380       memset(yuvBuf, 127, yuvSize);
381     }
382 
383     /* Benchmark */
384     iter = -1;
385     elapsed = elapsedEncode = 0.;
386     while (1) {
387       int tile = 0;
388 
389       totalJpegSize = 0;
390       start = getTime();
391       for (row = 0, srcPtr = srcBuf; row < ntilesh;
392            row++, srcPtr += pitch * tileh) {
393         for (col = 0, srcPtr2 = srcPtr; col < ntilesw;
394              col++, tile++, srcPtr2 += ps * tilew) {
395           int width = min(tilew, w - col * tilew);
396           int height = min(tileh, h - row * tileh);
397 
398           if (doYUV) {
399             double startEncode = getTime();
400 
401             if (tjEncodeYUV3(handle, srcPtr2, width, pitch, height, pf, yuvBuf,
402                              yuvPad, subsamp, flags) == -1)
403               THROW_TJ("executing tjEncodeYUV3()");
404             if (iter >= 0) elapsedEncode += getTime() - startEncode;
405             if (tjCompressFromYUV(handle, yuvBuf, width, yuvPad, height,
406                                   subsamp, &jpegBuf[tile], &jpegSize[tile],
407                                   jpegQual, flags) == -1)
408               THROW_TJ("executing tjCompressFromYUV()");
409           } else {
410             if (tjCompress2(handle, srcPtr2, width, pitch, height, pf,
411                             &jpegBuf[tile], &jpegSize[tile], subsamp, jpegQual,
412                             flags) == -1)
413               THROW_TJ("executing tjCompress2()");
414           }
415           totalJpegSize += jpegSize[tile];
416         }
417       }
418       elapsed += getTime() - start;
419       if (iter >= 0) {
420         iter++;
421         if (elapsed >= benchTime) break;
422       } else if (elapsed >= warmup) {
423         iter = 0;
424         elapsed = elapsedEncode = 0.;
425       }
426     }
427     if (doYUV) elapsed -= elapsedEncode;
428 
429     if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
430     handle = NULL;
431 
432     if (quiet == 1) printf("%-5d  %-5d   ", tilew, tileh);
433     if (quiet) {
434       if (doYUV)
435         printf("%-6s%s",
436                sigfig((double)(w * h) / 1000000. *
437                       (double)iter / elapsedEncode, 4, tempStr, 1024),
438                quiet == 2 ? "\n" : "  ");
439       printf("%-6s%s",
440              sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
441                     tempStr, 1024),
442              quiet == 2 ? "\n" : "  ");
443       printf("%-6s%s",
444              sigfig((double)(w * h * ps) / (double)totalJpegSize, 4, tempStr2,
445                     80),
446              quiet == 2 ? "\n" : "  ");
447     } else {
448       printf("\n%s size: %d x %d\n", doTile ? "Tile" : "Image", tilew, tileh);
449       if (doYUV) {
450         printf("Encode YUV    --> Frame rate:         %f fps\n",
451                (double)iter / elapsedEncode);
452         printf("                  Output image size:  %lu bytes\n", yuvSize);
453         printf("                  Compression ratio:  %f:1\n",
454                (double)(w * h * ps) / (double)yuvSize);
455         printf("                  Throughput:         %f Megapixels/sec\n",
456                (double)(w * h) / 1000000. * (double)iter / elapsedEncode);
457         printf("                  Output bit stream:  %f Megabits/sec\n",
458                (double)yuvSize * 8. / 1000000. * (double)iter / elapsedEncode);
459       }
460       printf("%s --> Frame rate:         %f fps\n",
461              doYUV ? "Comp from YUV" : "Compress     ",
462              (double)iter / elapsed);
463       printf("                  Output image size:  %d bytes\n",
464              totalJpegSize);
465       printf("                  Compression ratio:  %f:1\n",
466              (double)(w * h * ps) / (double)totalJpegSize);
467       printf("                  Throughput:         %f Megapixels/sec\n",
468              (double)(w * h) / 1000000. * (double)iter / elapsed);
469       printf("                  Output bit stream:  %f Megabits/sec\n",
470              (double)totalJpegSize * 8. / 1000000. * (double)iter / elapsed);
471     }
472     if (tilew == w && tileh == h && doWrite) {
473       snprintf(tempStr, 1024, "%s_%s_Q%d.jpg", fileName, subName[subsamp],
474                jpegQual);
475       if ((file = fopen(tempStr, "wb")) == NULL)
476         THROW_UNIX("opening reference image");
477       if (fwrite(jpegBuf[0], jpegSize[0], 1, file) != 1)
478         THROW_UNIX("writing reference image");
479       fclose(file);  file = NULL;
480       if (!quiet) printf("Reference image written to %s\n", tempStr);
481     }
482 
483     /* Decompression test */
484     if (!compOnly) {
485       if (decomp(srcBuf, jpegBuf, jpegSize, tmpBuf, w, h, subsamp, jpegQual,
486                  fileName, tilew, tileh) == -1)
487         goto bailout;
488     }
489 
490     for (i = 0; i < ntilesw * ntilesh; i++) {
491       if (jpegBuf[i]) tjFree(jpegBuf[i]);
492       jpegBuf[i] = NULL;
493     }
494     free(jpegBuf);  jpegBuf = NULL;
495     free(jpegSize);  jpegSize = NULL;
496     if (doYUV) {
497       free(yuvBuf);  yuvBuf = NULL;
498     }
499 
500     if (tilew == w && tileh == h) break;
501   }
502 
503 bailout:
504   if (file) { fclose(file);  file = NULL; }
505   if (jpegBuf) {
506     for (i = 0; i < ntilesw * ntilesh; i++) {
507       if (jpegBuf[i]) tjFree(jpegBuf[i]);
508       jpegBuf[i] = NULL;
509     }
510     free(jpegBuf);  jpegBuf = NULL;
511   }
512   if (yuvBuf) { free(yuvBuf);  yuvBuf = NULL; }
513   if (jpegSize) { free(jpegSize);  jpegSize = NULL; }
514   if (tmpBuf) { free(tmpBuf);  tmpBuf = NULL; }
515   if (handle) { tjDestroy(handle);  handle = NULL; }
516   return retval;
517 }
518 
519 
decompTest(char * fileName)520 int decompTest(char *fileName)
521 {
522   FILE *file = NULL;
523   tjhandle handle = NULL;
524   unsigned char **jpegBuf = NULL, *srcBuf = NULL;
525   unsigned long *jpegSize = NULL, srcSize, totalJpegSize;
526   tjtransform *t = NULL;
527   double start, elapsed;
528   int ps = tjPixelSize[pf], tile, row, col, i, iter, retval = 0, decompsrc = 0;
529   char *temp = NULL, tempStr[80], tempStr2[80];
530   /* Original image */
531   int w = 0, h = 0, tilew, tileh, ntilesw = 1, ntilesh = 1, subsamp = -1,
532     cs = -1;
533   /* Transformed image */
534   int tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;
535 
536   if ((file = fopen(fileName, "rb")) == NULL)
537     THROW_UNIX("opening file");
538   if (fseek(file, 0, SEEK_END) < 0 ||
539       (srcSize = ftell(file)) == (unsigned long)-1)
540     THROW_UNIX("determining file size");
541   if ((srcBuf = (unsigned char *)malloc(srcSize)) == NULL)
542     THROW_UNIX("allocating memory");
543   if (fseek(file, 0, SEEK_SET) < 0)
544     THROW_UNIX("setting file position");
545   if (fread(srcBuf, srcSize, 1, file) < 1)
546     THROW_UNIX("reading JPEG data");
547   fclose(file);  file = NULL;
548 
549   temp = strrchr(fileName, '.');
550   if (temp != NULL) *temp = '\0';
551 
552   if ((handle = tjInitTransform()) == NULL)
553     THROW_TJ("executing tjInitTransform()");
554   if (tjDecompressHeader3(handle, srcBuf, srcSize, &w, &h, &subsamp,
555                           &cs) == -1)
556     THROW_TJ("executing tjDecompressHeader3()");
557   if (w < 1 || h < 1)
558     THROW("reading JPEG header", "Invalid image dimensions");
559   if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
560     pf = TJPF_CMYK;  ps = tjPixelSize[pf];
561   }
562 
563   if (quiet == 1) {
564     printf("All performance values in Mpixels/sec\n\n");
565     printf("Bitmap     JPEG   JPEG     %s  %s   Xform   Comp    Decomp  ",
566            doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
567     if (doYUV) printf("Decode");
568     printf("\n");
569     printf("Format     CS     Subsamp  Width  Height  Perf    Ratio   Perf    ");
570     if (doYUV) printf("Perf");
571     printf("\n\n");
572   } else if (!quiet)
573     printf(">>>>>  JPEG %s --> %s (%s)  <<<<<\n",
574            formatName(subsamp, cs, tempStr), pixFormatStr[pf],
575            (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down");
576 
577   for (tilew = doTile ? 16 : w, tileh = doTile ? 16 : h; ;
578        tilew *= 2, tileh *= 2) {
579     if (tilew > w) tilew = w;
580     if (tileh > h) tileh = h;
581     ntilesw = (w + tilew - 1) / tilew;
582     ntilesh = (h + tileh - 1) / tileh;
583 
584     if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
585                                             ntilesw * ntilesh)) == NULL)
586       THROW_UNIX("allocating JPEG tile array");
587     memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
588     if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
589                                             ntilesw * ntilesh)) == NULL)
590       THROW_UNIX("allocating JPEG size array");
591     memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
592 
593     if ((flags & TJFLAG_NOREALLOC) != 0 &&
594         (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter))
595       for (i = 0; i < ntilesw * ntilesh; i++) {
596         if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
597           THROW("getting buffer size", "Image is too large");
598         if ((jpegBuf[i] = (unsigned char *)
599                           tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
600           THROW_UNIX("allocating JPEG tiles");
601       }
602 
603     tw = w;  th = h;  ttilew = tilew;  ttileh = tileh;
604     if (!quiet) {
605       printf("\n%s size: %d x %d", doTile ? "Tile" : "Image", ttilew, ttileh);
606       if (sf.num != 1 || sf.denom != 1)
607         printf(" --> %d x %d", TJSCALED(tw, sf), TJSCALED(th, sf));
608       printf("\n");
609     } else if (quiet == 1) {
610       printf("%-4s (%s)  %-5s  %-5s    ", pixFormatStr[pf],
611              (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", csName[cs],
612              subNameLong[subsamp]);
613       printf("%-5d  %-5d   ", tilew, tileh);
614     }
615 
616     tsubsamp = subsamp;
617     if (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter) {
618       if ((t = (tjtransform *)malloc(sizeof(tjtransform) * ntilesw *
619                                      ntilesh)) == NULL)
620         THROW_UNIX("allocating image transform array");
621 
622       if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
623           xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
624         tw = h;  th = w;  ttilew = tileh;  ttileh = tilew;
625       }
626 
627       if (xformOpt & TJXOPT_GRAY) tsubsamp = TJ_GRAYSCALE;
628       if (xformOp == TJXOP_HFLIP || xformOp == TJXOP_ROT180)
629         tw = tw - (tw % tjMCUWidth[tsubsamp]);
630       if (xformOp == TJXOP_VFLIP || xformOp == TJXOP_ROT180)
631         th = th - (th % tjMCUHeight[tsubsamp]);
632       if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT90)
633         tw = tw - (tw % tjMCUHeight[tsubsamp]);
634       if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT270)
635         th = th - (th % tjMCUWidth[tsubsamp]);
636       tntilesw = (tw + ttilew - 1) / ttilew;
637       tntilesh = (th + ttileh - 1) / ttileh;
638 
639       if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
640           xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
641         if (tsubsamp == TJSAMP_422) tsubsamp = TJSAMP_440;
642         else if (tsubsamp == TJSAMP_440) tsubsamp = TJSAMP_422;
643       }
644 
645       for (row = 0, tile = 0; row < tntilesh; row++) {
646         for (col = 0; col < tntilesw; col++, tile++) {
647           t[tile].r.w = min(ttilew, tw - col * ttilew);
648           t[tile].r.h = min(ttileh, th - row * ttileh);
649           t[tile].r.x = col * ttilew;
650           t[tile].r.y = row * ttileh;
651           t[tile].op = xformOp;
652           t[tile].options = xformOpt | TJXOPT_TRIM;
653           t[tile].customFilter = customFilter;
654           if (t[tile].options & TJXOPT_NOOUTPUT && jpegBuf[tile]) {
655             tjFree(jpegBuf[tile]);  jpegBuf[tile] = NULL;
656           }
657         }
658       }
659 
660       iter = -1;
661       elapsed = 0.;
662       while (1) {
663         start = getTime();
664         if (tjTransform(handle, srcBuf, srcSize, tntilesw * tntilesh, jpegBuf,
665                         jpegSize, t, flags) == -1)
666           THROW_TJ("executing tjTransform()");
667         elapsed += getTime() - start;
668         if (iter >= 0) {
669           iter++;
670           if (elapsed >= benchTime) break;
671         } else if (elapsed >= warmup) {
672           iter = 0;
673           elapsed = 0.;
674         }
675       }
676 
677       free(t);  t = NULL;
678 
679       for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
680         totalJpegSize += jpegSize[tile];
681 
682       if (quiet) {
683         printf("%-6s%s%-6s%s",
684                sigfig((double)(w * h) / 1000000. / elapsed, 4, tempStr, 80),
685                quiet == 2 ? "\n" : "  ",
686                sigfig((double)(w * h * ps) / (double)totalJpegSize, 4,
687                       tempStr2, 80),
688                quiet == 2 ? "\n" : "  ");
689       } else if (!quiet) {
690         printf("Transform     --> Frame rate:         %f fps\n",
691                1.0 / elapsed);
692         printf("                  Output image size:  %lu bytes\n",
693                totalJpegSize);
694         printf("                  Compression ratio:  %f:1\n",
695                (double)(w * h * ps) / (double)totalJpegSize);
696         printf("                  Throughput:         %f Megapixels/sec\n",
697                (double)(w * h) / 1000000. / elapsed);
698         printf("                  Output bit stream:  %f Megabits/sec\n",
699                (double)totalJpegSize * 8. / 1000000. / elapsed);
700       }
701     } else {
702       if (quiet == 1) printf("N/A     N/A     ");
703       if(jpegBuf[0]) tjFree(jpegBuf[0]);
704       jpegBuf[0] = NULL;
705       decompsrc = 1;
706     }
707 
708     if (w == tilew) ttilew = tw;
709     if (h == tileh) ttileh = th;
710     if (!(xformOpt & TJXOPT_NOOUTPUT)) {
711       if (decomp(NULL, decompsrc ? &srcBuf : jpegBuf,
712                  decompsrc ? &srcSize : jpegSize, NULL, tw, th, tsubsamp, 0,
713                  fileName, ttilew, ttileh) == -1)
714         goto bailout;
715     } else if (quiet == 1) printf("N/A\n");
716 
717     for (i = 0; i < ntilesw * ntilesh; i++) {
718       if(jpegBuf[i]) tjFree(jpegBuf[i]);
719       jpegBuf[i] = NULL;
720     }
721     free(jpegBuf);  jpegBuf = NULL;
722     if (jpegSize) { free(jpegSize);  jpegSize = NULL; }
723 
724     if (tilew == w && tileh == h) break;
725   }
726 
727 bailout:
728   if (file) { fclose(file);  file = NULL; }
729   if (jpegBuf) {
730     for (i = 0; i < ntilesw * ntilesh; i++) {
731       if (jpegBuf[i]) tjFree(jpegBuf[i]);
732       jpegBuf[i] = NULL;
733     }
734     free(jpegBuf);  jpegBuf = NULL;
735   }
736   if (jpegSize) { free(jpegSize);  jpegSize = NULL; }
737   if (srcBuf) { free(srcBuf);  srcBuf = NULL; }
738   if (t) { free(t);  t = NULL; }
739   if (handle) { tjDestroy(handle);  handle = NULL; }
740   return retval;
741 }
742 
743 
usage(char * progName)744 void usage(char *progName)
745 {
746   int i;
747 
748   printf("USAGE: %s\n", progName);
749   printf("       <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
750   printf("       %s\n", progName);
751   printf("       <Inputfile (JPG)> [options]\n\n");
752   printf("Options:\n\n");
753   printf("-alloc = Dynamically allocate JPEG image buffers\n");
754   printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
755   printf("-bottomup = Test bottom-up compression/decompression\n");
756   printf("-tile = Test performance of the codec when the image is encoded as separate\n");
757   printf("     tiles of varying sizes.\n");
758   printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
759   printf("     Test the specified color conversion path in the codec (default = BGR)\n");
760   printf("-cmyk = Indirectly test YCCK JPEG compression/decompression (the source\n");
761   printf("     and destination bitmaps are still RGB.  The conversion is done\n");
762   printf("     internally prior to compression or after decompression.)\n");
763   printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
764   printf("     the underlying codec\n");
765   printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
766   printf("     codec\n");
767   printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
768   printf("     underlying codec\n");
769   printf("-progressive = Use progressive entropy coding in JPEG images generated by\n");
770   printf("     compression and transform operations.\n");
771   printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
772   printf("     of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");
773   printf("     GRAY).  The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
774   printf("     sequence.\n");
775   printf("-quiet = Output results in tabular rather than verbose format\n");
776   printf("-yuv = Test YUV encoding/decoding functions\n");
777   printf("-yuvpad <p> = If testing YUV encoding/decoding, this specifies the number of\n");
778   printf("     bytes to which each row of each plane in the intermediate YUV image is\n");
779   printf("     padded (default = 1)\n");
780   printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
781   printf("     factor of M/N (M/N = ");
782   for (i = 0; i < nsf; i++) {
783     printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
784     if (nsf == 2 && i != nsf - 1) printf(" or ");
785     else if (nsf > 2) {
786       if (i != nsf - 1) printf(", ");
787       if (i == nsf - 2) printf("or ");
788     }
789     if (i % 8 == 0 && i != 0) printf("\n     ");
790   }
791   printf(")\n");
792   printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
793   printf("     Perform the corresponding lossless transform prior to\n");
794   printf("     decompression (these options are mutually exclusive)\n");
795   printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
796   printf("     test (can be combined with the other transforms above)\n");
797   printf("-copynone = Do not copy any extra markers (including EXIF and ICC profile data)\n");
798   printf("     when transforming the image.\n");
799   printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");
800   printf("-warmup <t> = Run each benchmark for <t> seconds (default = 1.0) prior to\n");
801   printf("     starting the timer, in order to prime the caches and thus improve the\n");
802   printf("     consistency of the results.\n");
803   printf("-componly = Stop after running compression tests.  Do not test decompression.\n");
804   printf("-nowrite = Do not write reference or output images (improves consistency of\n");
805   printf("     performance measurements.)\n");
806   printf("-stoponwarning = Immediately discontinue the current\n");
807   printf("     compression/decompression/transform operation if the underlying codec\n");
808   printf("     throws a warning (non-fatal error)\n\n");
809   printf("NOTE:  If the quality is specified as a range (e.g. 90-100), a separate\n");
810   printf("test will be performed for all quality values in the range.\n\n");
811   exit(1);
812 }
813 
814 
main(int argc,char * argv[])815 int main(int argc, char *argv[])
816 {
817   unsigned char *srcBuf = NULL;
818   int w = 0, h = 0, i, j, minQual = -1, maxQual = -1;
819   char *temp;
820   int minArg = 2, retval = 0, subsamp = -1;
821 
822   if ((scalingFactors = tjGetScalingFactors(&nsf)) == NULL || nsf == 0)
823     THROW("executing tjGetScalingFactors()", tjGetErrorStr());
824 
825   if (argc < minArg) usage(argv[0]);
826 
827   temp = strrchr(argv[1], '.');
828   if (temp != NULL) {
829     if (!strcasecmp(temp, ".bmp")) ext = "bmp";
830     if (!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg"))
831       decompOnly = 1;
832   }
833 
834   printf("\n");
835 
836   if (!decompOnly) {
837     minArg = 3;
838     if (argc < minArg) usage(argv[0]);
839     if ((minQual = atoi(argv[2])) < 1 || minQual > 100) {
840       puts("ERROR: Quality must be between 1 and 100.");
841       exit(1);
842     }
843     if ((temp = strchr(argv[2], '-')) != NULL && strlen(temp) > 1 &&
844         sscanf(&temp[1], "%d", &maxQual) == 1 && maxQual > minQual &&
845         maxQual >= 1 && maxQual <= 100) {}
846     else maxQual = minQual;
847   }
848 
849   if (argc > minArg) {
850     for (i = minArg; i < argc; i++) {
851       if (!strcasecmp(argv[i], "-tile")) {
852         doTile = 1;  xformOpt |= TJXOPT_CROP;
853       } else if (!strcasecmp(argv[i], "-fastupsample")) {
854         printf("Using fast upsampling code\n\n");
855         flags |= TJFLAG_FASTUPSAMPLE;
856       } else if (!strcasecmp(argv[i], "-fastdct")) {
857         printf("Using fastest DCT/IDCT algorithm\n\n");
858         flags |= TJFLAG_FASTDCT;
859       } else if (!strcasecmp(argv[i], "-accuratedct")) {
860         printf("Using most accurate DCT/IDCT algorithm\n\n");
861         flags |= TJFLAG_ACCURATEDCT;
862       } else if (!strcasecmp(argv[i], "-progressive")) {
863         printf("Using progressive entropy coding\n\n");
864         flags |= TJFLAG_PROGRESSIVE;
865       } else if (!strcasecmp(argv[i], "-rgb"))
866         pf = TJPF_RGB;
867       else if (!strcasecmp(argv[i], "-rgbx"))
868         pf = TJPF_RGBX;
869       else if (!strcasecmp(argv[i], "-bgr"))
870         pf = TJPF_BGR;
871       else if (!strcasecmp(argv[i], "-bgrx"))
872         pf = TJPF_BGRX;
873       else if (!strcasecmp(argv[i], "-xbgr"))
874         pf = TJPF_XBGR;
875       else if (!strcasecmp(argv[i], "-xrgb"))
876         pf = TJPF_XRGB;
877       else if (!strcasecmp(argv[i], "-cmyk"))
878         pf = TJPF_CMYK;
879       else if (!strcasecmp(argv[i], "-bottomup"))
880         flags |= TJFLAG_BOTTOMUP;
881       else if (!strcasecmp(argv[i], "-quiet"))
882         quiet = 1;
883       else if (!strcasecmp(argv[i], "-qq"))
884         quiet = 2;
885       else if (!strcasecmp(argv[i], "-scale") && i < argc - 1) {
886         int temp1 = 0, temp2 = 0, match = 0;
887 
888         if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) == 2) {
889           for (j = 0; j < nsf; j++) {
890             if ((double)temp1 / (double)temp2 ==
891                 (double)scalingFactors[j].num /
892                 (double)scalingFactors[j].denom) {
893               sf = scalingFactors[j];
894               match = 1;  break;
895             }
896           }
897           if (!match) usage(argv[0]);
898         } else usage(argv[0]);
899       } else if (!strcasecmp(argv[i], "-hflip"))
900         xformOp = TJXOP_HFLIP;
901       else if (!strcasecmp(argv[i], "-vflip"))
902         xformOp = TJXOP_VFLIP;
903       else if (!strcasecmp(argv[i], "-transpose"))
904         xformOp = TJXOP_TRANSPOSE;
905       else if (!strcasecmp(argv[i], "-transverse"))
906         xformOp = TJXOP_TRANSVERSE;
907       else if (!strcasecmp(argv[i], "-rot90"))
908         xformOp = TJXOP_ROT90;
909       else if (!strcasecmp(argv[i], "-rot180"))
910         xformOp = TJXOP_ROT180;
911       else if (!strcasecmp(argv[i], "-rot270"))
912         xformOp = TJXOP_ROT270;
913       else if (!strcasecmp(argv[i], "-grayscale"))
914         xformOpt |= TJXOPT_GRAY;
915       else if (!strcasecmp(argv[i], "-custom"))
916         customFilter = dummyDCTFilter;
917       else if (!strcasecmp(argv[i], "-nooutput"))
918         xformOpt |= TJXOPT_NOOUTPUT;
919       else if (!strcasecmp(argv[i], "-copynone"))
920         xformOpt |= TJXOPT_COPYNONE;
921       else if (!strcasecmp(argv[i], "-benchtime") && i < argc - 1) {
922         double temp = atof(argv[++i]);
923 
924         if (temp > 0.0) benchTime = temp;
925         else usage(argv[0]);
926       } else if (!strcasecmp(argv[i], "-warmup") && i < argc - 1) {
927         double temp = atof(argv[++i]);
928 
929         if (temp >= 0.0) warmup = temp;
930         else usage(argv[0]);
931         printf("Warmup time = %.1f seconds\n\n", warmup);
932       } else if (!strcasecmp(argv[i], "-alloc"))
933         flags &= (~TJFLAG_NOREALLOC);
934       else if (!strcasecmp(argv[i], "-bmp"))
935         ext = "bmp";
936       else if (!strcasecmp(argv[i], "-yuv")) {
937         printf("Testing YUV planar encoding/decoding\n\n");
938         doYUV = 1;
939       } else if (!strcasecmp(argv[i], "-yuvpad") && i < argc - 1) {
940         int temp = atoi(argv[++i]);
941 
942         if (temp >= 1) yuvPad = temp;
943       } else if (!strcasecmp(argv[i], "-subsamp") && i < argc - 1) {
944         i++;
945         if (toupper(argv[i][0]) == 'G') subsamp = TJSAMP_GRAY;
946         else {
947           int temp = atoi(argv[i]);
948 
949           switch (temp) {
950           case 444:  subsamp = TJSAMP_444;  break;
951           case 422:  subsamp = TJSAMP_422;  break;
952           case 440:  subsamp = TJSAMP_440;  break;
953           case 420:  subsamp = TJSAMP_420;  break;
954           case 411:  subsamp = TJSAMP_411;  break;
955           }
956         }
957       } else if (!strcasecmp(argv[i], "-componly"))
958         compOnly = 1;
959       else if (!strcasecmp(argv[i], "-nowrite"))
960         doWrite = 0;
961       else if (!strcasecmp(argv[i], "-stoponwarning"))
962         flags |= TJFLAG_STOPONWARNING;
963       else usage(argv[0]);
964     }
965   }
966 
967   if ((sf.num != 1 || sf.denom != 1) && doTile) {
968     printf("Disabling tiled compression/decompression tests, because those tests do not\n");
969     printf("work when scaled decompression is enabled.\n");
970     doTile = 0;
971   }
972 
973   if ((flags & TJFLAG_NOREALLOC) == 0 && doTile) {
974     printf("Disabling tiled compression/decompression tests, because those tests do not\n");
975     printf("work when dynamic JPEG buffer allocation is enabled.\n\n");
976     doTile = 0;
977   }
978 
979   if (!decompOnly) {
980     if ((srcBuf = tjLoadImage(argv[1], &w, 1, &h, &pf, flags)) == NULL)
981       THROW_TJG("loading bitmap");
982     temp = strrchr(argv[1], '.');
983     if (temp != NULL) *temp = '\0';
984   }
985 
986   if (quiet == 1 && !decompOnly) {
987     printf("All performance values in Mpixels/sec\n\n");
988     printf("Bitmap     JPEG     JPEG  %s  %s   ",
989            doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
990     if (doYUV) printf("Encode  ");
991     printf("Comp    Comp    Decomp  ");
992     if (doYUV) printf("Decode");
993     printf("\n");
994     printf("Format     Subsamp  Qual  Width  Height  ");
995     if (doYUV) printf("Perf    ");
996     printf("Perf    Ratio   Perf    ");
997     if (doYUV) printf("Perf");
998     printf("\n\n");
999   }
1000 
1001   if (decompOnly) {
1002     decompTest(argv[1]);
1003     printf("\n");
1004     goto bailout;
1005   }
1006   if (subsamp >= 0 && subsamp < TJ_NUMSAMP) {
1007     for (i = maxQual; i >= minQual; i--)
1008       fullTest(srcBuf, w, h, subsamp, i, argv[1]);
1009     printf("\n");
1010   } else {
1011     if (pf != TJPF_CMYK) {
1012       for (i = maxQual; i >= minQual; i--)
1013         fullTest(srcBuf, w, h, TJSAMP_GRAY, i, argv[1]);
1014       printf("\n");
1015     }
1016     for (i = maxQual; i >= minQual; i--)
1017       fullTest(srcBuf, w, h, TJSAMP_420, i, argv[1]);
1018     printf("\n");
1019     for (i = maxQual; i >= minQual; i--)
1020       fullTest(srcBuf, w, h, TJSAMP_422, i, argv[1]);
1021     printf("\n");
1022     for (i = maxQual; i >= minQual; i--)
1023       fullTest(srcBuf, w, h, TJSAMP_444, i, argv[1]);
1024     printf("\n");
1025   }
1026 
1027 bailout:
1028   if (srcBuf) tjFree(srcBuf);
1029   return retval;
1030 }
1031