static void decompTest()

in java/TJBench.java [526:759]


  static void decompTest(String fileName) throws Exception {
    TJTransformer tjt;
    byte[][] jpegBufs = null;
    byte[] srcBuf;
    int[] jpegSizes = null;
    int totalJpegSize;
    double start, elapsed;
    int ps = TJ.getPixelSize(pf), tile, x, y, iter;
    // Original image
    int w = 0, h = 0, ntilesw = 1, ntilesh = 1, subsamp = -1, cs = -1;
    // Transformed image
    int minTile = 16, tw, th, ttilew, ttileh, tntilesw, tntilesh, tsubsamp;

    FileInputStream fis = new FileInputStream(fileName);
    if (fis.getChannel().size() > (long)Integer.MAX_VALUE)
      throw new Exception("Image is too large");
    int srcSize = (int)fis.getChannel().size();
    srcBuf = new byte[srcSize];
    fis.read(srcBuf, 0, srcSize);
    fis.close();

    int index = fileName.lastIndexOf('.');
    if (index >= 0)
      fileName = new String(fileName.substring(0, index));

    tjt = new TJTransformer();
    tjt.set(TJ.PARAM_STOPONWARNING, stopOnWarning ? 1 : 0);
    tjt.set(TJ.PARAM_BOTTOMUP, bottomUp ? 1 : 0);
    tjt.set(TJ.PARAM_FASTUPSAMPLE, fastUpsample ? 1 : 0);
    tjt.set(TJ.PARAM_FASTDCT, fastDCT ? 1 : 0);
    tjt.set(TJ.PARAM_SCANLIMIT, limitScans ? 500 : 0);
    tjt.set(TJ.PARAM_MAXMEMORY, maxMemory);
    tjt.set(TJ.PARAM_MAXPIXELS, maxPixels);

    try {
      tjt.setSourceImage(srcBuf, srcSize);
    } catch (TJException e) { handleTJException(e); }
    w = tjt.getWidth();
    h = tjt.getHeight();
    subsamp = tjt.get(TJ.PARAM_SUBSAMP);
    precision = tjt.get(TJ.PARAM_PRECISION);
    cs = tjt.get(TJ.PARAM_COLORSPACE);
    if (tjt.get(TJ.PARAM_PROGRESSIVE) == 1)
      System.out.println("JPEG image is progressive\n");
    if (tjt.get(TJ.PARAM_ARITHMETIC) == 1)
      System.out.println("JPEG image uses arithmetic entropy coding\n");
    tjt.set(TJ.PARAM_PROGRESSIVE, progressive ? 1 : 0);
    tjt.set(TJ.PARAM_ARITHMETIC, arithmetic ? 1 : 0);

    if (cs == TJ.CS_YCCK || cs == TJ.CS_CMYK) {
      pf = TJ.PF_CMYK;  ps = TJ.getPixelSize(pf);
    }

    if (tjt.get(TJ.PARAM_LOSSLESS) != 0)
      sf = TJ.UNSCALED;

    tjt.setScalingFactor(sf);
    tjt.setCroppingRegion(cr);

    if (quiet == 1) {
      System.out.println("All performance values in Mpixels/sec\n");
      System.out.format("Pixel     JPEG             %s  %s   Xform   Comp    Decomp  ",
                        (doTile ? "Tile " : "Image"),
                        (doTile ? "Tile " : "Image"));
      if (doYUV)
        System.out.print("Decode");
      System.out.print("\n");
      System.out.print("Format    Format           Width  Height  Perf    Ratio   Perf    ");
      if (doYUV)
        System.out.print("Perf");
      System.out.println("\n");
    } else if (quiet == 0)
      System.out.format(">>>>>  %d-bit JPEG (%s) --> %s (%s)  <<<<<\n",
                        precision, formatName(subsamp, cs), PIXFORMATSTR[pf],
                        bottomUp ? "Bottom-up" : "Top-down");

    if (doTile) {
      if (subsamp == TJ.SAMP_UNKNOWN)
        throw new Exception("Could not determine subsampling level of JPEG image");
      minTile = Math.max(TJ.getMCUWidth(subsamp), TJ.getMCUHeight(subsamp));
    }
    for (int 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;

      tw = w;  th = h;  ttilew = tilew;  ttileh = tileh;
      if (quiet == 0) {
        System.out.format("\n%s size: %d x %d", (doTile ? "Tile" : "Image"),
                          ttilew, ttileh);
        if (sf.getNum() != 1 || sf.getDenom() != 1 || isCropped(cr))
          System.out.format(" --> %d x %d", getCroppedWidth(tw),
                            getCroppedHeight(th));
        System.out.println("");
      } else if (quiet == 1) {
        System.out.format("%-4s(%s)  %-14s   ", PIXFORMATSTR[pf],
                          bottomUp ? "BU" : "TD", formatName(subsamp, cs));
        System.out.format("%-5d  %-5d   ", getCroppedWidth(tilew),
                          getCroppedHeight(tileh));
      }

      tsubsamp = subsamp;
      if ((xformOpt & TJTransform.OPT_GRAY) != 0)
        tsubsamp = TJ.SAMP_GRAY;
      if (xformOp == TJTransform.OP_TRANSPOSE ||
          xformOp == TJTransform.OP_TRANSVERSE ||
          xformOp == TJTransform.OP_ROT90 ||
          xformOp == TJTransform.OP_ROT270) {
        if (tsubsamp == TJ.SAMP_422)
          tsubsamp = TJ.SAMP_440;
        else if (tsubsamp == TJ.SAMP_440)
          tsubsamp = TJ.SAMP_422;
        else if (tsubsamp == TJ.SAMP_411)
          tsubsamp = TJ.SAMP_441;
        else if (tsubsamp == TJ.SAMP_441)
          tsubsamp = TJ.SAMP_411;
      }

      if (doTile || xformOp != TJTransform.OP_NONE || xformOpt != 0 ||
          customFilter != null) {
        if (xformOp == TJTransform.OP_TRANSPOSE ||
            xformOp == TJTransform.OP_TRANSVERSE ||
            xformOp == TJTransform.OP_ROT90 ||
            xformOp == TJTransform.OP_ROT270) {
          tw = h;  th = w;  ttilew = tileh;  ttileh = tilew;
        }

        if (xformOp != TJTransform.OP_NONE &&
            xformOp != TJTransform.OP_TRANSPOSE && subsamp == TJ.SAMP_UNKNOWN)
          throw new Exception("Could not determine subsampling level of JPEG image");
        if (xformOp == TJTransform.OP_HFLIP ||
            xformOp == TJTransform.OP_TRANSVERSE ||
            xformOp == TJTransform.OP_ROT90 ||
            xformOp == TJTransform.OP_ROT180)
          tw = tw - (tw % TJ.getMCUWidth(tsubsamp));
        if (xformOp == TJTransform.OP_VFLIP ||
            xformOp == TJTransform.OP_TRANSVERSE ||
            xformOp == TJTransform.OP_ROT180 ||
            xformOp == TJTransform.OP_ROT270)
          th = th - (th % TJ.getMCUHeight(tsubsamp));
        tntilesw = (tw + ttilew - 1) / ttilew;
        tntilesh = (th + ttileh - 1) / ttileh;

        TJTransform[] t = new TJTransform[tntilesw * tntilesh];
        jpegBufs =
          new byte[tntilesw * tntilesh][TJ.bufSize(ttilew, ttileh, tsubsamp)];

        for (y = 0, tile = 0; y < th; y += ttileh) {
          for (x = 0; x < tw; x += ttilew, tile++) {
            t[tile] = new TJTransform();
            t[tile].width = Math.min(ttilew, tw - x);
            t[tile].height = Math.min(ttileh, th - y);
            t[tile].x = x;
            t[tile].y = y;
            t[tile].op = xformOp;
            t[tile].options = xformOpt | TJTransform.OPT_TRIM;
            t[tile].cf = customFilter;
            if ((t[tile].options & TJTransform.OPT_NOOUTPUT) != 0 &&
                jpegBufs[tile] != null)
              jpegBufs[tile] = null;
          }
        }

        iter = -1;
        elapsed = 0.;
        while (true) {
          start = getTime();
          try {
            tjt.transform(jpegBufs, t);
          } catch (TJException e) { handleTJException(e); }
          jpegSizes = tjt.getTransformedSizes();
          elapsed += getTime() - start;
          if (iter >= 0) {
            iter++;
            if (elapsed >= benchTime)
              break;
          } else if (elapsed >= warmup) {
            iter = 0;
            elapsed = 0.0;
          }
        }
        t = null;

        for (tile = 0, totalJpegSize = 0; tile < tntilesw * tntilesh; tile++)
          totalJpegSize += jpegSizes[tile];

        if (quiet != 0) {
          System.out.format("%-6s%s%-6s%s",
                            sigFig((double)(w * h) / 1000000. / elapsed, 4),
                            quiet == 2 ? "\n" : "  ",
                            sigFig((double)(w * h * ps) /
                                   (double)totalJpegSize, 4),
                            quiet == 2 ? "\n" : "  ");
        } else {
          System.out.format("Transform     --> Frame rate:         %f fps\n",
                            1.0 / 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. / elapsed);
          System.out.format("                  Output bit stream:  %f Megabits/sec\n",
                            (double)totalJpegSize * 8. / 1000000. / elapsed);
        }
      } else {
        if (quiet == 1)
          System.out.print("N/A     N/A     ");
        jpegBufs = new byte[1][TJ.bufSize(ttilew, ttileh, tsubsamp)];
        jpegSizes = new int[1];
        jpegBufs[0] = srcBuf;
        jpegSizes[0] = srcSize;
      }

      if (w == tilew)
        ttilew = tw;
      if (h == tileh)
        ttileh = th;
      if ((xformOpt & TJTransform.OPT_NOOUTPUT) == 0)
        decomp(jpegBufs, jpegSizes, null, tw, th, tsubsamp, 0, fileName,
               ttilew, ttileh);
      else if (quiet == 1)
        System.out.println("N/A");

      jpegBufs = null;
      jpegSizes = null;

      if (tilew == w && tileh == h) break;
    }
  }