/** @(#) WindowLevel.java 1.0 99/10/4
 * Copyright (c) 1999 Larry Rodrigues
 */
 package com.vistech.imageviewer;
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.*;
 import java.io.*;
 import javax.swing.*;
 import javax.swing.event.*;
 import java.beans.*;

/** Implements the Window/Level feature.
  * @version 1.0  4 Nov 1999
  * @author Lawrence Rodrigues
  **/
public class WindowLevel{
   protected int minValue =0;
   protected int maxValue =255;
   protected int MIN_SCREEN_VALUE = 0;
   protected int MAX_SCREEN_VALUE = 255;
   protected int LUT_SIZE = 256;
   protected int lutSize = LUT_SIZE;
   protected BufferedImage wlImage;
   protected int window, level;

   protected ImageDisplay imageCanvas;

   /** The WindowLevel constructor.
     * @param imageCanvas the canvas in which the image is drawn.
     */
   public WindowLevel(ImageDisplay imageCanvas) {
      this.imageCanvas = imageCanvas;
   }

   /** Sets the size of the lookup table.
     * @param size the lookup table size.
     */
    public void setLUTSize(int size) {
      if(size <=0) return;
      lutSize = size;
   }

   public int getLUTSize(){
      return lutSize;
   }

   /** Sets the window width.
     * @param win width of the window.
     */
   public void setWindow(int win) {
      window = win;
   }

   /** Sets the level.
     * @param lev the level.
     */
   public void setLevel(int lev) {
      level = lev;
   }

   /** Sets the window width and level.
     * This methods calls the createByteLookupTable()to create a lookup
     * table for the window and level values specified in the input. Using this lookup
     * table, creates a LookupOp object which is used for applying the
     * lookup table to the image displayed on the canvas.This method then displays
     * the resulting image on the canvas.
     * @see createByteLookupTable()
     * @param win width of the window.
     * @param lev the level.
     */
   public void setWindowLevel(int win, int lev) {
      ByteLookupTable blut = createByteLookupTable(win, lev);
      LookupOp lkop = new LookupOp(blut, null);
      wlImage = lkop.filter(imageCanvas.getOffScreenImage() , null);
      imageCanvas.setDisplayImage(wlImage);
   }

   /** Creates a lookup table for the specified window width and level.
     * @param win width of the window.
     * @param lev the level.
     */
   public ByteLookupTable createByteLookupTable(int win, int lev) {
      int windowStart= lev-win/2;
      int windowEnd  = lev+win/2;
      if(windowStart <=0) windowStart =0;
      if(windowEnd > lutSize) windowEnd = lutSize;
      byte lut[] = new byte[lutSize];
      double windowMappingRatio = (MAX_SCREEN_VALUE-MIN_SCREEN_VALUE)/(double)(win);
      for(int i=0;i<windowStart;i++) {
          lut[i] = (byte)MIN_SCREEN_VALUE;
      }
      for(int i = windowStart; i< windowEnd; i++) {
          lut[i] = (byte)((i-windowStart)* windowMappingRatio);
      }
      for(int i = windowEnd; i< lutSize; i++) {
          lut[i] = (byte)MAX_SCREEN_VALUE;
      }
      return  new ByteLookupTable(0, lut);
   }
}