/// <summary>
        /// Deserializes the <see cref="ColourCollection" /> contained by the specified <see cref="Stream" />.
        /// </summary>
        /// <param name="stream">The <see cref="Stream" /> that contains the palette to deserialize.</param>
        /// <returns>The <see cref="ColourCollection" /> being deserialized.</returns>
        public override ColourCollection Deserialise(Stream stream)
        {
            ColourCollection results;

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            results = new ColourCollection();

            for (int i = 0; i < stream.Length / 3; i++)
            {
                int r;
                int g;
                int b;

                r = stream.ReadByte();
                g = stream.ReadByte();
                b = stream.ReadByte();

                results.Add(Color.FromArgb(r, g, b));
            }

            return(results);
        }
        /// <summary>
        /// Deserializes the <see cref="ColourCollection" /> contained by the specified <see cref="Stream" />.
        /// </summary>
        /// <param name="stream">The <see cref="Stream" /> that contains the palette to deserialize.</param>
        /// <returns>The <see cref="ColourCollection" /> being deserialized.</returns>
        public override ColourCollection Deserialise(Stream stream)
        {
            ColourCollection results;

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            results = new ColourCollection();

            using (StreamReader reader = new StreamReader(stream))
            {
                string header;
                string version;
                int    colorCount;

                // check signature
                header  = reader.ReadLine();
                version = reader.ReadLine();

                if (header != "JASC-PAL" || version != "0100")
                {
                    throw new InvalidDataException("Invalid palette file");
                }

                colorCount = Convert.ToInt32(reader.ReadLine());
                for (int i = 0; i < colorCount; i++)
                {
                    int      r;
                    int      g;
                    int      b;
                    string   data;
                    string[] parts;

                    data  = reader.ReadLine();
                    parts = !string.IsNullOrEmpty(data) ? data.Split(new[]
                    {
                        ' ',
                        '\t'
                    }, StringSplitOptions.RemoveEmptyEntries) : new string[0];

                    if (!int.TryParse(parts[0], out r) || !int.TryParse(parts[1], out g) || !int.TryParse(parts[2], out b))
                    {
                        throw new InvalidDataException(string.Format("Invalid palette contents found with data '{0}'", data));
                    }

                    results.Add(Color.FromArgb(r, g, b));
                }
            }

            return(results);
        }
        /// <summary>
        /// Deserializes the <see cref="ColourCollection" /> contained by the specified <see cref="Stream" />.
        /// </summary>
        /// <param name="stream">The <see cref="Stream" /> that contains the palette to deserialize.</param>
        /// <returns>The <see cref="ColourCollection" /> being deserialized.</returns>
        public override ColourCollection Deserialise(Stream stream)
        {
            ColourCollection results;
            int count;

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            results = new ColourCollection();

            count = (int)(stream.Length / 3);

            for (int i = 0; i < count; i++)
            {
                int r;
                int g;
                int b;

                r = stream.ReadByte();
                g = stream.ReadByte();
                b = stream.ReadByte();

                results.Add(Color.FromArgb(r, g, b));
            }

            if (count == 257)
            {
                int realCount;

                // undocumented on the spec page, but when poking around
                // the Optimized Colors preset files of CS2, I found that
                // four extra bytes were added to some files. The second of
                // these extra bytes specified how many colours are
                // really in use (anything else is just padding)

                realCount = results[256].G;

                while (results.Count > realCount)
                {
                    results.RemoveAt(realCount);
                }
            }

            return(results);
        }
        /// <summary>
        /// Deserializes the <see cref="ColourCollection" /> contained by the specified <see cref="Stream" />.
        /// </summary>
        /// <param name="stream">The <see cref="Stream" /> that contains the palette to deserialize.</param>
        /// <returns>The <see cref="ColourCollection" /> being deserialized.</returns>
        public override ColourCollection Deserialise(Stream stream)
        {
            ColourCollection results;

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            results = new ColourCollection();

            using (StreamReader reader = new StreamReader(stream))
            {
                while (!reader.EndOfStream)
                {
                    string line;

                    line = reader.ReadLine();
                    if (!string.IsNullOrEmpty(line) && !line.StartsWith(";") && line.Length == 8)
                    {
                        int a;
                        int r;
                        int g;
                        int b;

                        a = int.Parse(line.Substring(0, 2), NumberStyles.HexNumber);
                        r = int.Parse(line.Substring(2, 2), NumberStyles.HexNumber);
                        g = int.Parse(line.Substring(4, 2), NumberStyles.HexNumber);
                        b = int.Parse(line.Substring(6, 2), NumberStyles.HexNumber);

                        results.Add(Color.FromArgb(a, r, g, b));
                    }
                }
            }

            return(results);
        }
Пример #5
0
        /// <summary>
        /// Deserializes the <see cref="ColourCollection" /> contained by the specified <see cref="Stream" />.
        /// </summary>
        /// <param name="stream">The <see cref="Stream" /> that contains the palette to deserialize.</param>
        /// <returns>The <see cref="ColourCollection" /> being deserialized.</returns>
        public override ColourCollection Deserialise(Stream stream)
        {
            ColourCollection results;

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            results = new ColourCollection();

            using (StreamReader reader = new StreamReader(stream))
            {
                string header;
                int    swatchIndex;
                bool   readingPalette;

                readingPalette = false;

                // check signature
                header = reader.ReadLine();

                if (header != "GIMP Palette")
                {
                    throw new InvalidDataException("Invalid palette file");
                }

                // read the swatches
                swatchIndex = 0;

                while (!reader.EndOfStream)
                {
                    string data;

                    data = reader.ReadLine();

                    if (!string.IsNullOrEmpty(data))
                    {
                        if (data[0] == '#')
                        {
                            // comment
                            readingPalette = true;
                        }
                        else if (!readingPalette)
                        {
                            // custom attribute
                        }
                        else if (readingPalette)
                        {
                            int      r;
                            int      g;
                            int      b;
                            string[] parts;
                            string   name;

                            // TODO: Optimize this a touch. Microoptimization? Maybe.

                            parts = !string.IsNullOrEmpty(data) ? data.Split(new[]
                            {
                                ' ',
                                '\t'
                            }, StringSplitOptions.RemoveEmptyEntries) : new string[0];
                            name = parts.Length > 3 ? string.Join(" ", parts, 3, parts.Length - 3) : null;

                            if (!int.TryParse(parts[0], out r) || !int.TryParse(parts[1], out g) || !int.TryParse(parts[2], out b))
                            {
                                throw new InvalidDataException(string.Format("Invalid palette contents found with data '{0}'", data));
                            }

                            results.Add(Color.FromArgb(r, g, b));
#if USENAMEHACK
                            results.SetName(swatchIndex, name);
#endif

                            swatchIndex++;
                        }
                    }
                }
            }

            return(results);
        }
        protected virtual ColourCollection ReadPalette(Stream stream, AdobePhotoshopColourSwatchFileVersion version)
        {
            int colourCount;
            ColourCollection results;

            results = new ColourCollection();

            // read the number of colors, which also occupies two bytes
            colourCount = this.ReadInt16(stream);

            for (int i = 0; i < colourCount; i++)
            {
                AdobePhotoshopColourSwatchColourSpace colorSpace;
                int    value1;
                int    value2;
                int    value3;
                string name;

                // again, two bytes for the color space
                colorSpace = (AdobePhotoshopColourSwatchColourSpace)this.ReadInt16(stream);

                value1 = this.ReadInt16(stream);
                value2 = this.ReadInt16(stream);
                value3 = this.ReadInt16(stream);
                this.ReadInt16(stream); // only CMYK supports this field. As we can't handle CMYK colors, we read the value to advance the stream but don't do anything with it

                if (version == AdobePhotoshopColourSwatchFileVersion.VERSION2)
                {
                    int length;

                    // need to read the name even though currently our colour collection doesn't support names
                    length = this.ReadInt32(stream);
                    name   = this.ReadString(stream, length);
                }
                else
                {
                    name = string.Empty;
                }

                switch (colorSpace)
                {
                case AdobePhotoshopColourSwatchColourSpace.RGB:
                    int red;
                    int green;
                    int blue;

                    // RGB.
                    // The first three values in the color data are red , green , and blue . They are full unsigned
                    //  16-bit values as in Apple's RGBColor data structure. Pure red = 65535, 0, 0.

                    red   = value1 / 256;
                    green = value2 / 256;
                    blue  = value3 / 256;

                    results.Add(Color.FromArgb(red, green, blue));
                    break;

                case AdobePhotoshopColourSwatchColourSpace.HSB:
                    double hue;
                    double saturation;
                    double brightness;

                    // HSB.
                    // The first three values in the color data are hue , saturation , and brightness . They are full
                    // unsigned 16-bit values as in Apple's HSVColor data structure. Pure red = 0,65535, 65535.

                    hue        = value1 / 182.04;
                    saturation = value2 / 655.35;
                    brightness = value3 / 655.35;

                    results.Add(new HSLColour(hue, saturation, brightness).ToRgbColour());
                    break;

                case AdobePhotoshopColourSwatchColourSpace.GRAYSCALE:

                    int gray;

                    // Grayscale.
                    // The first value in the color data is the gray value, from 0...10000.
                    gray = (int)(value1 / 39.0625);

                    results.Add(Color.FromArgb(gray, gray, gray));
                    break;

                default:
                    throw new InvalidDataException(string.Format("Color space '{0}' not supported.", colorSpace));
                }

#if USENAMEHACK
                results.SetName(i, name);
#endif
            }

            return(results);
        }
        /// <summary>
        /// Deserializes the <see cref="ColourCollection" /> contained by the specified <see cref="Stream" />.
        /// </summary>
        /// <param name="stream">The <see cref="Stream" /> that contains the palette to deserialize.</param>
        /// <returns>The <see cref="ColourCollection" /> being deserialized.</returns>
        public override ColourCollection Deserialise(Stream stream)
        {
            byte[]           buffer;
            string           header;
            ColourCollection results;

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            results = new ColourCollection();

            // read the FORM header that identifies the document as an IFF file
            buffer = new byte[4];
            stream.Read(buffer, 0, buffer.Length);
            if (Encoding.ASCII.GetString(buffer) != "FORM")
            {
                throw new InvalidDataException("Form header not found.");
            }

            // the next value is the size of all the data in the FORM chunk
            // We don't actually need this value, but we have to read it
            // regardless to advance the stream
            this.ReadInt32(stream);

            // read either the PBM or ILBM header that identifies this document as an image file
            stream.Read(buffer, 0, buffer.Length);
            header = Encoding.ASCII.GetString(buffer);
            if (header != "PBM " && header != "ILBM")
            {
                throw new InvalidDataException("Bitmap header not found.");
            }

            while (stream.Read(buffer, 0, buffer.Length) == buffer.Length)
            {
                int chunkLength;

                chunkLength = this.ReadInt32(stream);

                if (Encoding.ASCII.GetString(buffer) != "CMAP")
                {
                    // some other LBM chunk, skip it
                    if (stream.CanSeek)
                    {
                        stream.Seek(chunkLength, SeekOrigin.Current);
                    }
                    else
                    {
                        for (int i = 0; i < chunkLength; i++)
                        {
                            stream.ReadByte();
                        }
                    }
                }
                else
                {
                    // color map chunk!
                    for (int i = 0; i < chunkLength / 3; i++)
                    {
                        int r;
                        int g;
                        int b;

                        r = stream.ReadByte();
                        g = stream.ReadByte();
                        b = stream.ReadByte();

                        results.Add(Color.FromArgb(r, g, b));
                    }

                    // all done so stop reading the rest of the file
                    break;
                }

                // chunks always contain an even number of bytes even if the recorded length is odd
                // if the length is odd, then there's a padding byte in the file - just read and discard
                if (chunkLength % 2 != 0)
                {
                    stream.ReadByte();
                }
            }

            return(results);
        }