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