DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2009-2011 D. R. Commander. All Rights Reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * - Redistributions of source code must retain the above copyright notice, |
| 8 | * this list of conditions and the following disclaimer. |
| 9 | * - Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | * - Neither the name of the libjpeg-turbo Project nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived from this |
| 14 | * software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /* |
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 | { |
| 68 | "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale" |
| 69 | }; |
| 70 | |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame^] | 71 | const int alphaOffset[TJ_NUMPF] = {-1, -1, 3, 3, 0, 0, -1}; |
| 72 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 73 | const int _3byteFormats[]={TJPF_RGB, TJPF_BGR}; |
| 74 | const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB}; |
| 75 | const int _onlyGray[]={TJPF_GRAY}; |
| 76 | const int _onlyRGB[]={TJPF_RGB}; |
| 77 | |
| 78 | enum {YUVENCODE=1, YUVDECODE}; |
| 79 | int yuv=0, alloc=0; |
| 80 | |
| 81 | int exitStatus=0; |
| 82 | #define bailout() {exitStatus=-1; goto bailout;} |
| 83 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 84 | |
| 85 | void initBuf(unsigned char *buf, int w, int h, int pf, int flags) |
| 86 | { |
| 87 | int roffset=tjRedOffset[pf]; |
| 88 | int goffset=tjGreenOffset[pf]; |
| 89 | int boffset=tjBlueOffset[pf]; |
| 90 | int ps=tjPixelSize[pf]; |
| 91 | int index, row, col, halfway=16; |
| 92 | |
| 93 | memset(buf, 0, w*h*ps); |
| 94 | if(pf==TJPF_GRAY) |
| 95 | { |
| 96 | for(row=0; row<h; row++) |
| 97 | { |
| 98 | for(col=0; col<w; col++) |
| 99 | { |
| 100 | if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col; |
| 101 | else index=row*w+col; |
| 102 | if(((row/8)+(col/8))%2==0) buf[index]=(row<halfway)? 255:0; |
| 103 | else buf[index]=(row<halfway)? 76:226; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | for(row=0; row<h; row++) |
| 110 | { |
| 111 | for(col=0; col<w; col++) |
| 112 | { |
| 113 | if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col; |
| 114 | else index=row*w+col; |
| 115 | if(((row/8)+(col/8))%2==0) |
| 116 | { |
| 117 | if(row<halfway) |
| 118 | { |
| 119 | buf[index*ps+roffset]=255; |
| 120 | buf[index*ps+goffset]=255; |
| 121 | buf[index*ps+boffset]=255; |
| 122 | } |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | buf[index*ps+roffset]=255; |
| 127 | if(row>=halfway) buf[index*ps+goffset]=255; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | #define checkval(v, cv) { \ |
| 136 | if(v<cv-1 || v>cv+1) { \ |
| 137 | printf("\nComp. %s at %d,%d should be %d, not %d\n", \ |
| 138 | #v, row, col, cv, v); \ |
| 139 | retval=0; exitStatus=-1; goto bailout; \ |
| 140 | }} |
| 141 | |
| 142 | #define checkval0(v) { \ |
| 143 | if(v>1) { \ |
| 144 | printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \ |
| 145 | retval=0; exitStatus=-1; goto bailout; \ |
| 146 | }} |
| 147 | |
| 148 | #define checkval255(v) { \ |
| 149 | if(v<254) { \ |
| 150 | printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \ |
| 151 | retval=0; exitStatus=-1; goto bailout; \ |
| 152 | }} |
| 153 | |
| 154 | |
| 155 | int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp, |
| 156 | tjscalingfactor sf, int flags) |
| 157 | { |
| 158 | int roffset=tjRedOffset[pf]; |
| 159 | int goffset=tjGreenOffset[pf]; |
| 160 | int boffset=tjBlueOffset[pf]; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame^] | 161 | int aoffset=alphaOffset[pf]; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 162 | int ps=tjPixelSize[pf]; |
| 163 | int index, row, col, retval=1; |
| 164 | int halfway=16*sf.num/sf.denom; |
| 165 | int blocksize=8*sf.num/sf.denom; |
| 166 | |
| 167 | for(row=0; row<h; row++) |
| 168 | { |
| 169 | for(col=0; col<w; col++) |
| 170 | { |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame^] | 171 | unsigned char r, g, b, a; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 172 | if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col; |
| 173 | else index=row*w+col; |
| 174 | r=buf[index*ps+roffset]; |
| 175 | g=buf[index*ps+goffset]; |
| 176 | b=buf[index*ps+boffset]; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame^] | 177 | a=aoffset>=0? buf[index*ps+aoffset]:0xFF; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 178 | if(((row/blocksize)+(col/blocksize))%2==0) |
| 179 | { |
| 180 | if(row<halfway) |
| 181 | { |
| 182 | checkval255(r); checkval255(g); checkval255(b); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | checkval0(r); checkval0(g); checkval0(b); |
| 187 | } |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | if(subsamp==TJSAMP_GRAY) |
| 192 | { |
| 193 | if(row<halfway) |
| 194 | { |
| 195 | checkval(r, 76); checkval(g, 76); checkval(b, 76); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | checkval(r, 226); checkval(g, 226); checkval(b, 226); |
| 200 | } |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | if(row<halfway) |
| 205 | { |
| 206 | checkval255(r); checkval0(g); checkval0(b); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | checkval255(r); checkval255(g); checkval0(b); |
| 211 | } |
| 212 | } |
| 213 | } |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame^] | 214 | checkval255(a); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | bailout: |
| 219 | if(retval==0) |
| 220 | { |
| 221 | printf("\n"); |
| 222 | for(row=0; row<h; row++) |
| 223 | { |
| 224 | for(col=0; col<w; col++) |
| 225 | { |
| 226 | printf("%.3d/%.3d/%.3d ", buf[(row*w+col)*ps+roffset], |
| 227 | buf[(row*w+col)*ps+goffset], buf[(row*w+col)*ps+boffset]); |
| 228 | } |
| 229 | printf("\n"); |
| 230 | } |
| 231 | } |
| 232 | return retval; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) |
| 237 | |
| 238 | int checkBufYUV(unsigned char *buf, int w, int h, int subsamp) |
| 239 | { |
| 240 | int row, col; |
| 241 | int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8; |
| 242 | int pw=PAD(w, hsf), ph=PAD(h, vsf); |
| 243 | int cw=pw/hsf, ch=ph/vsf; |
| 244 | int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4); |
| 245 | int retval=1; |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 246 | int halfway=16; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 247 | |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 248 | for(row=0; row<ph; row++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 249 | { |
| 250 | for(col=0; col<pw; col++) |
| 251 | { |
| 252 | unsigned char y=buf[ypitch*row+col]; |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 253 | if(((row/8)+(col/8))%2==0) |
| 254 | { |
| 255 | if(row<halfway) checkval255(y) else checkval0(y); |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | if(row<halfway) checkval(y, 76) else checkval(y, 226); |
| 260 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | if(subsamp!=TJSAMP_GRAY) |
| 264 | { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 265 | halfway=16/vsf; |
| 266 | for(row=0; row<ch; row++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 267 | { |
| 268 | for(col=0; col<cw; col++) |
| 269 | { |
| 270 | unsigned char u=buf[ypitch*ph + (uvpitch*row+col)], |
| 271 | v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]; |
| 272 | if(((row*vsf/8)+(col*hsf/8))%2==0) |
| 273 | { |
| 274 | checkval(u, 128); checkval(v, 128); |
| 275 | } |
| 276 | else |
| 277 | { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 278 | if(row<halfway) |
| 279 | { |
| 280 | checkval(u, 85); checkval255(v); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | checkval0(u); checkval(v, 149); |
| 285 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | bailout: |
| 292 | if(retval==0) |
| 293 | { |
| 294 | for(row=0; row<ph; row++) |
| 295 | { |
| 296 | for(col=0; col<pw; col++) |
| 297 | printf("%.3d ", buf[ypitch*row+col]); |
| 298 | printf("\n"); |
| 299 | } |
| 300 | printf("\n"); |
| 301 | for(row=0; row<ch; row++) |
| 302 | { |
| 303 | for(col=0; col<cw; col++) |
| 304 | printf("%.3d ", buf[ypitch*ph + (uvpitch*row+col)]); |
| 305 | printf("\n"); |
| 306 | } |
| 307 | printf("\n"); |
| 308 | for(row=0; row<ch; row++) |
| 309 | { |
| 310 | for(col=0; col<cw; col++) |
| 311 | printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]); |
| 312 | printf("\n"); |
| 313 | } |
| 314 | printf("\n"); |
| 315 | } |
| 316 | |
| 317 | return retval; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename) |
| 322 | { |
| 323 | FILE *file=fopen(filename, "wb"); |
| 324 | if(!file || fwrite(jpegBuf, jpegSize, 1, file)!=1) |
| 325 | { |
| 326 | printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno)); |
| 327 | bailout(); |
| 328 | } |
| 329 | |
| 330 | bailout: |
| 331 | if(file) fclose(file); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | void compTest(tjhandle handle, unsigned char **dstBuf, |
| 336 | unsigned long *dstSize, int w, int h, int pf, char *basename, |
| 337 | int subsamp, int jpegQual, int flags) |
| 338 | { |
| 339 | char tempStr[1024]; unsigned char *srcBuf=NULL; |
| 340 | double t; |
| 341 | |
| 342 | if(yuv==YUVENCODE) |
| 343 | printf("%s %s -> %s YUV ... ", pixFormatStr[pf], |
| 344 | (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp]); |
| 345 | else |
| 346 | printf("%s %s -> %s Q%d ... ", pixFormatStr[pf], |
| 347 | (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down ", subNameLong[subsamp], |
| 348 | jpegQual); |
| 349 | |
| 350 | if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL) |
| 351 | _throw("Memory allocation failure"); |
| 352 | initBuf(srcBuf, w, h, pf, flags); |
| 353 | if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize); |
| 354 | |
| 355 | t=gettime(); |
| 356 | if(yuv==YUVENCODE) |
| 357 | { |
| 358 | _tj(tjEncodeYUV2(handle, srcBuf, w, 0, h, pf, *dstBuf, subsamp, flags)); |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | if(!alloc) |
| 363 | { |
| 364 | flags|=TJFLAG_NOREALLOC; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 365 | *dstSize=(yuv==YUVENCODE? tjBufSizeYUV(w, h, subsamp) |
| 366 | : tjBufSize(w, h, subsamp)); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 367 | } |
| 368 | _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp, |
| 369 | jpegQual, flags)); |
| 370 | } |
| 371 | t=gettime()-t; |
| 372 | |
| 373 | if(yuv==YUVENCODE) |
| 374 | snprintf(tempStr, 1024, "%s_enc_%s_%s_%s.yuv", basename, pixFormatStr[pf], |
| 375 | (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subName[subsamp]); |
| 376 | else |
| 377 | snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, |
| 378 | pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subName[subsamp], |
| 379 | jpegQual); |
| 380 | writeJPEG(*dstBuf, *dstSize, tempStr); |
| 381 | if(yuv==YUVENCODE) |
| 382 | { |
| 383 | if(checkBufYUV(*dstBuf, w, h, subsamp)) printf("Passed."); |
| 384 | else printf("FAILED!"); |
| 385 | } |
| 386 | else printf("Done."); |
| 387 | printf(" %f ms\n Result in %s\n", t*1000., tempStr); |
| 388 | |
| 389 | bailout: |
| 390 | if(srcBuf) free(srcBuf); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | void _decompTest(tjhandle handle, unsigned char *jpegBuf, |
| 395 | unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp, |
| 396 | int flags, tjscalingfactor sf) |
| 397 | { |
| 398 | unsigned char *dstBuf=NULL; |
| 399 | int _hdrw=0, _hdrh=0, _hdrsubsamp=-1; double t; |
| 400 | int scaledWidth=TJSCALED(w, sf); |
| 401 | int scaledHeight=TJSCALED(h, sf); |
| 402 | unsigned long dstSize=0; |
| 403 | |
| 404 | if(yuv==YUVENCODE) return; |
| 405 | |
| 406 | if(yuv==YUVDECODE) |
| 407 | printf("JPEG -> YUV %s ... ", subName[subsamp]); |
| 408 | else |
| 409 | { |
| 410 | printf("JPEG -> %s %s ", pixFormatStr[pf], |
| 411 | (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down "); |
| 412 | if(sf.num!=1 || sf.denom!=1) |
| 413 | printf("%d/%d ... ", sf.num, sf.denom); |
| 414 | else printf("... "); |
| 415 | } |
| 416 | |
| 417 | _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh, |
| 418 | &_hdrsubsamp)); |
| 419 | if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp) |
| 420 | _throw("Incorrect JPEG header"); |
| 421 | |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 422 | if(yuv==YUVDECODE) dstSize=tjBufSizeYUV(w, h, subsamp); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 423 | else dstSize=scaledWidth*scaledHeight*tjPixelSize[pf]; |
| 424 | if((dstBuf=(unsigned char *)malloc(dstSize))==NULL) |
| 425 | _throw("Memory allocation failure"); |
| 426 | memset(dstBuf, 0, dstSize); |
| 427 | |
| 428 | t=gettime(); |
| 429 | if(yuv==YUVDECODE) |
| 430 | { |
| 431 | _tj(tjDecompressToYUV(handle, jpegBuf, jpegSize, dstBuf, flags)); |
| 432 | } |
| 433 | else |
| 434 | { |
| 435 | _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0, |
| 436 | scaledHeight, pf, flags)); |
| 437 | } |
| 438 | t=gettime()-t; |
| 439 | |
| 440 | if(yuv==YUVDECODE) |
| 441 | { |
| 442 | if(checkBufYUV(dstBuf, w, h, subsamp)) printf("Passed."); |
| 443 | else printf("FAILED!"); |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags)) |
| 448 | printf("Passed."); |
| 449 | else printf("FAILED!"); |
| 450 | } |
| 451 | printf(" %f ms\n", t*1000.); |
| 452 | |
| 453 | bailout: |
| 454 | if(dstBuf) free(dstBuf); |
| 455 | } |
| 456 | |
| 457 | |
| 458 | void decompTest(tjhandle handle, unsigned char *jpegBuf, |
| 459 | unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp, |
| 460 | int flags) |
| 461 | { |
| 462 | int i, n=0; |
| 463 | tjscalingfactor *sf=tjGetScalingFactors(&n), sf1={1, 1}; |
| 464 | if(!sf || !n) _throwtj(); |
| 465 | |
| 466 | if((subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY) && !yuv) |
| 467 | { |
| 468 | for(i=0; i<n; i++) |
| 469 | _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp, |
| 470 | flags, sf[i]); |
| 471 | } |
| 472 | else |
| 473 | _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp, flags, |
| 474 | sf1); |
| 475 | |
| 476 | bailout: |
| 477 | printf("\n"); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | void doTest(int w, int h, const int *formats, int nformats, int subsamp, |
| 482 | char *basename) |
| 483 | { |
| 484 | tjhandle chandle=NULL, dhandle=NULL; |
| 485 | unsigned char *dstBuf=NULL; |
| 486 | unsigned long size=0; int pfi, pf, i; |
| 487 | |
| 488 | if(!alloc) |
| 489 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 490 | size=(yuv==YUVENCODE? tjBufSizeYUV(w, h, subsamp) |
| 491 | : tjBufSize(w, h, subsamp)); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 492 | if((dstBuf=(unsigned char *)tjAlloc(size))==NULL) |
| 493 | _throw("Memory allocation failure."); |
| 494 | } |
| 495 | |
| 496 | if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL) |
| 497 | _throwtj(); |
| 498 | |
| 499 | for(pfi=0; pfi<nformats; pfi++) |
| 500 | { |
| 501 | for(i=0; i<2; i++) |
| 502 | { |
| 503 | int flags=0; |
| 504 | if(i==1) |
| 505 | { |
| 506 | if(yuv==YUVDECODE) goto bailout; |
| 507 | else flags|=TJFLAG_BOTTOMUP; |
| 508 | } |
| 509 | pf=formats[pfi]; |
| 510 | compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100, |
| 511 | flags); |
| 512 | decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp, |
| 513 | flags); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | bailout: |
| 518 | if(chandle) tjDestroy(chandle); |
| 519 | if(dhandle) tjDestroy(dhandle); |
| 520 | |
| 521 | if(dstBuf) tjFree(dstBuf); |
| 522 | } |
| 523 | |
| 524 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 525 | void bufSizeTest(void) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 526 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 527 | int w, h, i, subsamp; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 528 | unsigned char *srcBuf=NULL, *jpegBuf=NULL; |
| 529 | tjhandle handle=NULL; |
| 530 | unsigned long jpegSize=0; |
| 531 | |
| 532 | if((handle=tjInitCompress())==NULL) _throwtj(); |
| 533 | |
| 534 | printf("Buffer size regression test\n"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 535 | for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 536 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 537 | for(w=1; w<48; w++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 538 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 539 | int maxh=(w==1)? 2048:48; |
| 540 | for(h=1; h<maxh; h++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 541 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 542 | if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h); |
| 543 | if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 544 | _throw("Memory allocation failure"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 545 | if(!alloc) |
| 546 | { |
| 547 | if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(w, h, subsamp))) |
| 548 | ==NULL) |
| 549 | _throw("Memory allocation failure"); |
| 550 | jpegSize=tjBufSize(w, h, subsamp); |
| 551 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 552 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 553 | for(i=0; i<w*h*4; i++) |
| 554 | { |
| 555 | if(random()<RAND_MAX/2) srcBuf[i]=0; |
| 556 | else srcBuf[i]=255; |
| 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 | _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &jpegBuf, |
| 560 | &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC)); |
| 561 | free(srcBuf); srcBuf=NULL; |
| 562 | tjFree(jpegBuf); jpegBuf=NULL; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 563 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 564 | if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 565 | _throw("Memory allocation failure"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 566 | if(!alloc) |
| 567 | { |
| 568 | if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(h, w, subsamp))) |
| 569 | ==NULL) |
| 570 | _throw("Memory allocation failure"); |
| 571 | jpegSize=tjBufSize(h, w, subsamp); |
| 572 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 573 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 574 | for(i=0; i<h*w*4; i++) |
| 575 | { |
| 576 | if(random()<RAND_MAX/2) srcBuf[i]=0; |
| 577 | else srcBuf[i]=255; |
| 578 | } |
| 579 | |
| 580 | _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &jpegBuf, |
| 581 | &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC)); |
| 582 | free(srcBuf); srcBuf=NULL; |
| 583 | tjFree(jpegBuf); jpegBuf=NULL; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 584 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | printf("Done. \n"); |
| 588 | |
| 589 | bailout: |
| 590 | if(srcBuf) free(srcBuf); |
| 591 | if(jpegBuf) free(jpegBuf); |
| 592 | if(handle) tjDestroy(handle); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | int main(int argc, char *argv[]) |
| 597 | { |
| 598 | int doyuv=0, i; |
DRC | e835ee3 | 2011-07-15 10:06:56 +0000 | [diff] [blame] | 599 | #ifdef _WIN32 |
| 600 | srand((unsigned int)time(NULL)); |
| 601 | #endif |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 602 | if(argc>1) |
| 603 | { |
| 604 | for(i=1; i<argc; i++) |
| 605 | { |
| 606 | if(!strcasecmp(argv[i], "-yuv")) doyuv=1; |
| 607 | if(!strcasecmp(argv[i], "-alloc")) alloc=1; |
| 608 | if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?")) |
| 609 | usage(argv[0]); |
| 610 | } |
| 611 | } |
| 612 | if(alloc) printf("Testing automatic buffer allocation\n"); |
| 613 | if(doyuv) {yuv=YUVENCODE; alloc=0;} |
| 614 | doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test"); |
| 615 | doTest(39, 41, _4byteFormats, 4, TJSAMP_444, "test"); |
| 616 | if(doyuv) |
| 617 | { |
| 618 | doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test"); |
| 619 | doTest(35, 39, _4byteFormats, 4, TJSAMP_422, "test"); |
| 620 | doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test"); |
| 621 | doTest(41, 35, _4byteFormats, 4, TJSAMP_420, "test"); |
| 622 | doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test"); |
| 623 | doTest(39, 41, _4byteFormats, 4, TJSAMP_440, "test"); |
| 624 | } |
| 625 | doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test"); |
| 626 | doTest(39, 41, _3byteFormats, 2, TJSAMP_GRAY, "test"); |
| 627 | doTest(41, 35, _4byteFormats, 4, TJSAMP_GRAY, "test"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 628 | if(!doyuv) bufSizeTest(); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 629 | if(doyuv) |
| 630 | { |
| 631 | yuv=YUVDECODE; |
| 632 | doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0"); |
| 633 | doTest(35, 39, _onlyRGB, 1, TJSAMP_444, "test_yuv1"); |
| 634 | doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0"); |
| 635 | doTest(39, 41, _onlyRGB, 1, TJSAMP_422, "test_yuv1"); |
| 636 | doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0"); |
| 637 | doTest(41, 35, _onlyRGB, 1, TJSAMP_420, "test_yuv1"); |
| 638 | doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0"); |
| 639 | doTest(35, 39, _onlyRGB, 1, TJSAMP_440, "test_yuv1"); |
| 640 | doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0"); |
| 641 | doTest(35, 39, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv1"); |
| 642 | doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0"); |
| 643 | doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test_yuv1"); |
| 644 | } |
| 645 | |
| 646 | return exitStatus; |
| 647 | } |