blob: 8204153de84d86b644fe7f211df41151b7bfd62f [file] [log] [blame]
DRCb8b359a2011-05-25 03:54:56 +00001/*
DRC38c99702014-02-11 09:45:18 +00002 * Copyright (C)2009-2014 D. R. Commander. All Rights Reserved.
DRCb8b359a2011-05-25 03:54:56 +00003 *
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");
DRCf610d612013-04-26 10:33:29 +000050 printf("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n");
DRCfef98522013-04-28 01:32:52 +000051 printf(" 4-byte boundary\n");
DRCb8b359a2011-05-25 03:54:56 +000052 printf("-alloc = test automatic buffer allocation\n");
53 exit(1);
54}
55
56
57#define _throwtj() {printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \
58 bailout();}
59#define _tj(f) {if((f)==-1) _throwtj();}
60#define _throw(m) {printf("ERROR: %s\n", m); bailout();}
61
62const char *subNameLong[TJ_NUMSAMP]=
63{
DRC1f3635c2013-08-18 10:19:00 +000064 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
DRCb8b359a2011-05-25 03:54:56 +000065};
DRC1f3635c2013-08-18 10:19:00 +000066const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440", "411"};
DRCb8b359a2011-05-25 03:54:56 +000067
68const char *pixFormatStr[TJ_NUMPF]=
69{
DRC67ce3b22011-12-19 02:21:03 +000070 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale",
DRCcd7c3e62013-08-23 02:49:25 +000071 "RGBA", "BGRA", "ABGR", "ARGB", "CMYK"
DRCb8b359a2011-05-25 03:54:56 +000072};
73
DRCcd7c3e62013-08-23 02:49:25 +000074const int alphaOffset[TJ_NUMPF] = {-1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1};
DRCc08e8c12011-09-08 23:54:40 +000075
DRCb8b359a2011-05-25 03:54:56 +000076const int _3byteFormats[]={TJPF_RGB, TJPF_BGR};
DRCcd7c3e62013-08-23 02:49:25 +000077const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB,
78 TJPF_CMYK};
DRCb8b359a2011-05-25 03:54:56 +000079const int _onlyGray[]={TJPF_GRAY};
80const int _onlyRGB[]={TJPF_RGB};
81
82enum {YUVENCODE=1, YUVDECODE};
DRCf610d612013-04-26 10:33:29 +000083int yuv=0, alloc=0, pad=4;
DRCb8b359a2011-05-25 03:54:56 +000084
85int exitStatus=0;
86#define bailout() {exitStatus=-1; goto bailout;}
87
DRCb8b359a2011-05-25 03:54:56 +000088
89void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
90{
91 int roffset=tjRedOffset[pf];
92 int goffset=tjGreenOffset[pf];
93 int boffset=tjBlueOffset[pf];
94 int ps=tjPixelSize[pf];
95 int index, row, col, halfway=16;
96
DRCb8b359a2011-05-25 03:54:56 +000097 if(pf==TJPF_GRAY)
98 {
DRCcd7c3e62013-08-23 02:49:25 +000099 memset(buf, 0, w*h*ps);
DRCb8b359a2011-05-25 03:54:56 +0000100 for(row=0; row<h; row++)
101 {
102 for(col=0; col<w; col++)
103 {
104 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
105 else index=row*w+col;
106 if(((row/8)+(col/8))%2==0) buf[index]=(row<halfway)? 255:0;
107 else buf[index]=(row<halfway)? 76:226;
108 }
109 }
110 }
DRCcd7c3e62013-08-23 02:49:25 +0000111 else if(pf==TJPF_CMYK)
112 {
113 memset(buf, 255, w*h*ps);
114 for(row=0; row<h; row++)
115 {
116 for(col=0; col<w; col++)
117 {
118 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
119 else index=row*w+col;
120 if(((row/8)+(col/8))%2==0)
121 {
122 if(row>=halfway) buf[index*ps+3]=0;
123 }
124 else
125 {
126 buf[index*ps+2]=0;
127 if(row<halfway) buf[index*ps+1]=0;
128 }
129 }
130 }
131 }
DRCb8b359a2011-05-25 03:54:56 +0000132 else
133 {
DRCcd7c3e62013-08-23 02:49:25 +0000134 memset(buf, 0, w*h*ps);
DRCb8b359a2011-05-25 03:54:56 +0000135 for(row=0; row<h; row++)
136 {
137 for(col=0; col<w; col++)
138 {
139 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
140 else index=row*w+col;
141 if(((row/8)+(col/8))%2==0)
142 {
143 if(row<halfway)
144 {
145 buf[index*ps+roffset]=255;
146 buf[index*ps+goffset]=255;
147 buf[index*ps+boffset]=255;
148 }
149 }
150 else
151 {
152 buf[index*ps+roffset]=255;
153 if(row>=halfway) buf[index*ps+goffset]=255;
154 }
155 }
156 }
157 }
158}
159
160
161#define checkval(v, cv) { \
162 if(v<cv-1 || v>cv+1) { \
163 printf("\nComp. %s at %d,%d should be %d, not %d\n", \
164 #v, row, col, cv, v); \
165 retval=0; exitStatus=-1; goto bailout; \
166 }}
167
168#define checkval0(v) { \
169 if(v>1) { \
170 printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \
171 retval=0; exitStatus=-1; goto bailout; \
172 }}
173
174#define checkval255(v) { \
175 if(v<254) { \
176 printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \
177 retval=0; exitStatus=-1; goto bailout; \
178 }}
179
180
181int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp,
182 tjscalingfactor sf, int flags)
183{
184 int roffset=tjRedOffset[pf];
185 int goffset=tjGreenOffset[pf];
186 int boffset=tjBlueOffset[pf];
DRCc08e8c12011-09-08 23:54:40 +0000187 int aoffset=alphaOffset[pf];
DRCb8b359a2011-05-25 03:54:56 +0000188 int ps=tjPixelSize[pf];
189 int index, row, col, retval=1;
190 int halfway=16*sf.num/sf.denom;
191 int blocksize=8*sf.num/sf.denom;
192
DRCcd7c3e62013-08-23 02:49:25 +0000193 if(pf==TJPF_CMYK)
194 {
195 for(row=0; row<h; row++)
196 {
197 for(col=0; col<w; col++)
198 {
199 unsigned char c, m, y, k;
200 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
201 else index=row*w+col;
202 c=buf[index*ps];
203 m=buf[index*ps+1];
204 y=buf[index*ps+2];
205 k=buf[index*ps+3];
206 if(((row/blocksize)+(col/blocksize))%2==0)
207 {
208 checkval255(c); checkval255(m); checkval255(y);
209 if(row<halfway) checkval255(k)
210 else checkval0(k)
211 }
212 else
213 {
214 checkval255(c); checkval0(y); checkval255(k);
215 if(row<halfway) checkval0(m)
216 else checkval255(m)
217 }
218 }
219 }
220 return 1;
221 }
222
DRCb8b359a2011-05-25 03:54:56 +0000223 for(row=0; row<h; row++)
224 {
225 for(col=0; col<w; col++)
226 {
DRCc08e8c12011-09-08 23:54:40 +0000227 unsigned char r, g, b, a;
DRCb8b359a2011-05-25 03:54:56 +0000228 if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col;
229 else index=row*w+col;
230 r=buf[index*ps+roffset];
231 g=buf[index*ps+goffset];
232 b=buf[index*ps+boffset];
DRCc08e8c12011-09-08 23:54:40 +0000233 a=aoffset>=0? buf[index*ps+aoffset]:0xFF;
DRCb8b359a2011-05-25 03:54:56 +0000234 if(((row/blocksize)+(col/blocksize))%2==0)
235 {
236 if(row<halfway)
237 {
238 checkval255(r); checkval255(g); checkval255(b);
239 }
240 else
241 {
242 checkval0(r); checkval0(g); checkval0(b);
243 }
244 }
245 else
246 {
247 if(subsamp==TJSAMP_GRAY)
248 {
249 if(row<halfway)
250 {
251 checkval(r, 76); checkval(g, 76); checkval(b, 76);
252 }
253 else
254 {
255 checkval(r, 226); checkval(g, 226); checkval(b, 226);
256 }
257 }
258 else
259 {
260 if(row<halfway)
261 {
262 checkval255(r); checkval0(g); checkval0(b);
263 }
264 else
265 {
266 checkval255(r); checkval255(g); checkval0(b);
267 }
268 }
269 }
DRCc08e8c12011-09-08 23:54:40 +0000270 checkval255(a);
DRCb8b359a2011-05-25 03:54:56 +0000271 }
272 }
273
274 bailout:
275 if(retval==0)
276 {
DRCb8b359a2011-05-25 03:54:56 +0000277 for(row=0; row<h; row++)
278 {
279 for(col=0; col<w; col++)
280 {
DRCcd7c3e62013-08-23 02:49:25 +0000281 if(pf==TJPF_CMYK)
282 printf("%.3d/%.3d/%.3d/%.3d ", buf[(row*w+col)*ps],
283 buf[(row*w+col)*ps+1], buf[(row*w+col)*ps+2],
284 buf[(row*w+col)*ps+3]);
285 else
286 printf("%.3d/%.3d/%.3d ", buf[(row*w+col)*ps+roffset],
287 buf[(row*w+col)*ps+goffset], buf[(row*w+col)*ps+boffset]);
DRCb8b359a2011-05-25 03:54:56 +0000288 }
289 printf("\n");
290 }
291 }
292 return retval;
293}
294
295
296#define PAD(v, p) ((v+(p)-1)&(~((p)-1)))
297
DRC910a3572013-10-30 23:02:57 +0000298void initBufYUV(unsigned char *buf, int w, int pad, int h, int subsamp)
299{
300 int row, col;
301 int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8;
302 int pw=PAD(w, hsf), ph=PAD(h, vsf);
303 int cw=pw/hsf, ch=ph/vsf;
304 int ypitch=PAD(pw, pad), uvpitch=PAD(cw, pad);
305 int halfway=16, blocksize=8;
306
307 memset(buf, 0, tjBufSizeYUV2(w, pad, h, subsamp));
308
309 for(row=0; row<ph; row++)
310 {
311 for(col=0; col<pw; col++)
312 {
313 unsigned char *y=&buf[ypitch*row+col];
314 if(((row/blocksize)+(col/blocksize))%2==0)
315 {
316 if(row<halfway) *y=255; else *y=0;
317 }
318 else
319 {
320 if(row<halfway) *y=76; else *y=226;
321 }
322 }
323 }
324 if(subsamp!=TJSAMP_GRAY)
325 {
326 halfway=16/vsf;
327 for(row=0; row<ch; row++)
328 {
329 for(col=0; col<cw; col++)
330 {
331 unsigned char *u=&buf[ypitch*ph + (uvpitch*row+col)],
332 *v=&buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)];
333 if(((row*vsf/blocksize)+(col*hsf/blocksize))%2==0)
334 *u=*v=128;
335 else
336 {
337 if(row<halfway)
338 {
339 *u=85; *v=255;
340 }
341 else
342 {
343 *u=0; *v=149;
344 }
345 }
346 }
347 }
348 }
349}
350
351
DRCf610d612013-04-26 10:33:29 +0000352int checkBufYUV(unsigned char *buf, int w, int h, int subsamp,
353 tjscalingfactor sf)
DRCb8b359a2011-05-25 03:54:56 +0000354{
355 int row, col;
356 int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8;
357 int pw=PAD(w, hsf), ph=PAD(h, vsf);
358 int cw=pw/hsf, ch=ph/vsf;
DRCf610d612013-04-26 10:33:29 +0000359 int ypitch=PAD(pw, pad), uvpitch=PAD(cw, pad);
DRCb8b359a2011-05-25 03:54:56 +0000360 int retval=1;
DRCf610d612013-04-26 10:33:29 +0000361 int halfway=16*sf.num/sf.denom;
362 int blocksize=8*sf.num/sf.denom;
DRCb8b359a2011-05-25 03:54:56 +0000363
DRC215aa8b2011-05-27 02:10:42 +0000364 for(row=0; row<ph; row++)
DRCb8b359a2011-05-25 03:54:56 +0000365 {
366 for(col=0; col<pw; col++)
367 {
368 unsigned char y=buf[ypitch*row+col];
DRCf610d612013-04-26 10:33:29 +0000369 if(((row/blocksize)+(col/blocksize))%2==0)
DRC215aa8b2011-05-27 02:10:42 +0000370 {
371 if(row<halfway) checkval255(y) else checkval0(y);
372 }
373 else
374 {
375 if(row<halfway) checkval(y, 76) else checkval(y, 226);
376 }
DRCb8b359a2011-05-25 03:54:56 +0000377 }
378 }
379 if(subsamp!=TJSAMP_GRAY)
380 {
DRCf610d612013-04-26 10:33:29 +0000381 int halfway=16/vsf*sf.num/sf.denom;
DRC215aa8b2011-05-27 02:10:42 +0000382 for(row=0; row<ch; row++)
DRCb8b359a2011-05-25 03:54:56 +0000383 {
384 for(col=0; col<cw; col++)
385 {
386 unsigned char u=buf[ypitch*ph + (uvpitch*row+col)],
387 v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)];
DRCf610d612013-04-26 10:33:29 +0000388 if(((row*vsf/blocksize)+(col*hsf/blocksize))%2==0)
DRCb8b359a2011-05-25 03:54:56 +0000389 {
390 checkval(u, 128); checkval(v, 128);
391 }
392 else
393 {
DRC215aa8b2011-05-27 02:10:42 +0000394 if(row<halfway)
395 {
396 checkval(u, 85); checkval255(v);
397 }
398 else
399 {
400 checkval0(u); checkval(v, 149);
401 }
DRCb8b359a2011-05-25 03:54:56 +0000402 }
403 }
404 }
405 }
406
407 bailout:
408 if(retval==0)
409 {
410 for(row=0; row<ph; row++)
411 {
412 for(col=0; col<pw; col++)
413 printf("%.3d ", buf[ypitch*row+col]);
414 printf("\n");
415 }
416 printf("\n");
417 for(row=0; row<ch; row++)
418 {
419 for(col=0; col<cw; col++)
420 printf("%.3d ", buf[ypitch*ph + (uvpitch*row+col)]);
421 printf("\n");
422 }
423 printf("\n");
424 for(row=0; row<ch; row++)
425 {
426 for(col=0; col<cw; col++)
427 printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]);
428 printf("\n");
429 }
DRCb8b359a2011-05-25 03:54:56 +0000430 }
431
432 return retval;
433}
434
435
436void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
437{
438 FILE *file=fopen(filename, "wb");
439 if(!file || fwrite(jpegBuf, jpegSize, 1, file)!=1)
440 {
441 printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
442 bailout();
443 }
444
445 bailout:
446 if(file) fclose(file);
447}
448
449
450void compTest(tjhandle handle, unsigned char **dstBuf,
451 unsigned long *dstSize, int w, int h, int pf, char *basename,
452 int subsamp, int jpegQual, int flags)
453{
454 char tempStr[1024]; unsigned char *srcBuf=NULL;
DRCfe739652013-10-31 04:53:27 +0000455 const char *pfStr=(yuv==YUVDECODE)? "YUV":pixFormatStr[pf];
456 const char *buStrLong=(flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ";
457 const char *buStr=(flags&TJFLAG_BOTTOMUP)? "BU":"TD";
DRCb8b359a2011-05-25 03:54:56 +0000458 double t;
459
DRC910a3572013-10-30 23:02:57 +0000460 if(yuv==YUVDECODE)
461 {
DRCfe739652013-10-31 04:53:27 +0000462 printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong,
463 jpegQual);
DRC910a3572013-10-30 23:02:57 +0000464 if((srcBuf=(unsigned char *)malloc(tjBufSizeYUV2(w, pad, h, subsamp)))
465 ==NULL)
466 _throw("Memory allocation failure");
467 initBufYUV(srcBuf, w, pad, h, subsamp);
468 }
DRCb8b359a2011-05-25 03:54:56 +0000469 else
DRC910a3572013-10-30 23:02:57 +0000470 {
471 if(yuv==YUVENCODE)
DRCfe739652013-10-31 04:53:27 +0000472 printf("%s %s -> %s YUV ... ", pfStr, buStrLong, subNameLong[subsamp]);
DRC910a3572013-10-30 23:02:57 +0000473 else
DRCfe739652013-10-31 04:53:27 +0000474 printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp],
DRC910a3572013-10-30 23:02:57 +0000475 jpegQual);
476 if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL)
477 _throw("Memory allocation failure");
478 initBuf(srcBuf, w, h, pf, flags);
479 }
DRCb8b359a2011-05-25 03:54:56 +0000480
DRCb8b359a2011-05-25 03:54:56 +0000481 if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize);
482
483 t=gettime();
484 if(yuv==YUVENCODE)
485 {
DRCf610d612013-04-26 10:33:29 +0000486 _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, pf, *dstBuf, pad, subsamp,
487 flags));
DRCb8b359a2011-05-25 03:54:56 +0000488 }
489 else
490 {
DRC910a3572013-10-30 23:02:57 +0000491 if(!alloc) flags|=TJFLAG_NOREALLOC;
492 if(yuv==YUVDECODE)
DRCb8b359a2011-05-25 03:54:56 +0000493 {
DRC910a3572013-10-30 23:02:57 +0000494 _tj(tjCompressFromYUV(handle, srcBuf, w, pad, h, subsamp, dstBuf,
495 dstSize, jpegQual, flags));
DRCb8b359a2011-05-25 03:54:56 +0000496 }
DRC910a3572013-10-30 23:02:57 +0000497 else
498 {
499 _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
500 jpegQual, flags));
501 }
DRCb8b359a2011-05-25 03:54:56 +0000502 }
503 t=gettime()-t;
504
505 if(yuv==YUVENCODE)
DRCfe739652013-10-31 04:53:27 +0000506 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s.yuv", basename, pfStr, buStr,
507 subName[subsamp]);
DRCb8b359a2011-05-25 03:54:56 +0000508 else
DRCfe739652013-10-31 04:53:27 +0000509 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr,
510 subName[subsamp], jpegQual);
DRCb8b359a2011-05-25 03:54:56 +0000511 writeJPEG(*dstBuf, *dstSize, tempStr);
512 if(yuv==YUVENCODE)
513 {
DRCf610d612013-04-26 10:33:29 +0000514 tjscalingfactor sf={1, 1};
515 if(checkBufYUV(*dstBuf, w, h, subsamp, sf)) printf("Passed.");
DRCb8b359a2011-05-25 03:54:56 +0000516 else printf("FAILED!");
517 }
518 else printf("Done.");
519 printf(" %f ms\n Result in %s\n", t*1000., tempStr);
520
521 bailout:
522 if(srcBuf) free(srcBuf);
523}
524
525
526void _decompTest(tjhandle handle, unsigned char *jpegBuf,
527 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
528 int flags, tjscalingfactor sf)
529{
530 unsigned char *dstBuf=NULL;
531 int _hdrw=0, _hdrh=0, _hdrsubsamp=-1; double t;
532 int scaledWidth=TJSCALED(w, sf);
533 int scaledHeight=TJSCALED(h, sf);
534 unsigned long dstSize=0;
535
536 if(yuv==YUVENCODE) return;
537
538 if(yuv==YUVDECODE)
DRC38cb1ec2013-08-23 04:45:43 +0000539 printf("JPEG -> YUV %s ", subNameLong[subsamp]);
DRCb8b359a2011-05-25 03:54:56 +0000540 else
DRCb8b359a2011-05-25 03:54:56 +0000541 printf("JPEG -> %s %s ", pixFormatStr[pf],
542 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
DRCf610d612013-04-26 10:33:29 +0000543 if(sf.num!=1 || sf.denom!=1)
544 printf("%d/%d ... ", sf.num, sf.denom);
545 else printf("... ");
DRCb8b359a2011-05-25 03:54:56 +0000546
547 _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
548 &_hdrsubsamp));
549 if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp)
550 _throw("Incorrect JPEG header");
551
DRCf610d612013-04-26 10:33:29 +0000552 if(yuv==YUVDECODE)
553 dstSize=tjBufSizeYUV2(scaledWidth, pad, scaledHeight, subsamp);
DRCb8b359a2011-05-25 03:54:56 +0000554 else dstSize=scaledWidth*scaledHeight*tjPixelSize[pf];
555 if((dstBuf=(unsigned char *)malloc(dstSize))==NULL)
556 _throw("Memory allocation failure");
557 memset(dstBuf, 0, dstSize);
558
559 t=gettime();
560 if(yuv==YUVDECODE)
561 {
DRCf610d612013-04-26 10:33:29 +0000562 _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth,
563 pad, scaledHeight, flags));
DRCb8b359a2011-05-25 03:54:56 +0000564 }
565 else
566 {
567 _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
568 scaledHeight, pf, flags));
569 }
570 t=gettime()-t;
571
572 if(yuv==YUVDECODE)
573 {
DRCf610d612013-04-26 10:33:29 +0000574 if(checkBufYUV(dstBuf, scaledWidth, scaledHeight, subsamp, sf))
575 printf("Passed.");
DRCb8b359a2011-05-25 03:54:56 +0000576 else printf("FAILED!");
577 }
578 else
579 {
580 if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
581 printf("Passed.");
582 else printf("FAILED!");
583 }
584 printf(" %f ms\n", t*1000.);
585
586 bailout:
587 if(dstBuf) free(dstBuf);
588}
589
590
591void decompTest(tjhandle handle, unsigned char *jpegBuf,
592 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
593 int flags)
594{
595 int i, n=0;
DRC418fe282013-05-07 21:17:35 +0000596 tjscalingfactor *sf=tjGetScalingFactors(&n);
DRCb8b359a2011-05-25 03:54:56 +0000597 if(!sf || !n) _throwtj();
598
DRC418fe282013-05-07 21:17:35 +0000599 for(i=0; i<n; i++)
DRCb8b359a2011-05-25 03:54:56 +0000600 {
DRC418fe282013-05-07 21:17:35 +0000601 if(subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY ||
DRC1f3635c2013-08-18 10:19:00 +0000602 (subsamp==TJSAMP_411 && sf[i].num==1 &&
603 (sf[i].denom==2 || sf[i].denom==1)) ||
604 (subsamp!=TJSAMP_411 && sf[i].num==1 &&
605 (sf[i].denom==4 || sf[i].denom==2 || sf[i].denom==1)))
DRCb8b359a2011-05-25 03:54:56 +0000606 _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
607 flags, sf[i]);
608 }
DRCb8b359a2011-05-25 03:54:56 +0000609
610 bailout:
DRCb7c41932013-05-04 23:41:33 +0000611 return;
DRCb8b359a2011-05-25 03:54:56 +0000612}
613
614
615void doTest(int w, int h, const int *formats, int nformats, int subsamp,
616 char *basename)
617{
618 tjhandle chandle=NULL, dhandle=NULL;
619 unsigned char *dstBuf=NULL;
620 unsigned long size=0; int pfi, pf, i;
621
DRC910a3572013-10-30 23:02:57 +0000622 if(yuv==YUVENCODE)
623 size=tjBufSizeYUV2(w, pad, h, subsamp);
624 else if(!alloc)
625 size=tjBufSize(w, h, subsamp);
626 if(size!=0)
DRCb8b359a2011-05-25 03:54:56 +0000627 if((dstBuf=(unsigned char *)tjAlloc(size))==NULL)
628 _throw("Memory allocation failure.");
DRCb8b359a2011-05-25 03:54:56 +0000629
630 if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL)
631 _throwtj();
632
633 for(pfi=0; pfi<nformats; pfi++)
634 {
635 for(i=0; i<2; i++)
636 {
637 int flags=0;
DRC1f3635c2013-08-18 10:19:00 +0000638 if(subsamp==TJSAMP_422 || subsamp==TJSAMP_420 || subsamp==TJSAMP_440 ||
639 subsamp==TJSAMP_411)
DRCcac10512012-03-16 14:37:36 +0000640 flags|=TJFLAG_FASTUPSAMPLE;
DRCb8b359a2011-05-25 03:54:56 +0000641 if(i==1)
642 {
643 if(yuv==YUVDECODE) goto bailout;
644 else flags|=TJFLAG_BOTTOMUP;
645 }
646 pf=formats[pfi];
647 compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100,
648 flags);
649 decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp,
650 flags);
DRC67ce3b22011-12-19 02:21:03 +0000651 if(pf>=TJPF_RGBX && pf<=TJPF_XRGB)
652 decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX),
653 basename, subsamp, flags);
DRCb7c41932013-05-04 23:41:33 +0000654 printf("\n");
DRCb8b359a2011-05-25 03:54:56 +0000655 }
656 }
DRCb7c41932013-05-04 23:41:33 +0000657 printf("--------------------\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000658
659 bailout:
660 if(chandle) tjDestroy(chandle);
661 if(dhandle) tjDestroy(dhandle);
662
663 if(dstBuf) tjFree(dstBuf);
664}
665
666
DRC724c56b2011-07-12 06:22:06 +0000667void bufSizeTest(void)
DRCb8b359a2011-05-25 03:54:56 +0000668{
DRC724c56b2011-07-12 06:22:06 +0000669 int w, h, i, subsamp;
DRC38c99702014-02-11 09:45:18 +0000670 unsigned char *srcBuf=NULL, *dstBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000671 tjhandle handle=NULL;
DRC38c99702014-02-11 09:45:18 +0000672 unsigned long dstSize=0;
DRCb8b359a2011-05-25 03:54:56 +0000673
674 if((handle=tjInitCompress())==NULL) _throwtj();
675
676 printf("Buffer size regression test\n");
DRC724c56b2011-07-12 06:22:06 +0000677 for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++)
DRCb8b359a2011-05-25 03:54:56 +0000678 {
DRC724c56b2011-07-12 06:22:06 +0000679 for(w=1; w<48; w++)
DRCb8b359a2011-05-25 03:54:56 +0000680 {
DRC724c56b2011-07-12 06:22:06 +0000681 int maxh=(w==1)? 2048:48;
682 for(h=1; h<maxh; h++)
DRCb8b359a2011-05-25 03:54:56 +0000683 {
DRC724c56b2011-07-12 06:22:06 +0000684 if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
685 if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000686 _throw("Memory allocation failure");
DRC38c99702014-02-11 09:45:18 +0000687 if(!alloc || yuv==YUVENCODE)
DRC724c56b2011-07-12 06:22:06 +0000688 {
DRC38c99702014-02-11 09:45:18 +0000689 if(yuv==YUVENCODE) dstSize=tjBufSizeYUV2(w, pad, h, subsamp);
690 else dstSize=tjBufSize(w, h, subsamp);
691 if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
DRC724c56b2011-07-12 06:22:06 +0000692 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000693 }
DRCb8b359a2011-05-25 03:54:56 +0000694
DRC724c56b2011-07-12 06:22:06 +0000695 for(i=0; i<w*h*4; i++)
696 {
697 if(random()<RAND_MAX/2) srcBuf[i]=0;
698 else srcBuf[i]=255;
699 }
DRCb8b359a2011-05-25 03:54:56 +0000700
DRC38c99702014-02-11 09:45:18 +0000701 if(yuv==YUVENCODE)
702 {
703 _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
704 subsamp, 0));
705 }
706 else
707 {
708 _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
709 &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
710 }
DRC724c56b2011-07-12 06:22:06 +0000711 free(srcBuf); srcBuf=NULL;
DRC38c99702014-02-11 09:45:18 +0000712 tjFree(dstBuf); dstBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000713
DRC724c56b2011-07-12 06:22:06 +0000714 if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000715 _throw("Memory allocation failure");
DRC38c99702014-02-11 09:45:18 +0000716 if(!alloc || yuv==YUVENCODE)
DRC724c56b2011-07-12 06:22:06 +0000717 {
DRC38c99702014-02-11 09:45:18 +0000718 if(yuv==YUVENCODE) dstSize=tjBufSizeYUV2(h, pad, w, subsamp);
719 else dstSize=tjBufSize(h, w, subsamp);
720 if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
DRC724c56b2011-07-12 06:22:06 +0000721 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000722 }
DRCb8b359a2011-05-25 03:54:56 +0000723
DRC724c56b2011-07-12 06:22:06 +0000724 for(i=0; i<h*w*4; i++)
725 {
726 if(random()<RAND_MAX/2) srcBuf[i]=0;
727 else srcBuf[i]=255;
728 }
729
DRC38c99702014-02-11 09:45:18 +0000730 if(yuv==YUVENCODE)
731 {
732 _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
733 subsamp, 0));
734 }
735 else
736 {
737 _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
738 &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
739 }
DRC724c56b2011-07-12 06:22:06 +0000740 free(srcBuf); srcBuf=NULL;
DRC38c99702014-02-11 09:45:18 +0000741 tjFree(dstBuf); dstBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000742 }
DRCb8b359a2011-05-25 03:54:56 +0000743 }
744 }
745 printf("Done. \n");
746
747 bailout:
748 if(srcBuf) free(srcBuf);
DRC38c99702014-02-11 09:45:18 +0000749 if(dstBuf) free(dstBuf);
DRCb8b359a2011-05-25 03:54:56 +0000750 if(handle) tjDestroy(handle);
751}
752
753
754int main(int argc, char *argv[])
755{
DRCcd7c3e62013-08-23 02:49:25 +0000756 int doyuv=0, i, num4bf=5;
DRCe835ee32011-07-15 10:06:56 +0000757 #ifdef _WIN32
758 srand((unsigned int)time(NULL));
759 #endif
DRCb8b359a2011-05-25 03:54:56 +0000760 if(argc>1)
761 {
762 for(i=1; i<argc; i++)
763 {
764 if(!strcasecmp(argv[i], "-yuv")) doyuv=1;
DRCf610d612013-04-26 10:33:29 +0000765 if(!strcasecmp(argv[i], "-noyuvpad")) pad=1;
DRCb8b359a2011-05-25 03:54:56 +0000766 if(!strcasecmp(argv[i], "-alloc")) alloc=1;
767 if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?"))
768 usage(argv[0]);
769 }
770 }
771 if(alloc) printf("Testing automatic buffer allocation\n");
DRC910a3572013-10-30 23:02:57 +0000772 if(doyuv) {yuv=YUVENCODE; num4bf=4;}
DRCb8b359a2011-05-25 03:54:56 +0000773 doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000774 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test");
DRCcac10512012-03-16 14:37:36 +0000775 doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000776 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test");
DRCcac10512012-03-16 14:37:36 +0000777 doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000778 doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test");
DRCcac10512012-03-16 14:37:36 +0000779 doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000780 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test");
DRC1f3635c2013-08-18 10:19:00 +0000781 doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000782 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test");
DRC1f3635c2013-08-18 10:19:00 +0000783 doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test");
784 doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test");
785 doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test");
DRC38c99702014-02-11 09:45:18 +0000786 bufSizeTest();
DRCb8b359a2011-05-25 03:54:56 +0000787 if(doyuv)
788 {
DRC38c99702014-02-11 09:45:18 +0000789 printf("\n--------------------\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000790 yuv=YUVDECODE;
791 doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
792 doTest(35, 39, _onlyRGB, 1, TJSAMP_444, "test_yuv1");
793 doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
794 doTest(39, 41, _onlyRGB, 1, TJSAMP_422, "test_yuv1");
795 doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
796 doTest(41, 35, _onlyRGB, 1, TJSAMP_420, "test_yuv1");
797 doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
798 doTest(35, 39, _onlyRGB, 1, TJSAMP_440, "test_yuv1");
DRC1f3635c2013-08-18 10:19:00 +0000799 doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0");
800 doTest(39, 41, _onlyRGB, 1, TJSAMP_411, "test_yuv1");
DRCb8b359a2011-05-25 03:54:56 +0000801 doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
DRC1f3635c2013-08-18 10:19:00 +0000802 doTest(41, 35, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv1");
DRCb8b359a2011-05-25 03:54:56 +0000803 doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
DRC1f3635c2013-08-18 10:19:00 +0000804 doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test_yuv1");
DRCb8b359a2011-05-25 03:54:56 +0000805 }
806
807 return exitStatus;
808}