blob: ab4d9f8ae01be4cc8c8608ab3baafaa1ba1878b9 [file] [log] [blame]
DRC2e7b76b2009-04-03 12:04:24 +00001/* Copyright (C)2004 Landmark Graphics Corporation
2 * Copyright (C)2005, 2006 Sun Microsystems, Inc.
DRC2e4d0442011-02-08 01:18:37 +00003 * Copyright (C)2009-2011 D. R. Commander
DRC2e7b76b2009-04-03 12:04:24 +00004 *
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>
DRCe8ce2e02010-11-12 10:14:11 +000020#include <errno.h>
DRC2e7b76b2009-04-03 12:04:24 +000021#include "./bmp.h"
22#include "./rrutil.h"
23#include "./rrtimer.h"
24#include "./turbojpeg.h"
25
DRCe8ce2e02010-11-12 10:14:11 +000026#define _throw(op, err) { \
DRC0f1f60d2011-03-01 20:23:12 +000027 printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
28 retval=-1; goto bailout;}
DRCe8ce2e02010-11-12 10:14:11 +000029#define _throwunix(m) _throw(m, strerror(errno))
30#define _throwtj(m) _throw(m, tjGetErrorStr())
31#define _throwbmp(m) _throw(m, bmpgeterr())
DRC2e7b76b2009-04-03 12:04:24 +000032
DRC9e17f7d2010-12-10 04:59:13 +000033enum {YUVENCODE=1, YUVDECODE};
DRCe8ce2e02010-11-12 10:14:11 +000034int forcemmx=0, forcesse=0, forcesse2=0, forcesse3=0, fastupsample=0,
DRCdae3ee92011-02-16 02:08:17 +000035 decomponly=0, yuv=0, quiet=0, dotile=0, pf=BMP_BGR, bu=0, useppm=0,
DRC109a5782011-03-01 09:53:07 +000036 scale_num=1, scale_denom=1;
DRC2e7b76b2009-04-03 12:04:24 +000037const int _ps[BMPPIXELFORMATS]={3, 4, 3, 4, 4, 4};
38const int _flags[BMPPIXELFORMATS]={0, 0, TJ_BGR, TJ_BGR,
39 TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST};
40const int _rindex[BMPPIXELFORMATS]={0, 0, 2, 2, 3, 1};
41const int _gindex[BMPPIXELFORMATS]={1, 1, 1, 1, 2, 2};
42const int _bindex[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3};
DRC035aac42011-02-19 18:10:05 +000043const char *_pfname[]={"RGB", "RGBX", "BGR", "BGRX", "XBGR", "XRGB"};
DRCa3f68b42011-03-04 12:52:45 +000044const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0"};
45const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY", "440"};
DRC109a5782011-03-01 09:53:07 +000046tjscalingfactor *sf=NULL; int nsf=0;
DRCf2cef492011-03-04 02:25:00 +000047int xformop=TJXFORM_NONE, xformopt=0;
48double benchtime=5.0;
DRC2e7b76b2009-04-03 12:04:24 +000049
50void printsigfig(double val, int figs)
51{
52 char format[80];
53 double _l=log10(val); int l;
54 if(_l<0.)
55 {
56 l=(int)fabs(_l);
DRCda5220a2011-03-02 02:17:30 +000057 snprintf(format, 80, "%%%d.%df", figs+l+2, figs+l);
DRC2e7b76b2009-04-03 12:04:24 +000058 }
59 else
60 {
61 l=(int)_l+1;
DRCda5220a2011-03-02 02:17:30 +000062 if(figs<=l) snprintf(format, 80, "%%.0f");
63 else snprintf(format, 80, "%%%d.%df", figs+1, figs-l);
DRC2e7b76b2009-04-03 12:04:24 +000064 }
65 printf(format, val);
66}
67
DRC993bf0e2011-02-15 21:57:26 +000068// Decompression test
69int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf,
70 unsigned long *comptilesize, unsigned char *rgbbuf, int w, int h,
71 int jpegsub, int qual, char *filename, int tilesizex, int tilesizey)
72{
DRC04def092011-03-31 20:48:30 +000073 char tempstr[1024], sizestr[20]="\0", qualstr[6]="\0", *ptr;
DRC993bf0e2011-02-15 21:57:26 +000074 FILE *outfile=NULL; tjhandle hnd=NULL;
75 int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0)
76 |(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0)
77 |(fastupsample?TJ_FASTUPSAMPLE:0);
DRC0f1f60d2011-03-01 20:23:12 +000078 int i, j, ITER, rgbbufalloc=0, retval=0;
DRC993bf0e2011-02-15 21:57:26 +000079 double start, elapsed;
80 int ps=_ps[pf];
DRC84241602011-02-25 02:08:23 +000081 int yuvsize=TJBUFSIZEYUV(w, h, jpegsub), bufsize;
DRC109a5782011-03-01 09:53:07 +000082 int scaledw=(yuv==YUVDECODE)? w : (w*scale_num+scale_denom-1)/scale_denom;
83 int scaledh=(yuv==YUVDECODE)? h : (h*scale_num+scale_denom-1)/scale_denom;
DRCb28fc572011-02-22 06:41:29 +000084 int pitch=scaledw*ps;
DRC993bf0e2011-02-15 21:57:26 +000085
86 if(qual>0)
87 {
DRC04def092011-03-31 20:48:30 +000088 snprintf(qualstr, 6, "_Q%d", qual);
DRC993bf0e2011-02-15 21:57:26 +000089 qualstr[4]=0;
90 }
91
92 flags |= _flags[pf];
93 if(bu) flags |= TJ_BOTTOMUP;
DRC993bf0e2011-02-15 21:57:26 +000094 if((hnd=tjInitDecompress())==NULL)
95 _throwtj("executing tjInitDecompress()");
96
DRC17ac3722011-02-26 21:20:46 +000097 bufsize=(yuv==YUVDECODE? yuvsize:pitch*h);
DRC993bf0e2011-02-15 21:57:26 +000098 if(rgbbuf==NULL)
99 {
DRC84241602011-02-25 02:08:23 +0000100 if((rgbbuf=(unsigned char *)malloc(bufsize)) == NULL)
DRC993bf0e2011-02-15 21:57:26 +0000101 _throwunix("allocating image buffer");
102 rgbbufalloc=1;
103 }
DRCb28fc572011-02-22 06:41:29 +0000104 // Grey image means decompressor did nothing
DRC84241602011-02-25 02:08:23 +0000105 memset(rgbbuf, 127, bufsize);
DRC993bf0e2011-02-15 21:57:26 +0000106
DRC84241602011-02-25 02:08:23 +0000107 if(yuv==YUVDECODE)
108 {
109 if(tjDecompressToYUV(hnd, jpegbuf[0], comptilesize[0], rgbbuf, flags)==-1)
110 _throwtj("executing tjDecompressToYUV()");
111 }
112 else if(tjDecompress(hnd, jpegbuf[0], comptilesize[0], rgbbuf, scaledw,
113 pitch, scaledh, ps, flags)==-1)
DRC993bf0e2011-02-15 21:57:26 +0000114 _throwtj("executing tjDecompress()");
115 ITER=0;
116 start=rrtime();
117 do
118 {
119 int tilen=0;
120 for(i=0; i<h; i+=tilesizey)
121 {
122 for(j=0; j<w; j+=tilesizex)
123 {
DRC890f1e02011-02-26 22:02:37 +0000124 int tempw=dotile? min(tilesizex, w-j):scaledw;
125 int temph=dotile? min(tilesizey, h-i):scaledh;
DRC84241602011-02-25 02:08:23 +0000126 if(yuv==YUVDECODE)
127 {
128 if(tjDecompressToYUV(hnd, jpegbuf[tilen], comptilesize[tilen],
129 &rgbbuf[pitch*i+ps*j], flags)==-1)
130 _throwtj("executing tjDecompressToYUV()");
131 }
132 else if(tjDecompress(hnd, jpegbuf[tilen], comptilesize[tilen],
DRC890f1e02011-02-26 22:02:37 +0000133 &rgbbuf[pitch*i+ps*j], tempw, pitch, temph, ps, flags)==-1)
DRCb28fc572011-02-22 06:41:29 +0000134 _throwtj("executing tjDecompress()");
DRC993bf0e2011-02-15 21:57:26 +0000135 tilen++;
136 }
137 }
138 ITER++;
DRCf2cef492011-03-04 02:25:00 +0000139 } while((elapsed=rrtime()-start)<benchtime);
DRC993bf0e2011-02-15 21:57:26 +0000140 if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()");
141 hnd=NULL;
142 if(quiet)
143 {
144 printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
145 printf("\n");
146 }
147 else
148 {
149 printf("D--> Frame rate: %f fps\n", (double)ITER/elapsed);
150 printf(" Dest. throughput: %f Megapixels/sec\n",
151 (double)(w*h)/1000000.*(double)ITER/elapsed);
152 }
153 if(yuv==YUVDECODE)
154 {
DRCda5220a2011-03-02 02:17:30 +0000155 snprintf(tempstr, 1024, "%s_%s%s.yuv", filename, _subnames[jpegsub],
156 qualstr);
DRC993bf0e2011-02-15 21:57:26 +0000157 if((outfile=fopen(tempstr, "wb"))==NULL)
158 _throwunix("opening YUV image for output");
159 if(fwrite(rgbbuf, yuvsize, 1, outfile)!=1)
160 _throwunix("writing YUV image");
161 fclose(outfile); outfile=NULL;
162 }
163 else
164 {
DRCd8ebb6b2011-03-04 04:14:47 +0000165 if(scale_num!=1 || scale_denom!=1)
166 snprintf(sizestr, 20, "%d_%d", scale_num, scale_denom);
167 else if(tilesizex!=w || tilesizey!=h)
168 snprintf(sizestr, 20, "%dx%d", tilesizex, tilesizey);
169 else snprintf(sizestr, 20, "full");
170 if(decomponly)
171 snprintf(tempstr, 1024, "%s_%s.%s", filename, sizestr,
172 useppm?"ppm":"bmp");
DRC0a325192011-03-02 09:22:41 +0000173 else
DRCd8ebb6b2011-03-04 04:14:47 +0000174 snprintf(tempstr, 1024, "%s_%s%s_%s.%s", filename,
175 _subnames[jpegsub], qualstr, sizestr, useppm?"ppm":"bmp");
DRCb28fc572011-02-22 06:41:29 +0000176 if(savebmp(tempstr, rgbbuf, scaledw, scaledh, pf, pitch, bu)==-1)
DRC993bf0e2011-02-15 21:57:26 +0000177 _throwbmp("saving bitmap");
DRCda5220a2011-03-02 02:17:30 +0000178 ptr=strrchr(tempstr, '.');
179 snprintf(ptr, 1024-(ptr-tempstr), "-err.%s", useppm?"ppm":"bmp");
DRC109a5782011-03-01 09:53:07 +0000180 if(srcbuf && scale_num==1 && scale_denom==1)
DRC993bf0e2011-02-15 21:57:26 +0000181 {
182 if(!quiet)
183 printf("Computing compression error and saving to %s.\n", tempstr);
184 if(jpegsub==TJ_GRAYSCALE)
185 {
186 for(j=0; j<h; j++)
187 {
188 for(i=0; i<w*ps; i+=ps)
189 {
190 int y=(int)((double)srcbuf[w*ps*j+i+_rindex[pf]]*0.299
191 + (double)srcbuf[w*ps*j+i+_gindex[pf]]*0.587
192 + (double)srcbuf[w*ps*j+i+_bindex[pf]]*0.114 + 0.5);
193 if(y>255) y=255; if(y<0) y=0;
194 rgbbuf[pitch*j+i+_rindex[pf]]=abs(rgbbuf[pitch*j+i+_rindex[pf]]-y);
195 rgbbuf[pitch*j+i+_gindex[pf]]=abs(rgbbuf[pitch*j+i+_gindex[pf]]-y);
196 rgbbuf[pitch*j+i+_bindex[pf]]=abs(rgbbuf[pitch*j+i+_bindex[pf]]-y);
197 }
198 }
199 }
200 else
201 {
202 for(j=0; j<h; j++) for(i=0; i<w*ps; i++)
203 rgbbuf[pitch*j+i]=abs(rgbbuf[pitch*j+i]-srcbuf[w*ps*j+i]);
204 }
205 if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
206 _throwbmp("saving bitmap");
207 }
208 }
209
DRC993bf0e2011-02-15 21:57:26 +0000210 bailout:
211 if(outfile) {fclose(outfile); outfile=NULL;}
212 if(hnd) {tjDestroy(hnd); hnd=NULL;}
213 if(rgbbuf && rgbbufalloc) {free(rgbbuf); rgbbuf=NULL;}
DRC0f1f60d2011-03-01 20:23:12 +0000214 return retval;
DRC993bf0e2011-02-15 21:57:26 +0000215}
216
217void dotest(unsigned char *srcbuf, int w, int h, int jpegsub, int qual,
218 char *filename)
DRC2e7b76b2009-04-03 12:04:24 +0000219{
220 char tempstr[1024];
DRC17ac3722011-02-26 21:20:46 +0000221 FILE *outfile=NULL; tjhandle hnd=NULL;
DRC2e7b76b2009-04-03 12:04:24 +0000222 unsigned char **jpegbuf=NULL, *rgbbuf=NULL;
DRC2e4d0442011-02-08 01:18:37 +0000223 double start, elapsed;
DRC17ac3722011-02-26 21:20:46 +0000224 int jpgbufsize=0, i, j, tilesizex=w, tilesizey=h, numtilesx=1, numtilesy=1,
DRC0f1f60d2011-03-01 20:23:12 +0000225 ITER, retval=0;
DRC2e7b76b2009-04-03 12:04:24 +0000226 unsigned long *comptilesize=NULL;
227 int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0)
DRC61e51f92009-04-05 21:53:20 +0000228 |(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0)
229 |(fastupsample?TJ_FASTUPSAMPLE:0);
DRC2e4d0442011-02-08 01:18:37 +0000230 int ps=_ps[pf], tilen;
DRC17ac3722011-02-26 21:20:46 +0000231 int pitch=w*ps, yuvsize=0;
DRC2e7b76b2009-04-03 12:04:24 +0000232
233 flags |= _flags[pf];
234 if(bu) flags |= TJ_BOTTOMUP;
235
DRC84241602011-02-25 02:08:23 +0000236 if(yuv==YUVENCODE) yuvsize=TJBUFSIZEYUV(w, h, jpegsub);
237 if((rgbbuf=(unsigned char *)malloc(max(yuvsize, pitch*h+1))) == NULL)
DRCe8ce2e02010-11-12 10:14:11 +0000238 _throwunix("allocating image buffer");
DRC2e7b76b2009-04-03 12:04:24 +0000239
DRCfbb67472010-11-24 04:02:37 +0000240 if(!quiet)
241 {
DRC9e17f7d2010-12-10 04:59:13 +0000242 if(yuv==YUVENCODE)
DRC0b11d1c2011-02-24 21:43:35 +0000243 printf(">>>>> %s (%s) <--> YUV %s <<<<<\n", _pfname[pf],
DRCfbb67472010-11-24 04:02:37 +0000244 bu?"Bottom-up":"Top-down", _subnamel[jpegsub]);
245 else
DRC0b11d1c2011-02-24 21:43:35 +0000246 printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", _pfname[pf],
DRCfbb67472010-11-24 04:02:37 +0000247 bu?"Bottom-up":"Top-down", _subnamel[jpegsub], qual);
248 }
DRC5cb1b682011-02-24 22:13:20 +0000249 if(yuv) dotile=0;
DRC2e7b76b2009-04-03 12:04:24 +0000250 if(dotile) {tilesizex=tilesizey=4;} else {tilesizex=w; tilesizey=h;}
251
252 do
253 {
254 tilesizex*=2; if(tilesizex>w) tilesizex=w;
255 tilesizey*=2; if(tilesizey>h) tilesizey=h;
256 numtilesx=(w+tilesizex-1)/tilesizex;
257 numtilesy=(h+tilesizey-1)/tilesizey;
DRCf2cef492011-03-04 02:25:00 +0000258
259 if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
260 *numtilesx*numtilesy))==NULL)
261 _throwunix("allocating image buffer array");
262 if((comptilesize=(unsigned long *)malloc(sizeof(unsigned long)
263 *numtilesx*numtilesy))==NULL)
264 _throwunix("allocating image size array");
DRC2e7b76b2009-04-03 12:04:24 +0000265 memset(jpegbuf, 0, sizeof(unsigned char *)*numtilesx*numtilesy);
DRCf2cef492011-03-04 02:25:00 +0000266
DRC2e7b76b2009-04-03 12:04:24 +0000267 for(i=0; i<numtilesx*numtilesy; i++)
268 {
DRC84241602011-02-25 02:08:23 +0000269 if((jpegbuf[i]=(unsigned char *)malloc(
270 yuv==YUVENCODE? TJBUFSIZEYUV(tilesizex, tilesizey, jpegsub)
271 : TJBUFSIZE(tilesizex, tilesizey))) == NULL)
DRCe8ce2e02010-11-12 10:14:11 +0000272 _throwunix("allocating image buffers");
DRC2e7b76b2009-04-03 12:04:24 +0000273 }
274
275 // Compression test
DRC2a285992011-02-19 17:20:11 +0000276 if(quiet==1) printf("%s\t%s\t%s\t%d\t", _pfname[pf], bu?"BU":"TD",
DRC2e7b76b2009-04-03 12:04:24 +0000277 _subnamel[jpegsub], qual);
278 for(i=0; i<h; i++) memcpy(&rgbbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
279 if((hnd=tjInitCompress())==NULL)
DRCe8ce2e02010-11-12 10:14:11 +0000280 _throwtj("executing tjInitCompress()");
DRC84241602011-02-25 02:08:23 +0000281 if(yuv==YUVENCODE)
282 {
283 if(tjEncodeYUV(hnd, rgbbuf, tilesizex, pitch, tilesizey, ps,
284 jpegbuf[0], jpegsub, flags)==-1)
285 _throwtj("executing tjEncodeYUV()");
286 comptilesize[0]=TJBUFSIZEYUV(tilesizex, tilesizey, jpegsub);
287 }
288 else if(tjCompress(hnd, rgbbuf, tilesizex, pitch, tilesizey, ps,
DRCe8ce2e02010-11-12 10:14:11 +0000289 jpegbuf[0], &comptilesize[0], jpegsub, qual, flags)==-1)
290 _throwtj("executing tjCompress()");
DRCf2cef492011-03-04 02:25:00 +0000291
DRC2e7b76b2009-04-03 12:04:24 +0000292 ITER=0;
DRC2e4d0442011-02-08 01:18:37 +0000293 start=rrtime();
DRC2e7b76b2009-04-03 12:04:24 +0000294 do
295 {
DRC2e4d0442011-02-08 01:18:37 +0000296 jpgbufsize=0; tilen=0;
DRC2e7b76b2009-04-03 12:04:24 +0000297 for(i=0; i<h; i+=tilesizey)
298 {
299 for(j=0; j<w; j+=tilesizex)
300 {
301 int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
DRC84241602011-02-25 02:08:23 +0000302 if(yuv==YUVENCODE)
303 {
304 if(tjEncodeYUV(hnd, &rgbbuf[pitch*i+j*ps], tempw, pitch,
305 temph, ps, jpegbuf[tilen], jpegsub, flags)==-1)
306 _throwtj("executing tjEncodeYUV()");
307 comptilesize[tilen]=TJBUFSIZEYUV(tempw, temph, jpegsub);
308 }
309 else if(tjCompress(hnd, &rgbbuf[pitch*i+j*ps], tempw, pitch,
DRC2e7b76b2009-04-03 12:04:24 +0000310 temph, ps, jpegbuf[tilen], &comptilesize[tilen], jpegsub, qual,
DRCe8ce2e02010-11-12 10:14:11 +0000311 flags)==-1)
312 _throwtj("executing tjCompress()");
DRC2e7b76b2009-04-03 12:04:24 +0000313 jpgbufsize+=comptilesize[tilen];
314 tilen++;
315 }
316 }
317 ITER++;
DRCf2cef492011-03-04 02:25:00 +0000318 } while((elapsed=rrtime()-start)<benchtime);
DRCe8ce2e02010-11-12 10:14:11 +0000319 if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()");
320 hnd=NULL;
DRCf2cef492011-03-04 02:25:00 +0000321 if(quiet==1) printf("%-4d %-4d\t", tilesizex, tilesizey);
DRC2a285992011-02-19 17:20:11 +0000322 if(quiet)
323 {
DRC2e7b76b2009-04-03 12:04:24 +0000324 printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
DRC2a285992011-02-19 17:20:11 +0000325 printf("%c", quiet==2? '\n':'\t');
DRC2e7b76b2009-04-03 12:04:24 +0000326 printsigfig((double)(w*h*ps)/(double)jpgbufsize, 4);
DRC2a285992011-02-19 17:20:11 +0000327 printf("%c", quiet==2? '\n':'\t');
DRC2e7b76b2009-04-03 12:04:24 +0000328 }
329 else
330 {
DRCf2cef492011-03-04 02:25:00 +0000331 printf("\n%s size: %d x %d\n", dotile? "Tile":"Image", tilesizex,
332 tilesizey);
DRC2e7b76b2009-04-03 12:04:24 +0000333 printf("C--> Frame rate: %f fps\n", (double)ITER/elapsed);
334 printf(" Output image size: %d bytes\n", jpgbufsize);
335 printf(" Compression ratio: %f:1\n",
336 (double)(w*h*ps)/(double)jpgbufsize);
337 printf(" Source throughput: %f Megapixels/sec\n",
338 (double)(w*h)/1000000.*(double)ITER/elapsed);
339 printf(" Output bit stream: %f Megabits/sec\n",
340 (double)jpgbufsize*8./1000000.*(double)ITER/elapsed);
341 }
342 if(tilesizex==w && tilesizey==h)
343 {
DRC9e17f7d2010-12-10 04:59:13 +0000344 if(yuv==YUVENCODE)
DRCda5220a2011-03-02 02:17:30 +0000345 snprintf(tempstr, 1024, "%s_%s.yuv", filename, _subnames[jpegsub]);
DRCfbb67472010-11-24 04:02:37 +0000346 else
DRC04def092011-03-31 20:48:30 +0000347 snprintf(tempstr, 1024, "%s_%s_Q%d.jpg", filename, _subnames[jpegsub],
DRCda5220a2011-03-02 02:17:30 +0000348 qual);
DRC2e7b76b2009-04-03 12:04:24 +0000349 if((outfile=fopen(tempstr, "wb"))==NULL)
DRCe8ce2e02010-11-12 10:14:11 +0000350 _throwunix("opening reference image");
DRC2e7b76b2009-04-03 12:04:24 +0000351 if(fwrite(jpegbuf[0], jpgbufsize, 1, outfile)!=1)
DRCe8ce2e02010-11-12 10:14:11 +0000352 _throwunix("writing reference image");
DRC9e17f7d2010-12-10 04:59:13 +0000353 fclose(outfile); outfile=NULL;
DRC2e7b76b2009-04-03 12:04:24 +0000354 if(!quiet) printf("Reference image written to %s\n", tempstr);
355 }
DRC0b11d1c2011-02-24 21:43:35 +0000356 if(yuv==YUVENCODE)
357 {
358 if(quiet==1) printf("\n"); goto bailout;
359 }
DRC2e7b76b2009-04-03 12:04:24 +0000360
361 // Decompression test
DRC993bf0e2011-02-15 21:57:26 +0000362 if(decomptest(srcbuf, jpegbuf, comptilesize, rgbbuf, w, h, jpegsub, qual,
363 filename, tilesizex, tilesizey)==-1)
364 goto bailout;
DRC2e7b76b2009-04-03 12:04:24 +0000365
366 // Cleanup
DRC0f1f60d2011-03-01 20:23:12 +0000367 for(i=0; i<numtilesx*numtilesy; i++)
368 {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;}
369 free(jpegbuf); jpegbuf=NULL;
370 free(comptilesize); comptilesize=NULL;
DRC2e7b76b2009-04-03 12:04:24 +0000371 } while(tilesizex<w || tilesizey<h);
372
DRC2e7b76b2009-04-03 12:04:24 +0000373 bailout:
DRC9e17f7d2010-12-10 04:59:13 +0000374 if(outfile) {fclose(outfile); outfile=NULL;}
DRC2e7b76b2009-04-03 12:04:24 +0000375 if(jpegbuf)
376 {
377 for(i=0; i<numtilesx*numtilesy; i++)
378 {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;}
379 free(jpegbuf); jpegbuf=NULL;
380 }
381 if(comptilesize) {free(comptilesize); comptilesize=NULL;}
382 if(rgbbuf) {free(rgbbuf); rgbbuf=NULL;}
DRCe8ce2e02010-11-12 10:14:11 +0000383 if(hnd) {tjDestroy(hnd); hnd=NULL;}
DRC2e7b76b2009-04-03 12:04:24 +0000384 return;
385}
386
387
DRC993bf0e2011-02-15 21:57:26 +0000388void dodecomptest(char *filename)
DRCe8ce2e02010-11-12 10:14:11 +0000389{
DRC993bf0e2011-02-15 21:57:26 +0000390 FILE *file=NULL; tjhandle hnd=NULL;
DRC890f1e02011-02-26 22:02:37 +0000391 unsigned char **jpegbuf=NULL, *srcbuf=NULL;
392 unsigned long *comptilesize=NULL, srcbufsize, jpgbufsize;
DRC0a325192011-03-02 09:22:41 +0000393 tjtransform *t=NULL;
DRCf2cef492011-03-04 02:25:00 +0000394 int w=0, h=0, jpegsub=-1, _w, _h, _tilesizex, _tilesizey,
DRCddd728c2011-03-04 03:38:47 +0000395 _numtilesx, _numtilesy, _jpegsub;
DRC61c15bd2010-11-13 05:31:25 +0000396 char *temp=NULL;
DRC0f1f60d2011-03-01 20:23:12 +0000397 int i, j, tilesizex, tilesizey, numtilesx, numtilesy, retval=0;
DRC890f1e02011-02-26 22:02:37 +0000398 double start, elapsed;
399 int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0)
400 |(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0);
401 int ps=_ps[pf], tilen;
DRCe8ce2e02010-11-12 10:14:11 +0000402
DRC993bf0e2011-02-15 21:57:26 +0000403 useppm=1;
DRCe8ce2e02010-11-12 10:14:11 +0000404
405 if((file=fopen(filename, "rb"))==NULL)
406 _throwunix("opening file");
DRC890f1e02011-02-26 22:02:37 +0000407 if(fseek(file, 0, SEEK_END)<0 || (srcbufsize=ftell(file))<0)
DRCe8ce2e02010-11-12 10:14:11 +0000408 _throwunix("determining file size");
DRC890f1e02011-02-26 22:02:37 +0000409 if((srcbuf=(unsigned char *)malloc(srcbufsize))==NULL)
DRCe8ce2e02010-11-12 10:14:11 +0000410 _throwunix("allocating memory");
411 if(fseek(file, 0, SEEK_SET)<0)
412 _throwunix("setting file position");
DRC890f1e02011-02-26 22:02:37 +0000413 if(fread(srcbuf, srcbufsize, 1, file)<1)
DRCe8ce2e02010-11-12 10:14:11 +0000414 _throwunix("reading JPEG data");
DRCcad1cfe2010-12-14 01:22:00 +0000415 fclose(file); file=NULL;
DRCe8ce2e02010-11-12 10:14:11 +0000416
417 temp=strrchr(filename, '.');
418 if(temp!=NULL) *temp='\0';
419
DRC890f1e02011-02-26 22:02:37 +0000420 if((hnd=tjInitTransform())==NULL) _throwtj("executing tjInitTransform()");
421 if(tjDecompressHeader2(hnd, srcbuf, srcbufsize, &w, &h, &jpegsub)==-1)
DRCcad1cfe2010-12-14 01:22:00 +0000422 _throwtj("executing tjDecompressHeader2()");
DRCe8ce2e02010-11-12 10:14:11 +0000423
DRC890f1e02011-02-26 22:02:37 +0000424 if(yuv) dotile=0;
425
DRCf2cef492011-03-04 02:25:00 +0000426 if(dotile) {tilesizex=tilesizey=8;} else {tilesizex=w; tilesizey=h;}
DRC0a325192011-03-02 09:22:41 +0000427
DRCf2cef492011-03-04 02:25:00 +0000428 if(quiet==1)
429 {
430 printf("All performance values in Mpixels/sec\n\n");
431 printf("Bitmap\tBitmap\tJPEG\t%s %s \tXform\tCompr\tDecomp\n",
432 dotile? "Tile ":"Image", dotile? "Tile ":"Image");
433 printf("Format\tOrder\tFormat\tWidth Height\tPerf \tRatio\tPerf\n\n");
434 }
435 else if(!quiet)
436 {
437 printf(">>>>> JPEG %s --> %s (%s) <<<<<\n", _subnamel[jpegsub],
438 _pfname[pf], bu?"Bottom-up":"Top-down");
439 }
440
441 do
442 {
443 tilesizex*=2; if(tilesizex>w) tilesizex=w;
444 tilesizey*=2; if(tilesizey>h) tilesizey=h;
445 numtilesx=(w+tilesizex-1)/tilesizex;
446 numtilesy=(h+tilesizey-1)/tilesizey;
447
448 if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
449 *numtilesx*numtilesy))==NULL)
450 _throwunix("allocating image buffer array");
451 if((comptilesize=(unsigned long *)malloc(sizeof(unsigned long)
452 *numtilesx*numtilesy))==NULL)
453 _throwunix("allocating image size array");
454 memset(jpegbuf, 0, sizeof(unsigned char *)*numtilesx*numtilesy);
455
456 for(i=0; i<numtilesx*numtilesy; i++)
457 {
458 if((jpegbuf[i]=(unsigned char *)malloc(
459 TJBUFSIZE(tilesizex, tilesizey))) == NULL)
460 _throwunix("allocating image buffers");
461 }
462
463 _w=w; _h=h; _tilesizex=tilesizex; _tilesizey=tilesizey;
464 if(!quiet)
465 {
DRC04def092011-03-31 20:48:30 +0000466 printf("\n%s size: %d x %d", dotile? "Tile":"Image", _tilesizex,
DRCf2cef492011-03-04 02:25:00 +0000467 _tilesizey);
468 if(scale_num!=1 || scale_denom!=1)
469 printf(" --> %d x %d", (_w*scale_num+scale_denom-1)/scale_denom,
470 (_h*scale_num+scale_denom-1)/scale_denom);
DRC04def092011-03-31 20:48:30 +0000471 printf("\n");
DRCf2cef492011-03-04 02:25:00 +0000472 }
473 else if(quiet==1)
474 {
475 printf("%s\t%s\t%s\t", _pfname[pf], bu?"BU":"TD", _subnamel[jpegsub]);
476 printf("%-4d %-4d\t", tilesizex, tilesizey);
477 }
478
DRCddd728c2011-03-04 03:38:47 +0000479 _jpegsub=jpegsub;
DRCf2cef492011-03-04 02:25:00 +0000480 if(dotile || xformop!=TJXFORM_NONE || xformopt!=0)
481 {
DRC0a325192011-03-02 09:22:41 +0000482 if((t=(tjtransform *)malloc(sizeof(tjtransform)*numtilesx*numtilesy))
483 ==NULL)
484 _throwunix("allocating image transform array");
DRC890f1e02011-02-26 22:02:37 +0000485
DRCf2cef492011-03-04 02:25:00 +0000486 if(xformop==TJXFORM_TRANSPOSE || xformop==TJXFORM_TRANSVERSE
487 || xformop==TJXFORM_ROT90 || xformop==TJXFORM_ROT270)
DRC890f1e02011-02-26 22:02:37 +0000488 {
DRCf2cef492011-03-04 02:25:00 +0000489 _w=h; _h=w; _tilesizex=tilesizey; _tilesizey=tilesizex;
DRC890f1e02011-02-26 22:02:37 +0000490 }
491
DRCddd728c2011-03-04 03:38:47 +0000492 if(xformopt&TJXFORM_GRAY) _jpegsub=TJ_GRAYSCALE;
DRCf2cef492011-03-04 02:25:00 +0000493 if(xformop==TJXFORM_HFLIP || xformop==TJXFORM_ROT180)
DRCddd728c2011-03-04 03:38:47 +0000494 _w=_w-(_w%tjmcuw[_jpegsub]);
DRCf2cef492011-03-04 02:25:00 +0000495 if(xformop==TJXFORM_VFLIP || xformop==TJXFORM_ROT180)
DRCddd728c2011-03-04 03:38:47 +0000496 _h=_h-(_h%tjmcuh[_jpegsub]);
DRCf2cef492011-03-04 02:25:00 +0000497 if(xformop==TJXFORM_TRANSVERSE || xformop==TJXFORM_ROT90)
DRCddd728c2011-03-04 03:38:47 +0000498 _w=_w-(_w%tjmcuh[_jpegsub]);
DRCf2cef492011-03-04 02:25:00 +0000499 if(xformop==TJXFORM_TRANSVERSE || xformop==TJXFORM_ROT270)
DRCddd728c2011-03-04 03:38:47 +0000500 _h=_h-(_h%tjmcuw[_jpegsub]);
DRCf2cef492011-03-04 02:25:00 +0000501 _numtilesx=(_w+_tilesizex-1)/_tilesizex;
502 _numtilesy=(_h+_tilesizey-1)/_tilesizey;
DRC890f1e02011-02-26 22:02:37 +0000503
DRCf2cef492011-03-04 02:25:00 +0000504 for(i=0, tilen=0; i<_h; i+=_tilesizey)
DRC890f1e02011-02-26 22:02:37 +0000505 {
DRCf2cef492011-03-04 02:25:00 +0000506 for(j=0; j<_w; j+=_tilesizex, tilen++)
DRC0a325192011-03-02 09:22:41 +0000507 {
DRCf2cef492011-03-04 02:25:00 +0000508 t[tilen].r.w=min(_tilesizex, _w-j);
509 t[tilen].r.h=min(_tilesizey, _h-i);
DRC0a325192011-03-02 09:22:41 +0000510 t[tilen].r.x=j;
511 t[tilen].r.y=i;
DRCf2cef492011-03-04 02:25:00 +0000512 t[tilen].op=xformop;
513 t[tilen].options=xformopt|TJXFORM_TRIM;
DRC890f1e02011-02-26 22:02:37 +0000514 }
515 }
DRC0a325192011-03-02 09:22:41 +0000516
517 start=rrtime();
DRCf2cef492011-03-04 02:25:00 +0000518 if(tjTransform(hnd, srcbuf, srcbufsize, _numtilesx*_numtilesy, jpegbuf,
DRC0a325192011-03-02 09:22:41 +0000519 comptilesize, t, flags)==-1)
520 _throwtj("executing tjTransform()");
DRC890f1e02011-02-26 22:02:37 +0000521 elapsed=rrtime()-start;
522
DRCf2cef492011-03-04 02:25:00 +0000523 for(tilen=0, jpgbufsize=0; tilen<_numtilesx*_numtilesy; tilen++)
DRC0a325192011-03-02 09:22:41 +0000524 jpgbufsize+=comptilesize[tilen];
DRC0a325192011-03-02 09:22:41 +0000525
DRC890f1e02011-02-26 22:02:37 +0000526 if(quiet)
527 {
528 printsigfig((double)(w*h)/1000000./elapsed, 4);
529 printf("%c", quiet==2? '\n':'\t');
530 printsigfig((double)(w*h*ps)/(double)jpgbufsize, 4);
531 printf("%c", quiet==2? '\n':'\t');
532 }
533 else if(!quiet)
534 {
DRC890f1e02011-02-26 22:02:37 +0000535 printf("X--> Frame rate: %f fps\n", 1.0/elapsed);
536 printf(" Output image size: %lu bytes\n", jpgbufsize);
537 printf(" Compression ratio: %f:1\n",
538 (double)(w*h*ps)/(double)jpgbufsize);
539 printf(" Source throughput: %f Megapixels/sec\n",
540 (double)(w*h)/1000000./elapsed);
541 printf(" Output bit stream: %f Megabits/sec\n",
542 (double)jpgbufsize*8./1000000./elapsed);
543 }
DRC890f1e02011-02-26 22:02:37 +0000544 }
DRCf2cef492011-03-04 02:25:00 +0000545 else
DRC890f1e02011-02-26 22:02:37 +0000546 {
DRCf2cef492011-03-04 02:25:00 +0000547 if(quiet==1) printf("N/A\tN/A\t");
548 comptilesize[0]=srcbufsize;
549 memcpy(jpegbuf[0], srcbuf, srcbufsize);
DRC890f1e02011-02-26 22:02:37 +0000550 }
DRCe8ce2e02010-11-12 10:14:11 +0000551
DRCf2cef492011-03-04 02:25:00 +0000552 if(w==tilesizex) _tilesizex=_w;
553 if(h==tilesizey) _tilesizey=_h;
DRCddd728c2011-03-04 03:38:47 +0000554 if(decomptest(NULL, jpegbuf, comptilesize, NULL, _w, _h, _jpegsub, 0,
DRCf2cef492011-03-04 02:25:00 +0000555 filename, _tilesizex, _tilesizey)==-1)
556 goto bailout;
557
558 // Cleanup
559 for(i=0; i<numtilesx*numtilesy; i++)
560 {free(jpegbuf[i]); jpegbuf[i]=NULL;}
561 free(jpegbuf); jpegbuf=NULL;
562 if(comptilesize) {free(comptilesize); comptilesize=NULL;}
563 } while(tilesizex<w || tilesizey<h);
DRCe8ce2e02010-11-12 10:14:11 +0000564
565 bailout:
DRCcad1cfe2010-12-14 01:22:00 +0000566 if(file) {fclose(file); file=NULL;}
DRC890f1e02011-02-26 22:02:37 +0000567 if(jpegbuf)
568 {
569 for(i=0; i<numtilesx*numtilesy; i++)
570 {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;}
571 free(jpegbuf); jpegbuf=NULL;
572 }
573 if(comptilesize) {free(comptilesize); comptilesize=NULL;}
574 if(srcbuf) {free(srcbuf); srcbuf=NULL;}
DRCe8ce2e02010-11-12 10:14:11 +0000575 if(hnd) {tjDestroy(hnd); hnd=NULL;}
576 return;
577}
578
579
580void usage(char *progname)
581{
DRC109a5782011-03-01 09:53:07 +0000582 int i;
DRCdae3ee92011-02-16 02:08:17 +0000583 printf("USAGE: %s\n", progname);
584 printf(" <Inputfile (BMP|PPM)> <%% Quality> [options]\n\n");
585 printf(" %s\n", progname);
586 printf(" <Inputfile (JPG)> [options]\n\n");
587 printf("Options:\n\n");
588 printf("-tile = Test performance of the codec when the image is encoded as separate\n");
589 printf(" tiles of varying sizes.\n");
590 printf("-forcemmx, -forcesse, -forcesse2, -forcesse3 =\n");
DRC035aac42011-02-19 18:10:05 +0000591 printf(" Force MMX, SSE, SSE2, or SSE3 code paths in the underlying codec\n");
592 printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
DRCdae3ee92011-02-16 02:08:17 +0000593 printf(" Test the specified color conversion path in the codec (default: BGR)\n");
594 printf("-fastupsample = Use fast, inaccurate upsampling code to perform 4:2:2 and 4:2:0\n");
595 printf(" YUV decoding in libjpeg decompressor\n");
596 printf("-quiet = Output results in tabular rather than verbose format\n");
597 printf("-yuvencode = Encode RGB input as planar YUV rather than compressing as JPEG\n");
598 printf("-yuvdecode = Decode JPEG image to planar YUV rather than RGB\n");
DRC109a5782011-03-01 09:53:07 +0000599 printf("-scale M/N = scale down the width/height of the decompressed JPEG image by a\n");
600 printf(" factor of M/N (M/N = ");
601 for(i=0; i<nsf; i++)
602 {
603 printf("%d/%d", sf[i].num, sf[i].denom);
604 if(nsf==2 && i!=nsf-1) printf(" or ");
605 else if(nsf>2)
606 {
607 if(i!=nsf-1) printf(", ");
608 if(i==nsf-2) printf("or ");
609 }
610 }
DRCf2cef492011-03-04 02:25:00 +0000611 printf(")\n");
612 printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
613 printf(" Perform the corresponding lossless transform prior to\n");
614 printf(" decompression (these options are mutually exclusive)\n");
615 printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
616 printf(" test (can be combined with the other transforms above)\n");
617 printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n\n");
DRCdae3ee92011-02-16 02:08:17 +0000618 printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
619 printf("test will be performed for all quality values in the range.\n\n");
DRCe8ce2e02010-11-12 10:14:11 +0000620 exit(1);
621}
622
623
DRC2e7b76b2009-04-03 12:04:24 +0000624int main(int argc, char *argv[])
625{
DRC109a5782011-03-01 09:53:07 +0000626 unsigned char *bmpbuf=NULL; int w, h, i, j;
DRC17ac3722011-02-26 21:20:46 +0000627 int qual=-1, hiqual=-1; char *temp;
DRC0f1f60d2011-03-01 20:23:12 +0000628 int minarg=2; int retval=0;
DRC2e7b76b2009-04-03 12:04:24 +0000629
DRC109a5782011-03-01 09:53:07 +0000630 if((sf=tjGetScalingFactors(&nsf))==NULL || nsf==0)
631 _throwtj("executing tjGetScalingFactors()");
632
DRCe8ce2e02010-11-12 10:14:11 +0000633 if(argc<minarg) usage(argv[0]);
DRC2e7b76b2009-04-03 12:04:24 +0000634
DRCe8ce2e02010-11-12 10:14:11 +0000635 temp=strrchr(argv[1], '.');
636 if(temp!=NULL)
DRC2e7b76b2009-04-03 12:04:24 +0000637 {
DRCe8ce2e02010-11-12 10:14:11 +0000638 if(!stricmp(temp, ".ppm")) useppm=1;
639 if(!stricmp(temp, ".jpg") || !stricmp(temp, ".jpeg")) decomponly=1;
640 }
641
DRC0b11d1c2011-02-24 21:43:35 +0000642 printf("\n");
643
DRCac08ef62010-12-09 10:01:57 +0000644 if(argc>minarg)
645 {
646 for(i=minarg; i<argc; i++)
647 {
DRC9e17f7d2010-12-10 04:59:13 +0000648 if(!stricmp(argv[i], "-yuvencode"))
DRCac08ef62010-12-09 10:01:57 +0000649 {
DRC0b11d1c2011-02-24 21:43:35 +0000650 printf("Testing YUV planar encoding\n\n");
DRC9e17f7d2010-12-10 04:59:13 +0000651 yuv=YUVENCODE; hiqual=qual=100;
652 }
653 if(!stricmp(argv[i], "-yuvdecode"))
654 {
DRC0b11d1c2011-02-24 21:43:35 +0000655 printf("Testing YUV planar decoding\n\n");
DRC9e17f7d2010-12-10 04:59:13 +0000656 yuv=YUVDECODE;
DRCac08ef62010-12-09 10:01:57 +0000657 }
658 }
659 }
660
DRC9e17f7d2010-12-10 04:59:13 +0000661 if(!decomponly && yuv!=YUVENCODE)
DRCe8ce2e02010-11-12 10:14:11 +0000662 {
663 minarg=3;
664 if(argc<minarg) usage(argv[0]);
665 if((qual=atoi(argv[2]))<1 || qual>100)
666 {
667 puts("ERROR: Quality must be between 1 and 100.");
668 exit(1);
669 }
670 if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
671 && sscanf(&temp[1], "%d", &hiqual)==1 && hiqual>qual && hiqual>=1
672 && hiqual<=100) {}
673 else hiqual=qual;
674 }
675
676 if(argc>minarg)
677 {
678 for(i=minarg; i<argc; i++)
DRC2e7b76b2009-04-03 12:04:24 +0000679 {
DRCf2cef492011-03-04 02:25:00 +0000680 if(!stricmp(argv[i], "-tile"))
681 {
682 dotile=1; xformopt|=TJXFORM_CROP;
683 }
DRC2e7b76b2009-04-03 12:04:24 +0000684 if(!stricmp(argv[i], "-forcesse3"))
685 {
DRC0b11d1c2011-02-24 21:43:35 +0000686 printf("Using SSE3 code\n\n");
DRC2e7b76b2009-04-03 12:04:24 +0000687 forcesse3=1;
688 }
689 if(!stricmp(argv[i], "-forcesse2"))
690 {
DRC0b11d1c2011-02-24 21:43:35 +0000691 printf("Using SSE2 code\n\n");
DRC2e7b76b2009-04-03 12:04:24 +0000692 forcesse2=1;
693 }
694 if(!stricmp(argv[i], "-forcesse"))
695 {
DRC0b11d1c2011-02-24 21:43:35 +0000696 printf("Using SSE code\n\n");
DRC2e7b76b2009-04-03 12:04:24 +0000697 forcesse=1;
698 }
699 if(!stricmp(argv[i], "-forcemmx"))
700 {
DRC0b11d1c2011-02-24 21:43:35 +0000701 printf("Using MMX code\n\n");
DRC2e7b76b2009-04-03 12:04:24 +0000702 forcemmx=1;
703 }
DRC61e51f92009-04-05 21:53:20 +0000704 if(!stricmp(argv[i], "-fastupsample"))
705 {
DRC0b11d1c2011-02-24 21:43:35 +0000706 printf("Using fast upsampling code\n\n");
DRC61e51f92009-04-05 21:53:20 +0000707 fastupsample=1;
708 }
DRC2e7b76b2009-04-03 12:04:24 +0000709 if(!stricmp(argv[i], "-rgb")) pf=BMP_RGB;
DRC035aac42011-02-19 18:10:05 +0000710 if(!stricmp(argv[i], "-rgbx")) pf=BMP_RGBX;
DRC2e7b76b2009-04-03 12:04:24 +0000711 if(!stricmp(argv[i], "-bgr")) pf=BMP_BGR;
DRC035aac42011-02-19 18:10:05 +0000712 if(!stricmp(argv[i], "-bgrx")) pf=BMP_BGRX;
713 if(!stricmp(argv[i], "-xbgr")) pf=BMP_XBGR;
714 if(!stricmp(argv[i], "-xrgb")) pf=BMP_XRGB;
DRC2e7b76b2009-04-03 12:04:24 +0000715 if(!stricmp(argv[i], "-bottomup")) bu=1;
716 if(!stricmp(argv[i], "-quiet")) quiet=1;
DRC2a285992011-02-19 17:20:11 +0000717 if(!stricmp(argv[i], "-qq")) quiet=2;
DRCdae3ee92011-02-16 02:08:17 +0000718 if(!stricmp(argv[i], "-scale") && i<argc-1)
719 {
DRC109a5782011-03-01 09:53:07 +0000720 int temp1=0, temp2=0, match=0;
721 if(sscanf(argv[++i], "%d/%d", &temp1, &temp2)==2)
722 {
723 for(j=0; j<nsf; j++)
724 {
725 if(temp1==sf[j].num && temp2==sf[j].denom)
726 {
727 scale_num=temp1; scale_denom=temp2;
728 match=1; break;
729 }
730 }
731 if(!match) usage(argv[0]);
732 }
733 else usage(argv[0]);
DRCdae3ee92011-02-16 02:08:17 +0000734 }
DRCf2cef492011-03-04 02:25:00 +0000735 if(!stricmp(argv[i], "-hflip")) xformop=TJXFORM_HFLIP;
736 if(!stricmp(argv[i], "-vflip")) xformop=TJXFORM_VFLIP;
737 if(!stricmp(argv[i], "-transpose")) xformop=TJXFORM_TRANSPOSE;
738 if(!stricmp(argv[i], "-transverse")) xformop=TJXFORM_TRANSVERSE;
739 if(!stricmp(argv[i], "-rot90")) xformop=TJXFORM_ROT90;
740 if(!stricmp(argv[i], "-rot180")) xformop=TJXFORM_ROT180;
741 if(!stricmp(argv[i], "-rot270")) xformop=TJXFORM_ROT270;
742 if(!stricmp(argv[i], "-grayscale")) xformopt|=TJXFORM_GRAY;
743 if(!stricmp(argv[i], "-benchtime") && i<argc-1)
744 {
745 double temp=atof(argv[++i]);
746 if(temp>0.0) benchtime=temp;
747 else usage(argv[0]);
748 }
DRC2e7b76b2009-04-03 12:04:24 +0000749 }
750 }
751
DRC109a5782011-03-01 09:53:07 +0000752 if((scale_num!=1 || scale_denom!=1) && dotile)
DRCdae3ee92011-02-16 02:08:17 +0000753 {
754 printf("Disabling tiled compression/decompression tests, because these tests do not\n");
755 printf("work when scaled decompression is enabled.\n");
756 dotile=0;
757 }
758
DRCe8ce2e02010-11-12 10:14:11 +0000759 if(!decomponly)
DRC2e7b76b2009-04-03 12:04:24 +0000760 {
DRCe8ce2e02010-11-12 10:14:11 +0000761 if(loadbmp(argv[1], &bmpbuf, &w, &h, pf, 1, bu)==-1)
762 _throwbmp("loading bitmap");
763 temp=strrchr(argv[1], '.');
764 if(temp!=NULL) *temp='\0';
DRC2e7b76b2009-04-03 12:04:24 +0000765 }
766
DRC2a285992011-02-19 17:20:11 +0000767 if(quiet==1 && !decomponly)
DRC2e7b76b2009-04-03 12:04:24 +0000768 {
DRC0b11d1c2011-02-24 21:43:35 +0000769 printf("All performance values in Mpixels/sec\n\n");
DRCf2cef492011-03-04 02:25:00 +0000770 printf("Bitmap\tBitmap\tJPEG\tJPEG\t%s %s \tCompr\tCompr\tDecomp\n",
771 dotile? "Tile ":"Image", dotile? "Tile ":"Image");
772 printf("Format\tOrder\tFormat\tQual\tWidth Height\tPerf \tRatio\tPerf\n\n");
DRC2e7b76b2009-04-03 12:04:24 +0000773 }
774
DRCe8ce2e02010-11-12 10:14:11 +0000775 if(decomponly)
776 {
DRC993bf0e2011-02-15 21:57:26 +0000777 dodecomptest(argv[1]);
DRC0b11d1c2011-02-24 21:43:35 +0000778 printf("\n");
DRCe8ce2e02010-11-12 10:14:11 +0000779 goto bailout;
780 }
DRC2e7b76b2009-04-03 12:04:24 +0000781 for(i=hiqual; i>=qual; i--)
DRC993bf0e2011-02-15 21:57:26 +0000782 dotest(bmpbuf, w, h, TJ_GRAYSCALE, i, argv[1]);
DRC0b11d1c2011-02-24 21:43:35 +0000783 printf("\n");
DRC2e7b76b2009-04-03 12:04:24 +0000784 for(i=hiqual; i>=qual; i--)
DRC993bf0e2011-02-15 21:57:26 +0000785 dotest(bmpbuf, w, h, TJ_420, i, argv[1]);
DRC0b11d1c2011-02-24 21:43:35 +0000786 printf("\n");
DRC2e7b76b2009-04-03 12:04:24 +0000787 for(i=hiqual; i>=qual; i--)
DRC993bf0e2011-02-15 21:57:26 +0000788 dotest(bmpbuf, w, h, TJ_422, i, argv[1]);
DRC0b11d1c2011-02-24 21:43:35 +0000789 printf("\n");
DRC2e7b76b2009-04-03 12:04:24 +0000790 for(i=hiqual; i>=qual; i--)
DRC993bf0e2011-02-15 21:57:26 +0000791 dotest(bmpbuf, w, h, TJ_444, i, argv[1]);
DRC0b11d1c2011-02-24 21:43:35 +0000792 printf("\n");
DRC2e7b76b2009-04-03 12:04:24 +0000793
DRC109a5782011-03-01 09:53:07 +0000794 bailout:
795 if(bmpbuf) free(bmpbuf);
DRC0f1f60d2011-03-01 20:23:12 +0000796 return retval;
DRC2e7b76b2009-04-03 12:04:24 +0000797}