コード例 #1
0
        public void EncodeImage(IFileReader fileReader, IFileWriter fileWriter, IImagePredictor imagePredictor)
        {
            if (fileReader == null)
            {
                throw new ArgumentNullException(nameof(fileReader));
            }

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

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

            ValidateImageFromFileReader(fileReader);
            GetImageFromFileReader(fileReader);

            ImageCodes       = new byte[OriginalImage.Width, OriginalImage.Height];
            PredictionMatrix = new byte[OriginalImage.Width, OriginalImage.Height];
            ErrorMatrix      = new int[OriginalImage.Width, OriginalImage.Height];

            UpdateImageCodes();

            HandleFirstPixel();
            HandleFirstColumn(imagePredictor);
            HandleFirstRow(imagePredictor);

            for (int row = 1; row < 256; row++)
            {
                for (int column = 1; column < 256; column++)
                {
                    var a = ImageCodes[row - 1, column];
                    var b = ImageCodes[row, column - 1];
                    var c = ImageCodes[row - 1, column - 1];

                    var prediction = imagePredictor.PredictValue(a, b, c);

                    HandlePrediction(row, column, prediction);
                }
            }

            CopyBitmapHeader(fileReader, fileWriter);
            WriteUsedImagePredictor(fileWriter, imagePredictor);
            errorMatrixWriter.WriteErrorMatrix(ErrorMatrix, fileWriter);

            fileWriter.Flush();
        }
コード例 #2
0
        public void EncodeFile(IFileReader fileReader, IFileWriter fileWriter, int bitsForOffset, int bitsForLength)
        {
            if (fileReader == null)
            {
                throw new ArgumentNullException(nameof(fileReader));
            }

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

            if (bitsForOffset < 3 || bitsForOffset > 15)
            {
                throw new ArgumentException($"{nameof(bitsForOffset)} should be between 3 and 15");
            }

            if (bitsForLength < 2 || bitsForLength > 7)
            {
                throw new ArgumentException($"{nameof(bitsForLength)} should be between 3 and 15");
            }

            if (Lz77Buffer.Length != bitsForLength || Lz77Buffer.Offset != bitsForOffset)
            {
                Lz77Buffer.SetOffsetAndLimit(bitsForLength, bitsForLength);
            }

            WriteHeader(fileWriter, bitsForOffset, bitsForLength);

            TokensFromPreviousRun.Clear();
            lz77BufferManager.TryToFillLookAheadBuffer(Lz77Buffer, fileReader);

            while (Lz77Buffer.LookAheadBuffer.Count > 0)
            {
                var lz77Token = lz77TokenExtractor.GetLz77TokenFromLz77Buffer(Lz77Buffer);

                lz77BufferManager.TryToFillSearchBufferBasedOnLz77Token(Lz77Buffer, lz77Token);
                lz77BufferManager.EmptyLookAheadBufferBasedOnLz77Token(Lz77Buffer, lz77Token);
                lz77BufferManager.TryToFillLookAheadBuffer(Lz77Buffer, fileReader);

                lz77TokenWriter.WriteToken(lz77Token, fileWriter, bitsForOffset, bitsForLength);

                TokensFromPreviousRun.Add(lz77Token);
            }

            fileWriter.Flush();
        }
        public void Encode(string sourceFilepath, string destinationFilepath, NearLosslessOptions nearLosslessOptions)
        {
            var image         = GetImageOrThrow(sourceFilepath);
            var imageMatrices = new ImageMatrices(image);

            image.Dispose();

            PredictionMatrixHelper.SetImageMatrices(imageMatrices, nearLosslessOptions);

            fileReader.Open(sourceFilepath);
            fileWriter.Open(destinationFilepath);

            CopyBitmapHeader();
            WriteOptions(nearLosslessOptions);
            var errorMatrixWriter = NearLosslessErrorMatrixWriterSelector.GetErrorMatrixWriter(nearLosslessOptions.SaveMode);

            errorMatrixWriter.WriteErrorMatrix(imageMatrices.QuantizedErrors, fileWriter);
            fileWriter.Flush();

            fileReader.Close();
            fileWriter.Close();
        }
コード例 #4
0
        public void TestPartialReadAll(string schemaStr, Codec.Type codecType)
        {
            // create and write out
            IList <Foo> records = MakeRecords(GetTestFooObject());

            MemoryStream dataFileOutputStream = new MemoryStream();

            Schema            schema  = Schema.Parse(schemaStr);
            DatumWriter <Foo> writer  = new SpecificWriter <Foo>(schema);
            int            numRecords = 0;
            List <SyncLog> syncLogs   = new List <SyncLog>();

            using (IFileWriter <Foo> dataFileWriter = DataFileWriter <Foo> .OpenWriter(writer, dataFileOutputStream, Codec.CreateCodec(codecType)))
            {
                dataFileWriter.Flush();
                syncLogs.Add(new SyncLog {
                    Position = dataFileOutputStream.Position - DataFileConstants.SyncSize + 1, RemainingRecords = numRecords
                });
                long lastPosition = dataFileOutputStream.Position;
                for (int i = 0; i < 10; ++i)
                {
                    foreach (Foo foo in records)
                    {
                        dataFileWriter.Append(foo);
                        if (dataFileOutputStream.Position != lastPosition)
                        {
                            syncLogs.Add(new SyncLog {
                                Position = dataFileOutputStream.Position - DataFileConstants.SyncSize + 1, RemainingRecords = numRecords
                            });
                            lastPosition = dataFileOutputStream.Position;
                        }
                        numRecords++;
                    }

                    // write out block
                    if (i == 1 || i == 4)
                    {
                        dataFileWriter.Sync();
                        syncLogs.Add(new SyncLog {
                            Position = dataFileOutputStream.Position - DataFileConstants.SyncSize + 1, RemainingRecords = numRecords
                        });
                        lastPosition = dataFileOutputStream.Position;
                    }
                }
                dataFileWriter.Flush();
                syncLogs.Add(new SyncLog {
                    Position = dataFileOutputStream.Position, RemainingRecords = numRecords
                });
            }

            MemoryStream dataFileInputStream = new MemoryStream(dataFileOutputStream.ToArray());

            // read back
            using (IFileReader <Foo> reader = DataFileReader <Foo> .OpenReader(dataFileInputStream))
            {
                long curPosition = 0;

                foreach (SyncLog syncLog in syncLogs)
                {
                    int  expectedRecords = numRecords - syncLog.RemainingRecords;
                    long nextSyncPoint   = syncLog.Position;
                    AssertNumRecordsFromPosition(reader, curPosition, expectedRecords);
                    AssertNumRecordsFromPosition(reader, nextSyncPoint - 1, expectedRecords);
                    curPosition = nextSyncPoint;
                }
            }
        }
コード例 #5
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            var    stateValues      = state as IEnumerable <KeyValuePair <string, object> >;
            string formattedMessage = formatter?.Invoke(state, exception);

            // If we don't have a message, there's nothing to log.
            if (string.IsNullOrEmpty(formattedMessage))
            {
                return;
            }

            bool isSystemTrace = Utility.GetStateBoolValue(stateValues, ScriptConstants.LogPropertyIsSystemLogKey);

            if (isSystemTrace)
            {
                // System traces are not logged to files.
                return;
            }

            bool isPrimaryHostTrace = Utility.GetStateBoolValue(stateValues, ScriptConstants.LogPropertyPrimaryHostKey);

            if (isPrimaryHostTrace && !_isPrimary())
            {
                return;
            }

            if (exception != null)
            {
                if (exception is FunctionInvocationException ||
                    exception is AggregateException)
                {
                    // we want to minimize the stack traces for function invocation
                    // failures, so we drill into the very inner exception, which will
                    // be the script error
                    Exception actualException = exception;
                    while (actualException.InnerException != null)
                    {
                        actualException = actualException.InnerException;
                    }

                    formattedMessage += $"{Environment.NewLine}{actualException.Message}";
                }
                else
                {
                    formattedMessage += $"{Environment.NewLine}{exception.ToFormattedString()}";
                }
            }

            formattedMessage = FormatLine(stateValues, logLevel, formattedMessage);

            try
            {
                _fileWriter.AppendLine(formattedMessage);

                // flush errors immediately
                if (logLevel == LogLevel.Error || exception != null)
                {
                    _fileWriter.Flush();
                }
            }
            catch (Exception)
            {
                // Make sure the Logger doesn't throw if there are Exceptions (disk full, etc).
            }
        }
コード例 #6
0
        public void EncodeFile(IFileReader fileReader, IFileWriter fileWriter, OnFullDictionaryOption onFullDictionaryOption, int numberOfBitsIndex)
        {
            if (fileReader == null)
            {
                throw new ArgumentNullException(nameof(fileReader));
            }

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

            if (numberOfBitsIndex < 9 || numberOfBitsIndex > 15)
            {
                throw new ArgumentException($"{nameof(numberOfBitsIndex)} must be at least 9, and at most 15");
            }

            WriteHeader(fileWriter, onFullDictionaryOption, numberOfBitsIndex);
            IndexesFromLastRun.Clear();

            LzWDictionary = new LzWDictionary((int)Math.Pow(2, numberOfBitsIndex) - 1, onFullDictionaryOption);

            var lastCharacter = (char)fileReader.ReadBits(8);
            var shouldStop = false;

            while (true)
            {
                var currentString = lastCharacter.ToString();
                uint lastIndex = 0;

                if (shouldStop)
                {
                    break;
                }

                while (true)
                {
                    if (LzWDictionary.ContainsString(currentString))
                    {
                        lastIndex = LzWDictionary.GetIndexByString(currentString);

                        if (shouldStop)
                        {
                            fileWriter.WriteValueOnBits(lastIndex, (byte)numberOfBitsIndex);
                            IndexesFromLastRun.Add(lastIndex);

                            break;
                        }
                    }
                    else
                    {
                        LzWDictionary.Add(currentString);

                        fileWriter.WriteValueOnBits(lastIndex, (byte)numberOfBitsIndex);
                        IndexesFromLastRun.Add(lastIndex);

                        break;
                    }

                    if (!fileReader.ReachedEndOfFile)
                    {
                        var readByte = (byte)fileReader.ReadBits(8);
                        currentString += (char)readByte;
                        lastCharacter = (char)readByte;
                    }
                    else
                    {
                        shouldStop = true;
                    }
                }
            }

            fileWriter.Flush();
        }
コード例 #7
0
 public void FlushBuffer()
 {
     FileWriter?.Flush();
 }