Beispiel #1
0
        /// <summary>
        /// Convert a Chinese string to utf8 string
        /// </summary>
        /// <param name="str">the Chinese string</param>
        /// <param name="srcharacterSet">the DICOM DataSet Specific characterSet</param>
        /// <returns>the utf8 encoded Chinese string</returns>
        public static string ToUTF8ChineseString(this string str, string srcharacterSet)
        {
            ///////////////////////////////////////////////////
            // the str from DICOM is already encode with the specific character set
            // so we need decode the bytes by the specific character set
            //////////////////////////////////////////////////
            var srcEncode = SpecificCharacterSetParser.GetEncoding(srcharacterSet);
            var bytes     = srcEncode.GetBytes(str);

            if (str.IsGBChinese())
            {
                var gbEncode = Encoding.GetEncoding("GB18030");
                var utfBytes = Encoding.Convert(gbEncode, Encoding.UTF8, bytes);
                return(Encoding.UTF8.GetString(utfBytes));
            }
            if (str.IsUTF8Chinese())
            {
                return(Encoding.UTF8.GetString(bytes));
            }
            return(str);
        }
Beispiel #2
0
        /// <summary>
        /// Makes sure the new value can be encoded with the specific character set in the dicom file header. If necessary and if it's allowed,
        /// convert the dicom file to use unicode (ISO_IR 192).
        /// </summary>
        /// <param name="file"></param>
        /// <param name="attr"></param>
        /// <param name="desiredValue"></param>
        /// <exception cref="DicomCharacterSetException">Thrown if unicode is not allowed or (for some reason) the value cannot be encoded using unicode</exception>
        private void EnsureCharacterSetIsGood(DicomFile file, DicomAttribute attr, string desiredValue)
        {
            var encodedValue = attr.GetEncodedString(file.TransferSyntax, file.DataSet.SpecificCharacterSet);

            var diff = Diff(attr.Tag, encodedValue, desiredValue);

            if (diff < 0)
            {
                // it's all good
                return;
            }

            // The current specific character set does not support the new value. Try to encode it using unicode (if it's configured)
            if (!CanSaveInUnicode)
            {
                Platform.Log(LogLevel.Debug, "'{0}' cannot be encoded using current Character Set and Unicode is not allowed. Aborting..", desiredValue);

                //Throw DicomCharacterSetException
                var    characterSetDescriptions = SpecificCharacterSetParser.GetCharacterSetInfoDescriptions(file.DataSet.SpecificCharacterSet);
                string instanceNumber           = file.DataSet[DicomTags.InstanceNumber].ToString();
                string instanceUid = file.DataSet[DicomTags.SopInstanceUid].ToString();
                char   badChar     = diff >= desiredValue.Length ? desiredValue[desiredValue.Length - 1] : desiredValue[diff];
                var    error       = string.Format("SOP {5}\n\nCannot set {0} to {1}. Character {2} is not covered by character set {3} [{4}].",
                                                   UpdateEntry.TagPath.Tag.Name, desiredValue, badChar, file.DataSet.SpecificCharacterSet,
                                                   StringUtilities.Combine(characterSetDescriptions, ","),
                                                   string.Format("#{0} [{1}]", instanceNumber, instanceUid)
                                                   );

                Platform.Log(LogLevel.Error, error);
                throw new DicomCharacterSetException(UpdateEntry.TagPath.Tag.TagValue, file.DataSet.SpecificCharacterSet, desiredValue, error);
            }


            // The attribute value has been set, only need to set specific character set to unicode
            const string newSpecificCharacterSet = "ISO_IR 192";

            // Before we commit the change, let's verify again if that's good.
            Platform.Log(LogLevel.Debug, "'{0}'cannot be encoded using current Character Set and Unicode is allowed. Checking for value consistency if switching to Unicode encoding", desiredValue);
            encodedValue = attr.GetEncodedString(file.TransferSyntax, newSpecificCharacterSet);
            diff         = Diff(attr.Tag, encodedValue, desiredValue);
            if (diff >= 0)
            {
                // not ok?
                var    characterSetDescriptions = SpecificCharacterSetParser.GetCharacterSetInfoDescriptions(file.DataSet.SpecificCharacterSet);
                string instanceNumber           = file.DataSet[DicomTags.InstanceNumber].ToString();
                string instanceUid = file.DataSet[DicomTags.SopInstanceUid].ToString();
                char   badChar     = diff >= desiredValue.Length ? desiredValue[desiredValue.Length - 1] : desiredValue[diff];
                var    error       = string.Format("SOP {5}\n\nCannot set {0} to {1}. Character {2} is not covered by character set {3} [{4}]. Attempt to use {6} did not solve the problem.",
                                                   UpdateEntry.TagPath.Tag.Name, desiredValue, badChar, file.DataSet.SpecificCharacterSet,
                                                   StringUtilities.Combine(characterSetDescriptions, ","),
                                                   string.Format("#{0} [{1}]", instanceNumber, instanceUid),
                                                   newSpecificCharacterSet
                                                   );

                Platform.Log(LogLevel.Error, error);
                throw new DicomCharacterSetException(UpdateEntry.TagPath.Tag.TagValue, file.DataSet.SpecificCharacterSet, desiredValue, error);
            }

            Platform.Log(LogLevel.Debug, "Specific Character Set for SOP {0} is now changed to unicode", file.MediaStorageSopInstanceUid);

            file.DataSet.SpecificCharacterSet = newSpecificCharacterSet;
            file.DataSet[DicomTags.SpecificCharacterSet].SetStringValue(newSpecificCharacterSet);
        }