blob: be6d23caeed22ca6f76e208309b75407ec8ce290 [file] [log] [blame]
DRCb8b359a2011-05-25 03:54:56 +00001/*
DRC1ff90822019-01-01 21:08:27 -06002 * Copyright (C)2009-2019 D. R. Commander. All Rights Reserved.
DRCb8b359a2011-05-25 03:54:56 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
DRCc6501f72013-11-04 23:07:54 +000032#include <ctype.h>
DRCb8b359a2011-05-25 03:54:56 +000033#include <math.h>
34#include <errno.h>
DRC2a9e3bd2019-07-11 15:30:04 -050035#include <limits.h>
DRCb8b359a2011-05-25 03:54:56 +000036#include <cdjpeg.h>
DRCb8b359a2011-05-25 03:54:56 +000037#include "./tjutil.h"
38#include "./turbojpeg.h"
39
40
DRCbce58f42019-04-12 07:49:35 -050041#define THROW(op, err) { \
DRC19c791c2018-03-08 10:55:20 -060042 printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
43 retval = -1; goto bailout; \
DRCc9453122017-06-29 16:49:09 -050044}
DRCbce58f42019-04-12 07:49:35 -050045#define THROW_UNIX(m) THROW(m, strerror(errno))
DRCc9453122017-06-29 16:49:09 -050046
DRC19c791c2018-03-08 10:55:20 -060047char tjErrorStr[JMSG_LENGTH_MAX] = "\0", tjErrorMsg[JMSG_LENGTH_MAX] = "\0";
48int tjErrorLine = -1, tjErrorCode = -1;
DRCc9453122017-06-29 16:49:09 -050049
DRCbce58f42019-04-12 07:49:35 -050050#define THROW_TJG(m) { \
DRC19c791c2018-03-08 10:55:20 -060051 printf("ERROR in line %d while %s:\n%s\n", __LINE__, m, \
52 tjGetErrorStr2(NULL)); \
53 retval = -1; goto bailout; \
DRCaa745902017-11-16 18:09:07 -060054}
55
DRCbce58f42019-04-12 07:49:35 -050056#define THROW_TJ(m) { \
DRC19c791c2018-03-08 10:55:20 -060057 int _tjErrorCode = tjGetErrorCode(handle); \
58 char *_tjErrorStr = tjGetErrorStr2(handle); \
DRCc9453122017-06-29 16:49:09 -050059 \
DRC19c791c2018-03-08 10:55:20 -060060 if (!(flags & TJFLAG_STOPONWARNING) && _tjErrorCode == TJERR_WARNING) { \
61 if (strncmp(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX) || \
62 strncmp(tjErrorMsg, m, JMSG_LENGTH_MAX) || \
63 tjErrorCode != _tjErrorCode || tjErrorLine != __LINE__) { \
DRCc7010142018-11-12 12:27:23 -060064 strncpy(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX - 1); \
65 strncpy(tjErrorMsg, m, JMSG_LENGTH_MAX - 1); \
DRC19c791c2018-03-08 10:55:20 -060066 tjErrorCode = _tjErrorCode; \
67 tjErrorLine = __LINE__; \
68 printf("WARNING in line %d while %s:\n%s\n", __LINE__, m, _tjErrorStr); \
69 } \
70 } else { \
71 printf("%s in line %d while %s:\n%s\n", \
72 _tjErrorCode == TJERR_WARNING ? "WARNING" : "ERROR", __LINE__, m, \
73 _tjErrorStr); \
74 retval = -1; goto bailout; \
75 } \
DRCc9453122017-06-29 16:49:09 -050076}
77
DRC19c791c2018-03-08 10:55:20 -060078int flags = TJFLAG_NOREALLOC, compOnly = 0, decompOnly = 0, doYUV = 0,
79 quiet = 0, doTile = 0, pf = TJPF_BGR, yuvPad = 1, doWrite = 1;
80char *ext = "ppm";
81const char *pixFormatStr[TJ_NUMPF] = {
82 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
DRCb8b359a2011-05-25 03:54:56 +000083};
DRC19c791c2018-03-08 10:55:20 -060084const char *subNameLong[TJ_NUMSAMP] = {
85 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
DRCb8b359a2011-05-25 03:54:56 +000086};
DRC19c791c2018-03-08 10:55:20 -060087const char *csName[TJ_NUMCS] = {
88 "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
DRCcd7c3e62013-08-23 02:49:25 +000089};
DRC19c791c2018-03-08 10:55:20 -060090const char *subName[TJ_NUMSAMP] = {
91 "444", "422", "420", "GRAY", "440", "411"
92};
93tjscalingfactor *scalingFactors = NULL, sf = { 1, 1 };
94int nsf = 0, xformOp = TJXOP_NONE, xformOpt = 0;
95int (*customFilter) (short *, tjregion, tjregion, int, int, tjtransform *);
96double benchTime = 5.0, warmup = 1.0;
DRCb8b359a2011-05-25 03:54:56 +000097
98
DRC6399d0a2019-04-23 14:10:04 -050099static char *formatName(int subsamp, int cs, char *buf)
DRCcd7c3e62013-08-23 02:49:25 +0000100{
DRC19c791c2018-03-08 10:55:20 -0600101 if (cs == TJCS_YCbCr)
102 return (char *)subNameLong[subsamp];
DRC2401e4d2018-04-26 18:01:52 -0500103 else if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
DRC19c791c2018-03-08 10:55:20 -0600104 snprintf(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
105 return buf;
106 } else
107 return (char *)csName[cs];
DRCcd7c3e62013-08-23 02:49:25 +0000108}
109
110
DRC6399d0a2019-04-23 14:10:04 -0500111static char *sigfig(double val, int figs, char *buf, int len)
DRCb8b359a2011-05-25 03:54:56 +0000112{
DRC19c791c2018-03-08 10:55:20 -0600113 char format[80];
114 int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));
115
116 if (digitsAfterDecimal < 1)
117 snprintf(format, 80, "%%.0f");
118 else
119 snprintf(format, 80, "%%.%df", digitsAfterDecimal);
120 snprintf(buf, len, format, val);
121 return buf;
DRCb8b359a2011-05-25 03:54:56 +0000122}
123
124
DRC57a37362011-09-17 00:41:14 +0000125/* Custom DCT filter which produces a negative of the image */
DRC6399d0a2019-04-23 14:10:04 -0500126static int dummyDCTFilter(short *coeffs, tjregion arrayRegion,
127 tjregion planeRegion, int componentIndex,
128 int transformIndex, tjtransform *transform)
DRC7bf04d32011-09-17 00:18:31 +0000129{
DRC19c791c2018-03-08 10:55:20 -0600130 int i;
131
132 for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
133 coeffs[i] = -coeffs[i];
134 return 0;
DRC7bf04d32011-09-17 00:18:31 +0000135}
136
DRC57a37362011-09-17 00:41:14 +0000137
DRCb8b359a2011-05-25 03:54:56 +0000138/* Decompression test */
DRC6399d0a2019-04-23 14:10:04 -0500139static int decomp(unsigned char *srcBuf, unsigned char **jpegBuf,
140 unsigned long *jpegSize, unsigned char *dstBuf, int w, int h,
141 int subsamp, int jpegQual, char *fileName, int tilew,
142 int tileh)
DRCb8b359a2011-05-25 03:54:56 +0000143{
DRC6399d0a2019-04-23 14:10:04 -0500144 char tempStr[1024], sizeStr[24] = "\0", qualStr[13] = "\0", *ptr;
DRC19c791c2018-03-08 10:55:20 -0600145 FILE *file = NULL;
146 tjhandle handle = NULL;
147 int row, col, iter = 0, dstBufAlloc = 0, retval = 0;
148 double elapsed, elapsedDecode;
149 int ps = tjPixelSize[pf];
150 int scaledw = TJSCALED(w, sf);
151 int scaledh = TJSCALED(h, sf);
152 int pitch = scaledw * ps;
153 int ntilesw = (w + tilew - 1) / tilew, ntilesh = (h + tileh - 1) / tileh;
154 unsigned char *dstPtr, *dstPtr2, *yuvBuf = NULL;
DRCb8b359a2011-05-25 03:54:56 +0000155
DRC19c791c2018-03-08 10:55:20 -0600156 if (jpegQual > 0) {
DRC031e16e2019-02-11 22:43:15 -0600157 snprintf(qualStr, 13, "_Q%d", jpegQual);
158 qualStr[12] = 0;
DRC19c791c2018-03-08 10:55:20 -0600159 }
DRCb8b359a2011-05-25 03:54:56 +0000160
DRC19c791c2018-03-08 10:55:20 -0600161 if ((handle = tjInitDecompress()) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500162 THROW_TJ("executing tjInitDecompress()");
DRCb8b359a2011-05-25 03:54:56 +0000163
DRC19c791c2018-03-08 10:55:20 -0600164 if (dstBuf == NULL) {
DRC2a9e3bd2019-07-11 15:30:04 -0500165 if ((unsigned long long)pitch * (unsigned long long)scaledh >
166 (unsigned long long)((size_t)-1))
167 THROW("allocating destination buffer", "Image is too large");
168 if ((dstBuf = (unsigned char *)malloc((size_t)pitch * scaledh)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500169 THROW_UNIX("allocating destination buffer");
DRC19c791c2018-03-08 10:55:20 -0600170 dstBufAlloc = 1;
171 }
172 /* Set the destination buffer to gray so we know whether the decompressor
173 attempted to write to it */
174 memset(dstBuf, 127, pitch * scaledh);
DRCb8b359a2011-05-25 03:54:56 +0000175
DRC19c791c2018-03-08 10:55:20 -0600176 if (doYUV) {
177 int width = doTile ? tilew : scaledw;
178 int height = doTile ? tileh : scaledh;
DRC2a9e3bd2019-07-11 15:30:04 -0500179 unsigned long yuvSize = tjBufSizeYUV2(width, yuvPad, height, subsamp);
DRCb8b359a2011-05-25 03:54:56 +0000180
DRC2a9e3bd2019-07-11 15:30:04 -0500181 if (yuvSize == (unsigned long)-1)
182 THROW_TJ("allocating YUV buffer");
DRC19c791c2018-03-08 10:55:20 -0600183 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500184 THROW_UNIX("allocating YUV buffer");
DRC19c791c2018-03-08 10:55:20 -0600185 memset(yuvBuf, 127, yuvSize);
186 }
DRCb8b359a2011-05-25 03:54:56 +0000187
DRC19c791c2018-03-08 10:55:20 -0600188 /* Benchmark */
189 iter = -1;
190 elapsed = elapsedDecode = 0.;
191 while (1) {
192 int tile = 0;
193 double start = getTime();
DRCb8b359a2011-05-25 03:54:56 +0000194
DRC19c791c2018-03-08 10:55:20 -0600195 for (row = 0, dstPtr = dstBuf; row < ntilesh;
196 row++, dstPtr += pitch * tileh) {
197 for (col = 0, dstPtr2 = dstPtr; col < ntilesw;
198 col++, tile++, dstPtr2 += ps * tilew) {
199 int width = doTile ? min(tilew, w - col * tilew) : scaledw;
200 int height = doTile ? min(tileh, h - row * tileh) : scaledh;
DRC9c8cee82016-01-13 13:01:45 -0600201
DRC19c791c2018-03-08 10:55:20 -0600202 if (doYUV) {
203 double startDecode;
DRC9c8cee82016-01-13 13:01:45 -0600204
DRC19c791c2018-03-08 10:55:20 -0600205 if (tjDecompressToYUV2(handle, jpegBuf[tile], jpegSize[tile], yuvBuf,
206 width, yuvPad, height, flags) == -1)
DRCbce58f42019-04-12 07:49:35 -0500207 THROW_TJ("executing tjDecompressToYUV2()");
DRC19c791c2018-03-08 10:55:20 -0600208 startDecode = getTime();
209 if (tjDecodeYUV(handle, yuvBuf, yuvPad, subsamp, dstPtr2, width,
210 pitch, height, pf, flags) == -1)
DRCbce58f42019-04-12 07:49:35 -0500211 THROW_TJ("executing tjDecodeYUV()");
DRC19c791c2018-03-08 10:55:20 -0600212 if (iter >= 0) elapsedDecode += getTime() - startDecode;
213 } else if (tjDecompress2(handle, jpegBuf[tile], jpegSize[tile],
214 dstPtr2, width, pitch, height, pf,
215 flags) == -1)
DRCbce58f42019-04-12 07:49:35 -0500216 THROW_TJ("executing tjDecompress2()");
DRC19c791c2018-03-08 10:55:20 -0600217 }
218 }
219 elapsed += getTime() - start;
220 if (iter >= 0) {
221 iter++;
222 if (elapsed >= benchTime) break;
223 } else if (elapsed >= warmup) {
224 iter = 0;
225 elapsed = elapsedDecode = 0.;
226 }
227 }
228 if (doYUV) elapsed -= elapsedDecode;
DRCc6501f72013-11-04 23:07:54 +0000229
DRCbce58f42019-04-12 07:49:35 -0500230 if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
DRC19c791c2018-03-08 10:55:20 -0600231 handle = NULL;
DRCb8b359a2011-05-25 03:54:56 +0000232
DRC19c791c2018-03-08 10:55:20 -0600233 if (quiet) {
234 printf("%-6s%s",
235 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
236 tempStr, 1024),
237 quiet == 2 ? "\n" : " ");
238 if (doYUV)
239 printf("%s\n",
240 sigfig((double)(w * h) / 1000000. * (double)iter / elapsedDecode,
241 4, tempStr, 1024));
242 else if (quiet != 2) printf("\n");
243 } else {
244 printf("%s --> Frame rate: %f fps\n",
245 doYUV ? "Decomp to YUV" : "Decompress ", (double)iter / elapsed);
246 printf(" Throughput: %f Megapixels/sec\n",
247 (double)(w * h) / 1000000. * (double)iter / elapsed);
248 if (doYUV) {
249 printf("YUV Decode --> Frame rate: %f fps\n",
250 (double)iter / elapsedDecode);
251 printf(" Throughput: %f Megapixels/sec\n",
252 (double)(w * h) / 1000000. * (double)iter / elapsedDecode);
253 }
254 }
255
256 if (!doWrite) goto bailout;
257
258 if (sf.num != 1 || sf.denom != 1)
DRC6399d0a2019-04-23 14:10:04 -0500259 snprintf(sizeStr, 24, "%d_%d", sf.num, sf.denom);
DRC19c791c2018-03-08 10:55:20 -0600260 else if (tilew != w || tileh != h)
DRC6399d0a2019-04-23 14:10:04 -0500261 snprintf(sizeStr, 24, "%dx%d", tilew, tileh);
262 else snprintf(sizeStr, 24, "full");
DRC19c791c2018-03-08 10:55:20 -0600263 if (decompOnly)
264 snprintf(tempStr, 1024, "%s_%s.%s", fileName, sizeStr, ext);
265 else
266 snprintf(tempStr, 1024, "%s_%s%s_%s.%s", fileName, subName[subsamp],
267 qualStr, sizeStr, ext);
268
269 if (tjSaveImage(tempStr, dstBuf, scaledw, 0, scaledh, pf, flags) == -1)
DRCbce58f42019-04-12 07:49:35 -0500270 THROW_TJG("saving bitmap");
DRC19c791c2018-03-08 10:55:20 -0600271 ptr = strrchr(tempStr, '.');
272 snprintf(ptr, 1024 - (ptr - tempStr), "-err.%s", ext);
273 if (srcBuf && sf.num == 1 && sf.denom == 1) {
274 if (!quiet) printf("Compression error written to %s.\n", tempStr);
275 if (subsamp == TJ_GRAYSCALE) {
DRC2a9e3bd2019-07-11 15:30:04 -0500276 unsigned long index, index2;
DRC19c791c2018-03-08 10:55:20 -0600277
278 for (row = 0, index = 0; row < h; row++, index += pitch) {
279 for (col = 0, index2 = index; col < w; col++, index2 += ps) {
DRC2a9e3bd2019-07-11 15:30:04 -0500280 unsigned long rindex = index2 + tjRedOffset[pf];
281 unsigned long gindex = index2 + tjGreenOffset[pf];
282 unsigned long bindex = index2 + tjBlueOffset[pf];
DRC19c791c2018-03-08 10:55:20 -0600283 int y = (int)((double)srcBuf[rindex] * 0.299 +
284 (double)srcBuf[gindex] * 0.587 +
285 (double)srcBuf[bindex] * 0.114 + 0.5);
286
287 if (y > 255) y = 255;
288 if (y < 0) y = 0;
289 dstBuf[rindex] = abs(dstBuf[rindex] - y);
290 dstBuf[gindex] = abs(dstBuf[gindex] - y);
291 dstBuf[bindex] = abs(dstBuf[bindex] - y);
292 }
293 }
294 } else {
295 for (row = 0; row < h; row++)
296 for (col = 0; col < w * ps; col++)
297 dstBuf[pitch * row + col] =
298 abs(dstBuf[pitch * row + col] - srcBuf[pitch * row + col]);
299 }
300 if (tjSaveImage(tempStr, dstBuf, w, 0, h, pf, flags) == -1)
DRCbce58f42019-04-12 07:49:35 -0500301 THROW_TJG("saving bitmap");
DRC19c791c2018-03-08 10:55:20 -0600302 }
303
304bailout:
305 if (file) fclose(file);
306 if (handle) tjDestroy(handle);
307 if (dstBuf && dstBufAlloc) free(dstBuf);
308 if (yuvBuf) free(yuvBuf);
309 return retval;
DRCb8b359a2011-05-25 03:54:56 +0000310}
311
312
DRC6399d0a2019-04-23 14:10:04 -0500313static int fullTest(unsigned char *srcBuf, int w, int h, int subsamp,
314 int jpegQual, char *fileName)
DRCb8b359a2011-05-25 03:54:56 +0000315{
DRC19c791c2018-03-08 10:55:20 -0600316 char tempStr[1024], tempStr2[80];
317 FILE *file = NULL;
318 tjhandle handle = NULL;
319 unsigned char **jpegBuf = NULL, *yuvBuf = NULL, *tmpBuf = NULL, *srcPtr,
320 *srcPtr2;
321 double start, elapsed, elapsedEncode;
322 int totalJpegSize = 0, row, col, i, tilew = w, tileh = h, retval = 0;
DRC2a9e3bd2019-07-11 15:30:04 -0500323 int iter;
324 unsigned long *jpegSize = NULL, yuvSize = 0;
DRC19c791c2018-03-08 10:55:20 -0600325 int ps = tjPixelSize[pf];
326 int ntilesw = 1, ntilesh = 1, pitch = w * ps;
327 const char *pfStr = pixFormatStr[pf];
DRCb8b359a2011-05-25 03:54:56 +0000328
DRC2a9e3bd2019-07-11 15:30:04 -0500329 if ((unsigned long long)pitch * (unsigned long long)h >
330 (unsigned long long)((size_t)-1))
331 THROW("allocating temporary image buffer", "Image is too large");
332 if ((tmpBuf = (unsigned char *)malloc((size_t)pitch * h)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500333 THROW_UNIX("allocating temporary image buffer");
DRCb8b359a2011-05-25 03:54:56 +0000334
DRC19c791c2018-03-08 10:55:20 -0600335 if (!quiet)
336 printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pfStr,
337 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down",
338 subNameLong[subsamp], jpegQual);
DRCb8b359a2011-05-25 03:54:56 +0000339
DRC19c791c2018-03-08 10:55:20 -0600340 for (tilew = doTile ? 8 : w, tileh = doTile ? 8 : h; ;
341 tilew *= 2, tileh *= 2) {
342 if (tilew > w) tilew = w;
343 if (tileh > h) tileh = h;
344 ntilesw = (w + tilew - 1) / tilew;
345 ntilesh = (h + tileh - 1) / tileh;
DRCb8b359a2011-05-25 03:54:56 +0000346
DRC19c791c2018-03-08 10:55:20 -0600347 if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
348 ntilesw * ntilesh)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500349 THROW_UNIX("allocating JPEG tile array");
DRC19c791c2018-03-08 10:55:20 -0600350 memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
351 if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
352 ntilesw * ntilesh)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500353 THROW_UNIX("allocating JPEG size array");
DRC19c791c2018-03-08 10:55:20 -0600354 memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
DRCb8b359a2011-05-25 03:54:56 +0000355
DRC19c791c2018-03-08 10:55:20 -0600356 if ((flags & TJFLAG_NOREALLOC) != 0)
357 for (i = 0; i < ntilesw * ntilesh; i++) {
DRC2a9e3bd2019-07-11 15:30:04 -0500358 if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
359 THROW("getting buffer size", "Image is too large");
DRC19c791c2018-03-08 10:55:20 -0600360 if ((jpegBuf[i] = (unsigned char *)
361 tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500362 THROW_UNIX("allocating JPEG tiles");
DRC19c791c2018-03-08 10:55:20 -0600363 }
DRCb8b359a2011-05-25 03:54:56 +0000364
DRC19c791c2018-03-08 10:55:20 -0600365 /* Compression test */
366 if (quiet == 1)
367 printf("%-4s (%s) %-5s %-3d ", pfStr,
368 (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", subNameLong[subsamp],
369 jpegQual);
370 for (i = 0; i < h; i++)
371 memcpy(&tmpBuf[pitch * i], &srcBuf[w * ps * i], w * ps);
372 if ((handle = tjInitCompress()) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500373 THROW_TJ("executing tjInitCompress()");
DRCb8b359a2011-05-25 03:54:56 +0000374
DRC19c791c2018-03-08 10:55:20 -0600375 if (doYUV) {
376 yuvSize = tjBufSizeYUV2(tilew, yuvPad, tileh, subsamp);
DRC2a9e3bd2019-07-11 15:30:04 -0500377 if (yuvSize == (unsigned long)-1)
378 THROW_TJ("allocating YUV buffer");
DRC19c791c2018-03-08 10:55:20 -0600379 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500380 THROW_UNIX("allocating YUV buffer");
DRC19c791c2018-03-08 10:55:20 -0600381 memset(yuvBuf, 127, yuvSize);
382 }
DRCb8b359a2011-05-25 03:54:56 +0000383
DRC19c791c2018-03-08 10:55:20 -0600384 /* Benchmark */
385 iter = -1;
386 elapsed = elapsedEncode = 0.;
387 while (1) {
388 int tile = 0;
DRCb8b359a2011-05-25 03:54:56 +0000389
DRC19c791c2018-03-08 10:55:20 -0600390 totalJpegSize = 0;
391 start = getTime();
392 for (row = 0, srcPtr = srcBuf; row < ntilesh;
393 row++, srcPtr += pitch * tileh) {
394 for (col = 0, srcPtr2 = srcPtr; col < ntilesw;
395 col++, tile++, srcPtr2 += ps * tilew) {
396 int width = min(tilew, w - col * tilew);
397 int height = min(tileh, h - row * tileh);
DRCb8b359a2011-05-25 03:54:56 +0000398
DRC19c791c2018-03-08 10:55:20 -0600399 if (doYUV) {
400 double startEncode = getTime();
DRCb8b359a2011-05-25 03:54:56 +0000401
DRC19c791c2018-03-08 10:55:20 -0600402 if (tjEncodeYUV3(handle, srcPtr2, width, pitch, height, pf, yuvBuf,
403 yuvPad, subsamp, flags) == -1)
DRCbce58f42019-04-12 07:49:35 -0500404 THROW_TJ("executing tjEncodeYUV3()");
DRC19c791c2018-03-08 10:55:20 -0600405 if (iter >= 0) elapsedEncode += getTime() - startEncode;
406 if (tjCompressFromYUV(handle, yuvBuf, width, yuvPad, height,
407 subsamp, &jpegBuf[tile], &jpegSize[tile],
408 jpegQual, flags) == -1)
DRCbce58f42019-04-12 07:49:35 -0500409 THROW_TJ("executing tjCompressFromYUV()");
DRC19c791c2018-03-08 10:55:20 -0600410 } else {
411 if (tjCompress2(handle, srcPtr2, width, pitch, height, pf,
412 &jpegBuf[tile], &jpegSize[tile], subsamp, jpegQual,
413 flags) == -1)
DRCbce58f42019-04-12 07:49:35 -0500414 THROW_TJ("executing tjCompress2()");
DRC19c791c2018-03-08 10:55:20 -0600415 }
416 totalJpegSize += jpegSize[tile];
417 }
418 }
419 elapsed += getTime() - start;
420 if (iter >= 0) {
421 iter++;
422 if (elapsed >= benchTime) break;
423 } else if (elapsed >= warmup) {
424 iter = 0;
425 elapsed = elapsedEncode = 0.;
426 }
427 }
428 if (doYUV) elapsed -= elapsedEncode;
DRCb8b359a2011-05-25 03:54:56 +0000429
DRCbce58f42019-04-12 07:49:35 -0500430 if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
DRC19c791c2018-03-08 10:55:20 -0600431 handle = NULL;
DRCb8b359a2011-05-25 03:54:56 +0000432
DRC19c791c2018-03-08 10:55:20 -0600433 if (quiet == 1) printf("%-5d %-5d ", tilew, tileh);
434 if (quiet) {
435 if (doYUV)
436 printf("%-6s%s",
437 sigfig((double)(w * h) / 1000000. *
438 (double)iter / elapsedEncode, 4, tempStr, 1024),
439 quiet == 2 ? "\n" : " ");
440 printf("%-6s%s",
441 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
442 tempStr, 1024),
443 quiet == 2 ? "\n" : " ");
444 printf("%-6s%s",
445 sigfig((double)(w * h * ps) / (double)totalJpegSize, 4, tempStr2,
446 80),
447 quiet == 2 ? "\n" : " ");
448 } else {
449 printf("\n%s size: %d x %d\n", doTile ? "Tile" : "Image", tilew, tileh);
450 if (doYUV) {
451 printf("Encode YUV --> Frame rate: %f fps\n",
452 (double)iter / elapsedEncode);
DRC2a9e3bd2019-07-11 15:30:04 -0500453 printf(" Output image size: %lu bytes\n", yuvSize);
DRC19c791c2018-03-08 10:55:20 -0600454 printf(" Compression ratio: %f:1\n",
455 (double)(w * h * ps) / (double)yuvSize);
456 printf(" Throughput: %f Megapixels/sec\n",
457 (double)(w * h) / 1000000. * (double)iter / elapsedEncode);
458 printf(" Output bit stream: %f Megabits/sec\n",
459 (double)yuvSize * 8. / 1000000. * (double)iter / elapsedEncode);
460 }
461 printf("%s --> Frame rate: %f fps\n",
462 doYUV ? "Comp from YUV" : "Compress ",
463 (double)iter / elapsed);
464 printf(" Output image size: %d bytes\n",
465 totalJpegSize);
466 printf(" Compression ratio: %f:1\n",
467 (double)(w * h * ps) / (double)totalJpegSize);
468 printf(" Throughput: %f Megapixels/sec\n",
469 (double)(w * h) / 1000000. * (double)iter / elapsed);
470 printf(" Output bit stream: %f Megabits/sec\n",
471 (double)totalJpegSize * 8. / 1000000. * (double)iter / elapsed);
472 }
473 if (tilew == w && tileh == h && doWrite) {
474 snprintf(tempStr, 1024, "%s_%s_Q%d.jpg", fileName, subName[subsamp],
475 jpegQual);
476 if ((file = fopen(tempStr, "wb")) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500477 THROW_UNIX("opening reference image");
DRC19c791c2018-03-08 10:55:20 -0600478 if (fwrite(jpegBuf[0], jpegSize[0], 1, file) != 1)
DRCbce58f42019-04-12 07:49:35 -0500479 THROW_UNIX("writing reference image");
DRC19c791c2018-03-08 10:55:20 -0600480 fclose(file); file = NULL;
481 if (!quiet) printf("Reference image written to %s\n", tempStr);
482 }
DRCb8b359a2011-05-25 03:54:56 +0000483
DRC19c791c2018-03-08 10:55:20 -0600484 /* Decompression test */
485 if (!compOnly) {
486 if (decomp(srcBuf, jpegBuf, jpegSize, tmpBuf, w, h, subsamp, jpegQual,
487 fileName, tilew, tileh) == -1)
488 goto bailout;
489 }
490
491 for (i = 0; i < ntilesw * ntilesh; i++) {
492 if (jpegBuf[i]) tjFree(jpegBuf[i]);
493 jpegBuf[i] = NULL;
494 }
495 free(jpegBuf); jpegBuf = NULL;
496 free(jpegSize); jpegSize = NULL;
497 if (doYUV) {
498 free(yuvBuf); yuvBuf = NULL;
499 }
500
501 if (tilew == w && tileh == h) break;
502 }
503
504bailout:
505 if (file) { fclose(file); file = NULL; }
506 if (jpegBuf) {
507 for (i = 0; i < ntilesw * ntilesh; i++) {
508 if (jpegBuf[i]) tjFree(jpegBuf[i]);
509 jpegBuf[i] = NULL;
510 }
511 free(jpegBuf); jpegBuf = NULL;
512 }
513 if (yuvBuf) { free(yuvBuf); yuvBuf = NULL; }
514 if (jpegSize) { free(jpegSize); jpegSize = NULL; }
515 if (tmpBuf) { free(tmpBuf); tmpBuf = NULL; }
516 if (handle) { tjDestroy(handle); handle = NULL; }
517 return retval;
DRCb8b359a2011-05-25 03:54:56 +0000518}
519
520
DRC6399d0a2019-04-23 14:10:04 -0500521static int decompTest(char *fileName)
DRCb8b359a2011-05-25 03:54:56 +0000522{
DRC19c791c2018-03-08 10:55:20 -0600523 FILE *file = NULL;
524 tjhandle handle = NULL;
525 unsigned char **jpegBuf = NULL, *srcBuf = NULL;
526 unsigned long *jpegSize = NULL, srcSize, totalJpegSize;
527 tjtransform *t = NULL;
DRC19c791c2018-03-08 10:55:20 -0600528 double start, elapsed;
DRC53bb9412018-05-15 14:51:49 -0500529 int ps = tjPixelSize[pf], tile, row, col, i, iter, retval = 0, decompsrc = 0;
530 char *temp = NULL, tempStr[80], tempStr2[80];
531 /* Original image */
532 int w = 0, h = 0, tilew, tileh, ntilesw = 1, ntilesh = 1, subsamp = -1,
533 cs = -1;
534 /* Transformed image */
535 int tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;
DRCb8b359a2011-05-25 03:54:56 +0000536
DRC19c791c2018-03-08 10:55:20 -0600537 if ((file = fopen(fileName, "rb")) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500538 THROW_UNIX("opening file");
DRC19c791c2018-03-08 10:55:20 -0600539 if (fseek(file, 0, SEEK_END) < 0 ||
540 (srcSize = ftell(file)) == (unsigned long)-1)
DRCbce58f42019-04-12 07:49:35 -0500541 THROW_UNIX("determining file size");
DRC19c791c2018-03-08 10:55:20 -0600542 if ((srcBuf = (unsigned char *)malloc(srcSize)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500543 THROW_UNIX("allocating memory");
DRC19c791c2018-03-08 10:55:20 -0600544 if (fseek(file, 0, SEEK_SET) < 0)
DRCbce58f42019-04-12 07:49:35 -0500545 THROW_UNIX("setting file position");
DRC19c791c2018-03-08 10:55:20 -0600546 if (fread(srcBuf, srcSize, 1, file) < 1)
DRCbce58f42019-04-12 07:49:35 -0500547 THROW_UNIX("reading JPEG data");
DRC19c791c2018-03-08 10:55:20 -0600548 fclose(file); file = NULL;
DRCb8b359a2011-05-25 03:54:56 +0000549
DRC19c791c2018-03-08 10:55:20 -0600550 temp = strrchr(fileName, '.');
551 if (temp != NULL) *temp = '\0';
DRCb8b359a2011-05-25 03:54:56 +0000552
DRC19c791c2018-03-08 10:55:20 -0600553 if ((handle = tjInitTransform()) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500554 THROW_TJ("executing tjInitTransform()");
DRC19c791c2018-03-08 10:55:20 -0600555 if (tjDecompressHeader3(handle, srcBuf, srcSize, &w, &h, &subsamp,
556 &cs) == -1)
DRCbce58f42019-04-12 07:49:35 -0500557 THROW_TJ("executing tjDecompressHeader3()");
DRC1ff90822019-01-01 21:08:27 -0600558 if (w < 1 || h < 1)
DRCbce58f42019-04-12 07:49:35 -0500559 THROW("reading JPEG header", "Invalid image dimensions");
DRC19c791c2018-03-08 10:55:20 -0600560 if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
561 pf = TJPF_CMYK; ps = tjPixelSize[pf];
562 }
DRCb8b359a2011-05-25 03:54:56 +0000563
DRC19c791c2018-03-08 10:55:20 -0600564 if (quiet == 1) {
565 printf("All performance values in Mpixels/sec\n\n");
566 printf("Bitmap JPEG JPEG %s %s Xform Comp Decomp ",
567 doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
568 if (doYUV) printf("Decode");
569 printf("\n");
570 printf("Format CS Subsamp Width Height Perf Ratio Perf ");
571 if (doYUV) printf("Perf");
572 printf("\n\n");
573 } else if (!quiet)
574 printf(">>>>> JPEG %s --> %s (%s) <<<<<\n",
575 formatName(subsamp, cs, tempStr), pixFormatStr[pf],
576 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down");
DRCb8b359a2011-05-25 03:54:56 +0000577
DRC19c791c2018-03-08 10:55:20 -0600578 for (tilew = doTile ? 16 : w, tileh = doTile ? 16 : h; ;
579 tilew *= 2, tileh *= 2) {
580 if (tilew > w) tilew = w;
581 if (tileh > h) tileh = h;
582 ntilesw = (w + tilew - 1) / tilew;
583 ntilesh = (h + tileh - 1) / tileh;
DRCb8b359a2011-05-25 03:54:56 +0000584
DRC19c791c2018-03-08 10:55:20 -0600585 if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
586 ntilesw * ntilesh)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500587 THROW_UNIX("allocating JPEG tile array");
DRC19c791c2018-03-08 10:55:20 -0600588 memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
589 if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
590 ntilesw * ntilesh)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500591 THROW_UNIX("allocating JPEG size array");
DRC19c791c2018-03-08 10:55:20 -0600592 memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
DRCb8b359a2011-05-25 03:54:56 +0000593
DRC2a9e3bd2019-07-11 15:30:04 -0500594 if ((flags & TJFLAG_NOREALLOC) != 0 &&
595 (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter))
DRC19c791c2018-03-08 10:55:20 -0600596 for (i = 0; i < ntilesw * ntilesh; i++) {
DRC2a9e3bd2019-07-11 15:30:04 -0500597 if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
598 THROW("getting buffer size", "Image is too large");
DRC19c791c2018-03-08 10:55:20 -0600599 if ((jpegBuf[i] = (unsigned char *)
600 tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500601 THROW_UNIX("allocating JPEG tiles");
DRC19c791c2018-03-08 10:55:20 -0600602 }
DRCb8b359a2011-05-25 03:54:56 +0000603
DRC53bb9412018-05-15 14:51:49 -0500604 tw = w; th = h; ttilew = tilew; ttileh = tileh;
DRC19c791c2018-03-08 10:55:20 -0600605 if (!quiet) {
DRC53bb9412018-05-15 14:51:49 -0500606 printf("\n%s size: %d x %d", doTile ? "Tile" : "Image", ttilew, ttileh);
DRC19c791c2018-03-08 10:55:20 -0600607 if (sf.num != 1 || sf.denom != 1)
DRC53bb9412018-05-15 14:51:49 -0500608 printf(" --> %d x %d", TJSCALED(tw, sf), TJSCALED(th, sf));
DRC19c791c2018-03-08 10:55:20 -0600609 printf("\n");
610 } else if (quiet == 1) {
611 printf("%-4s (%s) %-5s %-5s ", pixFormatStr[pf],
612 (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", csName[cs],
613 subNameLong[subsamp]);
614 printf("%-5d %-5d ", tilew, tileh);
615 }
DRCb8b359a2011-05-25 03:54:56 +0000616
DRC53bb9412018-05-15 14:51:49 -0500617 tsubsamp = subsamp;
DRC19c791c2018-03-08 10:55:20 -0600618 if (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter) {
619 if ((t = (tjtransform *)malloc(sizeof(tjtransform) * ntilesw *
620 ntilesh)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500621 THROW_UNIX("allocating image transform array");
DRCb8b359a2011-05-25 03:54:56 +0000622
DRC19c791c2018-03-08 10:55:20 -0600623 if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
624 xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
DRC53bb9412018-05-15 14:51:49 -0500625 tw = h; th = w; ttilew = tileh; ttileh = tilew;
DRC19c791c2018-03-08 10:55:20 -0600626 }
DRCb8b359a2011-05-25 03:54:56 +0000627
DRC53bb9412018-05-15 14:51:49 -0500628 if (xformOpt & TJXOPT_GRAY) tsubsamp = TJ_GRAYSCALE;
DRC19c791c2018-03-08 10:55:20 -0600629 if (xformOp == TJXOP_HFLIP || xformOp == TJXOP_ROT180)
DRC53bb9412018-05-15 14:51:49 -0500630 tw = tw - (tw % tjMCUWidth[tsubsamp]);
DRC19c791c2018-03-08 10:55:20 -0600631 if (xformOp == TJXOP_VFLIP || xformOp == TJXOP_ROT180)
DRC53bb9412018-05-15 14:51:49 -0500632 th = th - (th % tjMCUHeight[tsubsamp]);
DRC19c791c2018-03-08 10:55:20 -0600633 if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT90)
DRC53bb9412018-05-15 14:51:49 -0500634 tw = tw - (tw % tjMCUHeight[tsubsamp]);
DRC19c791c2018-03-08 10:55:20 -0600635 if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT270)
DRC53bb9412018-05-15 14:51:49 -0500636 th = th - (th % tjMCUWidth[tsubsamp]);
637 tntilesw = (tw + ttilew - 1) / ttilew;
638 tntilesh = (th + ttileh - 1) / ttileh;
DRCb8b359a2011-05-25 03:54:56 +0000639
DRC19c791c2018-03-08 10:55:20 -0600640 if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
641 xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
DRC53bb9412018-05-15 14:51:49 -0500642 if (tsubsamp == TJSAMP_422) tsubsamp = TJSAMP_440;
643 else if (tsubsamp == TJSAMP_440) tsubsamp = TJSAMP_422;
DRC19c791c2018-03-08 10:55:20 -0600644 }
DRCce6891f2014-03-13 20:33:43 +0000645
DRC53bb9412018-05-15 14:51:49 -0500646 for (row = 0, tile = 0; row < tntilesh; row++) {
647 for (col = 0; col < tntilesw; col++, tile++) {
648 t[tile].r.w = min(ttilew, tw - col * ttilew);
649 t[tile].r.h = min(ttileh, th - row * ttileh);
650 t[tile].r.x = col * ttilew;
651 t[tile].r.y = row * ttileh;
DRC19c791c2018-03-08 10:55:20 -0600652 t[tile].op = xformOp;
653 t[tile].options = xformOpt | TJXOPT_TRIM;
654 t[tile].customFilter = customFilter;
655 if (t[tile].options & TJXOPT_NOOUTPUT && jpegBuf[tile]) {
656 tjFree(jpegBuf[tile]); jpegBuf[tile] = NULL;
657 }
658 }
659 }
DRCb8b359a2011-05-25 03:54:56 +0000660
DRC19c791c2018-03-08 10:55:20 -0600661 iter = -1;
662 elapsed = 0.;
663 while (1) {
664 start = getTime();
DRC53bb9412018-05-15 14:51:49 -0500665 if (tjTransform(handle, srcBuf, srcSize, tntilesw * tntilesh, jpegBuf,
DRC19c791c2018-03-08 10:55:20 -0600666 jpegSize, t, flags) == -1)
DRCbce58f42019-04-12 07:49:35 -0500667 THROW_TJ("executing tjTransform()");
DRC19c791c2018-03-08 10:55:20 -0600668 elapsed += getTime() - start;
669 if (iter >= 0) {
670 iter++;
671 if (elapsed >= benchTime) break;
672 } else if (elapsed >= warmup) {
673 iter = 0;
674 elapsed = 0.;
675 }
676 }
DRCb8b359a2011-05-25 03:54:56 +0000677
DRC19c791c2018-03-08 10:55:20 -0600678 free(t); t = NULL;
DRCb8b359a2011-05-25 03:54:56 +0000679
DRC53bb9412018-05-15 14:51:49 -0500680 for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
DRC19c791c2018-03-08 10:55:20 -0600681 totalJpegSize += jpegSize[tile];
DRCb8b359a2011-05-25 03:54:56 +0000682
DRC19c791c2018-03-08 10:55:20 -0600683 if (quiet) {
684 printf("%-6s%s%-6s%s",
685 sigfig((double)(w * h) / 1000000. / elapsed, 4, tempStr, 80),
686 quiet == 2 ? "\n" : " ",
687 sigfig((double)(w * h * ps) / (double)totalJpegSize, 4,
688 tempStr2, 80),
689 quiet == 2 ? "\n" : " ");
690 } else if (!quiet) {
691 printf("Transform --> Frame rate: %f fps\n",
692 1.0 / elapsed);
693 printf(" Output image size: %lu bytes\n",
694 totalJpegSize);
695 printf(" Compression ratio: %f:1\n",
696 (double)(w * h * ps) / (double)totalJpegSize);
697 printf(" Throughput: %f Megapixels/sec\n",
698 (double)(w * h) / 1000000. / elapsed);
699 printf(" Output bit stream: %f Megabits/sec\n",
700 (double)totalJpegSize * 8. / 1000000. / elapsed);
701 }
702 } else {
703 if (quiet == 1) printf("N/A N/A ");
DRCb25adab2019-07-12 17:27:37 -0500704 if (jpegBuf[0]) tjFree(jpegBuf[0]);
DRC19c791c2018-03-08 10:55:20 -0600705 jpegBuf[0] = NULL;
706 decompsrc = 1;
707 }
DRCb8b359a2011-05-25 03:54:56 +0000708
DRC53bb9412018-05-15 14:51:49 -0500709 if (w == tilew) ttilew = tw;
710 if (h == tileh) ttileh = th;
DRC19c791c2018-03-08 10:55:20 -0600711 if (!(xformOpt & TJXOPT_NOOUTPUT)) {
712 if (decomp(NULL, decompsrc ? &srcBuf : jpegBuf,
DRC53bb9412018-05-15 14:51:49 -0500713 decompsrc ? &srcSize : jpegSize, NULL, tw, th, tsubsamp, 0,
714 fileName, ttilew, ttileh) == -1)
DRC19c791c2018-03-08 10:55:20 -0600715 goto bailout;
716 } else if (quiet == 1) printf("N/A\n");
DRCb8b359a2011-05-25 03:54:56 +0000717
DRC19c791c2018-03-08 10:55:20 -0600718 for (i = 0; i < ntilesw * ntilesh; i++) {
DRCb25adab2019-07-12 17:27:37 -0500719 if (jpegBuf[i]) tjFree(jpegBuf[i]);
DRC2a9e3bd2019-07-11 15:30:04 -0500720 jpegBuf[i] = NULL;
DRC19c791c2018-03-08 10:55:20 -0600721 }
722 free(jpegBuf); jpegBuf = NULL;
723 if (jpegSize) { free(jpegSize); jpegSize = NULL; }
DRCb8b359a2011-05-25 03:54:56 +0000724
DRC19c791c2018-03-08 10:55:20 -0600725 if (tilew == w && tileh == h) break;
726 }
DRCb8b359a2011-05-25 03:54:56 +0000727
DRC19c791c2018-03-08 10:55:20 -0600728bailout:
729 if (file) { fclose(file); file = NULL; }
730 if (jpegBuf) {
731 for (i = 0; i < ntilesw * ntilesh; i++) {
732 if (jpegBuf[i]) tjFree(jpegBuf[i]);
733 jpegBuf[i] = NULL;
734 }
735 free(jpegBuf); jpegBuf = NULL;
736 }
737 if (jpegSize) { free(jpegSize); jpegSize = NULL; }
738 if (srcBuf) { free(srcBuf); srcBuf = NULL; }
739 if (t) { free(t); t = NULL; }
740 if (handle) { tjDestroy(handle); handle = NULL; }
741 return retval;
DRCb8b359a2011-05-25 03:54:56 +0000742}
743
744
DRC6399d0a2019-04-23 14:10:04 -0500745static void usage(char *progName)
DRCb8b359a2011-05-25 03:54:56 +0000746{
DRC19c791c2018-03-08 10:55:20 -0600747 int i;
748
749 printf("USAGE: %s\n", progName);
750 printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
751 printf(" %s\n", progName);
752 printf(" <Inputfile (JPG)> [options]\n\n");
753 printf("Options:\n\n");
754 printf("-alloc = Dynamically allocate JPEG image buffers\n");
755 printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
756 printf("-bottomup = Test bottom-up compression/decompression\n");
757 printf("-tile = Test performance of the codec when the image is encoded as separate\n");
758 printf(" tiles of varying sizes.\n");
759 printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
760 printf(" Test the specified color conversion path in the codec (default = BGR)\n");
761 printf("-cmyk = Indirectly test YCCK JPEG compression/decompression (the source\n");
762 printf(" and destination bitmaps are still RGB. The conversion is done\n");
763 printf(" internally prior to compression or after decompression.)\n");
764 printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
765 printf(" the underlying codec\n");
766 printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
767 printf(" codec\n");
768 printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
769 printf(" underlying codec\n");
770 printf("-progressive = Use progressive entropy coding in JPEG images generated by\n");
771 printf(" compression and transform operations.\n");
772 printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
773 printf(" of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");
774 printf(" GRAY). The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
775 printf(" sequence.\n");
776 printf("-quiet = Output results in tabular rather than verbose format\n");
777 printf("-yuv = Test YUV encoding/decoding functions\n");
778 printf("-yuvpad <p> = If testing YUV encoding/decoding, this specifies the number of\n");
779 printf(" bytes to which each row of each plane in the intermediate YUV image is\n");
780 printf(" padded (default = 1)\n");
781 printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
782 printf(" factor of M/N (M/N = ");
783 for (i = 0; i < nsf; i++) {
784 printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
785 if (nsf == 2 && i != nsf - 1) printf(" or ");
786 else if (nsf > 2) {
787 if (i != nsf - 1) printf(", ");
788 if (i == nsf - 2) printf("or ");
789 }
790 if (i % 8 == 0 && i != 0) printf("\n ");
791 }
792 printf(")\n");
793 printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
794 printf(" Perform the corresponding lossless transform prior to\n");
795 printf(" decompression (these options are mutually exclusive)\n");
796 printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
797 printf(" test (can be combined with the other transforms above)\n");
798 printf("-copynone = Do not copy any extra markers (including EXIF and ICC profile data)\n");
799 printf(" when transforming the image.\n");
800 printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");
801 printf("-warmup <t> = Run each benchmark for <t> seconds (default = 1.0) prior to\n");
802 printf(" starting the timer, in order to prime the caches and thus improve the\n");
803 printf(" consistency of the results.\n");
804 printf("-componly = Stop after running compression tests. Do not test decompression.\n");
805 printf("-nowrite = Do not write reference or output images (improves consistency of\n");
806 printf(" performance measurements.)\n");
807 printf("-stoponwarning = Immediately discontinue the current\n");
808 printf(" compression/decompression/transform operation if the underlying codec\n");
809 printf(" throws a warning (non-fatal error)\n\n");
810 printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
811 printf("test will be performed for all quality values in the range.\n\n");
812 exit(1);
DRCb8b359a2011-05-25 03:54:56 +0000813}
814
815
816int main(int argc, char *argv[])
817{
DRC19c791c2018-03-08 10:55:20 -0600818 unsigned char *srcBuf = NULL;
819 int w = 0, h = 0, i, j, minQual = -1, maxQual = -1;
820 char *temp;
821 int minArg = 2, retval = 0, subsamp = -1;
DRCb8b359a2011-05-25 03:54:56 +0000822
DRC19c791c2018-03-08 10:55:20 -0600823 if ((scalingFactors = tjGetScalingFactors(&nsf)) == NULL || nsf == 0)
DRCbce58f42019-04-12 07:49:35 -0500824 THROW("executing tjGetScalingFactors()", tjGetErrorStr());
DRCb8b359a2011-05-25 03:54:56 +0000825
DRC19c791c2018-03-08 10:55:20 -0600826 if (argc < minArg) usage(argv[0]);
DRCb8b359a2011-05-25 03:54:56 +0000827
DRC19c791c2018-03-08 10:55:20 -0600828 temp = strrchr(argv[1], '.');
829 if (temp != NULL) {
830 if (!strcasecmp(temp, ".bmp")) ext = "bmp";
831 if (!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg"))
832 decompOnly = 1;
833 }
DRCb8b359a2011-05-25 03:54:56 +0000834
DRC19c791c2018-03-08 10:55:20 -0600835 printf("\n");
DRCb8b359a2011-05-25 03:54:56 +0000836
DRC19c791c2018-03-08 10:55:20 -0600837 if (!decompOnly) {
838 minArg = 3;
839 if (argc < minArg) usage(argv[0]);
840 if ((minQual = atoi(argv[2])) < 1 || minQual > 100) {
841 puts("ERROR: Quality must be between 1 and 100.");
842 exit(1);
843 }
844 if ((temp = strchr(argv[2], '-')) != NULL && strlen(temp) > 1 &&
845 sscanf(&temp[1], "%d", &maxQual) == 1 && maxQual > minQual &&
846 maxQual >= 1 && maxQual <= 100) {}
847 else maxQual = minQual;
848 }
DRCb8b359a2011-05-25 03:54:56 +0000849
DRC19c791c2018-03-08 10:55:20 -0600850 if (argc > minArg) {
851 for (i = minArg; i < argc; i++) {
852 if (!strcasecmp(argv[i], "-tile")) {
853 doTile = 1; xformOpt |= TJXOPT_CROP;
854 } else if (!strcasecmp(argv[i], "-fastupsample")) {
855 printf("Using fast upsampling code\n\n");
856 flags |= TJFLAG_FASTUPSAMPLE;
857 } else if (!strcasecmp(argv[i], "-fastdct")) {
858 printf("Using fastest DCT/IDCT algorithm\n\n");
859 flags |= TJFLAG_FASTDCT;
860 } else if (!strcasecmp(argv[i], "-accuratedct")) {
861 printf("Using most accurate DCT/IDCT algorithm\n\n");
862 flags |= TJFLAG_ACCURATEDCT;
863 } else if (!strcasecmp(argv[i], "-progressive")) {
864 printf("Using progressive entropy coding\n\n");
865 flags |= TJFLAG_PROGRESSIVE;
866 } else if (!strcasecmp(argv[i], "-rgb"))
867 pf = TJPF_RGB;
868 else if (!strcasecmp(argv[i], "-rgbx"))
869 pf = TJPF_RGBX;
870 else if (!strcasecmp(argv[i], "-bgr"))
871 pf = TJPF_BGR;
872 else if (!strcasecmp(argv[i], "-bgrx"))
873 pf = TJPF_BGRX;
874 else if (!strcasecmp(argv[i], "-xbgr"))
875 pf = TJPF_XBGR;
876 else if (!strcasecmp(argv[i], "-xrgb"))
877 pf = TJPF_XRGB;
878 else if (!strcasecmp(argv[i], "-cmyk"))
879 pf = TJPF_CMYK;
880 else if (!strcasecmp(argv[i], "-bottomup"))
881 flags |= TJFLAG_BOTTOMUP;
882 else if (!strcasecmp(argv[i], "-quiet"))
883 quiet = 1;
884 else if (!strcasecmp(argv[i], "-qq"))
885 quiet = 2;
886 else if (!strcasecmp(argv[i], "-scale") && i < argc - 1) {
887 int temp1 = 0, temp2 = 0, match = 0;
DRCb8b359a2011-05-25 03:54:56 +0000888
DRC19c791c2018-03-08 10:55:20 -0600889 if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) == 2) {
890 for (j = 0; j < nsf; j++) {
891 if ((double)temp1 / (double)temp2 ==
892 (double)scalingFactors[j].num /
893 (double)scalingFactors[j].denom) {
894 sf = scalingFactors[j];
895 match = 1; break;
896 }
897 }
898 if (!match) usage(argv[0]);
899 } else usage(argv[0]);
900 } else if (!strcasecmp(argv[i], "-hflip"))
901 xformOp = TJXOP_HFLIP;
902 else if (!strcasecmp(argv[i], "-vflip"))
903 xformOp = TJXOP_VFLIP;
904 else if (!strcasecmp(argv[i], "-transpose"))
905 xformOp = TJXOP_TRANSPOSE;
906 else if (!strcasecmp(argv[i], "-transverse"))
907 xformOp = TJXOP_TRANSVERSE;
908 else if (!strcasecmp(argv[i], "-rot90"))
909 xformOp = TJXOP_ROT90;
910 else if (!strcasecmp(argv[i], "-rot180"))
911 xformOp = TJXOP_ROT180;
912 else if (!strcasecmp(argv[i], "-rot270"))
913 xformOp = TJXOP_ROT270;
914 else if (!strcasecmp(argv[i], "-grayscale"))
915 xformOpt |= TJXOPT_GRAY;
916 else if (!strcasecmp(argv[i], "-custom"))
917 customFilter = dummyDCTFilter;
918 else if (!strcasecmp(argv[i], "-nooutput"))
919 xformOpt |= TJXOPT_NOOUTPUT;
920 else if (!strcasecmp(argv[i], "-copynone"))
921 xformOpt |= TJXOPT_COPYNONE;
922 else if (!strcasecmp(argv[i], "-benchtime") && i < argc - 1) {
DRC6399d0a2019-04-23 14:10:04 -0500923 double tempd = atof(argv[++i]);
DRCb8b359a2011-05-25 03:54:56 +0000924
DRC6399d0a2019-04-23 14:10:04 -0500925 if (tempd > 0.0) benchTime = tempd;
DRC19c791c2018-03-08 10:55:20 -0600926 else usage(argv[0]);
927 } else if (!strcasecmp(argv[i], "-warmup") && i < argc - 1) {
DRC6399d0a2019-04-23 14:10:04 -0500928 double tempd = atof(argv[++i]);
DRC684ace12014-08-22 03:04:06 +0000929
DRC6399d0a2019-04-23 14:10:04 -0500930 if (tempd >= 0.0) warmup = tempd;
DRC19c791c2018-03-08 10:55:20 -0600931 else usage(argv[0]);
932 printf("Warmup time = %.1f seconds\n\n", warmup);
933 } else if (!strcasecmp(argv[i], "-alloc"))
934 flags &= (~TJFLAG_NOREALLOC);
935 else if (!strcasecmp(argv[i], "-bmp"))
936 ext = "bmp";
937 else if (!strcasecmp(argv[i], "-yuv")) {
938 printf("Testing YUV planar encoding/decoding\n\n");
939 doYUV = 1;
940 } else if (!strcasecmp(argv[i], "-yuvpad") && i < argc - 1) {
DRC6399d0a2019-04-23 14:10:04 -0500941 int tempi = atoi(argv[++i]);
DRCb8b359a2011-05-25 03:54:56 +0000942
DRC6399d0a2019-04-23 14:10:04 -0500943 if (tempi >= 1) yuvPad = tempi;
DRC19c791c2018-03-08 10:55:20 -0600944 } else if (!strcasecmp(argv[i], "-subsamp") && i < argc - 1) {
945 i++;
946 if (toupper(argv[i][0]) == 'G') subsamp = TJSAMP_GRAY;
947 else {
DRC6399d0a2019-04-23 14:10:04 -0500948 int tempi = atoi(argv[i]);
DRCb8b359a2011-05-25 03:54:56 +0000949
DRC6399d0a2019-04-23 14:10:04 -0500950 switch (tempi) {
DRC19c791c2018-03-08 10:55:20 -0600951 case 444: subsamp = TJSAMP_444; break;
952 case 422: subsamp = TJSAMP_422; break;
953 case 440: subsamp = TJSAMP_440; break;
954 case 420: subsamp = TJSAMP_420; break;
955 case 411: subsamp = TJSAMP_411; break;
956 }
957 }
958 } else if (!strcasecmp(argv[i], "-componly"))
959 compOnly = 1;
960 else if (!strcasecmp(argv[i], "-nowrite"))
961 doWrite = 0;
962 else if (!strcasecmp(argv[i], "-stoponwarning"))
963 flags |= TJFLAG_STOPONWARNING;
964 else usage(argv[0]);
965 }
966 }
DRCb8b359a2011-05-25 03:54:56 +0000967
DRC19c791c2018-03-08 10:55:20 -0600968 if ((sf.num != 1 || sf.denom != 1) && doTile) {
969 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
970 printf("work when scaled decompression is enabled.\n");
971 doTile = 0;
972 }
973
974 if ((flags & TJFLAG_NOREALLOC) == 0 && doTile) {
975 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
976 printf("work when dynamic JPEG buffer allocation is enabled.\n\n");
977 doTile = 0;
978 }
979
980 if (!decompOnly) {
981 if ((srcBuf = tjLoadImage(argv[1], &w, 1, &h, &pf, flags)) == NULL)
DRCbce58f42019-04-12 07:49:35 -0500982 THROW_TJG("loading bitmap");
DRC19c791c2018-03-08 10:55:20 -0600983 temp = strrchr(argv[1], '.');
984 if (temp != NULL) *temp = '\0';
985 }
986
987 if (quiet == 1 && !decompOnly) {
988 printf("All performance values in Mpixels/sec\n\n");
989 printf("Bitmap JPEG JPEG %s %s ",
990 doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
991 if (doYUV) printf("Encode ");
992 printf("Comp Comp Decomp ");
993 if (doYUV) printf("Decode");
994 printf("\n");
995 printf("Format Subsamp Qual Width Height ");
996 if (doYUV) printf("Perf ");
997 printf("Perf Ratio Perf ");
998 if (doYUV) printf("Perf");
999 printf("\n\n");
1000 }
1001
1002 if (decompOnly) {
1003 decompTest(argv[1]);
1004 printf("\n");
1005 goto bailout;
1006 }
1007 if (subsamp >= 0 && subsamp < TJ_NUMSAMP) {
1008 for (i = maxQual; i >= minQual; i--)
1009 fullTest(srcBuf, w, h, subsamp, i, argv[1]);
1010 printf("\n");
1011 } else {
1012 if (pf != TJPF_CMYK) {
1013 for (i = maxQual; i >= minQual; i--)
1014 fullTest(srcBuf, w, h, TJSAMP_GRAY, i, argv[1]);
1015 printf("\n");
1016 }
1017 for (i = maxQual; i >= minQual; i--)
1018 fullTest(srcBuf, w, h, TJSAMP_420, i, argv[1]);
1019 printf("\n");
1020 for (i = maxQual; i >= minQual; i--)
1021 fullTest(srcBuf, w, h, TJSAMP_422, i, argv[1]);
1022 printf("\n");
1023 for (i = maxQual; i >= minQual; i--)
1024 fullTest(srcBuf, w, h, TJSAMP_444, i, argv[1]);
1025 printf("\n");
1026 }
1027
1028bailout:
1029 if (srcBuf) tjFree(srcBuf);
1030 return retval;
DRCb8b359a2011-05-25 03:54:56 +00001031}