package no.hiof.imagepr.examples;

import no.hiof.imagepr.filters.*;
import no.hiof.imagepr.*;
import java.io.*;

/**
   This is a program for testing the no.hiof.imagepr.filters.LocalHistogramFilter class. This program tests colorimages.
*/

public class LocalHistogramFilterColorTest
{

   // An image and a filter.
   private Image image;
   private LocalHistogramFilter lh;

   // The input-image. Must be a JPG-/ PNG-/ GIF-image containing a colorimage.
   public final static String filename = "testbilder/m.jpg";

   // Constuctor.
   public LocalHistogramFilterColorTest()
   {

      // Try to open the imagefile and convert it into an intensity-image.
      try
         {
            // Open and show the colorimage
            image = new RGBImage (filename);
            System.out.println ("The original image will be shown in a few seconds.");
            image.show ("Original image");

            image = new HSIImage ((RGBImage) image);
         }

      catch (IOException e)
         {
            System.err.println ("Can't open file " + filename + ".");
            System.exit(0);
         }

      catch (Exception e)
         {
            System.err.println ("Can't read the fileformat of " + filename + ".");
            System.exit(0);
         }

      // Create the filter. Use default filterparameters (maskHeight = maskwidth = 3 and edgeType = FILTER_EDGES)
      lh = new LocalHistogramFilter ();

      // Uncomment the next line if you want to observe what happend if the filter fails to filter the image.
      // (Use an image that is less than 10001 pixels high.)
      //lh.setFilterParam (10001, 21, LocalHistogramFilter.FILTER_EDGES);

      // Print a message when the filter starts and when it's finished. This may be useful to measure the amount
      // of time the program needs to filter the image.
      System.out.println ("Filter starts.");

      // The actual filtering
      image = lh.filter (image);

      // If  the filter failed to filter the image a null-pointer was returned. This will cause a
      // NullPointerException if we don't exit now.
      if (image == null)
         {
            System.err.println ("The image was not successfully filtered.");
            System.err.println ("Program exits.");
            System.exit (1);
         }

      System.out.println ("Filter finished.");
      System.out.println ("The filtered image will be shown in a few seconds. Please wait.");

      // Show the filtered image on the screen.
      image.show("Filtered image");
   }

   // The main-method.
   public static void main (String[] args)
   {
      System.out.println ("Program starts.");

      LocalHistogramFilterColorTest app = new LocalHistogramFilterColorTest ();

      System.out.println ("Program exits.");

      System.exit(0);
   }
}

