2 * Copyright (C)2009-2019 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.
38 #include "./turbojpeg.h"
41 #define THROW(op, err) { \
42 printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
43 retval = -1; goto bailout; \
47 #define THROW_UNIX(m) { \
49 strerror_r(errno, err_str, 256); \
53 #define THROW_UNIX(m) THROW(m, strerror(errno))
56 char tjErrorStr[JMSG_LENGTH_MAX] = "\0", tjErrorMsg[JMSG_LENGTH_MAX] = "\0";
57 int tjErrorLine = -1, tjErrorCode = -1;
59 #define THROW_TJG(m) { \
60 printf("ERROR in line %d while %s:\n%s\n", __LINE__, m, \
61 tjGetErrorStr2(NULL)); \
62 retval = -1; goto bailout; \
65 #define THROW_TJ(m) { \
66 int _tjErrorCode = tjGetErrorCode(handle); \
67 char *_tjErrorStr = tjGetErrorStr2(handle); \
69 if (!(flags & TJFLAG_STOPONWARNING) && _tjErrorCode == TJERR_WARNING) { \
70 if (strncmp(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX) || \
71 strncmp(tjErrorMsg, m, JMSG_LENGTH_MAX) || \
72 tjErrorCode != _tjErrorCode || tjErrorLine != __LINE__) { \
73 strncpy(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX - 1); \
74 strncpy(tjErrorMsg, m, JMSG_LENGTH_MAX - 1); \
75 tjErrorCode = _tjErrorCode; \
76 tjErrorLine = __LINE__; \
77 printf("WARNING in line %d while %s:\n%s\n", __LINE__, m, _tjErrorStr); \
80 printf("%s in line %d while %s:\n%s\n", \
81 _tjErrorCode == TJERR_WARNING ? "WARNING" : "ERROR", __LINE__, m, \
83 retval = -1; goto bailout; \
87 int flags = TJFLAG_NOREALLOC, compOnly = 0, decompOnly = 0, doYUV = 0,
88 quiet = 0, doTile = 0, pf = TJPF_BGR, yuvPad = 1, doWrite = 1;
90 const char *pixFormatStr[TJ_NUMPF] = {
91 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
93 const char *subNameLong[TJ_NUMSAMP] = {
94 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
96 const char *csName[TJ_NUMCS] = {
97 "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
99 const char *subName[TJ_NUMSAMP] = {
100 "444", "422", "420", "GRAY", "440", "411"
102 tjscalingfactor *scalingFactors = NULL, sf = { 1, 1 };
103 int nsf = 0, xformOp = TJXOP_NONE, xformOpt = 0;
104 int (*customFilter) (short *, tjregion, tjregion, int, int, tjtransform *);
105 double benchTime = 5.0, warmup = 1.0;
108 static char *formatName(int subsamp, int cs, char *buf)
110 if (cs == TJCS_YCbCr)
111 return (char *)subNameLong[subsamp];
112 else if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
113 snprintf(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
116 return (char *)csName[cs];
120 static char *sigfig(double val, int figs, char *buf, int len)
123 int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));
125 if (digitsAfterDecimal < 1)
126 snprintf(format, 80, "%%.0f");
128 snprintf(format, 80, "%%.%df", digitsAfterDecimal);
129 snprintf(buf, len, format, val);
134 /* Custom DCT filter which produces a negative of the image */
135 static int dummyDCTFilter(short *coeffs, tjregion arrayRegion,
136 tjregion planeRegion, int componentIndex,
137 int transformIndex, tjtransform *transform)
141 for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
142 coeffs[i] = -coeffs[i];
147 /* Decompression test */
148 static int decomp(unsigned char *srcBuf, unsigned char **jpegBuf,
149 unsigned long *jpegSize, unsigned char *dstBuf, int w, int h,
150 int subsamp, int jpegQual, char *fileName, int tilew,
153 char tempStr[1024], sizeStr[24] = "\0", qualStr[13] = "\0", *ptr;
155 tjhandle handle = NULL;
156 int row, col, iter = 0, dstBufAlloc = 0, retval = 0;
157 double elapsed, elapsedDecode;
158 int ps = tjPixelSize[pf];
159 int scaledw = TJSCALED(w, sf);
160 int scaledh = TJSCALED(h, sf);
161 int pitch = scaledw * ps;
162 int ntilesw = (w + tilew - 1) / tilew, ntilesh = (h + tileh - 1) / tileh;
163 unsigned char *dstPtr, *dstPtr2, *yuvBuf = NULL;
166 snprintf(qualStr, 13, "_Q%d", jpegQual);
170 if ((handle = tjInitDecompress()) == NULL)
171 THROW_TJ("executing tjInitDecompress()");
173 if (dstBuf == NULL) {
174 if ((unsigned long long)pitch * (unsigned long long)scaledh >
175 (unsigned long long)((size_t)-1))
176 THROW("allocating destination buffer", "Image is too large");
177 if ((dstBuf = (unsigned char *)malloc((size_t)pitch * scaledh)) == NULL)
178 THROW_UNIX("allocating destination buffer");
181 /* Set the destination buffer to gray so we know whether the decompressor
182 attempted to write to it */
183 memset(dstBuf, 127, (size_t)pitch * scaledh);
186 int width = doTile ? tilew : scaledw;
187 int height = doTile ? tileh : scaledh;
188 unsigned long yuvSize = tjBufSizeYUV2(width, yuvPad, height, subsamp);
190 if (yuvSize == (unsigned long)-1)
191 THROW_TJ("allocating YUV buffer");
192 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
193 THROW_UNIX("allocating YUV buffer");
194 memset(yuvBuf, 127, yuvSize);
199 elapsed = elapsedDecode = 0.;
202 double start = getTime();
204 for (row = 0, dstPtr = dstBuf; row < ntilesh;
205 row++, dstPtr += (size_t)pitch * tileh) {
206 for (col = 0, dstPtr2 = dstPtr; col < ntilesw;
207 col++, tile++, dstPtr2 += ps * tilew) {
208 int width = doTile ? min(tilew, w - col * tilew) : scaledw;
209 int height = doTile ? min(tileh, h - row * tileh) : scaledh;
214 if (tjDecompressToYUV2(handle, jpegBuf[tile], jpegSize[tile], yuvBuf,
215 width, yuvPad, height, flags) == -1)
216 THROW_TJ("executing tjDecompressToYUV2()");
217 startDecode = getTime();
218 if (tjDecodeYUV(handle, yuvBuf, yuvPad, subsamp, dstPtr2, width,
219 pitch, height, pf, flags) == -1)
220 THROW_TJ("executing tjDecodeYUV()");
221 if (iter >= 0) elapsedDecode += getTime() - startDecode;
222 } else if (tjDecompress2(handle, jpegBuf[tile], jpegSize[tile],
223 dstPtr2, width, pitch, height, pf,
225 THROW_TJ("executing tjDecompress2()");
228 elapsed += getTime() - start;
231 if (elapsed >= benchTime) break;
232 } else if (elapsed >= warmup) {
234 elapsed = elapsedDecode = 0.;
237 if (doYUV) elapsed -= elapsedDecode;
239 if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
244 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
246 quiet == 2 ? "\n" : " ");
249 sigfig((double)(w * h) / 1000000. * (double)iter / elapsedDecode,
251 else if (quiet != 2) printf("\n");
253 printf("%s --> Frame rate: %f fps\n",
254 doYUV ? "Decomp to YUV" : "Decompress ", (double)iter / elapsed);
255 printf(" Throughput: %f Megapixels/sec\n",
256 (double)(w * h) / 1000000. * (double)iter / elapsed);
258 printf("YUV Decode --> Frame rate: %f fps\n",
259 (double)iter / elapsedDecode);
260 printf(" Throughput: %f Megapixels/sec\n",
261 (double)(w * h) / 1000000. * (double)iter / elapsedDecode);
265 if (!doWrite) goto bailout;
267 if (sf.num != 1 || sf.denom != 1)
268 snprintf(sizeStr, 24, "%d_%d", sf.num, sf.denom);
269 else if (tilew != w || tileh != h)
270 snprintf(sizeStr, 24, "%dx%d", tilew, tileh);
271 else snprintf(sizeStr, 24, "full");
273 snprintf(tempStr, 1024, "%s_%s.%s", fileName, sizeStr, ext);
275 snprintf(tempStr, 1024, "%s_%s%s_%s.%s", fileName, subName[subsamp],
276 qualStr, sizeStr, ext);
278 if (tjSaveImage(tempStr, dstBuf, scaledw, 0, scaledh, pf, flags) == -1)
279 THROW_TJG("saving bitmap");
280 ptr = strrchr(tempStr, '.');
281 snprintf(ptr, 1024 - (ptr - tempStr), "-err.%s", ext);
282 if (srcBuf && sf.num == 1 && sf.denom == 1) {
283 if (!quiet) printf("Compression error written to %s.\n", tempStr);
284 if (subsamp == TJ_GRAYSCALE) {
285 unsigned long index, index2;
287 for (row = 0, index = 0; row < h; row++, index += pitch) {
288 for (col = 0, index2 = index; col < w; col++, index2 += ps) {
289 unsigned long rindex = index2 + tjRedOffset[pf];
290 unsigned long gindex = index2 + tjGreenOffset[pf];
291 unsigned long bindex = index2 + tjBlueOffset[pf];
292 int y = (int)((double)srcBuf[rindex] * 0.299 +
293 (double)srcBuf[gindex] * 0.587 +
294 (double)srcBuf[bindex] * 0.114 + 0.5);
296 if (y > 255) y = 255;
298 dstBuf[rindex] = abs(dstBuf[rindex] - y);
299 dstBuf[gindex] = abs(dstBuf[gindex] - y);
300 dstBuf[bindex] = abs(dstBuf[bindex] - y);
304 for (row = 0; row < h; row++)
305 for (col = 0; col < w * ps; col++)
306 dstBuf[pitch * row + col] =
307 abs(dstBuf[pitch * row + col] - srcBuf[pitch * row + col]);
309 if (tjSaveImage(tempStr, dstBuf, w, 0, h, pf, flags) == -1)
310 THROW_TJG("saving bitmap");
314 if (file) fclose(file);
315 if (handle) tjDestroy(handle);
316 if (dstBufAlloc) free(dstBuf);
322 static int fullTest(unsigned char *srcBuf, int w, int h, int subsamp,
323 int jpegQual, char *fileName)
325 char tempStr[1024], tempStr2[80];
327 tjhandle handle = NULL;
328 unsigned char **jpegBuf = NULL, *yuvBuf = NULL, *tmpBuf = NULL, *srcPtr,
330 double start, elapsed, elapsedEncode;
331 int totalJpegSize = 0, row, col, i, tilew = w, tileh = h, retval = 0;
333 unsigned long *jpegSize = NULL, yuvSize = 0;
334 int ps = tjPixelSize[pf];
335 int ntilesw = 1, ntilesh = 1, pitch = w * ps;
336 const char *pfStr = pixFormatStr[pf];
338 if ((unsigned long long)pitch * (unsigned long long)h >
339 (unsigned long long)((size_t)-1))
340 THROW("allocating temporary image buffer", "Image is too large");
341 if ((tmpBuf = (unsigned char *)malloc((size_t)pitch * h)) == NULL)
342 THROW_UNIX("allocating temporary image buffer");
345 printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pfStr,
346 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down",
347 subNameLong[subsamp], jpegQual);
349 for (tilew = doTile ? 8 : w, tileh = doTile ? 8 : h; ;
350 tilew *= 2, tileh *= 2) {
351 if (tilew > w) tilew = w;
352 if (tileh > h) tileh = h;
353 ntilesw = (w + tilew - 1) / tilew;
354 ntilesh = (h + tileh - 1) / tileh;
356 if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
357 ntilesw * ntilesh)) == NULL)
358 THROW_UNIX("allocating JPEG tile array");
359 memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
360 if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
361 ntilesw * ntilesh)) == NULL)
362 THROW_UNIX("allocating JPEG size array");
363 memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
365 if ((flags & TJFLAG_NOREALLOC) != 0)
366 for (i = 0; i < ntilesw * ntilesh; i++) {
367 if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
368 THROW("getting buffer size", "Image is too large");
369 if ((jpegBuf[i] = (unsigned char *)
370 tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
371 THROW_UNIX("allocating JPEG tiles");
374 /* Compression test */
376 printf("%-4s (%s) %-5s %-3d ", pfStr,
377 (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", subNameLong[subsamp],
379 for (i = 0; i < h; i++)
380 memcpy(&tmpBuf[pitch * i], &srcBuf[w * ps * i], w * ps);
381 if ((handle = tjInitCompress()) == NULL)
382 THROW_TJ("executing tjInitCompress()");
385 yuvSize = tjBufSizeYUV2(tilew, yuvPad, tileh, subsamp);
386 if (yuvSize == (unsigned long)-1)
387 THROW_TJ("allocating YUV buffer");
388 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
389 THROW_UNIX("allocating YUV buffer");
390 memset(yuvBuf, 127, yuvSize);
395 elapsed = elapsedEncode = 0.;
401 for (row = 0, srcPtr = srcBuf; row < ntilesh;
402 row++, srcPtr += pitch * tileh) {
403 for (col = 0, srcPtr2 = srcPtr; col < ntilesw;
404 col++, tile++, srcPtr2 += ps * tilew) {
405 int width = min(tilew, w - col * tilew);
406 int height = min(tileh, h - row * tileh);
409 double startEncode = getTime();
411 if (tjEncodeYUV3(handle, srcPtr2, width, pitch, height, pf, yuvBuf,
412 yuvPad, subsamp, flags) == -1)
413 THROW_TJ("executing tjEncodeYUV3()");
414 if (iter >= 0) elapsedEncode += getTime() - startEncode;
415 if (tjCompressFromYUV(handle, yuvBuf, width, yuvPad, height,
416 subsamp, &jpegBuf[tile], &jpegSize[tile],
417 jpegQual, flags) == -1)
418 THROW_TJ("executing tjCompressFromYUV()");
420 if (tjCompress2(handle, srcPtr2, width, pitch, height, pf,
421 &jpegBuf[tile], &jpegSize[tile], subsamp, jpegQual,
423 THROW_TJ("executing tjCompress2()");
425 totalJpegSize += jpegSize[tile];
428 elapsed += getTime() - start;
431 if (elapsed >= benchTime) break;
432 } else if (elapsed >= warmup) {
434 elapsed = elapsedEncode = 0.;
437 if (doYUV) elapsed -= elapsedEncode;
439 if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
442 if (quiet == 1) printf("%-5d %-5d ", tilew, tileh);
446 sigfig((double)(w * h) / 1000000. *
447 (double)iter / elapsedEncode, 4, tempStr, 1024),
448 quiet == 2 ? "\n" : " ");
450 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
452 quiet == 2 ? "\n" : " ");
454 sigfig((double)(w * h * ps) / (double)totalJpegSize, 4, tempStr2,
456 quiet == 2 ? "\n" : " ");
458 printf("\n%s size: %d x %d\n", doTile ? "Tile" : "Image", tilew, tileh);
460 printf("Encode YUV --> Frame rate: %f fps\n",
461 (double)iter / elapsedEncode);
462 printf(" Output image size: %lu bytes\n", yuvSize);
463 printf(" Compression ratio: %f:1\n",
464 (double)(w * h * ps) / (double)yuvSize);
465 printf(" Throughput: %f Megapixels/sec\n",
466 (double)(w * h) / 1000000. * (double)iter / elapsedEncode);
467 printf(" Output bit stream: %f Megabits/sec\n",
468 (double)yuvSize * 8. / 1000000. * (double)iter / elapsedEncode);
470 printf("%s --> Frame rate: %f fps\n",
471 doYUV ? "Comp from YUV" : "Compress ",
472 (double)iter / elapsed);
473 printf(" Output image size: %d bytes\n",
475 printf(" Compression ratio: %f:1\n",
476 (double)(w * h * ps) / (double)totalJpegSize);
477 printf(" Throughput: %f Megapixels/sec\n",
478 (double)(w * h) / 1000000. * (double)iter / elapsed);
479 printf(" Output bit stream: %f Megabits/sec\n",
480 (double)totalJpegSize * 8. / 1000000. * (double)iter / elapsed);
482 if (tilew == w && tileh == h && doWrite) {
483 snprintf(tempStr, 1024, "%s_%s_Q%d.jpg", fileName, subName[subsamp],
485 if ((file = fopen(tempStr, "wb")) == NULL)
486 THROW_UNIX("opening reference image");
487 if (fwrite(jpegBuf[0], jpegSize[0], 1, file) != 1)
488 THROW_UNIX("writing reference image");
489 fclose(file); file = NULL;
490 if (!quiet) printf("Reference image written to %s\n", tempStr);
493 /* Decompression test */
495 if (decomp(srcBuf, jpegBuf, jpegSize, tmpBuf, w, h, subsamp, jpegQual,
496 fileName, tilew, tileh) == -1)
498 } else if (quiet == 1) printf("N/A\n");
500 for (i = 0; i < ntilesw * ntilesh; i++) {
504 free(jpegBuf); jpegBuf = NULL;
505 free(jpegSize); jpegSize = NULL;
507 free(yuvBuf); yuvBuf = NULL;
510 if (tilew == w && tileh == h) break;
514 if (file) fclose(file);
516 for (i = 0; i < ntilesw * ntilesh; i++)
523 if (handle) tjDestroy(handle);
528 static int decompTest(char *fileName)
531 tjhandle handle = NULL;
532 unsigned char **jpegBuf = NULL, *srcBuf = NULL;
533 unsigned long *jpegSize = NULL, srcSize, totalJpegSize;
534 tjtransform *t = NULL;
535 double start, elapsed;
536 int ps = tjPixelSize[pf], tile, row, col, i, iter, retval = 0, decompsrc = 0;
537 char *temp = NULL, tempStr[80], tempStr2[80];
539 int w = 0, h = 0, tilew, tileh, ntilesw = 1, ntilesh = 1, subsamp = -1,
541 /* Transformed image */
542 int tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;
544 if ((file = fopen(fileName, "rb")) == NULL)
545 THROW_UNIX("opening file");
546 if (fseek(file, 0, SEEK_END) < 0 ||
547 (srcSize = ftell(file)) == (unsigned long)-1)
548 THROW_UNIX("determining file size");
549 if ((srcBuf = (unsigned char *)malloc(srcSize)) == NULL)
550 THROW_UNIX("allocating memory");
551 if (fseek(file, 0, SEEK_SET) < 0)
552 THROW_UNIX("setting file position");
553 if (fread(srcBuf, srcSize, 1, file) < 1)
554 THROW_UNIX("reading JPEG data");
555 fclose(file); file = NULL;
557 temp = strrchr(fileName, '.');
558 if (temp != NULL) *temp = '\0';
560 if ((handle = tjInitTransform()) == NULL)
561 THROW_TJ("executing tjInitTransform()");
562 if (tjDecompressHeader3(handle, srcBuf, srcSize, &w, &h, &subsamp,
564 THROW_TJ("executing tjDecompressHeader3()");
566 THROW("reading JPEG header", "Invalid image dimensions");
567 if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
568 pf = TJPF_CMYK; ps = tjPixelSize[pf];
572 printf("All performance values in Mpixels/sec\n\n");
573 printf("Bitmap JPEG JPEG %s %s Xform Comp Decomp ",
574 doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
575 if (doYUV) printf("Decode");
577 printf("Format CS Subsamp Width Height Perf Ratio Perf ");
578 if (doYUV) printf("Perf");
581 printf(">>>>> JPEG %s --> %s (%s) <<<<<\n",
582 formatName(subsamp, cs, tempStr), pixFormatStr[pf],
583 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down");
585 for (tilew = doTile ? 16 : w, tileh = doTile ? 16 : h; ;
586 tilew *= 2, tileh *= 2) {
587 if (tilew > w) tilew = w;
588 if (tileh > h) tileh = h;
589 ntilesw = (w + tilew - 1) / tilew;
590 ntilesh = (h + tileh - 1) / tileh;
592 if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
593 ntilesw * ntilesh)) == NULL)
594 THROW_UNIX("allocating JPEG tile array");
595 memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
596 if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
597 ntilesw * ntilesh)) == NULL)
598 THROW_UNIX("allocating JPEG size array");
599 memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
601 if ((flags & TJFLAG_NOREALLOC) != 0 &&
602 (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter))
603 for (i = 0; i < ntilesw * ntilesh; i++) {
604 if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
605 THROW("getting buffer size", "Image is too large");
606 if ((jpegBuf[i] = (unsigned char *)
607 tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
608 THROW_UNIX("allocating JPEG tiles");
611 tw = w; th = h; ttilew = tilew; ttileh = tileh;
613 printf("\n%s size: %d x %d", doTile ? "Tile" : "Image", ttilew, ttileh);
614 if (sf.num != 1 || sf.denom != 1)
615 printf(" --> %d x %d", TJSCALED(tw, sf), TJSCALED(th, sf));
617 } else if (quiet == 1) {
618 printf("%-4s (%s) %-5s %-5s ", pixFormatStr[pf],
619 (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", csName[cs],
620 subNameLong[subsamp]);
621 printf("%-5d %-5d ", tilew, tileh);
625 if (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter) {
626 if ((t = (tjtransform *)malloc(sizeof(tjtransform) * ntilesw *
628 THROW_UNIX("allocating image transform array");
630 if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
631 xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
632 tw = h; th = w; ttilew = tileh; ttileh = tilew;
635 if (xformOpt & TJXOPT_GRAY) tsubsamp = TJ_GRAYSCALE;
636 if (xformOp == TJXOP_HFLIP || xformOp == TJXOP_ROT180)
637 tw = tw - (tw % tjMCUWidth[tsubsamp]);
638 if (xformOp == TJXOP_VFLIP || xformOp == TJXOP_ROT180)
639 th = th - (th % tjMCUHeight[tsubsamp]);
640 if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT90)
641 tw = tw - (tw % tjMCUHeight[tsubsamp]);
642 if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT270)
643 th = th - (th % tjMCUWidth[tsubsamp]);
644 tntilesw = (tw + ttilew - 1) / ttilew;
645 tntilesh = (th + ttileh - 1) / ttileh;
647 if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
648 xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
649 if (tsubsamp == TJSAMP_422) tsubsamp = TJSAMP_440;
650 else if (tsubsamp == TJSAMP_440) tsubsamp = TJSAMP_422;
653 for (row = 0, tile = 0; row < tntilesh; row++) {
654 for (col = 0; col < tntilesw; col++, tile++) {
655 t[tile].r.w = min(ttilew, tw - col * ttilew);
656 t[tile].r.h = min(ttileh, th - row * ttileh);
657 t[tile].r.x = col * ttilew;
658 t[tile].r.y = row * ttileh;
659 t[tile].op = xformOp;
660 t[tile].options = xformOpt | TJXOPT_TRIM;
661 t[tile].customFilter = customFilter;
662 if (t[tile].options & TJXOPT_NOOUTPUT && jpegBuf[tile]) {
663 tjFree(jpegBuf[tile]); jpegBuf[tile] = NULL;
672 if (tjTransform(handle, srcBuf, srcSize, tntilesw * tntilesh, jpegBuf,
673 jpegSize, t, flags) == -1)
674 THROW_TJ("executing tjTransform()");
675 elapsed += getTime() - start;
678 if (elapsed >= benchTime) break;
679 } else if (elapsed >= warmup) {
687 for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
688 totalJpegSize += jpegSize[tile];
691 printf("%-6s%s%-6s%s",
692 sigfig((double)(w * h) / 1000000. / elapsed, 4, tempStr, 80),
693 quiet == 2 ? "\n" : " ",
694 sigfig((double)(w * h * ps) / (double)totalJpegSize, 4,
696 quiet == 2 ? "\n" : " ");
698 printf("Transform --> Frame rate: %f fps\n",
700 printf(" Output image size: %lu bytes\n",
702 printf(" Compression ratio: %f:1\n",
703 (double)(w * h * ps) / (double)totalJpegSize);
704 printf(" Throughput: %f Megapixels/sec\n",
705 (double)(w * h) / 1000000. / elapsed);
706 printf(" Output bit stream: %f Megabits/sec\n",
707 (double)totalJpegSize * 8. / 1000000. / elapsed);
710 if (quiet == 1) printf("N/A N/A ");
716 if (w == tilew) ttilew = tw;
717 if (h == tileh) ttileh = th;
718 if (!(xformOpt & TJXOPT_NOOUTPUT)) {
719 if (decomp(NULL, decompsrc ? &srcBuf : jpegBuf,
720 decompsrc ? &srcSize : jpegSize, NULL, tw, th, tsubsamp, 0,
721 fileName, ttilew, ttileh) == -1)
723 } else if (quiet == 1) printf("N/A\n");
725 for (i = 0; i < ntilesw * ntilesh; i++) {
729 free(jpegBuf); jpegBuf = NULL;
730 free(jpegSize); jpegSize = NULL;
732 if (tilew == w && tileh == h) break;
736 if (file) fclose(file);
738 for (i = 0; i < ntilesw * ntilesh; i++)
745 if (handle) { tjDestroy(handle); handle = NULL; }
750 static void usage(char *progName)
754 printf("USAGE: %s\n", progName);
755 printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
756 printf(" %s\n", progName);
757 printf(" <Inputfile (JPG)> [options]\n\n");
758 printf("Options:\n\n");
759 printf("-alloc = Dynamically allocate JPEG image buffers\n");
760 printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
761 printf("-bottomup = Test bottom-up compression/decompression\n");
762 printf("-tile = Test performance of the codec when the image is encoded as separate\n");
763 printf(" tiles of varying sizes.\n");
764 printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
765 printf(" Test the specified color conversion path in the codec (default = BGR)\n");
766 printf("-cmyk = Indirectly test YCCK JPEG compression/decompression (the source\n");
767 printf(" and destination bitmaps are still RGB. The conversion is done\n");
768 printf(" internally prior to compression or after decompression.)\n");
769 printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
770 printf(" the underlying codec\n");
771 printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
773 printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
774 printf(" underlying codec\n");
775 printf("-progressive = Use progressive entropy coding in JPEG images generated by\n");
776 printf(" compression and transform operations.\n");
777 printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
778 printf(" of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");
779 printf(" GRAY). The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
780 printf(" sequence.\n");
781 printf("-quiet = Output results in tabular rather than verbose format\n");
782 printf("-yuv = Test YUV encoding/decoding functions\n");
783 printf("-yuvpad <p> = If testing YUV encoding/decoding, this specifies the number of\n");
784 printf(" bytes to which each row of each plane in the intermediate YUV image is\n");
785 printf(" padded (default = 1)\n");
786 printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
787 printf(" factor of M/N (M/N = ");
788 for (i = 0; i < nsf; i++) {
789 printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
790 if (nsf == 2 && i != nsf - 1) printf(" or ");
792 if (i != nsf - 1) printf(", ");
793 if (i == nsf - 2) printf("or ");
795 if (i % 8 == 0 && i != 0) printf("\n ");
798 printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
799 printf(" Perform the corresponding lossless transform prior to\n");
800 printf(" decompression (these options are mutually exclusive)\n");
801 printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
802 printf(" test (can be combined with the other transforms above)\n");
803 printf("-copynone = Do not copy any extra markers (including EXIF and ICC profile data)\n");
804 printf(" when transforming the image.\n");
805 printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");
806 printf("-warmup <t> = Run each benchmark for <t> seconds (default = 1.0) prior to\n");
807 printf(" starting the timer, in order to prime the caches and thus improve the\n");
808 printf(" consistency of the results.\n");
809 printf("-componly = Stop after running compression tests. Do not test decompression.\n");
810 printf("-nowrite = Do not write reference or output images (improves consistency of\n");
811 printf(" performance measurements.)\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], "-stoponwarning"))
968 flags |= TJFLAG_STOPONWARNING;
973 if ((sf.num != 1 || sf.denom != 1) && doTile) {
974 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
975 printf("work when scaled decompression is enabled.\n");
979 if ((flags & TJFLAG_NOREALLOC) == 0 && doTile) {
980 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
981 printf("work when dynamic JPEG buffer allocation is enabled.\n\n");
986 if ((srcBuf = tjLoadImage(argv[1], &w, 1, &h, &pf, flags)) == NULL)
987 THROW_TJG("loading bitmap");
988 temp = strrchr(argv[1], '.');
989 if (temp != NULL) *temp = '\0';
992 if (quiet == 1 && !decompOnly) {
993 printf("All performance values in Mpixels/sec\n\n");
994 printf("Bitmap JPEG JPEG %s %s ",
995 doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
996 if (doYUV) printf("Encode ");
997 printf("Comp Comp Decomp ");
998 if (doYUV) printf("Decode");
1000 printf("Format Subsamp Qual Width Height ");
1001 if (doYUV) printf("Perf ");
1002 printf("Perf Ratio Perf ");
1003 if (doYUV) printf("Perf");
1008 decompTest(argv[1]);
1012 if (subsamp >= 0 && subsamp < TJ_NUMSAMP) {
1013 for (i = maxQual; i >= minQual; i--)
1014 fullTest(srcBuf, w, h, subsamp, i, argv[1]);
1017 if (pf != TJPF_CMYK) {
1018 for (i = maxQual; i >= minQual; i--)
1019 fullTest(srcBuf, w, h, TJSAMP_GRAY, i, argv[1]);
1022 for (i = maxQual; i >= minQual; i--)
1023 fullTest(srcBuf, w, h, TJSAMP_420, i, argv[1]);
1025 for (i = maxQual; i >= minQual; i--)
1026 fullTest(srcBuf, w, h, TJSAMP_422, i, argv[1]);
1028 for (i = maxQual; i >= minQual; i--)
1029 fullTest(srcBuf, w, h, TJSAMP_444, i, argv[1]);