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