package yi.iu.ft;

import java.util.*;
import java.io.*;
import java.awt.Dimension;
import yi.iu.region.*;

/**
 * Region based image feature.  Basically, it is a region + an image feature
 *
 * Parts of the codes are based on Todd's work
 * Title:        Object Image Generation
 * Description:  This project provides the required utilities to construct Object Images.  An Object Image is an image containing one or several predefined object sketch.  This kind of image is suitable to be used as experiment image when testing a learning algorithm.
 * Copyright:    Copyright (c) 2002
 * Company:      University of Washington
 * @author Yi Li
 * @version 1.0
 */

public class IFRegion extends ImageFeature{

    Region       m_region;          // the region
    ImageFeature m_feature;         // the image feature

    /**
     * Construct Region Image feature with image name and region ID
     */
    public IFRegion(Region r, ImageFeature feature) {
        this.m_region = r;
        this.m_feature = feature;
    }

    public Region getRegion() { return m_region; }

    public String toString() {
      return
        "<IFRegion>\n" +
          m_region.toString() +
          m_feature +
        "</IFRegion>\n";
    }

    /**
     * Construct region image features given image region map and region feature
     * data
     * [REQUIRES]
     *   labels are sequential already before passed in
     * [EFFECTS]
     *
     * @param labels pixel region label map
     * @param rdatas data about regions
     */
    public static IFRegion[] getIFRegions(int[] labels, int maxLabel,
        Dimension d, ImageFeature[] rdatas)
    {
        Region[] regions = Region.getRegions(labels, maxLabel, d);
        IFRegion[] ifrs = new IFRegion[maxLabel+1];
        for (int i=0; i<= maxLabel; i++) {
            ifrs[i] = new IFRegion(regions[i], rdatas[i]);
        }
        return ifrs;
    }

    /**
     * Mark the regions of the given region image features.  The regions will
     * be marked in the image with red ellipse and red major/minor axis
     */
    public static void markIFRegions(int[] img, Dimension dim, IFRegion[] ifr)
    {
        // call Region function to implement
        Region[] regions = new Region[ifr.length];
        for (int i=0; i<ifr.length; i++) {
            regions[i] = ifr[i].getRegion();
        }
        Region.markRegions(img, dim, regions);
    }

    /**
     * Output XML format of an array of IFRegions to out stream
     *
     * @param out output file
     * @param ifr Array of IFRegion to be output
     */
    public static void outputIFRegions(Writer out, String imageName, IFRegion[] ifr)
      throws IOException
    {
      out.write("<IFRegions imageName=\"" + imageName + "\">\n");
      for (int i=0; i<ifr.length; i++) out.write(ifr[i].toString());
      out.write("</IFRegions>\n");
    }

    /**
     * Convert XML format region feature info to raw format
     * The input file has the format of the output of IFRegion.outputIFRegions
     *
     * <IFRegions imageName="test2.gif">
     *   <IFRegion>
     *     <Region size="32658" centroidX="127.83682" centroidY="63.295486"
     *             momentX="5445.776" momentY="1357.3319" momentXY="20.520218"
     *             d="1.8419536E-4" e="-6.9406616E-7" f="4.5909754E-5"
     *             majAngle="0.005018909263476654" majLength="295.18479280295423"
     *             minAngle="1.5758152360583733" minLength="147.3623583564574"/>
     *     <IFColor R="238" G="132" B="12"/>
     *   </IFRegion>
     *
     *   ...
     *
     * </IFRegions>
     *
     * The output will be (it takes one line although I put them to several)
     *
     * test2.gif,32658,127.83682,63.295486,5445.776,1357.3319,20.520218,1.8419536E-4,
     *   -6.9406616E-7,4.5909754E-5,0.005018909263476654,295.18479280295423,
     *   1.5758152360583733,147.3623583564574,238,132,12
     *
     */
    public static class XML2Raw {
      public static void main(String[] args) {

      }
    }
}