private void ChangeThePalette() { if (_viewer.Image.BitsPerPixel == 1) { return; } try { int low = (int)(_windowLevelCenter - _windowLevelWidth / 2.0); int high = (int)(_windowLevelCenter + _windowLevelWidth / 2.0); int minValue, maxVale; _currentPalette = new RasterColor[_LUTSize]; MinMaxValuesCommand cmd = new MinMaxValuesCommand(); cmd.Run(_viewer.Image); minValue = cmd.MinimumValue; maxVale = cmd.MaximumValue; RasterPalette.WindowLevelFillLookupTable( _currentPalette, new RasterColor(0, 0, 0), new RasterColor(255, 255, 255), low, high, _viewer.Image.LowBit, _highBit, minValue, maxVale, 0, _flags); if (_viewer.Image.BitsPerPixel == 8) { _viewer.Image.SetPalette(_currentPalette, 0, _currentPalette.Length); } else { _viewer.Image.WindowLevel( _viewer.Image.LowBit, _highBit, _currentPalette, RasterWindowLevelMode.PaintAndProcessing); } } catch (Exception /*ex*/) { } }
public static void WindowLevel(RasterImage image, params string[] parameters) { if (parameters.Length >= 3) { int width, center; bool inverted; if (!int.TryParse(parameters[0], out width)) { return; } if (!int.TryParse(parameters[1], out center)) { return; } if (!bool.TryParse(parameters[2], out inverted)) { return; } int nLowBit = image.LowBit; int nHighBit = image.HighBit; if (nHighBit == -1) { nHighBit = image.BitsPerPixel - 1; } int uLUTLen = 1 << (nHighBit - image.LowBit + 1); int nFrom = center - (width >> 1); int nTo = center + ((width + 1) >> 1); int nFullMinValue; int nFullMaxValue; int nLookupTableLength = 0; if (image.GetLookupTable() != null) { nLookupTableLength = image.GetLookupTable().Length; } if (image.BitsPerPixel == 8) { nFullMinValue = 0; nFullMaxValue = 255; } else { if (nLookupTableLength == 0) { nFullMinValue = !image.Signed ? 0 : -(0xffff + 1) / 2; nFullMaxValue = !image.Signed ? 0xffff - 1 : (0xffff + 1) / 2 - 1; } else { nFullMinValue = !image.Signed ? 0 : -(nLookupTableLength + 1) / 2; nFullMaxValue = !image.Signed ? nLookupTableLength - 1 : (nLookupTableLength + 1) / 2 - 1; } } RasterColor[] lookUpTable = new RasterColor[uLUTLen]; RasterPaletteWindowLevelFlags flags = ((image.Signed) ? RasterPaletteWindowLevelFlags.Signed : RasterPaletteWindowLevelFlags.None) | RasterPaletteWindowLevelFlags.Outside | RasterPaletteWindowLevelFlags.Linear | RasterPaletteWindowLevelFlags.DicomStyle; RasterColor startColor = new RasterColor(0, 0, 0); RasterColor endColor = new RasterColor(255, 255, 255); RasterPalette.WindowLevelFillLookupTable(lookUpTable, (!inverted) ? startColor : endColor, (!inverted) ? endColor : startColor, nFrom, nTo, nLowBit, nHighBit, nFullMinValue, nFullMaxValue, 1, flags); if (image.BitsPerPixel == 8) { image.SetPalette(lookUpTable, 0, 256); } else { image.WindowLevel(nLowBit, nHighBit, lookUpTable, RasterWindowLevelMode.PaintAndProcessing); } } }