Beispiel #1
0
        // https://www.cyotek.com/blog/reading-photoshop-color-swatch-aco-files-using-csharp
        private static List <Color> ReadSwatches(Stream stream, FileVersion version)
        {
            int          colorCount;
            List <Color> results;

            results = new List <Color>();

            // read the number of colors, which also occupies two bytes
            colorCount = ByteHelper.ReadInt16BE(stream);

            for (int i = 0; i < colorCount; i++)
            {
                ColorSpace colorSpace;
                int        value1;
                int        value2;
                int        value3;
                int        value4;

                // again, two bytes for the color space
                colorSpace = (ColorSpace)(ByteHelper.ReadInt16BE(stream));

                value1 = ByteHelper.ReadInt16BE(stream);
                value2 = ByteHelper.ReadInt16BE(stream);
                value3 = ByteHelper.ReadInt16BE(stream);
                value4 = ByteHelper.ReadInt16BE(stream);

                if (version == FileVersion.Version2)
                {
                    int length;

                    // need to read the name even though currently our colour collection doesn't support names
                    length = ByteHelper.ReadInt32BE(stream);
                    Helper.ReadString(stream, length);
                }

                switch (colorSpace)
                {
                case ColorSpace.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;   // 0-255
                    green = value2 / 256;   // 0-255
                    blue  = value3 / 256;   // 0-255

                    results.Add(ARGB.FromARGB(red.ToByte(), green.ToByte(), blue.ToByte()));
                    break;

                case ColorSpace.Hsb:
                    float hue;
                    float saturation;
                    float 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.04f; // 0-359
                    saturation = value2 / 655.35f; // 0-100
                    brightness = value3 / 655.35f; // 0-100

                    results.Add(new HSB(hue, saturation, brightness).ToColor());
                    break;

                case ColorSpace.Grayscale:

                    int gray;

                    // Grayscale.
                    // The first value in the color data is the gray value, from 0...10000.

                    gray = (int)(value1 / 39.0625);     // 0-255

                    results.Add(ARGB.FromARGB(gray.ToByte(), gray.ToByte(), gray.ToByte()));
                    break;

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

            return(results);
        }
Beispiel #2
0
        // https://www.cyotek.com/blog/loading-the-color-palette-from-a-bbm-lbm-image-file-using-csharp#files
        public static List <Color> ReadColorMap(string fileName)
        {
            List <Color> colorPalette;

            colorPalette = new List <Color>();

            using (FileStream stream = File.OpenRead(fileName))
            {
                byte[] buffer;
                string header;

                // 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
                ByteHelper.ReadInt32BE(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 = ByteHelper.ReadInt32BE(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();

                            colorPalette.Add(ARGB.FromARGB(255, r.ToByte(), g.ToByte(), b.ToByte()));
                        }

                        // 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(colorPalette);
        }