1 /*
2 * Copyright (C)2009-2017 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 <ctype.h>
33 #include <math.h>
34 #include <errno.h>
35 #include <cdjpeg.h>
36 #include "./bmp.h"
37 #include "./tjutil.h"
38 #include "./turbojpeg.h"
39
40
41 #define _throw(op, err) { \
42 printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err); \
43 retval=-1; goto bailout;}
44 #define _throwunix(m) _throw(m, strerror(errno))
45 #define _throwtj(m) _throw(m, tjGetErrorStr())
46 #define _throwbmp(m) _throw(m, bmpgeterr())
47
48 int flags=TJFLAG_NOREALLOC, componly=0, decomponly=0, doyuv=0, quiet=0,
49 dotile=0, pf=TJPF_BGR, yuvpad=1, dowrite=1;
50 char *ext="ppm";
51 const char *pixFormatStr[TJ_NUMPF]=
52 {
53 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "GRAY", "", "", "", "", "CMYK"
54 };
55 const char *subNameLong[TJ_NUMSAMP]=
56 {
57 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
58 };
59 const char *csName[TJ_NUMCS]=
60 {
61 "RGB", "YCbCr", "GRAY", "CMYK", "YCCK"
62 };
63 const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440", "411"};
64 tjscalingfactor *scalingfactors=NULL, sf={1, 1}; int nsf=0;
65 int xformop=TJXOP_NONE, xformopt=0;
66 int (*customFilter)(short *, tjregion, tjregion, int, int, tjtransform *);
67 double benchtime=5.0, warmup=1.0;
68
69
formatName(int subsamp,int cs,char * buf)70 char *formatName(int subsamp, int cs, char *buf)
71 {
72 if(cs==TJCS_YCbCr) return (char *)subNameLong[subsamp];
73 else if(cs==TJCS_YCCK)
74 {
75 snprintf(buf, 80, "%s %s", csName[cs], subNameLong[subsamp]);
76 return buf;
77 }
78 else return (char *)csName[cs];
79 }
80
81
sigfig(double val,int figs,char * buf,int len)82 char *sigfig(double val, int figs, char *buf, int len)
83 {
84 char format[80];
85 int digitsafterdecimal=figs-(int)ceil(log10(fabs(val)));
86 if(digitsafterdecimal<1) snprintf(format, 80, "%%.0f");
87 else snprintf(format, 80, "%%.%df", digitsafterdecimal);
88 snprintf(buf, len, format, val);
89 return buf;
90 }
91
92
93 /* Custom DCT filter which produces a negative of the image */
dummyDCTFilter(short * coeffs,tjregion arrayRegion,tjregion planeRegion,int componentIndex,int transformIndex,tjtransform * transform)94 int dummyDCTFilter(short *coeffs, tjregion arrayRegion, tjregion planeRegion,
95 int componentIndex, int transformIndex, tjtransform *transform)
96 {
97 int i;
98 for(i=0; i<arrayRegion.w*arrayRegion.h; i++) coeffs[i]=-coeffs[i];
99 return 0;
100 }
101
102
103 /* Decompression test */
decomp(unsigned char * srcbuf,unsigned char ** jpegbuf,unsigned long * jpegsize,unsigned char * dstbuf,int w,int h,int subsamp,int jpegqual,char * filename,int tilew,int tileh)104 int decomp(unsigned char *srcbuf, unsigned char **jpegbuf,
105 unsigned long *jpegsize, unsigned char *dstbuf, int w, int h,
106 int subsamp, int jpegqual, char *filename, int tilew, int tileh)
107 {
108 char tempstr[1024], sizestr[20]="\0", qualstr[6]="\0", *ptr;
109 FILE *file=NULL; tjhandle handle=NULL;
110 int row, col, iter=0, dstbufalloc=0, retval=0;
111 double elapsed, elapsedDecode;
112 int ps=tjPixelSize[pf];
113 int scaledw=TJSCALED(w, sf);
114 int scaledh=TJSCALED(h, sf);
115 int pitch=scaledw*ps;
116 int ntilesw=(w+tilew-1)/tilew, ntilesh=(h+tileh-1)/tileh;
117 unsigned char *dstptr, *dstptr2, *yuvbuf=NULL;
118
119 if(jpegqual>0)
120 {
121 snprintf(qualstr, 6, "_Q%d", jpegqual);
122 qualstr[5]=0;
123 }
124
125 if((handle=tjInitDecompress())==NULL)
126 _throwtj("executing tjInitDecompress()");
127
128 if(dstbuf==NULL)
129 {
130 if ((unsigned long long)pitch * (unsigned long long)scaledh >
131 (unsigned long long)((size_t)-1))
132 _throw("allocating destination buffer", "Image is too large");
133 if((dstbuf=(unsigned char *)malloc((size_t)pitch*scaledh))==NULL)
134 _throwunix("allocating destination buffer");
135 dstbufalloc=1;
136 }
137 /* Set the destination buffer to gray so we know whether the decompressor
138 attempted to write to it */
139 memset(dstbuf, 127, pitch*scaledh);
140
141 if(doyuv)
142 {
143 int width=dotile? tilew:scaledw;
144 int height=dotile? tileh:scaledh;
145 unsigned long yuvsize=tjBufSizeYUV2(width, yuvpad, height, subsamp);
146
147 if (yuvsize == (unsigned long)-1)
148 _throwtj("allocating YUV buffer");
149 if((yuvbuf=(unsigned char *)malloc(yuvsize))==NULL)
150 _throwunix("allocating YUV buffer");
151 memset(yuvbuf, 127, yuvsize);
152 }
153
154 /* Benchmark */
155 iter=-1;
156 elapsed=elapsedDecode=0.;
157 while(1)
158 {
159 int tile=0;
160 double start=gettime();
161 for(row=0, dstptr=dstbuf; row<ntilesh; row++, dstptr+=pitch*tileh)
162 {
163 for(col=0, dstptr2=dstptr; col<ntilesw; col++, tile++, dstptr2+=ps*tilew)
164 {
165 int width=dotile? min(tilew, w-col*tilew):scaledw;
166 int height=dotile? min(tileh, h-row*tileh):scaledh;
167 if(doyuv)
168 {
169 double startDecode;
170 if(tjDecompressToYUV2(handle, jpegbuf[tile], jpegsize[tile], yuvbuf,
171 width, yuvpad, height, flags)==-1)
172 _throwtj("executing tjDecompressToYUV2()");
173 startDecode=gettime();
174 if(tjDecodeYUV(handle, yuvbuf, yuvpad, subsamp, dstptr2, width,
175 pitch, height, pf, flags)==-1)
176 _throwtj("executing tjDecodeYUV()");
177 if(iter>=0) elapsedDecode+=gettime()-startDecode;
178 }
179 else
180 if(tjDecompress2(handle, jpegbuf[tile], jpegsize[tile], dstptr2,
181 width, pitch, height, pf, flags)==-1)
182 _throwtj("executing tjDecompress2()");
183 }
184 }
185 elapsed+=gettime()-start;
186 if(iter>=0)
187 {
188 iter++;
189 if(elapsed>=benchtime) break;
190 }
191 else if(elapsed>=warmup)
192 {
193 iter=0;
194 elapsed=elapsedDecode=0.;
195 }
196 }
197 if(doyuv) elapsed-=elapsedDecode;
198
199 if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
200 handle=NULL;
201
202 if(quiet)
203 {
204 printf("%-6s%s",
205 sigfig((double)(w*h)/1000000.*(double)iter/elapsed, 4, tempstr, 1024),
206 quiet==2? "\n":" ");
207 if(doyuv)
208 printf("%s\n",
209 sigfig((double)(w*h)/1000000.*(double)iter/elapsedDecode, 4, tempstr,
210 1024));
211 else if(quiet!=2) printf("\n");
212 }
213 else
214 {
215 printf("%s --> Frame rate: %f fps\n",
216 doyuv? "Decomp to YUV":"Decompress ", (double)iter/elapsed);
217 printf(" Throughput: %f Megapixels/sec\n",
218 (double)(w*h)/1000000.*(double)iter/elapsed);
219 if(doyuv)
220 {
221 printf("YUV Decode --> Frame rate: %f fps\n",
222 (double)iter/elapsedDecode);
223 printf(" Throughput: %f Megapixels/sec\n",
224 (double)(w*h)/1000000.*(double)iter/elapsedDecode);
225 }
226 }
227
228 if (!dowrite) goto bailout;
229
230 if(sf.num!=1 || sf.denom!=1)
231 snprintf(sizestr, 20, "%d_%d", sf.num, sf.denom);
232 else if(tilew!=w || tileh!=h)
233 snprintf(sizestr, 20, "%dx%d", tilew, tileh);
234 else snprintf(sizestr, 20, "full");
235 if(decomponly)
236 snprintf(tempstr, 1024, "%s_%s.%s", filename, sizestr, ext);
237 else
238 snprintf(tempstr, 1024, "%s_%s%s_%s.%s", filename, subName[subsamp],
239 qualstr, sizestr, ext);
240
241 if(savebmp(tempstr, dstbuf, scaledw, scaledh, pf,
242 (flags&TJFLAG_BOTTOMUP)!=0)==-1)
243 _throwbmp("saving bitmap");
244 ptr=strrchr(tempstr, '.');
245 snprintf(ptr, 1024-(ptr-tempstr), "-err.%s", ext);
246 if(srcbuf && sf.num==1 && sf.denom==1)
247 {
248 if(!quiet) printf("Compression error written to %s.\n", tempstr);
249 if(subsamp==TJ_GRAYSCALE)
250 {
251 unsigned long index, index2;
252 for(row=0, index=0; row<h; row++, index+=pitch)
253 {
254 for(col=0, index2=index; col<w; col++, index2+=ps)
255 {
256 unsigned long rindex=index2+tjRedOffset[pf];
257 unsigned long gindex=index2+tjGreenOffset[pf];
258 unsigned long bindex=index2+tjBlueOffset[pf];
259 int y=(int)((double)srcbuf[rindex]*0.299
260 + (double)srcbuf[gindex]*0.587
261 + (double)srcbuf[bindex]*0.114 + 0.5);
262 if(y>255) y=255;
263 if(y<0) y=0;
264 dstbuf[rindex]=abs(dstbuf[rindex]-y);
265 dstbuf[gindex]=abs(dstbuf[gindex]-y);
266 dstbuf[bindex]=abs(dstbuf[bindex]-y);
267 }
268 }
269 }
270 else
271 {
272 for(row=0; row<h; row++)
273 for(col=0; col<w*ps; col++)
274 dstbuf[pitch*row+col]
275 =abs(dstbuf[pitch*row+col]-srcbuf[pitch*row+col]);
276 }
277 if(savebmp(tempstr, dstbuf, w, h, pf,
278 (flags&TJFLAG_BOTTOMUP)!=0)==-1)
279 _throwbmp("saving bitmap");
280 }
281
282 bailout:
283 if(file) fclose(file);
284 if(handle) tjDestroy(handle);
285 if(dstbuf && dstbufalloc) free(dstbuf);
286 if(yuvbuf) free(yuvbuf);
287 return retval;
288 }
289
290
fullTest(unsigned char * srcbuf,int w,int h,int subsamp,int jpegqual,char * filename)291 int fullTest(unsigned char *srcbuf, int w, int h, int subsamp, int jpegqual,
292 char *filename)
293 {
294 char tempstr[1024], tempstr2[80];
295 FILE *file=NULL; tjhandle handle=NULL;
296 unsigned char **jpegbuf=NULL, *yuvbuf=NULL, *tmpbuf=NULL, *srcptr, *srcptr2;
297 double start, elapsed, elapsedEncode;
298 int totaljpegsize=0, row, col, i, tilew=w, tileh=h, retval=0;
299 int iter;
300 unsigned long *jpegsize=NULL, yuvsize=0;
301 int ps=tjPixelSize[pf];
302 int ntilesw=1, ntilesh=1, pitch=w*ps;
303 const char *pfStr=pixFormatStr[pf];
304
305 if ((unsigned long long)pitch * (unsigned long long)h >
306 (unsigned long long)((size_t)-1))
307 _throw("allocating temporary image buffer", "Image is too large");
308 if((tmpbuf=(unsigned char *)malloc((size_t)pitch*h)) == NULL)
309 _throwunix("allocating temporary image buffer");
310
311 if(!quiet)
312 printf(">>>>> %s (%s) <--> JPEG %s Q%d <<<<<\n", pfStr,
313 (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp],
314 jpegqual);
315
316 for(tilew=dotile? 8:w, tileh=dotile? 8:h; ; tilew*=2, tileh*=2)
317 {
318 if(tilew>w) tilew=w;
319 if(tileh>h) tileh=h;
320 ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
321
322 if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
323 *ntilesw*ntilesh))==NULL)
324 _throwunix("allocating JPEG tile array");
325 memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
326 if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
327 *ntilesw*ntilesh))==NULL)
328 _throwunix("allocating JPEG size array");
329 memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
330
331 if((flags&TJFLAG_NOREALLOC)!=0)
332 for(i=0; i<ntilesw*ntilesh; i++)
333 {
334 if (tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
335 _throw("getting buffer size", "Image is too large");
336 if((jpegbuf[i]=(unsigned char *)tjAlloc(tjBufSize(tilew, tileh,
337 subsamp)))==NULL)
338 _throwunix("allocating JPEG tiles");
339 }
340
341 /* Compression test */
342 if(quiet==1)
343 printf("%-4s (%s) %-5s %-3d ", pfStr,
344 (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp], jpegqual);
345 for(i=0; i<h; i++)
346 memcpy(&tmpbuf[pitch*i], &srcbuf[w*ps*i], w*ps);
347 if((handle=tjInitCompress())==NULL)
348 _throwtj("executing tjInitCompress()");
349
350 if(doyuv)
351 {
352 yuvsize=tjBufSizeYUV2(tilew, yuvpad, tileh, subsamp);
353 if (yuvsize == (unsigned long)-1)
354 _throw("allocating YUV buffer", "Image too large");
355 if((yuvbuf=(unsigned char *)malloc(yuvsize))==NULL)
356 _throwunix("allocating YUV buffer");
357 memset(yuvbuf, 127, yuvsize);
358 }
359
360 /* Benchmark */
361 iter=-1;
362 elapsed=elapsedEncode=0.;
363 while(1)
364 {
365 int tile=0;
366 totaljpegsize=0;
367 start=gettime();
368 for(row=0, srcptr=srcbuf; row<ntilesh; row++, srcptr+=pitch*tileh)
369 {
370 for(col=0, srcptr2=srcptr; col<ntilesw; col++, tile++,
371 srcptr2+=ps*tilew)
372 {
373 int width=min(tilew, w-col*tilew);
374 int height=min(tileh, h-row*tileh);
375 if(doyuv)
376 {
377 double startEncode=gettime();
378 if(tjEncodeYUV3(handle, srcptr2, width, pitch, height, pf, yuvbuf,
379 yuvpad, subsamp, flags)==-1)
380 _throwtj("executing tjEncodeYUV3()");
381 if(iter>=0) elapsedEncode+=gettime()-startEncode;
382 if(tjCompressFromYUV(handle, yuvbuf, width, yuvpad, height,
383 subsamp, &jpegbuf[tile], &jpegsize[tile], jpegqual, flags)==-1)
384 _throwtj("executing tjCompressFromYUV()");
385 }
386 else
387 {
388 if(tjCompress2(handle, srcptr2, width, pitch, height, pf,
389 &jpegbuf[tile], &jpegsize[tile], subsamp, jpegqual, flags)==-1)
390 _throwtj("executing tjCompress2()");
391 }
392 totaljpegsize+=jpegsize[tile];
393 }
394 }
395 elapsed+=gettime()-start;
396 if(iter>=0)
397 {
398 iter++;
399 if(elapsed>=benchtime) break;
400 }
401 else if(elapsed>=warmup)
402 {
403 iter=0;
404 elapsed=elapsedEncode=0.;
405 }
406 }
407 if(doyuv) elapsed-=elapsedEncode;
408
409 if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()");
410 handle=NULL;
411
412 if(quiet==1) printf("%-5d %-5d ", tilew, tileh);
413 if(quiet)
414 {
415 if(doyuv)
416 printf("%-6s%s",
417 sigfig((double)(w*h)/1000000.*(double)iter/elapsedEncode, 4, tempstr,
418 1024), quiet==2? "\n":" ");
419 printf("%-6s%s",
420 sigfig((double)(w*h)/1000000.*(double)iter/elapsed, 4, tempstr, 1024),
421 quiet==2? "\n":" ");
422 printf("%-6s%s",
423 sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
424 quiet==2? "\n":" ");
425 }
426 else
427 {
428 printf("\n%s size: %d x %d\n", dotile? "Tile":"Image", tilew,
429 tileh);
430 if(doyuv)
431 {
432 printf("Encode YUV --> Frame rate: %f fps\n",
433 (double)iter/elapsedEncode);
434 printf(" Output image size: %lu bytes\n", yuvsize);
435 printf(" Compression ratio: %f:1\n",
436 (double)(w*h*ps)/(double)yuvsize);
437 printf(" Throughput: %f Megapixels/sec\n",
438 (double)(w*h)/1000000.*(double)iter/elapsedEncode);
439 printf(" Output bit stream: %f Megabits/sec\n",
440 (double)yuvsize*8./1000000.*(double)iter/elapsedEncode);
441 }
442 printf("%s --> Frame rate: %f fps\n",
443 doyuv? "Comp from YUV":"Compress ", (double)iter/elapsed);
444 printf(" Output image size: %d bytes\n",
445 totaljpegsize);
446 printf(" Compression ratio: %f:1\n",
447 (double)(w*h*ps)/(double)totaljpegsize);
448 printf(" Throughput: %f Megapixels/sec\n",
449 (double)(w*h)/1000000.*(double)iter/elapsed);
450 printf(" Output bit stream: %f Megabits/sec\n",
451 (double)totaljpegsize*8./1000000.*(double)iter/elapsed);
452 }
453 if(tilew==w && tileh==h && dowrite)
454 {
455 snprintf(tempstr, 1024, "%s_%s_Q%d.jpg", filename, subName[subsamp],
456 jpegqual);
457 if((file=fopen(tempstr, "wb"))==NULL)
458 _throwunix("opening reference image");
459 if(fwrite(jpegbuf[0], jpegsize[0], 1, file)!=1)
460 _throwunix("writing reference image");
461 fclose(file); file=NULL;
462 if(!quiet) printf("Reference image written to %s\n", tempstr);
463 }
464
465 /* Decompression test */
466 if(!componly)
467 {
468 if(decomp(srcbuf, jpegbuf, jpegsize, tmpbuf, w, h, subsamp, jpegqual,
469 filename, tilew, tileh)==-1)
470 goto bailout;
471 }
472
473 for(i=0; i<ntilesw*ntilesh; i++)
474 {
475 if(jpegbuf[i]) tjFree(jpegbuf[i]);
476 jpegbuf[i]=NULL;
477 }
478 free(jpegbuf); jpegbuf=NULL;
479 if(doyuv)
480 {
481 free(yuvbuf); yuvbuf=NULL;
482 }
483
484 if(tilew==w && tileh==h) break;
485 }
486
487 bailout:
488 if(file) {fclose(file); file=NULL;}
489 if(jpegbuf)
490 {
491 for(i=0; i<ntilesw*ntilesh; i++)
492 {
493 if(jpegbuf[i]) tjFree(jpegbuf[i]);
494 jpegbuf[i]=NULL;
495 }
496 free(jpegbuf); jpegbuf=NULL;
497 }
498 if(yuvbuf) {free(yuvbuf); yuvbuf=NULL;}
499 if(jpegsize) {free(jpegsize); jpegsize=NULL;}
500 if(tmpbuf) {free(tmpbuf); tmpbuf=NULL;}
501 if(handle) {tjDestroy(handle); handle=NULL;}
502 return retval;
503 }
504
505
decompTest(char * filename)506 int decompTest(char *filename)
507 {
508 FILE *file=NULL; tjhandle handle=NULL;
509 unsigned char **jpegbuf=NULL, *srcbuf=NULL;
510 unsigned long *jpegsize=NULL, srcsize, totaljpegsize;
511 tjtransform *t=NULL;
512 int w=0, h=0, subsamp=-1, cs=-1, _w, _h, _tilew, _tileh,
513 _ntilesw, _ntilesh, _subsamp;
514 char *temp=NULL, tempstr[80], tempstr2[80];
515 int row, col, i, iter, tilew, tileh, ntilesw=1, ntilesh=1, retval=0;
516 double start, elapsed;
517 int ps=tjPixelSize[pf], tile, decompsrc=0;
518
519 if((file=fopen(filename, "rb"))==NULL)
520 _throwunix("opening file");
521 if(fseek(file, 0, SEEK_END)<0 || (srcsize=ftell(file))==(unsigned long)-1)
522 _throwunix("determining file size");
523 if((srcbuf=(unsigned char *)malloc(srcsize))==NULL)
524 _throwunix("allocating memory");
525 if(fseek(file, 0, SEEK_SET)<0)
526 _throwunix("setting file position");
527 if(fread(srcbuf, srcsize, 1, file)<1)
528 _throwunix("reading JPEG data");
529 fclose(file); file=NULL;
530
531 temp=strrchr(filename, '.');
532 if(temp!=NULL) *temp='\0';
533
534 if((handle=tjInitTransform())==NULL)
535 _throwtj("executing tjInitTransform()");
536 if(tjDecompressHeader3(handle, srcbuf, srcsize, &w, &h, &subsamp, &cs)==-1)
537 _throwtj("executing tjDecompressHeader3()");
538 if(cs==TJCS_YCCK || cs==TJCS_CMYK)
539 {
540 pf=TJPF_CMYK; ps=tjPixelSize[pf];
541 }
542
543 if(quiet==1)
544 {
545 printf("All performance values in Mpixels/sec\n\n");
546 printf("Bitmap JPEG JPEG %s %s Xform Comp Decomp ",
547 dotile? "Tile ":"Image", dotile? "Tile ":"Image");
548 if(doyuv) printf("Decode");
549 printf("\n");
550 printf("Format CS Subsamp Width Height Perf Ratio Perf ");
551 if(doyuv) printf("Perf");
552 printf("\n\n");
553 }
554 else if(!quiet)
555 printf(">>>>> JPEG %s --> %s (%s) <<<<<\n",
556 formatName(subsamp, cs, tempstr), pixFormatStr[pf],
557 (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down");
558
559 for(tilew=dotile? 16:w, tileh=dotile? 16:h; ; tilew*=2, tileh*=2)
560 {
561 if(tilew>w) tilew=w;
562 if(tileh>h) tileh=h;
563 ntilesw=(w+tilew-1)/tilew; ntilesh=(h+tileh-1)/tileh;
564
565 if((jpegbuf=(unsigned char **)malloc(sizeof(unsigned char *)
566 *ntilesw*ntilesh))==NULL)
567 _throwunix("allocating JPEG tile array");
568 memset(jpegbuf, 0, sizeof(unsigned char *)*ntilesw*ntilesh);
569 if((jpegsize=(unsigned long *)malloc(sizeof(unsigned long)
570 *ntilesw*ntilesh))==NULL)
571 _throwunix("allocating JPEG size array");
572 memset(jpegsize, 0, sizeof(unsigned long)*ntilesw*ntilesh);
573
574 if((flags&TJFLAG_NOREALLOC)!=0 &&
575 (dotile || xformop != TJXOP_NONE || xformopt != 0 || customFilter))
576 for(i=0; i<ntilesw*ntilesh; i++)
577 {
578 if(tjBufSize(tilew, tileh, subsamp) > (unsigned long)INT_MAX)
579 _throw("getting buffer size", "Image is too large");
580 if((jpegbuf[i]=(unsigned char *)tjAlloc(tjBufSize(tilew, tileh,
581 subsamp)))==NULL)
582 _throwunix("allocating JPEG tiles");
583 }
584
585 _w=w; _h=h; _tilew=tilew; _tileh=tileh;
586 if(!quiet)
587 {
588 printf("\n%s size: %d x %d", dotile? "Tile":"Image", _tilew,
589 _tileh);
590 if(sf.num!=1 || sf.denom!=1)
591 printf(" --> %d x %d", TJSCALED(_w, sf), TJSCALED(_h, sf));
592 printf("\n");
593 }
594 else if(quiet==1)
595 {
596 printf("%-4s (%s) %-5s %-5s ", pixFormatStr[pf],
597 (flags&TJFLAG_BOTTOMUP)? "BU":"TD", csName[cs], subNameLong[subsamp]);
598 printf("%-5d %-5d ", tilew, tileh);
599 }
600
601 _subsamp=subsamp;
602 if(dotile || xformop!=TJXOP_NONE || xformopt!=0 || customFilter)
603 {
604 if((t=(tjtransform *)malloc(sizeof(tjtransform)*ntilesw*ntilesh))
605 ==NULL)
606 _throwunix("allocating image transform array");
607
608 if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE
609 || xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270)
610 {
611 _w=h; _h=w; _tilew=tileh; _tileh=tilew;
612 }
613
614 if(xformopt&TJXOPT_GRAY) _subsamp=TJ_GRAYSCALE;
615 if(xformop==TJXOP_HFLIP || xformop==TJXOP_ROT180)
616 _w=_w-(_w%tjMCUWidth[_subsamp]);
617 if(xformop==TJXOP_VFLIP || xformop==TJXOP_ROT180)
618 _h=_h-(_h%tjMCUHeight[_subsamp]);
619 if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT90)
620 _w=_w-(_w%tjMCUHeight[_subsamp]);
621 if(xformop==TJXOP_TRANSVERSE || xformop==TJXOP_ROT270)
622 _h=_h-(_h%tjMCUWidth[_subsamp]);
623 _ntilesw=(_w+_tilew-1)/_tilew;
624 _ntilesh=(_h+_tileh-1)/_tileh;
625
626 if(xformop==TJXOP_TRANSPOSE || xformop==TJXOP_TRANSVERSE
627 || xformop==TJXOP_ROT90 || xformop==TJXOP_ROT270)
628 {
629 if(_subsamp==TJSAMP_422) _subsamp=TJSAMP_440;
630 else if(_subsamp==TJSAMP_440) _subsamp=TJSAMP_422;
631 }
632
633 for(row=0, tile=0; row<_ntilesh; row++)
634 {
635 for(col=0; col<_ntilesw; col++, tile++)
636 {
637 t[tile].r.w=min(_tilew, _w-col*_tilew);
638 t[tile].r.h=min(_tileh, _h-row*_tileh);
639 t[tile].r.x=col*_tilew;
640 t[tile].r.y=row*_tileh;
641 t[tile].op=xformop;
642 t[tile].options=xformopt|TJXOPT_TRIM;
643 t[tile].customFilter=customFilter;
644 if(t[tile].options&TJXOPT_NOOUTPUT && jpegbuf[tile])
645 {
646 tjFree(jpegbuf[tile]); jpegbuf[tile]=NULL;
647 }
648 }
649 }
650
651 iter=-1;
652 elapsed=0.;
653 while(1)
654 {
655 start=gettime();
656 if(tjTransform(handle, srcbuf, srcsize, _ntilesw*_ntilesh, jpegbuf,
657 jpegsize, t, flags)==-1)
658 _throwtj("executing tjTransform()");
659 elapsed+=gettime()-start;
660 if(iter>=0)
661 {
662 iter++;
663 if(elapsed>=benchtime) break;
664 }
665 else if(elapsed>=warmup)
666 {
667 iter=0;
668 elapsed=0.;
669 }
670 }
671
672 free(t); t=NULL;
673
674 for(tile=0, totaljpegsize=0; tile<_ntilesw*_ntilesh; tile++)
675 totaljpegsize+=jpegsize[tile];
676
677 if(quiet)
678 {
679 printf("%-6s%s%-6s%s",
680 sigfig((double)(w*h)/1000000./elapsed, 4, tempstr, 80),
681 quiet==2? "\n":" ",
682 sigfig((double)(w*h*ps)/(double)totaljpegsize, 4, tempstr2, 80),
683 quiet==2? "\n":" ");
684 }
685 else if(!quiet)
686 {
687 printf("Transform --> Frame rate: %f fps\n", 1.0/elapsed);
688 printf(" Output image size: %lu bytes\n", totaljpegsize);
689 printf(" Compression ratio: %f:1\n",
690 (double)(w*h*ps)/(double)totaljpegsize);
691 printf(" Throughput: %f Megapixels/sec\n",
692 (double)(w*h)/1000000./elapsed);
693 printf(" Output bit stream: %f Megabits/sec\n",
694 (double)totaljpegsize*8./1000000./elapsed);
695 }
696 }
697 else
698 {
699 if(quiet==1) printf("N/A N/A ");
700 tjFree(jpegbuf[0]);
701 jpegbuf[0]=NULL;
702 decompsrc=1;
703 }
704
705 if(w==tilew) _tilew=_w;
706 if(h==tileh) _tileh=_h;
707 if(!(xformopt&TJXOPT_NOOUTPUT))
708 {
709 if(decomp(NULL, decompsrc? &srcbuf:jpegbuf, decompsrc? &srcsize:jpegsize,
710 NULL, _w, _h, _subsamp, 0, filename, _tilew, _tileh)==-1)
711 goto bailout;
712 }
713 else if(quiet==1) printf("N/A\n");
714
715 for(i=0; i<ntilesw*ntilesh; i++)
716 {
717 if (jpegbuf[i]) tjFree(jpegbuf[i]);
718 jpegbuf[i]=NULL;
719 }
720 free(jpegbuf); jpegbuf=NULL;
721 if(jpegsize) {free(jpegsize); jpegsize=NULL;}
722
723 if(tilew==w && tileh==h) break;
724 }
725
726 bailout:
727 if(file) {fclose(file); file=NULL;}
728 if(jpegbuf)
729 {
730 for(i=0; i<ntilesw*ntilesh; i++)
731 {
732 if(jpegbuf[i]) tjFree(jpegbuf[i]);
733 jpegbuf[i]=NULL;
734 }
735 free(jpegbuf); jpegbuf=NULL;
736 }
737 if(jpegsize) {free(jpegsize); jpegsize=NULL;}
738 if(srcbuf) {free(srcbuf); srcbuf=NULL;}
739 if(t) {free(t); t=NULL;}
740 if(handle) {tjDestroy(handle); handle=NULL;}
741 return retval;
742 }
743
744
usage(char * progname)745 void usage(char *progname)
746 {
747 int i;
748 printf("USAGE: %s\n", progname);
749 printf(" <Inputfile (BMP|PPM)> <Quality> [options]\n\n");
750 printf(" %s\n", progname);
751 printf(" <Inputfile (JPG)> [options]\n\n");
752 printf("Options:\n\n");
753 printf("-alloc = Dynamically allocate JPEG image buffers\n");
754 printf("-bmp = Generate output images in Windows Bitmap format (default = PPM)\n");
755 printf("-bottomup = Test bottom-up compression/decompression\n");
756 printf("-tile = Test performance of the codec when the image is encoded as separate\n");
757 printf(" tiles of varying sizes.\n");
758 printf("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb =\n");
759 printf(" Test the specified color conversion path in the codec (default = BGR)\n");
760 printf("-cmyk = Indirectly test YCCK JPEG compression/decompression (the source\n");
761 printf(" and destination bitmaps are still RGB. The conversion is done\n");
762 printf(" internally prior to compression or after decompression.)\n");
763 printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n");
764 printf(" the underlying codec\n");
765 printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n");
766 printf(" codec\n");
767 printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n");
768 printf(" underlying codec\n");
769 printf("-subsamp <s> = When testing JPEG compression, this option specifies the level\n");
770 printf(" of chrominance subsampling to use (<s> = 444, 422, 440, 420, 411, or\n");
771 printf(" GRAY). The default is to test Grayscale, 4:2:0, 4:2:2, and 4:4:4 in\n");
772 printf(" sequence.\n");
773 printf("-quiet = Output results in tabular rather than verbose format\n");
774 printf("-yuv = Test YUV encoding/decoding functions\n");
775 printf("-yuvpad <p> = If testing YUV encoding/decoding, this specifies the number of\n");
776 printf(" bytes to which each row of each plane in the intermediate YUV image is\n");
777 printf(" padded (default = 1)\n");
778 printf("-scale M/N = Scale down the width/height of the decompressed JPEG image by a\n");
779 printf(" factor of M/N (M/N = ");
780 for(i=0; i<nsf; i++)
781 {
782 printf("%d/%d", scalingfactors[i].num, scalingfactors[i].denom);
783 if(nsf==2 && i!=nsf-1) printf(" or ");
784 else if(nsf>2)
785 {
786 if(i!=nsf-1) printf(", ");
787 if(i==nsf-2) printf("or ");
788 }
789 if(i%8==0 && i!=0) printf("\n ");
790 }
791 printf(")\n");
792 printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n");
793 printf(" Perform the corresponding lossless transform prior to\n");
794 printf(" decompression (these options are mutually exclusive)\n");
795 printf("-grayscale = Perform lossless grayscale conversion prior to decompression\n");
796 printf(" test (can be combined with the other transforms above)\n");
797 printf("-benchtime <t> = Run each benchmark for at least <t> seconds (default = 5.0)\n");
798 printf("-warmup <t> = Run each benchmark for <t> seconds (default = 1.0) prior to\n");
799 printf(" starting the timer, in order to prime the caches and thus improve the\n");
800 printf(" consistency of the results.\n");
801 printf("-componly = Stop after running compression tests. Do not test decompression.\n");
802 printf("-nowrite = Do not write reference or output images (improves consistency of\n");
803 printf(" performance measurements.)\n\n");
804 printf("NOTE: If the quality is specified as a range (e.g. 90-100), a separate\n");
805 printf("test will be performed for all quality values in the range.\n\n");
806 exit(1);
807 }
808
809
main(int argc,char * argv[])810 int main(int argc, char *argv[])
811 {
812 unsigned char *srcbuf=NULL; int w=0, h=0, i, j;
813 int minqual=-1, maxqual=-1; char *temp;
814 int minarg=2, retval=0, subsamp=-1;
815
816 if((scalingfactors=tjGetScalingFactors(&nsf))==NULL || nsf==0)
817 _throwtj("executing tjGetScalingFactors()");
818
819 if(argc<minarg) usage(argv[0]);
820
821 temp=strrchr(argv[1], '.');
822 if(temp!=NULL)
823 {
824 if(!strcasecmp(temp, ".bmp")) ext="bmp";
825 if(!strcasecmp(temp, ".jpg") || !strcasecmp(temp, ".jpeg")) decomponly=1;
826 }
827
828 printf("\n");
829
830 if(!decomponly)
831 {
832 minarg=3;
833 if(argc<minarg) usage(argv[0]);
834 if((minqual=atoi(argv[2]))<1 || minqual>100)
835 {
836 puts("ERROR: Quality must be between 1 and 100.");
837 exit(1);
838 }
839 if((temp=strchr(argv[2], '-'))!=NULL && strlen(temp)>1
840 && sscanf(&temp[1], "%d", &maxqual)==1 && maxqual>minqual && maxqual>=1
841 && maxqual<=100) {}
842 else maxqual=minqual;
843 }
844
845 if(argc>minarg)
846 {
847 for(i=minarg; i<argc; i++)
848 {
849 if(!strcasecmp(argv[i], "-tile"))
850 {
851 dotile=1; xformopt|=TJXOPT_CROP;
852 }
853 else if(!strcasecmp(argv[i], "-fastupsample"))
854 {
855 printf("Using fast upsampling code\n\n");
856 flags|=TJFLAG_FASTUPSAMPLE;
857 }
858 else if(!strcasecmp(argv[i], "-fastdct"))
859 {
860 printf("Using fastest DCT/IDCT algorithm\n\n");
861 flags|=TJFLAG_FASTDCT;
862 }
863 else if(!strcasecmp(argv[i], "-accuratedct"))
864 {
865 printf("Using most accurate DCT/IDCT algorithm\n\n");
866 flags|=TJFLAG_ACCURATEDCT;
867 }
868 else if(!strcasecmp(argv[i], "-rgb")) pf=TJPF_RGB;
869 else if(!strcasecmp(argv[i], "-rgbx")) pf=TJPF_RGBX;
870 else if(!strcasecmp(argv[i], "-bgr")) pf=TJPF_BGR;
871 else if(!strcasecmp(argv[i], "-bgrx")) pf=TJPF_BGRX;
872 else if(!strcasecmp(argv[i], "-xbgr")) pf=TJPF_XBGR;
873 else if(!strcasecmp(argv[i], "-xrgb")) pf=TJPF_XRGB;
874 else if(!strcasecmp(argv[i], "-cmyk")) pf=TJPF_CMYK;
875 else if(!strcasecmp(argv[i], "-bottomup")) flags|=TJFLAG_BOTTOMUP;
876 else if(!strcasecmp(argv[i], "-quiet")) quiet=1;
877 else if(!strcasecmp(argv[i], "-qq")) quiet=2;
878 else if(!strcasecmp(argv[i], "-scale") && i<argc-1)
879 {
880 int temp1=0, temp2=0, match=0;
881 if(sscanf(argv[++i], "%d/%d", &temp1, &temp2)==2)
882 {
883 for(j=0; j<nsf; j++)
884 {
885 if((double)temp1/(double)temp2
886 == (double)scalingfactors[j].num/(double)scalingfactors[j].denom)
887 {
888 sf=scalingfactors[j];
889 match=1; break;
890 }
891 }
892 if(!match) usage(argv[0]);
893 }
894 else usage(argv[0]);
895 }
896 else if(!strcasecmp(argv[i], "-hflip")) xformop=TJXOP_HFLIP;
897 else if(!strcasecmp(argv[i], "-vflip")) xformop=TJXOP_VFLIP;
898 else if(!strcasecmp(argv[i], "-transpose")) xformop=TJXOP_TRANSPOSE;
899 else if(!strcasecmp(argv[i], "-transverse")) xformop=TJXOP_TRANSVERSE;
900 else if(!strcasecmp(argv[i], "-rot90")) xformop=TJXOP_ROT90;
901 else if(!strcasecmp(argv[i], "-rot180")) xformop=TJXOP_ROT180;
902 else if(!strcasecmp(argv[i], "-rot270")) xformop=TJXOP_ROT270;
903 else if(!strcasecmp(argv[i], "-grayscale")) xformopt|=TJXOPT_GRAY;
904 else if(!strcasecmp(argv[i], "-custom")) customFilter=dummyDCTFilter;
905 else if(!strcasecmp(argv[i], "-nooutput")) xformopt|=TJXOPT_NOOUTPUT;
906 else if(!strcasecmp(argv[i], "-benchtime") && i<argc-1)
907 {
908 double temp=atof(argv[++i]);
909 if(temp>0.0) benchtime=temp;
910 else usage(argv[0]);
911 }
912 else if(!strcasecmp(argv[i], "-warmup") && i<argc-1)
913 {
914 double temp=atof(argv[++i]);
915 if(temp>=0.0) warmup=temp;
916 else usage(argv[0]);
917 printf("Warmup time = %.1f seconds\n\n", warmup);
918 }
919 else if(!strcasecmp(argv[i], "-alloc")) flags&=(~TJFLAG_NOREALLOC);
920 else if(!strcasecmp(argv[i], "-bmp")) ext="bmp";
921 else if(!strcasecmp(argv[i], "-yuv"))
922 {
923 printf("Testing YUV planar encoding/decoding\n\n");
924 doyuv=1;
925 }
926 else if(!strcasecmp(argv[i], "-yuvpad") && i<argc-1)
927 {
928 int temp=atoi(argv[++i]);
929 if(temp>=1) yuvpad=temp;
930 }
931 else if(!strcasecmp(argv[i], "-subsamp") && i<argc-1)
932 {
933 i++;
934 if(toupper(argv[i][0])=='G') subsamp=TJSAMP_GRAY;
935 else
936 {
937 int temp=atoi(argv[i]);
938 switch(temp)
939 {
940 case 444: subsamp=TJSAMP_444; break;
941 case 422: subsamp=TJSAMP_422; break;
942 case 440: subsamp=TJSAMP_440; break;
943 case 420: subsamp=TJSAMP_420; break;
944 case 411: subsamp=TJSAMP_411; break;
945 }
946 }
947 }
948 else if(!strcasecmp(argv[i], "-componly")) componly=1;
949 else if(!strcasecmp(argv[i], "-nowrite")) dowrite=0;
950 else usage(argv[0]);
951 }
952 }
953
954 if((sf.num!=1 || sf.denom!=1) && dotile)
955 {
956 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
957 printf("work when scaled decompression is enabled.\n");
958 dotile=0;
959 }
960
961 if((flags&TJFLAG_NOREALLOC)==0 && dotile)
962 {
963 printf("Disabling tiled compression/decompression tests, because those tests do not\n");
964 printf("work when dynamic JPEG buffer allocation is enabled.\n\n");
965 dotile=0;
966 }
967
968 if(!decomponly)
969 {
970 if(loadbmp(argv[1], &srcbuf, &w, &h, pf, (flags&TJFLAG_BOTTOMUP)!=0)==-1)
971 _throwbmp("loading bitmap");
972 temp=strrchr(argv[1], '.');
973 if(temp!=NULL) *temp='\0';
974 }
975
976 if(quiet==1 && !decomponly)
977 {
978 printf("All performance values in Mpixels/sec\n\n");
979 printf("Bitmap JPEG JPEG %s %s ",
980 dotile? "Tile ":"Image", dotile? "Tile ":"Image");
981 if(doyuv) printf("Encode ");
982 printf("Comp Comp Decomp ");
983 if(doyuv) printf("Decode");
984 printf("\n");
985 printf("Format Subsamp Qual Width Height ");
986 if(doyuv) printf("Perf ");
987 printf("Perf Ratio Perf ");
988 if(doyuv) printf("Perf");
989 printf("\n\n");
990 }
991
992 if(decomponly)
993 {
994 decompTest(argv[1]);
995 printf("\n");
996 goto bailout;
997 }
998 if(subsamp>=0 && subsamp<TJ_NUMSAMP)
999 {
1000 for(i=maxqual; i>=minqual; i--)
1001 fullTest(srcbuf, w, h, subsamp, i, argv[1]);
1002 printf("\n");
1003 }
1004 else
1005 {
1006 if(pf!=TJPF_CMYK)
1007 {
1008 for(i=maxqual; i>=minqual; i--)
1009 fullTest(srcbuf, w, h, TJSAMP_GRAY, i, argv[1]);
1010 printf("\n");
1011 }
1012 for(i=maxqual; i>=minqual; i--)
1013 fullTest(srcbuf, w, h, TJSAMP_420, i, argv[1]);
1014 printf("\n");
1015 for(i=maxqual; i>=minqual; i--)
1016 fullTest(srcbuf, w, h, TJSAMP_422, i, argv[1]);
1017 printf("\n");
1018 for(i=maxqual; i>=minqual; i--)
1019 fullTest(srcbuf, w, h, TJSAMP_444, i, argv[1]);
1020 printf("\n");
1021 }
1022
1023 bailout:
1024 if(srcbuf) free(srcbuf);
1025 return retval;
1026 }
1027