tjbench.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright (C)2009-2018 D. R. Commander. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. * - Neither the name of the libjpeg-turbo Project nor the names of its
  13. * contributors may be used to endorse or promote products derived from this
  14. * software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <math.h>
  33. #include <errno.h>
  34. #include <cdjpeg.h>
  35. #include "./tjutil.h"
  36. #include "./turbojpeg.h"
  37. #define _throw(op, err) { \
  38. printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
  39. retval = -1; goto bailout; \
  40. }
  41. #define _throwunix(m) _throw(m, strerror(errno))
  42. char tjErrorStr[JMSG_LENGTH_MAX] = "\0", tjErrorMsg[JMSG_LENGTH_MAX] = "\0";
  43. int tjErrorLine = -1, tjErrorCode = -1;
  44. #define _throwtjg(m) { \
  45. printf("ERROR in line %d while %s:\n%s\n", __LINE__, m, \
  46. tjGetErrorStr2(NULL)); \
  47. retval = -1; goto bailout; \
  48. }
  49. #define _throwtj(m) { \
  50. int _tjErrorCode = tjGetErrorCode(handle); \
  51. char *_tjErrorStr = tjGetErrorStr2(handle); \
  52. \
  53. if (!(flags & TJFLAG_STOPONWARNING) && _tjErrorCode == TJERR_WARNING) { \
  54. if (strncmp(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX) || \
  55. strncmp(tjErrorMsg, m, JMSG_LENGTH_MAX) || \
  56. tjErrorCode != _tjErrorCode || tjErrorLine != __LINE__) { \
  57. strncpy(tjErrorStr, _tjErrorStr, JMSG_LENGTH_MAX); \
  58. strncpy(tjErrorMsg, m, JMSG_LENGTH_MAX); \
  59. tjErrorCode = _tjErrorCode; \
  60. tjErrorLine = __LINE__; \
  61. printf("WARNING in line %d while %s:\n%s\n", __LINE__, m, _tjErrorStr); \
  62. } \
  63. } else { \
  64. printf("%s in line %d while %s:\n%s\n", \
  65. _tjErrorCode == TJERR_WARNING ? "WARNING" : "ERROR", __LINE__, m, \
  66. _tjErrorStr); \
  67. retval = -1; goto bailout; \
  68. } \
  69. }
  70. int flags = TJFLAG_NOREALLOC, compOnly = 0, decompOnly = 0, doYUV = 0,
  71. quiet = 0, doTile = 0, pf = TJPF_BGR, yuvPad = 1, doWrite = 1;
  72. char *ext = "ppm";
  73. const char *pixFormatStr[TJ_NUMPF] = {
  74. "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
  75. };
  76. const char *subNameLong[TJ_NUMSAMP] = {
  77. "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
  78. };
  79. const char *csName[TJ_NUMCS] = {
  80. "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
  81. };
  82. const char *subName[TJ_NUMSAMP] = {
  83. "444", "422", "420", "GRAY", "440", "411"
  84. };
  85. tjscalingfactor *scalingFactors = NULL, sf = { 1, 1 };
  86. int nsf = 0, xformOp = TJXOP_NONE, xformOpt = 0;
  87. int (*customFilter) (short *, tjregion, tjregion, int, int, tjtransform *);
  88. double benchTime = 5.0, warmup = 1.0;
  89. char *formatName(int subsamp, int cs, char *buf)
  90. {
  91. if (cs == TJCS_YCbCr)
  92. return (char *)subNameLong[subsamp];
  93. else if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
  94. snprintf(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
  95. return buf;
  96. } else
  97. return (char *)csName[cs];
  98. }
  99. char *sigfig(double val, int figs, char *buf, int len)
  100. {
  101. char format[80];
  102. int digitsAfterDecimal = figs - (int)ceil(log10(fabs(val)));
  103. if (digitsAfterDecimal < 1)
  104. snprintf(format, 80, "%%.0f");
  105. else
  106. snprintf(format, 80, "%%.%df", digitsAfterDecimal);
  107. snprintf(buf, len, format, val);
  108. return buf;
  109. }
  110. /* Custom DCT filter which produces a negative of the image */
  111. int dummyDCTFilter(short *coeffs, tjregion arrayRegion, tjregion planeRegion,
  112. int componentIndex, int transformIndex,
  113. tjtransform *transform)
  114. {
  115. int i;
  116. for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
  117. coeffs[i] = -coeffs[i];
  118. return 0;
  119. }
  120. /* Decompression test */
  121. int decomp(unsigned char *srcBuf, unsigned char **jpegBuf,
  122. unsigned long *jpegSize, unsigned char *dstBuf, int w, int h,
  123. int subsamp, int jpegQual, char *fileName, int tilew, int tileh)
  124. {
  125. char tempStr[1024], sizeStr[20] = "\0", qualStr[6] = "\0", *ptr;
  126. FILE *file = NULL;
  127. tjhandle handle = NULL;
  128. int row, col, iter = 0, dstBufAlloc = 0, retval = 0;
  129. double elapsed, elapsedDecode;
  130. int ps = tjPixelSize[pf];
  131. int scaledw = TJSCALED(w, sf);
  132. int scaledh = TJSCALED(h, sf);
  133. int pitch = scaledw * ps;
  134. int ntilesw = (w + tilew - 1) / tilew, ntilesh = (h + tileh - 1) / tileh;
  135. unsigned char *dstPtr, *dstPtr2, *yuvBuf = NULL;
  136. if (jpegQual > 0) {
  137. snprintf(qualStr, 6, "_Q%d", jpegQual);
  138. qualStr[5] = 0;
  139. }
  140. if ((handle = tjInitDecompress()) == NULL)
  141. _throwtj("executing tjInitDecompress()");
  142. if (dstBuf == NULL) {
  143. if ((dstBuf = (unsigned char *)malloc(pitch * scaledh)) == NULL)
  144. _throwunix("allocating destination buffer");
  145. dstBufAlloc = 1;
  146. }
  147. /* Set the destination buffer to gray so we know whether the decompressor
  148. attempted to write to it */
  149. memset(dstBuf, 127, pitch * scaledh);
  150. if (doYUV) {
  151. int width = doTile ? tilew : scaledw;
  152. int height = doTile ? tileh : scaledh;
  153. int yuvSize = tjBufSizeYUV2(width, yuvPad, height, subsamp);
  154. if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
  155. _throwunix("allocating YUV buffer");
  156. memset(yuvBuf, 127, yuvSize);
  157. }
  158. /* Benchmark */
  159. iter = -1;
  160. elapsed = elapsedDecode = 0.;
  161. while (1) {
  162. int tile = 0;
  163. double start = getTime();
  164. for (row = 0, dstPtr = dstBuf; row < ntilesh;
  165. row++, dstPtr += pitch * tileh) {
  166. for (col = 0, dstPtr2 = dstPtr; col < ntilesw;
  167. col++, tile++, dstPtr2 += ps * tilew) {
  168. int width = doTile ? min(tilew, w - col * tilew) : scaledw;
  169. int height = doTile ? min(tileh, h - row * tileh) : scaledh;
  170. if (doYUV) {
  171. double startDecode;
  172. if (tjDecompressToYUV2(handle, jpegBuf[tile], jpegSize[tile], yuvBuf,
  173. width, yuvPad, height, flags) == -1)
  174. _throwtj("executing tjDecompressToYUV2()");
  175. startDecode = getTime();
  176. if (tjDecodeYUV(handle, yuvBuf, yuvPad, subsamp, dstPtr2, width,
  177. pitch, height, pf, flags) == -1)
  178. _throwtj("executing tjDecodeYUV()");
  179. if (iter >= 0) elapsedDecode += getTime() - startDecode;
  180. } else if (tjDecompress2(handle, jpegBuf[tile], jpegSize[tile],
  181. dstPtr2, width, pitch, height, pf,
  182. flags) == -1)
  183. _throwtj("executing tjDecompress2()");
  184. }
  185. }
  186. elapsed += getTime() - start;
  187. if (iter >= 0) {
  188. iter++;
  189. if (elapsed >= benchTime) break;
  190. } else if (elapsed >= warmup) {
  191. iter = 0;
  192. elapsed = elapsedDecode = 0.;
  193. }
  194. }
  195. if (doYUV) elapsed -= elapsedDecode;
  196. if (tjDestroy(handle) == -1) _throwtj("executing tjDestroy()");
  197. handle = NULL;
  198. if (quiet) {
  199. printf("%-6s%s",
  200. sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
  201. tempStr, 1024),
  202. quiet == 2 ? "\n" : " ");
  203. if (doYUV)
  204. printf("%s\n",
  205. sigfig((double)(w * h) / 1000000. * (double)iter / elapsedDecode,
  206. 4, tempStr, 1024));
  207. else if (quiet != 2) printf("\n");
  208. } else {
  209. printf("%s --> Frame rate: %f fps\n",
  210. doYUV ? "Decomp to YUV" : "Decompress ", (double)iter / elapsed);
  211. printf(" Throughput: %f Megapixels/sec\n",
  212. (double)(w * h) / 1000000. * (double)iter / elapsed);
  213. if (doYUV) {
  214. printf("YUV Decode --> Frame rate: %f fps\n",
  215. (double)iter / elapsedDecode);
  216. printf(" Throughput: %f Megapixels/sec\n",
  217. (double)(w * h) / 1000000. * (double)iter / elapsedDecode);
  218. }
  219. }
  220. if (!doWrite) goto bailout;
  221. if (sf.num != 1 || sf.denom != 1)
  222. snprintf(sizeStr, 20, "%d_%d", sf.num, sf.denom);
  223. else if (tilew != w || tileh != h)
  224. snprintf(sizeStr, 20, "%dx%d", tilew, tileh);
  225. else snprintf(sizeStr, 20, "full");
  226. if (decompOnly)
  227. snprintf(tempStr, 1024, "%s_%s.%s", fileName, sizeStr, ext);
  228. else
  229. snprintf(tempStr, 1024, "%s_%s%s_%s.%s", fileName, subName[subsamp],
  230. qualStr, sizeStr, ext);
  231. if (tjSaveImage(tempStr, dstBuf, scaledw, 0, scaledh, pf, flags) == -1)
  232. _throwtjg("saving bitmap");
  233. ptr = strrchr(tempStr, '.');
  234. snprintf(ptr, 1024 - (ptr - tempStr), "-err.%s", ext);
  235. if (srcBuf && sf.num == 1 && sf.denom == 1) {
  236. if (!quiet) printf("Compression error written to %s.\n", tempStr);
  237. if (subsamp == TJ_GRAYSCALE) {
  238. int index, index2;
  239. for (row = 0, index = 0; row < h; row++, index += pitch) {
  240. for (col = 0, index2 = index; col < w; col++, index2 += ps) {
  241. int rindex = index2 + tjRedOffset[pf];
  242. int gindex = index2 + tjGreenOffset[pf];
  243. int bindex = index2 + tjBlueOffset[pf];
  244. int y = (int)((double)srcBuf[rindex] * 0.299 +
  245. (double)srcBuf[gindex] * 0.587 +
  246. (double)srcBuf[bindex] * 0.114 + 0.5);
  247. if (y > 255) y = 255;
  248. if (y < 0) y = 0;
  249. dstBuf[rindex] = abs(dstBuf[rindex] - y);
  250. dstBuf[gindex] = abs(dstBuf[gindex] - y);
  251. dstBuf[bindex] = abs(dstBuf[bindex] - y);
  252. }
  253. }
  254. } else {
  255. for (row = 0; row < h; row++)
  256. for (col = 0; col < w * ps; col++)
  257. dstBuf[pitch * row + col] =
  258. abs(dstBuf[pitch * row + col] - srcBuf[pitch * row + col]);
  259. }
  260. if (tjSaveImage(tempStr, dstBuf, w, 0, h, pf, flags) == -1)
  261. _throwtjg("saving bitmap");
  262. }
  263. bailout:
  264. if (file) fclose(file);
  265. if (handle) tjDestroy(handle);
  266. if (dstBuf && dstBufAlloc) free(dstBuf);
  267. if (yuvBuf) free(yuvBuf);
  268. return retval;
  269. }
  270. int fullTest(unsigned char *srcBuf, int w, int h, int subsamp, int jpegQual,
  271. char *fileName)
  272. {
  273. char tempStr[1024], tempStr2[80];
  274. FILE *file = NULL;
  275. tjhandle handle = NULL;
  276. unsigned char **jpegBuf = NULL, *yuvBuf = NULL, *tmpBuf = NULL, *srcPtr,
  277. *srcPtr2;
  278. double start, elapsed, elapsedEncode;
  279. int totalJpegSize = 0, row, col, i, tilew = w, tileh = h, retval = 0;
  280. int iter, yuvSize = 0;
  281. unsigned long *jpegSize = NULL;
  282. int ps = tjPixelSize[pf];
  283. int ntilesw = 1, ntilesh = 1, pitch = w * ps;
  284. const char *pfStr = pixFormatStr[pf];
  285. if ((tmpBuf = (unsigned char *)malloc(pitch * h)) == NULL)
  286. _throwunix("allocating temporary image buffer");
  287. if (!quiet)
  288. printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pfStr,
  289. (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down",
  290. subNameLong[subsamp], jpegQual);
  291. for (tilew = doTile ? 8 : w, tileh = doTile ? 8 : h; ;
  292. tilew *= 2, tileh *= 2) {
  293. if (tilew > w) tilew = w;
  294. if (tileh > h) tileh = h;
  295. ntilesw = (w + tilew - 1) / tilew;
  296. ntilesh = (h + tileh - 1) / tileh;
  297. if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
  298. ntilesw * ntilesh)) == NULL)
  299. _throwunix("allocating JPEG tile array");
  300. memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
  301. if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
  302. ntilesw * ntilesh)) == NULL)
  303. _throwunix("allocating JPEG size array");
  304. memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
  305. if ((flags & TJFLAG_NOREALLOC) != 0)
  306. for (i = 0; i < ntilesw * ntilesh; i++) {
  307. if ((jpegBuf[i] = (unsigned char *)
  308. tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
  309. _throwunix("allocating JPEG tiles");
  310. }
  311. /* Compression test */
  312. if (quiet == 1)
  313. printf("%-4s (%s) %-5s %-3d ", pfStr,
  314. (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", subNameLong[subsamp],
  315. jpegQual);
  316. for (i = 0; i < h; i++)
  317. memcpy(&tmpBuf[pitch * i], &srcBuf[w * ps * i], w * ps);
  318. if ((handle = tjInitCompress()) == NULL)
  319. _throwtj("executing tjInitCompress()");
  320. if (doYUV) {
  321. yuvSize = tjBufSizeYUV2(tilew, yuvPad, tileh, subsamp);
  322. if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
  323. _throwunix("allocating YUV buffer");
  324. memset(yuvBuf, 127, yuvSize);
  325. }
  326. /* Benchmark */
  327. iter = -1;
  328. elapsed = elapsedEncode = 0.;
  329. while (1) {
  330. int tile = 0;
  331. totalJpegSize = 0;
  332. start = getTime();
  333. for (row = 0, srcPtr = srcBuf; row < ntilesh;
  334. row++, srcPtr += pitch * tileh) {
  335. for (col = 0, srcPtr2 = srcPtr; col < ntilesw;
  336. col++, tile++, srcPtr2 += ps * tilew) {
  337. int width = min(tilew, w - col * tilew);
  338. int height = min(tileh, h - row * tileh);
  339. if (doYUV) {
  340. double startEncode = getTime();
  341. if (tjEncodeYUV3(handle, srcPtr2, width, pitch, height, pf, yuvBuf,
  342. yuvPad, subsamp, flags) == -1)
  343. _throwtj("executing tjEncodeYUV3()");
  344. if (iter >= 0) elapsedEncode += getTime() - startEncode;
  345. if (tjCompressFromYUV(handle, yuvBuf, width, yuvPad, height,
  346. subsamp, &jpegBuf[tile], &jpegSize[tile],
  347. jpegQual, flags) == -1)
  348. _throwtj("executing tjCompressFromYUV()");
  349. } else {
  350. if (tjCompress2(handle, srcPtr2, width, pitch, height, pf,
  351. &jpegBuf[tile], &jpegSize[tile], subsamp, jpegQual,
  352. flags) == -1)
  353. _throwtj("executing tjCompress2()");
  354. }
  355. totalJpegSize += jpegSize[tile];
  356. }
  357. }
  358. elapsed += getTime() - start;
  359. if (iter >= 0) {
  360. iter++;
  361. if (elapsed >= benchTime) break;
  362. } else if (elapsed >= warmup) {
  363. iter = 0;
  364. elapsed = elapsedEncode = 0.;
  365. }
  366. }
  367. if (doYUV) elapsed -= elapsedEncode;
  368. if (tjDestroy(handle) == -1) _throwtj("executing tjDestroy()");
  369. handle = NULL;
  370. if (quiet == 1) printf("%-5d %-5d ", tilew, tileh);
  371. if (quiet) {
  372. if (doYUV)
  373. printf("%-6s%s",
  374. sigfig((double)(w * h) / 1000000. *
  375. (double)iter / elapsedEncode, 4, tempStr, 1024),
  376. quiet == 2 ? "\n" : " ");
  377. printf("%-6s%s",
  378. sigfig((double)(w * h) / 1000000. * (double)iter / elapsed, 4,
  379. tempStr, 1024),
  380. quiet == 2 ? "\n" : " ");
  381. printf("%-6s%s",
  382. sigfig((double)(w * h * ps) / (double)totalJpegSize, 4, tempStr2,
  383. 80),
  384. quiet == 2 ? "\n" : " ");
  385. } else {
  386. printf("\n%s size: %d x %d\n", doTile ? "Tile" : "Image", tilew, tileh);
  387. if (doYUV) {
  388. printf("Encode YUV --> Frame rate: %f fps\n",
  389. (double)iter / elapsedEncode);
  390. printf(" Output image size: %d bytes\n", yuvSize);
  391. printf(" Compression ratio: %f:1\n",
  392. (double)(w * h * ps) / (double)yuvSize);
  393. printf(" Throughput: %f Megapixels/sec\n",
  394. (double)(w * h) / 1000000. * (double)iter / elapsedEncode);
  395. printf(" Output bit stream: %f Megabits/sec\n",
  396. (double)yuvSize * 8. / 1000000. * (double)iter / elapsedEncode);
  397. }
  398. printf("%s --> Frame rate: %f fps\n",
  399. doYUV ? "Comp from YUV" : "Compress ",
  400. (double)iter / elapsed);
  401. printf(" Output image size: %d bytes\n",
  402. totalJpegSize);
  403. printf(" Compression ratio: %f:1\n",
  404. (double)(w * h * ps) / (double)totalJpegSize);
  405. printf(" Throughput: %f Megapixels/sec\n",
  406. (double)(w * h) / 1000000. * (double)iter / elapsed);
  407. printf(" Output bit stream: %f Megabits/sec\n",
  408. (double)totalJpegSize * 8. / 1000000. * (double)iter / elapsed);
  409. }
  410. if (tilew == w && tileh == h && doWrite) {
  411. snprintf(tempStr, 1024, "%s_%s_Q%d.jpg", fileName, subName[subsamp],
  412. jpegQual);
  413. if ((file = fopen(tempStr, "wb")) == NULL)
  414. _throwunix("opening reference image");
  415. if (fwrite(jpegBuf[0], jpegSize[0], 1, file) != 1)
  416. _throwunix("writing reference image");
  417. fclose(file); file = NULL;
  418. if (!quiet) printf("Reference image written to %s\n", tempStr);
  419. }
  420. /* Decompression test */
  421. if (!compOnly) {
  422. if (decomp(srcBuf, jpegBuf, jpegSize, tmpBuf, w, h, subsamp, jpegQual,
  423. fileName, tilew, tileh) == -1)
  424. goto bailout;
  425. }
  426. for (i = 0; i < ntilesw * ntilesh; i++) {
  427. if (jpegBuf[i]) tjFree(jpegBuf[i]);
  428. jpegBuf[i] = NULL;
  429. }
  430. free(jpegBuf); jpegBuf = NULL;
  431. free(jpegSize); jpegSize = NULL;
  432. if (doYUV) {
  433. free(yuvBuf); yuvBuf = NULL;
  434. }
  435. if (tilew == w && tileh == h) break;
  436. }
  437. bailout:
  438. if (file) { fclose(file); file = NULL; }
  439. if (jpegBuf) {
  440. for (i = 0; i < ntilesw * ntilesh; i++) {
  441. if (jpegBuf[i]) tjFree(jpegBuf[i]);
  442. jpegBuf[i] = NULL;
  443. }
  444. free(jpegBuf); jpegBuf = NULL;
  445. }
  446. if (yuvBuf) { free(yuvBuf); yuvBuf = NULL; }
  447. if (jpegSize) { free(jpegSize); jpegSize = NULL; }
  448. if (tmpBuf) { free(tmpBuf); tmpBuf = NULL; }
  449. if (handle) { tjDestroy(handle); handle = NULL; }
  450. return retval;
  451. }
  452. int decompTest(char *fileName)
  453. {
  454. FILE *file = NULL;
  455. tjhandle handle = NULL;
  456. unsigned char **jpegBuf = NULL, *srcBuf = NULL;
  457. unsigned long *jpegSize = NULL, srcSize, totalJpegSize;
  458. tjtransform *t = NULL;
  459. double start, elapsed;
  460. int ps = tjPixelSize[pf], tile, row, col, i, iter, retval = 0, decompsrc = 0;
  461. char *temp = NULL, tempStr[80], tempStr2[80];
  462. /* Original image */
  463. int w = 0, h = 0, tilew, tileh, ntilesw = 1, ntilesh = 1, subsamp = -1,
  464. cs = -1;
  465. /* Transformed image */
  466. int tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;
  467. if ((file = fopen(fileName, "rb")) == NULL)
  468. _throwunix("opening file");
  469. if (fseek(file, 0, SEEK_END) < 0 ||
  470. (srcSize = ftell(file)) == (unsigned long)-1)
  471. _throwunix("determining file size");
  472. if ((srcBuf = (unsigned char *)malloc(srcSize)) == NULL)
  473. _throwunix("allocating memory");
  474. if (fseek(file, 0, SEEK_SET) < 0)
  475. _throwunix("setting file position");
  476. if (fread(srcBuf, srcSize, 1, file) < 1)
  477. _throwunix("reading JPEG data");
  478. fclose(file); file = NULL;
  479. temp = strrchr(fileName, '.');
  480. if (temp != NULL) *temp = '\0';
  481. if ((handle = tjInitTransform()) == NULL)
  482. _throwtj("executing tjInitTransform()");
  483. if (tjDecompressHeader3(handle, srcBuf, srcSize, &w, &h, &subsamp,
  484. &cs) == -1)
  485. _throwtj("executing tjDecompressHeader3()");
  486. if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
  487. pf = TJPF_CMYK; ps = tjPixelSize[pf];
  488. }
  489. if (quiet == 1) {
  490. printf("All performance values in Mpixels/sec\n\n");
  491. printf("Bitmap JPEG JPEG %s %s Xform Comp Decomp ",
  492. doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
  493. if (doYUV) printf("Decode");
  494. printf("\n");
  495. printf("Format CS Subsamp Width Height Perf Ratio Perf ");
  496. if (doYUV) printf("Perf");
  497. printf("\n\n");
  498. } else if (!quiet)
  499. printf(">>>>> JPEG %s --> %s (%s) <<<<<\n",
  500. formatName(subsamp, cs, tempStr), pixFormatStr[pf],
  501. (flags & TJFLAG_BOTTOMUP) ? "Bottom-up" : "Top-down");
  502. for (tilew = doTile ? 16 : w, tileh = doTile ? 16 : h; ;
  503. tilew *= 2, tileh *= 2) {
  504. if (tilew > w) tilew = w;
  505. if (tileh > h) tileh = h;
  506. ntilesw = (w + tilew - 1) / tilew;
  507. ntilesh = (h + tileh - 1) / tileh;
  508. if ((jpegBuf = (unsigned char **)malloc(sizeof(unsigned char *) *
  509. ntilesw * ntilesh)) == NULL)
  510. _throwunix("allocating JPEG tile array");
  511. memset(jpegBuf, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
  512. if ((jpegSize = (unsigned long *)malloc(sizeof(unsigned long) *
  513. ntilesw * ntilesh)) == NULL)
  514. _throwunix("allocating JPEG size array");
  515. memset(jpegSize, 0, sizeof(unsigned long) * ntilesw * ntilesh);
  516. if ((flags & TJFLAG_NOREALLOC) != 0 || !doTile)
  517. for (i = 0; i < ntilesw * ntilesh; i++) {
  518. if ((jpegBuf[i] = (unsigned char *)
  519. tjAlloc(tjBufSize(tilew, tileh, subsamp))) == NULL)
  520. _throwunix("allocating JPEG tiles");
  521. }
  522. tw = w; th = h; ttilew = tilew; ttileh = tileh;
  523. if (!quiet) {
  524. printf("\n%s size: %d x %d", doTile ? "Tile" : "Image", ttilew, ttileh);
  525. if (sf.num != 1 || sf.denom != 1)
  526. printf(" --> %d x %d", TJSCALED(tw, sf), TJSCALED(th, sf));
  527. printf("\n");
  528. } else if (quiet == 1) {
  529. printf("%-4s (%s) %-5s %-5s ", pixFormatStr[pf],
  530. (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD", csName[cs],
  531. subNameLong[subsamp]);
  532. printf("%-5d %-5d ", tilew, tileh);
  533. }
  534. tsubsamp = subsamp;
  535. if (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter) {
  536. if ((t = (tjtransform *)malloc(sizeof(tjtransform) * ntilesw *
  537. ntilesh)) == NULL)
  538. _throwunix("allocating image transform array");
  539. if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
  540. xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
  541. tw = h; th = w; ttilew = tileh; ttileh = tilew;
  542. }
  543. if (xformOpt & TJXOPT_GRAY) tsubsamp = TJ_GRAYSCALE;
  544. if (xformOp == TJXOP_HFLIP || xformOp == TJXOP_ROT180)
  545. tw = tw - (tw % tjMCUWidth[tsubsamp]);
  546. if (xformOp == TJXOP_VFLIP || xformOp == TJXOP_ROT180)
  547. th = th - (th % tjMCUHeight[tsubsamp]);
  548. if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT90)
  549. tw = tw - (tw % tjMCUHeight[tsubsamp]);
  550. if (xformOp == TJXOP_TRANSVERSE || xformOp == TJXOP_ROT270)
  551. th = th - (th % tjMCUWidth[tsubsamp]);
  552. tntilesw = (tw + ttilew - 1) / ttilew;
  553. tntilesh = (th + ttileh - 1) / ttileh;
  554. if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
  555. xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
  556. if (tsubsamp == TJSAMP_422) tsubsamp = TJSAMP_440;
  557. else if (tsubsamp == TJSAMP_440) tsubsamp = TJSAMP_422;
  558. }
  559. for (row = 0, tile = 0; row < tntilesh; row++) {
  560. for (col = 0; col < tntilesw; col++, tile++) {
  561. t[tile].r.w = min(ttilew, tw - col * ttilew);
  562. t[tile].r.h = min(ttileh, th - row * ttileh);
  563. t[tile].r.x = col * ttilew;
  564. t[tile].r.y = row * ttileh;
  565. t[tile].op = xformOp;
  566. t[tile].options = xformOpt | TJXOPT_TRIM;
  567. t[tile].customFilter = customFilter;
  568. if (t[tile].options & TJXOPT_NOOUTPUT && jpegBuf[tile]) {
  569. tjFree(jpegBuf[tile]); jpegBuf[tile] = NULL;
  570. }
  571. }
  572. }
  573. iter = -1;
  574. elapsed = 0.;
  575. while (1) {
  576. start = getTime();
  577. if (tjTransform(handle, srcBuf, srcSize, tntilesw * tntilesh, jpegBuf,
  578. jpegSize, t, flags) == -1)
  579. _throwtj("executing tjTransform()");
  580. elapsed += getTime() - start;
  581. if (iter >= 0) {
  582. iter++;
  583. if (elapsed >= benchTime) break;
  584. } else if (elapsed >= warmup) {
  585. iter = 0;
  586. elapsed = 0.;
  587. }
  588. }
  589. free(t); t = NULL;
  590. for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
  591. totalJpegSize += jpegSize[tile];
  592. if (quiet) {
  593. printf("%-6s%s%-6s%s",
  594. sigfig((double)(w * h) / 1000000. / elapsed, 4, tempStr, 80),
  595. quiet == 2 ? "\n" : " ",
  596. sigfig((double)(w * h * ps) / (double)totalJpegSize, 4,
  597. tempStr2, 80),
  598. quiet == 2 ? "\n" : " ");
  599. } else if (!quiet) {
  600. printf("Transform --> Frame rate: %f fps\n",
  601. 1.0 / elapsed);
  602. printf(" Output image size: %lu bytes\n",
  603. totalJpegSize);
  604. printf(" Compression ratio: %f:1\n",
  605. (double)(w * h * ps) / (double)totalJpegSize);
  606. printf(" Throughput: %f Megapixels/sec\n",
  607. (double)(w * h) / 1000000. / elapsed);
  608. printf(" Output bit stream: %f Megabits/sec\n",
  609. (double)totalJpegSize * 8. / 1000000. / elapsed);
  610. }
  611. } else {
  612. if (quiet == 1) printf("N/A N/A ");
  613. tjFree(jpegBuf[0]);
  614. jpegBuf[0] = NULL;
  615. decompsrc = 1;
  616. }
  617. if (w == tilew) ttilew = tw;
  618. if (h == tileh) ttileh = th;
  619. if (!(xformOpt & TJXOPT_NOOUTPUT)) {
  620. if (decomp(NULL, decompsrc ? &srcBuf : jpegBuf,
  621. decompsrc ? &srcSize : jpegSize, NULL, tw, th, tsubsamp, 0,
  622. fileName, ttilew, ttileh) == -1)
  623. goto bailout;
  624. } else if (quiet == 1) printf("N/A\n");
  625. for (i = 0; i < ntilesw * ntilesh; i++) {
  626. tjFree(jpegBuf[i]); jpegBuf[i] = NULL;
  627. }
  628. free(jpegBuf); jpegBuf = NULL;
  629. if (jpegSize) { free(jpegSize); jpegSize = NULL; }
  630. if (tilew == w && tileh == h) break;
  631. }
  632. bailout:
  633. if (file) { fclose(file); file = NULL; }
  634. if (jpegBuf) {
  635. for (i = 0; i < ntilesw * ntilesh; i++) {
  636. if (jpegBuf[i]) tjFree(jpegBuf[i]);
  637. jpegBuf[i] = NULL;
  638. }
  639. free(jpegBuf); jpegBuf = NULL;
  640. }
  641. if (jpegSize) { free(jpegSize); jpegSize = NULL; }
  642. if (srcBuf) { free(srcBuf); srcBuf = NULL; }
  643. if (t) { free(t); t = NULL; }
  644. if (handle) { tjDestroy(handle); handle = NULL; }
  645. return retval;
  646. }
  647. void usage(char *progName)
  648. {
  649. int i;
  650. printf("USAGE: %s\n", progName);
  651. printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
  652. printf(" %s\n", progName);
  653. printf(" <Inputfile (JPG)> [options]\n\n");
  654. printf("Options:\n\n");
  655. printf("-alloc = Dynamically allocate JPEG image buffers\n");
  656. printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
  657. printf("-bottomup = Test bottom-up compression/decompression\n");
  658. printf("-tile = Test performance of the codec when the image is encoded as separate\n");
  659. printf(" tiles of varying sizes.\n");
  660. printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
  661. printf(" Test the specified color conversion path in the codec (default = BGR)\n");
  662. printf("-cmyk = Indirectly test YCCK JPEG compression/decompression (the source\n");
  663. printf(" and destination bitmaps are still RGB. The conversion is done\n");
  664. printf(" internally prior to compression or after decompression.)\n");
  665. printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
  666. printf(" the underlying codec\n");
  667. printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
  668. printf(" codec\n");
  669. printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
  670. printf(" underlying codec\n");
  671. printf("-progressive = Use progressive entropy coding in JPEG images generated by\n");
  672. printf(" compression and transform operations.\n");
  673. printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
  674. printf(" of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");
  675. printf(" GRAY). The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
  676. printf(" sequence.\n");
  677. printf("-quiet = Output results in tabular rather than verbose format\n");
  678. printf("-yuv = Test YUV encoding/decoding functions\n");
  679. printf("-yuvpad <p> = If testing YUV encoding/decoding, this specifies the number of\n");
  680. printf(" bytes to which each row of each plane in the intermediate YUV image is\n");
  681. printf(" padded (default = 1)\n");
  682. printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
  683. printf(" factor of M/N (M/N = ");
  684. for (i = 0; i < nsf; i++) {
  685. printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom);
  686. if (nsf == 2 && i != nsf - 1) printf(" or ");
  687. else if (nsf > 2) {
  688. if (i != nsf - 1) printf(", ");
  689. if (i == nsf - 2) printf("or ");
  690. }
  691. if (i % 8 == 0 && i != 0) printf("\n ");
  692. }
  693. printf(")\n");
  694. printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
  695. printf(" Perform the corresponding lossless transform prior to\n");
  696. printf(" decompression (these options are mutually exclusive)\n");
  697. printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
  698. printf(" test (can be combined with the other transforms above)\n");
  699. printf("-copynone = Do not copy any extra markers (including EXIF and ICC profile data)\n");
  700. printf(" when transforming the image.\n");
  701. printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");
  702. printf("-warmup <t> = Run each benchmark for <t> seconds (default = 1.0) prior to\n");
  703. printf(" starting the timer, in order to prime the caches and thus improve the\n");
  704. printf(" consistency of the results.\n");
  705. printf("-componly = Stop after running compression tests. Do not test decompression.\n");
  706. printf("-nowrite = Do not write reference or output images (improves consistency of\n");
  707. printf(" performance measurements.)\n");
  708. printf("-stoponwarning = Immediately discontinue the current\n");
  709. printf(" compression/decompression/transform operation if the underlying codec\n");
  710. printf(" throws a warning (non-fatal error)\n\n");
  711. printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
  712. printf("test will be performed for all quality values in the range.\n\n");
  713. exit(1);
  714. }
  715. int main(int argc, char *argv[])
  716. {
  717. unsigned char *srcBuf = NULL;
  718. int w = 0, h = 0, i, j, minQual = -1, maxQual = -1;
  719. char *temp;
  720. int minArg = 2, retval = 0, subsamp = -1;
  721. if ((scalingFactors = tjGetScalingFactors(&nsf)) == NULL || nsf == 0)
  722. _throw("executing tjGetScalingFactors()", tjGetErrorStr());
  723. if (argc < minArg) usage(argv[0]);
  724. temp = strrchr(argv[1], '.');
  725. if (temp != NULL) {
  726. if (!strcasecmp(temp, ".bmp")) ext = "bmp";
  727. if (!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg"))
  728. decompOnly = 1;
  729. }
  730. printf("\n");
  731. if (!decompOnly) {
  732. minArg = 3;
  733. if (argc < minArg) usage(argv[0]);
  734. if ((minQual = atoi(argv[2])) < 1 || minQual > 100) {
  735. puts("ERROR: Quality must be between 1 and 100.");
  736. exit(1);
  737. }
  738. if ((temp = strchr(argv[2], '-')) != NULL && strlen(temp) > 1 &&
  739. sscanf(&temp[1], "%d", &maxQual) == 1 && maxQual > minQual &&
  740. maxQual >= 1 && maxQual <= 100) {}
  741. else maxQual = minQual;
  742. }
  743. if (argc > minArg) {
  744. for (i = minArg; i < argc; i++) {
  745. if (!strcasecmp(argv[i], "-tile")) {
  746. doTile = 1; xformOpt |= TJXOPT_CROP;
  747. } else if (!strcasecmp(argv[i], "-fastupsample")) {
  748. printf("Using fast upsampling code\n\n");
  749. flags |= TJFLAG_FASTUPSAMPLE;
  750. } else if (!strcasecmp(argv[i], "-fastdct")) {
  751. printf("Using fastest DCT/IDCT algorithm\n\n");
  752. flags |= TJFLAG_FASTDCT;
  753. } else if (!strcasecmp(argv[i], "-accuratedct")) {
  754. printf("Using most accurate DCT/IDCT algorithm\n\n");
  755. flags |= TJFLAG_ACCURATEDCT;
  756. } else if (!strcasecmp(argv[i], "-progressive")) {
  757. printf("Using progressive entropy coding\n\n");
  758. flags |= TJFLAG_PROGRESSIVE;
  759. } else if (!strcasecmp(argv[i], "-rgb"))
  760. pf = TJPF_RGB;
  761. else if (!strcasecmp(argv[i], "-rgbx"))
  762. pf = TJPF_RGBX;
  763. else if (!strcasecmp(argv[i], "-bgr"))
  764. pf = TJPF_BGR;
  765. else if (!strcasecmp(argv[i], "-bgrx"))
  766. pf = TJPF_BGRX;
  767. else if (!strcasecmp(argv[i], "-xbgr"))
  768. pf = TJPF_XBGR;
  769. else if (!strcasecmp(argv[i], "-xrgb"))
  770. pf = TJPF_XRGB;
  771. else if (!strcasecmp(argv[i], "-cmyk"))
  772. pf = TJPF_CMYK;
  773. else if (!strcasecmp(argv[i], "-bottomup"))
  774. flags |= TJFLAG_BOTTOMUP;
  775. else if (!strcasecmp(argv[i], "-quiet"))
  776. quiet = 1;
  777. else if (!strcasecmp(argv[i], "-qq"))
  778. quiet = 2;
  779. else if (!strcasecmp(argv[i], "-scale") && i < argc - 1) {
  780. int temp1 = 0, temp2 = 0, match = 0;
  781. if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) == 2) {
  782. for (j = 0; j < nsf; j++) {
  783. if ((double)temp1 / (double)temp2 ==
  784. (double)scalingFactors[j].num /
  785. (double)scalingFactors[j].denom) {
  786. sf = scalingFactors[j];
  787. match = 1; break;
  788. }
  789. }
  790. if (!match) usage(argv[0]);
  791. } else usage(argv[0]);
  792. } else if (!strcasecmp(argv[i], "-hflip"))
  793. xformOp = TJXOP_HFLIP;
  794. else if (!strcasecmp(argv[i], "-vflip"))
  795. xformOp = TJXOP_VFLIP;
  796. else if (!strcasecmp(argv[i], "-transpose"))
  797. xformOp = TJXOP_TRANSPOSE;
  798. else if (!strcasecmp(argv[i], "-transverse"))
  799. xformOp = TJXOP_TRANSVERSE;
  800. else if (!strcasecmp(argv[i], "-rot90"))
  801. xformOp = TJXOP_ROT90;
  802. else if (!strcasecmp(argv[i], "-rot180"))
  803. xformOp = TJXOP_ROT180;
  804. else if (!strcasecmp(argv[i], "-rot270"))
  805. xformOp = TJXOP_ROT270;
  806. else if (!strcasecmp(argv[i], "-grayscale"))
  807. xformOpt |= TJXOPT_GRAY;
  808. else if (!strcasecmp(argv[i], "-custom"))
  809. customFilter = dummyDCTFilter;
  810. else if (!strcasecmp(argv[i], "-nooutput"))
  811. xformOpt |= TJXOPT_NOOUTPUT;
  812. else if (!strcasecmp(argv[i], "-copynone"))
  813. xformOpt |= TJXOPT_COPYNONE;
  814. else if (!strcasecmp(argv[i], "-benchtime") && i < argc - 1) {
  815. double temp = atof(argv[++i]);
  816. if (temp > 0.0) benchTime = temp;
  817. else usage(argv[0]);
  818. } else if (!strcasecmp(argv[i], "-warmup") && i < argc - 1) {
  819. double temp = atof(argv[++i]);
  820. if (temp >= 0.0) warmup = temp;
  821. else usage(argv[0]);
  822. printf("Warmup time = %.1f seconds\n\n", warmup);
  823. } else if (!strcasecmp(argv[i], "-alloc"))
  824. flags &= (~TJFLAG_NOREALLOC);
  825. else if (!strcasecmp(argv[i], "-bmp"))
  826. ext = "bmp";
  827. else if (!strcasecmp(argv[i], "-yuv")) {
  828. printf("Testing YUV planar encoding/decoding\n\n");
  829. doYUV = 1;
  830. } else if (!strcasecmp(argv[i], "-yuvpad") && i < argc - 1) {
  831. int temp = atoi(argv[++i]);
  832. if (temp >= 1) yuvPad = temp;
  833. } else if (!strcasecmp(argv[i], "-subsamp") && i < argc - 1) {
  834. i++;
  835. if (toupper(argv[i][0]) == 'G') subsamp = TJSAMP_GRAY;
  836. else {
  837. int temp = atoi(argv[i]);
  838. switch (temp) {
  839. case 444: subsamp = TJSAMP_444; break;
  840. case 422: subsamp = TJSAMP_422; break;
  841. case 440: subsamp = TJSAMP_440; break;
  842. case 420: subsamp = TJSAMP_420; break;
  843. case 411: subsamp = TJSAMP_411; break;
  844. }
  845. }
  846. } else if (!strcasecmp(argv[i], "-componly"))
  847. compOnly = 1;
  848. else if (!strcasecmp(argv[i], "-nowrite"))
  849. doWrite = 0;
  850. else if (!strcasecmp(argv[i], "-stoponwarning"))
  851. flags |= TJFLAG_STOPONWARNING;
  852. else usage(argv[0]);
  853. }
  854. }
  855. if ((sf.num != 1 || sf.denom != 1) && doTile) {
  856. printf("Disabling tiled compression/decompression tests, because those tests do not\n");
  857. printf("work when scaled decompression is enabled.\n");
  858. doTile = 0;
  859. }
  860. if ((flags & TJFLAG_NOREALLOC) == 0 && doTile) {
  861. printf("Disabling tiled compression/decompression tests, because those tests do not\n");
  862. printf("work when dynamic JPEG buffer allocation is enabled.\n\n");
  863. doTile = 0;
  864. }
  865. if (!decompOnly) {
  866. if ((srcBuf = tjLoadImage(argv[1], &w, 1, &h, &pf, flags)) == NULL)
  867. _throwtjg("loading bitmap");
  868. temp = strrchr(argv[1], '.');
  869. if (temp != NULL) *temp = '\0';
  870. }
  871. if (quiet == 1 && !decompOnly) {
  872. printf("All performance values in Mpixels/sec\n\n");
  873. printf("Bitmap JPEG JPEG %s %s ",
  874. doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
  875. if (doYUV) printf("Encode ");
  876. printf("Comp Comp Decomp ");
  877. if (doYUV) printf("Decode");
  878. printf("\n");
  879. printf("Format Subsamp Qual Width Height ");
  880. if (doYUV) printf("Perf ");
  881. printf("Perf Ratio Perf ");
  882. if (doYUV) printf("Perf");
  883. printf("\n\n");
  884. }
  885. if (decompOnly) {
  886. decompTest(argv[1]);
  887. printf("\n");
  888. goto bailout;
  889. }
  890. if (subsamp >= 0 && subsamp < TJ_NUMSAMP) {
  891. for (i = maxQual; i >= minQual; i--)
  892. fullTest(srcBuf, w, h, subsamp, i, argv[1]);
  893. printf("\n");
  894. } else {
  895. if (pf != TJPF_CMYK) {
  896. for (i = maxQual; i >= minQual; i--)
  897. fullTest(srcBuf, w, h, TJSAMP_GRAY, i, argv[1]);
  898. printf("\n");
  899. }
  900. for (i = maxQual; i >= minQual; i--)
  901. fullTest(srcBuf, w, h, TJSAMP_420, i, argv[1]);
  902. printf("\n");
  903. for (i = maxQual; i >= minQual; i--)
  904. fullTest(srcBuf, w, h, TJSAMP_422, i, argv[1]);
  905. printf("\n");
  906. for (i = maxQual; i >= minQual; i--)
  907. fullTest(srcBuf, w, h, TJSAMP_444, i, argv[1]);
  908. printf("\n");
  909. }
  910. bailout:
  911. if (srcBuf) tjFree(srcBuf);
  912. return retval;
  913. }