/*========================================================================= Program: FusionViewer Module: $RCSfile: WindowLevelModel.java,v $ Language: Java Date: $Date: 2007/02/02 19:27:11 $ Version: $Revision: 1.2 $ Copyright (c) Insightful Corporation. All rights reserved. See Copyright.txt for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ package org.fusionviewer.model; import org.fusionviewer.display.Image; /**\class WindowLevelModel *\brief Pixel value scaling parameters * * outputPixelValue = slope * inputPixelValue + offset * * In terms of window and level * * slope = MAX_PIXEL_VALUE / window * offset = level - window / 2 */ public class WindowLevelModel { /* * These members are described above */ private float offset, slope, window, level; private float minValue, pixelRange; /** * Initialize window and level parameters from an image so that all * values are linearly scaled to fill an 8-bit display range * * @param image an image */ public void initialize(Image image) { if (image == null) return; float maxValue = (float) image.getMaxValue(); minValue = (float) image.getMinValue(); if (image.isByteType()) { slope = 1.0f; offset = 0.0f; window = 255.0f; level = 255.0f / 2.0f; minValue = 0.0f; pixelRange = 255.0f; } else if (maxValue == minValue) { // Trap here to avoid a slope of zero slope = 1.0f; offset = minValue; window = 1.0f; level = minValue; minValue = 0.0f; pixelRange = 1.0f; } else { pixelRange = maxValue - minValue; slope = 255.0f / (float)(pixelRange); offset = minValue; window = pixelRange; level = pixelRange / 2.0f + minValue; } } /** * Get the pixel scaling offset: output = slope * input + offset * * @return scaling offset */ public float getOffset() { return offset; } /** * Get the pixel scaling slope: output = slope * input + offset * * @return scaling slope */ public float getSlope() { return slope; } /** * Get the pixel scaling window * * @return scaling window */ public float getWindow() { return window; } /** * Get the pixel scaling level * * @return scaling level */ public float getLevel() { return level; } /** * @return the minimum value of the image this model was initialized from */ public float getMinValue() { return minValue; } /** * @return the range of pixel values in the image this model was initialized from */ public float getPixelRange() { return pixelRange; } /** * Set the pixel scaling: output = slope * input + offset * * @param offset scaling offset * @param slope scaling slope * @return true if this object was modified */ public boolean setOffsetSlope(float offset, float slope) { if ((slope == this.slope) && (offset == this.offset)) return false; this.offset = offset; this.slope = slope; this.window = this.slope * 255.0f; this.level = offset + this.window / 2.0f; return true; } /** * Set the pixel scaling as window and level. * output = MAX_PIXEL_VALUE / window * input + (level - window / 2) * * @param window scaling window * @param level scaling level * @return true if this object was modified */ public boolean setWindowLevel(float window, float level) { if ((window == this.window) && (level == this.level)) return false; this.window = window; this.level = level; this.offset = level - (window / 2.0f); this.slope = 255.0f / window; return true; } }