public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { if (oldPixelData.UncompressedFrameSize % 2 == 1) { using (var stream = new MemoryStream(oldPixelData.GetFrameFragmentData(frame))) { var data = new byte[oldPixelData.UncompressedFrameSize]; stream.Read(data, 0, data.Length); newPixelData.AppendFrame(data); } } else { newPixelData.AppendFrame(oldPixelData.GetFrameFragmentData(frame)); } }
public void SavePixels(string filename) { DicomPixelData pd = DicomPixelData.CreateFrom(_dicomFile); if (File.Exists(filename)) { File.Delete(filename); } using (FileStream fs = new FileStream(filename, FileMode.CreateNew)) { byte[] ba; DicomCompressedPixelData compressed = pd as DicomCompressedPixelData; if (compressed != null) { ba = compressed.GetFrameFragmentData(0); } else { ba = pd.GetFrame(0); } fs.Write(ba, 0, ba.Length); fs.Flush(); fs.Close(); } }
public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { if (oldPixelData.UncompressedFrameSize%2 == 1) { using (var stream = new MemoryStream(oldPixelData.GetFrameFragmentData(frame))) { var data = new byte[oldPixelData.UncompressedFrameSize]; stream.Read(data, 0, data.Length); newPixelData.AppendFrame(data); } } else { newPixelData.AppendFrame(oldPixelData.GetFrameFragmentData(frame)); } }
public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters) { using (var input = new MemoryStream(oldPixelData.GetFrameFragmentData(frame))) using (var gzipStream = new GZipStream(input, CompressionMode.Decompress, false)) { var data = new byte[oldPixelData.UncompressedFrameSize]; gzipStream.Read(data, 0, data.Length); newPixelData.AppendFrame(data); } }
protected override void Upload(DicomFile dicomObject, int frame, IStorageLocation storeLocation) { var frameIndex = frame - 1; if (dicomObject.TransferSyntax == TransferSyntax.JpegBaselineProcess1) { DicomCompressedPixelData pd = DicomPixelData.CreateFrom(dicomObject) as DicomCompressedPixelData; byte[] buffer = pd.GetFrameFragmentData(frameIndex); storeLocation.Upload(buffer); } else if (false) //TODO: handle compressed images properly! { DicomFile dcmJpeg = new DicomFile( ); DicomUncompressedPixelData unCompressed = DicomPixelData.CreateFrom(dicomObject) as DicomUncompressedPixelData; DicomCompressedPixelData compressed = new DicomCompressedPixelData(unCompressed); //compressed.ImageWidth = unCompressed.ImageWidth; //compressed.ImageHeight = unCompressed.HighBit; compressed.BitsStored = 8; compressed.BitsAllocated = 8; //compressed.HighBit = 7; compressed.SamplesPerPixel = 3; //compressed.PlanarConfiguration = 0; compressed.PhotometricInterpretation = "YBR_FULL_422"; compressed.TransferSyntax = TransferSyntax.JpegBaselineProcess1; byte[] imageBuffer = unCompressed.GetFrame(frameIndex); compressed.AddFrameFragment(imageBuffer); compressed.UpdateMessage(dcmJpeg); storeLocation.Upload(compressed.GetFrame(frameIndex)); //ClearCanvas.Dicom.Codec.Jpeg.Jpeg8Codec codec = new ClearCanvas.Dicom.Codec.Jpeg.Jpeg8Codec (ClearCanvas.Dicom.Codec.Jpeg.JpegMode.Baseline, 0, 0 ) ; //ClearCanvas.Dicom.Codec.Jpeg.DicomJpegParameters jparam = new ClearCanvas.Dicom.Codec.Jpeg.DicomJpegParameters ( ) ; //jparam. //codec. //codec.Encode ( ) } }
//TODO: I should be able to replace this with the media readers now protected override WadoResponse DoProcess(IWadoUriRequest request, string mimeType) { var dcmLocation = MediaStorage.GetLocation(new DicomMediaId(request, MimeMediaTypes.DICOM)); //var dcmLocation = RetrieveService.RetrieveSopInstances ( request, mimeType ).FirstOrDefault(); if (!dcmLocation.Exists( )) { throw new ApplicationException("Object Not Found - return proper wado error "); } //if (string.Compare(mimeType, MimeMediaTypes.DICOM, true) == 0) { return(new WadoResponse(Location, mimeType)); } DicomFile file = new DicomFile( ); var frameIndex = request.ImageRequestInfo.FrameNumber - 1 ?? 0; frameIndex = Math.Max(frameIndex, 0); file.Load(dcmLocation.GetReadStream()); if (string.Compare(mimeType, MimeMediaTypes.Jpeg, true) == 0) { WadoResponse response = new WadoResponse(); if (file.TransferSyntax == TransferSyntax.JpegBaselineProcess1) { //ClearCanvas.Dicom.Codec.Jpeg.Jpeg8Codec codec = new ClearCanvas.Dicom.Codec.Jpeg.Jpeg8Codec (ClearCanvas.Dicom.Codec.Jpeg.JpegMode.Baseline, 0, 0 ) //codec.Encode () DicomCompressedPixelData pd = DicomPixelData.CreateFrom(file) as DicomCompressedPixelData; byte[] buffer = pd.GetFrameFragmentData(frameIndex); response.Content = new MemoryStream(buffer); response.MimeType = mimeType; return(response); } else { } } if (string.Compare(mimeType, MimeMediaTypes.UncompressedData) == 0) { WadoResponse response = null; DicomPixelData pd = null; byte[] buffer = null; response = new WadoResponse( ); pd = DicomPixelData.CreateFrom(file); buffer = pd.GetFrame(frameIndex); //********* TEST CODE*************** //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap (pd.ImageWidth, pd.ImageHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed ) ; //System.Drawing.Imaging.ColorPalette ncp = bitmap.Palette; // for (int i = 0; i < 256; i++) // ncp.Entries[i] = System.Drawing.Color.FromArgb(255, i, i, i); // bitmap.Palette = ncp; // System.Drawing.Imaging.BitmapData data = bitmap.LockBits (new System.Drawing.Rectangle ( 0,0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); //IntPtr ptr = data.Scan0 ; //System.Runtime.InteropServices.Marshal.Copy (buffer, 0, ptr, buffer.Length ) ; //string fileName = @"C:\Users\zalsafadi_p.SPX\Downloads\libwebp-master\Output\release-static\x86\bin\Samples\uncompressed.raw" ; //bitmap.UnlockBits (data); //bitmap.Save ( fileName); //File.WriteAllBytes(fileName, buffer) ; //********* TEST CODE*************** response.Content = new MemoryStream(buffer); response.MimeType = mimeType; return(response); } //if ( string.Compare(mimeType, MimeMediaTypes.WebP) == 0) //{ // WadoResponse response = new WadoResponse ( ) ; // byte[] buffer = File.ReadAllBytes(Location) ; // response.Content = new MemoryStream(buffer); // response.MimeType = mimeType ; // return response ; //} return(null); }