cornerstonejs / cornerstoneWADOImageLoader

[Deprecated] Use Cornerstone3D Instead https://cornerstonejs.org/
MIT License
285 stars 264 forks source link

Issue with loading dicom images in WADO loader in OHIF Viewer #514

Open mmkarthickrajan opened 1 year ago

mmkarthickrajan commented 1 year ago

I am working on a web based OHIF Dicom viewer. I am using clear canvas as my PACs server. So I developed one broker application in .net core which works like WADO-RS and supply information to OHIF viewer from clear canvas. In my broker application I am passing metadata to OHIF viewer in json format by using FO-dicom json converter which converts dcm file into Json string.

My code for sending metadata:

                    List<JObject> lstjo = new List<JObject>();
                    JObject jo;
                    foreach (var file in files)
                    {
                        var dicomDirectory = DicomFile.Open(file, FileReadOption.ReadAll);
                        if (dicomDirectory.Dataset.InternalTransferSyntax.UID.UID != DicomTransferSyntax.ImplicitVRLittleEndian.UID.UID)
                        {
                            var transcoder = new DicomTranscoder(dicomDirectory.Dataset.InternalTransferSyntax, DicomTransferSyntax.ImplicitVRLittleEndian);
                            dicomDirectory = transcoder.Transcode(dicomDirectory);
                        }
                        JsonDicomConverter dicomConverter = new JsonDicomConverter();
                        StringBuilder sb = new StringBuilder();
                        StringWriter sw = new StringWriter(sb);
                        JsonWriter writer = new JsonTextWriter(sw);
                        Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                        dicomConverter.WriteJson(writer, dicomDirectory.Dataset, serializer);
                        jo = JObject.Parse(sb.ToString());
                       // jo.Property("7FE00010").Remove();
                        retJsonstring += jo.ToString() + ",";
                    }
                    if (retJsonstring.Length > 6)
                    {
                        retJsonstring = retJsonstring.Substring(0, retJsonstring.Length - 1) + "]";
                    }
                    else
                    {
                        retJsonstring += "]";
                    }
                    retJsonstring = retJsonstring.Replace("\r", "").Replace("\n", "");
                }
                return retJsonstring;

During passing metadata there is no issue in Ohif viewer. After then OHIF viewer sending WADORS request for frames to display. My broker application also send responding for that request in multipart.

My code for sending Multipart response :

          {
              var dicomFile = DicomFile.Open(path, FileReadOption.ReadAll);
              string transfersyntax = dicomFile.Dataset.InternalTransferSyntax.UID.UID;
              MemoryStream streamContent = new MemoryStream();
              if (transfersyntax != DicomTransferSyntax.ImplicitVRLittleEndian.UID.UID)
              {
                  var transcoder = new DicomTranscoder(dicomFile.Dataset.InternalTransferSyntax, DicomTransferSyntax.ImplicitVRLittleEndian);
                  dicomFile = transcoder.Transcode(dicomFile);                        
              }
              dicomFile.Save(streamContent);
              DicomImage img = new DicomImage(dicomFile.Dataset, 0);

              streamContent.Seek(0, SeekOrigin.Begin);
              string boundary = Guid.NewGuid().ToString();
              MultipartContent multipartContent = new MultipartContent();

              //newFile.Save(multipartContent.Stream);
              multipartContent.Stream = streamContent;// File.OpenRead(path);
              multipartContent.ContentType = "application/octet-stream";
              multipartContent.transfersyntax = dicomFile.Dataset.InternalTransferSyntax.UID.UID;
              multipartContent.FileName = "";
              multiContentResult = new MultipartResult("related", boundary) { multipartContent };                    
              return multiContentResult;                  
          }

Mulitpart class and MulticontentResult Class:

 public class MultipartContent
  {
    public string ContentType { get; set; }
    public string FileName { get; set; }
    public Stream Stream { get; set; }
    public string transfersyntax { get; set; }
 }

public class MultipartResult : Collection<MultipartContent>, IActionResult
{
    private readonly System.Net.Http.MultipartContent content;
    public MultipartResult(string subtype = "byteranges", string boundary = null)
    {
        if (boundary == null)
        {
            this.content = new System.Net.Http.MultipartContent(subtype);
        }
        else
        {
            this.content = new System.Net.Http.MultipartContent(subtype, boundary);
        }
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        foreach (var item in this)
        {
            if (item.Stream != null)
            {
                var content = new StreamContent(item.Stream);
                if (item.ContentType != null)
                {
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(item.ContentType);
                    content.Headers.ContentType.Parameters.Add(new System.Net.Http.Headers.NameValueHeaderValue("transfer-syntax", item.transfersyntax));
                }
                this.content.Add(content);
            }
        }
        context.HttpContext.Response.ContentLength = content.Headers.ContentLength;
        context.HttpContext.Response.ContentType = content.Headers.ContentType.ToString();
        await content.CopyToAsync(context.HttpContext.Response.Body);
    }
}

After sending WAROrs response , I getting error in OHIF viewer in RangeError: offset is out of bounds in stackviewport.js during set pixeldata flat32array to scaledata flat32array. (REFER BELOW ATTACHED IMAGE )

So I inspect in browser after then I come know that Pixel data size and scaledata size is different. (REFER BELOW ATTACHED IMAGE )

To verify my dcm file I checked with ohif viewer by directly opened those file in https://v3-demo.ohif.org/local. It is opening properly.

So what are possible reason for this issue ? how to rectify? error1 error2