示例#1
0
        private static VectorFormat AddVectorFormat(SurfaceFormat surfaceFormat, params Type[] vectorTypes)
        {
            var vectorInfos  = vectorTypes.Select(x => VectorType.Get(x));
            var vectorFormat = new VectorFormat(surfaceFormat, vectorInfos);

            if (!VectorFormatBySurface.TryGetValue(surfaceFormat, out var bySurfaceSet))
            {
                bySurfaceSet = new HashSet <VectorFormat>();
                VectorFormatBySurface.TryAdd(surfaceFormat, bySurfaceSet);
                VectorFormatBySurfaceRO.TryAdd(surfaceFormat, bySurfaceSet.AsReadOnly());
            }
            bySurfaceSet.Add(vectorFormat);

            foreach (var vectorType in vectorTypes)
            {
                if (!VectorFormatsByType.TryGetValue(vectorType, out var byTypeSet))
                {
                    byTypeSet = new HashSet <VectorFormat>();
                    VectorFormatsByType.TryAdd(vectorType, byTypeSet);
                    VectorFormatsByTypeRO.TryAdd(vectorType, byTypeSet.AsReadOnly());
                }
                byTypeSet.Add(vectorFormat);
            }

            return(vectorFormat);
        }
        protected Image(Size size, bool zeroFill) : base(VectorType.Get <TPixel>(), size)
        {
            var memory     = new UnmanagedMemory <TPixel>(size.Width * size.Height, zeroFill);
            int byteStride = size.Width * PixelType.ElementSize;

            _buffer = new PixelBuffer(memory, byteStride, leaveOpen: false);
        }
        public static unsafe Bitmap ToBitmap(
            this IReadOnlyPixelBuffer pixels,
            Rectangle?sourceRectangle = null)
        {
            var srcRect = sourceRectangle ?? pixels.GetBounds();

            ImagingArgumentGuard.AssertRectangleInSource(pixels, srcRect, nameof(sourceRectangle));

            var srcType       = pixels.PixelType;
            var dstType       = VectorType.Get <Bgra32>();
            var convertPixels = Imaging.Image.GetConvertPixelsDelegate(srcType, dstType);
            int width         = srcRect.Width;
            int height        = srcRect.Height;

            var bitmap  = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            var bmpRect = new System.Drawing.Rectangle(0, 0, width, height);
            var bmpData = bitmap.LockBits(bmpRect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            try
            {
                for (int y = 0; y < bmpData.Height; y++)
                {
                    int srcX   = srcRect.X;
                    int srcY   = srcRect.Y + y;
                    int dstX   = 0;
                    int dstY   = y;
                    var dstPtr = (byte *)bmpData.Scan0 + dstY * bmpData.Stride;

                    var srcRow = pixels.GetPixelByteRowSpan(srcY)[srcX..];
示例#4
0
        public static Image <TPixel> LoadPixelData <TPixel>(
            ReadOnlySpan <byte> pixelData, Rectangle sourceRectangle, int?byteStride)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            var type  = VectorType.Get <TPixel>();
            var image = LoadPixelData(type, type, pixelData, sourceRectangle, byteStride);

            return((Image <TPixel>)image);
        }
        public Image(PixelBuffer buffer, Size size) : base(VectorType.Get <TPixel>(), size)
        {
            if (buffer.IsEmpty)
            {
                throw new ArgumentEmptyException(nameof(buffer));
            }

            _buffer = buffer;
        }
        public static (bool Depth, bool Components, VectorType?Type) TryGetVectorType(
            int components,
            int depth)
        {
            // Note: 32bit depth means floating-point

            (bool Depth, bool Components, VectorType? Type)tuple = (true, true, null);

            switch (components)
            {
            case 1:
                tuple.Type =
                    depth == 8 ? VectorType.Get <Gray8>() :
                    depth == 16 ? VectorType.Get <Gray16>() :
                    depth == 32 ? VectorType.Get <GrayF>() :
                    null;
                break;

            case 2:
                tuple.Type =
                    depth == 8 ? VectorType.Get <GrayAlpha16>() :
                    depth == 16 ? VectorType.Get <GrayAlpha32>() :
                    //TODO: depth == 32 ? VectorTypeInfo.Get<?>() :
                    null;
                break;

            case 3:
                tuple.Type =
                    depth == 8 ? VectorType.Get <Framework.Vectors.Rgb24>() :
                    depth == 16 ? VectorType.Get <Framework.Vectors.Rgb48>() :
                    depth == 32 ? VectorType.Get <RgbVector>() :
                    null;
                break;

            case 4:
                tuple.Type =
                    depth == 8 ? VectorType.Get <Color>() :
                    depth == 16 ? VectorType.Get <Rgba64>() :
                    depth == 32 ? VectorType.Get <RgbaVector>() :
                    null;
                break;

            default:
                tuple.Components = false;
                break;
            }

            if (tuple.Type == null)
            {
                tuple.Depth = false;
            }

            return(tuple);
        }
示例#7
0
        public static Image <TPixel>?Load <TPixel>(
            IImagingConfig config,
            Stream stream,
            DecoderOptions?decoderOptions = null,
            ImagingProgressCallback <IImageDecoder>?onProgress = null,
            CancellationToken cancellationToken = default)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            var preferredType = VectorType.Get <TPixel>();

            var image = Load(
                config, stream, preferredType, decoderOptions, onProgress, cancellationToken);

            return((Image <TPixel>?)image);
        }
        public static VectorType GetTypeByComp(int components, int depth)
        {
            Exception GetDepthException() => new ArgumentOutOfRangeException(nameof(depth));

            switch (components)
            {
            case 1:
                if (depth == 8)
                {
                    return(VectorType.Get <Alpha8>());
                }
                throw GetDepthException();

            case 2:
                if (depth == 8)
                {
                    return(VectorType.Get <GrayAlpha16>());
                }
                throw GetDepthException();

            case 3:
                if (depth == 8)
                {
                    return(VectorType.Get <Rgb24>());
                }
                throw GetDepthException();

            case 4:
                if (depth == 8)
                {
                    return(VectorType.Get <Color>());
                }
                throw GetDepthException();
            }
            throw new ArgumentOutOfRangeException(nameof(components));
        }