public void ConvertToJson() { var testDir = Path.Combine(TestDirPath, "convertToJson"); JsonDicomConverter jsonConverter = new JsonDicomConverter( ); Directory.CreateDirectory(testDir); foreach (string file in Directory.GetFiles(DicomHelpers.GetSampleImagesFolder( ))) { string fullPath = Path.Combine(testDir, Path.GetFileName(file)); fo.DicomDataset sourceDS = fo.DicomFile.Open(file).Dataset; jsonConverter.WriteInlineBinary = true; string sourceJsonDicom = jsonConverter.Convert(sourceDS); System.IO.File.WriteAllText(fullPath + ".jsn", sourceJsonDicom); fo.DicomDataset targetDs = jsonConverter.Convert(sourceJsonDicom); var dsF = new fo.DicomFile(targetDs); dsF.Save(fullPath + ".jsn.dcm"); string destJsonDicom = jsonConverter.Convert(targetDs); System.IO.File.WriteAllText(fullPath + ".gen.jsn", destJsonDicom); Assert.AreEqual(sourceJsonDicom, destJsonDicom); } }
public async Task <HttpResponseMessage> ProcessRequest ( IWebStoreRequest request, string studyInstanceUID ) { DicomAttributeCollection bodyContent = null; switch (request.MediaType) { //TODO: build the response here, { Successes.Add(objectMetadata), Failures.Add(objectMetadata), Create case MimeMediaTypes.DICOM: { bodyContent = await GetResponseDataset(request, studyInstanceUID); } break; case MimeMediaTypes.xmlDicom: { } break; case MimeMediaTypes.Json: { } break; default: { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } } if (null != bodyContent) { var result = new HttpResponseMessage(HttpStatusCode.OK); if (new MimeMediaType(MimeMediaTypes.Json).IsIn(request.AcceptHeader)) //this is not taking the "q" parameter { JsonDicomConverter converter = new JsonDicomConverter( ); result.Content = new StringContent(converter.Convert(bodyContent), System.Text.Encoding.UTF8, MimeMediaTypes.Json); } else { XmlDicomConverter xmlConverter = new XmlDicomConverter( ); result.Content = new StringContent(xmlConverter.Convert(bodyContent), System.Text.Encoding.UTF8, MimeMediaTypes.xmlDicom); } return(result); } else { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }
private static void ValidateResponseMetadataDataset(DicomDataset storedDataset, DicomDataset retrievedDataset) { // Compare result datasets by serializing. var jsonDicomConverter = new JsonDicomConverter(); Assert.Equal( JsonConvert.SerializeObject(storedDataset, jsonDicomConverter), JsonConvert.SerializeObject(retrievedDataset, jsonDicomConverter)); }
private HttpResponseMessage SearchForDicomEntity ( IQidoRequestModel request, DicomAttributeCollection dicomSource, DoQueryDelegate doQuery ) { if (null != request.Query) { var matchingParams = request.Query.MatchingElements; var includeParams = request.Query.IncludeElements; foreach (var queryParam in matchingParams) { string paramValue = queryParam.Value; InsertDicomElement(dicomSource, queryParam.Key, paramValue); } foreach (var returnParam in includeParams) { InsertDicomElement(dicomSource, returnParam, ""); } ICollection <DicomAttributeCollection> results = doQuery(QueryService, dicomSource, request.Limit, request.Offset); //TODO: move configuration params into their own object StringBuilder jsonReturn = new StringBuilder("["); JsonDicomConverter converter = new JsonDicomConverter( ) { IncludeEmptyElements = true }; foreach (var response in results) { jsonReturn.AppendLine(converter.Convert(response)); jsonReturn.Append(","); } if (results.Count > 0) { jsonReturn.Remove(jsonReturn.Length - 1, 1); } jsonReturn.Append("]"); return(new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent(jsonReturn.ToString( ), Encoding.UTF8, MimeMediaTypes.Json) }); } return(null); }
public void ConvertFromJson(string sourceJsonFile, string destinationDicomFile) { JsonDicomConverter jsonConverter = new JsonDicomConverter( ); string jsonDataset = System.IO.File.ReadAllText(sourceJsonFile); DicomDataset dataset = jsonConverter.Convert(jsonDataset); DicomFile dsF = new DicomFile(dataset); dsF.Save(destinationDicomFile); }
private static void ValidateResponseMetadataDataset(DicomDataset storedDataset, DicomDataset retrievedDataset) { // Trim the stored dataset to the expected items in the response metadata dataset (remove non-supported value representations). DicomDataset expectedDataset = storedDataset.CopyWithoutBulkDataItems(); // Compare result datasets by serializing. var jsonDicomConverter = new JsonDicomConverter(); Assert.Equal( JsonConvert.SerializeObject(expectedDataset, jsonDicomConverter), JsonConvert.SerializeObject(retrievedDataset, jsonDicomConverter)); Assert.Equal(expectedDataset.Count(), retrievedDataset.Count()); }
public void ConvertToJson(string sourceDicomFile, string destinationJsonFile) { JsonDicomConverter jsonConverter = new JsonDicomConverter( ) { WriteInlineBinary = true }; DicomDataset sourceDS = DicomFile.Open(sourceDicomFile).Dataset; string sourceJsonDicom = jsonConverter.Convert(sourceDS); System.IO.File.WriteAllText(destinationJsonFile, sourceJsonDicom); }
private async Task ValidateResponseMetadataDatasetAsync( DicomWebAsyncEnumerableResponse <DicomDataset> response, DicomDataset storedInstance1, DicomDataset storedInstance2) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("application/dicom+json", response.ContentHeaders.ContentType.MediaType); DicomDataset[] datasets = await response.ToArrayAsync(); Assert.Equal(2, datasets.Length); // Trim the stored dataset to the expected items in the response metadata dataset (remove non-supported value representations). DicomDataset expectedDataset1 = storedInstance1.CopyWithoutBulkDataItems(); DicomDataset expectedDataset2 = storedInstance2.CopyWithoutBulkDataItems(); DicomDataset retrievedDataset1 = datasets[0]; DicomDataset retrievedDataset2 = datasets[1]; // Compare result datasets by serializing. var jsonDicomConverter = new JsonDicomConverter(); string serializedExpectedDataset1 = JsonConvert.SerializeObject(expectedDataset1, jsonDicomConverter); string serializedExpectedDataset2 = JsonConvert.SerializeObject(expectedDataset2, jsonDicomConverter); string serializedRetrievedDataset1 = JsonConvert.SerializeObject(retrievedDataset1, jsonDicomConverter); string serializedRetrievedDataset2 = JsonConvert.SerializeObject(retrievedDataset2, jsonDicomConverter); if (string.Equals(serializedExpectedDataset1, serializedRetrievedDataset1, StringComparison.InvariantCultureIgnoreCase) && string.Equals(serializedExpectedDataset2, serializedRetrievedDataset2, StringComparison.InvariantCultureIgnoreCase)) { Assert.Equal(expectedDataset1.Count(), retrievedDataset1.Count()); Assert.Equal(expectedDataset2.Count(), retrievedDataset2.Count()); return; } else if (string.Equals(serializedExpectedDataset2, serializedRetrievedDataset1, StringComparison.InvariantCultureIgnoreCase) && string.Equals(serializedExpectedDataset1, serializedRetrievedDataset2, StringComparison.InvariantCultureIgnoreCase)) { Assert.Equal(expectedDataset2.Count(), retrievedDataset1.Count()); Assert.Equal(expectedDataset1.Count(), retrievedDataset2.Count()); return; } Assert.False(true, "Retrieved dataset doesnot match the stored dataset"); }
static void Main(string[] args) { string workingDir = AppDomain.CurrentDomain.BaseDirectory; string dicomFile = Path.Combine(workingDir, "I10"); var dataset = new DicomFile(dicomFile); dataset.Load(); var converter = new JsonDicomConverter(); string result = converter.Convert(dataset.DataSet); string output = Path.Combine(workingDir, "output.json"); using (var file = new FileStream(output, FileMode.Create)) { byte[] bytes = Encoding.UTF8.GetBytes(result); file.Write(bytes, 0, bytes.Length); } }
private void ValidateResponseMetadataDataset(DicomWebResponse <IReadOnlyList <DicomDataset> > response, DicomDataset storedInstance1, DicomDataset storedInstance2) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("application/dicom+json", response.Content.Headers.ContentType.MediaType); Assert.Equal(2, response.Value.Count()); // Trim the stored dataset to the expected items in the response metadata dataset (remove non-supported value representations). DicomDataset expectedDataset1 = storedInstance1.CopyWithoutBulkDataItems(); DicomDataset expectedDataset2 = storedInstance2.CopyWithoutBulkDataItems(); DicomDataset retrievedDataset1 = response.Value.First(); DicomDataset retrievedDataset2 = response.Value.Last(); // Compare result datasets by serializing. var jsonDicomConverter = new JsonDicomConverter(); string serializedExpectedDataset1 = JsonConvert.SerializeObject(expectedDataset1, jsonDicomConverter); string serializedExpectedDataset2 = JsonConvert.SerializeObject(expectedDataset2, jsonDicomConverter); string serializedRetrievedDataset1 = JsonConvert.SerializeObject(retrievedDataset1, jsonDicomConverter); string serializedRetrievedDataset2 = JsonConvert.SerializeObject(retrievedDataset2, jsonDicomConverter); if (string.Equals(serializedExpectedDataset1, serializedRetrievedDataset1, StringComparison.InvariantCultureIgnoreCase) && string.Equals(serializedExpectedDataset2, serializedRetrievedDataset2, StringComparison.InvariantCultureIgnoreCase)) { Assert.Equal(expectedDataset1.Count(), retrievedDataset1.Count()); Assert.Equal(expectedDataset2.Count(), retrievedDataset2.Count()); return; } else if (string.Equals(serializedExpectedDataset2, serializedRetrievedDataset1, StringComparison.InvariantCultureIgnoreCase) && string.Equals(serializedExpectedDataset1, serializedRetrievedDataset2, StringComparison.InvariantCultureIgnoreCase)) { Assert.Equal(expectedDataset2.Count(), retrievedDataset1.Count()); Assert.Equal(expectedDataset1.Count(), retrievedDataset2.Count()); return; } Assert.False(true, "Retrieved dataset doesnot match the stored dataset"); }
private static HttpResponseMessage CreateJsonResponse(IEnumerable <DicomDataset> results) { HttpResponseMessage response; StringBuilder jsonReturn = new StringBuilder("["); JsonDicomConverter converter = new JsonDicomConverter( ) { IncludeEmptyElements = true }; int count = 0; foreach (var dsResponse in results) { count++; jsonReturn.AppendLine(converter.Convert(dsResponse)); jsonReturn.Append(","); } if (count > 0) { jsonReturn.Remove(jsonReturn.Length - 1, 1); } jsonReturn.Append("]"); response = new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent(jsonReturn.ToString( ), Encoding.UTF8, MimeMediaTypes.Json) }; return(response); }
private void ValidateResponseDataset( QueryResource resource, DicomDataset storedInstance, DicomDataset responseInstance) { DicomDataset expectedDataset = storedInstance.Clone(); HashSet <DicomTag> levelTags = new HashSet <DicomTag>(); switch (resource) { case QueryResource.AllStudies: levelTags.Add(DicomTag.StudyInstanceUID); levelTags.Add(DicomTag.PatientID); levelTags.Add(DicomTag.PatientName); levelTags.Add(DicomTag.StudyDate); break; case QueryResource.AllSeries: levelTags.Add(DicomTag.StudyInstanceUID); levelTags.Add(DicomTag.PatientID); levelTags.Add(DicomTag.PatientName); levelTags.Add(DicomTag.StudyDate); levelTags.Add(DicomTag.SeriesInstanceUID); levelTags.Add(DicomTag.Modality); break; case QueryResource.AllInstances: levelTags.Add(DicomTag.StudyInstanceUID); levelTags.Add(DicomTag.PatientID); levelTags.Add(DicomTag.PatientName); levelTags.Add(DicomTag.StudyDate); levelTags.Add(DicomTag.SeriesInstanceUID); levelTags.Add(DicomTag.Modality); levelTags.Add(DicomTag.SOPInstanceUID); levelTags.Add(DicomTag.SOPClassUID); levelTags.Add(DicomTag.BitsAllocated); break; case QueryResource.StudySeries: levelTags.Add(DicomTag.StudyInstanceUID); levelTags.Add(DicomTag.SeriesInstanceUID); levelTags.Add(DicomTag.Modality); break; case QueryResource.StudyInstances: levelTags.Add(DicomTag.StudyInstanceUID); levelTags.Add(DicomTag.SeriesInstanceUID); levelTags.Add(DicomTag.Modality); levelTags.Add(DicomTag.SOPInstanceUID); levelTags.Add(DicomTag.SOPClassUID); levelTags.Add(DicomTag.BitsAllocated); break; case QueryResource.StudySeriesInstances: levelTags.Add(DicomTag.StudyInstanceUID); levelTags.Add(DicomTag.SeriesInstanceUID); levelTags.Add(DicomTag.SOPInstanceUID); levelTags.Add(DicomTag.SOPClassUID); levelTags.Add(DicomTag.BitsAllocated); break; } expectedDataset.Remove((di) => { return(!levelTags.Contains(di.Tag)); }); // Compare result datasets by serializing. var jsonDicomConverter = new JsonDicomConverter(); Assert.Equal( JsonConvert.SerializeObject(expectedDataset, jsonDicomConverter), JsonConvert.SerializeObject(responseInstance, jsonDicomConverter)); Assert.Equal(expectedDataset.Count(), responseInstance.Count()); }
public ResponseContent Convert(ConverterData data) { List <ConvertedFileInfo> convertedDcm = new List <ConvertedFileInfo>(); ResponseContent responseContent = new ResponseContent( ); if (data.Files.Count == 0) { responseContent.Status = System.Net.HttpStatusCode.NoContent; return(responseContent); } switch (data.Format) { case KnownFormats.JSON: { foreach (var file in data.Files) { var converter = new JsonDicomConverter(); var dcmFile = Dicom.DicomFile.Open(new MemoryStream(file.Data)); converter.WriteInlineBinary = true; convertedDcm.Add(new ConvertedFileInfo( ) { FileName = Path.ChangeExtension(file.FileName, KnownFormats.JSON), Data = Encoding.UTF8.GetBytes(converter.Convert(dcmFile.Dataset)) }); } } break; case KnownFormats.XML: { foreach (var file in data.Files) { var converter = new XmlDicomConverter(); var dcmFile = Dicom.DicomFile.Open(new MemoryStream(file.Data)); converter.WriteInlineBinary = true; convertedDcm.Add(new ConvertedFileInfo( ) { FileName = Path.ChangeExtension(file.FileName, KnownFormats.XML), Data = Encoding.UTF8.GetBytes(converter.Convert(dcmFile.Dataset)) }); } } break; case KnownFormats.JPG: { foreach (var file in data.Files) { FileStorageService storageService = new FileStorageService(Path.GetTempPath()); DicomMediaIdFactory mediaIdFactory = new DicomMediaIdFactory(); JpegMediaWriter writers = new JpegMediaWriter(storageService, mediaIdFactory); var dcmFile = Dicom.DicomFile.Open(new MemoryStream(file.Data)); var locations = writers.CreateMedia(new DicomMediaWriterParameters() { Dataset = dcmFile.Dataset, MediaInfo = new DicomMediaProperties() { MediaType = writers.MediaType } }); int count = 1; foreach (var location in locations) { var fileName = locations.Count > 1 ? Path.ChangeExtension((Path.GetFileNameWithoutExtension(file.FileName) + count++), KnownFormats.JPG): Path.ChangeExtension(file.FileName, KnownFormats.JPG); using (var stream = location.Download()) { convertedDcm.Add(new ConvertedFileInfo( ) { Data = stream.ReadAllBytes(), FileName = fileName }); } location.Delete(); } } } break; default: { responseContent.Status = System.Net.HttpStatusCode.UnsupportedMediaType; } break; } responseContent.FileName = Path.ChangeExtension(convertedDcm.First().FileName, "zip"); responseContent.Content = new ByteArrayContent(ZippingService.CreateZipData(convertedDcm)); responseContent.Status = System.Net.HttpStatusCode.OK; return(responseContent); }
private HttpResponseMessage SearchForDicomEntity ( IQidoRequestModel request, fo.DicomDataset dicomSource, DoQueryDelegate doQuery ) { if (null != request.Query) { var matchingParams = request.Query.MatchingElements; var includeParams = request.Query.IncludeElements; foreach (var returnParam in includeParams) { InsertDicomElement(dicomSource, returnParam, ""); } foreach (var queryParam in matchingParams) { string paramValue = queryParam.Value; InsertDicomElement(dicomSource, queryParam.Key, paramValue); } ICollection <fo.DicomDataset> results = doQuery(QueryService, dicomSource, request); //TODO: move configuration params into their own object if (MultipartResponseHelper.IsMultiPartRequest(request)) { if (MultipartResponseHelper.GetSubMediaType(request.AcceptHeader.FirstOrDefault( )) == MimeMediaTypes.xmlDicom) { HttpResponseMessage response; MultipartContent multiContent; response = new HttpResponseMessage( ); multiContent = new MultipartContent("related", MultipartResponseHelper.DicomDataBoundary); response.Content = multiContent; foreach (var result in results) { XmlDicomConverter converter = new XmlDicomConverter( ); MultipartResponseHelper.AddMultipartContent(multiContent, new WadoResponse(new MemoryStream(Encoding.ASCII.GetBytes(converter.Convert(result))), MimeMediaTypes.xmlDicom)); } multiContent.Headers.ContentType.Parameters.Add(new System.Net.Http.Headers.NameValueHeaderValue("type", "\"" + MimeMediaTypes.xmlDicom + "\"")); return(response); } else { return(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)); } } else { StringBuilder jsonReturn = new StringBuilder("["); JsonDicomConverter converter = new JsonDicomConverter( ) { IncludeEmptyElements = true }; foreach (var response in results) { jsonReturn.AppendLine(converter.Convert(response)); jsonReturn.Append(","); } if (results.Count > 0) { jsonReturn.Remove(jsonReturn.Length - 1, 1); } jsonReturn.Append("]"); return(new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent(jsonReturn.ToString( ), Encoding.UTF8, MimeMediaTypes.Json) }); } } return(null); }