PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/chromium/third_party/libjpeg_turbo/jpgtest.cxx

https://gitlab.com/f3822/qtwebengine-chromium
C++ | 392 lines | 351 code | 24 blank | 17 comment | 99 complexity | 4003b985704b61f091dd3e7da387b5e7 MD5 | raw file
  1. /* Copyright (C)2004 Landmark Graphics Corporation
  2. * Copyright (C)2005, 2006 Sun Microsystems, Inc.
  3. * Copyright (C)2009 D. R. Commander
  4. *
  5. * This library is free software and may be redistributed and/or modified under
  6. * the terms of the wxWindows Library License, Version 3.1 or (at your option)
  7. * any later version. The full license is in the LICENSE.txt file included
  8. * with this distribution.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * wxWindows Library License for more details.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include "./bmp.h"
  20. #include "./rrutil.h"
  21. #include "./rrtimer.h"
  22. #include "./turbojpeg.h"
  23. #define _catch(f) {if((f)==-1) {printf("Error in %s:\n%s\n", #f, tjGetErrorStr()); goto bailout;}}
  24. int forcemmx=0, forcesse=0, forcesse2=0, forcesse3=0, fastupsample=0;
  25. const int _ps[BMPPIXELFORMATS]={3, 4, 3, 4, 4, 4};
  26. const int _flags[BMPPIXELFORMATS]={0, 0, TJ_BGR, TJ_BGR,
  27. TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST};
  28. const int _rindex[BMPPIXELFORMATS]={0, 0, 2, 2, 3, 1};
  29. const int _gindex[BMPPIXELFORMATS]={1, 1, 1, 1, 2, 2};
  30. const int _bindex[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3};
  31. const char *_pfname[]={"RGB", "RGBA", "BGR", "BGRA", "ABGR", "ARGB"};
  32. const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY"};
  33. const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY"};
  34. void printsigfig(double val, int figs)
  35. {
  36. char format[80];
  37. double _l=log10(val); int l;
  38. if(_l<0.)
  39. {
  40. l=(int)fabs(_l);
  41. sprintf(format, "%%%d.%df", figs+l+2, figs+l);
  42. }
  43. else
  44. {
  45. l=(int)_l+1;
  46. if(figs<=l) sprintf(format, "%%.0f");
  47. else sprintf(format, "%%%d.%df", figs+1, figs-l);
  48. }
  49. printf(format, val);
  50. }
  51. void dotest(unsigned char *srcbuf, int w, int h, BMPPIXELFORMAT pf, int bu,
  52. int jpegsub, int qual, char *filename, int dotile, int useppm, int quiet)
  53. {
  54. char tempstr[1024];
  55. FILE *outfile; tjhandle hnd;
  56. unsigned char **jpegbuf=NULL, *rgbbuf=NULL;
  57. rrtimer timer; double elapsed;
  58. int jpgbufsize=0, i, j, tilesizex, tilesizey, numtilesx, numtilesy, ITER;
  59. unsigned long *comptilesize=NULL;
  60. int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0)
  61. |(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0)
  62. |(fastupsample?TJ_FASTUPSAMPLE:0);
  63. int ps=_ps[pf];
  64. int pitch=w*ps;
  65. flags |= _flags[pf];
  66. if(bu) flags |= TJ_BOTTOMUP;
  67. if((rgbbuf=(unsigned char *)malloc(pitch*h)) == NULL)
  68. {
  69. puts("ERROR: Could not allocate image buffer.");
  70. exit(1);
  71. }
  72. if(!quiet) printf("\n>>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", _pfname[pf],
  73. bu?"Bottom-up":"Top-down", _subnamel[jpegsub], qual);
  74. if(dotile) {tilesizex=tilesizey=4;} else {tilesizex=w; tilesizey=h;}
  75. do
  76. {
  77. tilesizex*=2; if(tilesizex>w) tilesizex=w;
  78. tilesizey*=2; if(tilesizey>h) tilesizey=h;
  79. numtilesx=(w+tilesizex-1)/tilesizex;
  80. numtilesy=(h+tilesizey-1)/tilesizey;
  81. if((comptilesize=(unsigned long *)malloc(sizeof(unsigned long)*numtilesx*numtilesy)) == NULL
  82. || (jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)*numtilesx*numtilesy)) == NULL)
  83. {
  84. puts("ERROR: Could not allocate image buffers.");
  85. goto bailout;
  86. }
  87. memset(jpegbuf, 0, sizeof(unsigned char *)*numtilesx*numtilesy);
  88. for(i=0; i<numtilesx*numtilesy; i++)
  89. {
  90. if((jpegbuf[i]=(unsigned char *)malloc(TJBUFSIZE(tilesizex, tilesizey))) == NULL)
  91. {
  92. puts("ERROR: Could not allocate image buffers.");
  93. goto bailout;
  94. }
  95. }
  96. // Compression test
  97. if(quiet) printf("%s\t%s\t%s\t%d\t", _pfname[pf], bu?"BU":"TD",
  98. _subnamel[jpegsub], qual);
  99. for(i=0; i<h; i++) memcpy(&rgbbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
  100. if((hnd=tjInitCompress())==NULL)
  101. {
  102. printf("Error in tjInitCompress():\n%s\n", tjGetErrorStr());
  103. goto bailout;
  104. }
  105. _catch(tjCompress(hnd, rgbbuf, tilesizex, pitch, tilesizey, ps,
  106. jpegbuf[0], &comptilesize[0], jpegsub, qual, flags));
  107. ITER=0;
  108. timer.start();
  109. do
  110. {
  111. jpgbufsize=0; int tilen=0;
  112. for(i=0; i<h; i+=tilesizey)
  113. {
  114. for(j=0; j<w; j+=tilesizex)
  115. {
  116. int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
  117. _catch(tjCompress(hnd, &rgbbuf[pitch*i+j*ps], tempw, pitch,
  118. temph, ps, jpegbuf[tilen], &comptilesize[tilen], jpegsub, qual,
  119. flags));
  120. jpgbufsize+=comptilesize[tilen];
  121. tilen++;
  122. }
  123. }
  124. ITER++;
  125. } while((elapsed=timer.elapsed())<5.);
  126. _catch(tjDestroy(hnd));
  127. if(quiet)
  128. {
  129. if(tilesizex==w && tilesizey==h) printf("Full \t");
  130. else printf("%-4d %-4d\t", tilesizex, tilesizey);
  131. printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
  132. printf("\t");
  133. printsigfig((double)(w*h*ps)/(double)jpgbufsize, 4);
  134. printf("\t");
  135. }
  136. else
  137. {
  138. if(tilesizex==w && tilesizey==h) printf("\nFull image\n");
  139. else printf("\nTile size: %d x %d\n", tilesizex, tilesizey);
  140. printf("C--> Frame rate: %f fps\n", (double)ITER/elapsed);
  141. printf(" Output image size: %d bytes\n", jpgbufsize);
  142. printf(" Compression ratio: %f:1\n",
  143. (double)(w*h*ps)/(double)jpgbufsize);
  144. printf(" Source throughput: %f Megapixels/sec\n",
  145. (double)(w*h)/1000000.*(double)ITER/elapsed);
  146. printf(" Output bit stream: %f Megabits/sec\n",
  147. (double)jpgbufsize*8./1000000.*(double)ITER/elapsed);
  148. }
  149. if(tilesizex==w && tilesizey==h)
  150. {
  151. sprintf(tempstr, "%s_%sQ%d.jpg", filename, _subnames[jpegsub], qual);
  152. if((outfile=fopen(tempstr, "wb"))==NULL)
  153. {
  154. puts("ERROR: Could not open reference image");
  155. exit(1);
  156. }
  157. if(fwrite(jpegbuf[0], jpgbufsize, 1, outfile)!=1)
  158. {
  159. puts("ERROR: Could not write reference image");
  160. exit(1);
  161. }
  162. fclose(outfile);
  163. if(!quiet) printf("Reference image written to %s\n", tempstr);
  164. }
  165. // Decompression test
  166. memset(rgbbuf, 127, pitch*h); // Grey image means decompressor did nothing
  167. if((hnd=tjInitDecompress())==NULL)
  168. {
  169. printf("Error in tjInitDecompress():\n%s\n", tjGetErrorStr());
  170. goto bailout;
  171. }
  172. _catch(tjDecompress(hnd, jpegbuf[0], jpgbufsize, rgbbuf, tilesizex, pitch,
  173. tilesizey, ps, flags));
  174. ITER=0;
  175. timer.start();
  176. do
  177. {
  178. int tilen=0;
  179. for(i=0; i<h; i+=tilesizey)
  180. {
  181. for(j=0; j<w; j+=tilesizex)
  182. {
  183. int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
  184. _catch(tjDecompress(hnd, jpegbuf[tilen], comptilesize[tilen],
  185. &rgbbuf[pitch*i+ps*j], tempw, pitch, temph, ps, flags));
  186. tilen++;
  187. }
  188. }
  189. ITER++;
  190. } while((elapsed=timer.elapsed())<5.);
  191. _catch(tjDestroy(hnd));
  192. if(quiet)
  193. {
  194. printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
  195. printf("\n");
  196. }
  197. else
  198. {
  199. printf("D--> Frame rate: %f fps\n", (double)ITER/elapsed);
  200. printf(" Dest. throughput: %f Megapixels/sec\n",
  201. (double)(w*h)/1000000.*(double)ITER/elapsed);
  202. }
  203. if(tilesizex==w && tilesizey==h)
  204. sprintf(tempstr, "%s_%sQ%d_full.%s", filename, _subnames[jpegsub], qual,
  205. useppm?"ppm":"bmp");
  206. else sprintf(tempstr, "%s_%sQ%d_%dx%d.%s", filename, _subnames[jpegsub],
  207. qual, tilesizex, tilesizey, useppm?"ppm":"bmp");
  208. if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
  209. {
  210. printf("ERROR saving bitmap: %s\n", bmpgeterr());
  211. goto bailout;
  212. }
  213. sprintf(strrchr(tempstr, '.'), "-err.%s", useppm?"ppm":"bmp");
  214. if(!quiet)
  215. printf("Computing compression error and saving to %s.\n", tempstr);
  216. if(jpegsub==TJ_GRAYSCALE)
  217. {
  218. for(j=0; j<h; j++)
  219. {
  220. for(i=0; i<w*ps; i+=ps)
  221. {
  222. int y=(int)((double)srcbuf[w*ps*j+i+_rindex[pf]]*0.299
  223. + (double)srcbuf[w*ps*j+i+_gindex[pf]]*0.587
  224. + (double)srcbuf[w*ps*j+i+_bindex[pf]]*0.114 + 0.5);
  225. if(y>255) y=255; if(y<0) y=0;
  226. rgbbuf[pitch*j+i+_rindex[pf]]=abs(rgbbuf[pitch*j+i+_rindex[pf]]-y);
  227. rgbbuf[pitch*j+i+_gindex[pf]]=abs(rgbbuf[pitch*j+i+_gindex[pf]]-y);
  228. rgbbuf[pitch*j+i+_bindex[pf]]=abs(rgbbuf[pitch*j+i+_bindex[pf]]-y);
  229. }
  230. }
  231. }
  232. else
  233. {
  234. for(j=0; j<h; j++) for(i=0; i<w*ps; i++)
  235. rgbbuf[pitch*j+i]=abs(rgbbuf[pitch*j+i]-srcbuf[w*ps*j+i]);
  236. }
  237. if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
  238. {
  239. printf("ERROR saving bitmap: %s\n", bmpgeterr());
  240. goto bailout;
  241. }
  242. // Cleanup
  243. if(jpegbuf)
  244. {
  245. for(i=0; i<numtilesx*numtilesy; i++)
  246. {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;}
  247. free(jpegbuf); jpegbuf=NULL;
  248. }
  249. if(comptilesize) {free(comptilesize); comptilesize=NULL;}
  250. } while(tilesizex<w || tilesizey<h);
  251. if(rgbbuf) {free(rgbbuf); rgbbuf=NULL;}
  252. return;
  253. bailout:
  254. if(jpegbuf)
  255. {
  256. for(i=0; i<numtilesx*numtilesy; i++)
  257. {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;}
  258. free(jpegbuf); jpegbuf=NULL;
  259. }
  260. if(comptilesize) {free(comptilesize); comptilesize=NULL;}
  261. if(rgbbuf) {free(rgbbuf); rgbbuf=NULL;}
  262. return;
  263. }
  264. int main(int argc, char *argv[])
  265. {
  266. unsigned char *bmpbuf=NULL; int w, h, i, useppm=0;
  267. int qual, dotile=0, quiet=0, hiqual=-1; char *temp;
  268. BMPPIXELFORMAT pf=BMP_BGR;
  269. int bu=0;
  270. printf("\n");
  271. if(argc<3)
  272. {
  273. printf("USAGE: %s <Inputfile (BMP|PPM)> <%% Quality>\n\n", argv[0]);
  274. printf(" [-tile]\n");
  275. printf(" Test performance of the codec when the image is encoded\n");
  276. printf(" as separate tiles of varying sizes.\n\n");
  277. printf(" [-forcemmx] [-forcesse] [-forcesse2] [-forcesse3]\n");
  278. printf(" Force MMX, SSE, or SSE2 code paths in Intel codec\n\n");
  279. printf(" [-rgb | -bgr | -rgba | -bgra | -abgr | -argb]\n");
  280. printf(" Test the specified color conversion path in the codec (default: BGR)\n\n");
  281. printf(" [-fastupsample]\n");
  282. printf(" Use fast, inaccurate upsampling code to perform 4:2:2 and 4:2:0\n");
  283. printf(" YUV decoding in libjpeg decompressor\n\n");
  284. printf(" [-quiet]\n");
  285. printf(" Output in tabular rather than verbose format\n\n");
  286. printf(" NOTE: If the quality is specified as a range, i.e. 90-100, a separate\n");
  287. printf(" test will be performed for all quality values in the range.\n");
  288. exit(1);
  289. }
  290. if((qual=atoi(argv[2]))<1 || qual>100)
  291. {
  292. puts("ERROR: Quality must be between 1 and 100.");
  293. exit(1);
  294. }
  295. if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
  296. && sscanf(&temp[1], "%d", &hiqual)==1 && hiqual>qual && hiqual>=1
  297. && hiqual<=100) {}
  298. else hiqual=qual;
  299. if(argc>3)
  300. {
  301. for(i=3; i<argc; i++)
  302. {
  303. if(!stricmp(argv[i], "-tile")) dotile=1;
  304. if(!stricmp(argv[i], "-forcesse3"))
  305. {
  306. printf("Using SSE3 code\n");
  307. forcesse3=1;
  308. }
  309. if(!stricmp(argv[i], "-forcesse2"))
  310. {
  311. printf("Using SSE2 code\n");
  312. forcesse2=1;
  313. }
  314. if(!stricmp(argv[i], "-forcesse"))
  315. {
  316. printf("Using SSE code\n");
  317. forcesse=1;
  318. }
  319. if(!stricmp(argv[i], "-forcemmx"))
  320. {
  321. printf("Using MMX code\n");
  322. forcemmx=1;
  323. }
  324. if(!stricmp(argv[i], "-fastupsample"))
  325. {
  326. printf("Using fast upsampling code\n");
  327. fastupsample=1;
  328. }
  329. if(!stricmp(argv[i], "-rgb")) pf=BMP_RGB;
  330. if(!stricmp(argv[i], "-rgba")) pf=BMP_RGBA;
  331. if(!stricmp(argv[i], "-bgr")) pf=BMP_BGR;
  332. if(!stricmp(argv[i], "-bgra")) pf=BMP_BGRA;
  333. if(!stricmp(argv[i], "-abgr")) pf=BMP_ABGR;
  334. if(!stricmp(argv[i], "-argb")) pf=BMP_ARGB;
  335. if(!stricmp(argv[i], "-bottomup")) bu=1;
  336. if(!stricmp(argv[i], "-quiet")) quiet=1;
  337. }
  338. }
  339. if(loadbmp(argv[1], &bmpbuf, &w, &h, pf, 1, bu)==-1)
  340. {
  341. printf("ERROR loading bitmap: %s\n", bmpgeterr()); exit(1);
  342. }
  343. temp=strrchr(argv[1], '.');
  344. if(temp!=NULL)
  345. {
  346. if(!stricmp(temp, ".ppm")) useppm=1;
  347. *temp='\0';
  348. }
  349. if(quiet)
  350. {
  351. printf("All performance values in Mpixels/sec\n\n");
  352. printf("Bitmap\tBitmap\tJPEG\tJPEG\tTile Size\tCompr\tCompr\tDecomp\n");
  353. printf("Format\tOrder\tFormat\tQual\t X Y \tPerf \tRatio\tPerf\n\n");
  354. }
  355. for(i=hiqual; i>=qual; i--)
  356. dotest(bmpbuf, w, h, pf, bu, TJ_GRAYSCALE, i, argv[1], dotile, useppm, quiet);
  357. if(quiet) printf("\n");
  358. for(i=hiqual; i>=qual; i--)
  359. dotest(bmpbuf, w, h, pf, bu, TJ_420, i, argv[1], dotile, useppm, quiet);
  360. if(quiet) printf("\n");
  361. for(i=hiqual; i>=qual; i--)
  362. dotest(bmpbuf, w, h, pf, bu, TJ_422, i, argv[1], dotile, useppm, quiet);
  363. if(quiet) printf("\n");
  364. for(i=hiqual; i>=qual; i--)
  365. dotest(bmpbuf, w, h, pf, bu, TJ_444, i, argv[1], dotile, useppm, quiet);
  366. if(bmpbuf) free(bmpbuf);
  367. return 0;
  368. }