DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 1 | /* |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 2 | * Copyright (C)2009-2014 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"); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 50 | printf("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n"); |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 51 | printf(" 4-byte boundary\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 52 | 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 | |
| 62 | const char *subNameLong[TJ_NUMSAMP]= |
| 63 | { |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 64 | "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1" |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 65 | }; |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 66 | const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440", "411"}; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 67 | |
| 68 | const char *pixFormatStr[TJ_NUMPF]= |
| 69 | { |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 70 | "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale", |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 71 | "RGBA", "BGRA", "ABGR", "ARGB", "CMYK" |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 74 | const int alphaOffset[TJ_NUMPF] = {-1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1}; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 75 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 76 | const int _3byteFormats[]={TJPF_RGB, TJPF_BGR}; |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 77 | const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB, |
| 78 | TJPF_CMYK}; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 79 | const int _onlyGray[]={TJPF_GRAY}; |
| 80 | const int _onlyRGB[]={TJPF_RGB}; |
| 81 | |
| 82 | enum {YUVENCODE=1, YUVDECODE}; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 83 | int yuv=0, alloc=0, pad=4; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 84 | |
| 85 | int exitStatus=0; |
| 86 | #define bailout() {exitStatus=-1; goto bailout;} |
| 87 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 88 | |
| 89 | void 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 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 97 | if(pf==TJPF_GRAY) |
| 98 | { |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 99 | memset(buf, 0, w*h*ps); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 100 | 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 | } |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 111 | 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 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 132 | else |
| 133 | { |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 134 | memset(buf, 0, w*h*ps); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 135 | 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 | |
| 181 | int 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]; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 187 | int aoffset=alphaOffset[pf]; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 188 | 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 | |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 193 | 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 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 223 | for(row=0; row<h; row++) |
| 224 | { |
| 225 | for(col=0; col<w; col++) |
| 226 | { |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 227 | unsigned char r, g, b, a; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 228 | 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]; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 233 | a=aoffset>=0? buf[index*ps+aoffset]:0xFF; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 234 | 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 | } |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 270 | checkval255(a); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | bailout: |
| 275 | if(retval==0) |
| 276 | { |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 277 | for(row=0; row<h; row++) |
| 278 | { |
| 279 | for(col=0; col<w; col++) |
| 280 | { |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 281 | 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]); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 288 | } |
| 289 | printf("\n"); |
| 290 | } |
| 291 | } |
| 292 | return retval; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) |
| 297 | |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 298 | void 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 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 352 | int checkBufYUV(unsigned char *buf, int w, int h, int subsamp, |
| 353 | tjscalingfactor sf) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 354 | { |
| 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; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 359 | int ypitch=PAD(pw, pad), uvpitch=PAD(cw, pad); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 360 | int retval=1; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 361 | int halfway=16*sf.num/sf.denom; |
| 362 | int blocksize=8*sf.num/sf.denom; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 363 | |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 364 | for(row=0; row<ph; row++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 365 | { |
| 366 | for(col=0; col<pw; col++) |
| 367 | { |
| 368 | unsigned char y=buf[ypitch*row+col]; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 369 | if(((row/blocksize)+(col/blocksize))%2==0) |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 370 | { |
| 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 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | if(subsamp!=TJSAMP_GRAY) |
| 380 | { |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 381 | int halfway=16/vsf*sf.num/sf.denom; |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 382 | for(row=0; row<ch; row++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 383 | { |
| 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)]; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 388 | if(((row*vsf/blocksize)+(col*hsf/blocksize))%2==0) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 389 | { |
| 390 | checkval(u, 128); checkval(v, 128); |
| 391 | } |
| 392 | else |
| 393 | { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 394 | if(row<halfway) |
| 395 | { |
| 396 | checkval(u, 85); checkval255(v); |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | checkval0(u); checkval(v, 149); |
| 401 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 402 | } |
| 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 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return retval; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | void 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 | |
| 450 | void 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; |
DRC | fe73965 | 2013-10-31 04:53:27 +0000 | [diff] [blame] | 455 | 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"; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 458 | double t; |
| 459 | |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 460 | if(yuv==YUVDECODE) |
| 461 | { |
DRC | fe73965 | 2013-10-31 04:53:27 +0000 | [diff] [blame] | 462 | printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong, |
| 463 | jpegQual); |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 464 | if((srcBuf=(unsigned char *)malloc(tjBufSizeYUV2(w, pad, h, subsamp))) |
| 465 | ==NULL) |
| 466 | _throw("Memory allocation failure"); |
| 467 | initBufYUV(srcBuf, w, pad, h, subsamp); |
| 468 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 469 | else |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 470 | { |
| 471 | if(yuv==YUVENCODE) |
DRC | fe73965 | 2013-10-31 04:53:27 +0000 | [diff] [blame] | 472 | printf("%s %s -> %s YUV ... ", pfStr, buStrLong, subNameLong[subsamp]); |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 473 | else |
DRC | fe73965 | 2013-10-31 04:53:27 +0000 | [diff] [blame] | 474 | printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp], |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 475 | jpegQual); |
| 476 | if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL) |
| 477 | _throw("Memory allocation failure"); |
| 478 | initBuf(srcBuf, w, h, pf, flags); |
| 479 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 480 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 481 | if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize); |
| 482 | |
| 483 | t=gettime(); |
| 484 | if(yuv==YUVENCODE) |
| 485 | { |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 486 | _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, pf, *dstBuf, pad, subsamp, |
| 487 | flags)); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 488 | } |
| 489 | else |
| 490 | { |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 491 | if(!alloc) flags|=TJFLAG_NOREALLOC; |
| 492 | if(yuv==YUVDECODE) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 493 | { |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 494 | _tj(tjCompressFromYUV(handle, srcBuf, w, pad, h, subsamp, dstBuf, |
| 495 | dstSize, jpegQual, flags)); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 496 | } |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 497 | else |
| 498 | { |
| 499 | _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp, |
| 500 | jpegQual, flags)); |
| 501 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 502 | } |
| 503 | t=gettime()-t; |
| 504 | |
| 505 | if(yuv==YUVENCODE) |
DRC | fe73965 | 2013-10-31 04:53:27 +0000 | [diff] [blame] | 506 | snprintf(tempStr, 1024, "%s_enc_%s_%s_%s.yuv", basename, pfStr, buStr, |
| 507 | subName[subsamp]); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 508 | else |
DRC | fe73965 | 2013-10-31 04:53:27 +0000 | [diff] [blame] | 509 | snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr, |
| 510 | subName[subsamp], jpegQual); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 511 | writeJPEG(*dstBuf, *dstSize, tempStr); |
| 512 | if(yuv==YUVENCODE) |
| 513 | { |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 514 | tjscalingfactor sf={1, 1}; |
| 515 | if(checkBufYUV(*dstBuf, w, h, subsamp, sf)) printf("Passed."); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 516 | else printf("FAILED!"); |
| 517 | } |
| 518 | else printf("Done."); |
| 519 | printf(" %f ms\n Result in %s\n", t*1000., tempStr); |
| 520 | |
| 521 | bailout: |
| 522 | if(srcBuf) free(srcBuf); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | void _decompTest(tjhandle handle, unsigned char *jpegBuf, |
| 527 | unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp, |
| 528 | int flags, tjscalingfactor sf) |
| 529 | { |
| 530 | unsigned char *dstBuf=NULL; |
| 531 | int _hdrw=0, _hdrh=0, _hdrsubsamp=-1; double t; |
| 532 | int scaledWidth=TJSCALED(w, sf); |
| 533 | int scaledHeight=TJSCALED(h, sf); |
| 534 | unsigned long dstSize=0; |
| 535 | |
| 536 | if(yuv==YUVENCODE) return; |
| 537 | |
| 538 | if(yuv==YUVDECODE) |
DRC | 38cb1ec | 2013-08-23 04:45:43 +0000 | [diff] [blame] | 539 | printf("JPEG -> YUV %s ", subNameLong[subsamp]); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 540 | else |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 541 | printf("JPEG -> %s %s ", pixFormatStr[pf], |
| 542 | (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down "); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 543 | if(sf.num!=1 || sf.denom!=1) |
| 544 | printf("%d/%d ... ", sf.num, sf.denom); |
| 545 | else printf("... "); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 546 | |
| 547 | _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh, |
| 548 | &_hdrsubsamp)); |
| 549 | if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp) |
| 550 | _throw("Incorrect JPEG header"); |
| 551 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 552 | if(yuv==YUVDECODE) |
| 553 | dstSize=tjBufSizeYUV2(scaledWidth, pad, scaledHeight, subsamp); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 554 | else dstSize=scaledWidth*scaledHeight*tjPixelSize[pf]; |
| 555 | if((dstBuf=(unsigned char *)malloc(dstSize))==NULL) |
| 556 | _throw("Memory allocation failure"); |
| 557 | memset(dstBuf, 0, dstSize); |
| 558 | |
| 559 | t=gettime(); |
| 560 | if(yuv==YUVDECODE) |
| 561 | { |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 562 | _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, |
| 563 | pad, scaledHeight, flags)); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 564 | } |
| 565 | else |
| 566 | { |
| 567 | _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0, |
| 568 | scaledHeight, pf, flags)); |
| 569 | } |
| 570 | t=gettime()-t; |
| 571 | |
| 572 | if(yuv==YUVDECODE) |
| 573 | { |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 574 | if(checkBufYUV(dstBuf, scaledWidth, scaledHeight, subsamp, sf)) |
| 575 | printf("Passed."); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 576 | else printf("FAILED!"); |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags)) |
| 581 | printf("Passed."); |
| 582 | else printf("FAILED!"); |
| 583 | } |
| 584 | printf(" %f ms\n", t*1000.); |
| 585 | |
| 586 | bailout: |
| 587 | if(dstBuf) free(dstBuf); |
| 588 | } |
| 589 | |
| 590 | |
| 591 | void decompTest(tjhandle handle, unsigned char *jpegBuf, |
| 592 | unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp, |
| 593 | int flags) |
| 594 | { |
| 595 | int i, n=0; |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 596 | tjscalingfactor *sf=tjGetScalingFactors(&n); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 597 | if(!sf || !n) _throwtj(); |
| 598 | |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 599 | for(i=0; i<n; i++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 600 | { |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 601 | if(subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY || |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 602 | (subsamp==TJSAMP_411 && sf[i].num==1 && |
| 603 | (sf[i].denom==2 || sf[i].denom==1)) || |
| 604 | (subsamp!=TJSAMP_411 && sf[i].num==1 && |
| 605 | (sf[i].denom==4 || sf[i].denom==2 || sf[i].denom==1))) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 606 | _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp, |
| 607 | flags, sf[i]); |
| 608 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 609 | |
| 610 | bailout: |
DRC | b7c4193 | 2013-05-04 23:41:33 +0000 | [diff] [blame] | 611 | return; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
| 615 | void doTest(int w, int h, const int *formats, int nformats, int subsamp, |
| 616 | char *basename) |
| 617 | { |
| 618 | tjhandle chandle=NULL, dhandle=NULL; |
| 619 | unsigned char *dstBuf=NULL; |
| 620 | unsigned long size=0; int pfi, pf, i; |
| 621 | |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 622 | if(yuv==YUVENCODE) |
| 623 | size=tjBufSizeYUV2(w, pad, h, subsamp); |
| 624 | else if(!alloc) |
| 625 | size=tjBufSize(w, h, subsamp); |
| 626 | if(size!=0) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 627 | if((dstBuf=(unsigned char *)tjAlloc(size))==NULL) |
| 628 | _throw("Memory allocation failure."); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 629 | |
| 630 | if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL) |
| 631 | _throwtj(); |
| 632 | |
| 633 | for(pfi=0; pfi<nformats; pfi++) |
| 634 | { |
| 635 | for(i=0; i<2; i++) |
| 636 | { |
| 637 | int flags=0; |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 638 | if(subsamp==TJSAMP_422 || subsamp==TJSAMP_420 || subsamp==TJSAMP_440 || |
| 639 | subsamp==TJSAMP_411) |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 640 | flags|=TJFLAG_FASTUPSAMPLE; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 641 | if(i==1) |
| 642 | { |
| 643 | if(yuv==YUVDECODE) goto bailout; |
| 644 | else flags|=TJFLAG_BOTTOMUP; |
| 645 | } |
| 646 | pf=formats[pfi]; |
| 647 | compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100, |
| 648 | flags); |
| 649 | decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp, |
| 650 | flags); |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 651 | if(pf>=TJPF_RGBX && pf<=TJPF_XRGB) |
| 652 | decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX), |
| 653 | basename, subsamp, flags); |
DRC | b7c4193 | 2013-05-04 23:41:33 +0000 | [diff] [blame] | 654 | printf("\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
DRC | b7c4193 | 2013-05-04 23:41:33 +0000 | [diff] [blame] | 657 | printf("--------------------\n\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 658 | |
| 659 | bailout: |
| 660 | if(chandle) tjDestroy(chandle); |
| 661 | if(dhandle) tjDestroy(dhandle); |
| 662 | |
| 663 | if(dstBuf) tjFree(dstBuf); |
| 664 | } |
| 665 | |
| 666 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 667 | void bufSizeTest(void) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 668 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 669 | int w, h, i, subsamp; |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 670 | unsigned char *srcBuf=NULL, *dstBuf=NULL; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 671 | tjhandle handle=NULL; |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 672 | unsigned long dstSize=0; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 673 | |
| 674 | if((handle=tjInitCompress())==NULL) _throwtj(); |
| 675 | |
| 676 | printf("Buffer size regression test\n"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 677 | for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 678 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 679 | for(w=1; w<48; w++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 680 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 681 | int maxh=(w==1)? 2048:48; |
| 682 | for(h=1; h<maxh; h++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 683 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 684 | if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h); |
| 685 | if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 686 | _throw("Memory allocation failure"); |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 687 | if(!alloc || yuv==YUVENCODE) |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 688 | { |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 689 | if(yuv==YUVENCODE) dstSize=tjBufSizeYUV2(w, pad, h, subsamp); |
| 690 | else dstSize=tjBufSize(w, h, subsamp); |
| 691 | if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL) |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 692 | _throw("Memory allocation failure"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 693 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 694 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 695 | for(i=0; i<w*h*4; i++) |
| 696 | { |
| 697 | if(random()<RAND_MAX/2) srcBuf[i]=0; |
| 698 | else srcBuf[i]=255; |
| 699 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 700 | |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 701 | if(yuv==YUVENCODE) |
| 702 | { |
| 703 | _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad, |
| 704 | subsamp, 0)); |
| 705 | } |
| 706 | else |
| 707 | { |
| 708 | _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf, |
| 709 | &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC)); |
| 710 | } |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 711 | free(srcBuf); srcBuf=NULL; |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 712 | tjFree(dstBuf); dstBuf=NULL; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 713 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 714 | if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 715 | _throw("Memory allocation failure"); |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 716 | if(!alloc || yuv==YUVENCODE) |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 717 | { |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 718 | if(yuv==YUVENCODE) dstSize=tjBufSizeYUV2(h, pad, w, subsamp); |
| 719 | else dstSize=tjBufSize(h, w, subsamp); |
| 720 | if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL) |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 721 | _throw("Memory allocation failure"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 722 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 723 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 724 | for(i=0; i<h*w*4; i++) |
| 725 | { |
| 726 | if(random()<RAND_MAX/2) srcBuf[i]=0; |
| 727 | else srcBuf[i]=255; |
| 728 | } |
| 729 | |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 730 | if(yuv==YUVENCODE) |
| 731 | { |
| 732 | _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad, |
| 733 | subsamp, 0)); |
| 734 | } |
| 735 | else |
| 736 | { |
| 737 | _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf, |
| 738 | &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC)); |
| 739 | } |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 740 | free(srcBuf); srcBuf=NULL; |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 741 | tjFree(dstBuf); dstBuf=NULL; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 742 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | printf("Done. \n"); |
| 746 | |
| 747 | bailout: |
| 748 | if(srcBuf) free(srcBuf); |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 749 | if(dstBuf) free(dstBuf); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 750 | if(handle) tjDestroy(handle); |
| 751 | } |
| 752 | |
| 753 | |
| 754 | int main(int argc, char *argv[]) |
| 755 | { |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 756 | int doyuv=0, i, num4bf=5; |
DRC | e835ee3 | 2011-07-15 10:06:56 +0000 | [diff] [blame] | 757 | #ifdef _WIN32 |
| 758 | srand((unsigned int)time(NULL)); |
| 759 | #endif |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 760 | if(argc>1) |
| 761 | { |
| 762 | for(i=1; i<argc; i++) |
| 763 | { |
| 764 | if(!strcasecmp(argv[i], "-yuv")) doyuv=1; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 765 | if(!strcasecmp(argv[i], "-noyuvpad")) pad=1; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 766 | if(!strcasecmp(argv[i], "-alloc")) alloc=1; |
| 767 | if(!strncasecmp(argv[i], "-h", 2) || !strcasecmp(argv[i], "-?")) |
| 768 | usage(argv[0]); |
| 769 | } |
| 770 | } |
| 771 | if(alloc) printf("Testing automatic buffer allocation\n"); |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 772 | if(doyuv) {yuv=YUVENCODE; num4bf=4;} |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 773 | doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 774 | doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test"); |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 775 | doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 776 | doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test"); |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 777 | doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 778 | doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test"); |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 779 | doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 780 | doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test"); |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 781 | doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 782 | doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test"); |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 783 | doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test"); |
| 784 | doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test"); |
| 785 | doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test"); |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 786 | bufSizeTest(); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 787 | if(doyuv) |
| 788 | { |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame^] | 789 | printf("\n--------------------\n\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 790 | yuv=YUVDECODE; |
| 791 | doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0"); |
| 792 | doTest(35, 39, _onlyRGB, 1, TJSAMP_444, "test_yuv1"); |
| 793 | doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0"); |
| 794 | doTest(39, 41, _onlyRGB, 1, TJSAMP_422, "test_yuv1"); |
| 795 | doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0"); |
| 796 | doTest(41, 35, _onlyRGB, 1, TJSAMP_420, "test_yuv1"); |
| 797 | doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0"); |
| 798 | doTest(35, 39, _onlyRGB, 1, TJSAMP_440, "test_yuv1"); |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 799 | doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0"); |
| 800 | doTest(39, 41, _onlyRGB, 1, TJSAMP_411, "test_yuv1"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 801 | doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0"); |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 802 | doTest(41, 35, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv1"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 803 | doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0"); |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 804 | doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test_yuv1"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | return exitStatus; |
| 808 | } |