blob: d6c4cb343dc465d872af624f18eef4f890a35be3 [file] [log] [blame]
DRC2e7b76b2009-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
DRC61e51f92009-04-05 21:53:20 +000026int forcemmx=0, forcesse=0, forcesse2=0, forcesse3=0, fastupsample=0;
DRC2e7b76b2009-04-03 12:04:24 +000027const 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"};
DRC61e51f92009-04-05 21:53:20 +000034const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY"};
35const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY"};
DRC2e7b76b2009-04-03 12:04:24 +000036
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)
DRC61e51f92009-04-05 21:53:20 +000065 |(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0)
66 |(fastupsample?TJ_FASTUPSAMPLE:0);
DRC2e7b76b2009-04-03 12:04:24 +000067 int ps=_ps[pf];
68 int pitch=w*ps;
69
70 flags |= _flags[pf];
71 if(bu) flags |= TJ_BOTTOMUP;
72
73 if((rgbbuf=(unsigned char *)malloc(pitch*h)) == NULL)
74 {
75 puts("ERROR: Could not allocate image buffer.");
76 exit(1);
77 }
78
79 if(!quiet) printf("\n>>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", _pfname[pf],
80 bu?"Bottom-up":"Top-down", _subnamel[jpegsub], qual);
81 if(dotile) {tilesizex=tilesizey=4;} else {tilesizex=w; tilesizey=h;}
82
83 do
84 {
85 tilesizex*=2; if(tilesizex>w) tilesizex=w;
86 tilesizey*=2; if(tilesizey>h) tilesizey=h;
87 numtilesx=(w+tilesizex-1)/tilesizex;
88 numtilesy=(h+tilesizey-1)/tilesizey;
89 if((comptilesize=(unsigned long *)malloc(sizeof(unsigned long)*numtilesx*numtilesy)) == NULL
90 || (jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)*numtilesx*numtilesy)) == NULL)
91 {
92 puts("ERROR: Could not allocate image buffers.");
93 goto bailout;
94 }
95 memset(jpegbuf, 0, sizeof(unsigned char *)*numtilesx*numtilesy);
96 for(i=0; i<numtilesx*numtilesy; i++)
97 {
98 if((jpegbuf[i]=(unsigned char *)malloc(TJBUFSIZE(tilesizex, tilesizey))) == NULL)
99 {
100 puts("ERROR: Could not allocate image buffers.");
101 goto bailout;
102 }
103 }
104
105 // Compression test
106 if(quiet) printf("%s\t%s\t%s\t%d\t", _pfname[pf], bu?"BU":"TD",
107 _subnamel[jpegsub], qual);
108 for(i=0; i<h; i++) memcpy(&rgbbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
109 if((hnd=tjInitCompress())==NULL)
110 {
111 printf("Error in tjInitCompress():\n%s\n", tjGetErrorStr());
112 goto bailout;
113 }
114 _catch(tjCompress(hnd, rgbbuf, tilesizex, pitch, tilesizey, ps,
115 jpegbuf[0], &comptilesize[0], jpegsub, qual, flags));
116 ITER=0;
117 timer.start();
118 do
119 {
120 jpgbufsize=0; int tilen=0;
121 for(i=0; i<h; i+=tilesizey)
122 {
123 for(j=0; j<w; j+=tilesizex)
124 {
125 int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
126 _catch(tjCompress(hnd, &rgbbuf[pitch*i+j*ps], tempw, pitch,
127 temph, ps, jpegbuf[tilen], &comptilesize[tilen], jpegsub, qual,
128 flags));
129 jpgbufsize+=comptilesize[tilen];
130 tilen++;
131 }
132 }
133 ITER++;
134 } while((elapsed=timer.elapsed())<5.);
135 _catch(tjDestroy(hnd));
136 if(quiet)
137 {
138 if(tilesizex==w && tilesizey==h) printf("Full \t");
139 else printf("%-4d %-4d\t", tilesizex, tilesizey);
140 printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
141 printf("\t");
142 printsigfig((double)(w*h*ps)/(double)jpgbufsize, 4);
143 printf("\t");
144 }
145 else
146 {
147 if(tilesizex==w && tilesizey==h) printf("\nFull image\n");
148 else printf("\nTile size: %d x %d\n", tilesizex, tilesizey);
149 printf("C--> Frame rate: %f fps\n", (double)ITER/elapsed);
150 printf(" Output image size: %d bytes\n", jpgbufsize);
151 printf(" Compression ratio: %f:1\n",
152 (double)(w*h*ps)/(double)jpgbufsize);
153 printf(" Source throughput: %f Megapixels/sec\n",
154 (double)(w*h)/1000000.*(double)ITER/elapsed);
155 printf(" Output bit stream: %f Megabits/sec\n",
156 (double)jpgbufsize*8./1000000.*(double)ITER/elapsed);
157 }
158 if(tilesizex==w && tilesizey==h)
159 {
160 sprintf(tempstr, "%s_%sQ%d.jpg", filename, _subnames[jpegsub], qual);
161 if((outfile=fopen(tempstr, "wb"))==NULL)
162 {
163 puts("ERROR: Could not open reference image");
164 exit(1);
165 }
166 if(fwrite(jpegbuf[0], jpgbufsize, 1, outfile)!=1)
167 {
168 puts("ERROR: Could not write reference image");
169 exit(1);
170 }
171 fclose(outfile);
172 if(!quiet) printf("Reference image written to %s\n", tempstr);
173 }
174
175 // Decompression test
176 memset(rgbbuf, 127, pitch*h); // Grey image means decompressor did nothing
177 if((hnd=tjInitDecompress())==NULL)
178 {
179 printf("Error in tjInitDecompress():\n%s\n", tjGetErrorStr());
180 goto bailout;
181 }
182 _catch(tjDecompress(hnd, jpegbuf[0], jpgbufsize, rgbbuf, tilesizex, pitch,
183 tilesizey, ps, flags));
184 ITER=0;
185 timer.start();
186 do
187 {
188 int tilen=0;
189 for(i=0; i<h; i+=tilesizey)
190 {
191 for(j=0; j<w; j+=tilesizex)
192 {
193 int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
194 _catch(tjDecompress(hnd, jpegbuf[tilen], comptilesize[tilen],
195 &rgbbuf[pitch*i+ps*j], tempw, pitch, temph, ps, flags));
196 tilen++;
197 }
198 }
199 ITER++;
200 } while((elapsed=timer.elapsed())<5.);
201 _catch(tjDestroy(hnd));
202 if(quiet)
203 {
204 printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
205 printf("\n");
206 }
207 else
208 {
209 printf("D--> Frame rate: %f fps\n", (double)ITER/elapsed);
210 printf(" Dest. throughput: %f Megapixels/sec\n",
211 (double)(w*h)/1000000.*(double)ITER/elapsed);
212 }
213 if(tilesizex==w && tilesizey==h)
214 sprintf(tempstr, "%s_%sQ%d_full.%s", filename, _subnames[jpegsub], qual,
215 useppm?"ppm":"bmp");
216 else sprintf(tempstr, "%s_%sQ%d_%dx%d.%s", filename, _subnames[jpegsub],
217 qual, tilesizex, tilesizey, useppm?"ppm":"bmp");
218 if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
219 {
220 printf("ERROR saving bitmap: %s\n", bmpgeterr());
221 goto bailout;
222 }
223 sprintf(strrchr(tempstr, '.'), "-err.%s", useppm?"ppm":"bmp");
224 if(!quiet)
225 printf("Computing compression error and saving to %s.\n", tempstr);
226 if(jpegsub==TJ_GRAYSCALE)
227 {
228 for(j=0; j<h; j++)
229 {
230 for(i=0; i<w*ps; i+=ps)
231 {
232 int y=(int)((double)srcbuf[w*ps*j+i+_rindex[pf]]*0.299
233 + (double)srcbuf[w*ps*j+i+_gindex[pf]]*0.587
234 + (double)srcbuf[w*ps*j+i+_bindex[pf]]*0.114 + 0.5);
235 if(y>255) y=255; if(y<0) y=0;
236 rgbbuf[pitch*j+i+_rindex[pf]]=abs(rgbbuf[pitch*j+i+_rindex[pf]]-y);
237 rgbbuf[pitch*j+i+_gindex[pf]]=abs(rgbbuf[pitch*j+i+_gindex[pf]]-y);
238 rgbbuf[pitch*j+i+_bindex[pf]]=abs(rgbbuf[pitch*j+i+_bindex[pf]]-y);
239 }
240 }
241 }
242 else
243 {
244 for(j=0; j<h; j++) for(i=0; i<w*ps; i++)
245 rgbbuf[pitch*j+i]=abs(rgbbuf[pitch*j+i]-srcbuf[w*ps*j+i]);
246 }
247 if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
248 {
249 printf("ERROR saving bitmap: %s\n", bmpgeterr());
250 goto bailout;
251 }
252
253 // Cleanup
254 if(jpegbuf)
255 {
256 for(i=0; i<numtilesx*numtilesy; i++)
257 {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;}
258 free(jpegbuf); jpegbuf=NULL;
259 }
260 if(comptilesize) {free(comptilesize); comptilesize=NULL;}
261 } while(tilesizex<w || tilesizey<h);
262
263 if(rgbbuf) {free(rgbbuf); rgbbuf=NULL;}
264 return;
265
266 bailout:
267 if(jpegbuf)
268 {
269 for(i=0; i<numtilesx*numtilesy; i++)
270 {if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;}
271 free(jpegbuf); jpegbuf=NULL;
272 }
273 if(comptilesize) {free(comptilesize); comptilesize=NULL;}
274 if(rgbbuf) {free(rgbbuf); rgbbuf=NULL;}
275 return;
276}
277
278
279int main(int argc, char *argv[])
280{
281 unsigned char *bmpbuf=NULL; int w, h, i, useppm=0;
282 int qual, dotile=0, quiet=0, hiqual=-1; char *temp;
283 BMPPIXELFORMAT pf=BMP_BGR;
284 int bu=0;
285
286 printf("\n");
287
288 if(argc<3)
289 {
290 printf("USAGE: %s <Inputfile (BMP|PPM)> <%% Quality>\n\n", argv[0]);
291 printf(" [-tile]\n");
292 printf(" Test performance of the codec when the image is encoded\n");
293 printf(" as separate tiles of varying sizes.\n\n");
294 printf(" [-forcemmx] [-forcesse] [-forcesse2] [-forcesse3]\n");
295 printf(" Force MMX, SSE, or SSE2 code paths in Intel codec\n\n");
296 printf(" [-rgb | -bgr | -rgba | -bgra | -abgr | -argb]\n");
297 printf(" Test the specified color conversion path in the codec (default: BGR)\n\n");
DRC61e51f92009-04-05 21:53:20 +0000298 printf(" [-fastupsample]\n");
299 printf(" Use fast, inaccurate upsampling code to perform 4:2:2 and 4:2:0\n\n");
300 printf(" YUV decoding in libjpeg decompressor\n");
DRC2e7b76b2009-04-03 12:04:24 +0000301 printf(" [-quiet]\n");
302 printf(" Output in tabular rather than verbose format\n\n");
303 printf(" NOTE: If the quality is specified as a range, i.e. 90-100, a separate\n");
304 printf(" test will be performed for all quality values in the range.\n");
305 exit(1);
306 }
307 if((qual=atoi(argv[2]))<1 || qual>100)
308 {
309 puts("ERROR: Quality must be between 1 and 100.");
310 exit(1);
311 }
312 if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
313 && sscanf(&temp[1], "%d", &hiqual)==1 && hiqual>qual && hiqual>=1
314 && hiqual<=100) {}
315 else hiqual=qual;
316
317 if(argc>3)
318 {
319 for(i=3; i<argc; i++)
320 {
321 if(!stricmp(argv[i], "-tile")) dotile=1;
322 if(!stricmp(argv[i], "-forcesse3"))
323 {
324 printf("Using SSE3 code in Intel compressor\n");
325 forcesse3=1;
326 }
327 if(!stricmp(argv[i], "-forcesse2"))
328 {
329 printf("Using SSE2 code in Intel compressor\n");
330 forcesse2=1;
331 }
332 if(!stricmp(argv[i], "-forcesse"))
333 {
334 printf("Using SSE code in Intel compressor\n");
335 forcesse=1;
336 }
337 if(!stricmp(argv[i], "-forcemmx"))
338 {
339 printf("Using MMX code in Intel compressor\n");
340 forcemmx=1;
341 }
DRC61e51f92009-04-05 21:53:20 +0000342 if(!stricmp(argv[i], "-fastupsample"))
343 {
344 printf("Using fast upsampling code\n");
345 fastupsample=1;
346 }
DRC2e7b76b2009-04-03 12:04:24 +0000347 if(!stricmp(argv[i], "-rgb")) pf=BMP_RGB;
348 if(!stricmp(argv[i], "-rgba")) pf=BMP_RGBA;
349 if(!stricmp(argv[i], "-bgr")) pf=BMP_BGR;
350 if(!stricmp(argv[i], "-bgra")) pf=BMP_BGRA;
351 if(!stricmp(argv[i], "-abgr")) pf=BMP_ABGR;
352 if(!stricmp(argv[i], "-argb")) pf=BMP_ARGB;
353 if(!stricmp(argv[i], "-bottomup")) bu=1;
354 if(!stricmp(argv[i], "-quiet")) quiet=1;
355 }
356 }
357
358 if(loadbmp(argv[1], &bmpbuf, &w, &h, pf, 1, bu)==-1)
359 {
360 printf("ERROR loading bitmap: %s\n", bmpgeterr()); exit(1);
361 }
362
363 temp=strrchr(argv[1], '.');
364 if(temp!=NULL)
365 {
366 if(!stricmp(temp, ".ppm")) useppm=1;
367 *temp='\0';
368 }
369
370 if(quiet)
371 {
372 printf("All performance values in Mpixels/sec\n\n");
373 printf("Bitmap\tBitmap\tJPEG\tJPEG\tTile Size\tCompr\tCompr\tDecomp\n");
374 printf("Format\tOrder\tFormat\tQual\t X Y \tPerf \tRatio\tPerf\n\n");
375 }
376
377 for(i=hiqual; i>=qual; i--)
378 dotest(bmpbuf, w, h, pf, bu, TJ_GRAYSCALE, i, argv[1], dotile, useppm, quiet);
379 if(quiet) printf("\n");
380 for(i=hiqual; i>=qual; i--)
DRC61e51f92009-04-05 21:53:20 +0000381 dotest(bmpbuf, w, h, pf, bu, TJ_420, i, argv[1], dotile, useppm, quiet);
DRC2e7b76b2009-04-03 12:04:24 +0000382 if(quiet) printf("\n");
383 for(i=hiqual; i>=qual; i--)
384 dotest(bmpbuf, w, h, pf, bu, TJ_422, i, argv[1], dotile, useppm, quiet);
385 if(quiet) printf("\n");
386 for(i=hiqual; i>=qual; i--)
387 dotest(bmpbuf, w, h, pf, bu, TJ_444, i, argv[1], dotile, useppm, quiet);
388
389 if(bmpbuf) free(bmpbuf);
390 return 0;
391}