Exemplo n.º 1
0
        private static Size DecodeJfif(BinaryReader binaryReader)
        {
            while (binaryReader.ReadByte() == 0xff)
            {
                byte  marker      = binaryReader.ReadByte();
                short chunkLength = ByteHelper.ReadInt16BE(binaryReader);
                if (marker == 0xc0 || marker == 0xc2) // c2: progressive
                {
                    binaryReader.ReadByte();
                    int height = ByteHelper.ReadInt16BE(binaryReader);
                    int width  = ByteHelper.ReadInt16BE(binaryReader);
                    return(new Size(width, height));
                }

                if (chunkLength < 0)
                {
                    ushort uchunkLength = (ushort)chunkLength;
                    binaryReader.ReadBytes(uchunkLength - 2);
                }
                else
                {
                    binaryReader.ReadBytes(chunkLength - 2);
                }
            }

            return(Size.Empty);
        }
Exemplo n.º 2
0
        // https://www.cyotek.com/blog/reading-photoshop-color-swatch-aco-files-using-csharp
        public static List <Color> ReadPhotoShopSwatchFile(string fileName)
        {
            List <Color> colorPalette;

            using (Stream stream = File.OpenRead(fileName))
            {
                FileVersion version;

                // read the version, which occupies two bytes
                version = (FileVersion)ByteHelper.ReadInt16BE(stream);

                if (version != FileVersion.Version1 && version != FileVersion.Version2)
                {
                    throw new InvalidDataException("Invalid version information.");
                }

                // the specification states that a version2 palette follows a version1
                // the only difference between version1 and version2 is the inclusion
                // of a name property. Perhaps there's addtional color spaces as well
                // but we can't support them all anyway
                // I noticed some files no longer include a version 1 palette

                colorPalette = ReadSwatches(stream, version);
                if (version == FileVersion.Version1)
                {
                    version = (FileVersion)ByteHelper.ReadInt16BE(stream);
                    if (version == FileVersion.Version2)
                    {
                        colorPalette = ReadSwatches(stream, version);
                    }
                }
            }

            return(colorPalette);
        }
Exemplo n.º 3
0
        private static Size DecodeTiffBE(BinaryReader binaryReader)
        {
            int idfStart = ByteHelper.ReadInt32BE(binaryReader);

            binaryReader.BaseStream.Seek(idfStart, SeekOrigin.Begin);

            int numberOfIDF = ByteHelper.ReadInt16BE(binaryReader);

            int width  = -1;
            int height = -1;

            for (int i = 0; i < numberOfIDF; i++)
            {
                short field = ByteHelper.ReadInt16BE(binaryReader);

                switch (field)
                {
                // https://www.awaresystems.be/imaging/tiff/tifftags/baseline.html
                default:
                    binaryReader.ReadBytes(10);
                    break;

                case 256:     // image width
                    binaryReader.ReadBytes(6);
                    width = ByteHelper.ReadInt32BE(binaryReader);
                    break;

                case 257:     // image length
                    binaryReader.ReadBytes(6);
                    height = ByteHelper.ReadInt32BE(binaryReader);
                    break;
                }
                if (width != -1 && height != -1)
                {
                    return(new Size(width, height));
                }
            }
            return(Size.Empty);
        }
Exemplo n.º 4
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);
        }