示例#1
0
        private static unsafe IImage ConvertHdrToBitmap(
            IList <float> hdr,
            int width,
            int height,
            float max)
        {
            var bitmap = new Rgba32Image(width, height);

            bitmap.Mutate((_, setHandler) => {
                var offset = 0;
                for (var y = 0; y < height; ++y)
                {
                    for (var x = 0; x < width; ++x)
                    {
                        var r = hdr[offset + 0] / max * 255;
                        var g = hdr[offset + 1] / max * 255;
                        var b = hdr[offset + 2] / max * 255;

                        // For some reason, alpha isn't used?
                        // TODO: How is this factored in?
                        var a = 255f; //hdrImage[offset + 3] / max * 255;

                        setHandler(x, y, (byte)r, (byte)g, (byte)b, (byte)a);
                    }
                }
            });

            return(bitmap);
        }
示例#2
0
        public static IImage Create1x1WithColor(Color color)
        {
            var bmp = new Rgba32Image(1, 1);

            bmp.Mutate((_, setHandler)
                       => setHandler(0, 0, color.R, color.G, color.B, color.A));
            return(bmp);
        }
示例#3
0
        public IImage ToBitmap()
        {
            ImageDataFormat imageDataFormat = (ImageDataFormat)null;

            switch ((byte)this.Header.Format)
            {
            case 0:
                imageDataFormat = ImageDataFormat.I4;
                break;

            case 1:
                imageDataFormat = ImageDataFormat.I8;
                break;

            case 2:
                imageDataFormat = ImageDataFormat.IA4;
                break;

            case 3:
                imageDataFormat = ImageDataFormat.IA8;
                break;

            case 4:
                imageDataFormat = ImageDataFormat.RGB565;
                break;

            case 5:
                imageDataFormat = ImageDataFormat.RGB5A3;
                break;

            case 6:
                imageDataFormat = ImageDataFormat.Rgba32;
                break;

            case 14:
                imageDataFormat = ImageDataFormat.Cmpr;
                break;
            }
            byte[] numArray = imageDataFormat.ConvertFrom(
                this.Data, (int)this.Header.Width, (int)this.Header.Height,
                (ProgressChangedEventHandler)null);

            var width  = this.Header.Width;
            var height = this.Header.Height;

            var bitmap = new Rgba32Image(width, height);

            bitmap.Mutate((_, setHandler) => {
                for (var y = 0; y < height; ++y)
                {
                    for (var x = 0; x < width; ++x)
                    {
                        var i = 4 * (y * width + x);

                        var b = numArray[i + 0];
                        var g = numArray[i + 1];
                        var r = numArray[i + 2];
                        var a = numArray[i + 3];

                        setHandler(x, y, r, g, b, a);
                    }
                }
            });

            return(bitmap);
        }
示例#4
0
        public IMipMap <IImage> Read(Stream stream)
        {
            using var pfimImage = Pfim.Pfim.FromStream(stream);

            return(MipMapUtil.From(
                       pfimImage.MipMaps.Select(pfimMipMap => {
                var mmWidth = pfimMipMap.Width;
                var mmHeight = pfimMipMap.Height;

                switch (pfimImage.Format)
                {
                case ImageFormat.Rgba32: {
                    var image = new Rgba32Image(mmWidth, mmHeight);
                    image.Mutate((_, setHandler) => {
                            for (var y = 0; y < mmHeight; ++y)
                            {
                                for (var x = 0; x < mmWidth; ++x)
                                {
                                    var i = pfimMipMap.DataOffset +
                                            4 * (y * mmWidth + x);

                                    var b = pfimImage.Data[i + 0];
                                    var g = pfimImage.Data[i + 1];
                                    var r = pfimImage.Data[i + 2];
                                    var a = pfimImage.Data[i + 3];

                                    setHandler(x, y, r, g, b, a);
                                }
                            }
                        });
                    return image as IImage;
                }

                case ImageFormat.Rgb24: {
                    var image = new Rgb24Image(mmWidth, mmHeight);
                    image.Mutate((_, setHandler) => {
                            for (var y = 0; y < mmHeight; ++y)
                            {
                                for (var x = 0; x < mmWidth; ++x)
                                {
                                    var i = pfimMipMap.DataOffset +
                                            3 * (y * mmWidth + x);

                                    var b = pfimImage.Data[i + 0];
                                    var g = pfimImage.Data[i + 1];
                                    var r = pfimImage.Data[i + 2];

                                    setHandler(x, y, r, g, b);
                                }
                            }
                        });
                    return image;
                }

                default:
                    throw new NotImplementedException(
                        $"Unsupported Pfim format: {pfimImage.Format}");
                }
            })
                       .ToList()));
        }