blob: 3a573479155a39d9dea489f1fd79fe90d76f284d [file] [log] [blame]
DRCb8b359a2011-05-25 03:54:56 +00001/*
2 * Copyright (C)2009-2011 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/*
DRCc52c5562011-06-15 02:43:42 +000030 * This program tests the various code paths in the TurboJPEG C Wrapper
DRCb8b359a2011-05-25 03:54:56 +000031 */
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <errno.h>
37#include "./tjutil.h"
38#include "./turbojpeg.h"
DRCe835ee32011-07-15 10:06:56 +000039#ifdef _WIN32
40 #include <time.h>
41 #define random() rand()
42#endif
DRCb8b359a2011-05-25 03:54:56 +000043
44
45void usage(char *progName)
46{
47 printf("\nUSAGE: %s [options]\n", progName);
48 printf("Options:\n");
49 printf("-yuv = test YUV encoding/decoding support\n");
50 printf("-alloc = test automatic buffer allocation\n");
51 exit(1);
52}
53
54
55#define _throwtj() {printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \
56 bailout();}
57#define _tj(f) {if((f)==-1) _throwtj();}
58#define _throw(m) {printf("ERROR: %s\n", m); bailout();}
59
60const char *subNameLong[TJ_NUMSAMP]=
61{
62 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0"
63};
64const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440"};
65
66const char *pixFormatStr[TJ_NUMPF]=
67{
68 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale"
69};
70
DRCc08e8c12011-09-08 23:54:40 +000071const int alphaOffset[TJ_NUMPF] = {-1, -1, 3, 3, 0, 0, -1};
72
DRCb8b359a2011-05-25 03:54:56 +000073const int _3byteFormats[]={TJPF_RGB, TJPF_BGR};
74const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB};
75const int _onlyGray[]={TJPF_GRAY};
76const int _onlyRGB[]={TJPF_RGB};
77
78enum {YUVENCODE=1, YUVDECODE};
79int yuv=0, alloc=0;
80
81int exitStatus=0;
82#define bailout() {exitStatus=-1; goto bailout;}
83
DRCb8b359a2011-05-25 03:54:56 +000084
85void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
86{
87 int roffset=tjRedOffset[pf];
88 int goffset=tjGreenOffset[pf];
89 int boffset=tjBlueOffset[pf];
90 int ps=tjPixelSize[pf];
91 int index, row, col, halfway=16;
92
93 memset(buf, 0, w*h*ps);
94 if(pf==TJPF_GRAY)
95 {
96 for(row=0; row<h; row++)
97 {
98 for(col=0; col<w; col++)
99 {
100 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
101 else index=row*w+col;
102 if(((row/8)+(col/8))%2==0) buf[index]=(row<halfway)? 255:0;
103 else buf[index]=(row<halfway)? 76:226;
104 }
105 }
106 }
107 else
108 {
109 for(row=0; row<h; row++)
110 {
111 for(col=0; col<w; col++)
112 {
113 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
114 else index=row*w+col;
115 if(((row/8)+(col/8))%2==0)
116 {
117 if(row<halfway)
118 {
119 buf[index*ps+roffset]=255;
120 buf[index*ps+goffset]=255;
121 buf[index*ps+boffset]=255;
122 }
123 }
124 else
125 {
126 buf[index*ps+roffset]=255;
127 if(row>=halfway) buf[index*ps+goffset]=255;
128 }
129 }
130 }
131 }
132}
133
134
135#define checkval(v, cv) { \
136 if(v<cv-1 || v>cv+1) { \
137 printf("\nComp. %s at %d,%d should be %d, not %d\n", \
138 #v, row, col, cv, v); \
139 retval=0; exitStatus=-1; goto bailout; \
140 }}
141
142#define checkval0(v) { \
143 if(v>1) { \
144 printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \
145 retval=0; exitStatus=-1; goto bailout; \
146 }}
147
148#define checkval255(v) { \
149 if(v<254) { \
150 printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \
151 retval=0; exitStatus=-1; goto bailout; \
152 }}
153
154
155int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp,
156 tjscalingfactor sf, int flags)
157{
158 int roffset=tjRedOffset[pf];
159 int goffset=tjGreenOffset[pf];
160 int boffset=tjBlueOffset[pf];
DRCc08e8c12011-09-08 23:54:40 +0000161 int aoffset=alphaOffset[pf];
DRCb8b359a2011-05-25 03:54:56 +0000162 int ps=tjPixelSize[pf];
163 int index, row, col, retval=1;
164 int halfway=16*sf.num/sf.denom;
165 int blocksize=8*sf.num/sf.denom;
166
167 for(row=0; row<h; row++)
168 {
169 for(col=0; col<w; col++)
170 {
DRCc08e8c12011-09-08 23:54:40 +0000171 unsigned char r, g, b, a;
DRCb8b359a2011-05-25 03:54:56 +0000172 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
173 else index=row*w+col;
174 r=buf[index*ps+roffset];
175 g=buf[index*ps+goffset];
176 b=buf[index*ps+boffset];
DRCc08e8c12011-09-08 23:54:40 +0000177 a=aoffset>=0? buf[index*ps+aoffset]:0xFF;
DRCb8b359a2011-05-25 03:54:56 +0000178 if(((row/blocksize)+(col/blocksize))%2==0)
179 {
180 if(row<halfway)
181 {
182 checkval255(r); checkval255(g); checkval255(b);
183 }
184 else
185 {
186 checkval0(r); checkval0(g); checkval0(b);
187 }
188 }
189 else
190 {
191 if(subsamp==TJSAMP_GRAY)
192 {
193 if(row<halfway)
194 {
195 checkval(r, 76); checkval(g, 76); checkval(b, 76);
196 }
197 else
198 {
199 checkval(r, 226); checkval(g, 226); checkval(b, 226);
200 }
201 }
202 else
203 {
204 if(row<halfway)
205 {
206 checkval255(r); checkval0(g); checkval0(b);
207 }
208 else
209 {
210 checkval255(r); checkval255(g); checkval0(b);
211 }
212 }
213 }
DRCc08e8c12011-09-08 23:54:40 +0000214 checkval255(a);
DRCb8b359a2011-05-25 03:54:56 +0000215 }
216 }
217
218 bailout:
219 if(retval==0)
220 {
221 printf("\n");
222 for(row=0; row<h; row++)
223 {
224 for(col=0; col<w; col++)
225 {
226 printf("%.3d/%.3d/%.3d ", buf[(row*w+col)*ps+roffset],
227 buf[(row*w+col)*ps+goffset], buf[(row*w+col)*ps+boffset]);
228 }
229 printf("\n");
230 }
231 }
232 return retval;
233}
234
235
236#define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
237
238int checkBufYUV(unsigned char *buf, int w, int h, int subsamp)
239{
240 int row, col;
241 int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8;
242 int pw=PAD(w, hsf), ph=PAD(h, vsf);
243 int cw=pw/hsf, ch=ph/vsf;
244 int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4);
245 int retval=1;
DRC215aa8b2011-05-27 02:10:42 +0000246 int halfway=16;
DRCb8b359a2011-05-25 03:54:56 +0000247
DRC215aa8b2011-05-27 02:10:42 +0000248 for(row=0; row<ph; row++)
DRCb8b359a2011-05-25 03:54:56 +0000249 {
250 for(col=0; col<pw; col++)
251 {
252 unsigned char y=buf[ypitch*row+col];
DRC215aa8b2011-05-27 02:10:42 +0000253 if(((row/8)+(col/8))%2==0)
254 {
255 if(row<halfway) checkval255(y) else checkval0(y);
256 }
257 else
258 {
259 if(row<halfway) checkval(y, 76) else checkval(y, 226);
260 }
DRCb8b359a2011-05-25 03:54:56 +0000261 }
262 }
263 if(subsamp!=TJSAMP_GRAY)
264 {
DRC215aa8b2011-05-27 02:10:42 +0000265 halfway=16/vsf;
266 for(row=0; row<ch; row++)
DRCb8b359a2011-05-25 03:54:56 +0000267 {
268 for(col=0; col<cw; col++)
269 {
270 unsigned char u=buf[ypitch*ph + (uvpitch*row+col)],
271 v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)];
272 if(((row*vsf/8)+(col*hsf/8))%2==0)
273 {
274 checkval(u, 128); checkval(v, 128);
275 }
276 else
277 {
DRC215aa8b2011-05-27 02:10:42 +0000278 if(row<halfway)
279 {
280 checkval(u, 85); checkval255(v);
281 }
282 else
283 {
284 checkval0(u); checkval(v, 149);
285 }
DRCb8b359a2011-05-25 03:54:56 +0000286 }
287 }
288 }
289 }
290
291 bailout:
292 if(retval==0)
293 {
294 for(row=0; row<ph; row++)
295 {
296 for(col=0; col<pw; col++)
297 printf("%.3d ", buf[ypitch*row+col]);
298 printf("\n");
299 }
300 printf("\n");
301 for(row=0; row<ch; row++)
302 {
303 for(col=0; col<cw; col++)
304 printf("%.3d ", buf[ypitch*ph + (uvpitch*row+col)]);
305 printf("\n");
306 }
307 printf("\n");
308 for(row=0; row<ch; row++)
309 {
310 for(col=0; col<cw; col++)
311 printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]);
312 printf("\n");
313 }
314 printf("\n");
315 }
316
317 return retval;
318}
319
320
321void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
322{
323 FILE *file=fopen(filename, "wb");
324 if(!file || fwrite(jpegBuf, jpegSize, 1, file)!=1)
325 {
326 printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
327 bailout();
328 }
329
330 bailout:
331 if(file) fclose(file);
332}
333
334
335void compTest(tjhandle handle, unsigned char **dstBuf,
336 unsigned long *dstSize, int w, int h, int pf, char *basename,
337 int subsamp, int jpegQual, int flags)
338{
339 char tempStr[1024]; unsigned char *srcBuf=NULL;
340 double t;
341
342 if(yuv==YUVENCODE)
343 printf("%s %s -> %s YUV ... ", pixFormatStr[pf],
344 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp]);
345 else
346 printf("%s %s -> %s Q%d ... ", pixFormatStr[pf],
347 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp],
348 jpegQual);
349
350 if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL)
351 _throw("Memory allocation failure");
352 initBuf(srcBuf, w, h, pf, flags);
353 if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize);
354
355 t=gettime();
356 if(yuv==YUVENCODE)
357 {
358 _tj(tjEncodeYUV2(handle, srcBuf, w, 0, h, pf, *dstBuf, subsamp, flags));
359 }
360 else
361 {
362 if(!alloc)
363 {
364 flags|=TJFLAG_NOREALLOC;
DRC9b49f0e2011-07-12 03:17:23 +0000365 *dstSize=(yuv==YUVENCODE? tjBufSizeYUV(w, h, subsamp)
366 : tjBufSize(w, h, subsamp));
DRCb8b359a2011-05-25 03:54:56 +0000367 }
368 _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
369 jpegQual, flags));
370 }
371 t=gettime()-t;
372
373 if(yuv==YUVENCODE)
374 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s.yuv", basename, pixFormatStr[pf],
375 (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subName[subsamp]);
376 else
377 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename,
378 pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subName[subsamp],
379 jpegQual);
380 writeJPEG(*dstBuf, *dstSize, tempStr);
381 if(yuv==YUVENCODE)
382 {
383 if(checkBufYUV(*dstBuf, w, h, subsamp)) printf("Passed.");
384 else printf("FAILED!");
385 }
386 else printf("Done.");
387 printf(" %f ms\n Result in %s\n", t*1000., tempStr);
388
389 bailout:
390 if(srcBuf) free(srcBuf);
391}
392
393
394void _decompTest(tjhandle handle, unsigned char *jpegBuf,
395 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
396 int flags, tjscalingfactor sf)
397{
398 unsigned char *dstBuf=NULL;
399 int _hdrw=0, _hdrh=0, _hdrsubsamp=-1; double t;
400 int scaledWidth=TJSCALED(w, sf);
401 int scaledHeight=TJSCALED(h, sf);
402 unsigned long dstSize=0;
403
404 if(yuv==YUVENCODE) return;
405
406 if(yuv==YUVDECODE)
407 printf("JPEG -> YUV %s ... ", subName[subsamp]);
408 else
409 {
410 printf("JPEG -> %s %s ", pixFormatStr[pf],
411 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
412 if(sf.num!=1 || sf.denom!=1)
413 printf("%d/%d ... ", sf.num, sf.denom);
414 else printf("... ");
415 }
416
417 _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
418 &_hdrsubsamp));
419 if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp)
420 _throw("Incorrect JPEG header");
421
DRC9b49f0e2011-07-12 03:17:23 +0000422 if(yuv==YUVDECODE) dstSize=tjBufSizeYUV(w, h, subsamp);
DRCb8b359a2011-05-25 03:54:56 +0000423 else dstSize=scaledWidth*scaledHeight*tjPixelSize[pf];
424 if((dstBuf=(unsigned char *)malloc(dstSize))==NULL)
425 _throw("Memory allocation failure");
426 memset(dstBuf, 0, dstSize);
427
428 t=gettime();
429 if(yuv==YUVDECODE)
430 {
431 _tj(tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags));
432 }
433 else
434 {
435 _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
436 scaledHeight, pf, flags));
437 }
438 t=gettime()-t;
439
440 if(yuv==YUVDECODE)
441 {
442 if(checkBufYUV(dstBuf, w, h, subsamp)) printf("Passed.");
443 else printf("FAILED!");
444 }
445 else
446 {
447 if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
448 printf("Passed.");
449 else printf("FAILED!");
450 }
451 printf(" %f ms\n", t*1000.);
452
453 bailout:
454 if(dstBuf) free(dstBuf);
455}
456
457
458void decompTest(tjhandle handle, unsigned char *jpegBuf,
459 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
460 int flags)
461{
462 int i, n=0;
463 tjscalingfactor *sf=tjGetScalingFactors(&n), sf1={1, 1};
464 if(!sf || !n) _throwtj();
465
466 if((subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY) && !yuv)
467 {
468 for(i=0; i<n; i++)
469 _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
470 flags, sf[i]);
471 }
472 else
473 _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp, flags,
474 sf1);
475
476 bailout:
477 printf("\n");
478}
479
480
481void doTest(int w, int h, const int *formats, int nformats, int subsamp,
482 char *basename)
483{
484 tjhandle chandle=NULL, dhandle=NULL;
485 unsigned char *dstBuf=NULL;
486 unsigned long size=0; int pfi, pf, i;
487
488 if(!alloc)
489 {
DRC9b49f0e2011-07-12 03:17:23 +0000490 size=(yuv==YUVENCODE? tjBufSizeYUV(w, h, subsamp)
491 : tjBufSize(w, h, subsamp));
DRCb8b359a2011-05-25 03:54:56 +0000492 if((dstBuf=(unsigned char *)tjAlloc(size))==NULL)
493 _throw("Memory allocation failure.");
494 }
495
496 if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL)
497 _throwtj();
498
499 for(pfi=0; pfi<nformats; pfi++)
500 {
501 for(i=0; i<2; i++)
502 {
503 int flags=0;
504 if(i==1)
505 {
506 if(yuv==YUVDECODE) goto bailout;
507 else flags|=TJFLAG_BOTTOMUP;
508 }
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 }
515 }
516
517 bailout:
518 if(chandle) tjDestroy(chandle);
519 if(dhandle) tjDestroy(dhandle);
520
521 if(dstBuf) tjFree(dstBuf);
522}
523
524
DRC724c56b2011-07-12 06:22:06 +0000525void bufSizeTest(void)
DRCb8b359a2011-05-25 03:54:56 +0000526{
DRC724c56b2011-07-12 06:22:06 +0000527 int w, h, i, subsamp;
DRCb8b359a2011-05-25 03:54:56 +0000528 unsigned char *srcBuf=NULL, *jpegBuf=NULL;
529 tjhandle handle=NULL;
530 unsigned long jpegSize=0;
531
532 if((handle=tjInitCompress())==NULL) _throwtj();
533
534 printf("Buffer size regression test\n");
DRC724c56b2011-07-12 06:22:06 +0000535 for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++)
DRCb8b359a2011-05-25 03:54:56 +0000536 {
DRC724c56b2011-07-12 06:22:06 +0000537 for(w=1; w<48; w++)
DRCb8b359a2011-05-25 03:54:56 +0000538 {
DRC724c56b2011-07-12 06:22:06 +0000539 int maxh=(w==1)? 2048:48;
540 for(h=1; h<maxh; h++)
DRCb8b359a2011-05-25 03:54:56 +0000541 {
DRC724c56b2011-07-12 06:22:06 +0000542 if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
543 if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000544 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000545 if(!alloc)
546 {
547 if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(w, h, subsamp)))
548 ==NULL)
549 _throw("Memory allocation failure");
550 jpegSize=tjBufSize(w, h, subsamp);
551 }
DRCb8b359a2011-05-25 03:54:56 +0000552
DRC724c56b2011-07-12 06:22:06 +0000553 for(i=0; i<w*h*4; i++)
554 {
555 if(random()<RAND_MAX/2) srcBuf[i]=0;
556 else srcBuf[i]=255;
557 }
DRCb8b359a2011-05-25 03:54:56 +0000558
DRC724c56b2011-07-12 06:22:06 +0000559 _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &jpegBuf,
560 &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
561 free(srcBuf); srcBuf=NULL;
562 tjFree(jpegBuf); jpegBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000563
DRC724c56b2011-07-12 06:22:06 +0000564 if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000565 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000566 if(!alloc)
567 {
568 if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(h, w, subsamp)))
569 ==NULL)
570 _throw("Memory allocation failure");
571 jpegSize=tjBufSize(h, w, subsamp);
572 }
DRCb8b359a2011-05-25 03:54:56 +0000573
DRC724c56b2011-07-12 06:22:06 +0000574 for(i=0; i<h*w*4; i++)
575 {
576 if(random()<RAND_MAX/2) srcBuf[i]=0;
577 else srcBuf[i]=255;
578 }
579
580 _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &jpegBuf,
581 &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
582 free(srcBuf); srcBuf=NULL;
583 tjFree(jpegBuf); jpegBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000584 }
DRCb8b359a2011-05-25 03:54:56 +0000585 }
586 }
587 printf("Done. \n");
588
589 bailout:
590 if(srcBuf) free(srcBuf);
591 if(jpegBuf) free(jpegBuf);
592 if(handle) tjDestroy(handle);
593}
594
595
596int main(int argc, char *argv[])
597{
598 int doyuv=0, i;
DRCe835ee32011-07-15 10:06:56 +0000599 #ifdef _WIN32
600 srand((unsigned int)time(NULL));
601 #endif
DRCb8b359a2011-05-25 03:54:56 +0000602 if(argc>1)
603 {
604 for(i=1; i<argc; i++)
605 {
606 if(!strcasecmp(argv[i], "-yuv")) doyuv=1;
607 if(!strcasecmp(argv[i], "-alloc")) alloc=1;
608 if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?"))
609 usage(argv[0]);
610 }
611 }
612 if(alloc) printf("Testing automatic buffer allocation\n");
613 if(doyuv) {yuv=YUVENCODE; alloc=0;}
614 doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
615 doTest(39, 41, _4byteFormats, 4, TJSAMP_444, "test");
616 if(doyuv)
617 {
618 doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
619 doTest(35, 39, _4byteFormats, 4, TJSAMP_422, "test");
620 doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
621 doTest(41, 35, _4byteFormats, 4, TJSAMP_420, "test");
622 doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
623 doTest(39, 41, _4byteFormats, 4, TJSAMP_440, "test");
624 }
625 doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test");
626 doTest(39, 41, _3byteFormats, 2, TJSAMP_GRAY, "test");
627 doTest(41, 35, _4byteFormats, 4, TJSAMP_GRAY, "test");
DRC724c56b2011-07-12 06:22:06 +0000628 if(!doyuv) bufSizeTest();
DRCb8b359a2011-05-25 03:54:56 +0000629 if(doyuv)
630 {
631 yuv=YUVDECODE;
632 doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
633 doTest(35, 39, _onlyRGB, 1, TJSAMP_444, "test_yuv1");
634 doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
635 doTest(39, 41, _onlyRGB, 1, TJSAMP_422, "test_yuv1");
636 doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
637 doTest(41, 35, _onlyRGB, 1, TJSAMP_420, "test_yuv1");
638 doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
639 doTest(35, 39, _onlyRGB, 1, TJSAMP_440, "test_yuv1");
640 doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
641 doTest(35, 39, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv1");
642 doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
643 doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test_yuv1");
644 }
645
646 return exitStatus;
647}