public void Encode(
            IReadOnlyPixelRows image,
            CancellationToken cancellationToken = default)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            AssertCanEncodeImage(image);

            var pixelInfo = image.PixelType.ComponentInfo;

            // TODO: change components to dynamic/controlled (maybe in encoder options)
            bool imageHasAlpha        = pixelInfo.HasComponentType(VectorComponentChannel.Alpha);
            bool encoderSupportsAlpha = true; // TODO: implement (encoderState.Encoder.SupportedComponents HasAttribute();)
            int  colors     = 3;              // TODO: also implement grayscale
            int  alpha      = (imageHasAlpha && encoderSupportsAlpha) ? 1 : 0;
            int  components = colors + alpha;

            int imageMaxDepth   = pixelInfo.MaxBitDepth;
            int encoderMinDepth = 8; // TODO: implement
            int encoderMaxDepth = 8; // TODO: implement
            int variableDepth   = Math.Max(encoderMinDepth, imageMaxDepth);
            int depth           = Math.Min(encoderMaxDepth, variableDepth);

            var provider = new PixelRowProvider(image, components, depth, cancellationToken);

            Write(provider);

            FrameIndex++;
        }
 protected void AssertCanEncodeImage(IReadOnlyPixelRows image)
 {
     if (!CanEncodeImage(image))
     {
         throw new ImagingException("Image may not be written in the current state of the encoder.");
     }
 }
 private static void ReflectionLoadPixelRows <TPixelFrom, TPixelTo>(
     IReadOnlyPixelRows pixels, IPixelBuffer destination, Rectangle?sourceRectangle)
     where TPixelFrom : unmanaged, IPixel
     where TPixelTo : unmanaged, IPixel <TPixelTo>
 {
     LoadPixels <TPixelFrom, TPixelTo>(pixels, (IPixelBuffer <TPixelTo>)destination, sourceRectangle);
 }
 public static Image <TPixel> ProcessRows <TPixel>(
     this IReadOnlyPixelRows <TPixel> pixels,
     PixelRowsProcessorCallback <TPixel> processor)
     where TPixel : unmanaged, IPixel <TPixel>
 {
     return(ProcessRows(pixels, ImagingConfig.Default, processor));
 }
Exemple #5
0
 public static IReadOnlyPixelRows ProjectRows(this IReadOnlyPixelRows pixels,
                                              ImagingConfig imagingConfig, PixelRowsProjectorCallback projector)
 {
     if (projector == null)
     {
         throw new ArgumentNullException(nameof(projector));
     }
     return(projector.Invoke(new ReadOnlyPixelRowsContext(imagingConfig, pixels)));
 }
 public static Image <TPixel> ProcessRows <TPixel>(
     this IReadOnlyPixelRows <TPixel> pixels,
     ImagingConfig imagingConfig, PixelRowsProcessorCallback <TPixel> processor)
     where TPixel : unmanaged, IPixel <TPixel>
 {
     if (processor == null)
     {
         throw new ArgumentNullException(nameof(processor));
     }
     return(processor.Invoke(new ReadOnlyPixelRowsContext <TPixel>(imagingConfig, pixels)));
 }
        public virtual bool CanEncodeImage(IReadOnlyPixelRows image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            AssertNotDisposed();

            return(FrameIndex == 0);
        }
Exemple #8
0
        public PixelRowProvider(IReadOnlyPixelRows pixels, int components)
        {
            _pixels    = pixels;
            Components = components;
            _rowBuffer = new byte[pixels.Size.Width * pixels.ElementSize];

            if (!_transform32DelegateCache.TryGetValue(pixels.PixelType, out _transform32))
            {
                _transform32 = CreateTransform <Transform32Delegate>(nameof(Transform32), pixels.PixelType);
                _transform32DelegateCache.TryAdd(pixels.PixelType, _transform32);
            }
        }
Exemple #9
0
 public static void Save(
     this IReadOnlyPixelRows image,
     string filePath,
     ImageFormat?format            = null,
     EncoderOptions?encoderOptions = null,
     ImagingProgressCallback <IImageEncoder>?onProgress = null,
     CancellationToken cancellationToken = default)
 {
     Save(
         image, ImagingConfig.Default, filePath, format,
         encoderOptions, onProgress, cancellationToken);
 }
Exemple #10
0
        public static void Save(
            this IReadOnlyPixelRows image,
            IImagingConfig imagingConfig,
            Stream output,
            ImageFormat format,
            EncoderOptions?encoderOptions = null,
            ImagingProgressCallback <IImageEncoder>?onProgress = null,
            CancellationToken cancellationToken = default)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            Save(
                new[] { image }, imagingConfig, output, format,
                encoderOptions, onProgress, cancellationToken);
        }
Exemple #11
0
        public static void Save(
            this IReadOnlyPixelRows image,
            IImagingConfig imagingConfig,
            string filePath,
            ImageFormat?format            = null,
            EncoderOptions?encoderOptions = null,
            ImagingProgressCallback <IImageEncoder>?onProgress = null,
            CancellationToken cancellationToken = default)
        {
            if (format == null)
            {
                format = ImageFormat.GetByPath(filePath)[0];
            }

            Save(
                new[] { image }, imagingConfig, filePath, format,
                encoderOptions, onProgress, cancellationToken);
        }
Exemple #12
0
        /// <summary>
        /// Creates a mouse cursor from the specified pixels.
        /// </summary>
        /// <param name="pixels">Pixels to use as the cursor image.</param>
        /// <param name="origin">A point in the pixels that is used as the cursor position.</param>
        /// <param name="sourceRectangle">Optional part of the image to use as the cursor.</param>
        public static MouseCursor FromPixels(
            IReadOnlyPixelRows pixels, Point origin, Rectangle?sourceRectangle = null)
        {
            if (pixels == null)
            {
                throw new ArgumentNullException(nameof(pixels));
            }

            Rectangle rect = sourceRectangle ?? pixels.GetBounds();

            if (!pixels.GetBounds().Contains(rect))
            {
                throw new ArgumentOutOfRangeException(
                          nameof(sourceRectangle), "The source rectangle is outside the pixel buffer.");
            }

            IReadOnlyPixelMemory <Color>?pixelBuffer = null;

            try
            {
                if (rect.Position == Point.Zero &&
                    pixels is IReadOnlyPixelMemory <Color> rgbaMemory &&
                    rgbaMemory.IsPaddedPixelContiguous())
                {
                    pixelBuffer = rgbaMemory;
                }
                else
                {
                    pixelBuffer = Image.LoadPixels <Color>(pixels.ProjectRows(x => x.Crop(rect)));
                }

                return(PlatformFromPixels(pixelBuffer, rect.Width, rect.Height, origin));
            }
            finally
            {
                pixelBuffer?.Dispose();
            }
        }
        public PixelRowProvider(IReadOnlyPixelRows pixelRows, int components, int depth, CancellationToken cancellationToken)
        {
            _pixelRows        = pixelRows ?? throw new ArgumentNullException(nameof(pixelRows));
            CancellationToken = cancellationToken;
            Components        = components;
            Depth             = depth;

            // call early to check args
            var resultType = GetTypeByComp(Components, Depth);

            if (_pixelRows is IReadOnlyPixelBuffer buffer)
            {
                _pixelBuffer = buffer;
                _rowBuffer   = null;
            }
            else
            {
                _pixelBuffer = null;
                _rowBuffer   = new byte[pixelRows.Width * pixelRows.ElementSize];
            }

            _convertPixels = Image.GetConvertPixelsDelegate(pixelRows.PixelType, resultType);
        }
        public static void LoadPixels <TPixelFrom, TPixelTo>(
            IReadOnlyPixelRows pixels, IPixelBuffer <TPixelTo> destination, Rectangle?sourceRectangle = null)
            where TPixelFrom : unmanaged, IPixel
            where TPixelTo : unmanaged, IPixel <TPixelTo>
        {
            if (pixels == null)
            {
                throw new ArgumentNullException(nameof(pixels));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            var rect = sourceRectangle ?? pixels.GetBounds();

            ImagingArgumentGuard.AssertNonEmptyRectangle(rect, nameof(sourceRectangle));

            Span <byte> rowByteBuffer = stackalloc byte[4096];
            var         rowBuffer     = MemoryMarshal.Cast <byte, TPixelFrom>(rowByteBuffer);

            for (int y = 0; y < rect.Height; y++)
            {
                var dstRow = destination.GetPixelRowSpan(y);

                int offsetX = 0;
                do
                {
                    int left  = rect.Width - offsetX;
                    int count = Math.Min(rowBuffer.Length, left);
                    var slice = rowBuffer.Slice(0, count);

                    pixels.GetPixelByteRow(rect.X + offsetX, rect.Y + y, MemoryMarshal.AsBytes(slice));

                    ConvertPixels(slice, dstRow);
                    dstRow   = dstRow[count..];
Exemple #15
0
 public ReadOnlyPixelRowsContext(IImagingConfig imagingConfig, IReadOnlyPixelRows pixels)
 {
     ImagingConfig = imagingConfig ?? throw new ArgumentNullException(nameof(imagingConfig));
     Pixels        = pixels ?? throw new ArgumentNullException(nameof(pixels));
 }
Exemple #16
0
        public override bool CanEncodeImage(IReadOnlyPixelRows image)
        {
            AssertNotDisposed();

            return(true);
        }
 public static Image <TPixel> ToImage <TPixel>(
     this IReadOnlyPixelRows source, Rectangle?sourceRectangle = null)
     where TPixel : unmanaged, IPixel <TPixel>
 {
     return(Image.LoadPixels <TPixel>(source, sourceRectangle));
 }
Exemple #18
0
 public ReadOnlyCropRows(IReadOnlyPixelRows pixels, Rectangle sourceRectangle)
 {
     Pixels          = pixels ?? throw new ArgumentNullException(nameof(pixels));
     SourceRectangle = sourceRectangle;
 }
Exemple #19
0
 public ReadOnlyPixelRowsContext(IImagingConfig imagingConfig, IReadOnlyPixelRows <TPixel> pixels) :
     base(imagingConfig, pixels)
 {
 }
 public static Image ProcessRows(
     this IReadOnlyPixelRows pixels,
     PixelRowsProcessorCallback processor)
 {
     return(ProcessRows(pixels, ImagingConfig.Default, processor));
 }