blob: d0baea537597f37af600f29a98d4a8125f0f8447 [file] [log] [blame]
DRCe34390b2009-04-03 12:04:24 +00001/* Copyright (C)2004 Landmark Graphics Corporation
2 * Copyright (C)2005, 2006 Sun Microsystems, Inc.
3 *
4 * This library is free software and may be redistributed and/or modified under
5 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
6 * any later version. The full license is in the LICENSE.txt file included
7 * with this distribution.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * wxWindows Library License for more details.
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <math.h>
19#include "./bmp.h"
20#include "./rrutil.h"
21#include "./rrtimer.h"
22#include "./turbojpeg.h"
23
24#define _catch(f) {if((f)==-1) {printf("Error in %s:\n%s\n", #f, tjGetErrorStr()); goto bailout;}}
25
26int forcemmx=0, forcesse=0, forcesse2=0, forcesse3=0;
27const int _ps[BMPPIXELFORMATS]={3, 4, 3, 4, 4, 4};
28const int _flags[BMPPIXELFORMATS]={0, 0, TJ_BGR, TJ_BGR,
29 TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST};
30const int _rindex[BMPPIXELFORMATS]={0, 0, 2, 2, 3, 1};
31const int _gindex[BMPPIXELFORMATS]={1, 1, 1, 1, 2, 2};
32const int _bindex[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3};
33const char *_pfname[]={"RGB", "RGBA", "BGR", "BGRA", "ABGR", "ARGB"};
34const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:1:1", "GRAY"};
35const char *_subnames[NUMSUBOPT]={"444", "422", "411", "GRAY"};
36
37void printsigfig(double val, int figs)
38{
39 char format[80];
40 double _l=log10(val); int l;
41 if(_l<0.)
42 {
43 l=(int)fabs(_l);
44 sprintf(format, "%%%d.%df", figs+l+2, figs+l);
45 }
46 else
47 {
48 l=(int)_l+1;
49 if(figs<=l) sprintf(format, "%%.0f");
50 else sprintf(format, "%%%d.%df", figs+1, figs-l);
51 }
52 printf(format, val);
53}
54
55void dotest(unsigned char *srcbuf, int w, int h, BMPPIXELFORMAT pf, int bu,
56 int jpegsub, int qual, char *filename, int dotile, int useppm, int quiet)
57{
58 char tempstr[1024];
59 FILE *outfile; tjhandle hnd;
60 unsigned char **jpegbuf=NULL, *rgbbuf=NULL;
61 rrtimer timer; double elapsed;
62 int jpgbufsize=0, i, j, tilesizex, tilesizey, numtilesx, numtilesy, ITER;
63 unsigned long *comptilesize=NULL;
64 int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0)
65 |(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0);
66 int ps=_ps[pf];
67 int pitch=w*ps;
68
69 flags |= _flags[pf];
70 if(bu) flags |= TJ_BOTTOMUP;
71
72 if((rgbbuf=(unsigned char *)malloc(pitch*h)) == NULL)
73 {
74 puts("ERROR: Could not allocate image buffer.");
75 exit(1);
76 }
77
78 if(!quiet) printf("\n>>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", _pfname[pf],
79 bu?"Bottom-up":"Top-down", _subnamel[jpegsub], qual);
80 if(dotile) {tilesizex=tilesizey=4;} else {tilesizex=w; tilesizey=h;}
81
82 do
83 {
84 tilesizex*=2; if(tilesizex>w) tilesizex=w;
85 tilesizey*=2; if(tilesizey>h) tilesizey=h;
86 numtilesx=(w+tilesizex-1)/tilesizex;
87 numtilesy=(h+tilesizey-1)/tilesizey;
88 if((comptilesize=(unsigned long *)malloc(sizeof(unsigned long)*numtilesx*numtilesy)) == NULL
89 || (jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)*numtilesx*numtilesy)) == NULL)
90 {
91 puts("ERROR: Could not allocate image buffers.");
92 goto bailout;
93 }
94 memset(jpegbuf, 0, sizeof(unsigned char *)*numtilesx*numtilesy);
95 for(i=0; i<numtilesx*numtilesy; i++)
96 {
97 if((jpegbuf[i]=(unsigned char *)malloc(TJBUFSIZE(tilesizex, tilesizey))) == NULL)
98 {
99 puts("ERROR: Could not allocate image buffers.");
100 goto bailout;
101 }
102 }
103
104 // Compression test
105 if(quiet) printf("%s\t%s\t%s\t%d\t", _pfname[pf], bu?"BU":"TD",
106 _subnamel[jpegsub], qual);
107 for(i=0; i<h; i++) memcpy(&rgbbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
108 if((hnd=tjInitCompress())==NULL)
109 {
110 printf("Error in tjInitCompress():\n%s\n", tjGetErrorStr());
111 goto bailout;
112 }
113 _catch(tjCompress(hnd, rgbbuf, tilesizex, pitch, tilesizey, ps,
114 jpegbuf[0], &comptilesize[0], jpegsub, qual, flags));
115 ITER=0;
116 timer.start();
117 do
118 {
119 jpgbufsize=0; int tilen=0;
120 for(i=0; i<h; i+=tilesizey)
121 {
122 for(j=0; j<w; j+=tilesizex)
123 {
124 int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
125 _catch(tjCompress(hnd, &rgbbuf[pitch*i+j*ps], tempw, pitch,
126 temph, ps, jpegbuf[tilen], &comptilesize[tilen], jpegsub, qual,
127 flags));
128 jpgbufsize+=comptilesize[tilen];
129 tilen++;
130 }
131 }
132 ITER++;
133 } while((elapsed=timer.elapsed())<5.);
134 _catch(tjDestroy(hnd));
135 if(quiet)
136 {
137 if(tilesizex==w && tilesizey==h) printf("Full \t");
138 else printf("%-4d %-4d\t", tilesizex, tilesizey);
139 printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
140 printf("\t");
141 printsigfig((double)(w*h*ps)/(double)jpgbufsize, 4);
142 printf("\t");
143 }
144 else
145 {
146 if(tilesizex==w && tilesizey==h) printf("\nFull image\n");
147 else printf("\nTile size: %d x %d\n", tilesizex, tilesizey);
148 printf("C--> Frame rate: %f fps\n", (double)ITER/elapsed);
149 printf(" Output image size: %d bytes\n", jpgbufsize);
150 printf(" Compression ratio: %f:1\n",
151 (double)(w*h*ps)/(double)jpgbufsize);
152 printf(" Source throughput: %f Megapixels/sec\n",
153 (double)(w*h)/1000000.*(double)ITER/elapsed);
154 printf(" Output bit stream: %f Megabits/sec\n",
155 (double)jpgbufsize*8./1000000.*(double)ITER/elapsed);
156 }
157 if(tilesizex==w && tilesizey==h)
158 {
159 sprintf(tempstr, "%s_%sQ%d.jpg", filename, _subnames[jpegsub], qual);
160 if((outfile=fopen(tempstr, "wb"))==NULL)
161 {
162 puts("ERROR: Could not open reference image");
163 exit(1);
164 }
165 if(fwrite(jpegbuf[0], jpgbufsize, 1, outfile)!=1)
166 {
167 puts("ERROR: Could not write reference image");
168 exit(1);
169 }
170 fclose(outfile);
171 if(!quiet) printf("Reference image written to %s\n", tempstr);
172 }
173
174 // Decompression test
175 memset(rgbbuf, 127, pitch*h); // Grey image means decompressor did nothing
176 if((hnd=tjInitDecompress())==NULL)
177 {
178 printf("Error in tjInitDecompress():\n%s\n", tjGetErrorStr());
179 goto bailout;
180 }
181 _catch(tjDecompress(hnd, jpegbuf[0], jpgbufsize, rgbbuf, tilesizex, pitch,
182 tilesizey, ps, flags));
183 ITER=0;
184 timer.start();
185 do
186 {
187 int tilen=0;
188 for(i=0; i<h; i+=tilesizey)
189 {
190 for(j=0; j<w; j+=tilesizex)
191 {
192 int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
193 _catch(tjDecompress(hnd, jpegbuf[tilen], comptilesize[tilen],
194 &rgbbuf[pitch*i+ps*j], tempw, pitch, temph, ps, flags));
195 tilen++;
196 }
197 }
198 ITER++;
199 } while((elapsed=timer.elapsed())<5.);
200 _catch(tjDestroy(hnd));
201 if(quiet)
202 {
203 printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
204 printf("\n");
205 }
206 else
207 {
208 printf("D--> Frame rate: %f fps\n", (double)ITER/elapsed);
209 printf(" Dest. throughput: %f Megapixels/sec\n",
210 (double)(w*h)/1000000.*(double)ITER/elapsed);
211 }
212 if(tilesizex==w && tilesizey==h)
213 sprintf(tempstr, "%s_%sQ%d_full.%s", filename, _subnames[jpegsub], qual,
214 useppm?"ppm":"bmp");
215 else sprintf(tempstr, "%s_%sQ%d_%dx%d.%s", filename, _subnames[jpegsub],
216 qual, tilesizex, tilesizey, useppm?"ppm":"bmp");
217 if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
218 {
219 printf("ERROR saving bitmap: %s\n", bmpgeterr());
220 goto bailout;
221 }
222 sprintf(strrchr(tempstr, '.'), "-err.%s", useppm?"ppm":"bmp");
223 if(!quiet)
224 printf("Computing compression error and saving to %s.\n", tempstr);
225 if(jpegsub==TJ_GRAYSCALE)
226 {
227 for(j=0; j<h; j++)
228 {
229 for(i=0; i<w*ps; i+=ps)
230 {
231 int y=(int)((double)srcbuf[w*ps*j+i+_rindex[pf]]*0.299
232 + (double)srcbuf[w*ps*j+i+_gindex[pf]]*0.587
233 + (double)srcbuf[w*ps*j+i+_bindex[pf]]*0.114 + 0.5);
234 if(y>255) y=255; if(y<0) y=0;
235 rgbbuf[pitch*j+i+_rindex[pf]]=abs(rgbbuf[pitch*j+i+_rindex[pf]]-y);
236 rgbbuf[pitch*j+i+_gindex[pf]]=abs(rgbbuf[pitch*j+i+_gindex[pf]]-y);
237 rgbbuf[pitch*j+i+_bindex[pf]]=abs(rgbbuf[pitch*j+i+_bindex[pf]]-y);
238 }
239 }
240 }
241 else
242 {
243 for(j=0; j<h; j++) for(i=0; i<w*ps; i++)
244 rgbbuf[pitch*j+i]=abs(rgbbuf[pitch*j+i]-srcbuf[w*ps*j+i]);
245 }
246 if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
247 {
248 printf("ERROR saving bitmap: %s\n", bmpgeterr());
249 goto bailout;
250 }
251
252 // Cleanup
253 if(jpegbuf)
254 {
255 for(i=0; i<numtilesx*numtilesy; i++)
256 {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;}
257 free(jpegbuf); jpegbuf=NULL;
258 }
259 if(comptilesize) {free(comptilesize); comptilesize=NULL;}
260 } while(tilesizex<w || tilesizey<h);
261
262 if(rgbbuf) {free(rgbbuf); rgbbuf=NULL;}
263 return;
264
265 bailout:
266 if(jpegbuf)
267 {
268 for(i=0; i<numtilesx*numtilesy; i++)
269 {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;}
270 free(jpegbuf); jpegbuf=NULL;
271 }
272 if(comptilesize) {free(comptilesize); comptilesize=NULL;}
273 if(rgbbuf) {free(rgbbuf); rgbbuf=NULL;}
274 return;
275}
276
277
278int main(int argc, char *argv[])
279{
280 unsigned char *bmpbuf=NULL; int w, h, i, useppm=0;
281 int qual, dotile=0, quiet=0, hiqual=-1; char *temp;
282 BMPPIXELFORMAT pf=BMP_BGR;
283 int bu=0;
284
285 printf("\n");
286
287 if(argc<3)
288 {
289 printf("USAGE: %s <Inputfile (BMP|PPM)> <%% Quality>\n\n", argv[0]);
290 printf(" [-tile]\n");
291 printf(" Test performance of the codec when the image is encoded\n");
292 printf(" as separate tiles of varying sizes.\n\n");
293 printf(" [-forcemmx] [-forcesse] [-forcesse2] [-forcesse3]\n");
294 printf(" Force MMX, SSE, or SSE2 code paths in Intel codec\n\n");
295 printf(" [-rgb | -bgr | -rgba | -bgra | -abgr | -argb]\n");
296 printf(" Test the specified color conversion path in the codec (default: BGR)\n\n");
297 printf(" [-quiet]\n");
298 printf(" Output in tabular rather than verbose format\n\n");
299 printf(" NOTE: If the quality is specified as a range, i.e. 90-100, a separate\n");
300 printf(" test will be performed for all quality values in the range.\n");
301 exit(1);
302 }
303 if((qual=atoi(argv[2]))<1 || qual>100)
304 {
305 puts("ERROR: Quality must be between 1 and 100.");
306 exit(1);
307 }
308 if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
309 && sscanf(&temp[1], "%d", &hiqual)==1 && hiqual>qual && hiqual>=1
310 && hiqual<=100) {}
311 else hiqual=qual;
312
313 if(argc>3)
314 {
315 for(i=3; i<argc; i++)
316 {
317 if(!stricmp(argv[i], "-tile")) dotile=1;
318 if(!stricmp(argv[i], "-forcesse3"))
319 {
320 printf("Using SSE3 code in Intel compressor\n");
321 forcesse3=1;
322 }
323 if(!stricmp(argv[i], "-forcesse2"))
324 {
325 printf("Using SSE2 code in Intel compressor\n");
326 forcesse2=1;
327 }
328 if(!stricmp(argv[i], "-forcesse"))
329 {
330 printf("Using SSE code in Intel compressor\n");
331 forcesse=1;
332 }
333 if(!stricmp(argv[i], "-forcemmx"))
334 {
335 printf("Using MMX code in Intel compressor\n");
336 forcemmx=1;
337 }
338 if(!stricmp(argv[i], "-rgb")) pf=BMP_RGB;
339 if(!stricmp(argv[i], "-rgba")) pf=BMP_RGBA;
340 if(!stricmp(argv[i], "-bgr")) pf=BMP_BGR;
341 if(!stricmp(argv[i], "-bgra")) pf=BMP_BGRA;
342 if(!stricmp(argv[i], "-abgr")) pf=BMP_ABGR;
343 if(!stricmp(argv[i], "-argb")) pf=BMP_ARGB;
344 if(!stricmp(argv[i], "-bottomup")) bu=1;
345 if(!stricmp(argv[i], "-quiet")) quiet=1;
346 }
347 }
348
349 if(loadbmp(argv[1], &bmpbuf, &w, &h, pf, 1, bu)==-1)
350 {
351 printf("ERROR loading bitmap: %s\n", bmpgeterr()); exit(1);
352 }
353
354 temp=strrchr(argv[1], '.');
355 if(temp!=NULL)
356 {
357 if(!stricmp(temp, ".ppm")) useppm=1;
358 *temp='\0';
359 }
360
361 if(quiet)
362 {
363 printf("All performance values in Mpixels/sec\n\n");
364 printf("Bitmap\tBitmap\tJPEG\tJPEG\tTile Size\tCompr\tCompr\tDecomp\n");
365 printf("Format\tOrder\tFormat\tQual\t X Y \tPerf \tRatio\tPerf\n\n");
366 }
367
368 for(i=hiqual; i>=qual; i--)
369 dotest(bmpbuf, w, h, pf, bu, TJ_GRAYSCALE, i, argv[1], dotile, useppm, quiet);
370 if(quiet) printf("\n");
371 for(i=hiqual; i>=qual; i--)
372 dotest(bmpbuf, w, h, pf, bu, TJ_411, i, argv[1], dotile, useppm, quiet);
373 if(quiet) printf("\n");
374 for(i=hiqual; i>=qual; i--)
375 dotest(bmpbuf, w, h, pf, bu, TJ_422, i, argv[1], dotile, useppm, quiet);
376 if(quiet) printf("\n");
377 for(i=hiqual; i>=qual; i--)
378 dotest(bmpbuf, w, h, pf, bu, TJ_444, i, argv[1], dotile, useppm, quiet);
379
380 if(bmpbuf) free(bmpbuf);
381 return 0;
382}