blob: d2d3e983d90516d4d59f50ce56fa716862914413 [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};
DRC34dca052014-02-28 09:17:14 +000083int doyuv=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
DRCf610d612013-04-26 10:33:29 +0000298int checkBufYUV(unsigned char *buf, int w, int h, int subsamp,
299 tjscalingfactor sf)
DRCb8b359a2011-05-25 03:54:56 +0000300{
301 int row, col;
302 int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8;
303 int pw=PAD(w, hsf), ph=PAD(h, vsf);
304 int cw=pw/hsf, ch=ph/vsf;
DRCf610d612013-04-26 10:33:29 +0000305 int ypitch=PAD(pw, pad), uvpitch=PAD(cw, pad);
DRCb8b359a2011-05-25 03:54:56 +0000306 int retval=1;
DRCf610d612013-04-26 10:33:29 +0000307 int halfway=16*sf.num/sf.denom;
308 int blocksize=8*sf.num/sf.denom;
DRCb8b359a2011-05-25 03:54:56 +0000309
DRC215aa8b2011-05-27 02:10:42 +0000310 for(row=0; row<ph; row++)
DRCb8b359a2011-05-25 03:54:56 +0000311 {
312 for(col=0; col<pw; col++)
313 {
314 unsigned char y=buf[ypitch*row+col];
DRCf610d612013-04-26 10:33:29 +0000315 if(((row/blocksize)+(col/blocksize))%2==0)
DRC215aa8b2011-05-27 02:10:42 +0000316 {
317 if(row<halfway) checkval255(y) else checkval0(y);
318 }
319 else
320 {
321 if(row<halfway) checkval(y, 76) else checkval(y, 226);
322 }
DRCb8b359a2011-05-25 03:54:56 +0000323 }
324 }
325 if(subsamp!=TJSAMP_GRAY)
326 {
DRCf610d612013-04-26 10:33:29 +0000327 int halfway=16/vsf*sf.num/sf.denom;
DRC215aa8b2011-05-27 02:10:42 +0000328 for(row=0; row<ch; row++)
DRCb8b359a2011-05-25 03:54:56 +0000329 {
330 for(col=0; col<cw; col++)
331 {
332 unsigned char u=buf[ypitch*ph + (uvpitch*row+col)],
333 v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)];
DRCf610d612013-04-26 10:33:29 +0000334 if(((row*vsf/blocksize)+(col*hsf/blocksize))%2==0)
DRCb8b359a2011-05-25 03:54:56 +0000335 {
336 checkval(u, 128); checkval(v, 128);
337 }
338 else
339 {
DRC215aa8b2011-05-27 02:10:42 +0000340 if(row<halfway)
341 {
342 checkval(u, 85); checkval255(v);
343 }
344 else
345 {
346 checkval0(u); checkval(v, 149);
347 }
DRCb8b359a2011-05-25 03:54:56 +0000348 }
349 }
350 }
351 }
352
353 bailout:
354 if(retval==0)
355 {
356 for(row=0; row<ph; row++)
357 {
358 for(col=0; col<pw; col++)
359 printf("%.3d ", buf[ypitch*row+col]);
360 printf("\n");
361 }
362 printf("\n");
363 for(row=0; row<ch; row++)
364 {
365 for(col=0; col<cw; col++)
366 printf("%.3d ", buf[ypitch*ph + (uvpitch*row+col)]);
367 printf("\n");
368 }
369 printf("\n");
370 for(row=0; row<ch; row++)
371 {
372 for(col=0; col<cw; col++)
373 printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]);
374 printf("\n");
375 }
DRCb8b359a2011-05-25 03:54:56 +0000376 }
377
378 return retval;
379}
380
381
382void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
383{
384 FILE *file=fopen(filename, "wb");
385 if(!file || fwrite(jpegBuf, jpegSize, 1, file)!=1)
386 {
387 printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
388 bailout();
389 }
390
391 bailout:
392 if(file) fclose(file);
393}
394
395
396void compTest(tjhandle handle, unsigned char **dstBuf,
397 unsigned long *dstSize, int w, int h, int pf, char *basename,
398 int subsamp, int jpegQual, int flags)
399{
DRC34dca052014-02-28 09:17:14 +0000400 char tempStr[1024]; unsigned char *srcBuf=NULL, *yuvBuf=NULL;
401 const char *pfStr=pixFormatStr[pf];
DRCfe739652013-10-31 04:53:27 +0000402 const char *buStrLong=(flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ";
403 const char *buStr=(flags&TJFLAG_BOTTOMUP)? "BU":"TD";
DRCb8b359a2011-05-25 03:54:56 +0000404
DRC34dca052014-02-28 09:17:14 +0000405 if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL)
406 _throw("Memory allocation failure");
407 initBuf(srcBuf, w, h, pf, flags);
DRCb8b359a2011-05-25 03:54:56 +0000408
DRCb8b359a2011-05-25 03:54:56 +0000409 if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize);
410
DRCb8b359a2011-05-25 03:54:56 +0000411
DRC34dca052014-02-28 09:17:14 +0000412 if(!alloc) flags|=TJFLAG_NOREALLOC;
413 if(doyuv)
414 {
415 unsigned long yuvSize=tjBufSizeYUV2(w, pad, h, subsamp);
416 tjscalingfactor sf={1, 1};
DRCc9014492014-03-10 09:34:04 +0000417 tjhandle handle2=tjInitCompress();
418 if(!handle2) _throwtj();
DRC34dca052014-02-28 09:17:14 +0000419
420 if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL)
421 _throw("Memory allocation failure");
DRC20e158d2014-03-08 20:50:35 +0000422 memset(yuvBuf, 0, yuvSize);
DRC34dca052014-02-28 09:17:14 +0000423
424 printf("%s %s -> YUV %s ... ", pfStr, buStrLong, subNameLong[subsamp]);
DRCc9014492014-03-10 09:34:04 +0000425 _tj(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp,
DRC34dca052014-02-28 09:17:14 +0000426 flags));
DRCc9014492014-03-10 09:34:04 +0000427 tjDestroy(handle2);
DRCfe739652013-10-31 04:53:27 +0000428 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s.yuv", basename, pfStr, buStr,
429 subName[subsamp]);
DRC34dca052014-02-28 09:17:14 +0000430 writeJPEG(yuvBuf, yuvSize, tempStr);
431 if(checkBufYUV(yuvBuf, w, h, subsamp, sf)) printf("Passed.\n");
432 else printf("FAILED!\n");
433
434 printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong,
435 jpegQual);
436 _tj(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf,
437 dstSize, jpegQual, flags));
DRCb8b359a2011-05-25 03:54:56 +0000438 }
DRC34dca052014-02-28 09:17:14 +0000439 else
440 {
441 printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp],
442 jpegQual);
443 _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
444 jpegQual, flags));
445 }
446
447 snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr,
448 subName[subsamp], jpegQual);
449 writeJPEG(*dstBuf, *dstSize, tempStr);
450 printf("Done.\n Result in %s\n", tempStr);
DRCb8b359a2011-05-25 03:54:56 +0000451
452 bailout:
DRC34dca052014-02-28 09:17:14 +0000453 if(yuvBuf) free(yuvBuf);
DRCb8b359a2011-05-25 03:54:56 +0000454 if(srcBuf) free(srcBuf);
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, tjscalingfactor sf)
461{
DRC34dca052014-02-28 09:17:14 +0000462 unsigned char *dstBuf=NULL, *yuvBuf=NULL;
DRC2bdadb42014-02-28 09:06:42 +0000463 int _hdrw=0, _hdrh=0, _hdrsubsamp=-1;
DRCb8b359a2011-05-25 03:54:56 +0000464 int scaledWidth=TJSCALED(w, sf);
465 int scaledHeight=TJSCALED(h, sf);
466 unsigned long dstSize=0;
467
DRCb8b359a2011-05-25 03:54:56 +0000468 _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
469 &_hdrsubsamp));
470 if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp)
471 _throw("Incorrect JPEG header");
472
DRC34dca052014-02-28 09:17:14 +0000473 dstSize=scaledWidth*scaledHeight*tjPixelSize[pf];
DRCb8b359a2011-05-25 03:54:56 +0000474 if((dstBuf=(unsigned char *)malloc(dstSize))==NULL)
475 _throw("Memory allocation failure");
476 memset(dstBuf, 0, dstSize);
477
DRC34dca052014-02-28 09:17:14 +0000478 if(doyuv)
DRCb8b359a2011-05-25 03:54:56 +0000479 {
DRC34dca052014-02-28 09:17:14 +0000480 unsigned long yuvSize=tjBufSizeYUV2(scaledWidth, pad, scaledHeight,
481 subsamp);
DRCc9014492014-03-10 09:34:04 +0000482 tjhandle handle2=tjInitDecompress();
483 if(!handle2) _throwtj();
DRC34dca052014-02-28 09:17:14 +0000484
485 if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL)
486 _throw("Memory allocation failure");
DRC20e158d2014-03-08 20:50:35 +0000487 memset(yuvBuf, 0, yuvSize);
DRC34dca052014-02-28 09:17:14 +0000488
489 printf("JPEG -> YUV %s ", subNameLong[subsamp]);
490 if(sf.num!=1 || sf.denom!=1)
491 printf("%d/%d ... ", sf.num, sf.denom);
492 else printf("... ");
493 _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth,
DRCf610d612013-04-26 10:33:29 +0000494 pad, scaledHeight, flags));
DRC34dca052014-02-28 09:17:14 +0000495 if(checkBufYUV(yuvBuf, scaledWidth, scaledHeight, subsamp, sf))
496 printf("Passed.\n");
497 else printf("FAILED!\n");
498
499 printf("YUV %s -> %s %s ... ", subNameLong[subsamp], pixFormatStr[pf],
500 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
DRCc9014492014-03-10 09:34:04 +0000501 _tj(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0,
DRC34dca052014-02-28 09:17:14 +0000502 scaledHeight, pf, flags));
DRCc9014492014-03-10 09:34:04 +0000503 tjDestroy(handle2);
DRCb8b359a2011-05-25 03:54:56 +0000504 }
505 else
506 {
DRC34dca052014-02-28 09:17:14 +0000507 printf("JPEG -> %s %s ", pixFormatStr[pf],
508 (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ");
509 if(sf.num!=1 || sf.denom!=1)
510 printf("%d/%d ... ", sf.num, sf.denom);
511 else printf("... ");
DRCb8b359a2011-05-25 03:54:56 +0000512 _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
513 scaledHeight, pf, flags));
514 }
DRCb8b359a2011-05-25 03:54:56 +0000515
DRC34dca052014-02-28 09:17:14 +0000516 if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
517 printf("Passed.");
518 else printf("FAILED!");
DRC2bdadb42014-02-28 09:06:42 +0000519 printf("\n");
DRCb8b359a2011-05-25 03:54:56 +0000520
521 bailout:
DRC34dca052014-02-28 09:17:14 +0000522 if(yuvBuf) free(yuvBuf);
DRCb8b359a2011-05-25 03:54:56 +0000523 if(dstBuf) free(dstBuf);
524}
525
526
527void decompTest(tjhandle handle, unsigned char *jpegBuf,
528 unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp,
529 int flags)
530{
531 int i, n=0;
DRC418fe282013-05-07 21:17:35 +0000532 tjscalingfactor *sf=tjGetScalingFactors(&n);
DRCb8b359a2011-05-25 03:54:56 +0000533 if(!sf || !n) _throwtj();
534
DRC418fe282013-05-07 21:17:35 +0000535 for(i=0; i<n; i++)
DRCb8b359a2011-05-25 03:54:56 +0000536 {
DRC418fe282013-05-07 21:17:35 +0000537 if(subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY ||
DRC1f3635c2013-08-18 10:19:00 +0000538 (subsamp==TJSAMP_411 && sf[i].num==1 &&
539 (sf[i].denom==2 || sf[i].denom==1)) ||
540 (subsamp!=TJSAMP_411 && sf[i].num==1 &&
541 (sf[i].denom==4 || sf[i].denom==2 || sf[i].denom==1)))
DRCb8b359a2011-05-25 03:54:56 +0000542 _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp,
543 flags, sf[i]);
544 }
DRCb8b359a2011-05-25 03:54:56 +0000545
546 bailout:
DRCb7c41932013-05-04 23:41:33 +0000547 return;
DRCb8b359a2011-05-25 03:54:56 +0000548}
549
550
551void doTest(int w, int h, const int *formats, int nformats, int subsamp,
552 char *basename)
553{
554 tjhandle chandle=NULL, dhandle=NULL;
555 unsigned char *dstBuf=NULL;
556 unsigned long size=0; int pfi, pf, i;
557
DRC34dca052014-02-28 09:17:14 +0000558 if(!alloc)
DRC910a3572013-10-30 23:02:57 +0000559 size=tjBufSize(w, h, subsamp);
560 if(size!=0)
DRCb8b359a2011-05-25 03:54:56 +0000561 if((dstBuf=(unsigned char *)tjAlloc(size))==NULL)
562 _throw("Memory allocation failure.");
DRCb8b359a2011-05-25 03:54:56 +0000563
564 if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL)
565 _throwtj();
566
567 for(pfi=0; pfi<nformats; pfi++)
568 {
569 for(i=0; i<2; i++)
570 {
571 int flags=0;
DRC1f3635c2013-08-18 10:19:00 +0000572 if(subsamp==TJSAMP_422 || subsamp==TJSAMP_420 || subsamp==TJSAMP_440 ||
573 subsamp==TJSAMP_411)
DRCcac10512012-03-16 14:37:36 +0000574 flags|=TJFLAG_FASTUPSAMPLE;
DRC34dca052014-02-28 09:17:14 +0000575 if(i==1) flags|=TJFLAG_BOTTOMUP;
DRCb8b359a2011-05-25 03:54:56 +0000576 pf=formats[pfi];
577 compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100,
578 flags);
579 decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp,
580 flags);
DRC67ce3b22011-12-19 02:21:03 +0000581 if(pf>=TJPF_RGBX && pf<=TJPF_XRGB)
DRC4798b7e2014-03-08 20:31:31 +0000582 {
583 printf("\n");
DRC67ce3b22011-12-19 02:21:03 +0000584 decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX),
585 basename, subsamp, flags);
DRC4798b7e2014-03-08 20:31:31 +0000586 }
DRCb7c41932013-05-04 23:41:33 +0000587 printf("\n");
DRCb8b359a2011-05-25 03:54:56 +0000588 }
589 }
DRCb7c41932013-05-04 23:41:33 +0000590 printf("--------------------\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000591
592 bailout:
593 if(chandle) tjDestroy(chandle);
594 if(dhandle) tjDestroy(dhandle);
595
596 if(dstBuf) tjFree(dstBuf);
597}
598
599
DRC724c56b2011-07-12 06:22:06 +0000600void bufSizeTest(void)
DRCb8b359a2011-05-25 03:54:56 +0000601{
DRC724c56b2011-07-12 06:22:06 +0000602 int w, h, i, subsamp;
DRC38c99702014-02-11 09:45:18 +0000603 unsigned char *srcBuf=NULL, *dstBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000604 tjhandle handle=NULL;
DRC38c99702014-02-11 09:45:18 +0000605 unsigned long dstSize=0;
DRCb8b359a2011-05-25 03:54:56 +0000606
607 if((handle=tjInitCompress())==NULL) _throwtj();
608
609 printf("Buffer size regression test\n");
DRC724c56b2011-07-12 06:22:06 +0000610 for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++)
DRCb8b359a2011-05-25 03:54:56 +0000611 {
DRC724c56b2011-07-12 06:22:06 +0000612 for(w=1; w<48; w++)
DRCb8b359a2011-05-25 03:54:56 +0000613 {
DRC724c56b2011-07-12 06:22:06 +0000614 int maxh=(w==1)? 2048:48;
615 for(h=1; h<maxh; h++)
DRCb8b359a2011-05-25 03:54:56 +0000616 {
DRC724c56b2011-07-12 06:22:06 +0000617 if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
618 if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000619 _throw("Memory allocation failure");
DRC34dca052014-02-28 09:17:14 +0000620 if(!alloc || doyuv)
DRC724c56b2011-07-12 06:22:06 +0000621 {
DRC34dca052014-02-28 09:17:14 +0000622 if(doyuv) dstSize=tjBufSizeYUV2(w, pad, h, subsamp);
DRC38c99702014-02-11 09:45:18 +0000623 else dstSize=tjBufSize(w, h, subsamp);
624 if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
DRC724c56b2011-07-12 06:22:06 +0000625 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000626 }
DRCb8b359a2011-05-25 03:54:56 +0000627
DRC724c56b2011-07-12 06:22:06 +0000628 for(i=0; i<w*h*4; i++)
629 {
630 if(random()<RAND_MAX/2) srcBuf[i]=0;
631 else srcBuf[i]=255;
632 }
DRCb8b359a2011-05-25 03:54:56 +0000633
DRC34dca052014-02-28 09:17:14 +0000634 if(doyuv)
DRC38c99702014-02-11 09:45:18 +0000635 {
636 _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
637 subsamp, 0));
638 }
639 else
640 {
641 _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
642 &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
643 }
DRC724c56b2011-07-12 06:22:06 +0000644 free(srcBuf); srcBuf=NULL;
DRC38c99702014-02-11 09:45:18 +0000645 tjFree(dstBuf); dstBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000646
DRC724c56b2011-07-12 06:22:06 +0000647 if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000648 _throw("Memory allocation failure");
DRC34dca052014-02-28 09:17:14 +0000649 if(!alloc || doyuv)
DRC724c56b2011-07-12 06:22:06 +0000650 {
DRC34dca052014-02-28 09:17:14 +0000651 if(doyuv) dstSize=tjBufSizeYUV2(h, pad, w, subsamp);
DRC38c99702014-02-11 09:45:18 +0000652 else dstSize=tjBufSize(h, w, subsamp);
653 if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL)
DRC724c56b2011-07-12 06:22:06 +0000654 _throw("Memory allocation failure");
DRC724c56b2011-07-12 06:22:06 +0000655 }
DRCb8b359a2011-05-25 03:54:56 +0000656
DRC724c56b2011-07-12 06:22:06 +0000657 for(i=0; i<h*w*4; i++)
658 {
659 if(random()<RAND_MAX/2) srcBuf[i]=0;
660 else srcBuf[i]=255;
661 }
662
DRC34dca052014-02-28 09:17:14 +0000663 if(doyuv)
DRC38c99702014-02-11 09:45:18 +0000664 {
665 _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
666 subsamp, 0));
667 }
668 else
669 {
670 _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
671 &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
672 }
DRC724c56b2011-07-12 06:22:06 +0000673 free(srcBuf); srcBuf=NULL;
DRC38c99702014-02-11 09:45:18 +0000674 tjFree(dstBuf); dstBuf=NULL;
DRCb8b359a2011-05-25 03:54:56 +0000675 }
DRCb8b359a2011-05-25 03:54:56 +0000676 }
677 }
678 printf("Done. \n");
679
680 bailout:
681 if(srcBuf) free(srcBuf);
DRC38c99702014-02-11 09:45:18 +0000682 if(dstBuf) free(dstBuf);
DRCb8b359a2011-05-25 03:54:56 +0000683 if(handle) tjDestroy(handle);
684}
685
686
687int main(int argc, char *argv[])
688{
DRC34dca052014-02-28 09:17:14 +0000689 int i, num4bf=5;
DRCe835ee32011-07-15 10:06:56 +0000690 #ifdef _WIN32
691 srand((unsigned int)time(NULL));
692 #endif
DRCb8b359a2011-05-25 03:54:56 +0000693 if(argc>1)
694 {
695 for(i=1; i<argc; i++)
696 {
697 if(!strcasecmp(argv[i], "-yuv")) doyuv=1;
DRCf610d612013-04-26 10:33:29 +0000698 if(!strcasecmp(argv[i], "-noyuvpad")) pad=1;
DRCb8b359a2011-05-25 03:54:56 +0000699 if(!strcasecmp(argv[i], "-alloc")) alloc=1;
700 if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?"))
701 usage(argv[0]);
702 }
703 }
704 if(alloc) printf("Testing automatic buffer allocation\n");
DRC34dca052014-02-28 09:17:14 +0000705 if(doyuv) num4bf=4;
DRCb8b359a2011-05-25 03:54:56 +0000706 doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000707 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test");
DRCcac10512012-03-16 14:37:36 +0000708 doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000709 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test");
DRCcac10512012-03-16 14:37:36 +0000710 doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000711 doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test");
DRCcac10512012-03-16 14:37:36 +0000712 doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000713 doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test");
DRC1f3635c2013-08-18 10:19:00 +0000714 doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test");
DRCcd7c3e62013-08-23 02:49:25 +0000715 doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test");
DRC1f3635c2013-08-18 10:19:00 +0000716 doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test");
717 doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test");
718 doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test");
DRC38c99702014-02-11 09:45:18 +0000719 bufSizeTest();
DRCb8b359a2011-05-25 03:54:56 +0000720 if(doyuv)
721 {
DRC38c99702014-02-11 09:45:18 +0000722 printf("\n--------------------\n\n");
DRCb8b359a2011-05-25 03:54:56 +0000723 doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0");
DRCb8b359a2011-05-25 03:54:56 +0000724 doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0");
DRCb8b359a2011-05-25 03:54:56 +0000725 doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0");
DRCb8b359a2011-05-25 03:54:56 +0000726 doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0");
DRC1f3635c2013-08-18 10:19:00 +0000727 doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0");
DRCb8b359a2011-05-25 03:54:56 +0000728 doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0");
DRCb8b359a2011-05-25 03:54:56 +0000729 doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0");
DRCb8b359a2011-05-25 03:54:56 +0000730 }
731
732 return exitStatus;
733}