/// <summary> /// Validates that the photometric interpretation is not unknown. /// </summary> /// <param name="photometricInterpretation"></param> public static void ValidatePhotometricInterpretation(PhotometricInterpretation photometricInterpretation) { if (photometricInterpretation == PhotometricInterpretation.Unknown) { throw new DicomDataException(String.Format(SR.ExceptionInvalidPhotometricInterpretation, photometricInterpretation)); } }
/// <summary> /// Validates that the input image property relationships are compatible. /// </summary> public static void ValidateImagePropertyRelationships(int bitsStored, int bitsAllocated, int highBit, PhotometricInterpretation photometricInterpretation, int planarConfiguration, int samplesPerPixel) { if (bitsStored > bitsAllocated) throw new DicomDataException(String.Format(SR.ExceptionInvalidBitsStoredBitsAllocated, bitsStored, bitsAllocated)); if (highBit > bitsAllocated - 1) throw new DicomDataException(String.Format(SR.ExceptionInvalidHighBitBitsAllocated, highBit, bitsAllocated)); if ((photometricInterpretation == PhotometricInterpretation.Monochrome1 || photometricInterpretation == PhotometricInterpretation.Monochrome2) && samplesPerPixel != 1) { throw new DicomDataException(String.Format(SR.ExceptionInvalidPhotometricInterpretationSamplesPerPixel, photometricInterpretation, samplesPerPixel)); } if (samplesPerPixel != 1) { if (planarConfiguration != 0 && planarConfiguration != 1) throw new DicomDataException(String.Format(SR.ExceptionInvalidPlanarConfiguration)); } if ((photometricInterpretation == PhotometricInterpretation.Rgb || photometricInterpretation == PhotometricInterpretation.YbrFull || photometricInterpretation == PhotometricInterpretation.YbrFull422 || photometricInterpretation == PhotometricInterpretation.YbrPartial422 || photometricInterpretation == PhotometricInterpretation.YbrIct || photometricInterpretation == PhotometricInterpretation.YbrRct) && samplesPerPixel != 3) { throw new DicomDataException(String.Format(SR.ExceptionInvalidPhotometricInterpretationSamplesPerPixel, photometricInterpretation, samplesPerPixel)); } }
/// <summary> /// Converts colour pixel data to ARGB. /// </summary> protected static byte[] ToArgb(IDicomAttributeProvider dicomAttributeProvider, byte[] pixelData, PhotometricInterpretation photometricInterpretation) { #if DEBUG CodeClock clock = new CodeClock(); clock.Start(); #endif int rows = dicomAttributeProvider[DicomTags.Rows].GetInt32(0, 0); int columns = dicomAttributeProvider[DicomTags.Columns].GetInt32(0, 0); int sizeInBytes = rows*columns*4; byte[] argbPixelData = MemoryManager.Allocate<byte>(sizeInBytes); // Convert palette colour images to ARGB so we don't get interpolation artifacts // when rendering. if (photometricInterpretation == PhotometricInterpretation.PaletteColor) { int bitsAllocated = dicomAttributeProvider[DicomTags.BitsAllocated].GetInt32(0, 0); int pixelRepresentation = dicomAttributeProvider[DicomTags.PixelRepresentation].GetInt32(0, 0); ColorSpaceConverter.ToArgb( bitsAllocated, pixelRepresentation != 0 ? true : false, pixelData, argbPixelData, PaletteColorMap.Create(dicomAttributeProvider)); } // Convert RGB and YBR variants to ARGB else { int planarConfiguration = dicomAttributeProvider[DicomTags.PlanarConfiguration].GetInt32(0, 0); ColorSpaceConverter.ToArgb( photometricInterpretation, planarConfiguration, pixelData, argbPixelData); } #if DEBUG clock.Stop(); PerformanceReportBroker.PublishReport("DicomMessageSopDataSource", "ToArgb", clock.Seconds); #endif return argbPixelData; }
private static void Add(PhotometricInterpretation photometricInterpretation) { _photometricInterpretations.Add(photometricInterpretation.Code, photometricInterpretation); }
private static void Add(PhotometricInterpretation photometricInterpretation) { _photometricInterpretations.Add(photometricInterpretation.Code, photometricInterpretation); }
public void SetPixelData(PixelData pixeldata) { if (IsColor) { _internalPhotometricInterpretation = null; PhotometricInterpretation = "RGB"; byte[] final = new byte[3 * Rows * Columns]; ArgbToRgb(pixeldata.Raw, final); PixelData = final; } else { PixelData = pixeldata.Raw; } }
private static YbrToRgb GetYbrToRgbConverter(PhotometricInterpretation photometricInterpretation) { YbrToRgb converter; if (photometricInterpretation == PhotometricInterpretation.YbrFull) converter = new YbrToRgb(YbrFullToRgb); else if (photometricInterpretation == PhotometricInterpretation.YbrFull422) converter = new YbrToRgb(YbrFull422ToRgb); else if (photometricInterpretation == PhotometricInterpretation.YbrIct) converter = new YbrToRgb(YbrIctToRgb); else if (photometricInterpretation == PhotometricInterpretation.YbrPartial422) converter = new YbrToRgb(YbrPartial422ToRgb); else converter = new YbrToRgb(YbrRctToRgb); return converter; }
private static void YbrPlanarToArgb( byte[] ybrPixelData, byte[] argbPixelData, int sizeInPixels, PhotometricInterpretation photometricInterpretation) { fixed (byte* pYbrPixelData = ybrPixelData) { fixed (byte* pArgbPixelData = argbPixelData) { int src = 0; int dst = 0; int bOffset = sizeInPixels; int rOffset = sizeInPixels*2; YbrToRgb converter = GetYbrToRgbConverter(photometricInterpretation); for (int i = 0; i < sizeInPixels; i++) { int rgb = converter( pYbrPixelData[src], pYbrPixelData[src + bOffset], pYbrPixelData[src + rOffset]); pArgbPixelData[dst] = Color.FromArgb(rgb).B; pArgbPixelData[dst + 1] = Color.FromArgb(rgb).G; pArgbPixelData[dst + 2] = Color.FromArgb(rgb).R; pArgbPixelData[dst + 3] = 0xff; src += 1; dst += 4; } } } }
/// <summary> /// Converts pixel data of a particular photometric interpretation /// to ARGB. /// </summary> /// <param name="photometricInterpretation">The <see cref="PhotometricInterpretation"/> of <paramref name="srcPixelData"/>.</param> /// <param name="planarConfiguration">The planar configuration of <paramref name="srcPixelData"/>.</param> /// <param name="srcPixelData">The input pixel data to be converted.</param> /// <param name="argbPixelData">The converted output pixel data in ARGB format.</param> /// <remarks> /// Only RGB and YBR variants can be converted. For PALETTE COLOR, use <see cref="ToArgb(int,bool,byte[],byte[],IDataLut)"/>. /// </remarks> public static void ToArgb( PhotometricInterpretation photometricInterpretation, int planarConfiguration, byte[] srcPixelData, byte[] argbPixelData) { int sizeInPixels = argbPixelData.Length / 4; if (photometricInterpretation == PhotometricInterpretation.Monochrome1 || photometricInterpretation == PhotometricInterpretation.Monochrome2 || photometricInterpretation == PhotometricInterpretation.PaletteColor || photometricInterpretation == PhotometricInterpretation.Unknown) throw new Exception("Invalid photometric interpretation. Must be either RGB or a YBR variant."); if (photometricInterpretation == PhotometricInterpretation.Rgb) { if (planarConfiguration == 0) RgbTripletToArgb(srcPixelData, argbPixelData, sizeInPixels); else RgbPlanarToArgb(srcPixelData, argbPixelData, sizeInPixels); } else { if (planarConfiguration == 0) YbrTripletToArgb(srcPixelData, argbPixelData, sizeInPixels, photometricInterpretation); else YbrPlanarToArgb(srcPixelData, argbPixelData, sizeInPixels, photometricInterpretation); } }