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