2 * Copyright (C)2009-2019, 2021-2022 D. R. Commander. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
30 #define _CRT_SECURE_NO_DEPRECATE
42 #include "./turbojpeg.h"
45 #define THROW(op, err) { \
46 printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
47 retval = -1; goto bailout; \
49 #define THROW_UNIX(m) THROW(m, strerror(errno))
51 char tjErrorStr[JMSG_LENGTH_MAX] = "\0", tjErrorMsg[JMSG_LENGTH_MAX] = "\0";
52 int tjErrorLine = -1, tjErrorCode = -1;
54 #define THROW_TJG(m) { \
55 printf("ERROR in line %d while %s:\n%s\n", __LINE__, m, \
56 tjGetErrorStr2(NULL)); \
57 retval = -1; goto bailout; \
60 #define THROW_TJ(m) { \
61 int _tjErrorCode = tjGetErrorCode(handle); \
62 char *_tjErrorStr = tjGetErrorStr2(handle); \
64 if (!(flags & TJFLAG_STOPONWARNING) && _tjErrorCode == TJERR_WARNING) { \
65 if (strncmp(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX) || \
66 strncmp(tjErrorMsg, m, JMSG_LENGTH_MAX) || \
67 tjErrorCode != _tjErrorCode || tjErrorLine != __LINE__) { \
68 strncpy(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX); \
69 tjErrorStr[JMSG_LENGTH_MAX - 1] = '\0'; \
70 strncpy(tjErrorMsg, m, JMSG_LENGTH_MAX); \
71 tjErrorMsg[JMSG_LENGTH_MAX - 1] = '\0'; \
72 tjErrorCode = _tjErrorCode; \
73 tjErrorLine = __LINE__; \
74 printf("WARNING in line %d while %s:\n%s\n", __LINE__, m, _tjErrorStr); \
77 printf("%s in line %d while %s:\n%s\n", \
78 _tjErrorCode == TJERR_WARNING ? "WARNING" : "ERROR", __LINE__, m, \
80 retval = -1; goto bailout; \
84 int flags = TJFLAG_NOREALLOC, compOnly = 0, decompOnly = 0, doYUV = 0,
85 quiet = 0, doTile = 0, pf = TJPF_BGR, yuvPad = 1, doWrite = 1;
87 const char *pixFormatStr[TJ_NUMPF] = {
88 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
90 const char *subNameLong[TJ_NUMSAMP] = {
91 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
93 const char *csName[TJ_NUMCS] = {
94 "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
96 const char *subName[TJ_NUMSAMP] = {
97 "444", "422", "420", "GRAY", "440", "411"
99 tjscalingfactor *scalingFactors = NULL, sf = { 1, 1 };
100 int nsf = 0, xformOp = TJXOP_NONE, xformOpt = 0;
101 int (*customFilter) (short *, tjregion, tjregion, int, int, tjtransform *);
102 double benchTime = 5.0, warmup = 1.0;
105 static char *formatName(int subsamp, int cs, char *buf)
107 if (cs == TJCS_YCbCr)
108 return (char *)subNameLong[subsamp];
109 else if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
110 SNPRINTF(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
113 return (char *)csName[cs];
117 static char *sigfig(double val, int figs, char *buf, int len)
120 int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));
122 if (digitsAfterDecimal < 1)
123 SNPRINTF(format, 80, "%%.0f");
125 SNPRINTF(format, 80, "%%.%df", digitsAfterDecimal);
126 SNPRINTF(buf, len, format, val);
131 /* Custom DCT filter which produces a negative of the image */
132 static int dummyDCTFilter(short *coeffs, tjregion arrayRegion,
133 tjregion planeRegion, int componentIndex,
134 int transformIndex, tjtransform *transform)
138 for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
139 coeffs[i] = -coeffs[i];
144 /* Decompression test */
145 static int decomp(unsigned char *srcBuf, unsigned char **jpegBuf,
146 unsigned long *jpegSize, unsigned char *dstBuf, int w, int h,
147 int subsamp, int jpegQual, char *fileName, int tilew,
150 char tempStr[1024], sizeStr[24] = "\0", qualStr[13] = "\0", *ptr;
152 tjhandle handle = NULL;
153 int row, col, iter = 0, dstBufAlloc = 0, retval = 0;
154 double elapsed, elapsedDecode;
155 int ps = tjPixelSize[pf];
156 int scaledw = TJSCALED(w, sf);
157 int scaledh = TJSCALED(h, sf);
158 int pitch = scaledw * ps;
159 int ntilesw = (w + tilew - 1) / tilew, ntilesh = (h + tileh - 1) / tileh;
160 unsigned char *dstPtr, *dstPtr2, *yuvBuf = NULL;
163 SNPRINTF(qualStr, 13, "_Q%d", jpegQual);
167 if ((handle = tjInitDecompress()) == NULL)
168 THROW_TJ("executing tjInitDecompress()");
170 if (dstBuf == NULL) {
171 if ((unsigned long long)pitch * (unsigned long long)scaledh >
172 (unsigned long long)((size_t)-1))
173 THROW("allocating destination buffer", "Image is too large");
174 if ((dstBuf = (unsigned char *)malloc((size_t)pitch * scaledh)) == NULL)
175 THROW_UNIX("allocating destination buffer");
178 /* Set the destination buffer to gray so we know whether the decompressor
179 attempted to write to it */
180 memset(dstBuf, 127, (size_t)pitch * scaledh);
183 int width = doTile ? tilew : scaledw;
184 int height = doTile ? tileh : scaledh;
185 unsigned long yuvSize = tjBufSizeYUV2(width, yuvPad, height, subsamp);
187 if (yuvSize == (unsigned long)-1)
188 THROW_TJ("allocating YUV buffer");
189 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
190 THROW_UNIX("allocating YUV buffer");
191 memset(yuvBuf, 127, yuvSize);
196 elapsed = elapsedDecode = 0.;
199 double start = getTime();
201 for (row = 0, dstPtr = dstBuf; row < ntilesh;
202 row++, dstPtr += (size_t)pitch * tileh) {
203 for (col = 0, dstPtr2 = dstPtr; col < ntilesw;
204 col++, tile++, dstPtr2 += ps * tilew) {
205 int width = doTile ? min(tilew, w - col * tilew) : scaledw;
206 int height = doTile ? min(tileh, h - row * tileh) : scaledh;
211 if (tjDecompressToYUV2(handle, jpegBuf[tile], jpegSize[tile], yuvBuf,
212 width, yuvPad, height, flags) == -1)
213 THROW_TJ("executing tjDecompressToYUV2()");
214 startDecode = getTime();
215 if (tjDecodeYUV(handle, yuvBuf, yuvPad, subsamp, dstPtr2, width,
216 pitch, height, pf, flags) == -1)
217 THROW_TJ("executing tjDecodeYUV()");
218 if (iter >= 0) elapsedDecode += getTime() - startDecode;
219 } else if (tjDecompress2(handle, jpegBuf[tile], jpegSize[tile],
220 dstPtr2, width, pitch, height, pf,
222 THROW_TJ("executing tjDecompress2()");
225 elapsed += getTime() - start;
228 if (elapsed >= benchTime) break;
229 } else if (elapsed >= warmup) {
231 elapsed = elapsedDecode = 0.;
234 if (doYUV) elapsed -= elapsedDecode;
236 if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
241 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
243 quiet == 2 ? "\n" : " ");
246 sigfig((double)(w * h) / 1000000. * (double)iter / elapsedDecode,
248 else if (quiet != 2) printf("\n");
250 printf("%s --> Frame rate: %f fps\n",
251 doYUV ? "Decomp to YUV" : "Decompress ", (double)iter / elapsed);
252 printf(" Throughput: %f Megapixels/sec\n",
253 (double)(w * h) / 1000000. * (double)iter / elapsed);
255 printf("YUV Decode --> Frame rate: %f fps\n",
256 (double)iter / elapsedDecode);
257 printf(" Throughput: %f Megapixels/sec\n",
258 (double)(w * h) / 1000000. * (double)iter / elapsedDecode);
262 if (!doWrite) goto bailout;
264 if (sf.num != 1 || sf.denom != 1)
265 SNPRINTF(sizeStr, 24, "%d_%d", sf.num, sf.denom);
266 else if (tilew != w || tileh != h)
267 SNPRINTF(sizeStr, 24, "%dx%d", tilew, tileh);
268 else SNPRINTF(sizeStr, 24, "full");
270 SNPRINTF(tempStr, 1024, "%s_%s.%s", fileName, sizeStr, ext);
272 SNPRINTF(tempStr, 1024, "%s_%s%s_%s.%s", fileName, subName[subsamp],
273 qualStr, sizeStr, ext);
275 if (tjSaveImage(tempStr, dstBuf, scaledw, 0, scaledh, pf, flags) == -1)
276 THROW_TJG("saving bitmap");
277 ptr = strrchr(tempStr, '.');
278 SNPRINTF(ptr, 1024 - (ptr - tempStr), "-err.%s", ext);
279 if (srcBuf && sf.num == 1 && sf.denom == 1) {
280 if (!quiet) printf("Compression error written to %s.\n", tempStr);
281 if (subsamp == TJ_GRAYSCALE) {
282 unsigned long index, index2;
284 for (row = 0, index = 0; row < h; row++, index += pitch) {
285 for (col = 0, index2 = index; col < w; col++, index2 += ps) {
286 unsigned long rindex = index2 + tjRedOffset[pf];
287 unsigned long gindex = index2 + tjGreenOffset[pf];
288 unsigned long bindex = index2 + tjBlueOffset[pf];
289 int y = (int)((double)srcBuf[rindex] * 0.299 +
290 (double)srcBuf[gindex] * 0.587 +
291 (double)srcBuf[bindex] * 0.114 + 0.5);
293 if (y > 255) y = 255;
295 dstBuf[rindex] = (unsigned char)abs(dstBuf[rindex] - y);
296 dstBuf[gindex] = (unsigned char)abs(dstBuf[gindex] - y);
297 dstBuf[bindex] = (unsigned char)abs(dstBuf[bindex] - y);
301 for (row = 0; row < h; row++)
302 for (col = 0; col < w * ps; col++)
303 dstBuf[pitch * row + col] =
304 (unsigned char)abs(dstBuf[pitch * row + col] -
305 srcBuf[pitch * row + col]);
307 if (tjSaveImage(tempStr, dstBuf, w, 0, h, pf, flags) == -1)
308 THROW_TJG("saving bitmap");
312 if (file) fclose(file);
313 if (handle) tjDestroy(handle);
314 if (dstBufAlloc) free(dstBuf);
320 static int fullTest(unsigned char *srcBuf, int w, int h, int subsamp,
321 int jpegQual, char *fileName)
323 char tempStr[1024], tempStr2[80];
325 tjhandle handle = NULL;
326 unsigned char **jpegBuf = NULL, *yuvBuf = NULL, *tmpBuf = NULL, *srcPtr,
328 double start, elapsed, elapsedEncode;
329 int totalJpegSize = 0, row, col, i, tilew = w, tileh = h, retval = 0;
331 unsigned long *jpegSize = NULL, yuvSize = 0;
332 int ps = tjPixelSize[pf];
333 int ntilesw = 1, ntilesh = 1, pitch = w * ps;
334 const char *pfStr = pixFormatStr[pf];
336 if ((unsigned long long)pitch * (unsigned long long)h >
337 (unsigned long long)((size_t)-1))
338 THROW("allocating temporary image buffer", "Image is too large");
339 if ((tmpBuf = (unsigned char *)malloc((size_t)pitch * h)) == NULL)
340 THROW_UNIX("allocating temporary image buffer");
343 printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pfStr,
344 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down",
345 subNameLong[subsamp], jpegQual);
347 for (tilew = doTile ? 8 : w, tileh = doTile ? 8 : h; ;
348 tilew *= 2, tileh *= 2) {
349 if (tilew > w) tilew = w;
350 if (tileh > h) tileh = h;
351 ntilesw = (w + tilew - 1) / tilew;
352 ntilesh = (h + tileh - 1) / tileh;
354 if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
355 ntilesw * ntilesh)) == NULL)
356 THROW_UNIX("allocating JPEG tile array");
357 memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
358 if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
359 ntilesw * ntilesh)) == NULL)
360 THROW_UNIX("allocating JPEG size array");
361 memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
363 if ((flags & TJFLAG_NOREALLOC) != 0)
364 for (i = 0; i < ntilesw * ntilesh; i++) {
365 if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
366 THROW("getting buffer size", "Image is too large");
367 if ((jpegBuf[i] = (unsigned char *)
368 tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
369 THROW_UNIX("allocating JPEG tiles");
372 /* Compression test */
374 printf("%-4s (%s) %-5s %-3d ", pfStr,
375 (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", subNameLong[subsamp],
377 for (i = 0; i < h; i++)
378 memcpy(&tmpBuf[pitch * i], &srcBuf[w * ps * i], w * ps);
379 if ((handle = tjInitCompress()) == NULL)
380 THROW_TJ("executing tjInitCompress()");
383 yuvSize = tjBufSizeYUV2(tilew, yuvPad, tileh, subsamp);
384 if (yuvSize == (unsigned long)-1)
385 THROW_TJ("allocating YUV buffer");
386 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
387 THROW_UNIX("allocating YUV buffer");
388 memset(yuvBuf, 127, yuvSize);
393 elapsed = elapsedEncode = 0.;
399 for (row = 0, srcPtr = srcBuf; row < ntilesh;
400 row++, srcPtr += pitch * tileh) {
401 for (col = 0, srcPtr2 = srcPtr; col < ntilesw;
402 col++, tile++, srcPtr2 += ps * tilew) {
403 int width = min(tilew, w - col * tilew);
404 int height = min(tileh, h - row * tileh);
407 double startEncode = getTime();
409 if (tjEncodeYUV3(handle, srcPtr2, width, pitch, height, pf, yuvBuf,
410 yuvPad, subsamp, flags) == -1)
411 THROW_TJ("executing tjEncodeYUV3()");
412 if (iter >= 0) elapsedEncode += getTime() - startEncode;
413 if (tjCompressFromYUV(handle, yuvBuf, width, yuvPad, height,
414 subsamp, &jpegBuf[tile], &jpegSize[tile],
415 jpegQual, flags) == -1)
416 THROW_TJ("executing tjCompressFromYUV()");
418 if (tjCompress2(handle, srcPtr2, width, pitch, height, pf,
419 &jpegBuf[tile], &jpegSize[tile], subsamp, jpegQual,
421 THROW_TJ("executing tjCompress2()");
423 totalJpegSize += jpegSize[tile];
426 elapsed += getTime() - start;
429 if (elapsed >= benchTime) break;
430 } else if (elapsed >= warmup) {
432 elapsed = elapsedEncode = 0.;
435 if (doYUV) elapsed -= elapsedEncode;
437 if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
440 if (quiet == 1) printf("%-5d %-5d ", tilew, tileh);
444 sigfig((double)(w * h) / 1000000. *
445 (double)iter / elapsedEncode, 4, tempStr, 1024),
446 quiet == 2 ? "\n" : " ");
448 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
450 quiet == 2 ? "\n" : " ");
452 sigfig((double)(w * h * ps) / (double)totalJpegSize, 4, tempStr2,
454 quiet == 2 ? "\n" : " ");
456 printf("\n%s size: %d x %d\n", doTile ? "Tile" : "Image", tilew, tileh);
458 printf("Encode YUV --> Frame rate: %f fps\n",
459 (double)iter / elapsedEncode);
460 printf(" Output image size: %lu bytes\n", yuvSize);
461 printf(" Compression ratio: %f:1\n",
462 (double)(w * h * ps) / (double)yuvSize);
463 printf(" Throughput: %f Megapixels/sec\n",
464 (double)(w * h) / 1000000. * (double)iter / elapsedEncode);
465 printf(" Output bit stream: %f Megabits/sec\n",
466 (double)yuvSize * 8. / 1000000. * (double)iter / elapsedEncode);
468 printf("%s --> Frame rate: %f fps\n",
469 doYUV ? "Comp from YUV" : "Compress ",
470 (double)iter / elapsed);
471 printf(" Output image size: %d bytes\n",
473 printf(" Compression ratio: %f:1\n",
474 (double)(w * h * ps) / (double)totalJpegSize);
475 printf(" Throughput: %f Megapixels/sec\n",
476 (double)(w * h) / 1000000. * (double)iter / elapsed);
477 printf(" Output bit stream: %f Megabits/sec\n",
478 (double)totalJpegSize * 8. / 1000000. * (double)iter / elapsed);
480 if (tilew == w && tileh == h && doWrite) {
481 SNPRINTF(tempStr, 1024, "%s_%s_Q%d.jpg", fileName, subName[subsamp],
483 if ((file = fopen(tempStr, "wb")) == NULL)
484 THROW_UNIX("opening reference image");
485 if (fwrite(jpegBuf[0], jpegSize[0], 1, file) != 1)
486 THROW_UNIX("writing reference image");
487 fclose(file); file = NULL;
488 if (!quiet) printf("Reference image written to %s\n", tempStr);
491 /* Decompression test */
493 if (decomp(srcBuf, jpegBuf, jpegSize, tmpBuf, w, h, subsamp, jpegQual,
494 fileName, tilew, tileh) == -1)
496 } else if (quiet == 1) printf("N/A\n");
498 for (i = 0; i < ntilesw * ntilesh; i++) {
502 free(jpegBuf); jpegBuf = NULL;
503 free(jpegSize); jpegSize = NULL;
505 free(yuvBuf); yuvBuf = NULL;
508 if (tilew == w && tileh == h) break;
512 if (file) fclose(file);
514 for (i = 0; i < ntilesw * ntilesh; i++)
521 if (handle) tjDestroy(handle);
526 static int decompTest(char *fileName)
529 tjhandle handle = NULL;
530 unsigned char **jpegBuf = NULL, *srcBuf = NULL;
531 unsigned long *jpegSize = NULL, srcSize, totalJpegSize;
532 tjtransform *t = NULL;
533 double start, elapsed;
534 int ps = tjPixelSize[pf], tile, row, col, i, iter, retval = 0, decompsrc = 0;
535 char *temp = NULL, tempStr[80], tempStr2[80];
537 int w = 0, h = 0, tilew, tileh, ntilesw = 1, ntilesh = 1, subsamp = -1,
539 /* Transformed image */
540 int tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;
542 if ((file = fopen(fileName, "rb")) == NULL)
543 THROW_UNIX("opening file");
544 if (fseek(file, 0, SEEK_END) < 0 ||
545 (srcSize = ftell(file)) == (unsigned long)-1)
546 THROW_UNIX("determining file size");
547 if ((srcBuf = (unsigned char *)malloc(srcSize)) == NULL)
548 THROW_UNIX("allocating memory");
549 if (fseek(file, 0, SEEK_SET) < 0)
550 THROW_UNIX("setting file position");
551 if (fread(srcBuf, srcSize, 1, file) < 1)
552 THROW_UNIX("reading JPEG data");
553 fclose(file); file = NULL;
555 temp = strrchr(fileName, '.');
556 if (temp != NULL) *temp = '\0';
558 if ((handle = tjInitTransform()) == NULL)
559 THROW_TJ("executing tjInitTransform()");
560 if (tjDecompressHeader3(handle, srcBuf, srcSize, &w, &h, &subsamp,
562 THROW_TJ("executing tjDecompressHeader3()");
564 THROW("reading JPEG header", "Invalid image dimensions");
565 if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
566 pf = TJPF_CMYK; ps = tjPixelSize[pf];
570 printf("All performance values in Mpixels/sec\n\n");
571 printf("Bitmap JPEG JPEG %s %s Xform Comp Decomp ",
572 doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
573 if (doYUV) printf("Decode");
575 printf("Format CS Subsamp Width Height Perf Ratio Perf ");
576 if (doYUV) printf("Perf");
579 printf(">>>>> JPEG %s --> %s (%s) <<<<<\n",
580 formatName(subsamp, cs, tempStr), pixFormatStr[pf],
581 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down");
583 for (tilew = doTile ? 16 : w, tileh = doTile ? 16 : h; ;
584 tilew *= 2, tileh *= 2) {
585 if (tilew > w) tilew = w;
586 if (tileh > h) tileh = h;
587 ntilesw = (w + tilew - 1) / tilew;
588 ntilesh = (h + tileh - 1) / tileh;
590 if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
591 ntilesw * ntilesh)) == NULL)
592 THROW_UNIX("allocating JPEG tile array");
593 memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
594 if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
595 ntilesw * ntilesh)) == NULL)
596 THROW_UNIX("allocating JPEG size array");
597 memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
599 if ((flags & TJFLAG_NOREALLOC) != 0 &&
600 (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter))
601 for (i = 0; i < ntilesw * ntilesh; i++) {
602 if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
603 THROW("getting buffer size", "Image is too large");
604 if ((jpegBuf[i] = (unsigned char *)
605 tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
606 THROW_UNIX("allocating JPEG tiles");
609 tw = w; th = h; ttilew = tilew; ttileh = tileh;
611 printf("\n%s size: %d x %d", doTile ? "Tile" : "Image", ttilew, ttileh);
612 if (sf.num != 1 || sf.denom != 1)
613 printf(" --> %d x %d", TJSCALED(tw, sf), TJSCALED(th, sf));
615 } else if (quiet == 1) {
616 printf("%-4s (%s) %-5s %-5s ", pixFormatStr[pf],
617 (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", csName[cs],
618 subNameLong[subsamp]);
619 printf("%-5d %-5d ", tilew, tileh);
623 if (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter) {
624 if ((t = (tjtransform *)malloc(sizeof(tjtransform) * ntilesw *
626 THROW_UNIX("allocating image transform array");
628 if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
629 xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
630 tw = h; th = w; ttilew = tileh; ttileh = tilew;
633 if (xformOpt & TJXOPT_GRAY) tsubsamp = TJ_GRAYSCALE;
634 if (xformOp == TJXOP_HFLIP || xformOp == TJXOP_ROT180)
635 tw = tw - (tw % tjMCUWidth[tsubsamp]);
636 if (xformOp == TJXOP_VFLIP || xformOp == TJXOP_ROT180)
637 th = th - (th % tjMCUHeight[tsubsamp]);
638 if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT90)
639 tw = tw - (tw % tjMCUHeight[tsubsamp]);
640 if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT270)
641 th = th - (th % tjMCUWidth[tsubsamp]);
642 tntilesw = (tw + ttilew - 1) / ttilew;
643 tntilesh = (th + ttileh - 1) / ttileh;
645 if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
646 xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
647 if (tsubsamp == TJSAMP_422) tsubsamp = TJSAMP_440;
648 else if (tsubsamp == TJSAMP_440) tsubsamp = TJSAMP_422;
651 for (row = 0, tile = 0; row < tntilesh; row++) {
652 for (col = 0; col < tntilesw; col++, tile++) {
653 t[tile].r.w = min(ttilew, tw - col * ttilew);
654 t[tile].r.h = min(ttileh, th - row * ttileh);
655 t[tile].r.x = col * ttilew;
656 t[tile].r.y = row * ttileh;
657 t[tile].op = xformOp;
658 t[tile].options = xformOpt | TJXOPT_TRIM;
659 t[tile].customFilter = customFilter;
660 if (t[tile].options & TJXOPT_NOOUTPUT && jpegBuf[tile]) {
661 tjFree(jpegBuf[tile]); jpegBuf[tile] = NULL;
670 if (tjTransform(handle, srcBuf, srcSize, tntilesw * tntilesh, jpegBuf,
671 jpegSize, t, flags) == -1)
672 THROW_TJ("executing tjTransform()");
673 elapsed += getTime() - start;
676 if (elapsed >= benchTime) break;
677 } else if (elapsed >= warmup) {
685 for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
686 totalJpegSize += jpegSize[tile];
689 printf("%-6s%s%-6s%s",
690 sigfig((double)(w * h) / 1000000. / elapsed, 4, tempStr, 80),
691 quiet == 2 ? "\n" : " ",
692 sigfig((double)(w * h * ps) / (double)totalJpegSize, 4,
694 quiet == 2 ? "\n" : " ");
696 printf("Transform --> Frame rate: %f fps\n",
698 printf(" Output image size: %lu bytes\n",
700 printf(" Compression ratio: %f:1\n",
701 (double)(w * h * ps) / (double)totalJpegSize);
702 printf(" Throughput: %f Megapixels/sec\n",
703 (double)(w * h) / 1000000. / elapsed);
704 printf(" Output bit stream: %f Megabits/sec\n",
705 (double)totalJpegSize * 8. / 1000000. / elapsed);
708 if (quiet == 1) printf("N/A N/A ");
714 if (w == tilew) ttilew = tw;
715 if (h == tileh) ttileh = th;
716 if (!(xformOpt & TJXOPT_NOOUTPUT)) {
717 if (decomp(NULL, decompsrc ? &srcBuf : jpegBuf,
718 decompsrc ? &srcSize : jpegSize, NULL, tw, th, tsubsamp, 0,
719 fileName, ttilew, ttileh) == -1)
721 } else if (quiet == 1) printf("N/A\n");
723 for (i = 0; i < ntilesw * ntilesh; i++) {
727 free(jpegBuf); jpegBuf = NULL;
728 free(jpegSize); jpegSize = NULL;
730 if (tilew == w && tileh == h) break;
734 if (file) fclose(file);
736 for (i = 0; i < ntilesw * ntilesh; i++)
743 if (handle) { tjDestroy(handle); handle = NULL; }
748 static void usage(char *progName)
752 printf("USAGE: %s\n", progName);
753 printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
754 printf(" %s\n", progName);
755 printf(" <Inputfile (JPG)> [options]\n\n");
756 printf("Options:\n\n");
757 printf("-alloc = Dynamically allocate JPEG image buffers\n");
758 printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
759 printf("-bottomup = Test bottom-up compression/decompression\n");
760 printf("-tile = Test performance of the codec when the image is encoded as separate\n");
761 printf(" tiles of varying sizes.\n");
762 printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
763 printf(" Test the specified color conversion path in the codec (default = BGR)\n");
764 printf("-cmyk = Indirectly test YCCK JPEG compression/decompression (the source\n");
765 printf(" and destination bitmaps are still RGB. The conversion is done\n");
766 printf(" internally prior to compression or after decompression.)\n");
767 printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
768 printf(" the underlying codec\n");
769 printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
771 printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
772 printf(" underlying codec\n");
773 printf("-progressive = Use progressive entropy coding in JPEG images generated by\n");
774 printf(" compression and transform operations.\n");
775 printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
776 printf(" of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");
777 printf(" GRAY). The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
778 printf(" sequence.\n");
779 printf("-quiet = Output results in tabular rather than verbose format\n");
780 printf("-yuv = Test YUV encoding/decoding functions\n");
781 printf("-yuvpad <p> = If testing YUV encoding/decoding, this specifies the number of\n");
782 printf(" bytes to which each row of each plane in the intermediate YUV image is\n");
783 printf(" padded (default = 1)\n");
784 printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
785 printf(" factor of M/N (M/N = ");
786 for (i = 0; i < nsf; i++) {
787 printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
788 if (nsf == 2 && i != nsf - 1) printf(" or ");
790 if (i != nsf - 1) printf(", ");
791 if (i == nsf - 2) printf("or ");
793 if (i % 8 == 0 && i != 0) printf("\n ");
796 printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
797 printf(" Perform the corresponding lossless transform prior to\n");
798 printf(" decompression (these options are mutually exclusive)\n");
799 printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
800 printf(" test (can be combined with the other transforms above)\n");
801 printf("-copynone = Do not copy any extra markers (including EXIF and ICC profile data)\n");
802 printf(" when transforming the image.\n");
803 printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");
804 printf("-warmup <t> = Run each benchmark for <t> seconds (default = 1.0) prior to\n");
805 printf(" starting the timer, in order to prime the caches and thus improve the\n");
806 printf(" consistency of the results.\n");
807 printf("-componly = Stop after running compression tests. Do not test decompression.\n");
808 printf("-nowrite = Do not write reference or output images (improves consistency of\n");
809 printf(" performance measurements.)\n");
810 printf("-limitscans = Refuse to decompress or transform progressive JPEG images that\n");
811 printf(" have an unreasonably large number of scans\n");
812 printf("-stoponwarning = Immediately discontinue the current\n");
813 printf(" compression/decompression/transform operation if the underlying codec\n");
814 printf(" throws a warning (non-fatal error)\n\n");
815 printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
816 printf("test will be performed for all quality values in the range.\n\n");
821 int main(int argc, char *argv[])
823 unsigned char *srcBuf = NULL;
824 int w = 0, h = 0, i, j, minQual = -1, maxQual = -1;
826 int minArg = 2, retval = 0, subsamp = -1;
828 if ((scalingFactors = tjGetScalingFactors(&nsf)) == NULL || nsf == 0)
829 THROW("executing tjGetScalingFactors()", tjGetErrorStr());
831 if (argc < minArg) usage(argv[0]);
833 temp = strrchr(argv[1], '.');
835 if (!strcasecmp(temp, ".bmp")) ext = "bmp";
836 if (!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg"))
844 if (argc < minArg) usage(argv[0]);
845 if ((minQual = atoi(argv[2])) < 1 || minQual > 100) {
846 puts("ERROR: Quality must be between 1 and 100.");
849 if ((temp = strchr(argv[2], '-')) != NULL && strlen(temp) > 1 &&
850 sscanf(&temp[1], "%d", &maxQual) == 1 && maxQual > minQual &&
851 maxQual >= 1 && maxQual <= 100) {}
852 else maxQual = minQual;
856 for (i = minArg; i < argc; i++) {
857 if (!strcasecmp(argv[i], "-tile")) {
858 doTile = 1; xformOpt |= TJXOPT_CROP;
859 } else if (!strcasecmp(argv[i], "-fastupsample")) {
860 printf("Using fast upsampling code\n\n");
861 flags |= TJFLAG_FASTUPSAMPLE;
862 } else if (!strcasecmp(argv[i], "-fastdct")) {
863 printf("Using fastest DCT/IDCT algorithm\n\n");
864 flags |= TJFLAG_FASTDCT;
865 } else if (!strcasecmp(argv[i], "-accuratedct")) {
866 printf("Using most accurate DCT/IDCT algorithm\n\n");
867 flags |= TJFLAG_ACCURATEDCT;
868 } else if (!strcasecmp(argv[i], "-progressive")) {
869 printf("Using progressive entropy coding\n\n");
870 flags |= TJFLAG_PROGRESSIVE;
871 } else if (!strcasecmp(argv[i], "-rgb"))
873 else if (!strcasecmp(argv[i], "-rgbx"))
875 else if (!strcasecmp(argv[i], "-bgr"))
877 else if (!strcasecmp(argv[i], "-bgrx"))
879 else if (!strcasecmp(argv[i], "-xbgr"))
881 else if (!strcasecmp(argv[i], "-xrgb"))
883 else if (!strcasecmp(argv[i], "-cmyk"))
885 else if (!strcasecmp(argv[i], "-bottomup"))
886 flags |= TJFLAG_BOTTOMUP;
887 else if (!strcasecmp(argv[i], "-quiet"))
889 else if (!strcasecmp(argv[i], "-qq"))
891 else if (!strcasecmp(argv[i], "-scale") && i < argc - 1) {
892 int temp1 = 0, temp2 = 0, match = 0;
894 if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) == 2) {
895 for (j = 0; j < nsf; j++) {
896 if ((double)temp1 / (double)temp2 ==
897 (double)scalingFactors[j].num /
898 (double)scalingFactors[j].denom) {
899 sf = scalingFactors[j];
903 if (!match) usage(argv[0]);
904 } else usage(argv[0]);
905 } else if (!strcasecmp(argv[i], "-hflip"))
906 xformOp = TJXOP_HFLIP;
907 else if (!strcasecmp(argv[i], "-vflip"))
908 xformOp = TJXOP_VFLIP;
909 else if (!strcasecmp(argv[i], "-transpose"))
910 xformOp = TJXOP_TRANSPOSE;
911 else if (!strcasecmp(argv[i], "-transverse"))
912 xformOp = TJXOP_TRANSVERSE;
913 else if (!strcasecmp(argv[i], "-rot90"))
914 xformOp = TJXOP_ROT90;
915 else if (!strcasecmp(argv[i], "-rot180"))
916 xformOp = TJXOP_ROT180;
917 else if (!strcasecmp(argv[i], "-rot270"))
918 xformOp = TJXOP_ROT270;
919 else if (!strcasecmp(argv[i], "-grayscale"))
920 xformOpt |= TJXOPT_GRAY;
921 else if (!strcasecmp(argv[i], "-custom"))
922 customFilter = dummyDCTFilter;
923 else if (!strcasecmp(argv[i], "-nooutput"))
924 xformOpt |= TJXOPT_NOOUTPUT;
925 else if (!strcasecmp(argv[i], "-copynone"))
926 xformOpt |= TJXOPT_COPYNONE;
927 else if (!strcasecmp(argv[i], "-benchtime") && i < argc - 1) {
928 double tempd = atof(argv[++i]);
930 if (tempd > 0.0) benchTime = tempd;
932 } else if (!strcasecmp(argv[i], "-warmup") && i < argc - 1) {
933 double tempd = atof(argv[++i]);
935 if (tempd >= 0.0) warmup = tempd;
937 printf("Warmup time = %.1f seconds\n\n", warmup);
938 } else if (!strcasecmp(argv[i], "-alloc"))
939 flags &= (~TJFLAG_NOREALLOC);
940 else if (!strcasecmp(argv[i], "-bmp"))
942 else if (!strcasecmp(argv[i], "-yuv")) {
943 printf("Testing YUV planar encoding/decoding\n\n");
945 } else if (!strcasecmp(argv[i], "-yuvpad") && i < argc - 1) {
946 int tempi = atoi(argv[++i]);
948 if (tempi >= 1) yuvPad = tempi;
949 } else if (!strcasecmp(argv[i], "-subsamp") && i < argc - 1) {
951 if (toupper(argv[i][0]) == 'G') subsamp = TJSAMP_GRAY;
953 int tempi = atoi(argv[i]);
956 case 444: subsamp = TJSAMP_444; break;
957 case 422: subsamp = TJSAMP_422; break;
958 case 440: subsamp = TJSAMP_440; break;
959 case 420: subsamp = TJSAMP_420; break;
960 case 411: subsamp = TJSAMP_411; break;
963 } else if (!strcasecmp(argv[i], "-componly"))
965 else if (!strcasecmp(argv[i], "-nowrite"))
967 else if (!strcasecmp(argv[i], "-limitscans"))
968 flags |= TJFLAG_LIMITSCANS;
969 else if (!strcasecmp(argv[i], "-stoponwarning"))
970 flags |= TJFLAG_STOPONWARNING;
975 if ((sf.num != 1 || sf.denom != 1) && doTile) {
976 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
977 printf("work when scaled decompression is enabled.\n");
981 if ((flags & TJFLAG_NOREALLOC) == 0 && doTile) {
982 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
983 printf("work when dynamic JPEG buffer allocation is enabled.\n\n");
988 if ((srcBuf = tjLoadImage(argv[1], &w, 1, &h, &pf, flags)) == NULL)
989 THROW_TJG("loading bitmap");
990 temp = strrchr(argv[1], '.');
991 if (temp != NULL) *temp = '\0';
994 if (quiet == 1 && !decompOnly) {
995 printf("All performance values in Mpixels/sec\n\n");
996 printf("Bitmap JPEG JPEG %s %s ",
997 doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
998 if (doYUV) printf("Encode ");
999 printf("Comp Comp Decomp ");
1000 if (doYUV) printf("Decode");
1002 printf("Format Subsamp Qual Width Height ");
1003 if (doYUV) printf("Perf ");
1004 printf("Perf Ratio Perf ");
1005 if (doYUV) printf("Perf");
1010 decompTest(argv[1]);
1014 if (subsamp >= 0 && subsamp < TJ_NUMSAMP) {
1015 for (i = maxQual; i >= minQual; i--)
1016 fullTest(srcBuf, w, h, subsamp, i, argv[1]);
1019 if (pf != TJPF_CMYK) {
1020 for (i = maxQual; i >= minQual; i--)
1021 fullTest(srcBuf, w, h, TJSAMP_GRAY, i, argv[1]);
1024 for (i = maxQual; i >= minQual; i--)
1025 fullTest(srcBuf, w, h, TJSAMP_420, i, argv[1]);
1027 for (i = maxQual; i >= minQual; i--)
1028 fullTest(srcBuf, w, h, TJSAMP_422, i, argv[1]);
1030 for (i = maxQual; i >= minQual; i--)
1031 fullTest(srcBuf, w, h, TJSAMP_444, i, argv[1]);