in tjbench.c [624:896]
static int decompTest(char *fileName)
{
FILE *file = NULL;
tjhandle handle = NULL;
unsigned char **jpegBufs = NULL, *srcBuf = NULL;
size_t *jpegSizes = NULL, srcSize, totalJpegSize;
tjtransform *t = NULL;
double start, elapsed;
int ps = tjPixelSize[pf], tile, row, col, i, iter, retval = 0, decompsrc = 0;
char *temp = NULL, tempStr[80], tempStr2[80];
/* Original image */
int w = 0, h = 0, minTile = 16, tilew, tileh, ntilesw = 1, ntilesh = 1,
subsamp = -1, cs = -1;
/* Transformed image */
int tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;
if ((file = fopen(fileName, "rb")) == NULL)
THROW_UNIX("opening file");
if (fseek(file, 0, SEEK_END) < 0 ||
(srcSize = ftell(file)) == (size_t)-1)
THROW_UNIX("determining file size");
if ((srcBuf = (unsigned char *)malloc(srcSize)) == NULL)
THROW_UNIX("allocating memory");
if (fseek(file, 0, SEEK_SET) < 0)
THROW_UNIX("setting file position");
if (fread(srcBuf, srcSize, 1, file) < 1)
THROW_UNIX("reading JPEG data");
fclose(file); file = NULL;
temp = strrchr(fileName, '.');
if (temp != NULL) *temp = '\0';
if ((handle = tj3Init(TJINIT_TRANSFORM)) == NULL)
THROW_TJG();
if (tj3Set(handle, TJPARAM_STOPONWARNING, stopOnWarning) == -1)
THROW_TJ();
if (tj3Set(handle, TJPARAM_BOTTOMUP, bottomUp) == -1)
THROW_TJ();
if (tj3Set(handle, TJPARAM_NOREALLOC, noRealloc) == -1)
THROW_TJ();
if (tj3Set(handle, TJPARAM_FASTUPSAMPLE, fastUpsample) == -1)
THROW_TJ();
if (tj3Set(handle, TJPARAM_FASTDCT, fastDCT) == -1)
THROW_TJ();
if (tj3Set(handle, TJPARAM_SCANLIMIT, limitScans ? 500 : 0) == -1)
THROW_TJ();
if (tj3Set(handle, TJPARAM_MAXMEMORY, maxMemory) == -1)
THROW_TJ();
if (tj3Set(handle, TJPARAM_MAXPIXELS, maxPixels) == -1)
THROW_TJ();
if (tj3DecompressHeader(handle, srcBuf, srcSize) == -1)
THROW_TJ();
w = tj3Get(handle, TJPARAM_JPEGWIDTH);
h = tj3Get(handle, TJPARAM_JPEGHEIGHT);
subsamp = tj3Get(handle, TJPARAM_SUBSAMP);
precision = tj3Get(handle, TJPARAM_PRECISION);
if (tj3Get(handle, TJPARAM_PROGRESSIVE) == 1)
printf("JPEG image is progressive\n\n");
if (tj3Get(handle, TJPARAM_ARITHMETIC) == 1)
printf("JPEG image uses arithmetic entropy coding\n\n");
if (tj3Set(handle, TJPARAM_PROGRESSIVE, progressive) == -1)
THROW_TJ();
if (tj3Set(handle, TJPARAM_ARITHMETIC, arithmetic) == -1)
THROW_TJ();
lossless = tj3Get(handle, TJPARAM_LOSSLESS);
sampleSize = (precision == 8 ? sizeof(unsigned char) : sizeof(short));
cs = tj3Get(handle, TJPARAM_COLORSPACE);
if (w < 1 || h < 1)
THROW("reading JPEG header", "Invalid image dimensions");
if (cs == TJCS_YCCK || cs == TJCS_CMYK) {
pf = TJPF_CMYK; ps = tjPixelSize[pf];
}
if (lossless) sf = TJUNSCALED;
if (tj3SetScalingFactor(handle, sf) == -1)
THROW_TJ();
if (tj3SetCroppingRegion(handle, cr) == -1)
THROW_TJ();
if (quiet == 1) {
printf("All performance values in Mpixels/sec\n\n");
printf("Pixel JPEG %s %s Xform Comp Decomp ",
doTile ? "Tile " : "Image", doTile ? "Tile " : "Image");
if (doYUV) printf("Decode");
printf("\n");
printf("Format Format Width Height Perf Ratio Perf ");
if (doYUV) printf("Perf");
printf("\n\n");
} else if (!quiet)
printf(">>>>> %d-bit JPEG (%s) --> %s (%s) <<<<<\n", precision,
formatName(subsamp, cs, tempStr), pixFormatStr[pf],
bottomUp ? "Bottom-up" : "Top-down");
if (doTile) {
if (subsamp == TJSAMP_UNKNOWN)
THROW("transforming",
"Could not determine subsampling level of JPEG image");
minTile = max(tjMCUWidth[subsamp], tjMCUHeight[subsamp]);
}
for (tilew = doTile ? minTile : w, tileh = doTile ? minTile : h; ;
tilew *= 2, tileh *= 2) {
if (tilew > w) tilew = w;
if (tileh > h) tileh = h;
ntilesw = (w + tilew - 1) / tilew;
ntilesh = (h + tileh - 1) / tileh;
if ((jpegBufs = (unsigned char **)malloc(sizeof(unsigned char *) *
ntilesw * ntilesh)) == NULL)
THROW_UNIX("allocating JPEG tile array");
memset(jpegBufs, 0, sizeof(unsigned char *) * ntilesw * ntilesh);
if ((jpegSizes = (size_t *)malloc(sizeof(size_t) * ntilesw *
ntilesh)) == NULL)
THROW_UNIX("allocating JPEG size array");
memset(jpegSizes, 0, sizeof(size_t) * ntilesw * ntilesh);
tsubsamp = (xformOpt & TJXOPT_GRAY) ? TJSAMP_GRAY : subsamp;
if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
if (tsubsamp == TJSAMP_422) tsubsamp = TJSAMP_440;
else if (tsubsamp == TJSAMP_440) tsubsamp = TJSAMP_422;
else if (tsubsamp == TJSAMP_411) tsubsamp = TJSAMP_441;
else if (tsubsamp == TJSAMP_441) tsubsamp = TJSAMP_411;
}
if (noRealloc &&
(doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter)) {
for (i = 0; i < ntilesw * ntilesh; i++) {
size_t jpegBufSize;
if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270)
jpegBufSize = tj3JPEGBufSize(tileh, tilew, tsubsamp);
else
jpegBufSize = tj3JPEGBufSize(tilew, tileh, tsubsamp);
if (jpegBufSize == 0)
THROW_TJG();
if ((jpegBufs[i] = tj3Alloc(jpegBufSize)) == NULL)
THROW_UNIX("allocating JPEG tiles");
}
}
tw = w; th = h; ttilew = tilew; ttileh = tileh;
if (!quiet) {
printf("\n%s size: %d x %d", doTile ? "Tile" : "Image", ttilew, ttileh);
if (sf.num != 1 || sf.denom != 1 || IS_CROPPED(cr))
printf(" --> %d x %d", CROPPED_WIDTH(tw), CROPPED_HEIGHT(th));
printf("\n");
} else if (quiet == 1) {
printf("%-4s(%s) %-14s ", pixFormatStr[pf],
bottomUp ? "BU" : "TD", formatName(subsamp, cs, tempStr));
printf("%-5d %-5d ", CROPPED_WIDTH(tilew), CROPPED_HEIGHT(tileh));
}
if (doTile || xformOp != TJXOP_NONE || xformOpt != 0 || customFilter) {
if ((t = (tjtransform *)malloc(sizeof(tjtransform) * ntilesw *
ntilesh)) == NULL)
THROW_UNIX("allocating image transform array");
if (xformOp == TJXOP_TRANSPOSE || xformOp == TJXOP_TRANSVERSE ||
xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT270) {
tw = h; th = w; ttilew = tileh; ttileh = tilew;
}
if (xformOp != TJXOP_NONE && xformOp != TJXOP_TRANSPOSE &&
subsamp == TJSAMP_UNKNOWN)
THROW("transforming",
"Could not determine subsampling level of JPEG image");
if (xformOp == TJXOP_HFLIP || xformOp == TJXOP_TRANSVERSE ||
xformOp == TJXOP_ROT90 || xformOp == TJXOP_ROT180)
tw = tw - (tw % tjMCUWidth[tsubsamp]);
if (xformOp == TJXOP_VFLIP || xformOp == TJXOP_TRANSVERSE ||
xformOp == TJXOP_ROT180 || xformOp == TJXOP_ROT270)
th = th - (th % tjMCUHeight[tsubsamp]);
tntilesw = (tw + ttilew - 1) / ttilew;
tntilesh = (th + ttileh - 1) / ttileh;
for (row = 0, tile = 0; row < tntilesh; row++) {
for (col = 0; col < tntilesw; col++, tile++) {
t[tile].r.w = min(ttilew, tw - col * ttilew);
t[tile].r.h = min(ttileh, th - row * ttileh);
t[tile].r.x = col * ttilew;
t[tile].r.y = row * ttileh;
t[tile].op = xformOp;
t[tile].options = xformOpt | TJXOPT_TRIM;
t[tile].customFilter = customFilter;
if (t[tile].options & TJXOPT_NOOUTPUT && jpegBufs[tile]) {
tj3Free(jpegBufs[tile]); jpegBufs[tile] = NULL;
}
}
}
iter = -1;
elapsed = 0.;
while (1) {
start = getTime();
if (tj3Transform(handle, srcBuf, srcSize, tntilesw * tntilesh,
jpegBufs, jpegSizes, t) == -1)
THROW_TJ();
elapsed += getTime() - start;
if (iter >= 0) {
iter++;
if (elapsed >= benchTime) break;
} else if (elapsed >= warmup) {
iter = 0;
elapsed = 0.;
}
}
free(t); t = NULL;
for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
totalJpegSize += jpegSizes[tile];
if (quiet) {
printf("%-6s%s%-6s%s",
sigfig((double)(w * h) / 1000000. / elapsed, 4, tempStr, 80),
quiet == 2 ? "\n" : " ",
sigfig((double)(w * h * ps) / (double)totalJpegSize, 4,
tempStr2, 80),
quiet == 2 ? "\n" : " ");
} else {
printf("Transform --> Frame rate: %f fps\n",
1.0 / elapsed);
printf(" Output image size: %lu bytes\n",
(unsigned long)totalJpegSize);
printf(" Compression ratio: %f:1\n",
(double)(w * h * ps) / (double)totalJpegSize);
printf(" Throughput: %f Megapixels/sec\n",
(double)(w * h) / 1000000. / elapsed);
printf(" Output bit stream: %f Megabits/sec\n",
(double)totalJpegSize * 8. / 1000000. / elapsed);
}
} else {
if (quiet == 1) printf("N/A N/A ");
tj3Free(jpegBufs[0]);
jpegBufs[0] = NULL;
decompsrc = 1;
}
if (w == tilew) ttilew = tw;
if (h == tileh) ttileh = th;
if (!(xformOpt & TJXOPT_NOOUTPUT)) {
if (decomp(decompsrc ? &srcBuf : jpegBufs,
decompsrc ? &srcSize : jpegSizes, NULL, tw, th, tsubsamp, 0,
fileName, ttilew, ttileh) == -1)
goto bailout;
} else if (quiet == 1) printf("N/A\n");
for (i = 0; i < ntilesw * ntilesh; i++) {
tj3Free(jpegBufs[i]);
jpegBufs[i] = NULL;
}
free(jpegBufs); jpegBufs = NULL;
free(jpegSizes); jpegSizes = NULL;
if (tilew == w && tileh == h) break;
}
bailout:
if (file) fclose(file);
if (jpegBufs) {
for (i = 0; i < ntilesw * ntilesh; i++)
tj3Free(jpegBufs[i]);
}
free(jpegBufs);
free(jpegSizes);
free(srcBuf);
free(t);
tj3Destroy(handle);
return retval;
}