Exemple #1
0
        internal static Image OnLoaded(Image image, ImageMetadata metadata, IList <Color> palette)
        {
            // try to fix invalid image resolution
            FixResolution();

            // apply orientation
            ApplyOrientation();

            // determine whether bitmap should be inverted
            bool invert = false;

            if (image.BitsPerPixel <= 8 && palette != null)
            {
                // convert color palette
                if (!BitmapExtensions.IsGrayScalePalette(image.BitsPerPixel, palette, out bool invertedPalette))
                {
                    return(image.ApplyPalette(palette));
                }

                invert = invertedPalette;

                // remove palette
                metadata?.RemovePropertyItems(x => x.Id == (int)TIFFField.PhotometricInterpretation ||
                                              x.Id == (int)TIFFField.ColorMap);
            }
            else
            {
                invert |= ApplyPhotometricInterpretation();
            }

            if (invert)
            {
                image.Not(image);
            }

            return(image);

            // private methods
            void FixResolution()
            {
                (float w, float h)[] standardResolutions = new (float, float)[]
Exemple #2
0
        internal static (Image Image, ImageMetadata Metadata) FromBitmapFrame(this BitmapFrame bitmapFrame)
        {
            if (bitmapFrame == null)
            {
                throw new ArgumentNullException(nameof(bitmapFrame));
            }

            int?xres = null;
            int?yres = null;

            // load metadata
            ImageMetadata metadata = null;

            if (bitmapFrame.Metadata is BitmapMetadata bitmapMetadata)
            {
                metadata = BitmapExtensions.ReadBitmapMetadata(bitmapMetadata);

                // reset resolution to 200 dpi for images that do not have resolution tag
                if (bitmapFrame.Decoder is TiffBitmapDecoder)
                {
                    if (!metadata.HasPropertyItem((int)TIFFField.XResolution))
                    {
                        xres = 200;
                    }

                    if (!metadata.HasPropertyItem((int)TIFFField.YResolution))
                    {
                        yres = 200;
                    }
                }

                // remove standard tags
                metadata.RemovePropertyItems(x => x.Id == (int)TIFFField.ImageWidth ||
                                             x.Id == (int)TIFFField.ImageLength ||
                                             x.Id == (int)TIFFField.XResolution ||
                                             x.Id == (int)TIFFField.YResolution ||
                                             x.Id == (int)TIFFField.BitsPerSample);
            }

            // verify palette presence
            System.Windows.Media.PixelFormat pixelFormat = bitmapFrame.Format;
            if ((pixelFormat == PixelFormats.Indexed1 ||
                 pixelFormat == PixelFormats.Indexed2 ||
                 pixelFormat == PixelFormats.Indexed4 ||
                 pixelFormat == PixelFormats.Indexed8) &&
                bitmapFrame.Palette?.Colors == null)
            {
                throw new InvalidOperationException(Properties.Resources.E_UnsupportedImageFormat);
            }

            // load image
            Image image = new Image(
                bitmapFrame.PixelWidth,
                bitmapFrame.PixelHeight,
                pixelFormat.BitsPerPixel,
                xres.GetValueOrDefault((int)(bitmapFrame.DpiX + 0.5f)),
                yres.GetValueOrDefault((int)(bitmapFrame.DpiY + 0.5f)));

            int strideInBytes = (((image.Width * image.BitsPerPixel) + 31) & ~31) >> 3;

            uint[] bits = new uint[image.Height * strideInBytes / sizeof(uint)];
            bitmapFrame.CopyPixels(bits, strideInBytes, 0);

            unsafe
            {
                fixed(uint *src = bits)
                {
                    fixed(ulong *dst = image.Bits)
                    {
                        Arrays.CopyStrides(
                            image.Height,
                            new IntPtr(src),
                            strideInBytes,
                            new IntPtr(dst),
                            image.Stride8);
                    }
                }
            }

            if (image.BitsPerPixel < 8)
            {
                Vectors.SwapBits(image.Bits.Length, image.BitsPerPixel, image.Bits, 0);
            }

            // special case for BitmapFrame BlackWhite pixel format
            if (bitmapFrame.Format == PixelFormats.BlackWhite)
            {
                image.Not(image);
                metadata.RemovePropertyItem((int)TIFFField.PhotometricInterpretation);
            }

            return(
                Image.OnLoaded(
                    image,
                    metadata,
                    bitmapFrame.Palette?.Colors?.Select(x => Color.FromArgb(x.A, x.R, x.G, x.B)).ToArray()),
                metadata);
        }