tjunittest.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. /*
  29. * This program tests the various code paths in the TurboJPEG C Wrapper
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <errno.h>
  35. #include "./tjutil.h"
  36. #include "./turbojpeg.h"
  37. #ifdef _WIN32
  38. #include <time.h>
  39. #define random() rand()
  40. #endif
  41. void usage(char *progName)
  42. {
  43. printf("\nUSAGE: %s [options]\n", progName);
  44. printf("Options:\n");
  45. printf("-yuv = test YUV encoding/decoding support\n");
  46. printf("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n");
  47. printf(" 4-byte boundary\n");
  48. printf("-alloc = test automatic buffer allocation\n");
  49. exit(1);
  50. }
  51. #define _throwtj() {printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \
  52. bailout();}
  53. #define _tj(f) {if((f)==-1) _throwtj();}
  54. #define _throw(m) {printf("ERROR: %s\n", m); bailout();}
  55. const char *subNameLong[TJ_NUMSAMP]=
  56. {
  57. "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
  58. };
  59. const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440", "411"};
  60. const char *pixFormatStr[TJ_NUMPF]=
  61. {
  62. "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale",
  63. "RGBA", "BGRA", "ABGR", "ARGB", "CMYK"
  64. };
  65. const int alphaOffset[TJ_NUMPF] = {-1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1};
  66. const int _3byteFormats[]={TJPF_RGB, TJPF_BGR};
  67. const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB,
  68. TJPF_CMYK};
  69. const int _onlyGray[]={TJPF_GRAY};
  70. const int _onlyRGB[]={TJPF_RGB};
  71. int doyuv=0, alloc=0, pad=4;
  72. int exitStatus=0;
  73. #define bailout() {exitStatus=-1; goto bailout;}
  74. void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
  75. {
  76. int roffset=tjRedOffset[pf];
  77. int goffset=tjGreenOffset[pf];
  78. int boffset=tjBlueOffset[pf];
  79. int ps=tjPixelSize[pf];
  80. int index, row, col, halfway=16;
  81. if(pf==TJPF_GRAY)
  82. {
  83. memset(buf, 0, w*h*ps);
  84. for(row=0; row<h; row++)
  85. {
  86. for(col=0; col<w; col++)
  87. {
  88. if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
  89. else index=row*w+col;
  90. if(((row/8)+(col/8))%2==0) buf[index]=(row<halfway)? 255:0;
  91. else buf[index]=(row<halfway)? 76:226;
  92. }
  93. }
  94. }
  95. else if(pf==TJPF_CMYK)
  96. {
  97. memset(buf, 255, w*h*ps);
  98. for(row=0; row<h; row++)
  99. {
  100. for(col=0; col<w; col++)
  101. {
  102. if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
  103. else index=row*w+col;
  104. if(((row/8)+(col/8))%2==0)
  105. {
  106. if(row>=halfway) buf[index*ps+3]=0;
  107. }
  108. else
  109. {
  110. buf[index*ps+2]=0;
  111. if(row<halfway) buf[index*ps+1]=0;
  112. }
  113. }
  114. }
  115. }
  116. else
  117. {
  118. memset(buf, 0, w*h*ps);
  119. for(row=0; row<h; row++)
  120. {
  121. for(col=0; col<w; col++)
  122. {
  123. if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
  124. else index=row*w+col;
  125. if(((row/8)+(col/8))%2==0)
  126. {
  127. if(row<halfway)
  128. {
  129. buf[index*ps+roffset]=255;
  130. buf[index*ps+goffset]=255;
  131. buf[index*ps+boffset]=255;
  132. }
  133. }
  134. else
  135. {
  136. buf[index*ps+roffset]=255;
  137. if(row>=halfway) buf[index*ps+goffset]=255;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. #define checkval(v, cv) { \
  144. if(v<cv-1 || v>cv+1) { \
  145. printf("\nComp. %s at %d,%d should be %d, not %d\n", \
  146. #v, row, col, cv, v); \
  147. retval=0; exitStatus=-1; goto bailout; \
  148. }}
  149. #define checkval0(v) { \
  150. if(v>1) { \
  151. printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \
  152. retval=0; exitStatus=-1; goto bailout; \
  153. }}
  154. #define checkval255(v) { \
  155. if(v<254) { \
  156. printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \
  157. retval=0; exitStatus=-1; goto bailout; \
  158. }}
  159. int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp,
  160. tjscalingfactor sf, int flags)
  161. {
  162. int roffset=tjRedOffset[pf];
  163. int goffset=tjGreenOffset[pf];
  164. int boffset=tjBlueOffset[pf];
  165. int aoffset=alphaOffset[pf];
  166. int ps=tjPixelSize[pf];
  167. int index, row, col, retval=1;
  168. int halfway=16*sf.num/sf.denom;
  169. int blocksize=8*sf.num/sf.denom;
  170. if(pf==TJPF_CMYK)
  171. {
  172. for(row=0; row<h; row++)
  173. {
  174. for(col=0; col<w; col++)
  175. {
  176. unsigned char c, m, y, k;
  177. if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
  178. else index=row*w+col;
  179. c=buf[index*ps];
  180. m=buf[index*ps+1];
  181. y=buf[index*ps+2];
  182. k=buf[index*ps+3];
  183. if(((row/blocksize)+(col/blocksize))%2==0)
  184. {
  185. checkval255(c); checkval255(m); checkval255(y);
  186. if(row<halfway) checkval255(k)
  187. else checkval0(k)
  188. }
  189. else
  190. {
  191. checkval255(c); checkval0(y); checkval255(k);
  192. if(row<halfway) checkval0(m)
  193. else checkval255(m)
  194. }
  195. }
  196. }
  197. return 1;
  198. }
  199. for(row=0; row<h; row++)
  200. {
  201. for(col=0; col<w; col++)
  202. {
  203. unsigned char r, g, b, a;
  204. if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
  205. else index=row*w+col;
  206. r=buf[index*ps+roffset];
  207. g=buf[index*ps+goffset];
  208. b=buf[index*ps+boffset];
  209. a=aoffset>=0? buf[index*ps+aoffset]:0xFF;
  210. if(((row/blocksize)+(col/blocksize))%2==0)
  211. {
  212. if(row<halfway)
  213. {
  214. checkval255(r); checkval255(g); checkval255(b);
  215. }
  216. else
  217. {
  218. checkval0(r); checkval0(g); checkval0(b);
  219. }
  220. }
  221. else
  222. {
  223. if(subsamp==TJSAMP_GRAY)
  224. {
  225. if(row<halfway)
  226. {
  227. checkval(r, 76); checkval(g, 76); checkval(b, 76);
  228. }
  229. else
  230. {
  231. checkval(r, 226); checkval(g, 226); checkval(b, 226);
  232. }
  233. }
  234. else
  235. {
  236. if(row<halfway)
  237. {
  238. checkval255(r); checkval0(g); checkval0(b);
  239. }
  240. else
  241. {
  242. checkval255(r); checkval255(g); checkval0(b);
  243. }
  244. }
  245. }
  246. checkval255(a);
  247. }
  248. }
  249. bailout:
  250. if(retval==0)
  251. {
  252. for(row=0; row<h; row++)
  253. {
  254. for(col=0; col<w; col++)
  255. {
  256. if(pf==TJPF_CMYK)
  257. printf("%.3d/%.3d/%.3d/%.3d ", buf[(row*w+col)*ps],
  258. buf[(row*w+col)*ps+1], buf[(row*w+col)*ps+2],
  259. buf[(row*w+col)*ps+3]);
  260. else
  261. printf("%.3d/%.3d/%.3d ", buf[(row*w+col)*ps+roffset],
  262. buf[(row*w+col)*ps+goffset], buf[(row*w+col)*ps+boffset]);
  263. }
  264. printf("\n");
  265. }
  266. }
  267. return retval;
  268. }
  269. #define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
  270. int checkBufYUV(unsigned char *buf, int w, int h, int subsamp,
  271. tjscalingfactor sf)
  272. {
  273. int row, col;
  274. int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8;
  275. int pw=PAD(w, hsf), ph=PAD(h, vsf);
  276. int cw=pw/hsf, ch=ph/vsf;
  277. int ypitch=PAD(pw, pad), uvpitch=PAD(cw, pad);
  278. int retval=1;
  279. int halfway=16*sf.num/sf.denom;
  280. int blocksize=8*sf.num/sf.denom;
  281. for(row=0; row<ph; row++)
  282. {
  283. for(col=0; col<pw; col++)
  284. {
  285. unsigned char y=buf[ypitch*row+col];
  286. if(((row/blocksize)+(col/blocksize))%2==0)
  287. {
  288. if(row<halfway) checkval255(y) else checkval0(y);
  289. }
  290. else
  291. {
  292. if(row<halfway) checkval(y, 76) else checkval(y, 226);
  293. }
  294. }
  295. }
  296. if(subsamp!=TJSAMP_GRAY)
  297. {
  298. int halfway=16/vsf*sf.num/sf.denom;
  299. for(row=0; row<ch; row++)
  300. {
  301. for(col=0; col<cw; col++)
  302. {
  303. unsigned char u=buf[ypitch*ph + (uvpitch*row+col)],
  304. v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)];
  305. if(((row*vsf/blocksize)+(col*hsf/blocksize))%2==0)
  306. {
  307. checkval(u, 128); checkval(v, 128);
  308. }
  309. else
  310. {
  311. if(row<halfway)
  312. {
  313. checkval(u, 85); checkval255(v);
  314. }
  315. else
  316. {
  317. checkval0(u); checkval(v, 149);
  318. }
  319. }
  320. }
  321. }
  322. }
  323. bailout:
  324. if(retval==0)
  325. {
  326. for(row=0; row<ph; row++)
  327. {
  328. for(col=0; col<pw; col++)
  329. printf("%.3d ", buf[ypitch*row+col]);
  330. printf("\n");
  331. }
  332. printf("\n");
  333. for(row=0; row<ch; row++)
  334. {
  335. for(col=0; col<cw; col++)
  336. printf("%.3d ", buf[ypitch*ph + (uvpitch*row+col)]);
  337. printf("\n");
  338. }
  339. printf("\n");
  340. for(row=0; row<ch; row++)
  341. {
  342. for(col=0; col<cw; col++)
  343. printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]);
  344. printf("\n");
  345. }
  346. }
  347. return retval;
  348. }
  349. void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
  350. {
  351. FILE *file=fopen(filename, "wb");
  352. if(!file || fwrite(jpegBuf, jpegSize, 1, file)!=1)
  353. {
  354. printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
  355. bailout();
  356. }
  357. bailout:
  358. if(file) fclose(file);
  359. }
  360. void compTest(tjhandle handle, unsigned char **dstBuf,
  361. unsigned long *dstSize, int w, int h, int pf, char *basename,
  362. int subsamp, int jpegQual, int flags)
  363. {
  364. char tempStr[1024]; unsigned char *srcBuf=NULL, *yuvBuf=NULL;
  365. const char *pfStr=pixFormatStr[pf];
  366. const char *buStrLong=(flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ";
  367. const char *buStr=(flags&TJFLAG_BOTTOMUP)? "BU":"TD";
  368. if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL)
  369. _throw("Memory allocation failure");
  370. initBuf(srcBuf, w, h, pf, flags);
  371. if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize);
  372. if(!alloc) flags|=TJFLAG_NOREALLOC;
  373. if(doyuv)
  374. {
  375. unsigned long yuvSize=tjBufSizeYUV2(w, pad, h, subsamp);
  376. tjscalingfactor sf={1, 1};
  377. tjhandle handle2=tjInitCompress();
  378. if(!handle2) _throwtj();
  379. if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL)
  380. _throw("Memory allocation failure");
  381. memset(yuvBuf, 0, yuvSize);
  382. printf("%s %s -> YUV %s ... ", pfStr, buStrLong, subNameLong[subsamp]);
  383. _tj(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp,
  384. flags));
  385. tjDestroy(handle2);
  386. if(checkBufYUV(yuvBuf, w, h, subsamp, sf)) printf("Passed.\n");
  387. else printf("FAILED!\n");
  388. printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong,
  389. jpegQual);
  390. _tj(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf,
  391. dstSize, jpegQual, flags));
  392. }
  393. else
  394. {
  395. printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp],
  396. jpegQual);
  397. _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
  398. jpegQual, flags));
  399. }
  400. snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr,
  401. subName[subsamp], jpegQual);
  402. writeJPEG(*dstBuf, *dstSize, tempStr);
  403. printf("Done.\n Result in %s\n", tempStr);
  404. bailout:
  405. if(yuvBuf) free(yuvBuf);
  406. if(srcBuf) free(srcBuf);
  407. }
  408. void _decompTest(tjhandle handle, unsigned char *jpegBuf,
  409. unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
  410. int flags, tjscalingfactor sf)
  411. {
  412. unsigned char *dstBuf=NULL, *yuvBuf=NULL;
  413. int _hdrw=0, _hdrh=0, _hdrsubsamp=-1;
  414. int scaledWidth=TJSCALED(w, sf);
  415. int scaledHeight=TJSCALED(h, sf);
  416. unsigned long dstSize=0;
  417. _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
  418. &_hdrsubsamp));
  419. if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp)
  420. _throw("Incorrect JPEG header");
  421. dstSize=scaledWidth*scaledHeight*tjPixelSize[pf];
  422. if((dstBuf=(unsigned char *)malloc(dstSize))==NULL)
  423. _throw("Memory allocation failure");
  424. memset(dstBuf, 0, dstSize);
  425. if(doyuv)
  426. {
  427. unsigned long yuvSize=tjBufSizeYUV2(scaledWidth, pad, scaledHeight,
  428. subsamp);
  429. tjhandle handle2=tjInitDecompress();
  430. if(!handle2) _throwtj();
  431. if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL)
  432. _throw("Memory allocation failure");
  433. memset(yuvBuf, 0, yuvSize);
  434. printf("JPEG -> YUV %s ", subNameLong[subsamp]);
  435. if(sf.num!=1 || sf.denom!=1)
  436. printf("%d/%d ... ", sf.num, sf.denom);
  437. else printf("... ");
  438. _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth,
  439. pad, scaledHeight, flags));
  440. if(checkBufYUV(yuvBuf, scaledWidth, scaledHeight, subsamp, sf))
  441. printf("Passed.\n");
  442. else printf("FAILED!\n");
  443. printf("YUV %s -> %s %s ... ", subNameLong[subsamp], pixFormatStr[pf],
  444. (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
  445. _tj(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0,
  446. scaledHeight, pf, flags));
  447. tjDestroy(handle2);
  448. }
  449. else
  450. {
  451. printf("JPEG -> %s %s ", pixFormatStr[pf],
  452. (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
  453. if(sf.num!=1 || sf.denom!=1)
  454. printf("%d/%d ... ", sf.num, sf.denom);
  455. else printf("... ");
  456. _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
  457. scaledHeight, pf, flags));
  458. }
  459. if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
  460. printf("Passed.");
  461. else printf("FAILED!");
  462. printf("\n");
  463. bailout:
  464. if(yuvBuf) free(yuvBuf);
  465. if(dstBuf) free(dstBuf);
  466. }
  467. void decompTest(tjhandle handle, unsigned char *jpegBuf,
  468. unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
  469. int flags)
  470. {
  471. int i, n=0;
  472. tjscalingfactor *sf=tjGetScalingFactors(&n);
  473. if(!sf || !n) _throwtj();
  474. for(i=0; i<n; i++)
  475. {
  476. if(subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY ||
  477. (subsamp==TJSAMP_411 && sf[i].num==1 &&
  478. (sf[i].denom==2 || sf[i].denom==1)) ||
  479. (subsamp!=TJSAMP_411 && sf[i].num==1 &&
  480. (sf[i].denom==4 || sf[i].denom==2 || sf[i].denom==1)))
  481. _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
  482. flags, sf[i]);
  483. }
  484. bailout:
  485. return;
  486. }
  487. void doTest(int w, int h, const int *formats, int nformats, int subsamp,
  488. char *basename)
  489. {
  490. tjhandle chandle=NULL, dhandle=NULL;
  491. unsigned char *dstBuf=NULL;
  492. unsigned long size=0; int pfi, pf, i;
  493. if(!alloc)
  494. size=tjBufSize(w, h, subsamp);
  495. if(size!=0)
  496. if((dstBuf=(unsigned char *)tjAlloc(size))==NULL)
  497. _throw("Memory allocation failure.");
  498. if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL)
  499. _throwtj();
  500. for(pfi=0; pfi<nformats; pfi++)
  501. {
  502. for(i=0; i<2; i++)
  503. {
  504. int flags=0;
  505. if(subsamp==TJSAMP_422 || subsamp==TJSAMP_420 || subsamp==TJSAMP_440 ||
  506. subsamp==TJSAMP_411)
  507. flags|=TJFLAG_FASTUPSAMPLE;
  508. if(i==1) flags|=TJFLAG_BOTTOMUP;
  509. pf=formats[pfi];
  510. compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100,
  511. flags);
  512. decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp,
  513. flags);
  514. if(pf>=TJPF_RGBX && pf<=TJPF_XRGB)
  515. {
  516. printf("\n");
  517. decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX),
  518. basename, subsamp, flags);
  519. }
  520. printf("\n");
  521. }
  522. }
  523. printf("--------------------\n\n");
  524. bailout:
  525. if(chandle) tjDestroy(chandle);
  526. if(dhandle) tjDestroy(dhandle);
  527. if(dstBuf) tjFree(dstBuf);
  528. }
  529. void bufSizeTest(void)
  530. {
  531. int w, h, i, subsamp;
  532. unsigned char *srcBuf=NULL, *dstBuf=NULL;
  533. tjhandle handle=NULL;
  534. unsigned long dstSize=0;
  535. if((handle=tjInitCompress())==NULL) _throwtj();
  536. printf("Buffer size regression test\n");
  537. for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++)
  538. {
  539. for(w=1; w<48; w++)
  540. {
  541. int maxh=(w==1)? 2048:48;
  542. for(h=1; h<maxh; h++)
  543. {
  544. if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
  545. if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL)
  546. _throw("Memory allocation failure");
  547. if(!alloc || doyuv)
  548. {
  549. if(doyuv) dstSize=tjBufSizeYUV2(w, pad, h, subsamp);
  550. else dstSize=tjBufSize(w, h, subsamp);
  551. if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
  552. _throw("Memory allocation failure");
  553. }
  554. for(i=0; i<w*h*4; i++)
  555. {
  556. if(random()<RAND_MAX/2) srcBuf[i]=0;
  557. else srcBuf[i]=255;
  558. }
  559. if(doyuv)
  560. {
  561. _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
  562. subsamp, 0));
  563. }
  564. else
  565. {
  566. _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
  567. &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
  568. }
  569. free(srcBuf); srcBuf=NULL;
  570. if(!alloc || doyuv)
  571. {
  572. tjFree(dstBuf); dstBuf=NULL;
  573. }
  574. if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
  575. _throw("Memory allocation failure");
  576. if(!alloc || doyuv)
  577. {
  578. if(doyuv) dstSize=tjBufSizeYUV2(h, pad, w, subsamp);
  579. else dstSize=tjBufSize(h, w, subsamp);
  580. if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
  581. _throw("Memory allocation failure");
  582. }
  583. for(i=0; i<h*w*4; i++)
  584. {
  585. if(random()<RAND_MAX/2) srcBuf[i]=0;
  586. else srcBuf[i]=255;
  587. }
  588. if(doyuv)
  589. {
  590. _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
  591. subsamp, 0));
  592. }
  593. else
  594. {
  595. _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
  596. &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
  597. }
  598. free(srcBuf); srcBuf=NULL;
  599. if(!alloc || doyuv)
  600. {
  601. tjFree(dstBuf); dstBuf=NULL;
  602. }
  603. }
  604. }
  605. }
  606. printf("Done. \n");
  607. bailout:
  608. if(srcBuf) free(srcBuf);
  609. if(dstBuf) tjFree(dstBuf);
  610. if(handle) tjDestroy(handle);
  611. }
  612. int main(int argc, char *argv[])
  613. {
  614. int i, num4bf=5;
  615. #ifdef _WIN32
  616. srand((unsigned int)time(NULL));
  617. #endif
  618. if(argc>1)
  619. {
  620. for(i=1; i<argc; i++)
  621. {
  622. if(!strcasecmp(argv[i], "-yuv")) doyuv=1;
  623. if(!strcasecmp(argv[i], "-noyuvpad")) pad=1;
  624. if(!strcasecmp(argv[i], "-alloc")) alloc=1;
  625. if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?"))
  626. usage(argv[0]);
  627. }
  628. }
  629. if(alloc) printf("Testing automatic buffer allocation\n");
  630. if(doyuv) num4bf=4;
  631. doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
  632. doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test");
  633. doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
  634. doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test");
  635. doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
  636. doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test");
  637. doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
  638. doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test");
  639. doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test");
  640. doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test");
  641. doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test");
  642. doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test");
  643. doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test");
  644. bufSizeTest();
  645. if(doyuv)
  646. {
  647. printf("\n--------------------\n\n");
  648. doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
  649. doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
  650. doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
  651. doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
  652. doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0");
  653. doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
  654. doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
  655. }
  656. return exitStatus;
  657. }