public override void Write(byte[] buffer, int offset, int count)
        {
            int currentChunkCapacity = (int)(Length - Position);

            int fillCurrentImage(int sourceBytesToCopy, int sourceOffset)
            {
                for (var to = sourceBytesToCopy + sourceOffset; sourceOffset < to; sourceOffset++)
                {
                    CurrentChunk[Position++] = buffer[sourceOffset];
                }

                CheckForNextImage();
                return(sourceOffset);
            }

            int copyCompleteImage(int offset)
            {
                byte[] nextChunk = new byte[Length];
                Array.Copy(buffer, offset, nextChunk, 0, Length);
                _chunks.Enqueue(nextChunk);
                return((int)(offset + Length));
            }

            if (currentChunkCapacity > count)
            {
                fillCurrentImage(count, offset);
                return;
            }

            // current image is too small
            if (count > currentChunkCapacity)
            {
                offset = fillCurrentImage(currentChunkCapacity, offset);
            }

            int remainingCounted = count - offset;

            // copy image-block-wise
            for (; remainingCounted >= Length; remainingCounted = count - offset)
            {
                offset = copyCompleteImage(offset);
            }

            // fill until source is depleted
            if (remainingCounted > 0)
            {
                fillCurrentImage(remainingCounted, offset);
                return;
            }
        }
Пример #2
0
        private async Task ParseInputStream(IBoundedQueue <MutableByteImage> queue, IBoundedQueue <byte[]> chunksQueue, int width, int height, ChunkedSimpleMemoryStream memoryStream)
        {
            int count = 0;

            while (true)
            {
                try
                {
                    var item = await chunksQueue.DequeueOrDefault();

                    if (item == default)
                    {
                        break;
                    }

                    _logger.NotifyFillstate(++count, "ParsedImages");
                    _logger.NotifyFillstate(chunksQueue.Count, "ChunkedQueue");
                    await queue.Enqueue(_factory.FromBytes(width, height, item));
                }
                catch (Exception e) { _logger.LogException(e); }
            }
            if (memoryStream.HasUnwrittenData)
            {
                _logger.WriteLine("Unwritten data exists after finishing parsings." +
                                  " This indicates a severe issue with frame splitting", Verbosity.Warning);
            }
        }
Пример #3
0
        private async Task DecodeImage(IBoundedQueue <T> queue)
        {
            MemoryStream data;

            while ((data = await dataToParse.DequeueOrDefault()) != null)
            {
                var bmp         = new Bitmap(data);
                var width       = bmp.Width;
                var height      = bmp.Height;
                var pixelFormat = bmp.PixelFormat;

                var bmp1Data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat);
                var length   = bmp1Data.Stride * bmp1Data.Height;

                byte[] bmp1Bytes = new byte[length];
                Marshal.Copy(bmp1Data.Scan0, bmp1Bytes, 0, length);

                await queue.Enqueue(factory.FromBytes(width, height, bmp1Bytes));

                bmp.UnlockBits(bmp1Data);
                bmp.Dispose();
                data.Dispose();
                logger.NotifyFillstate(dataToParse.Count, "ParseBuffer");
            }
        }
Пример #4
0
        public async override Task Produce(IBoundedQueue <T> queue)
        {
            foreach (var filename in filenames)
            {
                try
                {
                    logger.NotifyFillstate(queue.Count, this.GetType().Name);
                    var bmp1        = new Bitmap(filename);
                    var height      = bmp1.Height;
                    var width       = bmp1.Width;
                    var pixelFormat = bmp1.PixelFormat;

                    var bmp1Data = bmp1.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat);
                    var length   = bmp1Data.Stride * bmp1Data.Height;

                    byte[] bmp1Bytes = new byte[length];
                    Marshal.Copy(bmp1Data.Scan0, bmp1Bytes, 0, length);
                    var image = factory.FromBytes(width, height, bmp1Bytes);
                    await queue.Enqueue(image);

                    bmp1.UnlockBits(bmp1Data);
                    bmp1.Dispose();
                }
                catch (Exception e) { Console.WriteLine(e); }
            }
            queue.CompleteAdding();
        }
Пример #5
0
 public async override Task Produce(IBoundedQueue <T> queue)
 {
     for (int i = 0; i < count; i++)
     {
         logger.NotifyFillstate(i, "TestData");
         var data = new byte[data1.Length];
         ((i % 2 == 0) ? data1 : data2).CopyTo(data, 0);
         await queue.Enqueue(factory.FromBytes(width, height, data, format));
     }
     queue.CompleteAdding();
 }
Пример #6
0
        private async Task ReadFromDisk()
        {
            int i = 0;

            foreach (var filename in filenames)
            {
                try
                {
                    logger.NotifyFillstate(dataToParse.Count, "ReadBuffer");
                    logger.NotifyFillstate(i, "FilesRead");
                    await dataToParse.Enqueue(new MemoryStream(File.ReadAllBytes(filename), false));

                    i++;
                }
                catch (Exception e) { Console.WriteLine(e); }
            }
            dataToParse.CompleteAdding();
        }
Пример #7
0
        public async override Task Produce(IBoundedQueue <T> queue)
        {
            var bytesToRead = Width * Height * Image.GetPixelFormatSize(Format);

            while (this.InputStream.CanRead)
            {
                try
                {
                    var bm = new Bitmap(
                        Width,
                        Height,
                        Width,
                        Format,
                        Marshal.UnsafeAddrOfPinnedArrayElement(this.InputStream.ReadBytes(bytesToRead), 0));

                    await queue.Enqueue(factory.FromImage(bm));
                }
                catch (Exception e) { logger.LogException(e); }
            }
            queue.CompleteAdding();
        }