예제 #1
0
        public void Ctor_ShouldInstantiateImageFrame_When_CalledWithFormat()
        {
            var imageFrame = new ImageFrame(ImageFormat.Format.SBGRA, 640, 480);

            Assert.AreEqual(imageFrame.Format(), ImageFormat.Format.SBGRA);
            Assert.AreEqual(imageFrame.Width(), 640);
            Assert.AreEqual(imageFrame.Height(), 480);
            Assert.AreEqual(imageFrame.ChannelSize(), 1);
            Assert.AreEqual(imageFrame.NumberOfChannels(), 4);
            Assert.AreEqual(imageFrame.ByteDepth(), 1);
            Assert.AreEqual(imageFrame.WidthStep(), 640 * 4);
            Assert.AreEqual(imageFrame.PixelDataSize(), 640 * 480 * 4);
            Assert.AreEqual(imageFrame.PixelDataSizeStoredContiguously(), 640 * 480 * 4);
            Assert.False(imageFrame.IsEmpty());
            Assert.True(imageFrame.IsContiguous());
            Assert.True(imageFrame.IsAligned(16));
            Assert.AreNotEqual(imageFrame.MutablePixelData(), IntPtr.Zero);
        }
예제 #2
0
        public void Ctor_ShouldInstantiateImageFrame_When_CalledWithNoArguments()
        {
            var imageFrame = new ImageFrame();

            Assert.AreEqual(imageFrame.Format(), ImageFormat.Format.UNKNOWN);
            Assert.AreEqual(imageFrame.Width(), 0);
            Assert.AreEqual(imageFrame.Height(), 0);
            Assert.Throws <FormatException>(() => { imageFrame.ChannelSize(); });
            Assert.Throws <FormatException>(() => { imageFrame.NumberOfChannels(); });
            Assert.Throws <FormatException>(() => { imageFrame.ByteDepth(); });
            Assert.AreEqual(imageFrame.WidthStep(), 0);
            Assert.AreEqual(imageFrame.PixelDataSize(), 0);
            Assert.Throws <FormatException>(() => { imageFrame.PixelDataSizeStoredContiguously(); });
            Assert.True(imageFrame.IsEmpty());
            Assert.False(imageFrame.IsContiguous());
            Assert.False(imageFrame.IsAligned(16));
            Assert.AreEqual(imageFrame.MutablePixelData(), IntPtr.Zero);
        }
 public void Ctor_ShouldInstantiateImageFrame_When_CalledWithFormat()
 {
     using (var imageFrame = new ImageFrame(ImageFormat.Types.Format.Sbgra, 640, 480))
     {
         Assert.AreEqual(ImageFormat.Types.Format.Sbgra, imageFrame.Format());
         Assert.AreEqual(640, imageFrame.Width());
         Assert.AreEqual(480, imageFrame.Height());
         Assert.AreEqual(1, imageFrame.ChannelSize());
         Assert.AreEqual(4, imageFrame.NumberOfChannels());
         Assert.AreEqual(1, imageFrame.ByteDepth());
         Assert.AreEqual(640 * 4, imageFrame.WidthStep());
         Assert.AreEqual(640 * 480 * 4, imageFrame.PixelDataSize());
         Assert.AreEqual(640 * 480 * 4, imageFrame.PixelDataSizeStoredContiguously());
         Assert.False(imageFrame.IsEmpty());
         Assert.True(imageFrame.IsContiguous());
         Assert.True(imageFrame.IsAligned(16));
         Assert.AreNotEqual(IntPtr.Zero, imageFrame.MutablePixelData());
     }
 }
 public void Ctor_ShouldInstantiateImageFrame_When_CalledWithNoArguments()
 {
     using (var imageFrame = new ImageFrame())
     {
         Assert.AreEqual(ImageFormat.Types.Format.Unknown, imageFrame.Format());
         Assert.AreEqual(0, imageFrame.Width());
         Assert.AreEqual(0, imageFrame.Height(), 0);
         Assert.AreEqual(0, imageFrame.ChannelSize());
         Assert.AreEqual(0, imageFrame.NumberOfChannels());
         Assert.AreEqual(0, imageFrame.ByteDepth());
         Assert.AreEqual(0, imageFrame.WidthStep());
         Assert.AreEqual(0, imageFrame.PixelDataSize());
         Assert.AreEqual(0, imageFrame.PixelDataSizeStoredContiguously());
         Assert.True(imageFrame.IsEmpty());
         Assert.False(imageFrame.IsContiguous());
         Assert.False(imageFrame.IsAligned(16));
         Assert.AreEqual(IntPtr.Zero, imageFrame.MutablePixelData());
     }
 }
        private static bool TryReadVec32f2(ImageFrame imageFrame, Color32[] colors)
        {
            var width  = imageFrame.Width();
            var height = imageFrame.Height();
            var length = width * height;

            if (colors.Length != length)
            {
                Logger.LogWarning($"The length of colors ({colors.Length}) does not equal {width} * {height} = {length}");
                return(false);
            }

            var widthStep = imageFrame.WidthStep();
            var byteDepth = imageFrame.ByteDepth();

            unsafe
            {
                fixed(Color32 *dest = colors)
                {
                    // NOTE: We cannot assume that the pixel data is aligned properly.
                    var pLine = (byte *)imageFrame.MutablePixelData();
                    // The first element is at top-left (the image is not flipped at all).
                    var pDest = dest + (width * (height - 1));

                    for (var i = 0; i < height; i++)
                    {
                        var pSrc = (float *)pLine;
                        for (var j = 0; j < width; j++)
                        {
                            var x         = *pSrc++;
                            var y         = *pSrc++;
                            var magnitude = Mathf.Sqrt((x * x) + (y * y));
                            var angle     = Mathf.Atan2(y, x);
                            *   pDest++   = UnityEngine.Color.HSVToRGB(magnitude, 1.0f, angle);
                        }
                        pLine += widthStep;
                        pDest -= 2 * width;
                    }
                }
            }

            return(true);
        }
        private static bool TryReadSrgba64(ImageFrame imageFrame, Color32[] colors)
        {
            var width  = imageFrame.Width();
            var height = imageFrame.Height();
            var length = width * height;

            if (colors.Length != length)
            {
                Logger.LogWarning($"The length of colors ({colors.Length}) does not equal {width} * {height} = {length}");
                return(false);
            }

            var widthStep = imageFrame.WidthStep();
            var byteDepth = imageFrame.ByteDepth();

            unsafe
            {
                fixed(Color32 *dest = colors)
                {
                    // NOTE: We cannot assume that the pixel data is aligned properly.
                    var pLine = (byte *)imageFrame.MutablePixelData();
                    // The first element is at top-left (the image is not flipped at all).
                    var pDest = dest + (width * (height - 1));

                    for (var i = 0; i < height; i++)
                    {
                        var pSrc = (ushort *)pLine;
                        for (var j = 0; j < width; j++)
                        {
                            var r       = *pSrc++;
                            var g       = *pSrc++;
                            var b       = *pSrc++;
                            var a       = *pSrc++;
                            *   pDest++ = new Color32((byte)(r / 255), (byte)(g / 255), (byte)(b / 255), (byte)(a / 255));
                        }
                        pLine += widthStep;
                        pDest -= 2 * width;
                    }
                }
            }

            return(true);
        }
        private static bool TryReadChannel <TSrc, TDst>(ImageFrame imageFrame, int channelCount, int channelNumber, ChannelTransformer <TSrc, TDst> transformer,
                                                        TDst[] channelData, bool isHorizontallyFlipped, bool isVerticallyFlipped) where TSrc : unmanaged where TDst : unmanaged
        {
            var width  = imageFrame.Width();
            var height = imageFrame.Height();
            var length = width * height;

            if (channelData.Length != length)
            {
                Logger.LogWarning($"The length of channelData ({channelData.Length}) does not equal {width} * {height} = {length}");
                return(false);
            }

            var widthStep = imageFrame.WidthStep();
            var byteDepth = imageFrame.ByteDepth();

            unsafe
            {
                fixed(TDst *dest = channelData)
                {
                    // NOTE: We cannot assume that the pixel data is aligned properly.
                    var pLine = (byte *)imageFrame.MutablePixelData();

                    pLine += byteDepth * channelNumber;

                    if (isVerticallyFlipped)
                    {
                        if (isHorizontallyFlipped)
                        {
                            // The first element is at bottom-right.
                            var pDest = dest + width - 1;

                            for (var i = 0; i < height; i++)
                            {
                                var pSrc = (TSrc *)pLine;
                                for (var j = 0; j < width; j++)
                                {
                                    *pDest-- = transformer(*pSrc);
                                    pSrc += channelCount;
                                }
                                pLine += widthStep;
                                pDest += 2 * width;
                            }
                        }
                        else
                        {
                            // The first element is at bottom-left.
                            // NOTE: In the Unity coordinate system, the image can be considered as not flipped.
                            var pDest = dest;

                            for (var i = 0; i < height; i++)
                            {
                                var pSrc = (TSrc *)pLine;
                                for (var j = 0; j < width; j++)
                                {
                                    *pDest++ = transformer(*pSrc);
                                    pSrc += channelCount;
                                }
                                pLine += widthStep;
                            }
                        }
                    }
                    else
                    {
                        if (isHorizontallyFlipped)
                        {
                            // The first element is at top-right.
                            var pDest = dest + length - 1;

                            for (var i = 0; i < height; i++)
                            {
                                var pSrc = (TSrc *)pLine;
                                for (var j = 0; j < width; j++)
                                {
                                    *pDest-- = transformer(*pSrc);
                                    pSrc += channelCount;
                                }
                                pLine += widthStep;
                            }
                        }
                        else
                        {
                            // The first element is at top-left (the image is not flipped at all).
                            var pDest = dest + (width * (height - 1));

                            for (var i = 0; i < height; i++)
                            {
                                var pSrc = (TSrc *)pLine;
                                for (var j = 0; j < width; j++)
                                {
                                    *pDest++ = transformer(*pSrc);
                                    pSrc += channelCount;
                                }
                                pLine += widthStep;
                                pDest -= 2 * width;
                            }
                        }
                    }
                }
            }

            return(true);
        }