LaurelBridge.DCF.Examples.EncodeDecodeUnicode Namespace
LAUREL BRIDGE

LaurelBridge.DCF.Examples.EncodeDecodeUnicode Namespace

DICOM Connectivity Framework V3.4
The EncodeDecodeUnicode example demonstrates how DCF converts various character dialects.
Classes

  ClassDescription
Public classProgram
This example demonstrates how the encoding and decoding of unicode string values integrates with the DCF library.
Examples

EncodeDecodeUnicode Sample Code
public class Program
{
    private static readonly string _iso2022ir13_iso2022ir87_sampleText =
        "ヘッロ・ヲッルド is encoded in Japanese using the\r\n" +
        "ISO 2022 IR 13 and ISO 2022 IR 87 Character Sets.";
    private static readonly string _iso2022ir13_iso2022ir87_patientName = "ヤマダ^タロウ=山田^太郎=やまだ^たろう";
    private static readonly string _specificCharacterSet = "ISO 2022 IR 13\\ISO 2022 IR 87";
    private static readonly string _tmpFilePath = "EncodeDecodeUnicodeSampleDataset.dcm";
    private static readonly string _outputFilePath = "EncodeDecodeUnicodeOutputText.txt";

    /// <summary>
    /// Main entry point that demonstrates the encoding and decoding process for the given sample strings.
    /// </summary>
    public static void Main()
    {
        try
        {
            DicomDataSet dds = new DicomDataSet();
            DicomDataSet encodedDataSet;

            // The ElementFactory, where applicable, takes as input the DICOM Elements attribute tag,
            // the string to encode, and the specific character set to use when encoding. 
            // The specific character set is not required when creating any element that does not need encoding.
            dds.Insert(Tags.SpecificCharacterSet, _specificCharacterSet);
            dds.Insert(Tags.PatientName, _iso2022ir13_iso2022ir87_patientName);
            dds.Insert(Tags.PatientComments, _iso2022ir13_iso2022ir87_sampleText);

            // The encoding of any unicode strings within a dataset is deferred until write, be that to a file or a stream.
            using (DicomFileOutput dfo = new DicomFileOutput(_tmpFilePath, Uids.ExplicitVRLittleEndian))
            {
                dfo.CreateChapter10Format = false;
                dfo.WriteDataSet(dds);
            }

            // Read the dataset back in order to compare the results with the original unicode data
            using (DicomFileInput dfi = new DicomFileInput(_tmpFilePath))
            {
                encodedDataSet = dfi.ReadDataSet();
            }

            string output = PrintDataSetInfo(dds, encodedDataSet);
            WriteOutputToFile(_outputFilePath, output);
            Process.Start("notepad.exe", _outputFilePath);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception caught during execution: {0}", e);
        }

        if (Debugger.IsAttached)
        {
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey();
        }
    }

    /// <summary>
    /// Formats the original and resultant dataset unicodecode string data.
    /// </summary>
    /// <param name="originalDataSet">Original dataset</param>
    /// <param name="encodedDataSet">Encoded dataset</param>
    /// <returns>the dataset info</returns>
    private static string PrintDataSetInfo(DicomDataSet originalDataSet, DicomDataSet encodedDataSet)
    {
        DicomElement origScsElement = originalDataSet.GetElement(Tags.SpecificCharacterSet);
        DicomElement origPatientsNameElement = originalDataSet.GetElement(Tags.PatientName);
        DicomElement origPatientCommentsElement = originalDataSet.GetElement(Tags.PatientComments);

        DicomElement encodedScsElement = encodedDataSet.GetElement(Tags.SpecificCharacterSet);
        string encodedScs = encodedScsElement.ValuesAsString();
        DicomElement encodedPatientsNameElement = encodedDataSet.GetElement(Tags.PatientName);
        DicomElement encodedPatientCommentsElement = encodedDataSet.GetElement(Tags.PatientComments);

        StringBuilder sb = new StringBuilder();

        sb.AppendLine("DICOM Internationalization Example:");
        sb.AppendFormat("{0}Original Values:{0}", Environment.NewLine);
        sb.AppendFormat("{1}{0}", Environment.NewLine, origScsElement);
        sb.AppendFormat("{1}{0}",
            Environment.NewLine,
            origPatientsNameElement);
        sb.AppendFormat("{1}{0}",
            Environment.NewLine,
            origPatientCommentsElement);
        sb.AppendFormat("After Encoding and Decoding:{0}", Environment.NewLine);
        sb.AppendFormat("{1}{0}", Environment.NewLine, encodedScs);
        sb.AppendFormat("{1}{0}{2}{0}",
            Environment.NewLine,
            encodedPatientsNameElement,
            HexDump.ToHexString(((DicomStringElement)encodedPatientsNameElement).GetStringBuffer(encodedScs).Bytes));
        sb.AppendFormat("{1}{0}{2}{0}",
            Environment.NewLine,
            encodedPatientCommentsElement,
            HexDump.ToHexString(((DicomStringElement)encodedPatientCommentsElement).GetStringBuffer(encodedScs).Bytes));

        return sb.ToString();
    }

    /// <summary>
    /// Write the given output text to disk in UTF8.
    /// </summary>
    /// <param name="outputFile">The output file path</param>
    /// <param name="outputText">The text to write</param>
    private static void WriteOutputToFile(string outputFile, string outputText)
    {
        using (StreamWriter writer = new StreamWriter(outputFile, false, Encoding.UTF8))
        {
            writer.Write(outputText);
        }
    }
}