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