in java/TJBench.java [332:523]
static void fullTest(TJCompressor tjc, Object srcBuf, int w, int h,
int subsamp, int jpegQual, String fileName)
throws Exception {
Object tmpBuf;
byte[][] jpegBufs;
int[] jpegSizes;
double start, elapsed, elapsedEncode;
int totalJpegSize = 0, tilew, tileh, i, iter;
int ps = TJ.getPixelSize(pf);
int ntilesw = 1, ntilesh = 1, pitch = w * ps;
String pfStr = PIXFORMATSTR[pf];
YUVImage yuvImage = null;
if ((long)pitch * (long)h > (long)Integer.MAX_VALUE)
throw new Exception("Image is too large");
if (precision == 8)
tmpBuf = new byte[pitch * h];
else
tmpBuf = new short[pitch * h];
if (quiet == 0)
System.out.format(">>>>> %s (%s) <--> %d-bit JPEG (%s %s%d) <<<<<\n",
pfStr, bottomUp ? "Bottom-up" : "Top-down", precision,
lossless ? "Lossless" : SUBNAME_LONG[subsamp],
lossless ? "PSV" : "Q", jpegQual);
tjc.set(TJ.PARAM_SUBSAMP, subsamp);
tjc.set(TJ.PARAM_FASTDCT, fastDCT ? 1 : 0);
tjc.set(TJ.PARAM_OPTIMIZE, optimize ? 1 : 0);
tjc.set(TJ.PARAM_PROGRESSIVE, progressive ? 1 : 0);
tjc.set(TJ.PARAM_ARITHMETIC, arithmetic ? 1 : 0);
tjc.set(TJ.PARAM_LOSSLESS, lossless ? 1 : 0);
if (lossless)
tjc.set(TJ.PARAM_LOSSLESSPSV, jpegQual);
else
tjc.set(TJ.PARAM_QUALITY, jpegQual);
tjc.set(TJ.PARAM_RESTARTBLOCKS, restartIntervalBlocks);
tjc.set(TJ.PARAM_RESTARTROWS, restartIntervalRows);
tjc.set(TJ.PARAM_MAXMEMORY, maxMemory);
for (tilew = doTile ? 8 : w, tileh = doTile ? 8 : 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;
jpegBufs =
new byte[ntilesw * ntilesh][TJ.bufSize(tilew, tileh, subsamp)];
jpegSizes = new int[ntilesw * ntilesh];
/* Compression test */
if (quiet == 1)
System.out.format("%-4s(%s) %-2d/%-6s %-3d ", pfStr,
bottomUp ? "BU" : "TD", precision,
lossless ? "LOSSLS" : SUBNAME_LONG[subsamp],
jpegQual);
if (precision == 8) {
for (i = 0; i < h; i++)
System.arraycopy((byte[])srcBuf, w * ps * i, (byte[])tmpBuf,
pitch * i, w * ps);
} else {
for (i = 0; i < h; i++)
System.arraycopy((short[])srcBuf, w * ps * i, (short[])tmpBuf,
pitch * i, w * ps);
}
if (doYUV) {
yuvImage = new YUVImage(tilew, yuvAlign, tileh, subsamp);
Arrays.fill(yuvImage.getBuf(), (byte)127);
}
/* Benchmark */
iter = -1;
elapsed = elapsedEncode = 0.0;
while (true) {
int tile = 0;
totalJpegSize = 0;
start = getTime();
for (int y = 0; y < h; y += tileh) {
for (int x = 0; x < w; x += tilew, tile++) {
int width = Math.min(tilew, w - x);
int height = Math.min(tileh, h - y);
if (precision == 8)
tjc.setSourceImage((byte[])srcBuf, x, y, width, pitch, height,
pf);
else if (precision == 12)
tjc.setSourceImage12((short[])srcBuf, x, y, width, pitch, height,
pf);
else
tjc.setSourceImage16((short[])srcBuf, x, y, width, pitch, height,
pf);
if (doYUV) {
double startEncode = getTime();
yuvImage.setBuf(yuvImage.getBuf(), width, yuvAlign, height,
subsamp);
tjc.encodeYUV(yuvImage);
if (iter >= 0)
elapsedEncode += getTime() - startEncode;
tjc.setSourceImage(yuvImage);
}
tjc.compress(jpegBufs[tile]);
jpegSizes[tile] = tjc.getCompressedSize();
totalJpegSize += jpegSizes[tile];
}
}
elapsed += getTime() - start;
if (iter >= 0) {
iter++;
if (elapsed >= benchTime)
break;
} else if (elapsed >= warmup) {
iter = 0;
elapsed = elapsedEncode = 0.0;
}
}
if (doYUV)
elapsed -= elapsedEncode;
if (quiet == 1)
System.out.format("%-5d %-5d ", tilew, tileh);
if (quiet != 0) {
if (doYUV)
System.out.format("%-6s%s",
sigFig((double)(w * h) / 1000000. *
(double)iter / elapsedEncode, 4),
quiet == 2 ? "\n" : " ");
System.out.format("%-6s%s",
sigFig((double)(w * h) / 1000000. *
(double)iter / elapsed, 4),
quiet == 2 ? "\n" : " ");
System.out.format("%-6s%s",
sigFig((double)(w * h * ps) / (double)totalJpegSize,
4),
quiet == 2 ? "\n" : " ");
} else {
System.out.format("\n%s size: %d x %d\n", doTile ? "Tile" : "Image",
tilew, tileh);
if (doYUV) {
System.out.format("Encode YUV --> Frame rate: %f fps\n",
(double)iter / elapsedEncode);
System.out.format(" Output image size: %d bytes\n",
yuvImage.getSize());
System.out.format(" Compression ratio: %f:1\n",
(double)(w * h * ps) / (double)yuvImage.getSize());
System.out.format(" Throughput: %f Megapixels/sec\n",
(double)(w * h) / 1000000. *
(double)iter / elapsedEncode);
System.out.format(" Output bit stream: %f Megabits/sec\n",
(double)yuvImage.getSize() * 8. / 1000000. *
(double)iter / elapsedEncode);
}
System.out.format("%s --> Frame rate: %f fps\n",
doYUV ? "Comp from YUV" : "Compress ",
(double)iter / elapsed);
System.out.format(" Output image size: %d bytes\n",
totalJpegSize);
System.out.format(" Compression ratio: %f:1\n",
(double)(w * h * ps) / (double)totalJpegSize);
System.out.format(" Throughput: %f Megapixels/sec\n",
(double)(w * h) / 1000000. * (double)iter / elapsed);
System.out.format(" Output bit stream: %f Megabits/sec\n",
(double)totalJpegSize * 8. / 1000000. *
(double)iter / elapsed);
}
if (tilew == w && tileh == h && write) {
String tempStr = fileName + "_" +
(lossless ? "LOSSLS" : SUBNAME[subsamp]) + "_" +
(lossless ? "PSV" : "Q") + jpegQual + ".jpg";
FileOutputStream fos = new FileOutputStream(tempStr);
fos.write(jpegBufs[0], 0, jpegSizes[0]);
fos.close();
if (quiet == 0)
System.out.println("Reference image written to " + tempStr);
}
/* Decompression test */
if (!compOnly)
decomp(jpegBufs, jpegSizes, tmpBuf, w, h, subsamp, jpegQual, fileName,
tilew, tileh);
else if (quiet == 1)
System.out.println("N/A");
if (tilew == w && tileh == h) break;
}
}