2 * Copyright (C)2009-2018 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.
37 #include "./turbojpeg.h"
40 #define _throw(op, err) { \
41 printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
42 retval = -1; goto bailout; \
46 #define _throwunix(m) { \
48 strerror_r(errno, err_str, 256); \
52 #define _throwunix(m) _throw(m, strerror(errno))
55 char tjErrorStr[JMSG_LENGTH_MAX] = "\0", tjErrorMsg[JMSG_LENGTH_MAX] = "\0";
56 int tjErrorLine = -1, tjErrorCode = -1;
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; \
64 #define _throwtj(m) { \
65 int _tjErrorCode = tjGetErrorCode(handle); \
66 char *_tjErrorStr = tjGetErrorStr2(handle); \
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); \
79 printf("%s in line %d while %s:\n%s\n", \
80 _tjErrorCode == TJERR_WARNING ? "WARNING" : "ERROR", __LINE__, m, \
82 retval = -1; goto bailout; \
86 int flags = TJFLAG_NOREALLOC, compOnly = 0, decompOnly = 0, doYUV = 0,
87 quiet = 0, doTile = 0, pf = TJPF_BGR, yuvPad = 1, doWrite = 1;
89 const char *pixFormatStr[TJ_NUMPF] = {
90 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
92 const char *subNameLong[TJ_NUMSAMP] = {
93 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
95 const char *csName[TJ_NUMCS] = {
96 "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
98 const char *subName[TJ_NUMSAMP] = {
99 "444", "422", "420", "GRAY", "440", "411"
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;
107 char *formatName(int subsamp, int cs, char *buf)
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]);
115 return (char *)csName[cs];
119 char *sigfig(double val, int figs, char *buf, int len)
122 int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));
124 if (digitsAfterDecimal < 1)
125 snprintf(format, 80, "%%.0f");
127 snprintf(format, 80, "%%.%df", digitsAfterDecimal);
128 snprintf(buf, len, format, val);
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)
140 for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
141 coeffs[i] = -coeffs[i];
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)
151 char tempStr[1024], sizeStr[20] = "\0", qualStr[6] = "\0", *ptr;
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;
164 snprintf(qualStr, 6, "_Q%d", jpegQual);
168 if ((handle = tjInitDecompress()) == NULL)
169 _throwtj("executing tjInitDecompress()");
171 if (dstBuf == NULL) {
172 if ((dstBuf = (unsigned char *)malloc(pitch * scaledh)) == NULL)
173 _throwunix("allocating destination buffer");
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);
181 int width = doTile ? tilew : scaledw;
182 int height = doTile ? tileh : scaledh;
183 int yuvSize = tjBufSizeYUV2(width, yuvPad, height, subsamp);
185 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
186 _throwunix("allocating YUV buffer");
187 memset(yuvBuf, 127, yuvSize);
192 elapsed = elapsedDecode = 0.;
195 double start = getTime();
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;
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,
218 _throwtj("executing tjDecompress2()");
221 elapsed += getTime() - start;
224 if (elapsed >= benchTime) break;
225 } else if (elapsed >= warmup) {
227 elapsed = elapsedDecode = 0.;
230 if (doYUV) elapsed -= elapsedDecode;
232 if (tjDestroy(handle) == -1) _throwtj("executing tjDestroy()");
237 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
239 quiet == 2 ? "\n" : " ");
242 sigfig((double)(w * h) / 1000000. * (double)iter / elapsedDecode,
244 else if (quiet != 2) printf("\n");
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);
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);
258 if (!doWrite) goto bailout;
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");
266 snprintf(tempStr, 1024, "%s_%s.%s", fileName, sizeStr, ext);
268 snprintf(tempStr, 1024, "%s_%s%s_%s.%s", fileName, subName[subsamp],
269 qualStr, sizeStr, ext);
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) {
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);
289 if (y > 255) y = 255;
291 dstBuf[rindex] = abs(dstBuf[rindex] - y);
292 dstBuf[gindex] = abs(dstBuf[gindex] - y);
293 dstBuf[bindex] = abs(dstBuf[bindex] - y);
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]);
302 if (tjSaveImage(tempStr, dstBuf, w, 0, h, pf, flags) == -1)
303 _throwtjg("saving bitmap");
307 if (file) fclose(file);
308 if (handle) tjDestroy(handle);
309 if (dstBuf && dstBufAlloc) free(dstBuf);
310 if (yuvBuf) free(yuvBuf);
315 int fullTest(unsigned char *srcBuf, int w, int h, int subsamp, int jpegQual,
318 char tempStr[1024], tempStr2[80];
320 tjhandle handle = NULL;
321 unsigned char **jpegBuf = NULL, *yuvBuf = NULL, *tmpBuf = NULL, *srcPtr,
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];
331 if ((tmpBuf = (unsigned char *)malloc(pitch * h)) == NULL)
332 _throwunix("allocating temporary image buffer");
335 printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pfStr,
336 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down",
337 subNameLong[subsamp], jpegQual);
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;
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);
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");
362 /* Compression test */
364 printf("%-4s (%s) %-5s %-3d ", pfStr,
365 (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", subNameLong[subsamp],
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()");
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);
381 elapsed = elapsedEncode = 0.;
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);
395 double startEncode = getTime();
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()");
406 if (tjCompress2(handle, srcPtr2, width, pitch, height, pf,
407 &jpegBuf[tile], &jpegSize[tile], subsamp, jpegQual,
409 _throwtj("executing tjCompress2()");
411 totalJpegSize += jpegSize[tile];
414 elapsed += getTime() - start;
417 if (elapsed >= benchTime) break;
418 } else if (elapsed >= warmup) {
420 elapsed = elapsedEncode = 0.;
423 if (doYUV) elapsed -= elapsedEncode;
425 if (tjDestroy(handle) == -1) _throwtj("executing tjDestroy()");
428 if (quiet == 1) printf("%-5d %-5d ", tilew, tileh);
432 sigfig((double)(w * h) / 1000000. *
433 (double)iter / elapsedEncode, 4, tempStr, 1024),
434 quiet == 2 ? "\n" : " ");
436 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
438 quiet == 2 ? "\n" : " ");
440 sigfig((double)(w * h * ps) / (double)totalJpegSize, 4, tempStr2,
442 quiet == 2 ? "\n" : " ");
444 printf("\n%s size: %d x %d\n", doTile ? "Tile" : "Image", tilew, tileh);
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);
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",
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);
468 if (tilew == w && tileh == h && doWrite) {
469 snprintf(tempStr, 1024, "%s_%s_Q%d.jpg", fileName, subName[subsamp],
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);
479 /* Decompression test */
481 if (decomp(srcBuf, jpegBuf, jpegSize, tmpBuf, w, h, subsamp, jpegQual,
482 fileName, tilew, tileh) == -1)
486 for (i = 0; i < ntilesw * ntilesh; i++) {
487 if (jpegBuf[i]) tjFree(jpegBuf[i]);
490 free(jpegBuf); jpegBuf = NULL;
491 free(jpegSize); jpegSize = NULL;
493 free(yuvBuf); yuvBuf = NULL;
496 if (tilew == w && tileh == h) break;
500 if (file) { fclose(file); file = NULL; }
502 for (i = 0; i < ntilesw * ntilesh; i++) {
503 if (jpegBuf[i]) tjFree(jpegBuf[i]);
506 free(jpegBuf); jpegBuf = NULL;
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; }
516 int decompTest(char *fileName)
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];
527 int w = 0, h = 0, tilew, tileh, ntilesw = 1, ntilesh = 1, subsamp = -1,
529 /* Transformed image */
530 int tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;
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;
545 temp = strrchr(fileName, '.');
546 if (temp != NULL) *temp = '\0';
548 if ((handle = tjInitTransform()) == NULL)
549 _throwtj("executing tjInitTransform()");
550 if (tjDecompressHeader3(handle, srcBuf, srcSize, &w, &h, &subsamp,
552 _throwtj("executing tjDecompressHeader3()");
553 if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
554 pf = TJPF_CMYK; ps = tjPixelSize[pf];
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");
563 printf("Format CS Subsamp Width Height Perf Ratio Perf ");
564 if (doYUV) printf("Perf");
567 printf(">>>>> JPEG %s --> %s (%s) <<<<<\n",
568 formatName(subsamp, cs, tempStr), pixFormatStr[pf],
569 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down");
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;
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);
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");
594 tw = w; th = h; ttilew = tilew; ttileh = tileh;
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));
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);
608 if (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter) {
609 if ((t = (tjtransform *)malloc(sizeof(tjtransform) * ntilesw *
611 _throwunix("allocating image transform array");
613 if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
614 xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
615 tw = h; th = w; ttilew = tileh; ttileh = tilew;
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;
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;
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;
655 if (tjTransform(handle, srcBuf, srcSize, tntilesw * tntilesh, jpegBuf,
656 jpegSize, t, flags) == -1)
657 _throwtj("executing tjTransform()");
658 elapsed += getTime() - start;
661 if (elapsed >= benchTime) break;
662 } else if (elapsed >= warmup) {
670 for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
671 totalJpegSize += jpegSize[tile];
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,
679 quiet == 2 ? "\n" : " ");
681 printf("Transform --> Frame rate: %f fps\n",
683 printf(" Output image size: %lu bytes\n",
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);
693 if (quiet == 1) printf("N/A N/A ");
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)
706 } else if (quiet == 1) printf("N/A\n");
708 for (i = 0; i < ntilesw * ntilesh; i++) {
709 tjFree(jpegBuf[i]); jpegBuf[i] = NULL;
711 free(jpegBuf); jpegBuf = NULL;
712 if (jpegSize) { free(jpegSize); jpegSize = NULL; }
714 if (tilew == w && tileh == h) break;
718 if (file) { fclose(file); file = NULL; }
720 for (i = 0; i < ntilesw * ntilesh; i++) {
721 if (jpegBuf[i]) tjFree(jpegBuf[i]);
724 free(jpegBuf); jpegBuf = NULL;
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; }
734 void usage(char *progName)
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");
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 ");
776 if (i != nsf - 1) printf(", ");
777 if (i == nsf - 2) printf("or ");
779 if (i % 8 == 0 && i != 0) 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");
805 int main(int argc, char *argv[])
807 unsigned char *srcBuf = NULL;
808 int w = 0, h = 0, i, j, minQual = -1, maxQual = -1;
810 int minArg = 2, retval = 0, subsamp = -1;
812 if ((scalingFactors = tjGetScalingFactors(&nsf)) == NULL || nsf == 0)
813 _throw("executing tjGetScalingFactors()", tjGetErrorStr());
815 if (argc < minArg) usage(argv[0]);
817 temp = strrchr(argv[1], '.');
819 if (!strcasecmp(temp, ".bmp")) ext = "bmp";
820 if (!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg"))
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.");
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;
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"))
857 else if (!strcasecmp(argv[i], "-rgbx"))
859 else if (!strcasecmp(argv[i], "-bgr"))
861 else if (!strcasecmp(argv[i], "-bgrx"))
863 else if (!strcasecmp(argv[i], "-xbgr"))
865 else if (!strcasecmp(argv[i], "-xrgb"))
867 else if (!strcasecmp(argv[i], "-cmyk"))
869 else if (!strcasecmp(argv[i], "-bottomup"))
870 flags |= TJFLAG_BOTTOMUP;
871 else if (!strcasecmp(argv[i], "-quiet"))
873 else if (!strcasecmp(argv[i], "-qq"))
875 else if (!strcasecmp(argv[i], "-scale") && i < argc - 1) {
876 int temp1 = 0, temp2 = 0, match = 0;
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];
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]);
914 if (temp > 0.0) benchTime = temp;
916 } else if (!strcasecmp(argv[i], "-warmup") && i < argc - 1) {
917 double temp = atof(argv[++i]);
919 if (temp >= 0.0) warmup = temp;
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"))
926 else if (!strcasecmp(argv[i], "-yuv")) {
927 printf("Testing YUV planar encoding/decoding\n\n");
929 } else if (!strcasecmp(argv[i], "-yuvpad") && i < argc - 1) {
930 int temp = atoi(argv[++i]);
932 if (temp >= 1) yuvPad = temp;
933 } else if (!strcasecmp(argv[i], "-subsamp") && i < argc - 1) {
935 if (toupper(argv[i][0]) == 'G') subsamp = TJSAMP_GRAY;
937 int temp = atoi(argv[i]);
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;
947 } else if (!strcasecmp(argv[i], "-componly"))
949 else if (!strcasecmp(argv[i], "-nowrite"))
951 else if (!strcasecmp(argv[i], "-stoponwarning"))
952 flags |= TJFLAG_STOPONWARNING;
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");
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");
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';
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");
984 printf("Format Subsamp Qual Width Height ");
985 if (doYUV) printf("Perf ");
986 printf("Perf Ratio Perf ");
987 if (doYUV) printf("Perf");
996 if (subsamp >= 0 && subsamp < TJ_NUMSAMP) {
997 for (i = maxQual; i >= minQual; i--)
998 fullTest(srcBuf, w, h, subsamp, i, argv[1]);
1001 if (pf != TJPF_CMYK) {
1002 for (i = maxQual; i >= minQual; i--)
1003 fullTest(srcBuf, w, h, TJSAMP_GRAY, i, argv[1]);
1006 for (i = maxQual; i >= minQual; i--)
1007 fullTest(srcBuf, w, h, TJSAMP_420, i, argv[1]);
1009 for (i = maxQual; i >= minQual; i--)
1010 fullTest(srcBuf, w, h, TJSAMP_422, i, argv[1]);
1012 for (i = maxQual; i >= minQual; i--)
1013 fullTest(srcBuf, w, h, TJSAMP_444, i, argv[1]);
1018 if (srcBuf) tjFree(srcBuf);