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; \
45 #define THROW_UNIX(m) THROW(m, strerror(errno))
47 char tjErrorStr[JMSG_LENGTH_MAX] = "\0", tjErrorMsg[JMSG_LENGTH_MAX] = "\0";
48 int tjErrorLine = -1, tjErrorCode = -1;
50 #define THROW_TJG(m) { \
51 printf("ERROR in line %d while %s:\n%s\n", __LINE__, m, \
52 tjGetErrorStr2(NULL)); \
53 retval = -1; goto bailout; \
56 #define THROW_TJ(m) { \
57 int _tjErrorCode = tjGetErrorCode(handle); \
58 char *_tjErrorStr = tjGetErrorStr2(handle); \
60 if (!(flags & TJFLAG_STOPONWARNING) && _tjErrorCode == TJERR_WARNING) { \
61 if (strncmp(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX) || \
62 strncmp(tjErrorMsg, m, JMSG_LENGTH_MAX) || \
63 tjErrorCode != _tjErrorCode || tjErrorLine != __LINE__) { \
64 strncpy(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX - 1); \
65 strncpy(tjErrorMsg, m, JMSG_LENGTH_MAX - 1); \
66 tjErrorCode = _tjErrorCode; \
67 tjErrorLine = __LINE__; \
68 printf("WARNING in line %d while %s:\n%s\n", __LINE__, m, _tjErrorStr); \
71 printf("%s in line %d while %s:\n%s\n", \
72 _tjErrorCode == TJERR_WARNING ? "WARNING" : "ERROR", __LINE__, m, \
74 retval = -1; goto bailout; \
78 int flags = TJFLAG_NOREALLOC, compOnly = 0, decompOnly = 0, doYUV = 0,
79 quiet = 0, doTile = 0, pf = TJPF_BGR, yuvPad = 1, doWrite = 1;
81 const char *pixFormatStr[TJ_NUMPF] = {
82 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
84 const char *subNameLong[TJ_NUMSAMP] = {
85 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
87 const char *csName[TJ_NUMCS] = {
88 "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
90 const char *subName[TJ_NUMSAMP] = {
91 "444", "422", "420", "GRAY", "440", "411"
93 tjscalingfactor *scalingFactors = NULL, sf = { 1, 1 };
94 int nsf = 0, xformOp = TJXOP_NONE, xformOpt = 0;
95 int (*customFilter) (short *, tjregion, tjregion, int, int, tjtransform *);
96 double benchTime = 5.0, warmup = 1.0;
99 static char *formatName(int subsamp, int cs, char *buf)
101 if (cs == TJCS_YCbCr)
102 return (char *)subNameLong[subsamp];
103 else if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
104 snprintf(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
107 return (char *)csName[cs];
111 static char *sigfig(double val, int figs, char *buf, int len)
114 int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));
116 if (digitsAfterDecimal < 1)
117 snprintf(format, 80, "%%.0f");
119 snprintf(format, 80, "%%.%df", digitsAfterDecimal);
120 snprintf(buf, len, format, val);
125 /* Custom DCT filter which produces a negative of the image */
126 static int dummyDCTFilter(short *coeffs, tjregion arrayRegion,
127 tjregion planeRegion, int componentIndex,
128 int transformIndex, tjtransform *transform)
132 for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
133 coeffs[i] = -coeffs[i];
138 /* Decompression test */
139 static int decomp(unsigned char *srcBuf, unsigned char **jpegBuf,
140 unsigned long *jpegSize, unsigned char *dstBuf, int w, int h,
141 int subsamp, int jpegQual, char *fileName, int tilew,
144 char tempStr[1024], sizeStr[24] = "\0", qualStr[13] = "\0", *ptr;
146 tjhandle handle = NULL;
147 int row, col, iter = 0, dstBufAlloc = 0, retval = 0;
148 double elapsed, elapsedDecode;
149 int ps = tjPixelSize[pf];
150 int scaledw = TJSCALED(w, sf);
151 int scaledh = TJSCALED(h, sf);
152 int pitch = scaledw * ps;
153 int ntilesw = (w + tilew - 1) / tilew, ntilesh = (h + tileh - 1) / tileh;
154 unsigned char *dstPtr, *dstPtr2, *yuvBuf = NULL;
157 snprintf(qualStr, 13, "_Q%d", jpegQual);
161 if ((handle = tjInitDecompress()) == NULL)
162 THROW_TJ("executing tjInitDecompress()");
164 if (dstBuf == NULL) {
165 if ((unsigned long long)pitch * (unsigned long long)scaledh >
166 (unsigned long long)((size_t)-1))
167 THROW("allocating destination buffer", "Image is too large");
168 if ((dstBuf = (unsigned char *)malloc((size_t)pitch * scaledh)) == NULL)
169 THROW_UNIX("allocating destination buffer");
172 /* Set the destination buffer to gray so we know whether the decompressor
173 attempted to write to it */
174 memset(dstBuf, 127, (size_t)pitch * scaledh);
177 int width = doTile ? tilew : scaledw;
178 int height = doTile ? tileh : scaledh;
179 unsigned long yuvSize = tjBufSizeYUV2(width, yuvPad, height, subsamp);
181 if (yuvSize == (unsigned long)-1)
182 THROW_TJ("allocating YUV buffer");
183 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
184 THROW_UNIX("allocating YUV buffer");
185 memset(yuvBuf, 127, yuvSize);
190 elapsed = elapsedDecode = 0.;
193 double start = getTime();
195 for (row = 0, dstPtr = dstBuf; row < ntilesh;
196 row++, dstPtr += (size_t)pitch * tileh) {
197 for (col = 0, dstPtr2 = dstPtr; col < ntilesw;
198 col++, tile++, dstPtr2 += ps * tilew) {
199 int width = doTile ? min(tilew, w - col * tilew) : scaledw;
200 int height = doTile ? min(tileh, h - row * tileh) : scaledh;
205 if (tjDecompressToYUV2(handle, jpegBuf[tile], jpegSize[tile], yuvBuf,
206 width, yuvPad, height, flags) == -1)
207 THROW_TJ("executing tjDecompressToYUV2()");
208 startDecode = getTime();
209 if (tjDecodeYUV(handle, yuvBuf, yuvPad, subsamp, dstPtr2, width,
210 pitch, height, pf, flags) == -1)
211 THROW_TJ("executing tjDecodeYUV()");
212 if (iter >= 0) elapsedDecode += getTime() - startDecode;
213 } else if (tjDecompress2(handle, jpegBuf[tile], jpegSize[tile],
214 dstPtr2, width, pitch, height, pf,
216 THROW_TJ("executing tjDecompress2()");
219 elapsed += getTime() - start;
222 if (elapsed >= benchTime) break;
223 } else if (elapsed >= warmup) {
225 elapsed = elapsedDecode = 0.;
228 if (doYUV) elapsed -= elapsedDecode;
230 if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
235 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
237 quiet == 2 ? "\n" : " ");
240 sigfig((double)(w * h) / 1000000. * (double)iter / elapsedDecode,
242 else if (quiet != 2) printf("\n");
244 printf("%s --> Frame rate: %f fps\n",
245 doYUV ? "Decomp to YUV" : "Decompress ", (double)iter / elapsed);
246 printf(" Throughput: %f Megapixels/sec\n",
247 (double)(w * h) / 1000000. * (double)iter / elapsed);
249 printf("YUV Decode --> Frame rate: %f fps\n",
250 (double)iter / elapsedDecode);
251 printf(" Throughput: %f Megapixels/sec\n",
252 (double)(w * h) / 1000000. * (double)iter / elapsedDecode);
256 if (!doWrite) goto bailout;
258 if (sf.num != 1 || sf.denom != 1)
259 snprintf(sizeStr, 24, "%d_%d", sf.num, sf.denom);
260 else if (tilew != w || tileh != h)
261 snprintf(sizeStr, 24, "%dx%d", tilew, tileh);
262 else snprintf(sizeStr, 24, "full");
264 snprintf(tempStr, 1024, "%s_%s.%s", fileName, sizeStr, ext);
266 snprintf(tempStr, 1024, "%s_%s%s_%s.%s", fileName, subName[subsamp],
267 qualStr, sizeStr, ext);
269 if (tjSaveImage(tempStr, dstBuf, scaledw, 0, scaledh, pf, flags) == -1)
270 THROW_TJG("saving bitmap");
271 ptr = strrchr(tempStr, '.');
272 snprintf(ptr, 1024 - (ptr - tempStr), "-err.%s", ext);
273 if (srcBuf && sf.num == 1 && sf.denom == 1) {
274 if (!quiet) printf("Compression error written to %s.\n", tempStr);
275 if (subsamp == TJ_GRAYSCALE) {
276 unsigned long index, index2;
278 for (row = 0, index = 0; row < h; row++, index += pitch) {
279 for (col = 0, index2 = index; col < w; col++, index2 += ps) {
280 unsigned long rindex = index2 + tjRedOffset[pf];
281 unsigned long gindex = index2 + tjGreenOffset[pf];
282 unsigned long bindex = index2 + tjBlueOffset[pf];
283 int y = (int)((double)srcBuf[rindex] * 0.299 +
284 (double)srcBuf[gindex] * 0.587 +
285 (double)srcBuf[bindex] * 0.114 + 0.5);
287 if (y > 255) y = 255;
289 dstBuf[rindex] = abs(dstBuf[rindex] - y);
290 dstBuf[gindex] = abs(dstBuf[gindex] - y);
291 dstBuf[bindex] = abs(dstBuf[bindex] - y);
295 for (row = 0; row < h; row++)
296 for (col = 0; col < w * ps; col++)
297 dstBuf[pitch * row + col] =
298 abs(dstBuf[pitch * row + col] - srcBuf[pitch * row + col]);
300 if (tjSaveImage(tempStr, dstBuf, w, 0, h, pf, flags) == -1)
301 THROW_TJG("saving bitmap");
305 if (file) fclose(file);
306 if (handle) tjDestroy(handle);
307 if (dstBufAlloc) free(dstBuf);
313 static int fullTest(unsigned char *srcBuf, int w, int h, int subsamp,
314 int jpegQual, char *fileName)
316 char tempStr[1024], tempStr2[80];
318 tjhandle handle = NULL;
319 unsigned char **jpegBuf = NULL, *yuvBuf = NULL, *tmpBuf = NULL, *srcPtr,
321 double start, elapsed, elapsedEncode;
322 int totalJpegSize = 0, row, col, i, tilew = w, tileh = h, retval = 0;
324 unsigned long *jpegSize = NULL, yuvSize = 0;
325 int ps = tjPixelSize[pf];
326 int ntilesw = 1, ntilesh = 1, pitch = w * ps;
327 const char *pfStr = pixFormatStr[pf];
329 if ((unsigned long long)pitch * (unsigned long long)h >
330 (unsigned long long)((size_t)-1))
331 THROW("allocating temporary image buffer", "Image is too large");
332 if ((tmpBuf = (unsigned char *)malloc((size_t)pitch * h)) == NULL)
333 THROW_UNIX("allocating temporary image buffer");
336 printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pfStr,
337 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down",
338 subNameLong[subsamp], jpegQual);
340 for (tilew = doTile ? 8 : w, tileh = doTile ? 8 : h; ;
341 tilew *= 2, tileh *= 2) {
342 if (tilew > w) tilew = w;
343 if (tileh > h) tileh = h;
344 ntilesw = (w + tilew - 1) / tilew;
345 ntilesh = (h + tileh - 1) / tileh;
347 if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
348 ntilesw * ntilesh)) == NULL)
349 THROW_UNIX("allocating JPEG tile array");
350 memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
351 if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
352 ntilesw * ntilesh)) == NULL)
353 THROW_UNIX("allocating JPEG size array");
354 memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
356 if ((flags & TJFLAG_NOREALLOC) != 0)
357 for (i = 0; i < ntilesw * ntilesh; i++) {
358 if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
359 THROW("getting buffer size", "Image is too large");
360 if ((jpegBuf[i] = (unsigned char *)
361 tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
362 THROW_UNIX("allocating JPEG tiles");
365 /* Compression test */
367 printf("%-4s (%s) %-5s %-3d ", pfStr,
368 (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", subNameLong[subsamp],
370 for (i = 0; i < h; i++)
371 memcpy(&tmpBuf[pitch * i], &srcBuf[w * ps * i], w * ps);
372 if ((handle = tjInitCompress()) == NULL)
373 THROW_TJ("executing tjInitCompress()");
376 yuvSize = tjBufSizeYUV2(tilew, yuvPad, tileh, subsamp);
377 if (yuvSize == (unsigned long)-1)
378 THROW_TJ("allocating YUV buffer");
379 if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
380 THROW_UNIX("allocating YUV buffer");
381 memset(yuvBuf, 127, yuvSize);
386 elapsed = elapsedEncode = 0.;
392 for (row = 0, srcPtr = srcBuf; row < ntilesh;
393 row++, srcPtr += pitch * tileh) {
394 for (col = 0, srcPtr2 = srcPtr; col < ntilesw;
395 col++, tile++, srcPtr2 += ps * tilew) {
396 int width = min(tilew, w - col * tilew);
397 int height = min(tileh, h - row * tileh);
400 double startEncode = getTime();
402 if (tjEncodeYUV3(handle, srcPtr2, width, pitch, height, pf, yuvBuf,
403 yuvPad, subsamp, flags) == -1)
404 THROW_TJ("executing tjEncodeYUV3()");
405 if (iter >= 0) elapsedEncode += getTime() - startEncode;
406 if (tjCompressFromYUV(handle, yuvBuf, width, yuvPad, height,
407 subsamp, &jpegBuf[tile], &jpegSize[tile],
408 jpegQual, flags) == -1)
409 THROW_TJ("executing tjCompressFromYUV()");
411 if (tjCompress2(handle, srcPtr2, width, pitch, height, pf,
412 &jpegBuf[tile], &jpegSize[tile], subsamp, jpegQual,
414 THROW_TJ("executing tjCompress2()");
416 totalJpegSize += jpegSize[tile];
419 elapsed += getTime() - start;
422 if (elapsed >= benchTime) break;
423 } else if (elapsed >= warmup) {
425 elapsed = elapsedEncode = 0.;
428 if (doYUV) elapsed -= elapsedEncode;
430 if (tjDestroy(handle) == -1) THROW_TJ("executing tjDestroy()");
433 if (quiet == 1) printf("%-5d %-5d ", tilew, tileh);
437 sigfig((double)(w * h) / 1000000. *
438 (double)iter / elapsedEncode, 4, tempStr, 1024),
439 quiet == 2 ? "\n" : " ");
441 sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
443 quiet == 2 ? "\n" : " ");
445 sigfig((double)(w * h * ps) / (double)totalJpegSize, 4, tempStr2,
447 quiet == 2 ? "\n" : " ");
449 printf("\n%s size: %d x %d\n", doTile ? "Tile" : "Image", tilew, tileh);
451 printf("Encode YUV --> Frame rate: %f fps\n",
452 (double)iter / elapsedEncode);
453 printf(" Output image size: %lu bytes\n", yuvSize);
454 printf(" Compression ratio: %f:1\n",
455 (double)(w * h * ps) / (double)yuvSize);
456 printf(" Throughput: %f Megapixels/sec\n",
457 (double)(w * h) / 1000000. * (double)iter / elapsedEncode);
458 printf(" Output bit stream: %f Megabits/sec\n",
459 (double)yuvSize * 8. / 1000000. * (double)iter / elapsedEncode);
461 printf("%s --> Frame rate: %f fps\n",
462 doYUV ? "Comp from YUV" : "Compress ",
463 (double)iter / elapsed);
464 printf(" Output image size: %d bytes\n",
466 printf(" Compression ratio: %f:1\n",
467 (double)(w * h * ps) / (double)totalJpegSize);
468 printf(" Throughput: %f Megapixels/sec\n",
469 (double)(w * h) / 1000000. * (double)iter / elapsed);
470 printf(" Output bit stream: %f Megabits/sec\n",
471 (double)totalJpegSize * 8. / 1000000. * (double)iter / elapsed);
473 if (tilew == w && tileh == h && doWrite) {
474 snprintf(tempStr, 1024, "%s_%s_Q%d.jpg", fileName, subName[subsamp],
476 if ((file = fopen(tempStr, "wb")) == NULL)
477 THROW_UNIX("opening reference image");
478 if (fwrite(jpegBuf[0], jpegSize[0], 1, file) != 1)
479 THROW_UNIX("writing reference image");
480 fclose(file); file = NULL;
481 if (!quiet) printf("Reference image written to %s\n", tempStr);
484 /* Decompression test */
486 if (decomp(srcBuf, jpegBuf, jpegSize, tmpBuf, w, h, subsamp, jpegQual,
487 fileName, tilew, tileh) == -1)
489 } else if (quiet == 1) printf("N/A\n");
491 for (i = 0; i < ntilesw * ntilesh; i++) {
495 free(jpegBuf); jpegBuf = NULL;
496 free(jpegSize); jpegSize = NULL;
498 free(yuvBuf); yuvBuf = NULL;
501 if (tilew == w && tileh == h) break;
505 if (file) fclose(file);
507 for (i = 0; i < ntilesw * ntilesh; i++)
514 if (handle) tjDestroy(handle);
519 static int decompTest(char *fileName)
522 tjhandle handle = NULL;
523 unsigned char **jpegBuf = NULL, *srcBuf = NULL;
524 unsigned long *jpegSize = NULL, srcSize, totalJpegSize;
525 tjtransform *t = NULL;
526 double start, elapsed;
527 int ps = tjPixelSize[pf], tile, row, col, i, iter, retval = 0, decompsrc = 0;
528 char *temp = NULL, tempStr[80], tempStr2[80];
530 int w = 0, h = 0, tilew, tileh, ntilesw = 1, ntilesh = 1, subsamp = -1,
532 /* Transformed image */
533 int tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;
535 if ((file = fopen(fileName, "rb")) == NULL)
536 THROW_UNIX("opening file");
537 if (fseek(file, 0, SEEK_END) < 0 ||
538 (srcSize = ftell(file)) == (unsigned long)-1)
539 THROW_UNIX("determining file size");
540 if ((srcBuf = (unsigned char *)malloc(srcSize)) == NULL)
541 THROW_UNIX("allocating memory");
542 if (fseek(file, 0, SEEK_SET) < 0)
543 THROW_UNIX("setting file position");
544 if (fread(srcBuf, srcSize, 1, file) < 1)
545 THROW_UNIX("reading JPEG data");
546 fclose(file); file = NULL;
548 temp = strrchr(fileName, '.');
549 if (temp != NULL) *temp = '\0';
551 if ((handle = tjInitTransform()) == NULL)
552 THROW_TJ("executing tjInitTransform()");
553 if (tjDecompressHeader3(handle, srcBuf, srcSize, &w, &h, &subsamp,
555 THROW_TJ("executing tjDecompressHeader3()");
557 THROW("reading JPEG header", "Invalid image dimensions");
558 if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
559 pf = TJPF_CMYK; ps = tjPixelSize[pf];
563 printf("All performance values in Mpixels/sec\n\n");
564 printf("Bitmap JPEG JPEG %s %s Xform Comp Decomp ",
565 doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
566 if (doYUV) printf("Decode");
568 printf("Format CS Subsamp Width Height Perf Ratio Perf ");
569 if (doYUV) printf("Perf");
572 printf(">>>>> JPEG %s --> %s (%s) <<<<<\n",
573 formatName(subsamp, cs, tempStr), pixFormatStr[pf],
574 (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down");
576 for (tilew = doTile ? 16 : w, tileh = doTile ? 16 : h; ;
577 tilew *= 2, tileh *= 2) {
578 if (tilew > w) tilew = w;
579 if (tileh > h) tileh = h;
580 ntilesw = (w + tilew - 1) / tilew;
581 ntilesh = (h + tileh - 1) / tileh;
583 if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
584 ntilesw * ntilesh)) == NULL)
585 THROW_UNIX("allocating JPEG tile array");
586 memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
587 if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
588 ntilesw * ntilesh)) == NULL)
589 THROW_UNIX("allocating JPEG size array");
590 memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
592 if ((flags & TJFLAG_NOREALLOC) != 0 &&
593 (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter))
594 for (i = 0; i < ntilesw * ntilesh; i++) {
595 if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
596 THROW("getting buffer size", "Image is too large");
597 if ((jpegBuf[i] = (unsigned char *)
598 tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
599 THROW_UNIX("allocating JPEG tiles");
602 tw = w; th = h; ttilew = tilew; ttileh = tileh;
604 printf("\n%s size: %d x %d", doTile ? "Tile" : "Image", ttilew, ttileh);
605 if (sf.num != 1 || sf.denom != 1)
606 printf(" --> %d x %d", TJSCALED(tw, sf), TJSCALED(th, sf));
608 } else if (quiet == 1) {
609 printf("%-4s (%s) %-5s %-5s ", pixFormatStr[pf],
610 (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", csName[cs],
611 subNameLong[subsamp]);
612 printf("%-5d %-5d ", tilew, tileh);
616 if (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter) {
617 if ((t = (tjtransform *)malloc(sizeof(tjtransform) * ntilesw *
619 THROW_UNIX("allocating image transform array");
621 if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
622 xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
623 tw = h; th = w; ttilew = tileh; ttileh = tilew;
626 if (xformOpt & TJXOPT_GRAY) tsubsamp = TJ_GRAYSCALE;
627 if (xformOp == TJXOP_HFLIP || xformOp == TJXOP_ROT180)
628 tw = tw - (tw % tjMCUWidth[tsubsamp]);
629 if (xformOp == TJXOP_VFLIP || xformOp == TJXOP_ROT180)
630 th = th - (th % tjMCUHeight[tsubsamp]);
631 if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT90)
632 tw = tw - (tw % tjMCUHeight[tsubsamp]);
633 if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT270)
634 th = th - (th % tjMCUWidth[tsubsamp]);
635 tntilesw = (tw + ttilew - 1) / ttilew;
636 tntilesh = (th + ttileh - 1) / ttileh;
638 if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
639 xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
640 if (tsubsamp == TJSAMP_422) tsubsamp = TJSAMP_440;
641 else if (tsubsamp == TJSAMP_440) tsubsamp = TJSAMP_422;
644 for (row = 0, tile = 0; row < tntilesh; row++) {
645 for (col = 0; col < tntilesw; col++, tile++) {
646 t[tile].r.w = min(ttilew, tw - col * ttilew);
647 t[tile].r.h = min(ttileh, th - row * ttileh);
648 t[tile].r.x = col * ttilew;
649 t[tile].r.y = row * ttileh;
650 t[tile].op = xformOp;
651 t[tile].options = xformOpt | TJXOPT_TRIM;
652 t[tile].customFilter = customFilter;
653 if (t[tile].options & TJXOPT_NOOUTPUT && jpegBuf[tile]) {
654 tjFree(jpegBuf[tile]); jpegBuf[tile] = NULL;
663 if (tjTransform(handle, srcBuf, srcSize, tntilesw * tntilesh, jpegBuf,
664 jpegSize, t, flags) == -1)
665 THROW_TJ("executing tjTransform()");
666 elapsed += getTime() - start;
669 if (elapsed >= benchTime) break;
670 } else if (elapsed >= warmup) {
678 for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
679 totalJpegSize += jpegSize[tile];
682 printf("%-6s%s%-6s%s",
683 sigfig((double)(w * h) / 1000000. / elapsed, 4, tempStr, 80),
684 quiet == 2 ? "\n" : " ",
685 sigfig((double)(w * h * ps) / (double)totalJpegSize, 4,
687 quiet == 2 ? "\n" : " ");
689 printf("Transform --> Frame rate: %f fps\n",
691 printf(" Output image size: %lu bytes\n",
693 printf(" Compression ratio: %f:1\n",
694 (double)(w * h * ps) / (double)totalJpegSize);
695 printf(" Throughput: %f Megapixels/sec\n",
696 (double)(w * h) / 1000000. / elapsed);
697 printf(" Output bit stream: %f Megabits/sec\n",
698 (double)totalJpegSize * 8. / 1000000. / elapsed);
701 if (quiet == 1) printf("N/A N/A ");
707 if (w == tilew) ttilew = tw;
708 if (h == tileh) ttileh = th;
709 if (!(xformOpt & TJXOPT_NOOUTPUT)) {
710 if (decomp(NULL, decompsrc ? &srcBuf : jpegBuf,
711 decompsrc ? &srcSize : jpegSize, NULL, tw, th, tsubsamp, 0,
712 fileName, ttilew, ttileh) == -1)
714 } else if (quiet == 1) printf("N/A\n");
716 for (i = 0; i < ntilesw * ntilesh; i++) {
720 free(jpegBuf); jpegBuf = NULL;
721 free(jpegSize); jpegSize = NULL;
723 if (tilew == w && tileh == h) break;
727 if (file) fclose(file);
729 for (i = 0; i < ntilesw * ntilesh; i++)
736 if (handle) { tjDestroy(handle); handle = NULL; }
741 static void usage(char *progName)
745 printf("USAGE: %s\n", progName);
746 printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
747 printf(" %s\n", progName);
748 printf(" <Inputfile (JPG)> [options]\n\n");
749 printf("Options:\n\n");
750 printf("-alloc = Dynamically allocate JPEG image buffers\n");
751 printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
752 printf("-bottomup = Test bottom-up compression/decompression\n");
753 printf("-tile = Test performance of the codec when the image is encoded as separate\n");
754 printf(" tiles of varying sizes.\n");
755 printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
756 printf(" Test the specified color conversion path in the codec (default = BGR)\n");
757 printf("-cmyk = Indirectly test YCCK JPEG compression/decompression (the source\n");
758 printf(" and destination bitmaps are still RGB. The conversion is done\n");
759 printf(" internally prior to compression or after decompression.)\n");
760 printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
761 printf(" the underlying codec\n");
762 printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
764 printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
765 printf(" underlying codec\n");
766 printf("-progressive = Use progressive entropy coding in JPEG images generated by\n");
767 printf(" compression and transform operations.\n");
768 printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
769 printf(" of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");
770 printf(" GRAY). The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
771 printf(" sequence.\n");
772 printf("-quiet = Output results in tabular rather than verbose format\n");
773 printf("-yuv = Test YUV encoding/decoding functions\n");
774 printf("-yuvpad <p> = If testing YUV encoding/decoding, this specifies the number of\n");
775 printf(" bytes to which each row of each plane in the intermediate YUV image is\n");
776 printf(" padded (default = 1)\n");
777 printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
778 printf(" factor of M/N (M/N = ");
779 for (i = 0; i < nsf; i++) {
780 printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
781 if (nsf == 2 && i != nsf - 1) printf(" or ");
783 if (i != nsf - 1) printf(", ");
784 if (i == nsf - 2) printf("or ");
786 if (i % 8 == 0 && i != 0) printf("\n ");
789 printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
790 printf(" Perform the corresponding lossless transform prior to\n");
791 printf(" decompression (these options are mutually exclusive)\n");
792 printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
793 printf(" test (can be combined with the other transforms above)\n");
794 printf("-copynone = Do not copy any extra markers (including EXIF and ICC profile data)\n");
795 printf(" when transforming the image.\n");
796 printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");
797 printf("-warmup <t> = Run each benchmark for <t> seconds (default = 1.0) prior to\n");
798 printf(" starting the timer, in order to prime the caches and thus improve the\n");
799 printf(" consistency of the results.\n");
800 printf("-componly = Stop after running compression tests. Do not test decompression.\n");
801 printf("-nowrite = Do not write reference or output images (improves consistency of\n");
802 printf(" performance measurements.)\n");
803 printf("-stoponwarning = Immediately discontinue the current\n");
804 printf(" compression/decompression/transform operation if the underlying codec\n");
805 printf(" throws a warning (non-fatal error)\n\n");
806 printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
807 printf("test will be performed for all quality values in the range.\n\n");
812 int main(int argc, char *argv[])
814 unsigned char *srcBuf = NULL;
815 int w = 0, h = 0, i, j, minQual = -1, maxQual = -1;
817 int minArg = 2, retval = 0, subsamp = -1;
819 if ((scalingFactors = tjGetScalingFactors(&nsf)) == NULL || nsf == 0)
820 THROW("executing tjGetScalingFactors()", tjGetErrorStr());
822 if (argc < minArg) usage(argv[0]);
824 temp = strrchr(argv[1], '.');
826 if (!strcasecmp(temp, ".bmp")) ext = "bmp";
827 if (!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg"))
835 if (argc < minArg) usage(argv[0]);
836 if ((minQual = atoi(argv[2])) < 1 || minQual > 100) {
837 puts("ERROR: Quality must be between 1 and 100.");
840 if ((temp = strchr(argv[2], '-')) != NULL && strlen(temp) > 1 &&
841 sscanf(&temp[1], "%d", &maxQual) == 1 && maxQual > minQual &&
842 maxQual >= 1 && maxQual <= 100) {}
843 else maxQual = minQual;
847 for (i = minArg; i < argc; i++) {
848 if (!strcasecmp(argv[i], "-tile")) {
849 doTile = 1; xformOpt |= TJXOPT_CROP;
850 } else if (!strcasecmp(argv[i], "-fastupsample")) {
851 printf("Using fast upsampling code\n\n");
852 flags |= TJFLAG_FASTUPSAMPLE;
853 } else if (!strcasecmp(argv[i], "-fastdct")) {
854 printf("Using fastest DCT/IDCT algorithm\n\n");
855 flags |= TJFLAG_FASTDCT;
856 } else if (!strcasecmp(argv[i], "-accuratedct")) {
857 printf("Using most accurate DCT/IDCT algorithm\n\n");
858 flags |= TJFLAG_ACCURATEDCT;
859 } else if (!strcasecmp(argv[i], "-progressive")) {
860 printf("Using progressive entropy coding\n\n");
861 flags |= TJFLAG_PROGRESSIVE;
862 } else if (!strcasecmp(argv[i], "-rgb"))
864 else if (!strcasecmp(argv[i], "-rgbx"))
866 else if (!strcasecmp(argv[i], "-bgr"))
868 else if (!strcasecmp(argv[i], "-bgrx"))
870 else if (!strcasecmp(argv[i], "-xbgr"))
872 else if (!strcasecmp(argv[i], "-xrgb"))
874 else if (!strcasecmp(argv[i], "-cmyk"))
876 else if (!strcasecmp(argv[i], "-bottomup"))
877 flags |= TJFLAG_BOTTOMUP;
878 else if (!strcasecmp(argv[i], "-quiet"))
880 else if (!strcasecmp(argv[i], "-qq"))
882 else if (!strcasecmp(argv[i], "-scale") && i < argc - 1) {
883 int temp1 = 0, temp2 = 0, match = 0;
885 if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) == 2) {
886 for (j = 0; j < nsf; j++) {
887 if ((double)temp1 / (double)temp2 ==
888 (double)scalingFactors[j].num /
889 (double)scalingFactors[j].denom) {
890 sf = scalingFactors[j];
894 if (!match) usage(argv[0]);
895 } else usage(argv[0]);
896 } else if (!strcasecmp(argv[i], "-hflip"))
897 xformOp = TJXOP_HFLIP;
898 else if (!strcasecmp(argv[i], "-vflip"))
899 xformOp = TJXOP_VFLIP;
900 else if (!strcasecmp(argv[i], "-transpose"))
901 xformOp = TJXOP_TRANSPOSE;
902 else if (!strcasecmp(argv[i], "-transverse"))
903 xformOp = TJXOP_TRANSVERSE;
904 else if (!strcasecmp(argv[i], "-rot90"))
905 xformOp = TJXOP_ROT90;
906 else if (!strcasecmp(argv[i], "-rot180"))
907 xformOp = TJXOP_ROT180;
908 else if (!strcasecmp(argv[i], "-rot270"))
909 xformOp = TJXOP_ROT270;
910 else if (!strcasecmp(argv[i], "-grayscale"))
911 xformOpt |= TJXOPT_GRAY;
912 else if (!strcasecmp(argv[i], "-custom"))
913 customFilter = dummyDCTFilter;
914 else if (!strcasecmp(argv[i], "-nooutput"))
915 xformOpt |= TJXOPT_NOOUTPUT;
916 else if (!strcasecmp(argv[i], "-copynone"))
917 xformOpt |= TJXOPT_COPYNONE;
918 else if (!strcasecmp(argv[i], "-benchtime") && i < argc - 1) {
919 double tempd = atof(argv[++i]);
921 if (tempd > 0.0) benchTime = tempd;
923 } else if (!strcasecmp(argv[i], "-warmup") && i < argc - 1) {
924 double tempd = atof(argv[++i]);
926 if (tempd >= 0.0) warmup = tempd;
928 printf("Warmup time = %.1f seconds\n\n", warmup);
929 } else if (!strcasecmp(argv[i], "-alloc"))
930 flags &= (~TJFLAG_NOREALLOC);
931 else if (!strcasecmp(argv[i], "-bmp"))
933 else if (!strcasecmp(argv[i], "-yuv")) {
934 printf("Testing YUV planar encoding/decoding\n\n");
936 } else if (!strcasecmp(argv[i], "-yuvpad") && i < argc - 1) {
937 int tempi = atoi(argv[++i]);
939 if (tempi >= 1) yuvPad = tempi;
940 } else if (!strcasecmp(argv[i], "-subsamp") && i < argc - 1) {
942 if (toupper(argv[i][0]) == 'G') subsamp = TJSAMP_GRAY;
944 int tempi = atoi(argv[i]);
947 case 444: subsamp = TJSAMP_444; break;
948 case 422: subsamp = TJSAMP_422; break;
949 case 440: subsamp = TJSAMP_440; break;
950 case 420: subsamp = TJSAMP_420; break;
951 case 411: subsamp = TJSAMP_411; break;
954 } else if (!strcasecmp(argv[i], "-componly"))
956 else if (!strcasecmp(argv[i], "-nowrite"))
958 else if (!strcasecmp(argv[i], "-stoponwarning"))
959 flags |= TJFLAG_STOPONWARNING;
964 if ((sf.num != 1 || sf.denom != 1) && doTile) {
965 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
966 printf("work when scaled decompression is enabled.\n");
970 if ((flags & TJFLAG_NOREALLOC) == 0 && doTile) {
971 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
972 printf("work when dynamic JPEG buffer allocation is enabled.\n\n");
977 if ((srcBuf = tjLoadImage(argv[1], &w, 1, &h, &pf, flags)) == NULL)
978 THROW_TJG("loading bitmap");
979 temp = strrchr(argv[1], '.');
980 if (temp != NULL) *temp = '\0';
983 if (quiet == 1 && !decompOnly) {
984 printf("All performance values in Mpixels/sec\n\n");
985 printf("Bitmap JPEG JPEG %s %s ",
986 doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
987 if (doYUV) printf("Encode ");
988 printf("Comp Comp Decomp ");
989 if (doYUV) printf("Decode");
991 printf("Format Subsamp Qual Width Height ");
992 if (doYUV) printf("Perf ");
993 printf("Perf Ratio Perf ");
994 if (doYUV) printf("Perf");
1003 if (subsamp >= 0 && subsamp < TJ_NUMSAMP) {
1004 for (i = maxQual; i >= minQual; i--)
1005 fullTest(srcBuf, w, h, subsamp, i, argv[1]);
1008 if (pf != TJPF_CMYK) {
1009 for (i = maxQual; i >= minQual; i--)
1010 fullTest(srcBuf, w, h, TJSAMP_GRAY, i, argv[1]);
1013 for (i = maxQual; i >= minQual; i--)
1014 fullTest(srcBuf, w, h, TJSAMP_420, i, argv[1]);
1016 for (i = maxQual; i >= minQual; i--)
1017 fullTest(srcBuf, w, h, TJSAMP_422, i, argv[1]);
1019 for (i = maxQual; i >= minQual; i--)
1020 fullTest(srcBuf, w, h, TJSAMP_444, i, argv[1]);