blob: f298732c19e6a91cb53bb0a59ca6ac86179235fa [file] [log] [blame]
DRCb8b359a2011-05-25 03:54:56 +00001/*
DRCe006f5c2012-01-28 06:31:44 +00002 * Copyright (C)2009-2012 D. R. Commander. All Rights Reserved.
DRCb8b359a2011-05-25 03:54:56 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <math.h>
33#include <errno.h>
34#include <cdjpeg.h>
35#include "./bmp.h"
36#include "./tjutil.h"
37#include "./turbojpeg.h"
38
39
40#define _throw(op, err) { \
41 printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
42 retval=-1; goto bailout;}
43#define _throwunix(m) _throw(m, strerror(errno))
44#define _throwtj(m) _throw(m, tjGetErrorStr())
45#define _throwbmp(m) _throw(m, bmpgeterr())
46
47enum {YUVENCODE=1, YUVDECODE};
48int flags=TJFLAG_NOREALLOC, decomponly=0, yuv=0, quiet=0, dotile=0,
49 pf=TJPF_BGR;
50char *ext="ppm";
51const char *pixFormatStr[TJ_NUMPF]=
52{
53 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY"
54};
55const char *subNameLong[TJ_NUMSAMP]=
56{
57 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0"
58};
59const char *subName[NUMSUBOPT]={"444", "422", "420", "GRAY", "440"};
60tjscalingfactor *scalingfactors=NULL, sf={1, 1}; int nsf=0;
61int xformop=TJXOP_NONE, xformopt=0;
DRCf5467112011-09-20 05:02:19 +000062int (*customFilter)(short *, tjregion, tjregion, int, int, tjtransform *);
DRCb8b359a2011-05-25 03:54:56 +000063double benchtime=5.0;
64
65
66char *sigfig(double val, int figs, char *buf, int len)
67{
68 char format[80];
69 int digitsafterdecimal=figs-(int)ceil(log10(fabs(val)));
70 if(digitsafterdecimal<1) snprintf(format, 80, "%%.0f");
71 else snprintf(format, 80, "%%.%df", digitsafterdecimal);
72 snprintf(buf, len, format, val);
73 return buf;
74}
75
76
DRC57a37362011-09-17 00:41:14 +000077/* Custom DCT filter which produces a negative of the image */
DRC7bf04d32011-09-17 00:18:31 +000078int dummyDCTFilter(short *coeffs, tjregion arrayRegion, tjregion planeRegion,
DRCf5467112011-09-20 05:02:19 +000079 int componentIndex, int transformIndex, tjtransform *transform)
DRC7bf04d32011-09-17 00:18:31 +000080{
81 int i;
82 for(i=0; i<arrayRegion.w*arrayRegion.h; i++) coeffs[i]=-coeffs[i];
83 return 0;
84}
85
DRC57a37362011-09-17 00:41:14 +000086
DRCb8b359a2011-05-25 03:54:56 +000087/* Decompression test */
88int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf,
89 unsigned long *jpegsize, unsigned char *dstbuf, int w, int h,
90 int subsamp, int jpegqual, char *filename, int tilew, int tileh)
91{
92 char tempstr[1024], sizestr[20]="\0", qualstr[6]="\0", *ptr;
93 FILE *file=NULL; tjhandle handle=NULL;
94 int row, col, i, dstbufalloc=0, retval=0;
95 double start, elapsed;
96 int ps=tjPixelSize[pf];
DRC9b49f0e2011-07-12 03:17:23 +000097 int yuvsize=tjBufSizeYUV(w, h, subsamp), bufsize;
DRCb8b359a2011-05-25 03:54:56 +000098 int scaledw=(yuv==YUVDECODE)? w : TJSCALED(w, sf);
99 int scaledh=(yuv==YUVDECODE)? h : TJSCALED(h, sf);
100 int pitch=scaledw*ps;
101 int ntilesw=(w+tilew-1)/tilew, ntilesh=(h+tileh-1)/tileh;
102 unsigned char *dstptr, *dstptr2;
103
104 if(jpegqual>0)
105 {
106 snprintf(qualstr, 6, "_Q%d", jpegqual);
107 qualstr[5]=0;
108 }
109
110 if((handle=tjInitDecompress())==NULL)
111 _throwtj("executing tjInitDecompress()");
112
DRCe006f5c2012-01-28 06:31:44 +0000113 bufsize=(yuv==YUVDECODE? yuvsize:pitch*scaledh);
DRCb8b359a2011-05-25 03:54:56 +0000114 if(dstbuf==NULL)
115 {
116 if((dstbuf=(unsigned char *)malloc(bufsize)) == NULL)
117 _throwunix("allocating image buffer");
118 dstbufalloc=1;
119 }
120 /* Set the destination buffer to gray so we know whether the decompressor
121 attempted to write to it */
122 memset(dstbuf, 127, bufsize);
123
124 /* Execute once to preload cache */
125 if(yuv==YUVDECODE)
126 {
127 if(tjDecompressToYUV(handle, jpegbuf[0], jpegsize[0], dstbuf, flags)==-1)
128 _throwtj("executing tjDecompressToYUV()");
129 }
130 else if(tjDecompress2(handle, jpegbuf[0], jpegsize[0], dstbuf, scaledw,
131 pitch, scaledh, pf, flags)==-1)
132 _throwtj("executing tjDecompress2()");
133
134 /* Benchmark */
135 for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++)
136 {
137 int tile=0;
138 if(yuv==YUVDECODE)
139 {
140 if(tjDecompressToYUV(handle, jpegbuf[0], jpegsize[0], dstbuf, flags)==-1)
141 _throwtj("executing tjDecompressToYUV()");
142 }
143 else for(row=0, dstptr=dstbuf; row<ntilesh; row++, dstptr+=pitch*tileh)
144 {
145 for(col=0, dstptr2=dstptr; col<ntilesw; col++, tile++, dstptr2+=ps*tilew)
146 {
147 int width=dotile? min(tilew, w-col*tilew):scaledw;
148 int height=dotile? min(tileh, h-row*tileh):scaledh;
149 if(tjDecompress2(handle, jpegbuf[tile], jpegsize[tile], dstptr2, width,
150 pitch, height, pf, flags)==-1)
151 _throwtj("executing tjDecompress2()");
152 }
153 }
154 }
155
156 if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
157 handle=NULL;
158
159 if(quiet)
160 {
161 printf("%s\n",
162 sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024));
163 }
164 else
165 {
166 printf("D--> Frame rate: %f fps\n", (double)i/elapsed);
167 printf(" Dest. throughput: %f Megapixels/sec\n",
168 (double)(w*h)/1000000.*(double)i/elapsed);
169 }
170 if(yuv==YUVDECODE)
171 {
172 snprintf(tempstr, 1024, "%s_%s%s.yuv", filename, subName[subsamp],
173 qualstr);
174 if((file=fopen(tempstr, "wb"))==NULL)
175 _throwunix("opening YUV image for output");
176 if(fwrite(dstbuf, yuvsize, 1, file)!=1)
177 _throwunix("writing YUV image");
178 fclose(file); file=NULL;
179 }
180 else
181 {
182 if(sf.num!=1 || sf.denom!=1)
183 snprintf(sizestr, 20, "%d_%d", sf.num, sf.denom);
184 else if(tilew!=w || tileh!=h)
185 snprintf(sizestr, 20, "%dx%d", tilew, tileh);
186 else snprintf(sizestr, 20, "full");
187 if(decomponly)
188 snprintf(tempstr, 1024, "%s_%s.%s", filename, sizestr, ext);
189 else
190 snprintf(tempstr, 1024, "%s_%s%s_%s.%s", filename, subName[subsamp],
191 qualstr, sizestr, ext);
192 if(savebmp(tempstr, dstbuf, scaledw, scaledh, pf,
193 (flags&TJFLAG_BOTTOMUP)!=0)==-1)
194 _throwbmp("saving bitmap");
195 ptr=strrchr(tempstr, '.');
196 snprintf(ptr, 1024-(ptr-tempstr), "-err.%s", ext);
197 if(srcbuf && sf.num==1 && sf.denom==1)
198 {
199 if(!quiet) printf("Compression error written to %s.\n", tempstr);
200 if(subsamp==TJ_GRAYSCALE)
201 {
202 int index, index2;
203 for(row=0, index=0; row<h; row++, index+=pitch)
204 {
205 for(col=0, index2=index; col<w; col++, index2+=ps)
206 {
207 int rindex=index2+tjRedOffset[pf];
208 int gindex=index2+tjGreenOffset[pf];
209 int bindex=index2+tjBlueOffset[pf];
210 int y=(int)((double)srcbuf[rindex]*0.299
211 + (double)srcbuf[gindex]*0.587
212 + (double)srcbuf[bindex]*0.114 + 0.5);
213 if(y>255) y=255; if(y<0) y=0;
214 dstbuf[rindex]=abs(dstbuf[rindex]-y);
215 dstbuf[gindex]=abs(dstbuf[gindex]-y);
216 dstbuf[bindex]=abs(dstbuf[bindex]-y);
217 }
218 }
219 }
220 else
221 {
222 for(row=0; row<h; row++)
223 for(col=0; col<w*ps; col++)
224 dstbuf[pitch*row+col]
225 =abs(dstbuf[pitch*row+col]-srcbuf[pitch*row+col]);
226 }
227 if(savebmp(tempstr, dstbuf, w, h, pf,
228 (flags&TJFLAG_BOTTOMUP)!=0)==-1)
229 _throwbmp("saving bitmap");
230 }
231 }
232
233 bailout:
234 if(file) {fclose(file); file=NULL;}
235 if(handle) {tjDestroy(handle); handle=NULL;}
236 if(dstbuf && dstbufalloc) {free(dstbuf); dstbuf=NULL;}
237 return retval;
238}
239
240
241void dotestyuv(unsigned char *srcbuf, int w, int h, int subsamp,
242 char *filename)
243{
244 char tempstr[1024], tempstr2[80];
245 FILE *file=NULL; tjhandle handle=NULL;
246 unsigned char *dstbuf=NULL;
247 double start, elapsed;
248 int i, retval=0, ps=tjPixelSize[pf];
249 int yuvsize=0;
250
DRC9b49f0e2011-07-12 03:17:23 +0000251 yuvsize=tjBufSizeYUV(w, h, subsamp);
DRCb8b359a2011-05-25 03:54:56 +0000252 if((dstbuf=(unsigned char *)malloc(yuvsize)) == NULL)
253 _throwunix("allocating image buffer");
254
255 if(!quiet)
256 printf(">>>>> %s (%s) <--> YUV %s <<<<<\n", pixFormatStr[pf],
257 (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp]);
258
259 if(quiet==1)
260 printf("%s\t%s\t%s\tN/A\t", pixFormatStr[pf],
261 (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp]);
262
263 if((handle=tjInitCompress())==NULL)
264 _throwtj("executing tjInitCompress()");
265
266 /* Execute once to preload cache */
267 if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1)
268 _throwtj("executing tjEncodeYUV2()");
269
270 /* Benchmark */
271 for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++)
272 {
273 if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1)
274 _throwtj("executing tjEncodeYUV2()");
275 }
276
277 if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
278 handle=NULL;
279
280 if(quiet==1) printf("%-4d %-4d\t", w, h);
281 if(quiet)
282 {
283 printf("%s%c%s%c",
284 sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024),
285 quiet==2? '\n':'\t',
286 sigfig((double)(w*h*ps)/(double)yuvsize, 4, tempstr2, 80),
287 quiet==2? '\n':'\t');
288 }
289 else
290 {
291 printf("\n%s size: %d x %d\n", "Image", w, h);
292 printf("C--> Frame rate: %f fps\n", (double)i/elapsed);
293 printf(" Output image size: %d bytes\n", yuvsize);
294 printf(" Compression ratio: %f:1\n",
295 (double)(w*h*ps)/(double)yuvsize);
296 printf(" Source throughput: %f Megapixels/sec\n",
297 (double)(w*h)/1000000.*(double)i/elapsed);
298 printf(" Output bit stream: %f Megabits/sec\n",
299 (double)yuvsize*8./1000000.*(double)i/elapsed);
300 }
301 snprintf(tempstr, 1024, "%s_%s.yuv", filename, subName[subsamp]);
302 if((file=fopen(tempstr, "wb"))==NULL)
303 _throwunix("opening reference image");
304 if(fwrite(dstbuf, yuvsize, 1, file)!=1)
305 _throwunix("writing reference image");
306 fclose(file); file=NULL;
307 if(!quiet) printf("Reference image written to %s\n", tempstr);
308
309 bailout:
310 if(file) {fclose(file); file=NULL;}
311 if(dstbuf) {free(dstbuf); dstbuf=NULL;}
312 if(handle) {tjDestroy(handle); handle=NULL;}
313 return;
314}
315
316
317void dotest(unsigned char *srcbuf, int w, int h, int subsamp, int jpegqual,
318 char *filename)
319{
320 char tempstr[1024], tempstr2[80];
321 FILE *file=NULL; tjhandle handle=NULL;
322 unsigned char **jpegbuf=NULL, *tmpbuf=NULL, *srcptr, *srcptr2;
323 double start, elapsed;
324 int totaljpegsize=0, row, col, i, tilew=w, tileh=h, retval=0;
325 unsigned long *jpegsize=NULL;
326 int ps=tjPixelSize[pf], ntilesw=1, ntilesh=1, pitch=w*ps;
327
328 if(yuv==YUVENCODE) {dotestyuv(srcbuf, w, h, subsamp, filename); return;}
329
330 if((tmpbuf=(unsigned char *)malloc(pitch*h)) == NULL)
331 _throwunix("allocating temporary image buffer");
332
333 if(!quiet)
334 printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pixFormatStr[pf],
335 (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp],
336 jpegqual);
337
338 for(tilew=dotile? 8:w, tileh=dotile? 8:h; ; tilew*=2, tileh*=2)
339 {
340 if(tilew>w) tilew=w; if(tileh>h) tileh=h;
341 ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
342
343 if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
344 *ntilesw*ntilesh))==NULL)
345 _throwunix("allocating JPEG tile array");
346 memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
347 if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
348 *ntilesw*ntilesh))==NULL)
349 _throwunix("allocating JPEG size array");
350 memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
351
352 if((flags&TJFLAG_NOREALLOC)!=0)
353 for(i=0; i<ntilesw*ntilesh; i++)
354 {
DRC9b49f0e2011-07-12 03:17:23 +0000355 if((jpegbuf[i]=(unsigned char *)malloc(tjBufSize(tilew, tileh,
356 subsamp)))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000357 _throwunix("allocating JPEG tiles");
358 }
359
360 /* Compression test */
361 if(quiet==1)
362 printf("%s\t%s\t%s\t%d\t", pixFormatStr[pf],
363 (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp], jpegqual);
364 for(i=0; i<h; i++)
365 memcpy(&tmpbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
366 if((handle=tjInitCompress())==NULL)
367 _throwtj("executing tjInitCompress()");
368
369 /* Execute once to preload cache */
370 if(tjCompress2(handle, srcbuf, tilew, pitch, tileh, pf, &jpegbuf[0],
371 &jpegsize[0], subsamp, jpegqual, flags)==-1)
372 _throwtj("executing tjCompress2()");
373
374 /* Benchmark */
375 for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++)
376 {
377 int tile=0;
378 totaljpegsize=0;
379 for(row=0, srcptr=srcbuf; row<ntilesh; row++, srcptr+=pitch*tileh)
380 {
381 for(col=0, srcptr2=srcptr; col<ntilesw; col++, tile++,
382 srcptr2+=ps*tilew)
383 {
384 int width=min(tilew, w-col*tilew);
385 int height=min(tileh, h-row*tileh);
386 if(tjCompress2(handle, srcptr2, width, pitch, height, pf,
387 &jpegbuf[tile], &jpegsize[tile], subsamp, jpegqual, flags)==-1)
388 _throwtj("executing tjCompress()2");
389 totaljpegsize+=jpegsize[tile];
390 }
391 }
392 }
393
394 if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
395 handle=NULL;
396
397 if(quiet==1) printf("%-4d %-4d\t", tilew, tileh);
398 if(quiet)
399 {
400 printf("%s%c%s%c",
401 sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024),
402 quiet==2? '\n':'\t',
403 sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
404 quiet==2? '\n':'\t');
405 }
406 else
407 {
408 printf("\n%s size: %d x %d\n", dotile? "Tile":"Image", tilew,
409 tileh);
410 printf("C--> Frame rate: %f fps\n", (double)i/elapsed);
411 printf(" Output image size: %d bytes\n", totaljpegsize);
412 printf(" Compression ratio: %f:1\n",
413 (double)(w*h*ps)/(double)totaljpegsize);
414 printf(" Source throughput: %f Megapixels/sec\n",
415 (double)(w*h)/1000000.*(double)i/elapsed);
416 printf(" Output bit stream: %f Megabits/sec\n",
417 (double)totaljpegsize*8./1000000.*(double)i/elapsed);
418 }
419 if(tilew==w && tileh==h)
420 {
421 snprintf(tempstr, 1024, "%s_%s_Q%d.jpg", filename, subName[subsamp],
422 jpegqual);
423 if((file=fopen(tempstr, "wb"))==NULL)
424 _throwunix("opening reference image");
425 if(fwrite(jpegbuf[0], jpegsize[0], 1, file)!=1)
426 _throwunix("writing reference image");
427 fclose(file); file=NULL;
428 if(!quiet) printf("Reference image written to %s\n", tempstr);
429 }
430
431 /* Decompression test */
432 if(decomptest(srcbuf, jpegbuf, jpegsize, tmpbuf, w, h, subsamp, jpegqual,
433 filename, tilew, tileh)==-1)
434 goto bailout;
435
436 for(i=0; i<ntilesw*ntilesh; i++)
437 {
438 if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;
439 }
440 free(jpegbuf); jpegbuf=NULL;
441 free(jpegsize); jpegsize=NULL;
442
443 if(tilew==w && tileh==h) break;
444 }
445
446 bailout:
447 if(file) {fclose(file); file=NULL;}
448 if(jpegbuf)
449 {
450 for(i=0; i<ntilesw*ntilesh; i++)
451 {
452 if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;
453 }
454 free(jpegbuf); jpegbuf=NULL;
455 }
456 if(jpegsize) {free(jpegsize); jpegsize=NULL;}
457 if(tmpbuf) {free(tmpbuf); tmpbuf=NULL;}
458 if(handle) {tjDestroy(handle); handle=NULL;}
459 return;
460}
461
462
463void dodecomptest(char *filename)
464{
465 FILE *file=NULL; tjhandle handle=NULL;
466 unsigned char **jpegbuf=NULL, *srcbuf=NULL;
467 unsigned long *jpegsize=NULL, srcsize, totaljpegsize;
468 tjtransform *t=NULL;
469 int w=0, h=0, subsamp=-1, _w, _h, _tilew, _tileh,
470 _ntilesw, _ntilesh, _subsamp;
471 char *temp=NULL, tempstr[80], tempstr2[80];
DRC20c7dbc2011-10-18 22:06:22 +0000472 int row, col, i, tilew, tileh, ntilesw=1, ntilesh=1, retval=0;
DRCb8b359a2011-05-25 03:54:56 +0000473 double start, elapsed;
474 int ps=tjPixelSize[pf], tile;
475
476 if((file=fopen(filename, "rb"))==NULL)
477 _throwunix("opening file");
478 if(fseek(file, 0, SEEK_END)<0 || (srcsize=ftell(file))<0)
479 _throwunix("determining file size");
480 if((srcbuf=(unsigned char *)malloc(srcsize))==NULL)
481 _throwunix("allocating memory");
482 if(fseek(file, 0, SEEK_SET)<0)
483 _throwunix("setting file position");
484 if(fread(srcbuf, srcsize, 1, file)<1)
485 _throwunix("reading JPEG data");
486 fclose(file); file=NULL;
487
488 temp=strrchr(filename, '.');
489 if(temp!=NULL) *temp='\0';
490
491 if((handle=tjInitTransform())==NULL)
492 _throwtj("executing tjInitTransform()");
493 if(tjDecompressHeader2(handle, srcbuf, srcsize, &w, &h, &subsamp)==-1)
494 _throwtj("executing tjDecompressHeader2()");
495
496 if(quiet==1)
497 {
498 printf("All performance values in Mpixels/sec\n\n");
499 printf("Bitmap\tBitmap\tJPEG\t%s %s \tXform\tComp\tDecomp\n",
500 dotile? "Tile ":"Image", dotile? "Tile ":"Image");
501 printf("Format\tOrder\tSubsamp\tWidth Height\tPerf \tRatio\tPerf\n\n");
502 }
503 else if(!quiet)
504 {
505 printf(">>>>> JPEG %s --> %s (%s) <<<<<\n", subNameLong[subsamp],
506 pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down");
507 }
508
509 for(tilew=dotile? 16:w, tileh=dotile? 16:h; ; tilew*=2, tileh*=2)
510 {
511 if(tilew>w) tilew=w; if(tileh>h) tileh=h;
512 ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
513
514 if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
515 *ntilesw*ntilesh))==NULL)
516 _throwunix("allocating JPEG tile array");
517 memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
518 if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
519 *ntilesw*ntilesh))==NULL)
520 _throwunix("allocating JPEG size array");
521 memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
522
523 if((flags&TJFLAG_NOREALLOC)!=0)
524 for(i=0; i<ntilesw*ntilesh; i++)
525 {
DRC9b49f0e2011-07-12 03:17:23 +0000526 if((jpegbuf[i]=(unsigned char *)malloc(tjBufSize(tilew, tileh,
527 subsamp)))==NULL)
DRCb8b359a2011-05-25 03:54:56 +0000528 _throwunix("allocating JPEG tiles");
529 }
530
531 _w=w; _h=h; _tilew=tilew; _tileh=tileh;
532 if(!quiet)
533 {
534 printf("\n%s size: %d x %d", dotile? "Tile":"Image", _tilew,
535 _tileh);
536 if(sf.num!=1 || sf.denom!=1)
537 printf(" --> %d x %d", TJSCALED(_w, sf), TJSCALED(_h, sf));
538 printf("\n");
539 }
540 else if(quiet==1)
541 {
542 printf("%s\t%s\t%s\t", pixFormatStr[pf],
543 (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp]);
544 printf("%-4d %-4d\t", tilew, tileh);
545 }
546
547 _subsamp=subsamp;
DRC7bf04d32011-09-17 00:18:31 +0000548 if(dotile || xformop!=TJXOP_NONE || xformopt!=0 || customFilter)
DRCb8b359a2011-05-25 03:54:56 +0000549 {
550 if((t=(tjtransform *)malloc(sizeof(tjtransform)*ntilesw*ntilesh))
551 ==NULL)
552 _throwunix("allocating image transform array");
553
554 if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE
555 || xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270)
556 {
557 _w=h; _h=w; _tilew=tileh; _tileh=tilew;
558 }
559
560 if(xformopt&TJXOPT_GRAY) _subsamp=TJ_GRAYSCALE;
561 if(xformop==TJXOP_HFLIP || xformop==TJXOP_ROT180)
562 _w=_w-(_w%tjMCUWidth[_subsamp]);
563 if(xformop==TJXOP_VFLIP || xformop==TJXOP_ROT180)
564 _h=_h-(_h%tjMCUHeight[_subsamp]);
565 if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT90)
566 _w=_w-(_w%tjMCUHeight[_subsamp]);
567 if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT270)
568 _h=_h-(_h%tjMCUWidth[_subsamp]);
569 _ntilesw=(_w+_tilew-1)/_tilew;
570 _ntilesh=(_h+_tileh-1)/_tileh;
571
572 for(row=0, tile=0; row<_ntilesh; row++)
573 {
574 for(col=0; col<_ntilesw; col++, tile++)
575 {
576 t[tile].r.w=min(_tilew, _w-col*_tilew);
577 t[tile].r.h=min(_tileh, _h-row*_tileh);
578 t[tile].r.x=col*_tilew;
579 t[tile].r.y=row*_tileh;
580 t[tile].op=xformop;
581 t[tile].options=xformopt|TJXOPT_TRIM;
DRC7bf04d32011-09-17 00:18:31 +0000582 t[tile].customFilter=customFilter;
583 if(t[tile].options&TJXOPT_NOOUTPUT && jpegbuf[tile])
584 {
585 free(jpegbuf[tile]); jpegbuf[tile]=NULL;
586 }
DRCb8b359a2011-05-25 03:54:56 +0000587 }
588 }
589
590 start=gettime();
591 if(tjTransform(handle, srcbuf, srcsize, _ntilesw*_ntilesh, jpegbuf,
592 jpegsize, t, flags)==-1)
593 _throwtj("executing tjTransform()");
594 elapsed=gettime()-start;
595
596 free(t); t=NULL;
597
598 for(tile=0, totaljpegsize=0; tile<_ntilesw*_ntilesh; tile++)
599 totaljpegsize+=jpegsize[tile];
600
601 if(quiet)
602 {
603 printf("%s%c%s%c",
604 sigfig((double)(w*h)/1000000./elapsed, 4, tempstr, 80),
605 quiet==2? '\n':'\t',
606 sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
607 quiet==2? '\n':'\t');
608 }
609 else if(!quiet)
610 {
611 printf("X--> Frame rate: %f fps\n", 1.0/elapsed);
612 printf(" Output image size: %lu bytes\n", totaljpegsize);
613 printf(" Compression ratio: %f:1\n",
614 (double)(w*h*ps)/(double)totaljpegsize);
615 printf(" Source throughput: %f Megapixels/sec\n",
616 (double)(w*h)/1000000./elapsed);
617 printf(" Output bit stream: %f Megabits/sec\n",
618 (double)totaljpegsize*8./1000000./elapsed);
619 }
620 }
621 else
622 {
623 if(quiet==1) printf("N/A\tN/A\t");
624 jpegsize[0]=srcsize;
625 memcpy(jpegbuf[0], srcbuf, srcsize);
626 }
627
628 if(w==tilew) _tilew=_w;
629 if(h==tileh) _tileh=_h;
DRC7bf04d32011-09-17 00:18:31 +0000630 if(!(xformopt&TJXOPT_NOOUTPUT))
631 {
632 if(decomptest(NULL, jpegbuf, jpegsize, NULL, _w, _h, _subsamp, 0,
633 filename, _tilew, _tileh)==-1)
634 goto bailout;
635 }
636 else if(quiet==1) printf("N/A\n");
DRCb8b359a2011-05-25 03:54:56 +0000637
638 for(i=0; i<ntilesw*ntilesh; i++)
639 {
640 free(jpegbuf[i]); jpegbuf[i]=NULL;
641 }
642 free(jpegbuf); jpegbuf=NULL;
643 if(jpegsize) {free(jpegsize); jpegsize=NULL;}
644
645 if(tilew==w && tileh==h) break;
646 }
647
648 bailout:
649 if(file) {fclose(file); file=NULL;}
650 if(jpegbuf)
651 {
652 for(i=0; i<ntilesw*ntilesh; i++)
653 {
654 if(jpegbuf[i]) free(jpegbuf[i]); jpegbuf[i]=NULL;
655 }
656 free(jpegbuf); jpegbuf=NULL;
657 }
658 if(jpegsize) {free(jpegsize); jpegsize=NULL;}
659 if(srcbuf) {free(srcbuf); srcbuf=NULL;}
660 if(t) {free(t); t=NULL;}
661 if(handle) {tjDestroy(handle); handle=NULL;}
662 return;
663}
664
665
666void usage(char *progname)
667{
668 int i;
669 printf("USAGE: %s\n", progname);
670 printf(" <Inputfile (BMP|PPM)> <%% Quality> [options]\n\n");
671 printf(" %s\n", progname);
672 printf(" <Inputfile (JPG)> [options]\n\n");
673 printf("Options:\n\n");
674 printf("-alloc = Dynamically allocate JPEG image buffers\n");
675 printf("-bmp = Generate output images in Windows Bitmap format (default=PPM)\n");
676 printf("-bottomup = Test bottom-up compression/decompression\n");
677 printf("-tile = Test performance of the codec when the image is encoded as separate\n");
678 printf(" tiles of varying sizes.\n");
679 printf("-forcemmx, -forcesse, -forcesse2, -forcesse3 =\n");
680 printf(" Force MMX, SSE, SSE2, or SSE3 code paths in the underlying codec\n");
681 printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
682 printf(" Test the specified color conversion path in the codec (default: BGR)\n");
683 printf("-fastupsample = Use fast, inaccurate upsampling code to perform 4:2:2 and 4:2:0\n");
684 printf(" YUV decoding in libjpeg decompressor\n");
685 printf("-quiet = Output results in tabular rather than verbose format\n");
686 printf("-yuvencode = Encode RGB input as planar YUV rather than compressing as JPEG\n");
687 printf("-yuvdecode = Decode JPEG image to planar YUV rather than RGB\n");
688 printf("-scale M/N = scale down the width/height of the decompressed JPEG image by a\n");
689 printf(" factor of M/N (M/N = ");
690 for(i=0; i<nsf; i++)
691 {
692 printf("%d/%d", scalingfactors[i].num, scalingfactors[i].denom);
693 if(nsf==2 && i!=nsf-1) printf(" or ");
694 else if(nsf>2)
695 {
696 if(i!=nsf-1) printf(", ");
697 if(i==nsf-2) printf("or ");
698 }
699 }
700 printf(")\n");
701 printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
702 printf(" Perform the corresponding lossless transform prior to\n");
703 printf(" decompression (these options are mutually exclusive)\n");
704 printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
705 printf(" test (can be combined with the other transforms above)\n");
706 printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n\n");
707 printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
708 printf("test will be performed for all quality values in the range.\n\n");
709 exit(1);
710}
711
712
713int main(int argc, char *argv[])
714{
715 unsigned char *srcbuf=NULL; int w, h, i, j;
716 int minqual=-1, maxqual=-1; char *temp;
717 int minarg=2; int retval=0;
718
719 if((scalingfactors=tjGetScalingFactors(&nsf))==NULL || nsf==0)
720 _throwtj("executing tjGetScalingFactors()");
721
722 if(argc<minarg) usage(argv[0]);
723
724 temp=strrchr(argv[1], '.');
725 if(temp!=NULL)
726 {
727 if(!strcasecmp(temp, ".bmp")) ext="bmp";
728 if(!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg")) decomponly=1;
729 }
730
731 printf("\n");
732
733 if(argc>minarg)
734 {
735 for(i=minarg; i<argc; i++)
736 {
737 if(!strcasecmp(argv[i], "-yuvencode"))
738 {
739 printf("Testing YUV planar encoding\n\n");
740 yuv=YUVENCODE; maxqual=minqual=100;
741 }
742 if(!strcasecmp(argv[i], "-yuvdecode"))
743 {
744 printf("Testing YUV planar decoding\n\n");
745 yuv=YUVDECODE;
746 }
747 }
748 }
749
750 if(!decomponly && yuv!=YUVENCODE)
751 {
752 minarg=3;
753 if(argc<minarg) usage(argv[0]);
754 if((minqual=atoi(argv[2]))<1 || minqual>100)
755 {
756 puts("ERROR: Quality must be between 1 and 100.");
757 exit(1);
758 }
759 if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
760 && sscanf(&temp[1], "%d", &maxqual)==1 && maxqual>minqual && maxqual>=1
761 && maxqual<=100) {}
762 else maxqual=minqual;
763 }
764
765 if(argc>minarg)
766 {
767 for(i=minarg; i<argc; i++)
768 {
769 if(!strcasecmp(argv[i], "-tile"))
770 {
771 dotile=1; xformopt|=TJXOPT_CROP;
772 }
773 if(!strcasecmp(argv[i], "-forcesse3"))
774 {
775 printf("Forcing SSE3 code\n\n");
776 flags|=TJFLAG_FORCESSE3;
777 }
778 if(!strcasecmp(argv[i], "-forcesse2"))
779 {
780 printf("Forcing SSE2 code\n\n");
781 flags|=TJFLAG_FORCESSE2;
782 }
783 if(!strcasecmp(argv[i], "-forcesse"))
784 {
785 printf("Forcing SSE code\n\n");
786 flags|=TJFLAG_FORCESSE;
787 }
788 if(!strcasecmp(argv[i], "-forcemmx"))
789 {
790 printf("Forcing MMX code\n\n");
791 flags|=TJFLAG_FORCEMMX;
792 }
793 if(!strcasecmp(argv[i], "-fastupsample"))
794 {
795 printf("Using fast upsampling code\n\n");
796 flags|=TJFLAG_FASTUPSAMPLE;
797 }
798 if(!strcasecmp(argv[i], "-rgb")) pf=TJPF_RGB;
799 if(!strcasecmp(argv[i], "-rgbx")) pf=TJPF_RGBX;
800 if(!strcasecmp(argv[i], "-bgr")) pf=TJPF_BGR;
801 if(!strcasecmp(argv[i], "-bgrx")) pf=TJPF_BGRX;
802 if(!strcasecmp(argv[i], "-xbgr")) pf=TJPF_XBGR;
803 if(!strcasecmp(argv[i], "-xrgb")) pf=TJPF_XRGB;
804 if(!strcasecmp(argv[i], "-bottomup")) flags|=TJFLAG_BOTTOMUP;
805 if(!strcasecmp(argv[i], "-quiet")) quiet=1;
806 if(!strcasecmp(argv[i], "-qq")) quiet=2;
807 if(!strcasecmp(argv[i], "-scale") && i<argc-1)
808 {
809 int temp1=0, temp2=0, match=0;
810 if(sscanf(argv[++i], "%d/%d", &temp1, &temp2)==2)
811 {
812 for(j=0; j<nsf; j++)
813 {
814 if(temp1==scalingfactors[j].num && temp2==scalingfactors[j].denom)
815 {
816 sf=scalingfactors[j];
817 match=1; break;
818 }
819 }
820 if(!match) usage(argv[0]);
821 }
822 else usage(argv[0]);
823 }
824 if(!strcasecmp(argv[i], "-hflip")) xformop=TJXOP_HFLIP;
825 if(!strcasecmp(argv[i], "-vflip")) xformop=TJXOP_VFLIP;
826 if(!strcasecmp(argv[i], "-transpose")) xformop=TJXOP_TRANSPOSE;
827 if(!strcasecmp(argv[i], "-transverse")) xformop=TJXOP_TRANSVERSE;
828 if(!strcasecmp(argv[i], "-rot90")) xformop=TJXOP_ROT90;
829 if(!strcasecmp(argv[i], "-rot180")) xformop=TJXOP_ROT180;
830 if(!strcasecmp(argv[i], "-rot270")) xformop=TJXOP_ROT270;
831 if(!strcasecmp(argv[i], "-grayscale")) xformopt|=TJXOPT_GRAY;
DRC7bf04d32011-09-17 00:18:31 +0000832 if(!strcasecmp(argv[i], "-custom")) customFilter=dummyDCTFilter;
833 if(!strcasecmp(argv[i], "-nooutput")) xformopt|=TJXOPT_NOOUTPUT;
DRCb8b359a2011-05-25 03:54:56 +0000834 if(!strcasecmp(argv[i], "-benchtime") && i<argc-1)
835 {
836 double temp=atof(argv[++i]);
837 if(temp>0.0) benchtime=temp;
838 else usage(argv[0]);
839 }
840 if(!strcmp(argv[i], "-?")) usage(argv[0]);
841 if(!strcasecmp(argv[i], "-alloc")) flags&=(~TJFLAG_NOREALLOC);
DRC94f0e032011-05-25 04:35:53 +0000842 if(!strcasecmp(argv[i], "-bmp")) ext="bmp";
DRCb8b359a2011-05-25 03:54:56 +0000843 }
844 }
845
846 if((sf.num!=1 || sf.denom!=1) && dotile)
847 {
848 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
849 printf("work when scaled decompression is enabled.\n");
850 dotile=0;
851 }
852
853 if(yuv && dotile)
854 {
855 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
856 printf("work when YUV encoding or decoding is enabled.\n\n");
857 dotile=0;
858 }
859
860 if(!decomponly)
861 {
862 if(loadbmp(argv[1], &srcbuf, &w, &h, pf, (flags&TJFLAG_BOTTOMUP)!=0)==-1)
863 _throwbmp("loading bitmap");
864 temp=strrchr(argv[1], '.');
865 if(temp!=NULL) *temp='\0';
866 }
867
868 if(quiet==1 && !decomponly)
869 {
870 printf("All performance values in Mpixels/sec\n\n");
871 printf("Bitmap\tBitmap\tJPEG\tJPEG\t%s %s \tComp\tComp\tDecomp\n",
872 dotile? "Tile ":"Image", dotile? "Tile ":"Image");
873 printf("Format\tOrder\tSubsamp\tQual\tWidth Height\tPerf \tRatio\tPerf\n\n");
874 }
875
876 if(decomponly)
877 {
878 dodecomptest(argv[1]);
879 printf("\n");
880 goto bailout;
881 }
882 for(i=maxqual; i>=minqual; i--)
883 dotest(srcbuf, w, h, TJ_GRAYSCALE, i, argv[1]);
884 printf("\n");
885 for(i=maxqual; i>=minqual; i--)
886 dotest(srcbuf, w, h, TJ_420, i, argv[1]);
887 printf("\n");
888 for(i=maxqual; i>=minqual; i--)
889 dotest(srcbuf, w, h, TJ_422, i, argv[1]);
890 printf("\n");
891 for(i=maxqual; i>=minqual; i--)
892 dotest(srcbuf, w, h, TJ_444, i, argv[1]);
893 printf("\n");
894
895 bailout:
896 if(srcbuf) free(srcbuf);
897 return retval;
898}