blob: 6b14db1801ff0e42ae7b32be24b92666b81386c8 [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
DRC910a3572013-10-30 23:02:57 +0000459 if(yuv==YUVDECODE)
460 {
DRCfe739652013-10-31 04:53:27 +0000461 printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong,
462 jpegQual);
DRC910a3572013-10-30 23:02:57 +0000463 if((srcBuf=(unsigned char *)malloc(tjBufSizeYUV2(w, pad, h, subsamp)))
464 ==NULL)
465 _throw("Memory allocation failure");
466 initBufYUV(srcBuf, w, pad, h, subsamp);
467 }
DRCb8b359a2011-05-25 03:54:56 +0000468 else
DRC910a3572013-10-30 23:02:57 +0000469 {
470 if(yuv==YUVENCODE)
DRCfe739652013-10-31 04:53:27 +0000471 printf("%s %s -> %s YUV ... ", pfStr, buStrLong, subNameLong[subsamp]);
DRC910a3572013-10-30 23:02:57 +0000472 else
DRCfe739652013-10-31 04:53:27 +0000473 printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp],
DRC910a3572013-10-30 23:02:57 +0000474 jpegQual);
475 if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL)
476 _throw("Memory allocation failure");
477 initBuf(srcBuf, w, h, pf, flags);
478 }
DRCb8b359a2011-05-25 03:54:56 +0000479
DRCb8b359a2011-05-25 03:54:56 +0000480 if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize);
481
DRCb8b359a2011-05-25 03:54:56 +0000482 if(yuv==YUVENCODE)
483 {
DRCf610d612013-04-26 10:33:29 +0000484 _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, pf, *dstBuf, pad, subsamp,
485 flags));
DRCb8b359a2011-05-25 03:54:56 +0000486 }
487 else
488 {
DRC910a3572013-10-30 23:02:57 +0000489 if(!alloc) flags|=TJFLAG_NOREALLOC;
490 if(yuv==YUVDECODE)
DRCb8b359a2011-05-25 03:54:56 +0000491 {
DRC910a3572013-10-30 23:02:57 +0000492 _tj(tjCompressFromYUV(handle, srcBuf, w, pad, h, subsamp, dstBuf,
493 dstSize, jpegQual, flags));
DRCb8b359a2011-05-25 03:54:56 +0000494 }
DRC910a3572013-10-30 23:02:57 +0000495 else
496 {
497 _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
498 jpegQual, flags));
499 }
DRCb8b359a2011-05-25 03:54:56 +0000500 }
DRCb8b359a2011-05-25 03:54:56 +0000501
502 if(yuv==YUVENCODE)
DRCfe739652013-10-31 04:53:27 +0000503 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s.yuv", basename, pfStr, buStr,
504 subName[subsamp]);
DRCb8b359a2011-05-25 03:54:56 +0000505 else
DRCfe739652013-10-31 04:53:27 +0000506 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr,
507 subName[subsamp], jpegQual);
DRCb8b359a2011-05-25 03:54:56 +0000508 writeJPEG(*dstBuf, *dstSize, tempStr);
509 if(yuv==YUVENCODE)
510 {
DRCf610d612013-04-26 10:33:29 +0000511 tjscalingfactor sf={1, 1};
512 if(checkBufYUV(*dstBuf, w, h, subsamp, sf)) printf("Passed.");
DRCb8b359a2011-05-25 03:54:56 +0000513 else printf("FAILED!");
514 }
515 else printf("Done.");
DRC2bdadb42014-02-28 09:06:42 +0000516 printf("\n Result in %s\n", tempStr);
DRCb8b359a2011-05-25 03:54:56 +0000517
518 bailout:
519 if(srcBuf) free(srcBuf);
520}
521
522
523void _decompTest(tjhandle handle, unsigned char *jpegBuf,
524 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
525 int flags, tjscalingfactor sf)
526{
527 unsigned char *dstBuf=NULL;
DRC2bdadb42014-02-28 09:06:42 +0000528 int _hdrw=0, _hdrh=0, _hdrsubsamp=-1;
DRCb8b359a2011-05-25 03:54:56 +0000529 int scaledWidth=TJSCALED(w, sf);
530 int scaledHeight=TJSCALED(h, sf);
531 unsigned long dstSize=0;
532
533 if(yuv==YUVENCODE) return;
534
535 if(yuv==YUVDECODE)
DRC38cb1ec2013-08-23 04:45:43 +0000536 printf("JPEG -> YUV %s ", subNameLong[subsamp]);
DRCb8b359a2011-05-25 03:54:56 +0000537 else
DRCb8b359a2011-05-25 03:54:56 +0000538 printf("JPEG -> %s %s ", pixFormatStr[pf],
539 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
DRCf610d612013-04-26 10:33:29 +0000540 if(sf.num!=1 || sf.denom!=1)
541 printf("%d/%d ... ", sf.num, sf.denom);
542 else printf("... ");
DRCb8b359a2011-05-25 03:54:56 +0000543
544 _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
545 &_hdrsubsamp));
546 if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp)
547 _throw("Incorrect JPEG header");
548
DRCf610d612013-04-26 10:33:29 +0000549 if(yuv==YUVDECODE)
550 dstSize=tjBufSizeYUV2(scaledWidth, pad, scaledHeight, subsamp);
DRCb8b359a2011-05-25 03:54:56 +0000551 else dstSize=scaledWidth*scaledHeight*tjPixelSize[pf];
552 if((dstBuf=(unsigned char *)malloc(dstSize))==NULL)
553 _throw("Memory allocation failure");
554 memset(dstBuf, 0, dstSize);
555
DRCb8b359a2011-05-25 03:54:56 +0000556 if(yuv==YUVDECODE)
557 {
DRCf610d612013-04-26 10:33:29 +0000558 _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth,
559 pad, scaledHeight, flags));
DRCb8b359a2011-05-25 03:54:56 +0000560 }
561 else
562 {
563 _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
564 scaledHeight, pf, flags));
565 }
DRCb8b359a2011-05-25 03:54:56 +0000566
567 if(yuv==YUVDECODE)
568 {
DRCf610d612013-04-26 10:33:29 +0000569 if(checkBufYUV(dstBuf, scaledWidth, scaledHeight, subsamp, sf))
570 printf("Passed.");
DRCb8b359a2011-05-25 03:54:56 +0000571 else printf("FAILED!");
572 }
573 else
574 {
575 if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
576 printf("Passed.");
577 else printf("FAILED!");
578 }
DRC2bdadb42014-02-28 09:06:42 +0000579 printf("\n");
DRCb8b359a2011-05-25 03:54:56 +0000580
581 bailout:
582 if(dstBuf) free(dstBuf);
583}
584
585
586void decompTest(tjhandle handle, unsigned char *jpegBuf,
587 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
588 int flags)
589{
590 int i, n=0;
DRC418fe282013-05-07 21:17:35 +0000591 tjscalingfactor *sf=tjGetScalingFactors(&n);
DRCb8b359a2011-05-25 03:54:56 +0000592 if(!sf || !n) _throwtj();
593
DRC418fe282013-05-07 21:17:35 +0000594 for(i=0; i<n; i++)
DRCb8b359a2011-05-25 03:54:56 +0000595 {
DRC418fe282013-05-07 21:17:35 +0000596 if(subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY ||
DRC1f3635c2013-08-18 10:19:00 +0000597 (subsamp==TJSAMP_411 && sf[i].num==1 &&
598 (sf[i].denom==2 || sf[i].denom==1)) ||
599 (subsamp!=TJSAMP_411 && sf[i].num==1 &&
600 (sf[i].denom==4 || sf[i].denom==2 || sf[i].denom==1)))
DRCb8b359a2011-05-25 03:54:56 +0000601 _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
602 flags, sf[i]);
603 }
DRCb8b359a2011-05-25 03:54:56 +0000604
605 bailout:
DRCb7c41932013-05-04 23:41:33 +0000606 return;
DRCb8b359a2011-05-25 03:54:56 +0000607}
608
609
610void doTest(int w, int h, const int *formats, int nformats, int subsamp,
611 char *basename)
612{
613 tjhandle chandle=NULL, dhandle=NULL;
614 unsigned char *dstBuf=NULL;
615 unsigned long size=0; int pfi, pf, i;
616
DRC910a3572013-10-30 23:02:57 +0000617 if(yuv==YUVENCODE)
618 size=tjBufSizeYUV2(w, pad, h, subsamp);
619 else if(!alloc)
620 size=tjBufSize(w, h, subsamp);
621 if(size!=0)
DRCb8b359a2011-05-25 03:54:56 +0000622 if((dstBuf=(unsigned char *)tjAlloc(size))==NULL)
623 _throw("Memory allocation failure.");
DRCb8b359a2011-05-25 03:54:56 +0000624
625 if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL)
626 _throwtj();
627
628 for(pfi=0; pfi<nformats; pfi++)
629 {
630 for(i=0; i<2; i++)
631 {
632 int flags=0;
DRC1f3635c2013-08-18 10:19:00 +0000633 if(subsamp==TJSAMP_422 || subsamp==TJSAMP_420 || subsamp==TJSAMP_440 ||
634 subsamp==TJSAMP_411)
DRCcac10512012-03-16 14:37:36 +0000635 flags|=TJFLAG_FASTUPSAMPLE;
DRCb8b359a2011-05-25 03:54:56 +0000636 if(i==1)
637 {
638 if(yuv==YUVDECODE) goto bailout;
639 else flags|=TJFLAG_BOTTOMUP;
640 }
641 pf=formats[pfi];
642 compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100,
643 flags);
644 decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp,
645 flags);
DRC67ce3b22011-12-19 02:21:03 +0000646 if(pf>=TJPF_RGBX && pf<=TJPF_XRGB)
647 decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX),
648 basename, subsamp, flags);
DRCb7c41932013-05-04 23:41:33 +0000649 printf("\n");
DRCb8b359a2011-05-25 03:54:56 +0000650 }
651 }
DRCb7c41932013-05-04 23:41:33 +0000652 printf("--------------------\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000653
654 bailout:
655 if(chandle) tjDestroy(chandle);
656 if(dhandle) tjDestroy(dhandle);
657
658 if(dstBuf) tjFree(dstBuf);
659}
660
661
DRC724c56b2011-07-12 06:22:06 +0000662void bufSizeTest(void)
DRCb8b359a2011-05-25 03:54:56 +0000663{
DRC724c56b2011-07-12 06:22:06 +0000664 int w, h, i, subsamp;
DRC38c99702014-02-11 09:45:18 +0000665 unsigned char *srcBuf=NULL, *dstBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000666 tjhandle handle=NULL;
DRC38c99702014-02-11 09:45:18 +0000667 unsigned long dstSize=0;
DRCb8b359a2011-05-25 03:54:56 +0000668
669 if((handle=tjInitCompress())==NULL) _throwtj();
670
671 printf("Buffer size regression test\n");
DRC724c56b2011-07-12 06:22:06 +0000672 for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++)
DRCb8b359a2011-05-25 03:54:56 +0000673 {
DRC724c56b2011-07-12 06:22:06 +0000674 for(w=1; w<48; w++)
DRCb8b359a2011-05-25 03:54:56 +0000675 {
DRC724c56b2011-07-12 06:22:06 +0000676 int maxh=(w==1)? 2048:48;
677 for(h=1; h<maxh; h++)
DRCb8b359a2011-05-25 03:54:56 +0000678 {
DRC724c56b2011-07-12 06:22:06 +0000679 if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
680 if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000681 _throw("Memory allocation failure");
DRC38c99702014-02-11 09:45:18 +0000682 if(!alloc || yuv==YUVENCODE)
DRC724c56b2011-07-12 06:22:06 +0000683 {
DRC38c99702014-02-11 09:45:18 +0000684 if(yuv==YUVENCODE) dstSize=tjBufSizeYUV2(w, pad, h, subsamp);
685 else dstSize=tjBufSize(w, h, subsamp);
686 if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
DRC724c56b2011-07-12 06:22:06 +0000687 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000688 }
DRCb8b359a2011-05-25 03:54:56 +0000689
DRC724c56b2011-07-12 06:22:06 +0000690 for(i=0; i<w*h*4; i++)
691 {
692 if(random()<RAND_MAX/2) srcBuf[i]=0;
693 else srcBuf[i]=255;
694 }
DRCb8b359a2011-05-25 03:54:56 +0000695
DRC38c99702014-02-11 09:45:18 +0000696 if(yuv==YUVENCODE)
697 {
698 _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
699 subsamp, 0));
700 }
701 else
702 {
703 _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
704 &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
705 }
DRC724c56b2011-07-12 06:22:06 +0000706 free(srcBuf); srcBuf=NULL;
DRC38c99702014-02-11 09:45:18 +0000707 tjFree(dstBuf); dstBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000708
DRC724c56b2011-07-12 06:22:06 +0000709 if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000710 _throw("Memory allocation failure");
DRC38c99702014-02-11 09:45:18 +0000711 if(!alloc || yuv==YUVENCODE)
DRC724c56b2011-07-12 06:22:06 +0000712 {
DRC38c99702014-02-11 09:45:18 +0000713 if(yuv==YUVENCODE) dstSize=tjBufSizeYUV2(h, pad, w, subsamp);
714 else dstSize=tjBufSize(h, w, subsamp);
715 if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
DRC724c56b2011-07-12 06:22:06 +0000716 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000717 }
DRCb8b359a2011-05-25 03:54:56 +0000718
DRC724c56b2011-07-12 06:22:06 +0000719 for(i=0; i<h*w*4; i++)
720 {
721 if(random()<RAND_MAX/2) srcBuf[i]=0;
722 else srcBuf[i]=255;
723 }
724
DRC38c99702014-02-11 09:45:18 +0000725 if(yuv==YUVENCODE)
726 {
727 _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
728 subsamp, 0));
729 }
730 else
731 {
732 _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
733 &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
734 }
DRC724c56b2011-07-12 06:22:06 +0000735 free(srcBuf); srcBuf=NULL;
DRC38c99702014-02-11 09:45:18 +0000736 tjFree(dstBuf); dstBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000737 }
DRCb8b359a2011-05-25 03:54:56 +0000738 }
739 }
740 printf("Done. \n");
741
742 bailout:
743 if(srcBuf) free(srcBuf);
DRC38c99702014-02-11 09:45:18 +0000744 if(dstBuf) free(dstBuf);
DRCb8b359a2011-05-25 03:54:56 +0000745 if(handle) tjDestroy(handle);
746}
747
748
749int main(int argc, char *argv[])
750{
DRCcd7c3e62013-08-23 02:49:25 +0000751 int doyuv=0, i, num4bf=5;
DRCe835ee32011-07-15 10:06:56 +0000752 #ifdef _WIN32
753 srand((unsigned int)time(NULL));
754 #endif
DRCb8b359a2011-05-25 03:54:56 +0000755 if(argc>1)
756 {
757 for(i=1; i<argc; i++)
758 {
759 if(!strcasecmp(argv[i], "-yuv")) doyuv=1;
DRCf610d612013-04-26 10:33:29 +0000760 if(!strcasecmp(argv[i], "-noyuvpad")) pad=1;
DRCb8b359a2011-05-25 03:54:56 +0000761 if(!strcasecmp(argv[i], "-alloc")) alloc=1;
762 if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?"))
763 usage(argv[0]);
764 }
765 }
766 if(alloc) printf("Testing automatic buffer allocation\n");
DRC910a3572013-10-30 23:02:57 +0000767 if(doyuv) {yuv=YUVENCODE; num4bf=4;}
DRCb8b359a2011-05-25 03:54:56 +0000768 doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000769 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test");
DRCcac10512012-03-16 14:37:36 +0000770 doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000771 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test");
DRCcac10512012-03-16 14:37:36 +0000772 doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000773 doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test");
DRCcac10512012-03-16 14:37:36 +0000774 doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000775 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test");
DRC1f3635c2013-08-18 10:19:00 +0000776 doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000777 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test");
DRC1f3635c2013-08-18 10:19:00 +0000778 doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test");
779 doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test");
780 doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test");
DRC38c99702014-02-11 09:45:18 +0000781 bufSizeTest();
DRCb8b359a2011-05-25 03:54:56 +0000782 if(doyuv)
783 {
DRC38c99702014-02-11 09:45:18 +0000784 printf("\n--------------------\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000785 yuv=YUVDECODE;
786 doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
787 doTest(35, 39, _onlyRGB, 1, TJSAMP_444, "test_yuv1");
788 doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
789 doTest(39, 41, _onlyRGB, 1, TJSAMP_422, "test_yuv1");
790 doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
791 doTest(41, 35, _onlyRGB, 1, TJSAMP_420, "test_yuv1");
792 doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
793 doTest(35, 39, _onlyRGB, 1, TJSAMP_440, "test_yuv1");
DRC1f3635c2013-08-18 10:19:00 +0000794 doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0");
795 doTest(39, 41, _onlyRGB, 1, TJSAMP_411, "test_yuv1");
DRCb8b359a2011-05-25 03:54:56 +0000796 doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
DRC1f3635c2013-08-18 10:19:00 +0000797 doTest(41, 35, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv1");
DRCb8b359a2011-05-25 03:54:56 +0000798 doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
DRC1f3635c2013-08-18 10:19:00 +0000799 doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test_yuv1");
DRCb8b359a2011-05-25 03:54:56 +0000800 }
801
802 return exitStatus;
803}