context: #region License
  
  // Copyright (c) 2013, ClearCanvas Inc.
  // All rights reserved.
  // http://www.clearcanvas.ca
  //
  // This file is part of the ClearCanvas RIS/PACS open source project.
  //
  // The ClearCanvas RIS/PACS open source project is free software: you can
  // redistribute it and/or modify it under the terms of the GNU General Public
  // License as published by the Free Software Foundation, either version 3 of the
  // License, or (at your option) any later version.
  //
  // The ClearCanvas RIS/PACS open source project is distributed in the hope that it
  // will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  // Public License for more details.
  //
  // You should have received a copy of the GNU General Public License along with
  // the ClearCanvas RIS/PACS open source project.  If not, see
  // <http://www.gnu.org/licenses/>.
  
  #endregion
  
  #if UNIT_TESTS
  
  using System;
  using NUnit.Framework;
  
  namespace ClearCanvas.Dicom.Tests
  {
      [TestFixture]
      public class FileTest : AbstractTest
      {
          [Test]
          public void ConstructorTests()
          {
              DicomFile file = new DicomFile(null);
  
              file = new DicomFile("filename");
  
              file = new DicomFile(null, new DicomAttributeCollection(), new DicomAttributeCollection());
  
  
          }
  
          public void WriteOptionsTest(DicomFile sourceFile, DicomWriteOptions options)
          {
              bool result = sourceFile.Save(options);
  
              Assert.AreEqual(result, true);
  
              DicomFile newFile = new DicomFile(sourceFile.Filename);
  
              DicomReadOptions readOptions = DicomReadOptions.Default;
              newFile.Load(readOptions);
  
              Assert.AreEqual(sourceFile.DataSet.Equals(newFile.DataSet), true);
  
              // update a tag, and make sure it shows they're different
              newFile.DataSet[DicomTags.PatientsName].Values = "NewPatient^First";
              Assert.AreEqual(sourceFile.DataSet.Equals(newFile.DataSet), false);
  
              // Now update the original file with the name, and they should be the same again
              sourceFile.DataSet[DicomTags.PatientsName].Values = "NewPatient^First";
              Assert.AreEqual(sourceFile.DataSet.Equals(newFile.DataSet), true);
  
              // Return the original string value.
              sourceFile.DataSet[DicomTags.PatientsName].SetStringValue("Patient^Test");
  
          }
  
          [Test]
          public void MultiframeCreateFileTest()
          {
              DicomFile file = new DicomFile("MultiframeCreateFileTest.dcm");
  
              DicomAttributeCollection dataSet = file.DataSet;
  
              SetupMultiframeXA(dataSet, 511, 511, 5);
  
              SetupMetaInfo(file);
  
              // Little Endian Tests
              file.TransferSyntax = TransferSyntax.ExplicitVrLittleEndian;
  
              DicomWriteOptions writeOptions = DicomWriteOptions.Default;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequence;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequenceItem;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.None;
              WriteOptionsTest(file, writeOptions);
  
              // Big Endian Tests
              file.Filename = "MultiframeBigEndianCreateFileTest.dcm";
              file.TransferSyntax = TransferSyntax.ExplicitVrBigEndian;
  
              writeOptions = DicomWriteOptions.Default;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequence;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequenceItem;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.None;
              WriteOptionsTest(file, writeOptions);
  
          }
  
          [Test]
          public void CreateFileTest()
          {
              DicomFile file = new DicomFile("CreateFileTest.dcm");
  
              DicomAttributeCollection dataSet = file.DataSet;
  
              SetupMR(dataSet);
  
  
              string studyId = file.DataSet[DicomTags.StudyId].GetString(0, "");
              Assert.AreEqual(studyId, "1933");
  
              SetupMetaInfo(file);
  
              // Little Endian Tests
              file.TransferSyntax = TransferSyntax.ExplicitVrLittleEndian; 
  
              DicomWriteOptions writeOptions = DicomWriteOptions.Default;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequence;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequenceItem;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.None;
              WriteOptionsTest(file, writeOptions);
  
              // Big Endian Tests
              file.Filename = "BigEndianCreateFileTest.dcm";
              file.TransferSyntax = TransferSyntax.ExplicitVrBigEndian;
  
              writeOptions = DicomWriteOptions.Default;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequence;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequenceItem;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem;
              WriteOptionsTest(file, writeOptions);
  
              writeOptions = DicomWriteOptions.None;
              WriteOptionsTest(file, writeOptions);
  
          }
  
          public void ReadOptionsTest(DicomFile sourceFile, DicomReadOptions options, bool areEqual)
          {
              bool result = sourceFile.Save(DicomWriteOptions.Default);
  
              Assert.AreEqual(result, true);
  
              DicomFile newFile = new DicomFile(sourceFile.Filename);
  
              newFile.Load(options);
  
              if (areEqual)
                  Assert.AreEqual(sourceFile.DataSet.Equals(newFile.DataSet), true);
              else
                  Assert.AreNotEqual(sourceFile.DataSet.Equals(newFile.DataSet), true);
          }
  
          [Test]
          public void FileReadTest()
          {
              DicomFile file = new DicomFile("LittleEndianReadFileTest.dcm");
  
              DicomAttributeCollection dataSet = file.DataSet;
  
              SetupMR(dataSet);
  
              SetupMetaInfo(file);
  
              // Little Endian Tests
              file.TransferSyntax = TransferSyntax.ExplicitVrLittleEndian;
  
              DicomReadOptions readOptions = DicomReadOptions.Default;
              ReadOptionsTest(file, readOptions, true);
  
              readOptions = DicomReadOptions.StorePixelDataReferences;
              ReadOptionsTest(file, readOptions, true);
  
              readOptions = DicomReadOptions.DoNotStorePixelDataInDataSet;
              ReadOptionsTest(file, readOptions, false);
  
              readOptions = DicomReadOptions.None;
              ReadOptionsTest(file, readOptions, true);
  
  
              // Big Endian Tests
              file.Filename = "BigEndianReadTest.dcm";
              file.TransferSyntax = TransferSyntax.ExplicitVrBigEndian;
  
              readOptions = DicomReadOptions.Default;
              ReadOptionsTest(file, readOptions, true);
  
              readOptions = DicomReadOptions.StorePixelDataReferences;
              ReadOptionsTest(file, readOptions, true);
  
              readOptions = DicomReadOptions.DoNotStorePixelDataInDataSet;
              ReadOptionsTest(file, readOptions, false);
  
              readOptions = DicomReadOptions.None;
              ReadOptionsTest(file, readOptions, true);
  
          }
          [Test]
          public void MultiframeReadTest()
          {
              DicomFile file = new DicomFile("LittleEndianMultiframeTest.dcm");
  
              DicomAttributeCollection dataSet = file.DataSet;
  
              SetupMultiframeXA(dataSet, 511, 511, 5);
  
              SetupMetaInfo(file);
  
              // Little Endian Tests
              file.TransferSyntax = TransferSyntax.ExplicitVrLittleEndian;
  
              DicomReadOptions readOptions = DicomReadOptions.Default;
              ReadOptionsTest(file, readOptions, true);
  
              readOptions = DicomReadOptions.StorePixelDataReferences;
              ReadOptionsTest(file, readOptions, true);
  
              readOptions = DicomReadOptions.DoNotStorePixelDataInDataSet;
              ReadOptionsTest(file, readOptions, false);
  
              readOptions = DicomReadOptions.None;
              ReadOptionsTest(file, readOptions, true);
  
  
              // Big Endian Tests
              file.Filename = "BigEndianMultiframeTest.dcm";
              file.TransferSyntax = TransferSyntax.ExplicitVrBigEndian;
  
              readOptions = DicomReadOptions.Default;
              ReadOptionsTest(file, readOptions, true);
  
              readOptions = DicomReadOptions.StorePixelDataReferences;
              ReadOptionsTest(file, readOptions, true);
  
              readOptions = DicomReadOptions.DoNotStorePixelDataInDataSet;
              ReadOptionsTest(file, readOptions, false);
  
              readOptions = DicomReadOptions.None;
              ReadOptionsTest(file, readOptions, true);
  
          }
  
          private void CheckPixels(string filename)
          {
              DicomReadOptions readOptions = DicomReadOptions.StorePixelDataReferences;
              DicomFile newFile = new DicomFile(filename);
  
              newFile.Load(readOptions);
              DicomUncompressedPixelData pd = new DicomUncompressedPixelData(newFile.DataSet);
  
  
gt:             for (int frame = 0; frame < pd.NumberOfFrames; frame++)