public GhostReport ghostCalculation(int maxImageSmallDimension, int numThreads)
        throws IOException {
      File ghostOutputfile;
      byte[] imageInByte;
      String ghostBase64; // convert to base64 the image file
      GhostReport ghostReport = new GhostReport();
      GhostExtractor ghostExtractor;
      ghostExtractor = new GhostExtractor(sourceFile, maxImageSmallDimension, numThreads);
      BufferedImage ghostMap;
      for (int ghostMapInd = 0; ghostMapInd < ghostExtractor.ghostMaps.size(); ghostMapInd++) {
        ghostOutputfile =
            new File(baseFolder, "GhostOutput" + String.format("%02d", ghostMapInd) + ".png");
        ghostMap = ghostExtractor.ghostMaps.get(ghostMapInd);
        ImageIO.write(ghostMap, "png", ghostOutputfile);
        ByteArrayOutputStream ghostbytes = new ByteArrayOutputStream();
        ImageIO.write(ghostMap, "png", ghostbytes);
        imageInByte = ghostbytes.toByteArray();

        ghostReport.maps.add(ghostOutputfile.getCanonicalPath());
        ghostReport.differences = ghostExtractor.allDifferences;
        ghostReport.minQuality = ghostExtractor.qualityMin;
        ghostReport.maxQuality = ghostExtractor.qualityMax;
        ghostReport.qualities = ghostExtractor.ghostQualities;
        ghostReport.minValues = ghostExtractor.ghostMin;
        ghostReport.maxValues = ghostExtractor.ghostMax;
      }
      ghostReport.completed = true;
      return ghostReport;
    }
    public GridsBothReport gridsCalculation() throws IOException {
      GridsBothReport gridsBothReport = new GridsBothReport();
      GridsExtractor gridsExtractor;
      gridsExtractor = new GridsExtractor(sourceFile);
      ImageIO.write(gridsExtractor.displaySurfaceG, "png", outputFileG);
      ByteArrayOutputStream gridsbytes = new ByteArrayOutputStream();
      ImageIO.write(gridsExtractor.displaySurfaceG, "png", gridsbytes);

      ImageIO.write(gridsExtractor.displaySurfaceGI, "png", outputFileGI);
      ByteArrayOutputStream gridsInvbytes = new ByteArrayOutputStream();
      ImageIO.write(gridsExtractor.displaySurfaceGI, "png", gridsInvbytes);

      gridsBothReport.mapG = outputFileG.getCanonicalPath();
      gridsBothReport.mapGI = outputFileGI.getCanonicalPath();
      gridsBothReport.maxValueG = gridsExtractor.gridsmaxG;
      gridsBothReport.minValueG = gridsExtractor.gridsminG;
      gridsBothReport.maxValueG = gridsExtractor.gridsmaxGI;
      gridsBothReport.minValueG = gridsExtractor.gridsminGI;
      gridsBothReport.completed = true;

      gridsBothReport.gridsNormalReport.map = outputFileG.getCanonicalPath();
      gridsBothReport.gridsNormalReport.maxValue = gridsExtractor.gridsmaxG;
      gridsBothReport.gridsNormalReport.minValue = gridsExtractor.gridsminG;
      gridsBothReport.gridsNormalReport.completed = true;

      gridsBothReport.gridsInversedReport.map = outputFileGI.getCanonicalPath();
      gridsBothReport.gridsInversedReport.maxValue = gridsExtractor.gridsmaxGI;
      gridsBothReport.gridsInversedReport.minValue = gridsExtractor.gridsminGI;
      gridsBothReport.gridsInversedReport.completed = true;
      return gridsBothReport;
    }
    public MedianNoiseReport medianNoiseCalculation() throws IOException {
      MedianNoiseReport medianNoiseReport = new MedianNoiseReport();
      MedianNoiseExtractor medianNoiseExtractor;
      medianNoiseExtractor = new MedianNoiseExtractor(sourceFile);
      ImageIO.write(medianNoiseExtractor.displaySurface, "png", outputFile);
      ByteArrayOutputStream medianNoisebytes = new ByteArrayOutputStream();
      ImageIO.write(medianNoiseExtractor.displaySurface, "png", medianNoisebytes);

      medianNoiseReport.map = outputFile.getCanonicalPath();
      medianNoiseReport.completed = true;
      return medianNoiseReport;
    }
 public dqReport dqCalculation() throws IOException {
   dqReport dqReport = new dqReport();
   DQExtractor dqDetector;
   dqDetector = new DQExtractor(sourceFile);
   ImageIO.write(dqDetector.displaySurface, "png", outputFile);
   ByteArrayOutputStream dqbytes = new ByteArrayOutputStream();
   ImageIO.write(dqDetector.displaySurface, "png", dqbytes);
   dqReport.map = outputFile.getCanonicalPath();
   dqReport.maxValue = dqDetector.maxProbValue;
   dqReport.minvalue = dqDetector.minProbValue;
   dqReport.completed = true;
   return dqReport;
 }
    public ELAReport elaCalculation() throws IOException {
      ELAReport elaReport = new ELAReport();
      ELAExtractor elaExtractor;
      elaExtractor = new ELAExtractor(sourceFile);
      ImageIO.write(elaExtractor.displaySurface, "png", outputFile);
      ByteArrayOutputStream elabytes = new ByteArrayOutputStream();
      ImageIO.write(elaExtractor.displaySurface, "png", elabytes);

      elaReport.map = outputFile.getCanonicalPath();
      elaReport.maxValue = elaExtractor.elaMax;
      elaReport.minvalue = elaExtractor.elaMin;
      elaReport.completed = true;
      return elaReport;
    }
    public DWNoiseReport noiseDWCalculation() throws IOException {
      DWNoiseReport dwNoiseReport = new DWNoiseReport();
      DWNoiseVarExtractor noiseExtractor;
      noiseExtractor = new DWNoiseVarExtractor(sourceFile);
      ImageIO.write(noiseExtractor.displaySurface, "png", outputFile);
      ByteArrayOutputStream noisebytes = new ByteArrayOutputStream();
      ImageIO.write(noiseExtractor.displaySurface, "png", noisebytes);

      dwNoiseReport.map = outputFile.getCanonicalPath();
      dwNoiseReport.maxvalue = noiseExtractor.maxNoiseValue;
      dwNoiseReport.minValue = noiseExtractor.minNoiseValue;
      dwNoiseReport.completed = true;
      return dwNoiseReport;
    }
    public BlockingReport blkCalculation() throws IOException {
      BlockingReport blockingReport = new BlockingReport();
      BlockingExtractor blockingExtractor;
      blockingExtractor = new BlockingExtractor(sourceFile);
      ImageIO.write(blockingExtractor.displaySurface, "png", outputFile);
      ByteArrayOutputStream blockbytes = new ByteArrayOutputStream();
      ImageIO.write(blockingExtractor.displaySurface, "png", blockbytes);

      blockingReport.map = outputFile.getCanonicalPath();
      blockingReport.maxValue = blockingExtractor.blkmax;
      blockingReport.minValue = blockingExtractor.blkmin;
      blockingReport.completed = true;
      return blockingReport;
    }
  public static String downloadURL(String urlIn, String folderOut, String mongoHostIP)
      throws IOException {
    System.out.println("downloadURL");
    String imgHash = null;
    byte[] data = null;

    // connect to URL and get input stream
    URL imageURL = new URL(urlIn);
    File localDir = new File(folderOut);
    localDir.mkdir();

    InputStream inputStream = null;
    URLConnection urlConnection = null;
    int noOfBytes = 0;
    byte[] byteChunk = new byte[4096];
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    urlConnection = imageURL.openConnection();
    urlConnection.addRequestProperty(
        "User-Agent",
        "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    urlConnection.connect();
    inputStream = urlConnection.getInputStream();
    while ((noOfBytes = inputStream.read(byteChunk)) > 0) {
      byteOutputStream.write(byteChunk, 0, noOfBytes);
    }
    // hash creation from image file
    try {
      System.out.println("Start MD5 Digest");
      data = byteOutputStream.toByteArray();
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(data);
      byte[] hash = md.digest();
      imgHash = String.format("%032x", new java.math.BigInteger(1, hash));
      System.out.println("Hash : " + imgHash);
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    MongoClient mongoclient = new MongoClient(mongoHostIP, 27017);
    // System.out.println("mongoHostIP :: " + mongoHostIP);
    Morphia morphia = new Morphia();
    morphia.map(ForensicReport.class).map(dqReport.class);
    Datastore ds = new Morphia().createDatastore(mongoclient, "ForensicDatabase");
    ds.ensureCaps();

    String baseFolder = folderOut + imgHash + "/";
    ForensicReport report = ds.get(ForensicReport.class, imgHash);
    // check if hash exist
    if (report != null) {
      System.out.println("Exists");
    } else {
      // if hash does not exist in database, then download the image
      report = new ForensicReport();
      report.id = imgHash;
      try {
        File writeFolder = new File(baseFolder);
        if (!writeFolder.exists()) writeFolder.mkdirs();
        File imageFile = new File(baseFolder, "Raw");
        OutputStream outputStream = new FileOutputStream(imageFile);

        byteOutputStream.writeTo(outputStream);
        outputStream.close();
        BufferedImage downloadedImage = ImageIO.read(imageFile);
        ImageIO.write(downloadedImage, "JPEG", new File(baseFolder, "Display.jpg"));
        // store in database image information
        report.sourceImage = baseFolder + "Raw";
        report.displayImage = baseFolder + "Display.jpg";
        report.sourceURL = urlIn;
        report.status = "Downloaded";
        ds.save(report);
      } catch (Exception e) {
        e.printStackTrace();
        mongoclient.close();
        return "URL_ERROR";
      }
    }
    mongoclient.close();
    System.out.println("Downloaded.");
    return imgHash;
  }
  public static String reportCalculation(
      String urlHash,
      String mongoHostIP,
      String folderOut,
      int maxGhostImageSmallDimension,
      int numGhostThreads,
      int numberOfThreads,
      long computationTimeoutLimit)
      throws UnknownHostException {
    String outMessage = "COMPLETEDSUCCESSFULLY";

    ExecutorService threadpool = Executors.newFixedThreadPool(numberOfThreads);
    MongoClient mongoclient = new MongoClient(mongoHostIP, 27017);
    Morphia morphia = new Morphia();
    morphia.map(ForensicReport.class).map(dqReport.class);
    Datastore ds = new Morphia().createDatastore(mongoclient, "ForensicDatabase");
    ds.ensureCaps();
    String baseFolder = folderOut + urlHash + "/";
    ForensicReport report = ds.get(ForensicReport.class, urlHash);

    if (report == null) {
      mongoclient.close();
      return "HASHNOTFOUND";
    }
    if (report.status.equalsIgnoreCase("Processing")) {
      mongoclient.close();
      return "ALREADYPROCESSING";
    }
    if (report.status.equalsIgnoreCase("Done")) {
      mongoclient.close();
      return "PROCESSINGALREADYCOMPLETE";
    }
    System.out.println("start processing");
    report.status = "Processing";
    dqReport dqReport = new dqReport();
    ELAReport elaReport = new ELAReport();
    GhostReport ghostReport = new GhostReport();
    DWNoiseReport dwNoiseReport = new DWNoiseReport();
    BlockingReport blockingReport = new BlockingReport();
    MedianNoiseReport medianNoiseReport = new MedianNoiseReport();
    GPSReport gpsReport = new GPSReport();
    GridsNormalReport gridsReport = new GridsNormalReport();
    GridsInversedReport gridsInversedReport = new GridsInversedReport();

    File dqOutputfile = new File(baseFolder, "DQOutput.png");
    File dwNoiseOutputfile = new File(baseFolder, "DWNoiseOutput.png");
    File ghostOutputfile;
    File elaOutputfile = new File(baseFolder, "ELAOutput.png");
    File blkOutputfile = new File(baseFolder, "BLKOutput.png");
    File medianNoiseOutputFile = new File(baseFolder, "MedianNoiseOutput.png");
    File gridsOutputFile = new File(baseFolder, "GridsOutput.png");
    File gridsInversedOutputFile = new File(baseFolder, "GridsInversedOutput.png");
    try {
      if (ImageIO.read(new File(report.sourceImage)).getColorModel().hasAlpha()) {
        System.out.println(
            "If image has an alpha channel, then assume transparent PNG -No point in processing it");
        // If image has an alpha channel, then assume transparent PNG -No point in processing it
        BufferedImage transparentPNGNotAccepted = ArtificialImages.transparentPNGNotAccepted();
        ImageIO.write(transparentPNGNotAccepted, "png", dqOutputfile);
        System.out.println(
            " dqOutputfile.getCanonicalPath() 1 :: " + dqOutputfile.getCanonicalPath());
        dqReport.map = dqOutputfile.getCanonicalPath();
        dqReport.completed = true;
        report.dqReport = dqReport;
        ImageIO.write(transparentPNGNotAccepted, "png", dwNoiseOutputfile);
        System.out.println(
            " dwNoiseOutputfile.getCanonicalPath() :: " + dwNoiseOutputfile.getCanonicalPath());
        dwNoiseReport.map = dwNoiseOutputfile.getCanonicalPath();
        dwNoiseReport.completed = true;
        report.dwNoiseReport = dwNoiseReport;
        ghostOutputfile = new File(baseFolder, "GhostOutput" + String.format("%02d", 0) + ".png");
        ImageIO.write(transparentPNGNotAccepted, "png", ghostOutputfile);
        ghostReport.maps.add(ghostOutputfile.getCanonicalPath());
        System.out.println(
            " ghostOutputfile.getCanonicalPath() :: " + ghostOutputfile.getCanonicalPath());
        ghostReport.differences.add((float) 0.0);
        ghostReport.qualities.add(0);
        ghostReport.minValues.add((float) 0.0);
        ghostReport.maxValues.add((float) 0.0);
        ghostReport.completed = true;
        report.ghostReport = ghostReport;
        ImageIO.write(transparentPNGNotAccepted, "png", elaOutputfile);
        System.out.println(
            " elaOutputfile.getCanonicalPath() :: " + elaOutputfile.getCanonicalPath());
        elaReport.map = elaOutputfile.getCanonicalPath();
        elaReport.completed = true;
        report.elaReport = elaReport;
        ImageIO.write(transparentPNGNotAccepted, "png", blkOutputfile);
        blockingReport.map = blkOutputfile.getCanonicalPath();
        System.out.println(
            " blkOutputfile.getCanonicalPath() :: " + blkOutputfile.getCanonicalPath());
        blockingReport.completed = true;
        report.blockingReport = blockingReport;
        ImageIO.write(transparentPNGNotAccepted, "png", medianNoiseOutputFile);
        medianNoiseReport.map = medianNoiseOutputFile.getCanonicalPath();
        System.out.println(
            " medianNoiseOutputFile.getCanonicalPath() :: "
                + medianNoiseOutputFile.getCanonicalPath());
        medianNoiseReport.completed = true;
        report.medianNoiseReport = medianNoiseReport;
        ImageIO.write(transparentPNGNotAccepted, "png", gridsOutputFile);
        gridsReport.map = gridsOutputFile.getCanonicalPath();
        System.out.println(
            " gridsOutputFile.getCanonicalPath() :: " + gridsOutputFile.getCanonicalPath());
        ImageIO.write(transparentPNGNotAccepted, "png", gridsInversedOutputFile);
        gridsInversedReport.map = gridsInversedOutputFile.getCanonicalPath();
        System.out.println(
            " gridsInversedOutputFile.getCanonicalPath() :: "
                + gridsInversedOutputFile.getCanonicalPath());
        gridsReport.completed = true;
        gridsInversedReport.completed = true;
        report.gridsReport = gridsReport;
        report.gridsInversedReport = gridsInversedReport;
        ds.save(report);
      } else {

        Boolean dqSaved = false,
            noiseDWSaved = false,
            ghostSaved = false,
            elaSaved = false,
            blkSaved = false,
            medianNoiseSaved = false,
            gridsSaved = false;
        DQThread dqThread = new DQThread(report.sourceImage, dqOutputfile);
        Future dqFuture = threadpool.submit(dqThread);
        noiseDWThread noiseDWThread = new noiseDWThread(report.sourceImage, dwNoiseOutputfile);
        Future noiseDWFuture = threadpool.submit(noiseDWThread);
        GhostThread ghostThread =
            new GhostThread(
                report.sourceImage, baseFolder, maxGhostImageSmallDimension, numGhostThreads);
        Future ghostFuture = threadpool.submit(ghostThread);
        ELAThread elaThread = new ELAThread(report.sourceImage, elaOutputfile);
        Future elaFuture = threadpool.submit(elaThread);
        BLKThread blkThread = new BLKThread(report.sourceImage, blkOutputfile);
        Future blkFuture = threadpool.submit(blkThread);
        MedianNoiseThread medianNoiseThread =
            new MedianNoiseThread(report.sourceImage, medianNoiseOutputFile);
        Future medianNoiseFuture = threadpool.submit(medianNoiseThread);
        GridsThread gridsThread =
            new GridsThread(report.sourceImage, gridsOutputFile, gridsInversedOutputFile);
        Future gridsFuture = threadpool.submit(gridsThread);
        //  GridsInversedThread gridsInversedThread = new
        // GridsInversedThread(report.sourceImage,gridsInversedOutputFile);
        //  Future gridsInversedFuture = threadpool.submit(gridsInversedThread);

        Long startTime = System.currentTimeMillis();
        MetadataExtractor metaExtractor;
        metaExtractor = new MetadataExtractor(report.sourceImage);
        JsonObject metadataReport = metaExtractor.metadataReport;
        metadataReport.addProperty("completed", true);
        report.metadataStringReport = metadataReport.toString();
        ds.save(report);

        GPSExtractor gpsExtractor = new GPSExtractor(metadataReport);
        gpsReport.completed = true;
        gpsReport.exists = gpsExtractor.exists;
        gpsReport.latitude = gpsExtractor.latitude;
        gpsReport.longitude = gpsExtractor.longitude;
        report.gpsReport = gpsReport;
        ds.save(report);

        ThumbnailReport thumbnail = new ThumbnailReport();
        ThumbnailExtractor thumbnailExtractor;
        thumbnailExtractor = new ThumbnailExtractor(report.sourceImage);
        thumbnail.numberOfThumbnails = thumbnailExtractor.numberOfThumbnails;
        File thumbFile = null;
        for (int thumbInd = 0; thumbInd < thumbnailExtractor.numberOfThumbnails; thumbInd++) {
          thumbFile = new File(baseFolder, "Thumbnail" + String.valueOf(thumbInd) + ".png");
          ImageIO.write(thumbnailExtractor.thumbnails.get(thumbInd), "png", thumbFile);
          thumbnail.thumbnailList.add(thumbFile.getCanonicalPath());
        }

        report.thumbnailReport = thumbnail;
        ds.save(report);

        while (!dqFuture.isDone()
            | !noiseDWFuture.isDone()
            | !ghostFuture.isDone()
            | !elaFuture.isDone()
            | !blkFuture.isDone()
            | !medianNoiseFuture.isDone()
            | !gridsFuture.isDone()) {
          Thread.sleep(100); // sleep for 1 millisecond before checking again
          if (dqFuture.isDone() & !dqSaved) {
            report.dqReport = (dqReport) dqFuture.get();
            dqSaved = true;
            ds.save(report);
            System.out.println("dqReport Done");
          }
          if (noiseDWFuture.isDone() & !noiseDWSaved) {
            report.dwNoiseReport = (DWNoiseReport) noiseDWFuture.get();
            noiseDWSaved = true;
            ds.save(report);
            System.out.println("DWNoiseReport Done");
          }
          if (ghostFuture.isDone() & !ghostSaved) {
            report.ghostReport = (GhostReport) ghostFuture.get();
            ghostSaved = true;
            ds.save(report);
            System.out.println("Ghost Done");
          }
          if (elaFuture.isDone() & !elaSaved) {
            report.elaReport = (ELAReport) elaFuture.get();
            elaSaved = true;
            ds.save(report);
            System.out.println("elaReport Done");
          }
          if (blkFuture.isDone() & !blkSaved) {
            report.blockingReport = (BlockingReport) blkFuture.get();
            blkSaved = true;
            ds.save(report);
            System.out.println("blockingReport Done");
          }
          if (medianNoiseFuture.isDone() & !medianNoiseSaved) {
            report.medianNoiseReport = (MedianNoiseReport) medianNoiseFuture.get();
            medianNoiseSaved = true;
            ds.save(report);
            System.out.println("Median Noise Done");
          }
          if (gridsFuture.isDone() & !gridsSaved) {
            GridsBothReport gridsBothReport = (GridsBothReport) gridsFuture.get();
            report.gridsReport = gridsBothReport.gridsNormalReport;
            report.gridsReport.completed = true;
            report.gridsInversedReport = gridsBothReport.gridsInversedReport;
            report.gridsInversedReport.completed = true;
            gridsSaved = true;
            ds.save(report);
            System.out.println("GridsReport Done");
          }
          if ((System.currentTimeMillis() - startTime) > computationTimeoutLimit) {
            System.out.println("Computation timed out");
            outMessage = "TIMEDOUT";
            // kill if timeout
            dqFuture.cancel(true);
            noiseDWFuture.cancel(true);
            ghostFuture.cancel(true);
            blkFuture.cancel(true);
            medianNoiseFuture.cancel(true);
            elaFuture.cancel(true);
            gridsFuture.cancel(true);
            break;
          }
        }
        threadpool.shutdown();
      }
    } catch (Exception e) {
      threadpool.shutdown();
      e.printStackTrace();
    }
    report.status = "Done";
    ds.save(report);
    System.out.println("Will now close mongodb connection");
    mongoclient.close();
    return outMessage;
  }