tjbench.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * Copyright (C)2009-2014 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 "./bmp.h"
  36. #include "./tjutil.h"
  37. #include "./turbojpeg.h"
  38. #define _throw(op, err) { \
  39. printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
  40. retval=-1; goto bailout;}
  41. #define _throwunix(m) _throw(m, strerror(errno))
  42. #define _throwtj(m) _throw(m, tjGetErrorStr())
  43. #define _throwbmp(m) _throw(m, bmpgeterr())
  44. enum {YUVENCODE=1, YUVDECODE};
  45. int flags=TJFLAG_NOREALLOC, decomponly=0, yuv=0, quiet=0, dotile=0,
  46. pf=TJPF_BGR;
  47. char *ext="ppm";
  48. const char *pixFormatStr[TJ_NUMPF]=
  49. {
  50. "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY"
  51. };
  52. const char *subNameLong[TJ_NUMSAMP]=
  53. {
  54. "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0"
  55. };
  56. const char *subName[NUMSUBOPT]={"444", "422", "420", "GRAY", "440"};
  57. tjscalingfactor *scalingfactors=NULL, sf={1, 1}; int nsf=0;
  58. int xformop=TJXOP_NONE, xformopt=0;
  59. int (*customFilter)(short *, tjregion, tjregion, int, int, tjtransform *);
  60. double benchtime=5.0;
  61. char *sigfig(double val, int figs, char *buf, int len)
  62. {
  63. char format[80];
  64. int digitsafterdecimal=figs-(int)ceil(log10(fabs(val)));
  65. if(digitsafterdecimal<1) snprintf(format, 80, "%%.0f");
  66. else snprintf(format, 80, "%%.%df", digitsafterdecimal);
  67. snprintf(buf, len, format, val);
  68. return buf;
  69. }
  70. /* Custom DCT filter which produces a negative of the image */
  71. int dummyDCTFilter(short *coeffs, tjregion arrayRegion, tjregion planeRegion,
  72. int componentIndex, int transformIndex, tjtransform *transform)
  73. {
  74. int i;
  75. for(i=0; i<arrayRegion.w*arrayRegion.h; i++) coeffs[i]=-coeffs[i];
  76. return 0;
  77. }
  78. /* Decompression test */
  79. int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf,
  80. unsigned long *jpegsize, unsigned char *dstbuf, int w, int h,
  81. int subsamp, int jpegqual, char *filename, int tilew, int tileh)
  82. {
  83. char tempstr[1024], sizestr[20]="\0", qualstr[6]="\0", *ptr;
  84. FILE *file=NULL; tjhandle handle=NULL;
  85. int row, col, i, dstbufalloc=0, retval=0;
  86. double start, elapsed;
  87. int ps=tjPixelSize[pf];
  88. int yuvsize=tjBufSizeYUV(w, h, subsamp), bufsize;
  89. int scaledw=(yuv==YUVDECODE)? w : TJSCALED(w, sf);
  90. int scaledh=(yuv==YUVDECODE)? h : TJSCALED(h, sf);
  91. int pitch=scaledw*ps;
  92. int ntilesw=(w+tilew-1)/tilew, ntilesh=(h+tileh-1)/tileh;
  93. unsigned char *dstptr, *dstptr2;
  94. if(jpegqual>0)
  95. {
  96. snprintf(qualstr, 6, "_Q%d", jpegqual);
  97. qualstr[5]=0;
  98. }
  99. if((handle=tjInitDecompress())==NULL)
  100. _throwtj("executing tjInitDecompress()");
  101. bufsize=(yuv==YUVDECODE? yuvsize:pitch*scaledh);
  102. if(dstbuf==NULL)
  103. {
  104. if((dstbuf=(unsigned char *)malloc(bufsize)) == NULL)
  105. _throwunix("allocating image buffer");
  106. dstbufalloc=1;
  107. }
  108. /* Set the destination buffer to gray so we know whether the decompressor
  109. attempted to write to it */
  110. memset(dstbuf, 127, bufsize);
  111. /* Execute once to preload cache */
  112. if(yuv==YUVDECODE)
  113. {
  114. if(tjDecompressToYUV(handle, jpegbuf[0], jpegsize[0], dstbuf, flags)==-1)
  115. _throwtj("executing tjDecompressToYUV()");
  116. }
  117. else if(tjDecompress2(handle, jpegbuf[0], jpegsize[0], dstbuf, scaledw,
  118. pitch, scaledh, pf, flags)==-1)
  119. _throwtj("executing tjDecompress2()");
  120. /* Benchmark */
  121. for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++)
  122. {
  123. int tile=0;
  124. if(yuv==YUVDECODE)
  125. {
  126. if(tjDecompressToYUV(handle, jpegbuf[0], jpegsize[0], dstbuf, flags)==-1)
  127. _throwtj("executing tjDecompressToYUV()");
  128. }
  129. else for(row=0, dstptr=dstbuf; row<ntilesh; row++, dstptr+=pitch*tileh)
  130. {
  131. for(col=0, dstptr2=dstptr; col<ntilesw; col++, tile++, dstptr2+=ps*tilew)
  132. {
  133. int width=dotile? min(tilew, w-col*tilew):scaledw;
  134. int height=dotile? min(tileh, h-row*tileh):scaledh;
  135. if(tjDecompress2(handle, jpegbuf[tile], jpegsize[tile], dstptr2, width,
  136. pitch, height, pf, flags)==-1)
  137. _throwtj("executing tjDecompress2()");
  138. }
  139. }
  140. }
  141. if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
  142. handle=NULL;
  143. if(quiet)
  144. {
  145. printf("%s\n",
  146. sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024));
  147. }
  148. else
  149. {
  150. printf("D--> Frame rate: %f fps\n", (double)i/elapsed);
  151. printf(" Dest. throughput: %f Megapixels/sec\n",
  152. (double)(w*h)/1000000.*(double)i/elapsed);
  153. }
  154. if(yuv==YUVDECODE)
  155. {
  156. snprintf(tempstr, 1024, "%s_%s%s.yuv", filename, subName[subsamp],
  157. qualstr);
  158. if((file=fopen(tempstr, "wb"))==NULL)
  159. _throwunix("opening YUV image for output");
  160. if(fwrite(dstbuf, yuvsize, 1, file)!=1)
  161. _throwunix("writing YUV image");
  162. fclose(file); file=NULL;
  163. }
  164. else
  165. {
  166. if(sf.num!=1 || sf.denom!=1)
  167. snprintf(sizestr, 20, "%d_%d", sf.num, sf.denom);
  168. else if(tilew!=w || tileh!=h)
  169. snprintf(sizestr, 20, "%dx%d", tilew, tileh);
  170. else snprintf(sizestr, 20, "full");
  171. if(decomponly)
  172. snprintf(tempstr, 1024, "%s_%s.%s", filename, sizestr, ext);
  173. else
  174. snprintf(tempstr, 1024, "%s_%s%s_%s.%s", filename, subName[subsamp],
  175. qualstr, sizestr, ext);
  176. if(savebmp(tempstr, dstbuf, scaledw, scaledh, pf,
  177. (flags&TJFLAG_BOTTOMUP)!=0)==-1)
  178. _throwbmp("saving bitmap");
  179. ptr=strrchr(tempstr, '.');
  180. snprintf(ptr, 1024-(ptr-tempstr), "-err.%s", ext);
  181. if(srcbuf && sf.num==1 && sf.denom==1)
  182. {
  183. if(!quiet) printf("Compression error written to %s.\n", tempstr);
  184. if(subsamp==TJ_GRAYSCALE)
  185. {
  186. int index, index2;
  187. for(row=0, index=0; row<h; row++, index+=pitch)
  188. {
  189. for(col=0, index2=index; col<w; col++, index2+=ps)
  190. {
  191. int rindex=index2+tjRedOffset[pf];
  192. int gindex=index2+tjGreenOffset[pf];
  193. int bindex=index2+tjBlueOffset[pf];
  194. int y=(int)((double)srcbuf[rindex]*0.299
  195. + (double)srcbuf[gindex]*0.587
  196. + (double)srcbuf[bindex]*0.114 + 0.5);
  197. if(y>255) y=255; if(y<0) y=0;
  198. dstbuf[rindex]=abs(dstbuf[rindex]-y);
  199. dstbuf[gindex]=abs(dstbuf[gindex]-y);
  200. dstbuf[bindex]=abs(dstbuf[bindex]-y);
  201. }
  202. }
  203. }
  204. else
  205. {
  206. for(row=0; row<h; row++)
  207. for(col=0; col<w*ps; col++)
  208. dstbuf[pitch*row+col]
  209. =abs(dstbuf[pitch*row+col]-srcbuf[pitch*row+col]);
  210. }
  211. if(savebmp(tempstr, dstbuf, w, h, pf,
  212. (flags&TJFLAG_BOTTOMUP)!=0)==-1)
  213. _throwbmp("saving bitmap");
  214. }
  215. }
  216. bailout:
  217. if(file) {fclose(file); file=NULL;}
  218. if(handle) {tjDestroy(handle); handle=NULL;}
  219. if(dstbuf && dstbufalloc) {free(dstbuf); dstbuf=NULL;}
  220. return retval;
  221. }
  222. void dotestyuv(unsigned char *srcbuf, int w, int h, int subsamp,
  223. char *filename)
  224. {
  225. char tempstr[1024], tempstr2[80];
  226. FILE *file=NULL; tjhandle handle=NULL;
  227. unsigned char *dstbuf=NULL;
  228. double start, elapsed;
  229. int i, retval=0, ps=tjPixelSize[pf];
  230. int yuvsize=0;
  231. yuvsize=tjBufSizeYUV(w, h, subsamp);
  232. if((dstbuf=(unsigned char *)malloc(yuvsize)) == NULL)
  233. _throwunix("allocating image buffer");
  234. if(!quiet)
  235. printf(">>>>> %s (%s) <--> YUV %s <<<<<\n", pixFormatStr[pf],
  236. (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp]);
  237. if(quiet==1)
  238. printf("%s\t%s\t%s\tN/A\t", pixFormatStr[pf],
  239. (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp]);
  240. if((handle=tjInitCompress())==NULL)
  241. _throwtj("executing tjInitCompress()");
  242. /* Execute once to preload cache */
  243. if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1)
  244. _throwtj("executing tjEncodeYUV2()");
  245. /* Benchmark */
  246. for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++)
  247. {
  248. if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1)
  249. _throwtj("executing tjEncodeYUV2()");
  250. }
  251. if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
  252. handle=NULL;
  253. if(quiet==1) printf("%-4d %-4d\t", w, h);
  254. if(quiet)
  255. {
  256. printf("%s%c%s%c",
  257. sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024),
  258. quiet==2? '\n':'\t',
  259. sigfig((double)(w*h*ps)/(double)yuvsize, 4, tempstr2, 80),
  260. quiet==2? '\n':'\t');
  261. }
  262. else
  263. {
  264. printf("\n%s size: %d x %d\n", "Image", w, h);
  265. printf("C--> Frame rate: %f fps\n", (double)i/elapsed);
  266. printf(" Output image size: %d bytes\n", yuvsize);
  267. printf(" Compression ratio: %f:1\n",
  268. (double)(w*h*ps)/(double)yuvsize);
  269. printf(" Source throughput: %f Megapixels/sec\n",
  270. (double)(w*h)/1000000.*(double)i/elapsed);
  271. printf(" Output bit stream: %f Megabits/sec\n",
  272. (double)yuvsize*8./1000000.*(double)i/elapsed);
  273. }
  274. snprintf(tempstr, 1024, "%s_%s.yuv", filename, subName[subsamp]);
  275. if((file=fopen(tempstr, "wb"))==NULL)
  276. _throwunix("opening reference image");
  277. if(fwrite(dstbuf, yuvsize, 1, file)!=1)
  278. _throwunix("writing reference image");
  279. fclose(file); file=NULL;
  280. if(!quiet) printf("Reference image written to %s\n", tempstr);
  281. bailout:
  282. if(file) {fclose(file); file=NULL;}
  283. if(dstbuf) {free(dstbuf); dstbuf=NULL;}
  284. if(handle) {tjDestroy(handle); handle=NULL;}
  285. return;
  286. }
  287. void dotest(unsigned char *srcbuf, int w, int h, int subsamp, int jpegqual,
  288. char *filename)
  289. {
  290. char tempstr[1024], tempstr2[80];
  291. FILE *file=NULL; tjhandle handle=NULL;
  292. unsigned char **jpegbuf=NULL, *tmpbuf=NULL, *srcptr, *srcptr2;
  293. double start, elapsed;
  294. int totaljpegsize=0, row, col, i, tilew=w, tileh=h, retval=0;
  295. unsigned long *jpegsize=NULL;
  296. int ps=tjPixelSize[pf], ntilesw=1, ntilesh=1, pitch=w*ps;
  297. if(yuv==YUVENCODE) {dotestyuv(srcbuf, w, h, subsamp, filename); return;}
  298. if((tmpbuf=(unsigned char *)malloc(pitch*h)) == NULL)
  299. _throwunix("allocating temporary image buffer");
  300. if(!quiet)
  301. printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pixFormatStr[pf],
  302. (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp],
  303. jpegqual);
  304. for(tilew=dotile? 8:w, tileh=dotile? 8:h; ; tilew*=2, tileh*=2)
  305. {
  306. if(tilew>w) tilew=w; if(tileh>h) tileh=h;
  307. ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
  308. if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
  309. *ntilesw*ntilesh))==NULL)
  310. _throwunix("allocating JPEG tile array");
  311. memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
  312. if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
  313. *ntilesw*ntilesh))==NULL)
  314. _throwunix("allocating JPEG size array");
  315. memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
  316. if((flags&TJFLAG_NOREALLOC)!=0)
  317. for(i=0; i<ntilesw*ntilesh; i++)
  318. {
  319. if((jpegbuf[i]=(unsigned char *)malloc(tjBufSize(tilew, tileh,
  320. subsamp)))==NULL)
  321. _throwunix("allocating JPEG tiles");
  322. }
  323. /* Compression test */
  324. if(quiet==1)
  325. printf("%s\t%s\t%s\t%d\t", pixFormatStr[pf],
  326. (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp], jpegqual);
  327. for(i=0; i<h; i++)
  328. memcpy(&tmpbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
  329. if((handle=tjInitCompress())==NULL)
  330. _throwtj("executing tjInitCompress()");
  331. /* Execute once to preload cache */
  332. if(tjCompress2(handle, srcbuf, tilew, pitch, tileh, pf, &jpegbuf[0],
  333. &jpegsize[0], subsamp, jpegqual, flags)==-1)
  334. _throwtj("executing tjCompress2()");
  335. /* Benchmark */
  336. for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++)
  337. {
  338. int tile=0;
  339. totaljpegsize=0;
  340. for(row=0, srcptr=srcbuf; row<ntilesh; row++, srcptr+=pitch*tileh)
  341. {
  342. for(col=0, srcptr2=srcptr; col<ntilesw; col++, tile++,
  343. srcptr2+=ps*tilew)
  344. {
  345. int width=min(tilew, w-col*tilew);
  346. int height=min(tileh, h-row*tileh);
  347. if(tjCompress2(handle, srcptr2, width, pitch, height, pf,
  348. &jpegbuf[tile], &jpegsize[tile], subsamp, jpegqual, flags)==-1)
  349. _throwtj("executing tjCompress()2");
  350. totaljpegsize+=jpegsize[tile];
  351. }
  352. }
  353. }
  354. if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
  355. handle=NULL;
  356. if(quiet==1) printf("%-4d %-4d\t", tilew, tileh);
  357. if(quiet)
  358. {
  359. printf("%s%c%s%c",
  360. sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024),
  361. quiet==2? '\n':'\t',
  362. sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
  363. quiet==2? '\n':'\t');
  364. }
  365. else
  366. {
  367. printf("\n%s size: %d x %d\n", dotile? "Tile":"Image", tilew,
  368. tileh);
  369. printf("C--> Frame rate: %f fps\n", (double)i/elapsed);
  370. printf(" Output image size: %d bytes\n", totaljpegsize);
  371. printf(" Compression ratio: %f:1\n",
  372. (double)(w*h*ps)/(double)totaljpegsize);
  373. printf(" Source throughput: %f Megapixels/sec\n",
  374. (double)(w*h)/1000000.*(double)i/elapsed);
  375. printf(" Output bit stream: %f Megabits/sec\n",
  376. (double)totaljpegsize*8./1000000.*(double)i/elapsed);
  377. }
  378. if(tilew==w && tileh==h)
  379. {
  380. snprintf(tempstr, 1024, "%s_%s_Q%d.jpg", filename, subName[subsamp],
  381. jpegqual);
  382. if((file=fopen(tempstr, "wb"))==NULL)
  383. _throwunix("opening reference image");
  384. if(fwrite(jpegbuf[0], jpegsize[0], 1, file)!=1)
  385. _throwunix("writing reference image");
  386. fclose(file); file=NULL;
  387. if(!quiet) printf("Reference image written to %s\n", tempstr);
  388. }
  389. /* Decompression test */
  390. if(decomptest(srcbuf, jpegbuf, jpegsize, tmpbuf, w, h, subsamp, jpegqual,
  391. filename, tilew, tileh)==-1)
  392. goto bailout;
  393. for(i=0; i<ntilesw*ntilesh; i++)
  394. {
  395. if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;
  396. }
  397. free(jpegbuf); jpegbuf=NULL;
  398. free(jpegsize); jpegsize=NULL;
  399. if(tilew==w && tileh==h) break;
  400. }
  401. bailout:
  402. if(file) {fclose(file); file=NULL;}
  403. if(jpegbuf)
  404. {
  405. for(i=0; i<ntilesw*ntilesh; i++)
  406. {
  407. if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;
  408. }
  409. free(jpegbuf); jpegbuf=NULL;
  410. }
  411. if(jpegsize) {free(jpegsize); jpegsize=NULL;}
  412. if(tmpbuf) {free(tmpbuf); tmpbuf=NULL;}
  413. if(handle) {tjDestroy(handle); handle=NULL;}
  414. return;
  415. }
  416. void dodecomptest(char *filename)
  417. {
  418. FILE *file=NULL; tjhandle handle=NULL;
  419. unsigned char **jpegbuf=NULL, *srcbuf=NULL;
  420. unsigned long *jpegsize=NULL, srcsize, totaljpegsize;
  421. tjtransform *t=NULL;
  422. int w=0, h=0, subsamp=-1, _w, _h, _tilew, _tileh,
  423. _ntilesw, _ntilesh, _subsamp;
  424. char *temp=NULL, tempstr[80], tempstr2[80];
  425. int row, col, i, tilew, tileh, ntilesw=1, ntilesh=1, retval=0;
  426. double start, elapsed;
  427. int ps=tjPixelSize[pf], tile;
  428. if((file=fopen(filename, "rb"))==NULL)
  429. _throwunix("opening file");
  430. if(fseek(file, 0, SEEK_END)<0 || (srcsize=ftell(file))==(unsigned long)-1)
  431. _throwunix("determining file size");
  432. if((srcbuf=(unsigned char *)malloc(srcsize))==NULL)
  433. _throwunix("allocating memory");
  434. if(fseek(file, 0, SEEK_SET)<0)
  435. _throwunix("setting file position");
  436. if(fread(srcbuf, srcsize, 1, file)<1)
  437. _throwunix("reading JPEG data");
  438. fclose(file); file=NULL;
  439. temp=strrchr(filename, '.');
  440. if(temp!=NULL) *temp='\0';
  441. if((handle=tjInitTransform())==NULL)
  442. _throwtj("executing tjInitTransform()");
  443. if(tjDecompressHeader2(handle, srcbuf, srcsize, &w, &h, &subsamp)==-1)
  444. _throwtj("executing tjDecompressHeader2()");
  445. if(quiet==1)
  446. {
  447. printf("All performance values in Mpixels/sec\n\n");
  448. printf("Bitmap\tBitmap\tJPEG\t%s %s \tXform\tComp\tDecomp\n",
  449. dotile? "Tile ":"Image", dotile? "Tile ":"Image");
  450. printf("Format\tOrder\tSubsamp\tWidth Height\tPerf \tRatio\tPerf\n\n");
  451. }
  452. else if(!quiet)
  453. {
  454. printf(">>>>> JPEG %s --> %s (%s) <<<<<\n", subNameLong[subsamp],
  455. pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down");
  456. }
  457. for(tilew=dotile? 16:w, tileh=dotile? 16:h; ; tilew*=2, tileh*=2)
  458. {
  459. if(tilew>w) tilew=w; if(tileh>h) tileh=h;
  460. ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
  461. if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
  462. *ntilesw*ntilesh))==NULL)
  463. _throwunix("allocating JPEG tile array");
  464. memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
  465. if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
  466. *ntilesw*ntilesh))==NULL)
  467. _throwunix("allocating JPEG size array");
  468. memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
  469. if((flags&TJFLAG_NOREALLOC)!=0 || !dotile)
  470. for(i=0; i<ntilesw*ntilesh; i++)
  471. {
  472. if((jpegbuf[i]=(unsigned char *)malloc(tjBufSize(tilew, tileh,
  473. subsamp)))==NULL)
  474. _throwunix("allocating JPEG tiles");
  475. }
  476. _w=w; _h=h; _tilew=tilew; _tileh=tileh;
  477. if(!quiet)
  478. {
  479. printf("\n%s size: %d x %d", dotile? "Tile":"Image", _tilew,
  480. _tileh);
  481. if(sf.num!=1 || sf.denom!=1)
  482. printf(" --> %d x %d", TJSCALED(_w, sf), TJSCALED(_h, sf));
  483. printf("\n");
  484. }
  485. else if(quiet==1)
  486. {
  487. printf("%s\t%s\t%s\t", pixFormatStr[pf],
  488. (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp]);
  489. printf("%-4d %-4d\t", tilew, tileh);
  490. }
  491. _subsamp=subsamp;
  492. if(dotile || xformop!=TJXOP_NONE || xformopt!=0 || customFilter)
  493. {
  494. if((t=(tjtransform *)malloc(sizeof(tjtransform)*ntilesw*ntilesh))
  495. ==NULL)
  496. _throwunix("allocating image transform array");
  497. if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE
  498. || xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270)
  499. {
  500. _w=h; _h=w; _tilew=tileh; _tileh=tilew;
  501. }
  502. if(xformopt&TJXOPT_GRAY) _subsamp=TJ_GRAYSCALE;
  503. if(xformop==TJXOP_HFLIP || xformop==TJXOP_ROT180)
  504. _w=_w-(_w%tjMCUWidth[_subsamp]);
  505. if(xformop==TJXOP_VFLIP || xformop==TJXOP_ROT180)
  506. _h=_h-(_h%tjMCUHeight[_subsamp]);
  507. if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT90)
  508. _w=_w-(_w%tjMCUHeight[_subsamp]);
  509. if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT270)
  510. _h=_h-(_h%tjMCUWidth[_subsamp]);
  511. _ntilesw=(_w+_tilew-1)/_tilew;
  512. _ntilesh=(_h+_tileh-1)/_tileh;
  513. for(row=0, tile=0; row<_ntilesh; row++)
  514. {
  515. for(col=0; col<_ntilesw; col++, tile++)
  516. {
  517. t[tile].r.w=min(_tilew, _w-col*_tilew);
  518. t[tile].r.h=min(_tileh, _h-row*_tileh);
  519. t[tile].r.x=col*_tilew;
  520. t[tile].r.y=row*_tileh;
  521. t[tile].op=xformop;
  522. t[tile].options=xformopt|TJXOPT_TRIM;
  523. t[tile].customFilter=customFilter;
  524. if(t[tile].options&TJXOPT_NOOUTPUT && jpegbuf[tile])
  525. {
  526. free(jpegbuf[tile]); jpegbuf[tile]=NULL;
  527. }
  528. }
  529. }
  530. start=gettime();
  531. if(tjTransform(handle, srcbuf, srcsize, _ntilesw*_ntilesh, jpegbuf,
  532. jpegsize, t, flags)==-1)
  533. _throwtj("executing tjTransform()");
  534. elapsed=gettime()-start;
  535. free(t); t=NULL;
  536. for(tile=0, totaljpegsize=0; tile<_ntilesw*_ntilesh; tile++)
  537. totaljpegsize+=jpegsize[tile];
  538. if(quiet)
  539. {
  540. printf("%s%c%s%c",
  541. sigfig((double)(w*h)/1000000./elapsed, 4, tempstr, 80),
  542. quiet==2? '\n':'\t',
  543. sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
  544. quiet==2? '\n':'\t');
  545. }
  546. else if(!quiet)
  547. {
  548. printf("X--> Frame rate: %f fps\n", 1.0/elapsed);
  549. printf(" Output image size: %lu bytes\n", totaljpegsize);
  550. printf(" Compression ratio: %f:1\n",
  551. (double)(w*h*ps)/(double)totaljpegsize);
  552. printf(" Source throughput: %f Megapixels/sec\n",
  553. (double)(w*h)/1000000./elapsed);
  554. printf(" Output bit stream: %f Megabits/sec\n",
  555. (double)totaljpegsize*8./1000000./elapsed);
  556. }
  557. }
  558. else
  559. {
  560. if(quiet==1) printf("N/A\tN/A\t");
  561. jpegsize[0]=srcsize;
  562. memcpy(jpegbuf[0], srcbuf, srcsize);
  563. }
  564. if(w==tilew) _tilew=_w;
  565. if(h==tileh) _tileh=_h;
  566. if(!(xformopt&TJXOPT_NOOUTPUT))
  567. {
  568. if(decomptest(NULL, jpegbuf, jpegsize, NULL, _w, _h, _subsamp, 0,
  569. filename, _tilew, _tileh)==-1)
  570. goto bailout;
  571. }
  572. else if(quiet==1) printf("N/A\n");
  573. for(i=0; i<ntilesw*ntilesh; i++)
  574. {
  575. free(jpegbuf[i]); jpegbuf[i]=NULL;
  576. }
  577. free(jpegbuf); jpegbuf=NULL;
  578. if(jpegsize) {free(jpegsize); jpegsize=NULL;}
  579. if(tilew==w && tileh==h) break;
  580. }
  581. bailout:
  582. if(file) {fclose(file); file=NULL;}
  583. if(jpegbuf)
  584. {
  585. for(i=0; i<ntilesw*ntilesh; i++)
  586. {
  587. if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;
  588. }
  589. free(jpegbuf); jpegbuf=NULL;
  590. }
  591. if(jpegsize) {free(jpegsize); jpegsize=NULL;}
  592. if(srcbuf) {free(srcbuf); srcbuf=NULL;}
  593. if(t) {free(t); t=NULL;}
  594. if(handle) {tjDestroy(handle); handle=NULL;}
  595. return;
  596. }
  597. void usage(char *progname)
  598. {
  599. int i;
  600. printf("USAGE: %s\n", progname);
  601. printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
  602. printf(" %s\n", progname);
  603. printf(" <Inputfile (JPG)> [options]\n\n");
  604. printf("Options:\n\n");
  605. printf("-alloc = Dynamically allocate JPEG image buffers\n");
  606. printf("-bmp = Generate output images in Windows Bitmap format (default=PPM)\n");
  607. printf("-bottomup = Test bottom-up compression/decompression\n");
  608. printf("-tile = Test performance of the codec when the image is encoded as separate\n");
  609. printf(" tiles of varying sizes.\n");
  610. printf("-forcemmx, -forcesse, -forcesse2, -forcesse3 =\n");
  611. printf(" Force MMX, SSE, SSE2, or SSE3 code paths in the underlying codec\n");
  612. printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
  613. printf(" Test the specified color conversion path in the codec (default: BGR)\n");
  614. printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
  615. printf(" the underlying codec\n");
  616. printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
  617. printf(" codec\n");
  618. printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
  619. printf(" underlying codec\n");
  620. printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
  621. printf(" of chrominance subsampling to use (<s> = 444, 422, 440, 420, or GRAY).\n");
  622. printf(" The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in sequence.\n");
  623. printf("-quiet = Output results in tabular rather than verbose format\n");
  624. printf("-yuvencode = Encode RGB input as planar YUV rather than compressing as JPEG\n");
  625. printf("-yuvdecode = Decode JPEG image to planar YUV rather than RGB\n");
  626. printf("-scale M/N = scale down the width/height of the decompressed JPEG image by a\n");
  627. printf(" factor of M/N (M/N = ");
  628. for(i=0; i<nsf; i++)
  629. {
  630. printf("%d/%d", scalingfactors[i].num, scalingfactors[i].denom);
  631. if(nsf==2 && i!=nsf-1) printf(" or ");
  632. else if(nsf>2)
  633. {
  634. if(i!=nsf-1) printf(", ");
  635. if(i==nsf-2) printf("or ");
  636. }
  637. if(i%8==0 && i!=0) printf("\n ");
  638. }
  639. printf(")\n");
  640. printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
  641. printf(" Perform the corresponding lossless transform prior to\n");
  642. printf(" decompression (these options are mutually exclusive)\n");
  643. printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
  644. printf(" test (can be combined with the other transforms above)\n");
  645. printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n\n");
  646. printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
  647. printf("test will be performed for all quality values in the range.\n\n");
  648. exit(1);
  649. }
  650. int main(int argc, char *argv[])
  651. {
  652. unsigned char *srcbuf=NULL; int w, h, i, j;
  653. int minqual=-1, maxqual=-1; char *temp;
  654. int minarg=2, retval=0, subsamp=-1;
  655. if((scalingfactors=tjGetScalingFactors(&nsf))==NULL || nsf==0)
  656. _throwtj("executing tjGetScalingFactors()");
  657. if(argc<minarg) usage(argv[0]);
  658. temp=strrchr(argv[1], '.');
  659. if(temp!=NULL)
  660. {
  661. if(!strcasecmp(temp, ".bmp")) ext="bmp";
  662. if(!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg")) decomponly=1;
  663. }
  664. printf("\n");
  665. if(argc>minarg)
  666. {
  667. for(i=minarg; i<argc; i++)
  668. {
  669. if(!strcasecmp(argv[i], "-yuvencode"))
  670. {
  671. printf("Testing YUV planar encoding\n\n");
  672. yuv=YUVENCODE; maxqual=minqual=100;
  673. }
  674. if(!strcasecmp(argv[i], "-yuvdecode"))
  675. {
  676. printf("Testing YUV planar decoding\n\n");
  677. yuv=YUVDECODE;
  678. }
  679. }
  680. }
  681. if(!decomponly && yuv!=YUVENCODE)
  682. {
  683. minarg=3;
  684. if(argc<minarg) usage(argv[0]);
  685. if((minqual=atoi(argv[2]))<1 || minqual>100)
  686. {
  687. puts("ERROR: Quality must be between 1 and 100.");
  688. exit(1);
  689. }
  690. if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
  691. && sscanf(&temp[1], "%d", &maxqual)==1 && maxqual>minqual && maxqual>=1
  692. && maxqual<=100) {}
  693. else maxqual=minqual;
  694. }
  695. if(argc>minarg)
  696. {
  697. for(i=minarg; i<argc; i++)
  698. {
  699. if(!strcasecmp(argv[i], "-tile"))
  700. {
  701. dotile=1; xformopt|=TJXOPT_CROP;
  702. }
  703. if(!strcasecmp(argv[i], "-forcesse3"))
  704. {
  705. printf("Forcing SSE3 code\n\n");
  706. flags|=TJFLAG_FORCESSE3;
  707. }
  708. if(!strcasecmp(argv[i], "-forcesse2"))
  709. {
  710. printf("Forcing SSE2 code\n\n");
  711. flags|=TJFLAG_FORCESSE2;
  712. }
  713. if(!strcasecmp(argv[i], "-forcesse"))
  714. {
  715. printf("Forcing SSE code\n\n");
  716. flags|=TJFLAG_FORCESSE;
  717. }
  718. if(!strcasecmp(argv[i], "-forcemmx"))
  719. {
  720. printf("Forcing MMX code\n\n");
  721. flags|=TJFLAG_FORCEMMX;
  722. }
  723. if(!strcasecmp(argv[i], "-fastupsample"))
  724. {
  725. printf("Using fast upsampling code\n\n");
  726. flags|=TJFLAG_FASTUPSAMPLE;
  727. }
  728. if(!strcasecmp(argv[i], "-fastdct"))
  729. {
  730. printf("Using fastest DCT/IDCT algorithm\n\n");
  731. flags|=TJFLAG_FASTDCT;
  732. }
  733. if(!strcasecmp(argv[i], "-accuratedct"))
  734. {
  735. printf("Using most accurate DCT/IDCT algorithm\n\n");
  736. flags|=TJFLAG_ACCURATEDCT;
  737. }
  738. if(!strcasecmp(argv[i], "-rgb")) pf=TJPF_RGB;
  739. if(!strcasecmp(argv[i], "-rgbx")) pf=TJPF_RGBX;
  740. if(!strcasecmp(argv[i], "-bgr")) pf=TJPF_BGR;
  741. if(!strcasecmp(argv[i], "-bgrx")) pf=TJPF_BGRX;
  742. if(!strcasecmp(argv[i], "-xbgr")) pf=TJPF_XBGR;
  743. if(!strcasecmp(argv[i], "-xrgb")) pf=TJPF_XRGB;
  744. if(!strcasecmp(argv[i], "-bottomup")) flags|=TJFLAG_BOTTOMUP;
  745. if(!strcasecmp(argv[i], "-quiet")) quiet=1;
  746. if(!strcasecmp(argv[i], "-qq")) quiet=2;
  747. if(!strcasecmp(argv[i], "-scale") && i<argc-1)
  748. {
  749. int temp1=0, temp2=0, match=0;
  750. if(sscanf(argv[++i], "%d/%d", &temp1, &temp2)==2)
  751. {
  752. for(j=0; j<nsf; j++)
  753. {
  754. if((double)temp1/(double)temp2
  755. == (double)scalingfactors[j].num/(double)scalingfactors[j].denom)
  756. {
  757. sf=scalingfactors[j];
  758. match=1; break;
  759. }
  760. }
  761. if(!match) usage(argv[0]);
  762. }
  763. else usage(argv[0]);
  764. }
  765. if(!strcasecmp(argv[i], "-hflip")) xformop=TJXOP_HFLIP;
  766. if(!strcasecmp(argv[i], "-vflip")) xformop=TJXOP_VFLIP;
  767. if(!strcasecmp(argv[i], "-transpose")) xformop=TJXOP_TRANSPOSE;
  768. if(!strcasecmp(argv[i], "-transverse")) xformop=TJXOP_TRANSVERSE;
  769. if(!strcasecmp(argv[i], "-rot90")) xformop=TJXOP_ROT90;
  770. if(!strcasecmp(argv[i], "-rot180")) xformop=TJXOP_ROT180;
  771. if(!strcasecmp(argv[i], "-rot270")) xformop=TJXOP_ROT270;
  772. if(!strcasecmp(argv[i], "-grayscale")) xformopt|=TJXOPT_GRAY;
  773. if(!strcasecmp(argv[i], "-custom")) customFilter=dummyDCTFilter;
  774. if(!strcasecmp(argv[i], "-nooutput")) xformopt|=TJXOPT_NOOUTPUT;
  775. if(!strcasecmp(argv[i], "-benchtime") && i<argc-1)
  776. {
  777. double temp=atof(argv[++i]);
  778. if(temp>0.0) benchtime=temp;
  779. else usage(argv[0]);
  780. }
  781. if(!strcmp(argv[i], "-?")) usage(argv[0]);
  782. if(!strcasecmp(argv[i], "-alloc")) flags&=(~TJFLAG_NOREALLOC);
  783. if(!strcasecmp(argv[i], "-bmp")) ext="bmp";
  784. if(!strcasecmp(argv[i], "-subsamp") && i<argc-1)
  785. {
  786. i++;
  787. if(toupper(argv[i][0])=='G') subsamp=TJSAMP_GRAY;
  788. else
  789. {
  790. int temp=atoi(argv[i]);
  791. switch(temp)
  792. {
  793. case 444: subsamp=TJSAMP_444; break;
  794. case 422: subsamp=TJSAMP_422; break;
  795. case 440: subsamp=TJSAMP_440; break;
  796. case 420: subsamp=TJSAMP_420; break;
  797. }
  798. }
  799. }
  800. }
  801. }
  802. if((sf.num!=1 || sf.denom!=1) && dotile)
  803. {
  804. printf("Disabling tiled compression/decompression tests, because those tests do not\n");
  805. printf("work when scaled decompression is enabled.\n");
  806. dotile=0;
  807. }
  808. if(yuv && dotile)
  809. {
  810. printf("Disabling tiled compression/decompression tests, because those tests do not\n");
  811. printf("work when YUV encoding or decoding is enabled.\n\n");
  812. dotile=0;
  813. }
  814. if(!decomponly)
  815. {
  816. if(loadbmp(argv[1], &srcbuf, &w, &h, pf, (flags&TJFLAG_BOTTOMUP)!=0)==-1)
  817. _throwbmp("loading bitmap");
  818. temp=strrchr(argv[1], '.');
  819. if(temp!=NULL) *temp='\0';
  820. }
  821. if(quiet==1 && !decomponly)
  822. {
  823. printf("All performance values in Mpixels/sec\n\n");
  824. printf("Bitmap\tBitmap\tJPEG\tJPEG\t%s %s \tComp\tComp\tDecomp\n",
  825. dotile? "Tile ":"Image", dotile? "Tile ":"Image");
  826. printf("Format\tOrder\tSubsamp\tQual\tWidth Height\tPerf \tRatio\tPerf\n\n");
  827. }
  828. if(decomponly)
  829. {
  830. dodecomptest(argv[1]);
  831. printf("\n");
  832. goto bailout;
  833. }
  834. if(subsamp>=0 && subsamp<TJ_NUMSAMP)
  835. {
  836. for(i=maxqual; i>=minqual; i--)
  837. dotest(srcbuf, w, h, subsamp, i, argv[1]);
  838. printf("\n");
  839. }
  840. else
  841. {
  842. for(i=maxqual; i>=minqual; i--)
  843. dotest(srcbuf, w, h, TJSAMP_GRAY, i, argv[1]);
  844. printf("\n");
  845. for(i=maxqual; i>=minqual; i--)
  846. dotest(srcbuf, w, h, TJSAMP_420, i, argv[1]);
  847. printf("\n");
  848. for(i=maxqual; i>=minqual; i--)
  849. dotest(srcbuf, w, h, TJSAMP_422, i, argv[1]);
  850. printf("\n");
  851. for(i=maxqual; i>=minqual; i--)
  852. dotest(srcbuf, w, h, TJSAMP_444, i, argv[1]);
  853. printf("\n");
  854. }
  855. bailout:
  856. if(srcbuf) free(srcbuf);
  857. return retval;
  858. }