package vnt;
/**
 * Debug_tool.java - v1.0
 * Began: July 10, 2005
 * Last Updated: July 28, 2005 
 * 
 * Copyright (C) 2005 - Michael D. Miller - mdm162@truman.edu
 * Truman State University
 * 100 E. Normal
 * Kirksville, MO - 63501
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

import ij.*;
import ij.gui.*;
import ij.process.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;
import ij.plugin.filter.*;
import ij.plugin.filter.PlugInFilter;
import java.io.*;
import ij.io.*;

import java.awt.Rectangle;

/**
 * <p>A simple tool that displays the selected rectangular ROI's
 * pixel values. Mostly useful for finding weird color anomalies
 * that the eye can't see.</p>
 * 
 * <p>Originally designed for debugging the DistanceMap_Skeleton
 * plug-in utility.</p>
 * 
 * @author Michael Miller - Truman State University
 * @version 1.0
 * @since 1.0
 */
public class Debug_tool implements PlugInFilter {

    /**
    * Specifies the preconditions for the plug-in.
    * If this method succeeds then run() is called.
    *
    * Pre: ImageJ is running and an 8-bit grayscale image is open. The plug-in was just activated.
    * <br />Post: Either an argument was processed, or the plug-in is cleared to run on the image.
    * @param arg Required by the interface. The argument list passed to the plug-in.
    * @param imp Required by the interface. The ImagePlus datastructure holding (among other things) information to grab path and filename.
    * @return If DONE is returned, ImageJ quits without run()'ing the plug-in. Otherwise, the plug-in signals to ImageJ that this plugin only handles 8-bit (256 grayscale) and will not change the original image.
    * @see #run
    */
    public int setup(String arg, ImagePlus ip) {
        if (arg.equals("about")) { 
            IJ.showMessage("Debug Tool", " * Displays the color values of the selected ROI. (Copyright 2005. Michael Miller mdm162@truman.edu)");
            return DONE; // exit without run()
        }
        return DOES_8G+ROI_REQUIRED+NO_CHANGES; // success, run()
    }

    /**
     * Displays in an ImageJ popup window the highlighted ROI
     * pixel data of a grayscale image.
     *  
     * Pre: The image was cleared to run by the setup() method. Assumes the ROI is rectangular.
     * <br />Post: The image is processed by the segmentation routines. A new segmented binary image is drawn. 
     * @param bp Required by the interface. The access information to the original image.
     * @see #setup
     */
    public void run(ImageProcessor ip){ 
        byte[] pixels = (byte[])ip.getPixels();
        int pix,x,y;
        int width = ip.getWidth();
        Rectangle r = ip.getRoi();
        int offset, i;
        String data = "----";
        for (x=r.x; x<(r.x+r.width); x++) {
            data = data + x + "  ";
        }
        data = data + "\n----|";
        for (x=r.x; x<(r.x+r.width); x++) {
            data = data + "-----";
        }
        
        for (y=r.y; y<(r.y+r.height); y++) {
            offset = y*width;
            data = data + y + " | ";
            for (x=r.x; x<(r.x+r.width); x++) {
                i = offset + x;
                 pix = 0xff & pixels[i];
                data = data + pix + "  ";
            }
            data = data + "\n";
        }
        IJ.showMessage("Debug Info", data);
    }
}