Example #1
0
        /// <summary>
        /// Load color look-up table from file.
        /// </summary>
        /// <param name="path">File name.</param>
        /// <returns>Loaded color look-up table.</returns>
        public static Color32[] LoadLUT(string path)
        {
            try
            {
                byte[] data;
                using (var stream = IOManager.CreateFileReference(path).OpenRead())
                {
                    var length = (int)stream.Length;
                    if (length != (256 * 3)) return null;

                    data = new byte[length];
                    stream.Read(data, 0, length);
                }

                var lut = new Color32[256];
                for (var i = 0; i < 256; i++)
                {
                    lut[i] = new Color32(0xff, data[i], data[i + 256], data[i + 512]);
                }

                return lut;
            }
            catch
            {
                return null;
            }
        }
Example #2
0
 /// <summary>
 /// Returns the reverse of an existing color table.
 /// </summary>
 /// <param name="lut">Look-up table subject to reversal.</param>
 /// <returns>Reversed look-up table.</returns>
 public static Color32[] Reverse(Color32[] lut)
 {
     Color32[] clone = new Color32[lut.Length];
     Array.Copy(lut, clone, clone.Length);
     Array.Reverse(clone);
     return clone;
 }
Example #3
0
		public static void SaveLUT(string file, Color32[] lut) {
			if (lut.Length != 256) return;
			FileStream fs = new FileStream(file, FileMode.Create);
			for (int i = 0; i < 256; i++) fs.WriteByte(lut[i].R);
			for (int i = 0; i < 256; i++) fs.WriteByte(lut[i].G);
			for (int i = 0; i < 256; i++) fs.WriteByte(lut[i].B);
			fs.Close();
		}
		public static void SaveLUT(string file, Color32[] lut) {
			if (lut.Length != 256) return;
			using (FileStream fs = new FileStream(file, FileMode.Create))
			{
				fs.Write(lut.Select(color => color.R).ToArray(), 0, lut.Length);
				fs.Write(lut.Select(color => color.G).ToArray(), 0, lut.Length);
				fs.Write(lut.Select(color => color.B).ToArray(), 0, lut.Length);
			}
		}
		private static Color32[] InitGrayscaleLUT(bool reverse) {
			Color32[] LUT = new Color32[256];
			int i;
			byte b;
			if (reverse) {
				for (i = 0, b = 255; i < 256; i++, b--) {
					LUT[i] = new Color32(0xff, b, b, b);
				}
			} else {
				for (i = 0, b = 0; i < 256; i++, b++) {
					LUT[i] = new Color32(0xff, b, b, b);
				}
			}
			return LUT;
		}
Example #6
0
		public static Color32[] LoadLUT(string file) {
			try {
				byte[] data = File.ReadAllBytes(file);
				if (data.Length != (256 * 3))
					return null;

				Color32[] LUT = new Color32[256];
				for (int i = 0; i < 256; i++) {
					LUT[i] = new Color32(0xff, data[i], data[i + 256], data[i + 512]);
				}
				return LUT;
			} catch {
				return null;
			}
		}
		public static Color32[] LoadLUT(string file) {
			try {
				byte[] data; //= File.ReadAllBytes(file);
			    using (var stream = File.OpenRead(file))
			    {
			        var count = stream.Length;
                    data = new byte[count];
			        stream.Read(data, 0, (int)count);
			    }
			    if (data.Length != (256 * 3))
					return null;

				Color32[] LUT = new Color32[256];
				for (int i = 0; i < 256; i++) {
					LUT[i] = new Color32(0xff, data[i], data[i + 256], data[i + 512]);
				}
				return LUT;
			} catch {
				return null;
			}
		}
Example #8
0
        /// <summary>
        /// Extracts the palette color LUT from DICOM dataset, valid for PALETTE COLOR <seealso cref="PhotometricInterpretation"/>
        /// </summary>
        /// <returns>Palette color LUT</returns>
        /// <exception cref="DicomImagingException">Invalid photometric interpretation or plaette color lUT missing from database</exception>
        private Color32[] GetPaletteColorLUT()
        {
            if (PhotometricInterpretation != PhotometricInterpretation.PaletteColor)
                throw new DicomImagingException(
                    "Attempted to get Palette Color LUT from image with invalid photometric interpretation.");

            if (!Dataset.Contains(DicomTag.RedPaletteColorLookupTableDescriptor)) throw new DicomImagingException("Palette Color LUT missing from dataset.");

            int size = Dataset.Get<int>(DicomTag.RedPaletteColorLookupTableDescriptor, 0);
            int first = Dataset.Get<int>(DicomTag.RedPaletteColorLookupTableDescriptor, 1);
            int bits = Dataset.Get<int>(DicomTag.RedPaletteColorLookupTableDescriptor, 2);

            var r = Dataset.Get<byte[]>(DicomTag.RedPaletteColorLookupTableData);
            var g = Dataset.Get<byte[]>(DicomTag.GreenPaletteColorLookupTableData);
            var b = Dataset.Get<byte[]>(DicomTag.BluePaletteColorLookupTableData);

            // If the LUT size is 0, that means it's 65536 in size.
            if (size == 0) size = 65536;

            var lut = new Color32[size];

            if (r.Length == size)
            {
                // 8-bit LUT entries
                for (int i = 0; i < size; i++) lut[i] = new Color32(0xff, r[i], g[i], b[i]);
            }
            else
            {
                // 16-bit LUT entries... we only support 8-bit until someone can find a sample image with a 16-bit palette

                // 8-bit entries with 16-bits allocated
                int offset = 0;

                // 16-bit entries with 8-bits stored
                if (bits == 16) offset = 1;

                for (int i = 0; i < size; i++, offset += 2) lut[i] = new Color32(0xff, r[offset], g[offset], b[offset]);
            }

            return lut;
        }
Example #9
0
        /// <summary>
        /// Save color look-up table to file.
        /// </summary>
        /// <param name="path">File name.</param>
        /// <param name="lut">Look-up table to save.</param>
        public static void SaveLUT(string path, Color32[] lut)
        {
            if (lut.Length != 256) return;

            var file = IOManager.CreateFileReference(path);
            using (var fs = file.Create())
            {
                for (var i = 0; i < 256; i++) fs.WriteByte(lut[i].R);
                for (var i = 0; i < 256; i++) fs.WriteByte(lut[i].G);
                for (var i = 0; i < 256; i++) fs.WriteByte(lut[i].B);
            }
        }