Пример #1
0
        public DirectoryList Extract(Stream inputStream)
        {
            IndexedReader reader         = new IndexedSeekingReader(inputStream);
            var           directory      = new EpsDirectory();
            var           epsDirectories = new List <Directory>()
            {
                directory
            };

            // 0xC5D0D3C6 signifies an EPS Header block which contains 32-bytes of basic information
            // 0x25215053 (%!PS) signifies an EPS File and leads straight into the PostScript

            int startingPosition = (int)inputStream.Position;

            switch (reader.GetInt32(0))
            {
            case unchecked ((int)0xC5D0D3C6):
                reader = reader.WithByteOrder(isMotorolaByteOrder: false);
                int postScriptOffset = reader.GetInt32(4);
                int postScriptLength = reader.GetInt32(8);
                int wmfOffset        = reader.GetInt32(12);
                int wmfSize          = reader.GetInt32(16);
                int tifOffset        = reader.GetInt32(20);
                int tifSize          = reader.GetInt32(24);
                //int checkSum = reader.getInt32(28);

                // Get Tiff/WMF preview data if applicable
                if (tifSize != 0)
                {
                    directory.Set(EpsDirectory.TagTiffPreviewSize, tifSize);
                    directory.Set(EpsDirectory.TagTiffPreviewOffset, tifOffset);
                    // Get Tiff metadata
                    try
                    {
                        ByteArrayReader byteArrayReader = new(reader.GetBytes(tifOffset, tifSize));
                        TiffReader.ProcessTiff(byteArrayReader, new PhotoshopTiffHandler(epsDirectories));
                    }
                    catch (TiffProcessingException ex)
                    {
                        directory.AddError("Unable to process TIFF data: " + ex.Message);
                    }
                }
                else if (wmfSize != 0)
                {
                    directory.Set(EpsDirectory.TagWmfPreviewSize, wmfSize);
                    directory.Set(EpsDirectory.TagWmfPreviewOffset, wmfOffset);
                }

                // TODO avoid allocating byte array here -- read directly from InputStream
                Extract(directory, epsDirectories, new SequentialByteArrayReader(reader.GetBytes(postScriptOffset, postScriptLength)));
                break;

            case 0x25215053:
                inputStream.Position = startingPosition;
                Extract(directory, epsDirectories, new SequentialStreamReader(inputStream));
                break;

            default:
                directory.AddError("File type not supported.");
                break;
            }

            return(epsDirectories);
        }