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