public static void TransformImage(DisplayData currentPage, IntPtr outputRGBBuffer) { try { if (outputRGBBuffer == IntPtr.Zero) { return; } PresentationState pstate = currentPage.PState; int renderWidth = pstate.RenderWidth; int renderHeight = pstate.RenderHeight; int samplesPerPixel = currentPage.SamplesPerPixel; int width = currentPage.Width; int height = currentPage.Height; int pixelBits = currentPage.PixelBits; int pixelRepresentation = currentPage.PixelRepresentation; double rescaleSlope = currentPage.RescaleSlope; double rescaleIntercept = currentPage.RescaleIntercept; //if a image is a original PET image, its SopClassUID is 1.2.840.10008.5.1.4.1.1.128, So we use bIsOriPT to describe //if a image, its Modality is PT and it's not a RGB image, we use bIsNotRGBPT to describe bool bIsOriPT = (currentPage.SOPClassUID == "1.2.840.10008.5.1.4.1.1.128"); bool bIsNotRGBPT = (currentPage.Modality == Modality.PT && currentPage.SamplesPerPixel == 1); bool bIsPseudoColor = false; if (bIsOriPT) { bIsPseudoColor = !(PresentationState.PalettesEqual(pstate.Palette, PresentationState.DefaultPalette) || PresentationState.PalettesEqual(pstate.Palette, PresentationState.GetInversePalette(PresentationState.DefaultPalette))); } IntPtr imageDataPtr = currentPage.CreateSafePixelDataPtr(); IntPtr imageMaskPtr = currentPage.CreateSafeMaskDataPtr(); if (imageDataPtr != IntPtr.Zero) { NativeMethods.ImageTransform(renderWidth, renderHeight, 0, 0, renderWidth, renderHeight, imageDataPtr, imageMaskPtr, outputRGBBuffer, width, height, samplesPerPixel, pixelBits, pixelRepresentation, rescaleSlope, rescaleIntercept, pstate.RotationClockwise, pstate.OffsetLeft, pstate.OffsetTop, pstate.ScaleX, pstate.ScaleY, pstate.WindowLevel.WindowCenter, pstate.WindowLevel.WindowWidth, pstate.EnhancePara, pstate.PaletteData, (bIsOriPT && !bIsPseudoColor) ? !pstate.IsPaletteReversed : pstate.IsPaletteReversed, (int)DisplayData.InterpMode.Bilinear, bIsNotRGBPT); } else { int stride = (renderWidth * 3 % 4 == 0) ? (renderWidth * 3) : ((renderWidth * 3 / 4 + 1) * 4); NativeMethods.MemSet(outputRGBBuffer, 0, (uint)(stride * renderHeight)); } currentPage.FreeSafePixelDataPtr(); currentPage.FreeSafeMaskDataPtr(); } catch (Exception exp) { MedViewerLogger.Instance.LogDevError(MedViewerLogger.Source, "Exception: " + exp.ToString()); } }
public static WriteableBitmap ConvertDisplayDataToBitmap(WriteableBitmap bitmap, DisplayData currentPage) { try { if (currentPage.SkipInterpolationForColorImage && (currentPage.SamplesPerPixel == 3 || currentPage.SamplesPerPixel == 4)) { if (currentPage.SamplesPerPixel == 3) { //if (bitmap.PixelWidth != currentPage.Width || bitmap.PixelHeight != currentPage.Height || // bitmap.Format != PixelFormats.Rgb24) //{ bitmap = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight, 96, 96, PixelFormats.Rgb24, null); // this.ImageControl.Source = bitmap; //} } else if (currentPage.SamplesPerPixel == 4) { //if (bitmap.PixelWidth != currentPage.Width || bitmap.PixelHeight != currentPage.Height || // bitmap.Format != PixelFormats.Bgra32) //{ bitmap = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight, 96, 96, PixelFormats.Bgra32, null); // this.ImageControl.Source = bitmap; //} } IntPtr imageDataPtr = currentPage.CreateSafePixelDataPtr(); bitmap.Lock(); bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); NativeMethods.CopyMemory(bitmap.BackBuffer, imageDataPtr, bitmap.BackBufferStride * bitmap.PixelHeight); currentPage.FreeSafePixelDataPtr(); } else { //if (this.DataSource.ViewerControlSetting.UseAsynchronizedRender) //{ // byte[] rgbPixels = new byte[bitmap.BackBufferStride * bitmap.PixelHeight]; // Task.Factory.StartNew(new Action(() => { // unsafe // { // fixed (byte* imageDataPtr = rgbPixels) // { // TransformImage(currentPage, new IntPtr(imageDataPtr)); // bitmap.Dispatcher.BeginInvoke(new Action(() => { // var rect = new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight); // bitmap.Lock(); // bitmap.AddDirtyRect(rect); // bitmap.WritePixels(rect, rgbPixels, bitmap.BackBufferStride, 0); // bitmap.Unlock(); // })); // } // } // })); //} //else //{ bitmap.Lock(); bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); TransformImage(currentPage, bitmap.BackBuffer); //} } } catch (Exception exp) { MedViewerLogger.Instance.LogDevError(MedViewerLogger.Source, "Exception: " + exp.ToString()); } finally { bitmap.Unlock(); } return(bitmap); }