public static void main()

in java/TJBench.java [867:1249]


  public static void main(String[] argv) {
    Object srcBuf = null;
    int w = 0, h = 0, minQual = -1, maxQual = -1;
    int minArg = 1, retval = 0;
    int subsamp = -1;
    TJCompressor tjc = null;

    try {

      if (argv.length < minArg)
        usage();

      String tempStr = argv[0].toLowerCase();
      if (tempStr.endsWith(".jpg") || tempStr.endsWith(".jpeg"))
        decompOnly = true;
      if (tempStr.endsWith(".bmp"))
        bmp = true;

      System.out.println("");

      if (!decompOnly) {
        minArg = 2;
        if (argv.length < minArg)
          usage();
        String[] quals = argv[1].split("-", 2);
        try {
          minQual = Integer.parseInt(quals[0]);
        } catch (NumberFormatException e) {}
        if (quals.length > 1) {
          try {
            maxQual = Integer.parseInt(quals[1]);
          } catch (NumberFormatException e) {}
        }
        if (maxQual < minQual)
          maxQual = minQual;
      }

      if (argv.length > minArg) {
        for (int i = minArg; i < argv.length; i++) {
          if (argv[i].equalsIgnoreCase("-tile")) {
            doTile = true;  xformOpt |= TJTransform.OPT_CROP;
          } else if (argv[i].equalsIgnoreCase("-precision") &&
                     i < argv.length - 1) {
            int temp = 0;

            try {
              temp = Integer.parseInt(argv[++i]);
            } catch (NumberFormatException e) {}
            if (temp == 8 || temp == 12 || temp == 16)
              precision = temp;
            else
              usage();
          } else if (argv[i].equalsIgnoreCase("-fastupsample")) {
            System.out.println("Using fastest upsampling algorithm\n");
            fastUpsample = true;
          } else if (argv[i].equalsIgnoreCase("-fastdct")) {
            System.out.println("Using fastest DCT/IDCT algorithm\n");
            fastDCT = true;
          } else if (argv[i].equalsIgnoreCase("-optimize")) {
            optimize = true;
            xformOpt |= TJTransform.OPT_OPTIMIZE;
          } else if (argv[i].equalsIgnoreCase("-progressive")) {
            System.out.println("Generating progressive JPEG images\n");
            progressive = true;
            xformOpt |= TJTransform.OPT_PROGRESSIVE;
          } else if (argv[i].equalsIgnoreCase("-arithmetic")) {
            System.out.println("Using arithmetic entropy coding\n");
            arithmetic = true;
            xformOpt |= TJTransform.OPT_ARITHMETIC;
          } else if (argv[i].equalsIgnoreCase("-lossless"))
            lossless = true;
          else if (argv[i].equalsIgnoreCase("-rgb"))
            pf = TJ.PF_RGB;
          else if (argv[i].equalsIgnoreCase("-rgbx"))
            pf = TJ.PF_RGBX;
          else if (argv[i].equalsIgnoreCase("-bgr"))
            pf = TJ.PF_BGR;
          else if (argv[i].equalsIgnoreCase("-bgrx"))
            pf = TJ.PF_BGRX;
          else if (argv[i].equalsIgnoreCase("-xbgr"))
            pf = TJ.PF_XBGR;
          else if (argv[i].equalsIgnoreCase("-xrgb"))
            pf = TJ.PF_XRGB;
          else if (argv[i].equalsIgnoreCase("-cmyk"))
            pf = TJ.PF_CMYK;
          else if (argv[i].equalsIgnoreCase("-bottomup"))
            bottomUp = true;
          else if (argv[i].equalsIgnoreCase("-quiet"))
            quiet = 1;
          else if (argv[i].equalsIgnoreCase("-qq"))
            quiet = 2;
          else if (argv[i].equalsIgnoreCase("-scale") && i < argv.length - 1) {
            int temp1 = 0, temp2 = 0;
            boolean match = false, scanned = true;
            Scanner scanner = new Scanner(argv[++i]).useDelimiter("/");

            try {
              temp1 = scanner.nextInt();
              temp2 = scanner.nextInt();
            } catch (Exception e) {}
            if (temp2 <= 0) temp2 = 1;
            if (temp1 > 0) {
              TJScalingFactor[] scalingFactors = TJ.getScalingFactors();

              for (int j = 0; j < scalingFactors.length; j++) {
                if ((double)temp1 / (double)temp2 ==
                    (double)scalingFactors[j].getNum() /
                    (double)scalingFactors[j].getDenom()) {
                  sf = scalingFactors[j];
                  match = true;  break;
                }
              }
              if (!match) usage();
            } else
              usage();
          } else if (argv[i].equalsIgnoreCase("-crop") &&
                     i < argv.length - 1) {
            int temp1 = -1, temp2 = -1, temp3 = -1, temp4 = -1;
            Scanner scanner = new Scanner(argv[++i]).useDelimiter("x|\\+");

            try {
              temp1 = scanner.nextInt();
              temp2 = scanner.nextInt();
              temp3 = scanner.nextInt();
              temp4 = scanner.nextInt();
            } catch (Exception e) {}

            if (temp1 < 0 || temp2 < 0 || temp3 < 0 || temp4 < 0)
              usage();
            cr.width = temp1;  cr.height = temp2;  cr.x = temp3;  cr.y = temp4;
          } else if (argv[i].equalsIgnoreCase("-hflip"))
            xformOp = TJTransform.OP_HFLIP;
          else if (argv[i].equalsIgnoreCase("-vflip"))
            xformOp = TJTransform.OP_VFLIP;
          else if (argv[i].equalsIgnoreCase("-transpose"))
            xformOp = TJTransform.OP_TRANSPOSE;
          else if (argv[i].equalsIgnoreCase("-transverse"))
            xformOp = TJTransform.OP_TRANSVERSE;
          else if (argv[i].equalsIgnoreCase("-rot90"))
            xformOp = TJTransform.OP_ROT90;
          else if (argv[i].equalsIgnoreCase("-rot180"))
            xformOp = TJTransform.OP_ROT180;
          else if (argv[i].equalsIgnoreCase("-rot270"))
            xformOp = TJTransform.OP_ROT270;
          else if (argv[i].equalsIgnoreCase("-grayscale"))
            xformOpt |= TJTransform.OPT_GRAY;
          else if (argv[i].equalsIgnoreCase("-custom"))
            customFilter = new DummyDCTFilter();
          else if (argv[i].equalsIgnoreCase("-nooutput"))
            xformOpt |= TJTransform.OPT_NOOUTPUT;
          else if (argv[i].equalsIgnoreCase("-copynone"))
            xformOpt |= TJTransform.OPT_COPYNONE;
          else if (argv[i].equalsIgnoreCase("-benchtime") &&
                   i < argv.length - 1) {
            double temp = -1;

            try {
              temp = Double.parseDouble(argv[++i]);
            } catch (NumberFormatException e) {}
            if (temp > 0.0)
              benchTime = temp;
            else
              usage();
          } else if (argv[i].equalsIgnoreCase("-warmup") &&
                     i < argv.length - 1) {
            double temp = -1;

            try {
              temp = Double.parseDouble(argv[++i]);
            } catch (NumberFormatException e) {}
            if (temp >= 0.0) {
              warmup = temp;
              System.out.format("Warmup time = %.1f seconds\n\n", warmup);
            } else
              usage();
          } else if (argv[i].equalsIgnoreCase("-bmp"))
            bmp = true;
          else if (argv[i].equalsIgnoreCase("-yuv")) {
            System.out.println("Testing planar YUV encoding/decoding\n");
            doYUV = true;
          } else if (argv[i].equalsIgnoreCase("-yuvpad") &&
                     i < argv.length - 1) {
            int temp = 0;

            try {
              temp = Integer.parseInt(argv[++i]);
            } catch (NumberFormatException e) {}
            if (temp >= 1 && (temp & (temp - 1)) == 0)
              yuvAlign = temp;
            else
              usage();
          } else if (argv[i].equalsIgnoreCase("-subsamp") &&
                     i < argv.length - 1) {
            i++;
            if (argv[i].toUpperCase().startsWith("G"))
              subsamp = TJ.SAMP_GRAY;
            else if (argv[i].equals("444"))
              subsamp = TJ.SAMP_444;
            else if (argv[i].equals("422"))
              subsamp = TJ.SAMP_422;
            else if (argv[i].equals("440"))
              subsamp = TJ.SAMP_440;
            else if (argv[i].equals("420"))
              subsamp = TJ.SAMP_420;
            else if (argv[i].equals("411"))
              subsamp = TJ.SAMP_411;
            else if (argv[i].equals("441"))
              subsamp = TJ.SAMP_441;
            else
              usage();
          } else if (argv[i].equalsIgnoreCase("-componly"))
            compOnly = true;
          else if (argv[i].equalsIgnoreCase("-nowrite"))
            write = false;
          else if (argv[i].equalsIgnoreCase("-limitscans"))
            limitScans = true;
          else if (argv[i].equalsIgnoreCase("-maxmemory") &&
                   i < argv.length - 1) {
            int temp = -1;

            try {
              temp = Integer.parseInt(argv[++i]);
            } catch (NumberFormatException e) {}
            if (temp < 0)
              usage();
            maxMemory = temp;
          } else if (argv[i].equalsIgnoreCase("-maxpixels") &&
                     i < argv.length - 1) {
            int temp = -1;

            try {
              temp = Integer.parseInt(argv[++i]);
            } catch (NumberFormatException e) {}
            if (temp < 0)
              usage();
            maxPixels = temp;
          } else if (argv[i].equalsIgnoreCase("-restart") &&
                   i < argv.length - 1) {
            int temp = -1;
            String arg = argv[++i];
            Scanner scanner = new Scanner(arg).useDelimiter("b|B");

            try {
              temp = scanner.nextInt();
            } catch (Exception e) {}

            if (temp < 0 || temp > 65535 || scanner.hasNext())
              usage();
            if (arg.endsWith("B") || arg.endsWith("b"))
              restartIntervalBlocks = temp;
            else
              restartIntervalRows = temp;
          } else if (argv[i].equalsIgnoreCase("-stoponwarning"))
            stopOnWarning = true;
          else usage();
        }
      }

      if (optimize && !progressive && !arithmetic && !lossless &&
          precision != 12)
        System.out.println("Computing optimal Huffman tables\n");

      if (lossless)
        subsamp = TJ.SAMP_444;

      if (precision == 16 && !lossless)
        throw new Exception("-lossless must be specified along with -precision 16");
      if (precision != 8 && doYUV)
        throw new Exception("-yuv requires 8-bit data precision");
      if (lossless && doYUV)
        throw new Exception("ERROR: -lossless and -yuv are incompatible");

      if ((sf.getNum() != 1 || sf.getDenom() != 1) && doTile) {
        System.out.println("Disabling tiled compression/decompression tests, because those tests do not");
        System.out.println("work when scaled decompression is enabled.\n");
        doTile = false;
        xformOpt &= (~TJTransform.OPT_CROP);
      }

      if (isCropped(cr)) {
        if (!decompOnly)
          throw new Exception("ERROR: Partial image decompression can only be enabled for JPEG input images");
        if (doTile) {
          System.out.println("Disabling tiled compression/decompression tests, because those tests do not");
          System.out.println("work when partial image decompression is enabled.\n");
          doTile = false;
          xformOpt &= (~TJTransform.OPT_CROP);
        }
        if (doYUV)
          throw new Exception("ERROR: -crop and -yuv are incompatible");
      }

      if (!decompOnly) {
        int[] width = new int[1], height = new int[1],
          pixelFormat = new int[1];

        tjc = new TJCompressor();
        tjc.set(TJ.PARAM_STOPONWARNING, stopOnWarning ? 1 : 0);
        tjc.set(TJ.PARAM_BOTTOMUP, bottomUp ? 1 : 0);
        tjc.set(TJ.PARAM_MAXPIXELS, maxPixels);

        pixelFormat[0] = pf;
        srcBuf = tjc.loadImage(precision, argv[0], width, 1, height,
                               pixelFormat);
        w = width[0];  h = height[0];  pf = pixelFormat[0];
        int index = -1;
        if ((index = argv[0].lastIndexOf('.')) >= 0)
          argv[0] = argv[0].substring(0, index);
      }

      if (quiet == 1 && !decompOnly) {
        System.out.println("All performance values in Mpixels/sec\n");
        System.out.format("Pixel     JPEG      JPEG  %s  %s   ",
                          (doTile ? "Tile " : "Image"),
                          (doTile ? "Tile " : "Image"));
        if (doYUV)
          System.out.print("Encode  ");
        System.out.print("Comp    Comp    Decomp  ");
        if (doYUV)
          System.out.print("Decode");
        System.out.print("\n");
        System.out.format("Format    Format    %s  Width  Height  ",
                          lossless ? "PSV " : "Qual");
        if (doYUV)
          System.out.print("Perf    ");
        System.out.print("Perf    Ratio   Perf    ");
        if (doYUV)
          System.out.print("Perf");
        System.out.println("\n");
      }

      if (decompOnly) {
        decompTest(argv[0]);
        System.out.println("");
        System.exit(retval);
      }

      System.gc();
      if (lossless) {
        if (minQual < 1 || minQual > 7 || maxQual < 1 || maxQual > 7)
          throw new Exception("PSV must be between 1 and 7.");
      } else {
        if (minQual < 1 || minQual > 100 || maxQual < 1 || maxQual > 100)
          throw new Exception("Quality must be between 1 and 100.");
      }
      if (subsamp >= 0 && subsamp < TJ.NUMSAMP) {
        for (int i = maxQual; i >= minQual; i--)
          fullTest(tjc, srcBuf, w, h, subsamp, i, argv[0]);
        System.out.println("");
      } else {
        if (pf != TJ.PF_CMYK) {
          for (int i = maxQual; i >= minQual; i--)
            fullTest(tjc, srcBuf, w, h, TJ.SAMP_GRAY, i, argv[0]);
          System.out.println("");
          System.gc();
        }
        for (int i = maxQual; i >= minQual; i--)
          fullTest(tjc, srcBuf, w, h, TJ.SAMP_420, i, argv[0]);
        System.out.println("");
        System.gc();
        for (int i = maxQual; i >= minQual; i--)
          fullTest(tjc, srcBuf, w, h, TJ.SAMP_422, i, argv[0]);
        System.out.println("");
        System.gc();
        for (int i = maxQual; i >= minQual; i--)
          fullTest(tjc, srcBuf, w, h, TJ.SAMP_444, i, argv[0]);
        System.out.println("");
      }

    } catch (Exception e) {
      if (e instanceof TJException) {
        TJException tje = (TJException)e;

        System.out.println((tje.getErrorCode() == TJ.ERR_WARNING ?
                            "WARNING: " : "ERROR: ") + tje.getMessage());
      } else
        System.out.println("ERROR: " + e.getMessage());
      e.printStackTrace();
      retval = -1;
    }

    System.exit(retval);
  }