DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 1 | /* |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 2 | * Copyright (C)2009-2012 D. R. Commander. All Rights Reserved. |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 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 | /* |
DRC | c52c556 | 2011-06-15 02:43:42 +0000 | [diff] [blame] | 30 | * This program tests the various code paths in the TurboJPEG C Wrapper |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 31 | */ |
| 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" |
DRC | e835ee3 | 2011-07-15 10:06:56 +0000 | [diff] [blame] | 39 | #ifdef _WIN32 |
| 40 | #include <time.h> |
| 41 | #define random() rand() |
| 42 | #endif |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | void 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 | |
| 60 | const char *subNameLong[TJ_NUMSAMP]= |
| 61 | { |
| 62 | "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0" |
| 63 | }; |
| 64 | const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440"}; |
| 65 | |
| 66 | const char *pixFormatStr[TJ_NUMPF]= |
| 67 | { |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 68 | "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale", |
| 69 | "RGBA", "BGRA", "ABGR", "ARGB" |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 72 | const int alphaOffset[TJ_NUMPF] = {-1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0}; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 73 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 74 | const int _3byteFormats[]={TJPF_RGB, TJPF_BGR}; |
| 75 | const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB}; |
| 76 | const int _onlyGray[]={TJPF_GRAY}; |
| 77 | const int _onlyRGB[]={TJPF_RGB}; |
| 78 | |
| 79 | enum {YUVENCODE=1, YUVDECODE}; |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 80 | int yuv=0, alloc=0; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 81 | |
| 82 | int exitStatus=0; |
| 83 | #define bailout() {exitStatus=-1; goto bailout;} |
| 84 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 85 | |
| 86 | void 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 | |
| 156 | int 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]; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 162 | int aoffset=alphaOffset[pf]; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 163 | 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 | { |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 172 | unsigned char r, g, b, a; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 173 | 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]; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 178 | a=aoffset>=0? buf[index*ps+aoffset]:0xFF; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 179 | 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 | } |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 215 | checkval255(a); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 216 | } |
| 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 | |
| 239 | int 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; |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 247 | int halfway=16; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 248 | |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 249 | for(row=0; row<ph; row++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 250 | { |
| 251 | for(col=0; col<pw; col++) |
| 252 | { |
| 253 | unsigned char y=buf[ypitch*row+col]; |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 254 | 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 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | if(subsamp!=TJSAMP_GRAY) |
| 265 | { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 266 | halfway=16/vsf; |
| 267 | for(row=0; row<ch; row++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 268 | { |
| 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 | { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 279 | if(row<halfway) |
| 280 | { |
| 281 | checkval(u, 85); checkval255(v); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | checkval0(u); checkval(v, 149); |
| 286 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 287 | } |
| 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 | |
| 322 | void 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 | |
| 336 | void 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; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 366 | *dstSize=(yuv==YUVENCODE? tjBufSizeYUV(w, h, subsamp) |
| 367 | : tjBufSize(w, h, subsamp)); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 368 | } |
| 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 | |
| 395 | void _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 | |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 423 | if(yuv==YUVDECODE) dstSize=tjBufSizeYUV(w, h, subsamp); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 424 | 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 | |
| 459 | void 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 | |
| 482 | void 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 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 491 | size=(yuv==YUVENCODE? tjBufSizeYUV(w, h, subsamp) |
| 492 | : tjBufSize(w, h, subsamp)); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 493 | 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; |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 505 | if(subsamp==TJSAMP_422 || subsamp==TJSAMP_420 || subsamp==TJSAMP_440) |
| 506 | flags|=TJFLAG_FASTUPSAMPLE; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 507 | if(i==1) |
| 508 | { |
| 509 | if(yuv==YUVDECODE) goto bailout; |
| 510 | else flags|=TJFLAG_BOTTOMUP; |
| 511 | } |
| 512 | pf=formats[pfi]; |
| 513 | compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100, |
| 514 | flags); |
| 515 | decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp, |
| 516 | flags); |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 517 | if(pf>=TJPF_RGBX && pf<=TJPF_XRGB) |
| 518 | decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX), |
| 519 | basename, subsamp, flags); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | bailout: |
| 524 | if(chandle) tjDestroy(chandle); |
| 525 | if(dhandle) tjDestroy(dhandle); |
| 526 | |
| 527 | if(dstBuf) tjFree(dstBuf); |
| 528 | } |
| 529 | |
| 530 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 531 | void bufSizeTest(void) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 532 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 533 | int w, h, i, subsamp; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 534 | unsigned char *srcBuf=NULL, *jpegBuf=NULL; |
| 535 | tjhandle handle=NULL; |
| 536 | unsigned long jpegSize=0; |
| 537 | |
| 538 | if((handle=tjInitCompress())==NULL) _throwtj(); |
| 539 | |
| 540 | printf("Buffer size regression test\n"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 541 | for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 542 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 543 | for(w=1; w<48; w++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 544 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 545 | int maxh=(w==1)? 2048:48; |
| 546 | for(h=1; h<maxh; h++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 547 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 548 | if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h); |
| 549 | if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 550 | _throw("Memory allocation failure"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 551 | if(!alloc) |
| 552 | { |
| 553 | if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(w, h, subsamp))) |
| 554 | ==NULL) |
| 555 | _throw("Memory allocation failure"); |
| 556 | jpegSize=tjBufSize(w, h, subsamp); |
| 557 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 558 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 559 | for(i=0; i<w*h*4; i++) |
| 560 | { |
| 561 | if(random()<RAND_MAX/2) srcBuf[i]=0; |
| 562 | else srcBuf[i]=255; |
| 563 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 564 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 565 | _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &jpegBuf, |
| 566 | &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC)); |
| 567 | free(srcBuf); srcBuf=NULL; |
| 568 | tjFree(jpegBuf); jpegBuf=NULL; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 569 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 570 | if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 571 | _throw("Memory allocation failure"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 572 | if(!alloc) |
| 573 | { |
| 574 | if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(h, w, subsamp))) |
| 575 | ==NULL) |
| 576 | _throw("Memory allocation failure"); |
| 577 | jpegSize=tjBufSize(h, w, subsamp); |
| 578 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 579 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 580 | for(i=0; i<h*w*4; i++) |
| 581 | { |
| 582 | if(random()<RAND_MAX/2) srcBuf[i]=0; |
| 583 | else srcBuf[i]=255; |
| 584 | } |
| 585 | |
| 586 | _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &jpegBuf, |
| 587 | &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC)); |
| 588 | free(srcBuf); srcBuf=NULL; |
| 589 | tjFree(jpegBuf); jpegBuf=NULL; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 590 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | printf("Done. \n"); |
| 594 | |
| 595 | bailout: |
| 596 | if(srcBuf) free(srcBuf); |
| 597 | if(jpegBuf) free(jpegBuf); |
| 598 | if(handle) tjDestroy(handle); |
| 599 | } |
| 600 | |
| 601 | |
| 602 | int main(int argc, char *argv[]) |
| 603 | { |
| 604 | int doyuv=0, i; |
DRC | e835ee3 | 2011-07-15 10:06:56 +0000 | [diff] [blame] | 605 | #ifdef _WIN32 |
| 606 | srand((unsigned int)time(NULL)); |
| 607 | #endif |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 608 | if(argc>1) |
| 609 | { |
| 610 | for(i=1; i<argc; i++) |
| 611 | { |
| 612 | if(!strcasecmp(argv[i], "-yuv")) doyuv=1; |
| 613 | if(!strcasecmp(argv[i], "-alloc")) alloc=1; |
| 614 | if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?")) |
| 615 | usage(argv[0]); |
| 616 | } |
| 617 | } |
| 618 | if(alloc) printf("Testing automatic buffer allocation\n"); |
| 619 | if(doyuv) {yuv=YUVENCODE; alloc=0;} |
| 620 | doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test"); |
| 621 | doTest(39, 41, _4byteFormats, 4, TJSAMP_444, "test"); |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 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"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 628 | doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test"); |
| 629 | doTest(39, 41, _3byteFormats, 2, TJSAMP_GRAY, "test"); |
| 630 | doTest(41, 35, _4byteFormats, 4, TJSAMP_GRAY, "test"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 631 | if(!doyuv) bufSizeTest(); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 632 | if(doyuv) |
| 633 | { |
| 634 | yuv=YUVDECODE; |
| 635 | doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0"); |
| 636 | doTest(35, 39, _onlyRGB, 1, TJSAMP_444, "test_yuv1"); |
| 637 | doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0"); |
| 638 | doTest(39, 41, _onlyRGB, 1, TJSAMP_422, "test_yuv1"); |
| 639 | doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0"); |
| 640 | doTest(41, 35, _onlyRGB, 1, TJSAMP_420, "test_yuv1"); |
| 641 | doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0"); |
| 642 | doTest(35, 39, _onlyRGB, 1, TJSAMP_440, "test_yuv1"); |
| 643 | doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0"); |
| 644 | doTest(35, 39, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv1"); |
| 645 | doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0"); |
| 646 | doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test_yuv1"); |
| 647 | } |
| 648 | |
| 649 | return exitStatus; |
| 650 | } |