DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1 | /* Copyright (C)2004 Landmark Graphics Corporation |
| 2 | * Copyright (C)2005, 2006 Sun Microsystems, Inc. |
DRC | 2e4d044 | 2011-02-08 01:18:37 +0000 | [diff] [blame] | 3 | * Copyright (C)2009-2011 D. R. Commander |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 4 | * |
| 5 | * This library is free software and may be redistributed and/or modified under |
| 6 | * the terms of the wxWindows Library License, Version 3.1 or (at your option) |
| 7 | * any later version. The full license is in the LICENSE.txt file included |
| 8 | * with this distribution. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * wxWindows Library License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <math.h> |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 20 | #include <errno.h> |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 21 | #include "./bmp.h" |
| 22 | #include "./rrutil.h" |
| 23 | #include "./rrtimer.h" |
| 24 | #include "./turbojpeg.h" |
| 25 | |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 26 | #define _throw(op, err) { \ |
| 27 | printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); goto bailout;} |
| 28 | #define _throwunix(m) _throw(m, strerror(errno)) |
| 29 | #define _throwtj(m) _throw(m, tjGetErrorStr()) |
| 30 | #define _throwbmp(m) _throw(m, bmpgeterr()) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 31 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 32 | #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) |
| 33 | |
| 34 | enum {YUVENCODE=1, YUVDECODE}; |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 35 | int forcemmx=0, forcesse=0, forcesse2=0, forcesse3=0, fastupsample=0, |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 36 | decomponly=0, yuv=0, quiet=0, dotile=0, pf=BMP_BGR, bu=0, useppm=0; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 37 | const int _ps[BMPPIXELFORMATS]={3, 4, 3, 4, 4, 4}; |
| 38 | const int _flags[BMPPIXELFORMATS]={0, 0, TJ_BGR, TJ_BGR, |
| 39 | TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST}; |
| 40 | const int _rindex[BMPPIXELFORMATS]={0, 0, 2, 2, 3, 1}; |
| 41 | const int _gindex[BMPPIXELFORMATS]={1, 1, 1, 1, 2, 2}; |
| 42 | const int _bindex[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3}; |
| 43 | const char *_pfname[]={"RGB", "RGBA", "BGR", "BGRA", "ABGR", "ARGB"}; |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 44 | const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY"}; |
| 45 | const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY"}; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 46 | const int _hsf[NUMSUBOPT]={1, 2, 2, 1}; |
| 47 | const int _vsf[NUMSUBOPT]={1, 1, 2, 1}; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 48 | |
| 49 | void printsigfig(double val, int figs) |
| 50 | { |
| 51 | char format[80]; |
| 52 | double _l=log10(val); int l; |
| 53 | if(_l<0.) |
| 54 | { |
| 55 | l=(int)fabs(_l); |
| 56 | sprintf(format, "%%%d.%df", figs+l+2, figs+l); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | l=(int)_l+1; |
| 61 | if(figs<=l) sprintf(format, "%%.0f"); |
| 62 | else sprintf(format, "%%%d.%df", figs+1, figs-l); |
| 63 | } |
| 64 | printf(format, val); |
| 65 | } |
| 66 | |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 67 | // Decompression test |
| 68 | int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf, |
| 69 | unsigned long *comptilesize, unsigned char *rgbbuf, int w, int h, |
| 70 | int jpegsub, int qual, char *filename, int tilesizex, int tilesizey) |
| 71 | { |
| 72 | char tempstr[1024], qualstr[5]="\0"; |
| 73 | FILE *outfile=NULL; tjhandle hnd=NULL; |
| 74 | int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0) |
| 75 | |(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0) |
| 76 | |(fastupsample?TJ_FASTUPSAMPLE:0); |
| 77 | int i, j, ITER, rgbbufalloc=0; |
| 78 | double start, elapsed; |
| 79 | int ps=_ps[pf]; |
| 80 | int pitch=w*ps; |
| 81 | int hsf=_hsf[jpegsub], vsf=_vsf[jpegsub]; |
| 82 | int pw=PAD(w, hsf), ph=PAD(h, vsf); |
| 83 | int cw=pw/hsf, ch=ph/vsf; |
| 84 | int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4); |
| 85 | int yuvsize=ypitch*ph + (jpegsub==TJ_GRAYSCALE? 0:uvpitch*ch*2); |
| 86 | |
| 87 | if(qual>0) |
| 88 | { |
| 89 | snprintf(qualstr, 5, "Q%d", qual); |
| 90 | qualstr[4]=0; |
| 91 | } |
| 92 | |
| 93 | flags |= _flags[pf]; |
| 94 | if(bu) flags |= TJ_BOTTOMUP; |
| 95 | if(yuv==YUVDECODE) flags |= TJ_YUV; |
| 96 | if((hnd=tjInitDecompress())==NULL) |
| 97 | _throwtj("executing tjInitDecompress()"); |
| 98 | |
| 99 | if(rgbbuf==NULL) |
| 100 | { |
| 101 | if((rgbbuf=(unsigned char *)malloc(max(yuvsize, pitch*h))) == NULL) |
| 102 | _throwunix("allocating image buffer"); |
| 103 | rgbbufalloc=1; |
| 104 | } |
| 105 | memset(rgbbuf, 127, max(yuvsize, pitch*h)); // Grey image means decompressor did nothing |
| 106 | |
| 107 | if(tjDecompress(hnd, jpegbuf[0], comptilesize[0], rgbbuf, tilesizex, pitch, |
| 108 | tilesizey, ps, flags)==-1) |
| 109 | _throwtj("executing tjDecompress()"); |
| 110 | ITER=0; |
| 111 | start=rrtime(); |
| 112 | do |
| 113 | { |
| 114 | int tilen=0; |
| 115 | for(i=0; i<h; i+=tilesizey) |
| 116 | { |
| 117 | for(j=0; j<w; j+=tilesizex) |
| 118 | { |
| 119 | int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i); |
| 120 | if(tjDecompress(hnd, jpegbuf[tilen], comptilesize[tilen], |
| 121 | &rgbbuf[pitch*i+ps*j], tempw, pitch, temph, ps, flags)==-1) |
| 122 | _throwtj("executing tjDecompress()"); |
| 123 | tilen++; |
| 124 | } |
| 125 | } |
| 126 | ITER++; |
| 127 | } while((elapsed=rrtime()-start)<5.); |
| 128 | if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()"); |
| 129 | hnd=NULL; |
| 130 | if(quiet) |
| 131 | { |
| 132 | printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4); |
| 133 | printf("\n"); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | printf("D--> Frame rate: %f fps\n", (double)ITER/elapsed); |
| 138 | printf(" Dest. throughput: %f Megapixels/sec\n", |
| 139 | (double)(w*h)/1000000.*(double)ITER/elapsed); |
| 140 | } |
| 141 | if(yuv==YUVDECODE) |
| 142 | { |
| 143 | sprintf(tempstr, "%s_%s%s.yuv", filename, _subnames[jpegsub], qualstr); |
| 144 | if((outfile=fopen(tempstr, "wb"))==NULL) |
| 145 | _throwunix("opening YUV image for output"); |
| 146 | if(fwrite(rgbbuf, yuvsize, 1, outfile)!=1) |
| 147 | _throwunix("writing YUV image"); |
| 148 | fclose(outfile); outfile=NULL; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | if(tilesizex==w && tilesizey==h) |
DRC | bc507f6 | 2011-02-16 01:23:48 +0000 | [diff] [blame^] | 153 | { |
| 154 | if(decomponly) |
| 155 | sprintf(tempstr, "%s_full.%s", filename, useppm?"ppm":"bmp"); |
| 156 | else |
| 157 | sprintf(tempstr, "%s_%s%s_full.%s", filename, _subnames[jpegsub], |
| 158 | qualstr, useppm?"ppm":"bmp"); |
| 159 | } |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 160 | else sprintf(tempstr, "%s_%s%s_%dx%d.%s", filename, _subnames[jpegsub], |
| 161 | qualstr, tilesizex, tilesizey, useppm?"ppm":"bmp"); |
| 162 | if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1) |
| 163 | _throwbmp("saving bitmap"); |
| 164 | sprintf(strrchr(tempstr, '.'), "-err.%s", useppm?"ppm":"bmp"); |
| 165 | if(srcbuf) |
| 166 | { |
| 167 | if(!quiet) |
| 168 | printf("Computing compression error and saving to %s.\n", tempstr); |
| 169 | if(jpegsub==TJ_GRAYSCALE) |
| 170 | { |
| 171 | for(j=0; j<h; j++) |
| 172 | { |
| 173 | for(i=0; i<w*ps; i+=ps) |
| 174 | { |
| 175 | int y=(int)((double)srcbuf[w*ps*j+i+_rindex[pf]]*0.299 |
| 176 | + (double)srcbuf[w*ps*j+i+_gindex[pf]]*0.587 |
| 177 | + (double)srcbuf[w*ps*j+i+_bindex[pf]]*0.114 + 0.5); |
| 178 | if(y>255) y=255; if(y<0) y=0; |
| 179 | rgbbuf[pitch*j+i+_rindex[pf]]=abs(rgbbuf[pitch*j+i+_rindex[pf]]-y); |
| 180 | rgbbuf[pitch*j+i+_gindex[pf]]=abs(rgbbuf[pitch*j+i+_gindex[pf]]-y); |
| 181 | rgbbuf[pitch*j+i+_bindex[pf]]=abs(rgbbuf[pitch*j+i+_bindex[pf]]-y); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | for(j=0; j<h; j++) for(i=0; i<w*ps; i++) |
| 188 | rgbbuf[pitch*j+i]=abs(rgbbuf[pitch*j+i]-srcbuf[w*ps*j+i]); |
| 189 | } |
| 190 | if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1) |
| 191 | _throwbmp("saving bitmap"); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if(hnd) {tjDestroy(hnd); hnd=NULL;} |
| 196 | if(rgbbuf && rgbbufalloc) {free(rgbbuf); rgbbuf=NULL;} |
| 197 | return 0; |
| 198 | |
| 199 | bailout: |
| 200 | if(outfile) {fclose(outfile); outfile=NULL;} |
| 201 | if(hnd) {tjDestroy(hnd); hnd=NULL;} |
| 202 | if(rgbbuf && rgbbufalloc) {free(rgbbuf); rgbbuf=NULL;} |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | void dotest(unsigned char *srcbuf, int w, int h, int jpegsub, int qual, |
| 207 | char *filename) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 208 | { |
| 209 | char tempstr[1024]; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 210 | FILE *outfile=NULL; tjhandle hnd; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 211 | unsigned char **jpegbuf=NULL, *rgbbuf=NULL; |
DRC | 2e4d044 | 2011-02-08 01:18:37 +0000 | [diff] [blame] | 212 | double start, elapsed; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 213 | int jpgbufsize=0, i, j, tilesizex, tilesizey, numtilesx, numtilesy, ITER; |
| 214 | unsigned long *comptilesize=NULL; |
| 215 | int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0) |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 216 | |(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0) |
| 217 | |(fastupsample?TJ_FASTUPSAMPLE:0); |
DRC | 2e4d044 | 2011-02-08 01:18:37 +0000 | [diff] [blame] | 218 | int ps=_ps[pf], tilen; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 219 | int pitch=w*ps, yuvsize; |
| 220 | int hsf=_hsf[jpegsub], vsf=_vsf[jpegsub]; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 221 | int pw=PAD(w, hsf), ph=PAD(h, vsf); |
| 222 | int cw=pw/hsf, ch=ph/vsf; |
| 223 | int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 224 | |
| 225 | flags |= _flags[pf]; |
| 226 | if(bu) flags |= TJ_BOTTOMUP; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 227 | if(yuv==YUVENCODE) flags |= TJ_YUV; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 228 | |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 229 | yuvsize=ypitch*ph + (jpegsub==TJ_GRAYSCALE? 0:uvpitch*ch*2); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 230 | if((rgbbuf=(unsigned char *)malloc(max(yuvsize, pitch*h))) == NULL) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 231 | _throwunix("allocating image buffer"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 232 | |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 233 | if(!quiet) |
| 234 | { |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 235 | if(yuv==YUVENCODE) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 236 | printf("\n>>>>> %s (%s) <--> YUV %s <<<<<\n", _pfname[pf], |
| 237 | bu?"Bottom-up":"Top-down", _subnamel[jpegsub]); |
| 238 | else |
| 239 | printf("\n>>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", _pfname[pf], |
| 240 | bu?"Bottom-up":"Top-down", _subnamel[jpegsub], qual); |
| 241 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 242 | if(yuv==YUVDECODE) dotile=0; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 243 | if(dotile) {tilesizex=tilesizey=4;} else {tilesizex=w; tilesizey=h;} |
| 244 | |
| 245 | do |
| 246 | { |
| 247 | tilesizex*=2; if(tilesizex>w) tilesizex=w; |
| 248 | tilesizey*=2; if(tilesizey>h) tilesizey=h; |
| 249 | numtilesx=(w+tilesizex-1)/tilesizex; |
| 250 | numtilesy=(h+tilesizey-1)/tilesizey; |
| 251 | if((comptilesize=(unsigned long *)malloc(sizeof(unsigned long)*numtilesx*numtilesy)) == NULL |
| 252 | || (jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)*numtilesx*numtilesy)) == NULL) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 253 | _throwunix("allocating image buffers"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 254 | memset(jpegbuf, 0, sizeof(unsigned char *)*numtilesx*numtilesy); |
| 255 | for(i=0; i<numtilesx*numtilesy; i++) |
| 256 | { |
| 257 | if((jpegbuf[i]=(unsigned char *)malloc(TJBUFSIZE(tilesizex, tilesizey))) == NULL) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 258 | _throwunix("allocating image buffers"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Compression test |
| 262 | if(quiet) printf("%s\t%s\t%s\t%d\t", _pfname[pf], bu?"BU":"TD", |
| 263 | _subnamel[jpegsub], qual); |
| 264 | for(i=0; i<h; i++) memcpy(&rgbbuf[pitch*i], &srcbuf[w*ps*i], w*ps); |
| 265 | if((hnd=tjInitCompress())==NULL) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 266 | _throwtj("executing tjInitCompress()"); |
| 267 | if(tjCompress(hnd, rgbbuf, tilesizex, pitch, tilesizey, ps, |
| 268 | jpegbuf[0], &comptilesize[0], jpegsub, qual, flags)==-1) |
| 269 | _throwtj("executing tjCompress()"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 270 | ITER=0; |
DRC | 2e4d044 | 2011-02-08 01:18:37 +0000 | [diff] [blame] | 271 | start=rrtime(); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 272 | do |
| 273 | { |
DRC | 2e4d044 | 2011-02-08 01:18:37 +0000 | [diff] [blame] | 274 | jpgbufsize=0; tilen=0; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 275 | for(i=0; i<h; i+=tilesizey) |
| 276 | { |
| 277 | for(j=0; j<w; j+=tilesizex) |
| 278 | { |
| 279 | int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i); |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 280 | if(tjCompress(hnd, &rgbbuf[pitch*i+j*ps], tempw, pitch, |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 281 | temph, ps, jpegbuf[tilen], &comptilesize[tilen], jpegsub, qual, |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 282 | flags)==-1) |
| 283 | _throwtj("executing tjCompress()"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 284 | jpgbufsize+=comptilesize[tilen]; |
| 285 | tilen++; |
| 286 | } |
| 287 | } |
| 288 | ITER++; |
DRC | 2e4d044 | 2011-02-08 01:18:37 +0000 | [diff] [blame] | 289 | } while((elapsed=rrtime()-start)<5.); |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 290 | if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()"); |
| 291 | hnd=NULL; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 292 | if(quiet) |
| 293 | { |
| 294 | if(tilesizex==w && tilesizey==h) printf("Full \t"); |
| 295 | else printf("%-4d %-4d\t", tilesizex, tilesizey); |
| 296 | printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4); |
| 297 | printf("\t"); |
| 298 | printsigfig((double)(w*h*ps)/(double)jpgbufsize, 4); |
| 299 | printf("\t"); |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | if(tilesizex==w && tilesizey==h) printf("\nFull image\n"); |
| 304 | else printf("\nTile size: %d x %d\n", tilesizex, tilesizey); |
| 305 | printf("C--> Frame rate: %f fps\n", (double)ITER/elapsed); |
| 306 | printf(" Output image size: %d bytes\n", jpgbufsize); |
| 307 | printf(" Compression ratio: %f:1\n", |
| 308 | (double)(w*h*ps)/(double)jpgbufsize); |
| 309 | printf(" Source throughput: %f Megapixels/sec\n", |
| 310 | (double)(w*h)/1000000.*(double)ITER/elapsed); |
| 311 | printf(" Output bit stream: %f Megabits/sec\n", |
| 312 | (double)jpgbufsize*8./1000000.*(double)ITER/elapsed); |
| 313 | } |
| 314 | if(tilesizex==w && tilesizey==h) |
| 315 | { |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 316 | if(yuv==YUVENCODE) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 317 | sprintf(tempstr, "%s_%s.yuv", filename, _subnames[jpegsub]); |
| 318 | else |
| 319 | sprintf(tempstr, "%s_%sQ%d.jpg", filename, _subnames[jpegsub], qual); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 320 | if((outfile=fopen(tempstr, "wb"))==NULL) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 321 | _throwunix("opening reference image"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 322 | if(fwrite(jpegbuf[0], jpgbufsize, 1, outfile)!=1) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 323 | _throwunix("writing reference image"); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 324 | fclose(outfile); outfile=NULL; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 325 | if(!quiet) printf("Reference image written to %s\n", tempstr); |
| 326 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 327 | if(yuv==YUVENCODE) goto bailout; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 328 | |
| 329 | // Decompression test |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 330 | if(decomptest(srcbuf, jpegbuf, comptilesize, rgbbuf, w, h, jpegsub, qual, |
| 331 | filename, tilesizex, tilesizey)==-1) |
| 332 | goto bailout; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 333 | |
| 334 | // Cleanup |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 335 | if(outfile) {fclose(outfile); outfile=NULL;} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 336 | if(jpegbuf) |
| 337 | { |
| 338 | for(i=0; i<numtilesx*numtilesy; i++) |
| 339 | {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;} |
| 340 | free(jpegbuf); jpegbuf=NULL; |
| 341 | } |
| 342 | if(comptilesize) {free(comptilesize); comptilesize=NULL;} |
| 343 | } while(tilesizex<w || tilesizey<h); |
| 344 | |
| 345 | if(rgbbuf) {free(rgbbuf); rgbbuf=NULL;} |
| 346 | return; |
| 347 | |
| 348 | bailout: |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 349 | if(outfile) {fclose(outfile); outfile=NULL;} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 350 | if(jpegbuf) |
| 351 | { |
| 352 | for(i=0; i<numtilesx*numtilesy; i++) |
| 353 | {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;} |
| 354 | free(jpegbuf); jpegbuf=NULL; |
| 355 | } |
| 356 | if(comptilesize) {free(comptilesize); comptilesize=NULL;} |
| 357 | if(rgbbuf) {free(rgbbuf); rgbbuf=NULL;} |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 358 | if(hnd) {tjDestroy(hnd); hnd=NULL;} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 359 | return; |
| 360 | } |
| 361 | |
| 362 | |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 363 | void dodecomptest(char *filename) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 364 | { |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 365 | FILE *file=NULL; tjhandle hnd=NULL; |
| 366 | unsigned char *jpegbuf=NULL; |
| 367 | int w=0, h=0, jpegsub=-1; |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 368 | unsigned long jpgbufsize=0; |
DRC | 61c15bd | 2010-11-13 05:31:25 +0000 | [diff] [blame] | 369 | char *temp=NULL; |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 370 | |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 371 | useppm=1; |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 372 | |
| 373 | if((file=fopen(filename, "rb"))==NULL) |
| 374 | _throwunix("opening file"); |
DRC | 61c15bd | 2010-11-13 05:31:25 +0000 | [diff] [blame] | 375 | if(fseek(file, 0, SEEK_END)<0 || (jpgbufsize=ftell(file))<0) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 376 | _throwunix("determining file size"); |
DRC | 61c15bd | 2010-11-13 05:31:25 +0000 | [diff] [blame] | 377 | if((jpegbuf=(unsigned char *)malloc(jpgbufsize))==NULL) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 378 | _throwunix("allocating memory"); |
| 379 | if(fseek(file, 0, SEEK_SET)<0) |
| 380 | _throwunix("setting file position"); |
DRC | 61c15bd | 2010-11-13 05:31:25 +0000 | [diff] [blame] | 381 | if(fread(jpegbuf, jpgbufsize, 1, file)<1) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 382 | _throwunix("reading JPEG data"); |
DRC | cad1cfe | 2010-12-14 01:22:00 +0000 | [diff] [blame] | 383 | fclose(file); file=NULL; |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 384 | |
| 385 | temp=strrchr(filename, '.'); |
| 386 | if(temp!=NULL) *temp='\0'; |
| 387 | |
DRC | 72b0f2b | 2011-02-15 21:57:50 +0000 | [diff] [blame] | 388 | if((hnd=tjInitDecompress())==NULL) _throwtj("executing tjInitDecompress()"); |
DRC | cad1cfe | 2010-12-14 01:22:00 +0000 | [diff] [blame] | 389 | if(tjDecompressHeader2(hnd, jpegbuf, jpgbufsize, &w, &h, &jpegsub)==-1) |
| 390 | _throwtj("executing tjDecompressHeader2()"); |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 391 | if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()"); |
| 392 | hnd=NULL; |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 393 | |
| 394 | if(quiet) |
| 395 | { |
| 396 | printf("All performance values in Mpixels/sec\n\n"); |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 397 | printf("Bitmap\tBitmap\tJPEG\tImage Size\tDecomp\n"), |
| 398 | printf("Format\tOrder\tFormat\t X Y \tPerf\n\n"); |
| 399 | printf("%s\t%s\t%s\t%-4d %-4d\t", _pfname[pf], bu?"BU":"TD", |
| 400 | _subnamel[jpegsub], w, h); |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 401 | } |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 402 | else |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 403 | { |
DRC | cad1cfe | 2010-12-14 01:22:00 +0000 | [diff] [blame] | 404 | if(yuv==YUVDECODE) |
| 405 | printf("\n>>>>> JPEG --> YUV %s <<<<<\n", _subnamel[jpegsub]); |
| 406 | else |
| 407 | printf("\n>>>>> JPEG --> %s (%s) <<<<<\n", _pfname[pf], |
| 408 | bu?"Bottom-up":"Top-down"); |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 409 | printf("\nImage size: %d x %d\n", w, h); |
| 410 | } |
| 411 | |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 412 | decomptest(NULL, &jpegbuf, &jpgbufsize, NULL, w, h, jpegsub, 0, filename, w, |
| 413 | h); |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 414 | |
| 415 | bailout: |
DRC | cad1cfe | 2010-12-14 01:22:00 +0000 | [diff] [blame] | 416 | if(file) {fclose(file); file=NULL;} |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 417 | if(jpegbuf) {free(jpegbuf); jpegbuf=NULL;} |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 418 | if(hnd) {tjDestroy(hnd); hnd=NULL;} |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | void usage(char *progname) |
| 424 | { |
DRC | e89bbae | 2010-11-12 10:18:08 +0000 | [diff] [blame] | 425 | printf("USAGE: %s <Inputfile (BMP|PPM))> <%% Quality>\n", progname); |
| 426 | printf(" %s <Inputfile (JPG))>\n\n", progname); |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 427 | printf(" [-tile]\n"); |
| 428 | printf(" Test performance of the codec when the image is encoded\n"); |
| 429 | printf(" as separate tiles of varying sizes.\n\n"); |
| 430 | printf(" [-forcemmx] [-forcesse] [-forcesse2] [-forcesse3]\n"); |
| 431 | printf(" Force MMX, SSE, or SSE2 code paths in Intel codec\n\n"); |
| 432 | printf(" [-rgb | -bgr | -rgba | -bgra | -abgr | -argb]\n"); |
| 433 | printf(" Test the specified color conversion path in the codec (default: BGR)\n\n"); |
| 434 | printf(" [-fastupsample]\n"); |
| 435 | printf(" Use fast, inaccurate upsampling code to perform 4:2:2 and 4:2:0\n"); |
| 436 | printf(" YUV decoding in libjpeg decompressor\n\n"); |
| 437 | printf(" [-quiet]\n"); |
DRC | ac08ef6 | 2010-12-09 10:01:57 +0000 | [diff] [blame] | 438 | printf(" Output in tabular rather than verbose format\n\n"); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 439 | printf(" [-yuvencode]\n"); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 440 | printf(" Encode RGB input as planar YUV rather than compressing as JPEG\n\n"); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 441 | printf(" [-yuvdecode]\n"); |
| 442 | printf(" Decode JPEG image to planar YUV rather than RGB\n\n"); |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 443 | printf(" NOTE: If the quality is specified as a range, i.e. 90-100, a separate\n"); |
| 444 | printf(" test will be performed for all quality values in the range.\n"); |
| 445 | exit(1); |
| 446 | } |
| 447 | |
| 448 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 449 | int main(int argc, char *argv[]) |
| 450 | { |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 451 | unsigned char *bmpbuf=NULL; int w, h, i; |
| 452 | int qual, hiqual=-1; char *temp; |
| 453 | int minarg=2; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 454 | |
| 455 | printf("\n"); |
| 456 | |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 457 | if(argc<minarg) usage(argv[0]); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 458 | |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 459 | temp=strrchr(argv[1], '.'); |
| 460 | if(temp!=NULL) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 461 | { |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 462 | if(!stricmp(temp, ".ppm")) useppm=1; |
| 463 | if(!stricmp(temp, ".jpg") || !stricmp(temp, ".jpeg")) decomponly=1; |
| 464 | } |
| 465 | |
DRC | ac08ef6 | 2010-12-09 10:01:57 +0000 | [diff] [blame] | 466 | if(argc>minarg) |
| 467 | { |
| 468 | for(i=minarg; i<argc; i++) |
| 469 | { |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 470 | if(!stricmp(argv[i], "-yuvencode")) |
DRC | ac08ef6 | 2010-12-09 10:01:57 +0000 | [diff] [blame] | 471 | { |
| 472 | printf("Testing YUV planar encoding\n"); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 473 | yuv=YUVENCODE; hiqual=qual=100; |
| 474 | } |
| 475 | if(!stricmp(argv[i], "-yuvdecode")) |
| 476 | { |
| 477 | printf("Testing YUV planar decoding\n"); |
| 478 | yuv=YUVDECODE; |
DRC | ac08ef6 | 2010-12-09 10:01:57 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 483 | if(!decomponly && yuv!=YUVENCODE) |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 484 | { |
| 485 | minarg=3; |
| 486 | if(argc<minarg) usage(argv[0]); |
| 487 | if((qual=atoi(argv[2]))<1 || qual>100) |
| 488 | { |
| 489 | puts("ERROR: Quality must be between 1 and 100."); |
| 490 | exit(1); |
| 491 | } |
| 492 | if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1 |
| 493 | && sscanf(&temp[1], "%d", &hiqual)==1 && hiqual>qual && hiqual>=1 |
| 494 | && hiqual<=100) {} |
| 495 | else hiqual=qual; |
| 496 | } |
| 497 | |
| 498 | if(argc>minarg) |
| 499 | { |
| 500 | for(i=minarg; i<argc; i++) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 501 | { |
| 502 | if(!stricmp(argv[i], "-tile")) dotile=1; |
| 503 | if(!stricmp(argv[i], "-forcesse3")) |
| 504 | { |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 505 | printf("Using SSE3 code\n"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 506 | forcesse3=1; |
| 507 | } |
| 508 | if(!stricmp(argv[i], "-forcesse2")) |
| 509 | { |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 510 | printf("Using SSE2 code\n"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 511 | forcesse2=1; |
| 512 | } |
| 513 | if(!stricmp(argv[i], "-forcesse")) |
| 514 | { |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 515 | printf("Using SSE code\n"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 516 | forcesse=1; |
| 517 | } |
| 518 | if(!stricmp(argv[i], "-forcemmx")) |
| 519 | { |
DRC | 0c6a271 | 2010-02-22 08:34:44 +0000 | [diff] [blame] | 520 | printf("Using MMX code\n"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 521 | forcemmx=1; |
| 522 | } |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 523 | if(!stricmp(argv[i], "-fastupsample")) |
| 524 | { |
| 525 | printf("Using fast upsampling code\n"); |
| 526 | fastupsample=1; |
| 527 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 528 | if(!stricmp(argv[i], "-rgb")) pf=BMP_RGB; |
| 529 | if(!stricmp(argv[i], "-rgba")) pf=BMP_RGBA; |
| 530 | if(!stricmp(argv[i], "-bgr")) pf=BMP_BGR; |
| 531 | if(!stricmp(argv[i], "-bgra")) pf=BMP_BGRA; |
| 532 | if(!stricmp(argv[i], "-abgr")) pf=BMP_ABGR; |
| 533 | if(!stricmp(argv[i], "-argb")) pf=BMP_ARGB; |
| 534 | if(!stricmp(argv[i], "-bottomup")) bu=1; |
| 535 | if(!stricmp(argv[i], "-quiet")) quiet=1; |
| 536 | } |
| 537 | } |
| 538 | |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 539 | if(!decomponly) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 540 | { |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 541 | if(loadbmp(argv[1], &bmpbuf, &w, &h, pf, 1, bu)==-1) |
| 542 | _throwbmp("loading bitmap"); |
| 543 | temp=strrchr(argv[1], '.'); |
| 544 | if(temp!=NULL) *temp='\0'; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 545 | } |
| 546 | |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 547 | if(quiet && !decomponly) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 548 | { |
| 549 | printf("All performance values in Mpixels/sec\n\n"); |
| 550 | printf("Bitmap\tBitmap\tJPEG\tJPEG\tTile Size\tCompr\tCompr\tDecomp\n"); |
| 551 | printf("Format\tOrder\tFormat\tQual\t X Y \tPerf \tRatio\tPerf\n\n"); |
| 552 | } |
| 553 | |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 554 | if(decomponly) |
| 555 | { |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 556 | dodecomptest(argv[1]); |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 557 | goto bailout; |
| 558 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 559 | for(i=hiqual; i>=qual; i--) |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 560 | dotest(bmpbuf, w, h, TJ_GRAYSCALE, i, argv[1]); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 561 | if(quiet) printf("\n"); |
| 562 | for(i=hiqual; i>=qual; i--) |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 563 | dotest(bmpbuf, w, h, TJ_420, i, argv[1]); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 564 | if(quiet) printf("\n"); |
| 565 | for(i=hiqual; i>=qual; i--) |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 566 | dotest(bmpbuf, w, h, TJ_422, i, argv[1]); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 567 | if(quiet) printf("\n"); |
| 568 | for(i=hiqual; i>=qual; i--) |
DRC | 993bf0e | 2011-02-15 21:57:26 +0000 | [diff] [blame] | 569 | dotest(bmpbuf, w, h, TJ_444, i, argv[1]); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 570 | |
DRC | e8ce2e0 | 2010-11-12 10:14:11 +0000 | [diff] [blame] | 571 | bailout: |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 572 | if(bmpbuf) free(bmpbuf); |
| 573 | return 0; |
| 574 | } |