blob: d14ec5285d86ac9d7a9ab8d8f2b63a100ccf8bbf [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{
DRC67ce3b22011-12-19 02:21:03 +000068 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale",
69 "RGBA", "BGRA", "ABGR", "ARGB"
DRCb8b359a2011-05-25 03:54:56 +000070};
71
DRC67ce3b22011-12-19 02:21:03 +000072const int alphaOffset[TJ_NUMPF] = {-1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0};
DRCc08e8c12011-09-08 23:54:40 +000073
DRCb8b359a2011-05-25 03:54:56 +000074const int _3byteFormats[]={TJPF_RGB, TJPF_BGR};
75const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB};
76const int _onlyGray[]={TJPF_GRAY};
77const int _onlyRGB[]={TJPF_RGB};
78
79enum {YUVENCODE=1, YUVDECODE};
DRC67ce3b22011-12-19 02:21:03 +000080int yuv=0, alloc=0, alpha=0;
DRCb8b359a2011-05-25 03:54:56 +000081
82int exitStatus=0;
83#define bailout() {exitStatus=-1; goto bailout;}
84
DRCb8b359a2011-05-25 03:54:56 +000085
86void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
87{
88 int roffset=tjRedOffset[pf];
89 int goffset=tjGreenOffset[pf];
90 int boffset=tjBlueOffset[pf];
91 int ps=tjPixelSize[pf];
92 int index, row, col, halfway=16;
93
94 memset(buf, 0, w*h*ps);
95 if(pf==TJPF_GRAY)
96 {
97 for(row=0; row<h; row++)
98 {
99 for(col=0; col<w; col++)
100 {
101 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
102 else index=row*w+col;
103 if(((row/8)+(col/8))%2==0) buf[index]=(row<halfway)? 255:0;
104 else buf[index]=(row<halfway)? 76:226;
105 }
106 }
107 }
108 else
109 {
110 for(row=0; row<h; row++)
111 {
112 for(col=0; col<w; col++)
113 {
114 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
115 else index=row*w+col;
116 if(((row/8)+(col/8))%2==0)
117 {
118 if(row<halfway)
119 {
120 buf[index*ps+roffset]=255;
121 buf[index*ps+goffset]=255;
122 buf[index*ps+boffset]=255;
123 }
124 }
125 else
126 {
127 buf[index*ps+roffset]=255;
128 if(row>=halfway) buf[index*ps+goffset]=255;
129 }
130 }
131 }
132 }
133}
134
135
136#define checkval(v, cv) { \
137 if(v<cv-1 || v>cv+1) { \
138 printf("\nComp. %s at %d,%d should be %d, not %d\n", \
139 #v, row, col, cv, v); \
140 retval=0; exitStatus=-1; goto bailout; \
141 }}
142
143#define checkval0(v) { \
144 if(v>1) { \
145 printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \
146 retval=0; exitStatus=-1; goto bailout; \
147 }}
148
149#define checkval255(v) { \
150 if(v<254) { \
151 printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \
152 retval=0; exitStatus=-1; goto bailout; \
153 }}
154
155
156int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp,
157 tjscalingfactor sf, int flags)
158{
159 int roffset=tjRedOffset[pf];
160 int goffset=tjGreenOffset[pf];
161 int boffset=tjBlueOffset[pf];
DRCc08e8c12011-09-08 23:54:40 +0000162 int aoffset=alphaOffset[pf];
DRCb8b359a2011-05-25 03:54:56 +0000163 int ps=tjPixelSize[pf];
164 int index, row, col, retval=1;
165 int halfway=16*sf.num/sf.denom;
166 int blocksize=8*sf.num/sf.denom;
167
168 for(row=0; row<h; row++)
169 {
170 for(col=0; col<w; col++)
171 {
DRCc08e8c12011-09-08 23:54:40 +0000172 unsigned char r, g, b, a;
DRCb8b359a2011-05-25 03:54:56 +0000173 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
174 else index=row*w+col;
175 r=buf[index*ps+roffset];
176 g=buf[index*ps+goffset];
177 b=buf[index*ps+boffset];
DRCc08e8c12011-09-08 23:54:40 +0000178 a=aoffset>=0? buf[index*ps+aoffset]:0xFF;
DRCb8b359a2011-05-25 03:54:56 +0000179 if(((row/blocksize)+(col/blocksize))%2==0)
180 {
181 if(row<halfway)
182 {
183 checkval255(r); checkval255(g); checkval255(b);
184 }
185 else
186 {
187 checkval0(r); checkval0(g); checkval0(b);
188 }
189 }
190 else
191 {
192 if(subsamp==TJSAMP_GRAY)
193 {
194 if(row<halfway)
195 {
196 checkval(r, 76); checkval(g, 76); checkval(b, 76);
197 }
198 else
199 {
200 checkval(r, 226); checkval(g, 226); checkval(b, 226);
201 }
202 }
203 else
204 {
205 if(row<halfway)
206 {
207 checkval255(r); checkval0(g); checkval0(b);
208 }
209 else
210 {
211 checkval255(r); checkval255(g); checkval0(b);
212 }
213 }
214 }
DRCc08e8c12011-09-08 23:54:40 +0000215 checkval255(a);
DRCb8b359a2011-05-25 03:54:56 +0000216 }
217 }
218
219 bailout:
220 if(retval==0)
221 {
222 printf("\n");
223 for(row=0; row<h; row++)
224 {
225 for(col=0; col<w; col++)
226 {
227 printf("%.3d/%.3d/%.3d ", buf[(row*w+col)*ps+roffset],
228 buf[(row*w+col)*ps+goffset], buf[(row*w+col)*ps+boffset]);
229 }
230 printf("\n");
231 }
232 }
233 return retval;
234}
235
236
237#define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
238
239int checkBufYUV(unsigned char *buf, int w, int h, int subsamp)
240{
241 int row, col;
242 int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8;
243 int pw=PAD(w, hsf), ph=PAD(h, vsf);
244 int cw=pw/hsf, ch=ph/vsf;
245 int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4);
246 int retval=1;
DRC215aa8b2011-05-27 02:10:42 +0000247 int halfway=16;
DRCb8b359a2011-05-25 03:54:56 +0000248
DRC215aa8b2011-05-27 02:10:42 +0000249 for(row=0; row<ph; row++)
DRCb8b359a2011-05-25 03:54:56 +0000250 {
251 for(col=0; col<pw; col++)
252 {
253 unsigned char y=buf[ypitch*row+col];
DRC215aa8b2011-05-27 02:10:42 +0000254 if(((row/8)+(col/8))%2==0)
255 {
256 if(row<halfway) checkval255(y) else checkval0(y);
257 }
258 else
259 {
260 if(row<halfway) checkval(y, 76) else checkval(y, 226);
261 }
DRCb8b359a2011-05-25 03:54:56 +0000262 }
263 }
264 if(subsamp!=TJSAMP_GRAY)
265 {
DRC215aa8b2011-05-27 02:10:42 +0000266 halfway=16/vsf;
267 for(row=0; row<ch; row++)
DRCb8b359a2011-05-25 03:54:56 +0000268 {
269 for(col=0; col<cw; col++)
270 {
271 unsigned char u=buf[ypitch*ph + (uvpitch*row+col)],
272 v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)];
273 if(((row*vsf/8)+(col*hsf/8))%2==0)
274 {
275 checkval(u, 128); checkval(v, 128);
276 }
277 else
278 {
DRC215aa8b2011-05-27 02:10:42 +0000279 if(row<halfway)
280 {
281 checkval(u, 85); checkval255(v);
282 }
283 else
284 {
285 checkval0(u); checkval(v, 149);
286 }
DRCb8b359a2011-05-25 03:54:56 +0000287 }
288 }
289 }
290 }
291
292 bailout:
293 if(retval==0)
294 {
295 for(row=0; row<ph; row++)
296 {
297 for(col=0; col<pw; col++)
298 printf("%.3d ", buf[ypitch*row+col]);
299 printf("\n");
300 }
301 printf("\n");
302 for(row=0; row<ch; row++)
303 {
304 for(col=0; col<cw; col++)
305 printf("%.3d ", buf[ypitch*ph + (uvpitch*row+col)]);
306 printf("\n");
307 }
308 printf("\n");
309 for(row=0; row<ch; row++)
310 {
311 for(col=0; col<cw; col++)
312 printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]);
313 printf("\n");
314 }
315 printf("\n");
316 }
317
318 return retval;
319}
320
321
322void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
323{
324 FILE *file=fopen(filename, "wb");
325 if(!file || fwrite(jpegBuf, jpegSize, 1, file)!=1)
326 {
327 printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
328 bailout();
329 }
330
331 bailout:
332 if(file) fclose(file);
333}
334
335
336void compTest(tjhandle handle, unsigned char **dstBuf,
337 unsigned long *dstSize, int w, int h, int pf, char *basename,
338 int subsamp, int jpegQual, int flags)
339{
340 char tempStr[1024]; unsigned char *srcBuf=NULL;
341 double t;
342
343 if(yuv==YUVENCODE)
344 printf("%s %s -> %s YUV ... ", pixFormatStr[pf],
345 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp]);
346 else
347 printf("%s %s -> %s Q%d ... ", pixFormatStr[pf],
348 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp],
349 jpegQual);
350
351 if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL)
352 _throw("Memory allocation failure");
353 initBuf(srcBuf, w, h, pf, flags);
354 if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize);
355
356 t=gettime();
357 if(yuv==YUVENCODE)
358 {
359 _tj(tjEncodeYUV2(handle, srcBuf, w, 0, h, pf, *dstBuf, subsamp, flags));
360 }
361 else
362 {
363 if(!alloc)
364 {
365 flags|=TJFLAG_NOREALLOC;
DRC9b49f0e2011-07-12 03:17:23 +0000366 *dstSize=(yuv==YUVENCODE? tjBufSizeYUV(w, h, subsamp)
367 : tjBufSize(w, h, subsamp));
DRCb8b359a2011-05-25 03:54:56 +0000368 }
369 _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
370 jpegQual, flags));
371 }
372 t=gettime()-t;
373
374 if(yuv==YUVENCODE)
375 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s.yuv", basename, pixFormatStr[pf],
376 (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subName[subsamp]);
377 else
378 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename,
379 pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subName[subsamp],
380 jpegQual);
381 writeJPEG(*dstBuf, *dstSize, tempStr);
382 if(yuv==YUVENCODE)
383 {
384 if(checkBufYUV(*dstBuf, w, h, subsamp)) printf("Passed.");
385 else printf("FAILED!");
386 }
387 else printf("Done.");
388 printf(" %f ms\n Result in %s\n", t*1000., tempStr);
389
390 bailout:
391 if(srcBuf) free(srcBuf);
392}
393
394
395void _decompTest(tjhandle handle, unsigned char *jpegBuf,
396 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
397 int flags, tjscalingfactor sf)
398{
399 unsigned char *dstBuf=NULL;
400 int _hdrw=0, _hdrh=0, _hdrsubsamp=-1; double t;
401 int scaledWidth=TJSCALED(w, sf);
402 int scaledHeight=TJSCALED(h, sf);
403 unsigned long dstSize=0;
404
405 if(yuv==YUVENCODE) return;
406
407 if(yuv==YUVDECODE)
408 printf("JPEG -> YUV %s ... ", subName[subsamp]);
409 else
410 {
411 printf("JPEG -> %s %s ", pixFormatStr[pf],
412 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
413 if(sf.num!=1 || sf.denom!=1)
414 printf("%d/%d ... ", sf.num, sf.denom);
415 else printf("... ");
416 }
417
418 _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
419 &_hdrsubsamp));
420 if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp)
421 _throw("Incorrect JPEG header");
422
DRC9b49f0e2011-07-12 03:17:23 +0000423 if(yuv==YUVDECODE) dstSize=tjBufSizeYUV(w, h, subsamp);
DRCb8b359a2011-05-25 03:54:56 +0000424 else dstSize=scaledWidth*scaledHeight*tjPixelSize[pf];
425 if((dstBuf=(unsigned char *)malloc(dstSize))==NULL)
426 _throw("Memory allocation failure");
427 memset(dstBuf, 0, dstSize);
428
429 t=gettime();
430 if(yuv==YUVDECODE)
431 {
432 _tj(tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags));
433 }
434 else
435 {
436 _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
437 scaledHeight, pf, flags));
438 }
439 t=gettime()-t;
440
441 if(yuv==YUVDECODE)
442 {
443 if(checkBufYUV(dstBuf, w, h, subsamp)) printf("Passed.");
444 else printf("FAILED!");
445 }
446 else
447 {
448 if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
449 printf("Passed.");
450 else printf("FAILED!");
451 }
452 printf(" %f ms\n", t*1000.);
453
454 bailout:
455 if(dstBuf) free(dstBuf);
456}
457
458
459void decompTest(tjhandle handle, unsigned char *jpegBuf,
460 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
461 int flags)
462{
463 int i, n=0;
464 tjscalingfactor *sf=tjGetScalingFactors(&n), sf1={1, 1};
465 if(!sf || !n) _throwtj();
466
467 if((subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY) && !yuv)
468 {
469 for(i=0; i<n; i++)
470 _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
471 flags, sf[i]);
472 }
473 else
474 _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp, flags,
475 sf1);
476
477 bailout:
478 printf("\n");
479}
480
481
482void doTest(int w, int h, const int *formats, int nformats, int subsamp,
483 char *basename)
484{
485 tjhandle chandle=NULL, dhandle=NULL;
486 unsigned char *dstBuf=NULL;
487 unsigned long size=0; int pfi, pf, i;
488
489 if(!alloc)
490 {
DRC9b49f0e2011-07-12 03:17:23 +0000491 size=(yuv==YUVENCODE? tjBufSizeYUV(w, h, subsamp)
492 : tjBufSize(w, h, subsamp));
DRCb8b359a2011-05-25 03:54:56 +0000493 if((dstBuf=(unsigned char *)tjAlloc(size))==NULL)
494 _throw("Memory allocation failure.");
495 }
496
497 if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL)
498 _throwtj();
499
500 for(pfi=0; pfi<nformats; pfi++)
501 {
502 for(i=0; i<2; i++)
503 {
504 int flags=0;
505 if(i==1)
506 {
507 if(yuv==YUVDECODE) goto bailout;
508 else flags|=TJFLAG_BOTTOMUP;
509 }
510 pf=formats[pfi];
511 compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100,
512 flags);
513 decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp,
514 flags);
DRC67ce3b22011-12-19 02:21:03 +0000515 if(pf>=TJPF_RGBX && pf<=TJPF_XRGB)
516 decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX),
517 basename, subsamp, flags);
DRCb8b359a2011-05-25 03:54:56 +0000518 }
519 }
520
521 bailout:
522 if(chandle) tjDestroy(chandle);
523 if(dhandle) tjDestroy(dhandle);
524
525 if(dstBuf) tjFree(dstBuf);
526}
527
528
DRC724c56b2011-07-12 06:22:06 +0000529void bufSizeTest(void)
DRCb8b359a2011-05-25 03:54:56 +0000530{
DRC724c56b2011-07-12 06:22:06 +0000531 int w, h, i, subsamp;
DRCb8b359a2011-05-25 03:54:56 +0000532 unsigned char *srcBuf=NULL, *jpegBuf=NULL;
533 tjhandle handle=NULL;
534 unsigned long jpegSize=0;
535
536 if((handle=tjInitCompress())==NULL) _throwtj();
537
538 printf("Buffer size regression test\n");
DRC724c56b2011-07-12 06:22:06 +0000539 for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++)
DRCb8b359a2011-05-25 03:54:56 +0000540 {
DRC724c56b2011-07-12 06:22:06 +0000541 for(w=1; w<48; w++)
DRCb8b359a2011-05-25 03:54:56 +0000542 {
DRC724c56b2011-07-12 06:22:06 +0000543 int maxh=(w==1)? 2048:48;
544 for(h=1; h<maxh; h++)
DRCb8b359a2011-05-25 03:54:56 +0000545 {
DRC724c56b2011-07-12 06:22:06 +0000546 if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
547 if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000548 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000549 if(!alloc)
550 {
551 if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(w, h, subsamp)))
552 ==NULL)
553 _throw("Memory allocation failure");
554 jpegSize=tjBufSize(w, h, subsamp);
555 }
DRCb8b359a2011-05-25 03:54:56 +0000556
DRC724c56b2011-07-12 06:22:06 +0000557 for(i=0; i<w*h*4; i++)
558 {
559 if(random()<RAND_MAX/2) srcBuf[i]=0;
560 else srcBuf[i]=255;
561 }
DRCb8b359a2011-05-25 03:54:56 +0000562
DRC724c56b2011-07-12 06:22:06 +0000563 _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &jpegBuf,
564 &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
565 free(srcBuf); srcBuf=NULL;
566 tjFree(jpegBuf); jpegBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000567
DRC724c56b2011-07-12 06:22:06 +0000568 if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000569 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000570 if(!alloc)
571 {
572 if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(h, w, subsamp)))
573 ==NULL)
574 _throw("Memory allocation failure");
575 jpegSize=tjBufSize(h, w, subsamp);
576 }
DRCb8b359a2011-05-25 03:54:56 +0000577
DRC724c56b2011-07-12 06:22:06 +0000578 for(i=0; i<h*w*4; i++)
579 {
580 if(random()<RAND_MAX/2) srcBuf[i]=0;
581 else srcBuf[i]=255;
582 }
583
584 _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &jpegBuf,
585 &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
586 free(srcBuf); srcBuf=NULL;
587 tjFree(jpegBuf); jpegBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000588 }
DRCb8b359a2011-05-25 03:54:56 +0000589 }
590 }
591 printf("Done. \n");
592
593 bailout:
594 if(srcBuf) free(srcBuf);
595 if(jpegBuf) free(jpegBuf);
596 if(handle) tjDestroy(handle);
597}
598
599
600int main(int argc, char *argv[])
601{
602 int doyuv=0, i;
DRCe835ee32011-07-15 10:06:56 +0000603 #ifdef _WIN32
604 srand((unsigned int)time(NULL));
605 #endif
DRCb8b359a2011-05-25 03:54:56 +0000606 if(argc>1)
607 {
608 for(i=1; i<argc; i++)
609 {
610 if(!strcasecmp(argv[i], "-yuv")) doyuv=1;
611 if(!strcasecmp(argv[i], "-alloc")) alloc=1;
612 if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?"))
613 usage(argv[0]);
614 }
615 }
616 if(alloc) printf("Testing automatic buffer allocation\n");
617 if(doyuv) {yuv=YUVENCODE; alloc=0;}
618 doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
619 doTest(39, 41, _4byteFormats, 4, TJSAMP_444, "test");
620 if(doyuv)
621 {
622 doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
623 doTest(35, 39, _4byteFormats, 4, TJSAMP_422, "test");
624 doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
625 doTest(41, 35, _4byteFormats, 4, TJSAMP_420, "test");
626 doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
627 doTest(39, 41, _4byteFormats, 4, TJSAMP_440, "test");
628 }
629 doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test");
630 doTest(39, 41, _3byteFormats, 2, TJSAMP_GRAY, "test");
631 doTest(41, 35, _4byteFormats, 4, TJSAMP_GRAY, "test");
DRC724c56b2011-07-12 06:22:06 +0000632 if(!doyuv) bufSizeTest();
DRCb8b359a2011-05-25 03:54:56 +0000633 if(doyuv)
634 {
635 yuv=YUVDECODE;
636 doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
637 doTest(35, 39, _onlyRGB, 1, TJSAMP_444, "test_yuv1");
638 doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
639 doTest(39, 41, _onlyRGB, 1, TJSAMP_422, "test_yuv1");
640 doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
641 doTest(41, 35, _onlyRGB, 1, TJSAMP_420, "test_yuv1");
642 doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
643 doTest(35, 39, _onlyRGB, 1, TJSAMP_440, "test_yuv1");
644 doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
645 doTest(35, 39, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv1");
646 doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
647 doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test_yuv1");
648 }
649
650 return exitStatus;
651}