tjunittest.c 17 KB

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