Ejemplo n.º 1
0
        /// <summary>
        /// Create image from stream. Pfim will try to detect the format based on several leading bytes
        /// </summary>
        public static IImage FromStream(Stream stream, PfimConfig config)
        {
            byte[] magic = new byte[4];
            if (stream.Read(magic, 0, 4) != 4)
            {
                throw new ArgumentException("stream must contain magic header", nameof(stream));
            }

            if (magic[0] == 0x44 && magic[1] == 0x44 && magic[2] == 0x53 && magic[3] == 0x20)
            {
                return(Dds.CreateSkipMagic(stream, config));
            }

            return(Targa.CreateWithPartialHeader(stream, config, magic));
        }
Ejemplo n.º 2
0
        private static Targa DecodeTarga(Stream str, PfimConfig config, TargaHeader header)
        {
            var targa = (header.IsCompressed)
                ? (IDecodeTarga)(new CompressedTarga())
                : new UncompressedTarga();

            byte[] data;
            switch (header.Orientation)
            {
            case TargaHeader.TargaOrientation.BottomLeft:
                data = targa.BottomLeft(str, header, config);
                break;

            case TargaHeader.TargaOrientation.BottomRight:
                data = targa.BottomRight(str, header, config);
                break;

            case TargaHeader.TargaOrientation.TopRight:
                data = targa.TopRight(str, header, config);
                break;

            case TargaHeader.TargaOrientation.TopLeft:
                data = targa.TopLeft(str, header, config);
                break;

            default:
                throw new Exception("Targa orientation not recognized");
            }

            var stride = Util.Stride(header.Width, header.PixelDepthBits);
            var len    = header.Height * stride;
            var result = new Targa(header, config, data, len);

            if (config.ApplyColorMap)
            {
                result.ApplyColorMap();
            }

            return(result);
        }