DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 1 | /* |
DRC | e006f5c | 2012-01-28 06:31:44 +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 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <math.h> |
| 33 | #include <errno.h> |
| 34 | #include <cdjpeg.h> |
| 35 | #include "./bmp.h" |
| 36 | #include "./tjutil.h" |
| 37 | #include "./turbojpeg.h" |
| 38 | |
| 39 | |
| 40 | #define _throw(op, err) { \ |
| 41 | printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \ |
| 42 | retval=-1; goto bailout;} |
| 43 | #define _throwunix(m) _throw(m, strerror(errno)) |
| 44 | #define _throwtj(m) _throw(m, tjGetErrorStr()) |
| 45 | #define _throwbmp(m) _throw(m, bmpgeterr()) |
| 46 | |
| 47 | enum {YUVENCODE=1, YUVDECODE}; |
| 48 | int flags=TJFLAG_NOREALLOC, decomponly=0, yuv=0, quiet=0, dotile=0, |
| 49 | pf=TJPF_BGR; |
| 50 | char *ext="ppm"; |
| 51 | const char *pixFormatStr[TJ_NUMPF]= |
| 52 | { |
| 53 | "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY" |
| 54 | }; |
| 55 | const char *subNameLong[TJ_NUMSAMP]= |
| 56 | { |
| 57 | "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0" |
| 58 | }; |
| 59 | const char *subName[NUMSUBOPT]={"444", "422", "420", "GRAY", "440"}; |
| 60 | tjscalingfactor *scalingfactors=NULL, sf={1, 1}; int nsf=0; |
| 61 | int xformop=TJXOP_NONE, xformopt=0; |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 62 | int (*customFilter)(short *, tjregion, tjregion, int, int, tjtransform *); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 63 | double benchtime=5.0; |
| 64 | |
| 65 | |
| 66 | char *sigfig(double val, int figs, char *buf, int len) |
| 67 | { |
| 68 | char format[80]; |
| 69 | int digitsafterdecimal=figs-(int)ceil(log10(fabs(val))); |
| 70 | if(digitsafterdecimal<1) snprintf(format, 80, "%%.0f"); |
| 71 | else snprintf(format, 80, "%%.%df", digitsafterdecimal); |
| 72 | snprintf(buf, len, format, val); |
| 73 | return buf; |
| 74 | } |
| 75 | |
| 76 | |
DRC | 57a3736 | 2011-09-17 00:41:14 +0000 | [diff] [blame] | 77 | /* Custom DCT filter which produces a negative of the image */ |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 78 | int dummyDCTFilter(short *coeffs, tjregion arrayRegion, tjregion planeRegion, |
DRC | f546711 | 2011-09-20 05:02:19 +0000 | [diff] [blame] | 79 | int componentIndex, int transformIndex, tjtransform *transform) |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 80 | { |
| 81 | int i; |
| 82 | for(i=0; i<arrayRegion.w*arrayRegion.h; i++) coeffs[i]=-coeffs[i]; |
| 83 | return 0; |
| 84 | } |
| 85 | |
DRC | 57a3736 | 2011-09-17 00:41:14 +0000 | [diff] [blame] | 86 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 87 | /* Decompression test */ |
| 88 | int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf, |
| 89 | unsigned long *jpegsize, unsigned char *dstbuf, int w, int h, |
| 90 | int subsamp, int jpegqual, char *filename, int tilew, int tileh) |
| 91 | { |
| 92 | char tempstr[1024], sizestr[20]="\0", qualstr[6]="\0", *ptr; |
| 93 | FILE *file=NULL; tjhandle handle=NULL; |
| 94 | int row, col, i, dstbufalloc=0, retval=0; |
| 95 | double start, elapsed; |
| 96 | int ps=tjPixelSize[pf]; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 97 | int yuvsize=tjBufSizeYUV(w, h, subsamp), bufsize; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 98 | int scaledw=(yuv==YUVDECODE)? w : TJSCALED(w, sf); |
| 99 | int scaledh=(yuv==YUVDECODE)? h : TJSCALED(h, sf); |
| 100 | int pitch=scaledw*ps; |
| 101 | int ntilesw=(w+tilew-1)/tilew, ntilesh=(h+tileh-1)/tileh; |
| 102 | unsigned char *dstptr, *dstptr2; |
| 103 | |
| 104 | if(jpegqual>0) |
| 105 | { |
| 106 | snprintf(qualstr, 6, "_Q%d", jpegqual); |
| 107 | qualstr[5]=0; |
| 108 | } |
| 109 | |
| 110 | if((handle=tjInitDecompress())==NULL) |
| 111 | _throwtj("executing tjInitDecompress()"); |
| 112 | |
DRC | e006f5c | 2012-01-28 06:31:44 +0000 | [diff] [blame] | 113 | bufsize=(yuv==YUVDECODE? yuvsize:pitch*scaledh); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 114 | if(dstbuf==NULL) |
| 115 | { |
| 116 | if((dstbuf=(unsigned char *)malloc(bufsize)) == NULL) |
| 117 | _throwunix("allocating image buffer"); |
| 118 | dstbufalloc=1; |
| 119 | } |
| 120 | /* Set the destination buffer to gray so we know whether the decompressor |
| 121 | attempted to write to it */ |
| 122 | memset(dstbuf, 127, bufsize); |
| 123 | |
| 124 | /* Execute once to preload cache */ |
| 125 | if(yuv==YUVDECODE) |
| 126 | { |
| 127 | if(tjDecompressToYUV(handle, jpegbuf[0], jpegsize[0], dstbuf, flags)==-1) |
| 128 | _throwtj("executing tjDecompressToYUV()"); |
| 129 | } |
| 130 | else if(tjDecompress2(handle, jpegbuf[0], jpegsize[0], dstbuf, scaledw, |
| 131 | pitch, scaledh, pf, flags)==-1) |
| 132 | _throwtj("executing tjDecompress2()"); |
| 133 | |
| 134 | /* Benchmark */ |
| 135 | for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++) |
| 136 | { |
| 137 | int tile=0; |
| 138 | if(yuv==YUVDECODE) |
| 139 | { |
| 140 | if(tjDecompressToYUV(handle, jpegbuf[0], jpegsize[0], dstbuf, flags)==-1) |
| 141 | _throwtj("executing tjDecompressToYUV()"); |
| 142 | } |
| 143 | else for(row=0, dstptr=dstbuf; row<ntilesh; row++, dstptr+=pitch*tileh) |
| 144 | { |
| 145 | for(col=0, dstptr2=dstptr; col<ntilesw; col++, tile++, dstptr2+=ps*tilew) |
| 146 | { |
| 147 | int width=dotile? min(tilew, w-col*tilew):scaledw; |
| 148 | int height=dotile? min(tileh, h-row*tileh):scaledh; |
| 149 | if(tjDecompress2(handle, jpegbuf[tile], jpegsize[tile], dstptr2, width, |
| 150 | pitch, height, pf, flags)==-1) |
| 151 | _throwtj("executing tjDecompress2()"); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()"); |
| 157 | handle=NULL; |
| 158 | |
| 159 | if(quiet) |
| 160 | { |
| 161 | printf("%s\n", |
| 162 | sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024)); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | printf("D--> Frame rate: %f fps\n", (double)i/elapsed); |
| 167 | printf(" Dest. throughput: %f Megapixels/sec\n", |
| 168 | (double)(w*h)/1000000.*(double)i/elapsed); |
| 169 | } |
| 170 | if(yuv==YUVDECODE) |
| 171 | { |
| 172 | snprintf(tempstr, 1024, "%s_%s%s.yuv", filename, subName[subsamp], |
| 173 | qualstr); |
| 174 | if((file=fopen(tempstr, "wb"))==NULL) |
| 175 | _throwunix("opening YUV image for output"); |
| 176 | if(fwrite(dstbuf, yuvsize, 1, file)!=1) |
| 177 | _throwunix("writing YUV image"); |
| 178 | fclose(file); file=NULL; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | if(sf.num!=1 || sf.denom!=1) |
| 183 | snprintf(sizestr, 20, "%d_%d", sf.num, sf.denom); |
| 184 | else if(tilew!=w || tileh!=h) |
| 185 | snprintf(sizestr, 20, "%dx%d", tilew, tileh); |
| 186 | else snprintf(sizestr, 20, "full"); |
| 187 | if(decomponly) |
| 188 | snprintf(tempstr, 1024, "%s_%s.%s", filename, sizestr, ext); |
| 189 | else |
| 190 | snprintf(tempstr, 1024, "%s_%s%s_%s.%s", filename, subName[subsamp], |
| 191 | qualstr, sizestr, ext); |
| 192 | if(savebmp(tempstr, dstbuf, scaledw, scaledh, pf, |
| 193 | (flags&TJFLAG_BOTTOMUP)!=0)==-1) |
| 194 | _throwbmp("saving bitmap"); |
| 195 | ptr=strrchr(tempstr, '.'); |
| 196 | snprintf(ptr, 1024-(ptr-tempstr), "-err.%s", ext); |
| 197 | if(srcbuf && sf.num==1 && sf.denom==1) |
| 198 | { |
| 199 | if(!quiet) printf("Compression error written to %s.\n", tempstr); |
| 200 | if(subsamp==TJ_GRAYSCALE) |
| 201 | { |
| 202 | int index, index2; |
| 203 | for(row=0, index=0; row<h; row++, index+=pitch) |
| 204 | { |
| 205 | for(col=0, index2=index; col<w; col++, index2+=ps) |
| 206 | { |
| 207 | int rindex=index2+tjRedOffset[pf]; |
| 208 | int gindex=index2+tjGreenOffset[pf]; |
| 209 | int bindex=index2+tjBlueOffset[pf]; |
| 210 | int y=(int)((double)srcbuf[rindex]*0.299 |
| 211 | + (double)srcbuf[gindex]*0.587 |
| 212 | + (double)srcbuf[bindex]*0.114 + 0.5); |
| 213 | if(y>255) y=255; if(y<0) y=0; |
| 214 | dstbuf[rindex]=abs(dstbuf[rindex]-y); |
| 215 | dstbuf[gindex]=abs(dstbuf[gindex]-y); |
| 216 | dstbuf[bindex]=abs(dstbuf[bindex]-y); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | for(row=0; row<h; row++) |
| 223 | for(col=0; col<w*ps; col++) |
| 224 | dstbuf[pitch*row+col] |
| 225 | =abs(dstbuf[pitch*row+col]-srcbuf[pitch*row+col]); |
| 226 | } |
| 227 | if(savebmp(tempstr, dstbuf, w, h, pf, |
| 228 | (flags&TJFLAG_BOTTOMUP)!=0)==-1) |
| 229 | _throwbmp("saving bitmap"); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | bailout: |
| 234 | if(file) {fclose(file); file=NULL;} |
| 235 | if(handle) {tjDestroy(handle); handle=NULL;} |
| 236 | if(dstbuf && dstbufalloc) {free(dstbuf); dstbuf=NULL;} |
| 237 | return retval; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | void dotestyuv(unsigned char *srcbuf, int w, int h, int subsamp, |
| 242 | char *filename) |
| 243 | { |
| 244 | char tempstr[1024], tempstr2[80]; |
| 245 | FILE *file=NULL; tjhandle handle=NULL; |
| 246 | unsigned char *dstbuf=NULL; |
| 247 | double start, elapsed; |
| 248 | int i, retval=0, ps=tjPixelSize[pf]; |
| 249 | int yuvsize=0; |
| 250 | |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 251 | yuvsize=tjBufSizeYUV(w, h, subsamp); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 252 | if((dstbuf=(unsigned char *)malloc(yuvsize)) == NULL) |
| 253 | _throwunix("allocating image buffer"); |
| 254 | |
| 255 | if(!quiet) |
| 256 | printf(">>>>> %s (%s) <--> YUV %s <<<<<\n", pixFormatStr[pf], |
| 257 | (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp]); |
| 258 | |
| 259 | if(quiet==1) |
| 260 | printf("%s\t%s\t%s\tN/A\t", pixFormatStr[pf], |
| 261 | (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp]); |
| 262 | |
| 263 | if((handle=tjInitCompress())==NULL) |
| 264 | _throwtj("executing tjInitCompress()"); |
| 265 | |
| 266 | /* Execute once to preload cache */ |
| 267 | if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1) |
| 268 | _throwtj("executing tjEncodeYUV2()"); |
| 269 | |
| 270 | /* Benchmark */ |
| 271 | for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++) |
| 272 | { |
| 273 | if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1) |
| 274 | _throwtj("executing tjEncodeYUV2()"); |
| 275 | } |
| 276 | |
| 277 | if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()"); |
| 278 | handle=NULL; |
| 279 | |
| 280 | if(quiet==1) printf("%-4d %-4d\t", w, h); |
| 281 | if(quiet) |
| 282 | { |
| 283 | printf("%s%c%s%c", |
| 284 | sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024), |
| 285 | quiet==2? '\n':'\t', |
| 286 | sigfig((double)(w*h*ps)/(double)yuvsize, 4, tempstr2, 80), |
| 287 | quiet==2? '\n':'\t'); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | printf("\n%s size: %d x %d\n", "Image", w, h); |
| 292 | printf("C--> Frame rate: %f fps\n", (double)i/elapsed); |
| 293 | printf(" Output image size: %d bytes\n", yuvsize); |
| 294 | printf(" Compression ratio: %f:1\n", |
| 295 | (double)(w*h*ps)/(double)yuvsize); |
| 296 | printf(" Source throughput: %f Megapixels/sec\n", |
| 297 | (double)(w*h)/1000000.*(double)i/elapsed); |
| 298 | printf(" Output bit stream: %f Megabits/sec\n", |
| 299 | (double)yuvsize*8./1000000.*(double)i/elapsed); |
| 300 | } |
| 301 | snprintf(tempstr, 1024, "%s_%s.yuv", filename, subName[subsamp]); |
| 302 | if((file=fopen(tempstr, "wb"))==NULL) |
| 303 | _throwunix("opening reference image"); |
| 304 | if(fwrite(dstbuf, yuvsize, 1, file)!=1) |
| 305 | _throwunix("writing reference image"); |
| 306 | fclose(file); file=NULL; |
| 307 | if(!quiet) printf("Reference image written to %s\n", tempstr); |
| 308 | |
| 309 | bailout: |
| 310 | if(file) {fclose(file); file=NULL;} |
| 311 | if(dstbuf) {free(dstbuf); dstbuf=NULL;} |
| 312 | if(handle) {tjDestroy(handle); handle=NULL;} |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | void dotest(unsigned char *srcbuf, int w, int h, int subsamp, int jpegqual, |
| 318 | char *filename) |
| 319 | { |
| 320 | char tempstr[1024], tempstr2[80]; |
| 321 | FILE *file=NULL; tjhandle handle=NULL; |
| 322 | unsigned char **jpegbuf=NULL, *tmpbuf=NULL, *srcptr, *srcptr2; |
| 323 | double start, elapsed; |
| 324 | int totaljpegsize=0, row, col, i, tilew=w, tileh=h, retval=0; |
| 325 | unsigned long *jpegsize=NULL; |
| 326 | int ps=tjPixelSize[pf], ntilesw=1, ntilesh=1, pitch=w*ps; |
| 327 | |
| 328 | if(yuv==YUVENCODE) {dotestyuv(srcbuf, w, h, subsamp, filename); return;} |
| 329 | |
| 330 | if((tmpbuf=(unsigned char *)malloc(pitch*h)) == NULL) |
| 331 | _throwunix("allocating temporary image buffer"); |
| 332 | |
| 333 | if(!quiet) |
| 334 | printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pixFormatStr[pf], |
| 335 | (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp], |
| 336 | jpegqual); |
| 337 | |
| 338 | for(tilew=dotile? 8:w, tileh=dotile? 8:h; ; tilew*=2, tileh*=2) |
| 339 | { |
| 340 | if(tilew>w) tilew=w; if(tileh>h) tileh=h; |
| 341 | ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh; |
| 342 | |
| 343 | if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *) |
| 344 | *ntilesw*ntilesh))==NULL) |
| 345 | _throwunix("allocating JPEG tile array"); |
| 346 | memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh); |
| 347 | if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long) |
| 348 | *ntilesw*ntilesh))==NULL) |
| 349 | _throwunix("allocating JPEG size array"); |
| 350 | memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh); |
| 351 | |
| 352 | if((flags&TJFLAG_NOREALLOC)!=0) |
| 353 | for(i=0; i<ntilesw*ntilesh; i++) |
| 354 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 355 | if((jpegbuf[i]=(unsigned char *)malloc(tjBufSize(tilew, tileh, |
| 356 | subsamp)))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 357 | _throwunix("allocating JPEG tiles"); |
| 358 | } |
| 359 | |
| 360 | /* Compression test */ |
| 361 | if(quiet==1) |
| 362 | printf("%s\t%s\t%s\t%d\t", pixFormatStr[pf], |
| 363 | (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp], jpegqual); |
| 364 | for(i=0; i<h; i++) |
| 365 | memcpy(&tmpbuf[pitch*i], &srcbuf[w*ps*i], w*ps); |
| 366 | if((handle=tjInitCompress())==NULL) |
| 367 | _throwtj("executing tjInitCompress()"); |
| 368 | |
| 369 | /* Execute once to preload cache */ |
| 370 | if(tjCompress2(handle, srcbuf, tilew, pitch, tileh, pf, &jpegbuf[0], |
| 371 | &jpegsize[0], subsamp, jpegqual, flags)==-1) |
| 372 | _throwtj("executing tjCompress2()"); |
| 373 | |
| 374 | /* Benchmark */ |
| 375 | for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++) |
| 376 | { |
| 377 | int tile=0; |
| 378 | totaljpegsize=0; |
| 379 | for(row=0, srcptr=srcbuf; row<ntilesh; row++, srcptr+=pitch*tileh) |
| 380 | { |
| 381 | for(col=0, srcptr2=srcptr; col<ntilesw; col++, tile++, |
| 382 | srcptr2+=ps*tilew) |
| 383 | { |
| 384 | int width=min(tilew, w-col*tilew); |
| 385 | int height=min(tileh, h-row*tileh); |
| 386 | if(tjCompress2(handle, srcptr2, width, pitch, height, pf, |
| 387 | &jpegbuf[tile], &jpegsize[tile], subsamp, jpegqual, flags)==-1) |
| 388 | _throwtj("executing tjCompress()2"); |
| 389 | totaljpegsize+=jpegsize[tile]; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()"); |
| 395 | handle=NULL; |
| 396 | |
| 397 | if(quiet==1) printf("%-4d %-4d\t", tilew, tileh); |
| 398 | if(quiet) |
| 399 | { |
| 400 | printf("%s%c%s%c", |
| 401 | sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024), |
| 402 | quiet==2? '\n':'\t', |
| 403 | sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80), |
| 404 | quiet==2? '\n':'\t'); |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | printf("\n%s size: %d x %d\n", dotile? "Tile":"Image", tilew, |
| 409 | tileh); |
| 410 | printf("C--> Frame rate: %f fps\n", (double)i/elapsed); |
| 411 | printf(" Output image size: %d bytes\n", totaljpegsize); |
| 412 | printf(" Compression ratio: %f:1\n", |
| 413 | (double)(w*h*ps)/(double)totaljpegsize); |
| 414 | printf(" Source throughput: %f Megapixels/sec\n", |
| 415 | (double)(w*h)/1000000.*(double)i/elapsed); |
| 416 | printf(" Output bit stream: %f Megabits/sec\n", |
| 417 | (double)totaljpegsize*8./1000000.*(double)i/elapsed); |
| 418 | } |
| 419 | if(tilew==w && tileh==h) |
| 420 | { |
| 421 | snprintf(tempstr, 1024, "%s_%s_Q%d.jpg", filename, subName[subsamp], |
| 422 | jpegqual); |
| 423 | if((file=fopen(tempstr, "wb"))==NULL) |
| 424 | _throwunix("opening reference image"); |
| 425 | if(fwrite(jpegbuf[0], jpegsize[0], 1, file)!=1) |
| 426 | _throwunix("writing reference image"); |
| 427 | fclose(file); file=NULL; |
| 428 | if(!quiet) printf("Reference image written to %s\n", tempstr); |
| 429 | } |
| 430 | |
| 431 | /* Decompression test */ |
| 432 | if(decomptest(srcbuf, jpegbuf, jpegsize, tmpbuf, w, h, subsamp, jpegqual, |
| 433 | filename, tilew, tileh)==-1) |
| 434 | goto bailout; |
| 435 | |
| 436 | for(i=0; i<ntilesw*ntilesh; i++) |
| 437 | { |
| 438 | if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL; |
| 439 | } |
| 440 | free(jpegbuf); jpegbuf=NULL; |
| 441 | free(jpegsize); jpegsize=NULL; |
| 442 | |
| 443 | if(tilew==w && tileh==h) break; |
| 444 | } |
| 445 | |
| 446 | bailout: |
| 447 | if(file) {fclose(file); file=NULL;} |
| 448 | if(jpegbuf) |
| 449 | { |
| 450 | for(i=0; i<ntilesw*ntilesh; i++) |
| 451 | { |
| 452 | if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL; |
| 453 | } |
| 454 | free(jpegbuf); jpegbuf=NULL; |
| 455 | } |
| 456 | if(jpegsize) {free(jpegsize); jpegsize=NULL;} |
| 457 | if(tmpbuf) {free(tmpbuf); tmpbuf=NULL;} |
| 458 | if(handle) {tjDestroy(handle); handle=NULL;} |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | |
| 463 | void dodecomptest(char *filename) |
| 464 | { |
| 465 | FILE *file=NULL; tjhandle handle=NULL; |
| 466 | unsigned char **jpegbuf=NULL, *srcbuf=NULL; |
| 467 | unsigned long *jpegsize=NULL, srcsize, totaljpegsize; |
| 468 | tjtransform *t=NULL; |
| 469 | int w=0, h=0, subsamp=-1, _w, _h, _tilew, _tileh, |
| 470 | _ntilesw, _ntilesh, _subsamp; |
| 471 | char *temp=NULL, tempstr[80], tempstr2[80]; |
DRC | 20c7dbc | 2011-10-18 22:06:22 +0000 | [diff] [blame] | 472 | int row, col, i, tilew, tileh, ntilesw=1, ntilesh=1, retval=0; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 473 | double start, elapsed; |
| 474 | int ps=tjPixelSize[pf], tile; |
| 475 | |
| 476 | if((file=fopen(filename, "rb"))==NULL) |
| 477 | _throwunix("opening file"); |
| 478 | if(fseek(file, 0, SEEK_END)<0 || (srcsize=ftell(file))<0) |
| 479 | _throwunix("determining file size"); |
| 480 | if((srcbuf=(unsigned char *)malloc(srcsize))==NULL) |
| 481 | _throwunix("allocating memory"); |
| 482 | if(fseek(file, 0, SEEK_SET)<0) |
| 483 | _throwunix("setting file position"); |
| 484 | if(fread(srcbuf, srcsize, 1, file)<1) |
| 485 | _throwunix("reading JPEG data"); |
| 486 | fclose(file); file=NULL; |
| 487 | |
| 488 | temp=strrchr(filename, '.'); |
| 489 | if(temp!=NULL) *temp='\0'; |
| 490 | |
| 491 | if((handle=tjInitTransform())==NULL) |
| 492 | _throwtj("executing tjInitTransform()"); |
| 493 | if(tjDecompressHeader2(handle, srcbuf, srcsize, &w, &h, &subsamp)==-1) |
| 494 | _throwtj("executing tjDecompressHeader2()"); |
| 495 | |
| 496 | if(quiet==1) |
| 497 | { |
| 498 | printf("All performance values in Mpixels/sec\n\n"); |
| 499 | printf("Bitmap\tBitmap\tJPEG\t%s %s \tXform\tComp\tDecomp\n", |
| 500 | dotile? "Tile ":"Image", dotile? "Tile ":"Image"); |
| 501 | printf("Format\tOrder\tSubsamp\tWidth Height\tPerf \tRatio\tPerf\n\n"); |
| 502 | } |
| 503 | else if(!quiet) |
| 504 | { |
| 505 | printf(">>>>> JPEG %s --> %s (%s) <<<<<\n", subNameLong[subsamp], |
| 506 | pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down"); |
| 507 | } |
| 508 | |
| 509 | for(tilew=dotile? 16:w, tileh=dotile? 16:h; ; tilew*=2, tileh*=2) |
| 510 | { |
| 511 | if(tilew>w) tilew=w; if(tileh>h) tileh=h; |
| 512 | ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh; |
| 513 | |
| 514 | if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *) |
| 515 | *ntilesw*ntilesh))==NULL) |
| 516 | _throwunix("allocating JPEG tile array"); |
| 517 | memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh); |
| 518 | if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long) |
| 519 | *ntilesw*ntilesh))==NULL) |
| 520 | _throwunix("allocating JPEG size array"); |
| 521 | memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh); |
| 522 | |
| 523 | if((flags&TJFLAG_NOREALLOC)!=0) |
| 524 | for(i=0; i<ntilesw*ntilesh; i++) |
| 525 | { |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame] | 526 | if((jpegbuf[i]=(unsigned char *)malloc(tjBufSize(tilew, tileh, |
| 527 | subsamp)))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 528 | _throwunix("allocating JPEG tiles"); |
| 529 | } |
| 530 | |
| 531 | _w=w; _h=h; _tilew=tilew; _tileh=tileh; |
| 532 | if(!quiet) |
| 533 | { |
| 534 | printf("\n%s size: %d x %d", dotile? "Tile":"Image", _tilew, |
| 535 | _tileh); |
| 536 | if(sf.num!=1 || sf.denom!=1) |
| 537 | printf(" --> %d x %d", TJSCALED(_w, sf), TJSCALED(_h, sf)); |
| 538 | printf("\n"); |
| 539 | } |
| 540 | else if(quiet==1) |
| 541 | { |
| 542 | printf("%s\t%s\t%s\t", pixFormatStr[pf], |
| 543 | (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp]); |
| 544 | printf("%-4d %-4d\t", tilew, tileh); |
| 545 | } |
| 546 | |
| 547 | _subsamp=subsamp; |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 548 | if(dotile || xformop!=TJXOP_NONE || xformopt!=0 || customFilter) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 549 | { |
| 550 | if((t=(tjtransform *)malloc(sizeof(tjtransform)*ntilesw*ntilesh)) |
| 551 | ==NULL) |
| 552 | _throwunix("allocating image transform array"); |
| 553 | |
| 554 | if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE |
| 555 | || xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270) |
| 556 | { |
| 557 | _w=h; _h=w; _tilew=tileh; _tileh=tilew; |
| 558 | } |
| 559 | |
| 560 | if(xformopt&TJXOPT_GRAY) _subsamp=TJ_GRAYSCALE; |
| 561 | if(xformop==TJXOP_HFLIP || xformop==TJXOP_ROT180) |
| 562 | _w=_w-(_w%tjMCUWidth[_subsamp]); |
| 563 | if(xformop==TJXOP_VFLIP || xformop==TJXOP_ROT180) |
| 564 | _h=_h-(_h%tjMCUHeight[_subsamp]); |
| 565 | if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT90) |
| 566 | _w=_w-(_w%tjMCUHeight[_subsamp]); |
| 567 | if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT270) |
| 568 | _h=_h-(_h%tjMCUWidth[_subsamp]); |
| 569 | _ntilesw=(_w+_tilew-1)/_tilew; |
| 570 | _ntilesh=(_h+_tileh-1)/_tileh; |
| 571 | |
| 572 | for(row=0, tile=0; row<_ntilesh; row++) |
| 573 | { |
| 574 | for(col=0; col<_ntilesw; col++, tile++) |
| 575 | { |
| 576 | t[tile].r.w=min(_tilew, _w-col*_tilew); |
| 577 | t[tile].r.h=min(_tileh, _h-row*_tileh); |
| 578 | t[tile].r.x=col*_tilew; |
| 579 | t[tile].r.y=row*_tileh; |
| 580 | t[tile].op=xformop; |
| 581 | t[tile].options=xformopt|TJXOPT_TRIM; |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 582 | t[tile].customFilter=customFilter; |
| 583 | if(t[tile].options&TJXOPT_NOOUTPUT && jpegbuf[tile]) |
| 584 | { |
| 585 | free(jpegbuf[tile]); jpegbuf[tile]=NULL; |
| 586 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | start=gettime(); |
| 591 | if(tjTransform(handle, srcbuf, srcsize, _ntilesw*_ntilesh, jpegbuf, |
| 592 | jpegsize, t, flags)==-1) |
| 593 | _throwtj("executing tjTransform()"); |
| 594 | elapsed=gettime()-start; |
| 595 | |
| 596 | free(t); t=NULL; |
| 597 | |
| 598 | for(tile=0, totaljpegsize=0; tile<_ntilesw*_ntilesh; tile++) |
| 599 | totaljpegsize+=jpegsize[tile]; |
| 600 | |
| 601 | if(quiet) |
| 602 | { |
| 603 | printf("%s%c%s%c", |
| 604 | sigfig((double)(w*h)/1000000./elapsed, 4, tempstr, 80), |
| 605 | quiet==2? '\n':'\t', |
| 606 | sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80), |
| 607 | quiet==2? '\n':'\t'); |
| 608 | } |
| 609 | else if(!quiet) |
| 610 | { |
| 611 | printf("X--> Frame rate: %f fps\n", 1.0/elapsed); |
| 612 | printf(" Output image size: %lu bytes\n", totaljpegsize); |
| 613 | printf(" Compression ratio: %f:1\n", |
| 614 | (double)(w*h*ps)/(double)totaljpegsize); |
| 615 | printf(" Source throughput: %f Megapixels/sec\n", |
| 616 | (double)(w*h)/1000000./elapsed); |
| 617 | printf(" Output bit stream: %f Megabits/sec\n", |
| 618 | (double)totaljpegsize*8./1000000./elapsed); |
| 619 | } |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | if(quiet==1) printf("N/A\tN/A\t"); |
| 624 | jpegsize[0]=srcsize; |
| 625 | memcpy(jpegbuf[0], srcbuf, srcsize); |
| 626 | } |
| 627 | |
| 628 | if(w==tilew) _tilew=_w; |
| 629 | if(h==tileh) _tileh=_h; |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 630 | if(!(xformopt&TJXOPT_NOOUTPUT)) |
| 631 | { |
| 632 | if(decomptest(NULL, jpegbuf, jpegsize, NULL, _w, _h, _subsamp, 0, |
| 633 | filename, _tilew, _tileh)==-1) |
| 634 | goto bailout; |
| 635 | } |
| 636 | else if(quiet==1) printf("N/A\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 637 | |
| 638 | for(i=0; i<ntilesw*ntilesh; i++) |
| 639 | { |
| 640 | free(jpegbuf[i]); jpegbuf[i]=NULL; |
| 641 | } |
| 642 | free(jpegbuf); jpegbuf=NULL; |
| 643 | if(jpegsize) {free(jpegsize); jpegsize=NULL;} |
| 644 | |
| 645 | if(tilew==w && tileh==h) break; |
| 646 | } |
| 647 | |
| 648 | bailout: |
| 649 | if(file) {fclose(file); file=NULL;} |
| 650 | if(jpegbuf) |
| 651 | { |
| 652 | for(i=0; i<ntilesw*ntilesh; i++) |
| 653 | { |
| 654 | if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL; |
| 655 | } |
| 656 | free(jpegbuf); jpegbuf=NULL; |
| 657 | } |
| 658 | if(jpegsize) {free(jpegsize); jpegsize=NULL;} |
| 659 | if(srcbuf) {free(srcbuf); srcbuf=NULL;} |
| 660 | if(t) {free(t); t=NULL;} |
| 661 | if(handle) {tjDestroy(handle); handle=NULL;} |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | void usage(char *progname) |
| 667 | { |
| 668 | int i; |
| 669 | printf("USAGE: %s\n", progname); |
DRC | fd3aba3 | 2012-06-29 23:14:48 +0000 | [diff] [blame] | 670 | printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 671 | printf(" %s\n", progname); |
| 672 | printf(" <Inputfile (JPG)> [options]\n\n"); |
| 673 | printf("Options:\n\n"); |
| 674 | printf("-alloc = Dynamically allocate JPEG image buffers\n"); |
| 675 | printf("-bmp = Generate output images in Windows Bitmap format (default=PPM)\n"); |
| 676 | printf("-bottomup = Test bottom-up compression/decompression\n"); |
| 677 | printf("-tile = Test performance of the codec when the image is encoded as separate\n"); |
| 678 | printf(" tiles of varying sizes.\n"); |
| 679 | printf("-forcemmx, -forcesse, -forcesse2, -forcesse3 =\n"); |
| 680 | printf(" Force MMX, SSE, SSE2, or SSE3 code paths in the underlying codec\n"); |
| 681 | printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n"); |
| 682 | printf(" Test the specified color conversion path in the codec (default: BGR)\n"); |
DRC | cc336e7 | 2012-06-29 23:51:35 +0000 | [diff] [blame] | 683 | printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n"); |
| 684 | printf(" the underlying codec\n"); |
DRC | fd3aba3 | 2012-06-29 23:14:48 +0000 | [diff] [blame] | 685 | printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n"); |
| 686 | printf(" codec\n"); |
| 687 | printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n"); |
| 688 | printf(" underlying codec\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 689 | printf("-quiet = Output results in tabular rather than verbose format\n"); |
| 690 | printf("-yuvencode = Encode RGB input as planar YUV rather than compressing as JPEG\n"); |
| 691 | printf("-yuvdecode = Decode JPEG image to planar YUV rather than RGB\n"); |
| 692 | printf("-scale M/N = scale down the width/height of the decompressed JPEG image by a\n"); |
| 693 | printf(" factor of M/N (M/N = "); |
| 694 | for(i=0; i<nsf; i++) |
| 695 | { |
| 696 | printf("%d/%d", scalingfactors[i].num, scalingfactors[i].denom); |
| 697 | if(nsf==2 && i!=nsf-1) printf(" or "); |
| 698 | else if(nsf>2) |
| 699 | { |
| 700 | if(i!=nsf-1) printf(", "); |
| 701 | if(i==nsf-2) printf("or "); |
| 702 | } |
| 703 | } |
| 704 | printf(")\n"); |
| 705 | printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n"); |
| 706 | printf(" Perform the corresponding lossless transform prior to\n"); |
| 707 | printf(" decompression (these options are mutually exclusive)\n"); |
| 708 | printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n"); |
| 709 | printf(" test (can be combined with the other transforms above)\n"); |
| 710 | printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n\n"); |
| 711 | printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n"); |
| 712 | printf("test will be performed for all quality values in the range.\n\n"); |
| 713 | exit(1); |
| 714 | } |
| 715 | |
| 716 | |
| 717 | int main(int argc, char *argv[]) |
| 718 | { |
| 719 | unsigned char *srcbuf=NULL; int w, h, i, j; |
| 720 | int minqual=-1, maxqual=-1; char *temp; |
| 721 | int minarg=2; int retval=0; |
| 722 | |
| 723 | if((scalingfactors=tjGetScalingFactors(&nsf))==NULL || nsf==0) |
| 724 | _throwtj("executing tjGetScalingFactors()"); |
| 725 | |
| 726 | if(argc<minarg) usage(argv[0]); |
| 727 | |
| 728 | temp=strrchr(argv[1], '.'); |
| 729 | if(temp!=NULL) |
| 730 | { |
| 731 | if(!strcasecmp(temp, ".bmp")) ext="bmp"; |
| 732 | if(!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg")) decomponly=1; |
| 733 | } |
| 734 | |
| 735 | printf("\n"); |
| 736 | |
| 737 | if(argc>minarg) |
| 738 | { |
| 739 | for(i=minarg; i<argc; i++) |
| 740 | { |
| 741 | if(!strcasecmp(argv[i], "-yuvencode")) |
| 742 | { |
| 743 | printf("Testing YUV planar encoding\n\n"); |
| 744 | yuv=YUVENCODE; maxqual=minqual=100; |
| 745 | } |
| 746 | if(!strcasecmp(argv[i], "-yuvdecode")) |
| 747 | { |
| 748 | printf("Testing YUV planar decoding\n\n"); |
| 749 | yuv=YUVDECODE; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | if(!decomponly && yuv!=YUVENCODE) |
| 755 | { |
| 756 | minarg=3; |
| 757 | if(argc<minarg) usage(argv[0]); |
| 758 | if((minqual=atoi(argv[2]))<1 || minqual>100) |
| 759 | { |
| 760 | puts("ERROR: Quality must be between 1 and 100."); |
| 761 | exit(1); |
| 762 | } |
| 763 | if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1 |
| 764 | && sscanf(&temp[1], "%d", &maxqual)==1 && maxqual>minqual && maxqual>=1 |
| 765 | && maxqual<=100) {} |
| 766 | else maxqual=minqual; |
| 767 | } |
| 768 | |
| 769 | if(argc>minarg) |
| 770 | { |
| 771 | for(i=minarg; i<argc; i++) |
| 772 | { |
| 773 | if(!strcasecmp(argv[i], "-tile")) |
| 774 | { |
| 775 | dotile=1; xformopt|=TJXOPT_CROP; |
| 776 | } |
| 777 | if(!strcasecmp(argv[i], "-forcesse3")) |
| 778 | { |
| 779 | printf("Forcing SSE3 code\n\n"); |
| 780 | flags|=TJFLAG_FORCESSE3; |
| 781 | } |
| 782 | if(!strcasecmp(argv[i], "-forcesse2")) |
| 783 | { |
| 784 | printf("Forcing SSE2 code\n\n"); |
| 785 | flags|=TJFLAG_FORCESSE2; |
| 786 | } |
| 787 | if(!strcasecmp(argv[i], "-forcesse")) |
| 788 | { |
| 789 | printf("Forcing SSE code\n\n"); |
| 790 | flags|=TJFLAG_FORCESSE; |
| 791 | } |
| 792 | if(!strcasecmp(argv[i], "-forcemmx")) |
| 793 | { |
| 794 | printf("Forcing MMX code\n\n"); |
| 795 | flags|=TJFLAG_FORCEMMX; |
| 796 | } |
| 797 | if(!strcasecmp(argv[i], "-fastupsample")) |
| 798 | { |
| 799 | printf("Using fast upsampling code\n\n"); |
| 800 | flags|=TJFLAG_FASTUPSAMPLE; |
| 801 | } |
DRC | fd3aba3 | 2012-06-29 23:14:48 +0000 | [diff] [blame] | 802 | if(!strcasecmp(argv[i], "-fastdct")) |
| 803 | { |
| 804 | printf("Using fastest DCT/IDCT algorithm\n\n"); |
| 805 | flags|=TJFLAG_FASTDCT; |
| 806 | } |
| 807 | if(!strcasecmp(argv[i], "-accuratedct")) |
| 808 | { |
| 809 | printf("Using most accurate DCT/IDCT algorithm\n\n"); |
| 810 | flags|=TJFLAG_ACCURATEDCT; |
| 811 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 812 | if(!strcasecmp(argv[i], "-rgb")) pf=TJPF_RGB; |
| 813 | if(!strcasecmp(argv[i], "-rgbx")) pf=TJPF_RGBX; |
| 814 | if(!strcasecmp(argv[i], "-bgr")) pf=TJPF_BGR; |
| 815 | if(!strcasecmp(argv[i], "-bgrx")) pf=TJPF_BGRX; |
| 816 | if(!strcasecmp(argv[i], "-xbgr")) pf=TJPF_XBGR; |
| 817 | if(!strcasecmp(argv[i], "-xrgb")) pf=TJPF_XRGB; |
| 818 | if(!strcasecmp(argv[i], "-bottomup")) flags|=TJFLAG_BOTTOMUP; |
| 819 | if(!strcasecmp(argv[i], "-quiet")) quiet=1; |
| 820 | if(!strcasecmp(argv[i], "-qq")) quiet=2; |
| 821 | if(!strcasecmp(argv[i], "-scale") && i<argc-1) |
| 822 | { |
| 823 | int temp1=0, temp2=0, match=0; |
| 824 | if(sscanf(argv[++i], "%d/%d", &temp1, &temp2)==2) |
| 825 | { |
| 826 | for(j=0; j<nsf; j++) |
| 827 | { |
| 828 | if(temp1==scalingfactors[j].num && temp2==scalingfactors[j].denom) |
| 829 | { |
| 830 | sf=scalingfactors[j]; |
| 831 | match=1; break; |
| 832 | } |
| 833 | } |
| 834 | if(!match) usage(argv[0]); |
| 835 | } |
| 836 | else usage(argv[0]); |
| 837 | } |
| 838 | if(!strcasecmp(argv[i], "-hflip")) xformop=TJXOP_HFLIP; |
| 839 | if(!strcasecmp(argv[i], "-vflip")) xformop=TJXOP_VFLIP; |
| 840 | if(!strcasecmp(argv[i], "-transpose")) xformop=TJXOP_TRANSPOSE; |
| 841 | if(!strcasecmp(argv[i], "-transverse")) xformop=TJXOP_TRANSVERSE; |
| 842 | if(!strcasecmp(argv[i], "-rot90")) xformop=TJXOP_ROT90; |
| 843 | if(!strcasecmp(argv[i], "-rot180")) xformop=TJXOP_ROT180; |
| 844 | if(!strcasecmp(argv[i], "-rot270")) xformop=TJXOP_ROT270; |
| 845 | if(!strcasecmp(argv[i], "-grayscale")) xformopt|=TJXOPT_GRAY; |
DRC | 7bf04d3 | 2011-09-17 00:18:31 +0000 | [diff] [blame] | 846 | if(!strcasecmp(argv[i], "-custom")) customFilter=dummyDCTFilter; |
| 847 | if(!strcasecmp(argv[i], "-nooutput")) xformopt|=TJXOPT_NOOUTPUT; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 848 | if(!strcasecmp(argv[i], "-benchtime") && i<argc-1) |
| 849 | { |
| 850 | double temp=atof(argv[++i]); |
| 851 | if(temp>0.0) benchtime=temp; |
| 852 | else usage(argv[0]); |
| 853 | } |
| 854 | if(!strcmp(argv[i], "-?")) usage(argv[0]); |
| 855 | if(!strcasecmp(argv[i], "-alloc")) flags&=(~TJFLAG_NOREALLOC); |
DRC | 94f0e03 | 2011-05-25 04:35:53 +0000 | [diff] [blame] | 856 | if(!strcasecmp(argv[i], "-bmp")) ext="bmp"; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | if((sf.num!=1 || sf.denom!=1) && dotile) |
| 861 | { |
| 862 | printf("Disabling tiled compression/decompression tests, because those tests do not\n"); |
| 863 | printf("work when scaled decompression is enabled.\n"); |
| 864 | dotile=0; |
| 865 | } |
| 866 | |
| 867 | if(yuv && dotile) |
| 868 | { |
| 869 | printf("Disabling tiled compression/decompression tests, because those tests do not\n"); |
| 870 | printf("work when YUV encoding or decoding is enabled.\n\n"); |
| 871 | dotile=0; |
| 872 | } |
| 873 | |
| 874 | if(!decomponly) |
| 875 | { |
| 876 | if(loadbmp(argv[1], &srcbuf, &w, &h, pf, (flags&TJFLAG_BOTTOMUP)!=0)==-1) |
| 877 | _throwbmp("loading bitmap"); |
| 878 | temp=strrchr(argv[1], '.'); |
| 879 | if(temp!=NULL) *temp='\0'; |
| 880 | } |
| 881 | |
| 882 | if(quiet==1 && !decomponly) |
| 883 | { |
| 884 | printf("All performance values in Mpixels/sec\n\n"); |
| 885 | printf("Bitmap\tBitmap\tJPEG\tJPEG\t%s %s \tComp\tComp\tDecomp\n", |
| 886 | dotile? "Tile ":"Image", dotile? "Tile ":"Image"); |
| 887 | printf("Format\tOrder\tSubsamp\tQual\tWidth Height\tPerf \tRatio\tPerf\n\n"); |
| 888 | } |
| 889 | |
| 890 | if(decomponly) |
| 891 | { |
| 892 | dodecomptest(argv[1]); |
| 893 | printf("\n"); |
| 894 | goto bailout; |
| 895 | } |
| 896 | for(i=maxqual; i>=minqual; i--) |
| 897 | dotest(srcbuf, w, h, TJ_GRAYSCALE, i, argv[1]); |
| 898 | printf("\n"); |
| 899 | for(i=maxqual; i>=minqual; i--) |
| 900 | dotest(srcbuf, w, h, TJ_420, i, argv[1]); |
| 901 | printf("\n"); |
| 902 | for(i=maxqual; i>=minqual; i--) |
| 903 | dotest(srcbuf, w, h, TJ_422, i, argv[1]); |
| 904 | printf("\n"); |
| 905 | for(i=maxqual; i>=minqual; i--) |
| 906 | dotest(srcbuf, w, h, TJ_444, i, argv[1]); |
| 907 | printf("\n"); |
| 908 | |
| 909 | bailout: |
| 910 | if(srcbuf) free(srcbuf); |
| 911 | return retval; |
| 912 | } |